-
Notifications
You must be signed in to change notification settings - Fork 607
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
Change Pool interface calc methods to using integers #1345
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9dc730d
Change Pool interface calc methods to using integers
ValarDragon 760bbc7
Update breaking change notes
ValarDragon 85e2a78
Update x/gamm/pool-models/balancer/amm_test.go
ValarDragon 9eeb475
Update failing tests due to panic
ValarDragon 1441021
Change tolerance precision
ValarDragon c76e243
Comment updates
ValarDragon 9a0d149
Merge branch 'main' into dev/change_calc_methods_to_int
ValarDragon fc279a3
Fix amm_test for calc amount and in inverse relation
ValarDragon 0bf5810
Address Bez' comments
ValarDragon 196c716
Merge branch 'main' into dev/change_calc_methods_to_int
ValarDragon 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
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,14 @@ | ||
package osmoutils | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// intended to be used with require/assert: require.True(DecEq(...)) | ||
// TODO: Replace with function in SDK types package when we update | ||
func DecApproxEq(t *testing.T, d1 sdk.Dec, d2 sdk.Dec, tol sdk.Dec) (*testing.T, bool, string, string, string) { | ||
diff := d1.Sub(d2).Abs() | ||
return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String() | ||
} |
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
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -30,7 +30,7 @@ func solveConstantFunctionInvariant( | |||||||
// weightRatio = (weightX/weightY) | ||||||||
weightRatio := tokenWeightFixed.Quo(tokenWeightUnknown) | ||||||||
|
||||||||
// y = balanceXBefore/balanceYAfter | ||||||||
// y = balanceXBefore/balanceXAfter | ||||||||
y := tokenBalanceFixedBefore.Quo(tokenBalanceFixedAfter) | ||||||||
|
||||||||
// amountY = balanceY * (1 - (y ^ weightRatio)) | ||||||||
|
@@ -47,10 +47,10 @@ func (p Pool) CalcOutAmtGivenIn( | |||||||
tokensIn sdk.Coins, | ||||||||
tokenOutDenom string, | ||||||||
swapFee sdk.Dec, | ||||||||
) (sdk.DecCoin, error) { | ||||||||
) (sdk.Coin, error) { | ||||||||
tokenIn, poolAssetIn, poolAssetOut, err := p.parsePoolAssets(tokensIn, tokenOutDenom) | ||||||||
if err != nil { | ||||||||
return sdk.DecCoin{}, err | ||||||||
return sdk.Coin{}, err | ||||||||
} | ||||||||
|
||||||||
tokenAmountInAfterFee := tokenIn.Amount.ToDec().Mul(sdk.OneDec().Sub(swapFee)) | ||||||||
|
@@ -67,7 +67,13 @@ func (p Pool) CalcOutAmtGivenIn( | |||||||
poolAssetOut.Weight.ToDec(), | ||||||||
) | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return sdk.NewDecCoinFromDec(tokenOutDenom, tokenAmountOut), nil | ||||||||
// We ignore the decimal component, as we round down the token amount out. | ||||||||
tokenAmountOutInt := tokenAmountOut.TruncateInt() | ||||||||
if !tokenAmountOutInt.IsPositive() { | ||||||||
return sdk.Coin{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount must be positive") | ||||||||
} | ||||||||
|
||||||||
return sdk.NewCoin(tokenOutDenom, tokenAmountOutInt), nil | ||||||||
} | ||||||||
|
||||||||
// SwapOutAmtGivenIn is a mutative method for CalcOutAmtGivenIn, which includes the actual swap. | ||||||||
|
@@ -79,14 +85,10 @@ func (p *Pool) SwapOutAmtGivenIn( | |||||||
) ( | ||||||||
tokenOut sdk.Coin, err error, | ||||||||
) { | ||||||||
tokenOutDecCoin, err := p.CalcOutAmtGivenIn(ctx, tokensIn, tokenOutDenom, swapFee) | ||||||||
tokenOutCoin, err := p.CalcOutAmtGivenIn(ctx, tokensIn, tokenOutDenom, swapFee) | ||||||||
if err != nil { | ||||||||
return sdk.Coin{}, err | ||||||||
} | ||||||||
tokenOutCoin, _ := tokenOutDecCoin.TruncateDecimal() | ||||||||
if !tokenOutCoin.Amount.IsPositive() { | ||||||||
return sdk.Coin{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount must be positive") | ||||||||
} | ||||||||
|
||||||||
err = p.applySwap(ctx, tokensIn, sdk.Coins{tokenOutCoin}) | ||||||||
if err != nil { | ||||||||
|
@@ -99,11 +101,11 @@ func (p *Pool) SwapOutAmtGivenIn( | |||||||
// given the swapped out amount, using solveConstantFunctionInvariant. | ||||||||
func (p Pool) CalcInAmtGivenOut( | ||||||||
ctx sdk.Context, tokensOut sdk.Coins, tokenInDenom string, swapFee sdk.Dec) ( | ||||||||
tokenIn sdk.DecCoin, err error, | ||||||||
tokenIn sdk.Coin, err error, | ||||||||
) { | ||||||||
tokenOut, poolAssetOut, poolAssetIn, err := p.parsePoolAssets(tokensOut, tokenInDenom) | ||||||||
if err != nil { | ||||||||
return sdk.DecCoin{}, err | ||||||||
return sdk.Coin{}, err | ||||||||
} | ||||||||
|
||||||||
// delta balanceOut is positive(tokens inside the pool decreases) | ||||||||
|
@@ -119,27 +121,25 @@ func (p Pool) CalcInAmtGivenOut( | |||||||
// Thus in order to give X amount out, we solve the invariant for the invariant input. However invariant input = (1 - swapfee) * trade input. | ||||||||
// Therefore we divide by (1 - swapfee) here | ||||||||
tokenAmountInBeforeFee := tokenAmountIn.Quo(sdk.OneDec().Sub(swapFee)) | ||||||||
// TODO: Once we make Calc methods return integers | ||||||||
// if tokenInDecimal is non-zero, we add 1 to the tokenInCoin | ||||||||
// if tokenInDecimal.Amount.IsPositive() { | ||||||||
// tokenInCoin.Amount = tokenInCoin.Amount.AddRaw(1) | ||||||||
// } | ||||||||
return sdk.NewDecCoinFromDec(tokenInDenom, tokenAmountInBeforeFee), nil | ||||||||
|
||||||||
// We round up tokenInAmt, as this is whats charged for the swap, for the precise amount out. | ||||||||
// Otherwise, the pool would under-charge by this rounding error. | ||||||||
tokenInAmt := tokenAmountInBeforeFee.Ceil().TruncateInt() | ||||||||
|
||||||||
if !tokenInAmt.IsPositive() { | ||||||||
return sdk.Coin{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount must be positive") | ||||||||
} | ||||||||
return sdk.NewCoin(tokenInDenom, tokenInAmt), nil | ||||||||
} | ||||||||
|
||||||||
// SwapInAmtGivenOut is a mutative method for CalcOutAmtGivenIn, which includes the actual swap. | ||||||||
func (p *Pool) SwapInAmtGivenOut( | ||||||||
ctx sdk.Context, tokensOut sdk.Coins, tokenInDenom string, swapFee sdk.Dec) ( | ||||||||
tokenIn sdk.Coin, err error, | ||||||||
) { | ||||||||
tokenInDecCoin, err := p.CalcInAmtGivenOut(ctx, tokensOut, tokenInDenom, swapFee) | ||||||||
tokenInCoin, err := p.CalcInAmtGivenOut(ctx, tokensOut, tokenInDenom, swapFee) | ||||||||
if err != nil { | ||||||||
return sdk.Coin{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount is zero or negative") | ||||||||
} | ||||||||
tokenInCoin, _ := tokenInDecCoin.TruncateDecimal() | ||||||||
|
||||||||
if !tokenInCoin.Amount.IsPositive() { | ||||||||
return sdk.Coin{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount must be positive") | ||||||||
return sdk.Coin{}, err | ||||||||
} | ||||||||
|
||||||||
err = p.applySwap(ctx, sdk.Coins{tokenInCoin}, tokensOut) | ||||||||
|
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.
RIP
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.
This made me sad as well :(