-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add megavault withdrawal fee estimation (#2242)
- Loading branch information
Showing
11 changed files
with
1,010 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package vault | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/lib" | ||
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types" | ||
) | ||
|
||
// SkewAntiderivative returns the antiderivative of skew given a vault's skew | ||
// factor and leverage. | ||
// skew_antiderivative = skew_factor * leverage^2 + skew_factor^2 * leverage^3 / 3 | ||
func SkewAntiderivative( | ||
skewFactorPpm uint32, | ||
leverage *big.Rat, | ||
) *big.Rat { | ||
bigSkewFactorPpm := new(big.Rat).SetUint64(uint64(skewFactorPpm)) | ||
bigOneMillion := lib.BigRatOneMillion() | ||
|
||
// a = skew_factor * leverage^2. | ||
a := new(big.Rat).Mul(leverage, leverage) | ||
a.Mul(a, bigSkewFactorPpm) | ||
|
||
// b = skew_factor^2 * leverage^3 / 3. | ||
b := new(big.Rat).Set(a) | ||
b.Mul(b, leverage) | ||
b.Mul(b, bigSkewFactorPpm) | ||
b.Quo(b, big.NewRat(3, 1)) | ||
|
||
// normalize `a` whose unit currently is ppm. | ||
a.Quo(a, bigOneMillion) | ||
// normalize `b` whose unit currently is ppm * ppm. | ||
b.Quo(b, bigOneMillion) | ||
b.Quo(b, bigOneMillion) | ||
|
||
// return a + b. | ||
return a.Add(a, b) | ||
} | ||
|
||
// SpreadPpm returns the spread that a vault should quote at given its | ||
// quoting params and corresponding market param. | ||
// spread_ppm = max(spread_min_ppm, spread_buffer_ppm + min_price_change_ppm) | ||
func SpreadPpm( | ||
quotingParams *types.QuotingParams, | ||
marketParam *pricestypes.MarketParam, | ||
) uint32 { | ||
return lib.Max( | ||
quotingParams.SpreadMinPpm, | ||
quotingParams.SpreadBufferPpm+marketParam.MinPriceChangePpm, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package vault_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/lib/vault" | ||
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types" | ||
) | ||
|
||
func TestSkewAntiderivativePpm(t *testing.T) { | ||
tests := map[string]struct { | ||
skewFactorPpm uint32 | ||
leverage *big.Rat | ||
expected *big.Rat | ||
}{ | ||
"Zero skew factor and leverage": { | ||
skewFactorPpm: 0, | ||
leverage: big.NewRat(0, 1), | ||
expected: big.NewRat(0, 1), | ||
}, | ||
"Non-zero skew factor, zero leverage": { | ||
skewFactorPpm: 1_000_000, | ||
leverage: big.NewRat(0, 1), | ||
expected: big.NewRat(0, 1), | ||
}, | ||
"Zero skew factor, non-zero leverage": { | ||
skewFactorPpm: 0, | ||
leverage: big.NewRat(1_000_000, 1), | ||
expected: big.NewRat(0, 1), | ||
}, | ||
"Small skew factor and small positive leverage": { | ||
skewFactorPpm: 500_000, // 0.5 | ||
leverage: big.NewRat(4, 5), // 0.8 | ||
// 0.5 * 0.8^2 + 0.5^2 * 0.8^3 / 3 = 136/375 | ||
expected: big.NewRat(136, 375), | ||
}, | ||
"Small skew factor and small negative leverage": { | ||
skewFactorPpm: 500_000, // 0.5 | ||
leverage: big.NewRat(-4, 5), // -0.8 | ||
// 0.5 * (-0.8)^2 + 0.5^2 * (-0.8)^3 / 3 = 104/375 | ||
expected: big.NewRat(104, 375), | ||
}, | ||
"Large skew factor and large positive leverage": { | ||
skewFactorPpm: 5_000_000, // 5 | ||
leverage: big.NewRat(87, 10), // 8.7 | ||
// 5 * (8.7)^2 + 5^2 * (8.7)^3 / 3 = 234639/40 | ||
expected: big.NewRat(234_639, 40), | ||
}, | ||
"Large skew factor and large negative leverage": { | ||
skewFactorPpm: 5_000_000, // 5 | ||
leverage: big.NewRat(-87, 10), // -8.7 | ||
// 5 * (-8.7)^2 + 5^2 * (-8.7)^3 / 3 = -204363/40 | ||
expected: big.NewRat(-204_363, 40), | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
actual := vault.SkewAntiderivative(tc.skewFactorPpm, tc.leverage) | ||
require.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestSpreadPpm(t *testing.T) { | ||
tests := map[string]struct { | ||
quotingParams *types.QuotingParams | ||
marketParam *pricestypes.MarketParam | ||
expected uint32 | ||
}{ | ||
"SpreadMinPpm > SpreadBufferPpm + MinPriceChangePpm": { | ||
quotingParams: &types.QuotingParams{ | ||
SpreadMinPpm: 1000, | ||
SpreadBufferPpm: 200, | ||
}, | ||
marketParam: &pricestypes.MarketParam{ | ||
MinPriceChangePpm: 500, | ||
}, | ||
expected: 1000, | ||
}, | ||
"SpreadMinPpm < SpreadBufferPpm + MinPriceChangePpm": { | ||
quotingParams: &types.QuotingParams{ | ||
SpreadMinPpm: 1000, | ||
SpreadBufferPpm: 600, | ||
}, | ||
marketParam: &pricestypes.MarketParam{ | ||
MinPriceChangePpm: 500, | ||
}, | ||
expected: 1100, | ||
}, | ||
"SpreadMinPpm = SpreadBufferPpm + MinPriceChangePpm": { | ||
quotingParams: &types.QuotingParams{ | ||
SpreadMinPpm: 1000, | ||
SpreadBufferPpm: 400, | ||
}, | ||
marketParam: &pricestypes.MarketParam{ | ||
MinPriceChangePpm: 600, | ||
}, | ||
expected: 1000, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
require.Equal( | ||
t, | ||
tc.expected, | ||
vault.SpreadPpm(tc.quotingParams, tc.marketParam), | ||
) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.