-
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
Add boilerplate logic for stableswap swaps and spot price #1287
Conversation
func (pa Pool) CalcOutAmtGivenIn(ctx sdk.Context, tokenIn sdk.Coins, tokenOutDenom string, swapFee sdk.Dec) (tokenOut sdk.DecCoin, err error) { | ||
if tokenIn.Len() != 1 { | ||
return sdk.DecCoin{}, errors.New("asdf") | ||
return sdk.DecCoin{}, errors.New("stableswap CalcOutAmtGivenIn: tokenIn is of wrong length") |
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.
does it make sense to format the length value in the message?
// we ignore the decimal component, as token out amount must round down | ||
tokenOut, _ = tokenOutDec.TruncateDecimal() | ||
if !tokenOut.Amount.IsPositive() { | ||
return sdk.Coin{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount must be positive") |
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.
does it make sense to format what it actually was in the message?
p.PoolLiquidity = p.PoolLiquidity.Add(tokensIn...).Sub(tokensOut) | ||
// sanity check that no new denoms were added | ||
if len(p.PoolLiquidity) != l { | ||
panic("applySwap changed number of tokens in pool") |
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.
q: At first, I thought that we can propagate the error up top. However, it seems that the panic here is for a reason. We don't want to let the caller handle the error if the number of tokens happens to change. Is my reasoning correct?
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.
yeah, this should never happen. (It should have been caught by the methods that call it in this package, and not be able to be triggered from functions outside the package)
} | ||
|
||
func (pa Pool) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coins, tokenInDenom string, swapFee sdk.Dec) (tokenIn sdk.DecCoin, err error) { | ||
return sdk.DecCoin{}, types.ErrNotImplemented | ||
if tokenOut.Len() != 1 { | ||
return sdk.DecCoin{}, errors.New("stableswap CalcInAmtGivenOut: tokenOut is of wrong length") |
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: format the length if makes sense
Codecov Report
@@ Coverage Diff @@
## main #1287 +/- ##
==========================================
+ Coverage 20.14% 20.23% +0.08%
==========================================
Files 203 203
Lines 26824 26868 +44
==========================================
+ Hits 5405 5437 +32
- Misses 20412 20421 +9
- Partials 1007 1010 +3
Continue to review full report at Codecov.
|
Decision from call: Add test for this module, don't amend the more general testing Roman recently added right now |
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.
Left some minor comments! (mostly comments for the purpose of actually understanding what's happening rather than changes requests)
x/gamm/pool-models/balancer/amm.go
Outdated
tokenInCoin, tokenInDecimal := tokenInDecCoin.TruncateDecimal() | ||
// if tokenInDecimal is non-zero, we add 1 to the tokenInCoin | ||
if tokenInDecimal.Amount.IsPositive() { | ||
tokenInCoin.Amount.AddRaw(1) |
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.
don't we have to do tokenInCoin = tokenInCoin.Amount.AddRaw(1)
?
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.
Also, I'm curious why we add 1 in the case of non-zeros!
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.
ah yeah your right, good catch.
The idea here is that I want to get exactly 100
tokens out after the trade. But to do that, I have to provide say 50.82
tokens as input. Tokens are stored as integers, so before we were only requiring 50
tokens to be sent. But actually it should have been 51
when its converted to integers.
We should always round-up here for correctness!
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.
I'm going to revert this change for Balancer, and fix it in a second PR that fixes CalcSpotPrice to be an integer API
if len(p.PoolLiquidity) != numTokens { | ||
panic("updatePoolLiquidityForSwap changed number of tokens in pool") | ||
} |
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.
love this! 🚀
// We are solving for the amount of token in, cfmm(x,y) = cfmm(x + x_in, y - y_out) | ||
// x = tokenInSupply, y = tokenOutSupply, yIn = -tokenOutAmount | ||
inAmtRaw := solveCfmm(tokenInSupply, tokenOutSupply, tokenOut.Amount.ToDec().Neg()) |
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.
Could you explain why we use a negative value for y in?
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.
Core function LGTM once https://github.com/osmosis-labs/osmosis/pull/1287/files#r855833475 is fixed.
I propose we merge this to main without tests as of now, since this PR is a big blocker in gamm generalization parallelization!
Adds logic for stableswap implementing the swap and spot price interfaces. The PR is marked as draft until some test cases are added.
For contributor use:o
docs/
) or specification (x/<module>/spec/
)Unreleased
section inCHANGELOG.md
Files changed
in the Github PR explorer