-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add megavault withdrawal fee estimation #2242
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78ded0f
add megavault withdrawal fee estimation
tqin7 836b555
pass in shares to withdraw instead of portion
tqin7 3749685
use rational number and simplified math
tqin7 1128b83
Merge branch 'main' into tq/tra-592
tqin7 b7456a2
calculate leverage ppm correctly
tqin7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Maybe a bit more of a comment on the normalization (ppm * ppm * ppm / 1,000,000,000,000) = (ppm)?