Skip to content

Commit 79db31a

Browse files
authored
add check for invalid skew factor (#2249)
1 parent ebf8fe4 commit 79db31a

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

protocol/x/vault/types/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,9 @@ var (
155155
30,
156156
"Shares must be positive",
157157
)
158+
ErrInvalidSkewFactor = errorsmod.Register(
159+
ModuleName,
160+
31,
161+
"Skew factor times order_size_pct must be less than 2 to avoid skewing over the spread",
162+
)
158163
)

protocol/x/vault/types/params.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"math"
5+
"math/big"
56

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

4655
return nil
4756
}

protocol/x/vault/types/params_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types_test
22

33
import (
4+
"math"
45
"testing"
56

67
"github.com/dydxprotocol/v4-chain/protocol/dtypes"
@@ -80,6 +81,42 @@ func TestValidateQuotingParams(t *testing.T) {
8081
},
8182
expectedErr: types.ErrInvalidActivationThresholdQuoteQuantums,
8283
},
84+
"Failure - SkewFactorPpm is high. Product of SkewFactorPpm and OrderSizePctPpm is above threshold.": {
85+
params: types.QuotingParams{
86+
Layers: 2,
87+
SpreadMinPpm: 3_000,
88+
SpreadBufferPpm: 1_500,
89+
SkewFactorPpm: 20_000_000,
90+
OrderSizePctPpm: 100_000,
91+
OrderExpirationSeconds: 5,
92+
ActivationThresholdQuoteQuantums: dtypes.NewInt(1),
93+
},
94+
expectedErr: types.ErrInvalidSkewFactor,
95+
},
96+
"Failure - OrderSizePctPpm is high. Product of SkewFactorPpm and OrderSizePctPpm is above threshold.": {
97+
params: types.QuotingParams{
98+
Layers: 2,
99+
SpreadMinPpm: 3_000,
100+
SpreadBufferPpm: 1_500,
101+
SkewFactorPpm: 2_000_000,
102+
OrderSizePctPpm: 1_000_000,
103+
OrderExpirationSeconds: 5,
104+
ActivationThresholdQuoteQuantums: dtypes.NewInt(1),
105+
},
106+
expectedErr: types.ErrInvalidSkewFactor,
107+
},
108+
"Failure - Both OrderSizePctPpm and SkewFactorPpm are MaxUint32. The product is above the threshold.": {
109+
params: types.QuotingParams{
110+
Layers: 2,
111+
SpreadMinPpm: 3_000,
112+
SpreadBufferPpm: 1_500,
113+
SkewFactorPpm: math.MaxUint32,
114+
OrderSizePctPpm: math.MaxUint32,
115+
OrderExpirationSeconds: 5,
116+
ActivationThresholdQuoteQuantums: dtypes.NewInt(1),
117+
},
118+
expectedErr: types.ErrInvalidSkewFactor,
119+
},
83120
}
84121

85122
for name, tc := range tests {

0 commit comments

Comments
 (0)