Commit aab9ac6a authored by mattverse's avatar mattverse
Browse files

WIP

parent a35002c9
Showing with 57 additions and 51 deletions
+57 -51
......@@ -200,7 +200,7 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
p := k.getPoolbyId(ctx, poolId)
asset0 := p.Token0
asset1 := p.Token1
zeroForOne := tokenOut.Denom == asset0
zeroForOne := tokenIn.Denom == asset0
// get current sqrt price from pool
// curSqrtPrice := sdk.NewDecWithPrec(int64(p.CurrentSqrtPrice.Uint64()), 6)
......@@ -255,6 +255,7 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
// TODO: This should be GT 0 but some instances have very small remainder
// need to look into fixing this
for swapState.amountSpecifiedRemaining.GT(sdk.NewDecWithPrec(1, 6)) {
// amountRemaining := sdk.NewDec(99999999999)
nextTick, _ := k.NextInitializedTick(ctx, poolId, swapState.tick.Int64(), zeroForOne)
// TODO: we can enable this error checking once we fix tick initialization
// if !ok {
......@@ -266,6 +267,7 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
}
// TODO: In and out get flipped based on if we are calculating for in or out, need to fix this
// Matt thinks we should use a different equation here
sqrtPrice, amountIn, amountOut := computeSwapStep(
swapState.sqrtPrice,
nextSqrtPrice,
......@@ -274,22 +276,22 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
zeroForOne,
)
swapState.amountSpecifiedRemaining = swapState.amountSpecifiedRemaining.Sub(amountIn)
swapState.amountCalculated = swapState.amountCalculated.Add(amountOut.Quo(sdk.OneDec().Sub(swapFee)))
swapState.amountSpecifiedRemaining = swapState.amountSpecifiedRemaining.Sub(amountOut)
swapState.amountCalculated = swapState.amountCalculated.Add(amountIn.Quo(sdk.OneDec().Sub(swapFee)))
if swapState.sqrtPrice.Equal(sqrtPrice) {
if nextSqrtPrice.Equal(sqrtPrice) {
liquidityDelta, err := k.crossTick(ctx, p.Id, nextTick)
if err != nil {
return sdk.Coin{}, sdk.Coin{}, err
}
if !zeroForOne {
if zeroForOne {
liquidityDelta = liquidityDelta.Neg()
}
swapState.liquidity = swapState.liquidity.Add(liquidityDelta.ToDec())
if swapState.liquidity.LTE(sdk.ZeroDec()) || swapState.liquidity.IsNil() {
return sdk.Coin{}, sdk.Coin{}, fmt.Errorf("no liquidity available, cannot swap")
}
if !zeroForOne {
if zeroForOne {
swapState.tick = sdk.NewInt(nextTick - 1)
} else {
swapState.tick = sdk.NewInt(nextTick)
......
package concentrated_liquidity_test
import (
fmt "fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
cl "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity"
......@@ -13,43 +15,43 @@ func (s *KeeperTestSuite) TestCalcOutAmtGivenIn() {
s.Require().NoError(err)
s.SetupPosition(pool.Id)
// test asset a to b logic
tokenIn := sdk.NewCoin("eth", sdk.NewInt(133700))
tokenOutDenom := "usdc"
swapFee := sdk.NewDec(0)
// // test asset a to b logic
// tokenIn := sdk.NewCoin("eth", sdk.NewInt(133700))
// tokenOutDenom := "usdc"
// swapFee := sdk.NewDec(0)
minPrice := sdk.NewDec(4500)
maxPrice := sdk.NewDec(5500)
_, amountOut, err := s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
s.Require().NoError(err)
s.Require().Equal(sdk.NewDec(666975610).String(), amountOut.Amount.ToDec().String())
// _, amountOut, err := s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
// s.Require().NoError(err)
// s.Require().Equal(sdk.NewDec(666975610).String(), amountOut.Amount.ToDec().String())
// test asset b to a logic
tokenIn = sdk.NewCoin("usdc", sdk.NewInt(4199999999))
tokenOutDenom = "eth"
swapFee = sdk.NewDec(0)
tokenIn := sdk.NewCoin("usdc", sdk.NewInt(4_199_999_999))
tokenOutDenom := "eth"
swapFee := sdk.NewDec(0)
_, amountOut, err = s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
_, amountOut, err := s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
s.Require().NoError(err)
s.Require().Equal(sdk.NewDec(805287), amountOut.Amount.ToDec())
// test asset b to a logic
tokenIn = sdk.NewCoin("usdc", sdk.NewInt(42000000))
tokenOutDenom = "eth"
swapFee = sdk.NewDec(0)
_, amountOut, err = s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
s.Require().NoError(err)
s.Require().Equal(sdk.NewDec(8396), amountOut.Amount.ToDec())
// test with swap fee
tokenIn = sdk.NewCoin("usdc", sdk.NewInt(4199999999))
tokenOutDenom = "eth"
swapFee = sdk.NewDecWithPrec(2, 2)
_, amountOut, err = s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
s.Require().NoError(err)
s.Require().Equal(sdk.NewDec(789834), amountOut.Amount.ToDec())
// tokenIn := sdk.NewCoin("usdc", sdk.NewInt(42_000_000))
// tokenOutDenom := "eth"
// swapFee := sdk.NewDec(0)
// _, amountOut, err := s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
// s.Require().NoError(err)
// s.Require().Equal(sdk.NewDec(8396), amountOut.Amount.ToDec())
// // test with swap fee
// tokenzIn = sdk.NewCoin("usdc", sdk.NewInt(4199999999))
// tokenOutDenom = "eth"
// swapFee = sdk.NewDecWithPrec(2, 2)
// _, amountOut, err = s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
// s.Require().NoError(err)
// s.Require().Equal(sdk.NewDec(789834), amountOut.Amount.ToDec())
}
func (s *KeeperTestSuite) TestCalcInAmtGivenOut() {
......@@ -59,33 +61,35 @@ func (s *KeeperTestSuite) TestCalcInAmtGivenOut() {
s.SetupPosition(pool.Id)
// test asset a to b logic
tokenOut := sdk.NewCoin("usdc", sdk.NewInt(4199999999))
tokenInDenom := "eth"
tokenOut := sdk.NewCoin("eth", sdk.NewInt(805287))
tokenInDenom := "usdc"
swapFee := sdk.NewDec(0)
minPrice := sdk.NewDec(4500)
maxPrice := sdk.NewDec(5500)
amountIn, _, err := s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
amountIn, amountOut, err := s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
fmt.Println(amountIn.String())
fmt.Println(amountOut.String())
s.Require().NoError(err)
s.Require().Equal(sdk.NewDec(805287), amountIn.Amount.ToDec())
s.Require().Equal(sdk.NewDec(4_199_999_999), amountIn.Amount.ToDec())
// test asset b to a logic
tokenOut = sdk.NewCoin("eth", sdk.NewInt(133700))
tokenInDenom = "usdc"
swapFee = sdk.NewDec(0)
// // test asset b to a logic
// tokenOut = sdk.NewCoin("eth", sdk.NewInt(133700))
// tokenInDenom = "usdc"
// swapFee = sdk.NewDec(0)
amountIn, _, err = s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
s.Require().NoError(err)
s.Require().Equal(sdk.NewDec(666975610), amountIn.Amount.ToDec())
// amountIn, _, err = s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
// s.Require().NoError(err)
// s.Require().Equal(sdk.NewDec(666975610), amountIn.Amount.ToDec())
// test asset a to b logic
tokenOut = sdk.NewCoin("usdc", sdk.NewInt(4199999999))
tokenInDenom = "eth"
swapFee = sdk.NewDecWithPrec(2, 2)
// // test asset a to b logic
// tokenOut = sdk.NewCoin("usdc", sdk.NewInt(4199999999))
// tokenInDenom = "eth"
// swapFee = sdk.NewDecWithPrec(2, 2)
amountIn, _, err = s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
s.Require().NoError(err)
s.Require().Equal(sdk.NewDec(821722), amountIn.Amount.ToDec())
// amountIn, _, err = s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
// s.Require().NoError(err)
// s.Require().Equal(sdk.NewDec(821722), amountIn.Amount.ToDec())
}
func (s *KeeperTestSuite) TestOrderInitialPoolDenoms() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment