Skip to content
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

Merged
merged 6 commits into from
Apr 25, 2022

Conversation

ValarDragon
Copy link
Member

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

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

@github-actions github-actions bot added the C:x/gamm Changes, features and bugs related to the gamm module. label Apr 18, 2022
x/gamm/pool-models/stableswap/pool.go Outdated Show resolved Hide resolved
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")
Copy link
Member

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")
Copy link
Member

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")
Copy link
Member

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?

Copy link
Member Author

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")
Copy link
Member

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-commenter
Copy link

codecov-commenter commented Apr 19, 2022

Codecov Report

Merging #1287 (0b2da2f) into main (298ad00) will increase coverage by 0.08%.
The diff coverage is 0.00%.

@@            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     
Impacted Files Coverage Δ
x/gamm/pool-models/balancer/amm.go 17.41% <0.00%> (+14.39%) ⬆️
x/gamm/pool-models/stableswap/amm.go 43.28% <0.00%> (-12.49%) ⬇️
x/gamm/pool-models/stableswap/pool.go 0.00% <0.00%> (ø)
x/gamm/pool-models/balancer/balancer_pool.go 64.37% <0.00%> (+1.28%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 298ad00...0b2da2f. Read the comment docs.

@ValarDragon
Copy link
Member Author

Decision from call: Add test for this module, don't amend the more general testing Roman recently added right now

Copy link
Member

@mattverse mattverse left a 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)

tokenInCoin, tokenInDecimal := tokenInDecCoin.TruncateDecimal()
// if tokenInDecimal is non-zero, we add 1 to the tokenInCoin
if tokenInDecimal.Amount.IsPositive() {
tokenInCoin.Amount.AddRaw(1)
Copy link
Member

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) ?

Copy link
Member

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!

Copy link
Member Author

@ValarDragon ValarDragon Apr 22, 2022

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!

Copy link
Member Author

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

Comment on lines +81 to +83
if len(p.PoolLiquidity) != numTokens {
panic("updatePoolLiquidityForSwap changed number of tokens in pool")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love this! 🚀

Comment on lines +213 to +215
// 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())
Copy link
Member

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?

@mattverse mattverse self-requested a review April 25, 2022 16:51
Copy link
Member

@mattverse mattverse left a 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!

@ValarDragon ValarDragon marked this pull request as ready for review April 25, 2022 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge C:x/gamm Changes, features and bugs related to the gamm module.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants