Skip to content

Commit

Permalink
Merge pull request #11234 from vegaprotocol/feature/11217
Browse files Browse the repository at this point in the history
feat: allow market proposals to override risk factors
  • Loading branch information
jeremyletang authored May 15, 2024
2 parents 1262a7b + 3da8755 commit 4a5ca8b
Show file tree
Hide file tree
Showing 13 changed files with 1,192 additions and 469 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

- [11209](https://github.com/vegaprotocol/vega/issues/11209) - Publish ongoing games data.
- [11196](https://github.com/vegaprotocol/vega/issues/11196) - Add an active field in the price monitoring bounds payload.
- [11211](https://github.com/vegaprotocol/vega/issues/11211) - Liquidation engine includes `vAMM` shapes as available volume.
- [11217](https://github.com/vegaprotocol/vega/issues/11217) - Allow market proposals to override risk factors.

### 🐛 Fixes

- [11066](https://github.com/vegaprotocol/vega/issues/11066) - Ensure vesting statistics match vesting accounts numbers.
- [11279](https://github.com/vegaprotocol/vega/issues/11279) - Handle properly the case of multiple transfers for the same game id.
- [11279](https://github.com/vegaprotocol/vega/issues/11279) - Handle properly the case of multiple transfers for the same game id.


## 0.76.1
Expand Down Expand Up @@ -67,7 +69,6 @@
- [11143](https://github.com/vegaprotocol/vega/issues/11143) - Add support for new asset proposal in batch governance proposal
- [11182](https://github.com/vegaprotocol/vega/issues/11182) - Remove reduce only restriction on spot markets stop orders.
- [11153](https://github.com/vegaprotocol/vega/issues/11153) - Add check on start-up that bridge `RPC-endpoints` are functional.
- [11211](https://github.com/vegaprotocol/vega/issues/11211) - Liquidation engine includes `vAMM` shapes as available volume.

### 🐛 Fixes

Expand Down
54 changes: 54 additions & 0 deletions commands/proposal_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,33 @@ func checkNewLogNormalRiskParameters(params *protoTypes.NewMarketConfiguration_L
return errs.FinalAddForProperty("new_market.changes.risk_parameters.log_normal.params.r", errors.New("must be between [-1,1]"))
}

// if not nil, both short and long must be specified and correct
if params.LogNormal.RiskFactorOverride != nil {
short := params.LogNormal.RiskFactorOverride.Short
long := params.LogNormal.RiskFactorOverride.Long
if len(short) <= 0 {
return errs.FinalAddForProperty("new_market.changes.risk_parameters.log_normal.risk_factor_override.short", ErrIsRequired)
}

d, err := num.DecimalFromString(short)
if err != nil {
errs.AddForProperty("new_market.changes.risk_parameters.log_normal.risk_factor_override.short", ErrIsNotValidNumber)
} else if d.LessThanOrEqual(num.DecimalZero()) {
errs.AddForProperty("new_market.changes.risk_parameters.log_normal.risk_factor_override.short", ErrMustBePositive)
}

if len(long) <= 0 {
return errs.FinalAddForProperty("new_market.changes.risk_parameters.log_normal.risk_factor_override.long", ErrIsRequired)
}

d, err = num.DecimalFromString(long)
if err != nil {
errs.AddForProperty("new_market.changes.risk_parameters.log_normal.risk_factor_override.long", ErrIsNotValidNumber)
} else if d.LessThanOrEqual(num.DecimalZero()) {
errs.AddForProperty("new_market.changes.risk_parameters.log_normal.risk_factor_override.long", ErrMustBePositive)
}
}

return errs
}

Expand Down Expand Up @@ -2314,6 +2341,33 @@ func checkUpdateLogNormalRiskParameters(params *protoTypes.UpdateMarketConfigura
return errs.FinalAddForProperty("update_market.changes.risk_parameters.log_normal.params.r", ErrIsNotValidNumber)
}

// if not nil, both short and long must be specified and correct
if params.LogNormal.RiskFactorOverride != nil {
short := params.LogNormal.RiskFactorOverride.Short
long := params.LogNormal.RiskFactorOverride.Long
if len(short) <= 0 {
errs.AddForProperty("update_market.changes.risk_parameters.log_normal.risk_factor_override.short", ErrIsRequired)
}

d, err := num.DecimalFromString(short)
if err != nil {
errs.AddForProperty("update_market.changes.risk_parameters.log_normal.risk_factor_override.short", ErrIsNotValidNumber)
} else if d.LessThanOrEqual(num.DecimalZero()) {
errs.AddForProperty("update_market.changes.risk_parameters.log_normal.risk_factor_override.short", ErrMustBePositive)
}

if len(long) <= 0 {
errs.AddForProperty("update_market.changes.risk_parameters.log_normal.risk_factor_override.long", ErrIsRequired)
}

d, err = num.DecimalFromString(long)
if err != nil {
errs.AddForProperty("update_market.changes.risk_parameters.log_normal.risk_factor_override.long", ErrIsNotValidNumber)
} else if d.LessThanOrEqual(num.DecimalZero()) {
errs.AddForProperty("update_market.changes.risk_parameters.log_normal.risk_factor_override.long", ErrMustBePositive)
}
}

return errs
}

Expand Down
125 changes: 125 additions & 0 deletions commands/proposal_submission_new_market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func TestCheckProposalSubmissionForNewMarket(t *testing.T) {
t.Run("Submitting a new spot market with invalid liquidity fee settings", testLiquidityFeeSettingsSpot)
t.Run("Submitting a new market with invalid mark price configuration ", testCompositePriceConfiguration)
t.Run("Submitting a new market with invalid tick size fails and with valid tick size succeeds", testNewMarketTickSize)

t.Run("Log Normal risk factor overrides", testNewLogNormalRiskParametersChangeSubmissionWithOverrides)
}

type tickSizeCase struct {
Expand Down Expand Up @@ -2497,6 +2499,129 @@ func testNewLogNormalRiskParametersChangeSubmissionWithLogNormalRiskParametersSu
assert.NotContains(t, err.Get("proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal"), commands.ErrIsRequired)
}

func testNewLogNormalRiskParametersChangeSubmissionWithOverrides(t *testing.T) {
cases := []struct {
desc string
get string
override *vegapb.RiskFactorOverride
err string
}{
{
desc: "no override is valid",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override",
override: nil,
err: "",
},
{
desc: "non nil, but with empty short",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{},
err: "is required",
},
{
desc: "non nil, but with bad value short",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{
Short: "asd",
},
err: "is not a valid number",
},
{
desc: "non nil, but with negative short",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{
Short: "-1",
},
err: "must be positive",
},
{
desc: "non nil, but with 0 short",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{
Short: "0",
},
err: "must be positive",
},
{
desc: "non nil, but with empty long",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
},
err: "is required",
},
{
desc: "non nil, but with bad value long",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "asd",
},
err: "is not a valid number",
},
{
desc: "non nil, but with negative long",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "-1",
},
err: "must be positive",
},
{
desc: "non nil, but with 0 long",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "0",
},
err: "must be positive",
},
{
desc: "both valid",
get: "proposal_submission.terms.change.new_market.changes.risk_parameters.log_normal.risk_factor_override",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "0.1",
},
err: "",
},
}

for _, c := range cases {
err := checkProposalSubmission(&commandspb.ProposalSubmission{
Terms: &vegapb.ProposalTerms{
Change: &vegapb.ProposalTerms_NewMarket{
NewMarket: &vegapb.NewMarket{
Changes: &vegapb.NewMarketConfiguration{
RiskParameters: &vegapb.NewMarketConfiguration_LogNormal{
LogNormal: &vegapb.LogNormalRiskModel{
RiskAversionParameter: 0.1,
Tau: 1,
Params: &vegapb.LogNormalModelParams{
Mu: 0,
Sigma: 0.1,
R: 0,
},
RiskFactorOverride: c.override,
},
},
},
},
},
},
})

if len(c.err) <= 0 {
// no error
assert.Len(t, err.Get(c.get), 0, c.desc)
continue
}

assert.Contains(t, err.Get(c.get), errors.New(c.err), "test: %v, err: %v", c.desc, err)
}
}

func testNewLogNormalRiskParametersChangeSubmissionWithoutParamsFails(t *testing.T) {
err := checkProposalSubmission(&commandspb.ProposalSubmission{
Terms: &vegapb.ProposalTerms{
Expand Down
124 changes: 124 additions & 0 deletions commands/proposal_submission_update_market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestCheckProposalSubmissionForUpdateMarket(t *testing.T) {
t.Run("Submitting a market update with invalid intenal composite price configuration", testUpdatePerpsMarketChangeSubmissionWithInternalCompositePriceConfig)
t.Run("Submitting a market update with invalid tick size fails and valid tick size succeeds", testUpdateMarketTickSize)
t.Run("Submitting a spot market update with invalid liquifity fee settings", testUpdateLiquidityFeeSettingsSpot)
t.Run("Update Log Normal with overrides", testUpdateLogNormalRiskParametersChangeSubmissionWithOverrides)
}

func testUpdateMarketTickSize(t *testing.T) {
Expand Down Expand Up @@ -2184,6 +2185,129 @@ func testUpdateSimpleRiskParametersChangeSubmissionWithRightProbabilityOfTrading
}
}

func testUpdateLogNormalRiskParametersChangeSubmissionWithOverrides(t *testing.T) {
cases := []struct {
desc string
get string
override *vegapb.RiskFactorOverride
err string
}{
{
desc: "no override is valid",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override",
override: nil,
err: "",
},
{
desc: "non nil, but with empty short",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{},
err: "is required",
},
{
desc: "non nil, but with bad value short",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{
Short: "asd",
},
err: "is not a valid number",
},
{
desc: "non nil, but with negative short",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{
Short: "-1",
},
err: "must be positive",
},
{
desc: "non nil, but with 0 short",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.short",
override: &vegapb.RiskFactorOverride{
Short: "0",
},
err: "must be positive",
},
{
desc: "non nil, but with empty long",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
},
err: "is required",
},
{
desc: "non nil, but with bad value long",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "asd",
},
err: "is not a valid number",
},
{
desc: "non nil, but with negative long",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "-1",
},
err: "must be positive",
},
{
desc: "non nil, but with 0 long",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override.long",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "0",
},
err: "must be positive",
},
{
desc: "both valid",
get: "proposal_submission.terms.change.update_market.changes.risk_parameters.log_normal.risk_factor_override",
override: &vegapb.RiskFactorOverride{
Short: "0.1",
Long: "0.1",
},
err: "",
},
}

for _, c := range cases {
err := checkProposalSubmission(&commandspb.ProposalSubmission{
Terms: &vegapb.ProposalTerms{
Change: &vegapb.ProposalTerms_UpdateMarket{
UpdateMarket: &vegapb.UpdateMarket{
Changes: &vegapb.UpdateMarketConfiguration{
RiskParameters: &vegapb.UpdateMarketConfiguration_LogNormal{
LogNormal: &vegapb.LogNormalRiskModel{
RiskAversionParameter: 0.1,
Tau: 1,
Params: &vegapb.LogNormalModelParams{
Mu: 0,
Sigma: 0.1,
R: 0,
},
RiskFactorOverride: c.override,
},
},
},
},
},
},
})

if len(c.err) <= 0 {
// no error
assert.Len(t, err.Get(c.get), 0, c.desc)
continue
}

assert.Contains(t, err.Get(c.get), errors.New(c.err), "test: %v, err: %v", c.desc, err)
}
}

func testUpdateLogNormalRiskParametersChangeSubmissionWithoutLogNormalRiskParametersFails(t *testing.T) {
err := checkProposalSubmission(&commandspb.ProposalSubmission{
Terms: &protoTypes.ProposalTerms{
Expand Down
Loading

0 comments on commit 4a5ca8b

Please sign in to comment.