Skip to content

Commit

Permalink
add check for invalid skew factor (#2249)
Browse files Browse the repository at this point in the history
  • Loading branch information
sr33j authored Sep 23, 2024
1 parent ebf8fe4 commit 79db31a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions protocol/x/vault/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,9 @@ var (
30,
"Shares must be positive",
)
ErrInvalidSkewFactor = errorsmod.Register(
ModuleName,
31,
"Skew factor times order_size_pct must be less than 2 to avoid skewing over the spread",
)
)
9 changes: 9 additions & 0 deletions protocol/x/vault/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"math"
"math/big"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/lib"
Expand Down Expand Up @@ -42,6 +43,14 @@ func (p QuotingParams) Validate() error {
if p.ActivationThresholdQuoteQuantums.Sign() < 0 {
return ErrInvalidActivationThresholdQuoteQuantums
}
// Skew factor times order_size_pct must be less than 2 to avoid skewing over the spread
skewFactor := new(big.Int).SetUint64(uint64(p.SkewFactorPpm))
orderSizePct := new(big.Int).SetUint64(uint64(p.OrderSizePctPpm))
skewFactorOrderSizePctProduct := new(big.Int).Mul(skewFactor, orderSizePct)
skewFactorOrderSizePctProductThreshold := big.NewInt(2_000_000 * 1_000_000)
if skewFactorOrderSizePctProduct.Cmp(skewFactorOrderSizePctProductThreshold) >= 0 {
return ErrInvalidSkewFactor
}

return nil
}
Expand Down
37 changes: 37 additions & 0 deletions protocol/x/vault/types/params_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"math"
"testing"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
Expand Down Expand Up @@ -80,6 +81,42 @@ func TestValidateQuotingParams(t *testing.T) {
},
expectedErr: types.ErrInvalidActivationThresholdQuoteQuantums,
},
"Failure - SkewFactorPpm is high. Product of SkewFactorPpm and OrderSizePctPpm is above threshold.": {
params: types.QuotingParams{
Layers: 2,
SpreadMinPpm: 3_000,
SpreadBufferPpm: 1_500,
SkewFactorPpm: 20_000_000,
OrderSizePctPpm: 100_000,
OrderExpirationSeconds: 5,
ActivationThresholdQuoteQuantums: dtypes.NewInt(1),
},
expectedErr: types.ErrInvalidSkewFactor,
},
"Failure - OrderSizePctPpm is high. Product of SkewFactorPpm and OrderSizePctPpm is above threshold.": {
params: types.QuotingParams{
Layers: 2,
SpreadMinPpm: 3_000,
SpreadBufferPpm: 1_500,
SkewFactorPpm: 2_000_000,
OrderSizePctPpm: 1_000_000,
OrderExpirationSeconds: 5,
ActivationThresholdQuoteQuantums: dtypes.NewInt(1),
},
expectedErr: types.ErrInvalidSkewFactor,
},
"Failure - Both OrderSizePctPpm and SkewFactorPpm are MaxUint32. The product is above the threshold.": {
params: types.QuotingParams{
Layers: 2,
SpreadMinPpm: 3_000,
SpreadBufferPpm: 1_500,
SkewFactorPpm: math.MaxUint32,
OrderSizePctPpm: math.MaxUint32,
OrderExpirationSeconds: 5,
ActivationThresholdQuoteQuantums: dtypes.NewInt(1),
},
expectedErr: types.ErrInvalidSkewFactor,
},
}

for name, tc := range tests {
Expand Down

0 comments on commit 79db31a

Please sign in to comment.