-
Notifications
You must be signed in to change notification settings - Fork 122
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
- Loading branch information
Showing
9 changed files
with
801 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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" | ||
) | ||
|
||
// SkewAntiderivativePpm returns the antiderivative of skew given a vault's skew | ||
// factor and leverage. | ||
// skew_antiderivative_ppm = skew_factor * leverage^2 + skew_factor^2 * leverage^3 / 3 | ||
func SkewAntiderivativePpm( | ||
skewFactorPpm uint32, | ||
leveragePpm *big.Int, | ||
) *big.Int { | ||
bigSkewFactorPpm := new(big.Int).SetUint64(uint64(skewFactorPpm)) | ||
bigOneTrillion := lib.BigIntOneTrillion() | ||
|
||
// a = skew_factor * leverage^2. | ||
a := new(big.Int).Mul(leveragePpm, leveragePpm) | ||
a.Mul(a, bigSkewFactorPpm) | ||
|
||
// b = skew_factor^2 * leverage^3 / 3. | ||
b := new(big.Int).Set(a) | ||
b.Mul(b, leveragePpm) | ||
b.Mul(b, bigSkewFactorPpm) | ||
b = lib.BigDivCeil(b, big.NewInt(3)) | ||
|
||
// normalize a and b. | ||
a = lib.BigDivCeil(a, bigOneTrillion) | ||
b = lib.BigDivCeil(b, bigOneTrillion) | ||
b = lib.BigDivCeil(b, bigOneTrillion) | ||
|
||
// 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,118 @@ | ||
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 | ||
leveragePpm *big.Int | ||
expected *big.Int | ||
}{ | ||
"Zero skew factor and leverage": { | ||
skewFactorPpm: 0, | ||
leveragePpm: big.NewInt(0), | ||
expected: big.NewInt(0), | ||
}, | ||
"Non-zero skew factor, zero leverage": { | ||
skewFactorPpm: 1_000_000, | ||
leveragePpm: big.NewInt(0), | ||
expected: big.NewInt(0), | ||
}, | ||
"Zero skew factor, non-zero leverage": { | ||
skewFactorPpm: 0, | ||
leveragePpm: big.NewInt(1_000_000), | ||
expected: big.NewInt(0), | ||
}, | ||
"Small skew factor and small positive leverage": { | ||
skewFactorPpm: 500_000, // 0.5 | ||
leveragePpm: big.NewInt(800_000), // 0.8 | ||
// 0.5 * 0.8^2 + 0.5^2 * 0.8^3 / 3 ~= 0.362666 | ||
// round up to 0.362667 | ||
expected: big.NewInt(362_667), | ||
}, | ||
"Small skew factor and small negative leverage": { | ||
skewFactorPpm: 500_000, // 0.5 | ||
leveragePpm: big.NewInt(-800_000), // -0.8 | ||
// 0.5 * (-0.8)^2 + 0.5^2 * (-0.8)^3 / 3 ~= 0.277333 | ||
// round up to 0.277334 | ||
expected: big.NewInt(277_334), | ||
}, | ||
"Large skew factor and large positive leverage": { | ||
skewFactorPpm: 5_000_000, // 5 | ||
leveragePpm: big.NewInt(8_700_000), // 8.7 | ||
// 5 * (8.7)^2 + 5^2 * (8.7)^3 / 3 = 5865.975 | ||
expected: big.NewInt(5_865_975_000), | ||
}, | ||
"Large skew factor and large negative leverage": { | ||
skewFactorPpm: 5_000_000, // 5 | ||
leveragePpm: big.NewInt(-8_700_000), // -8.7 | ||
// 5 * (-8.7)^2 + 5^2 * (-8.7)^3 / 3 = -5109.075 | ||
expected: big.NewInt(-5_109_075_000), | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
actual := vault.SkewAntiderivativePpm(tc.skewFactorPpm, tc.leveragePpm) | ||
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.