-
Notifications
You must be signed in to change notification settings - Fork 608
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
Changes from all commits
dd05943
60f4c35
0b2da2f
c613b44
3456a19
56b87cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
||
"github.com/osmosis-labs/osmosis/v7/x/gamm/types" | ||
) | ||
|
@@ -68,34 +69,86 @@ func (pa Pool) getPoolAmts(denoms ...string) ([]sdk.Int, error) { | |
return result, nil | ||
} | ||
|
||
// These should all get moved to amm.go | ||
// updatePoolLiquidityForSwap updates the pool liquidity. | ||
// It requires caller to validate that tokensIn and tokensOut only consist of | ||
// denominations in the pool. | ||
// The function sanity checks this, and panics if not the case. | ||
func (p *Pool) updatePoolLiquidityForSwap(tokensIn sdk.Coins, tokensOut sdk.Coins) { | ||
numTokens := p.PoolLiquidity.Len() | ||
// update liquidity | ||
p.PoolLiquidity = p.PoolLiquidity.Add(tokensIn...).Sub(tokensOut) | ||
// sanity check that no new denoms were added | ||
if len(p.PoolLiquidity) != numTokens { | ||
panic("updatePoolLiquidityForSwap changed number of tokens in pool") | ||
} | ||
Comment on lines
+81
to
+83
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. love this! 🚀 |
||
} | ||
|
||
// TODO: These should all get moved to amm.go | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. does it make sense to format the length value in the message? |
||
} | ||
reserves, err := pa.getPoolAmts(tokenIn[0].Denom, tokenOutDenom) | ||
amt, err := pa.calcOutAmtGivenIn(tokenIn[0], tokenOutDenom, swapFee) | ||
if err != nil { | ||
return sdk.DecCoin{}, err | ||
} | ||
// document which is x vs y | ||
outAmt := solveCfmm(reserves[1].ToDec(), reserves[0].ToDec(), tokenIn[0].Amount.ToDec()) | ||
return sdk.DecCoin{Denom: tokenOutDenom, Amount: outAmt}, nil | ||
return sdk.DecCoin{Denom: tokenOutDenom, Amount: amt}, nil | ||
} | ||
|
||
func (pa *Pool) SwapOutAmtGivenIn(ctx sdk.Context, tokenIn sdk.Coins, tokenOutDenom string, swapFee sdk.Dec) (tokenOut sdk.Coin, err error) { | ||
return sdk.Coin{}, types.ErrNotImplemented | ||
tokenOutDec, err := pa.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee) | ||
if err != nil { | ||
return sdk.Coin{}, err | ||
} | ||
// 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 commentThe 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? |
||
} | ||
pa.updatePoolLiquidityForSwap(tokenIn, sdk.NewCoins(tokenOut)) | ||
|
||
return tokenOut, nil | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. nit: format the length if makes sense |
||
} | ||
// TODO: Refactor this later to handle scaling factors | ||
amt, err := pa.calcInAmtGivenOut(tokenOut[0], tokenInDenom, swapFee) | ||
if err != nil { | ||
return sdk.DecCoin{}, err | ||
} | ||
// TODO: Once we make calc in amt given out return a Coin | ||
// if tokenInDecimal is non-zero, we add 1 to the tokenInCoin | ||
// this is because tokenIn must round up | ||
// if tokenInDecimal.Amount.IsPositive() { | ||
// tokenIn.Amount = tokenIn.Amount.AddRaw(1) | ||
// } | ||
return sdk.DecCoin{Denom: tokenInDenom, Amount: amt}, nil | ||
} | ||
|
||
func (pa *Pool) SwapInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coins, tokenInDenom string, swapFee sdk.Dec) (tokenIn sdk.Coin, err error) { | ||
return sdk.Coin{}, types.ErrNotImplemented | ||
tokenInDec, err := pa.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee) | ||
if err != nil { | ||
return sdk.Coin{}, err | ||
} | ||
tokenIn, _ = tokenInDec.TruncateDecimal() | ||
if !tokenIn.Amount.IsPositive() { | ||
return sdk.Coin{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount must be positive") | ||
} | ||
pa.updatePoolLiquidityForSwap(sdk.NewCoins(tokenIn), tokenOut) | ||
|
||
return tokenIn, nil | ||
} | ||
|
||
func (pa Pool) SpotPrice(ctx sdk.Context, baseAssetDenom string, quoteAssetDenom string) (sdk.Dec, error) { | ||
return sdk.Dec{}, types.ErrNotImplemented | ||
reserves, err := pa.getPoolAmts(baseAssetDenom, quoteAssetDenom) | ||
if err != nil { | ||
return sdk.Dec{}, err | ||
} | ||
// TODO: apply scaling factors here | ||
return spotPrice(reserves[0].ToDec(), reserves[1].ToDec()), nil | ||
} | ||
|
||
func (pa Pool) CalcJoinPoolShares(ctx sdk.Context, tokensIn sdk.Coins, swapFee sdk.Dec) (numShares sdk.Int, newLiquidity sdk.Coins, err error) { | ||
|
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?