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

concentrated liquidity: further swap/lp work #3101

Merged
merged 9 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- "main"
- "concentrated-liquidity-main"
- "v[0-9]**"
workflow_dispatch:

Expand Down Expand Up @@ -124,3 +125,4 @@ jobs:
-
name: Test e2e and Upgrade
run: make test-e2e-ci

4 changes: 2 additions & 2 deletions x/concentrated-liquidity/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package concentrated_liquidity

// OrderInitialPoolDenoms sets the pool denoms of a cl pool
func (p *Pool) OrderInitialPoolDenoms(denom0, denom1 string) error {
return p.orderInitialPoolDenoms(denom0, denom1)
func (k Keeper) OrderInitialPoolDenoms(denom0, denom1 string) (string, string, error) {
return k.orderInitialPoolDenoms(denom0, denom1)
}
24 changes: 10 additions & 14 deletions x/concentrated-liquidity/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,22 @@ func (k Keeper) Mint(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, liqui
if liquidityIn.IsZero() {
return sdk.Int{}, sdk.Int{}, fmt.Errorf("token in amount is zero")
}

// TODO: we need to check if TickInfo is initialized on a specific tick before updating, otherwise get wont work
// k.setTickInfo(ctx, poolId, lowerTick, TickInfo{})
// k.UpdateTickWithNewLiquidity(ctx, poolId, lowerTick, liquidityIn)
// k.setTickInfo(ctx, poolId, upperTick, TickInfo{})
// k.UpdateTickWithNewLiquidity(ctx, poolId, upperTick, liquidityIn)

// k.updatePositionWithLiquidity(ctx, poolId, owner.String(), lowerTick, upperTick, liquidityIn)
// k.setPosition(ctx, poolId, owner, lowerTick, upperTick, Position{})
// k.updatePositionWithLiquidity(ctx, poolId, owner, lowerTick, upperTick, liquidityIn)

pool := k.getPoolbyId(ctx, poolId)

currentSqrtPrice := pool.CurrentSqrtPrice
sqrtRatioUpperTick, err := k.getSqrtRatioAtTick(upperTick)
if err != nil {
return sdk.Int{}, sdk.Int{}, err
}
sqrtRatioLowerTick, err := k.getSqrtRatioAtTick(lowerTick)
if err != nil {
return sdk.Int{}, sdk.Int{}, err
}
currentSqrtPrice := sdk.NewDecWithPrec(int64(pool.CurrentSqrtPrice.Uint64()), 6)
czarcas7ic marked this conversation as resolved.
Show resolved Hide resolved
mattverse marked this conversation as resolved.
Show resolved Hide resolved
sqrtRatioUpperTick, _ := k.tickToPrice(sdk.NewInt(upperTick)).ApproxSqrt()
sqrtRatioLowerTick, _ := k.tickToPrice(sdk.NewInt(lowerTick)).ApproxSqrt()

amtDenom0 = calcAmount0Delta(currentSqrtPrice.ToDec(), sqrtRatioUpperTick, liquidityIn.ToDec()).RoundInt()
amtDenom1 = calcAmount1Delta(currentSqrtPrice.ToDec(), sqrtRatioLowerTick, liquidityIn.ToDec()).RoundInt()
amtDenom0 = calcAmount0Delta(liquidityIn.ToDec(), currentSqrtPrice, sqrtRatioUpperTick).RoundInt()
Copy link
Member

Choose a reason for hiding this comment

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

Why do we round and not truncate?

Copy link
Member

Choose a reason for hiding this comment

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

Why not round? IMO rounding is more accurate no?

amtDenom1 = calcAmount1Delta(liquidityIn.ToDec(), currentSqrtPrice, sqrtRatioLowerTick).RoundInt()

return amtDenom0, amtDenom1, nil
}
Expand Down
16 changes: 9 additions & 7 deletions x/concentrated-liquidity/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@ func (s *KeeperTestSuite) TestMint() {
// current tick: 85176
// lower tick: 84222
// upper tick: 86129
// liquidity(token in):1517882343751509868544
// current sqrt price: 5602277097478614198912276234240
// denom0: uosmo
// liquidity(token in):1517882323
// current sqrt price: 70710678
// denom0: eth
// denom1: usdc
poolId := uint64(1)
currentTick := sdk.NewInt(85176)
lowerTick := int64(84222)
upperTick := int64(86129)
liquidity, ok := sdk.NewIntFromString("1517882343751509868544")
liquidity, ok := sdk.NewIntFromString("1517882323")
s.Require().True(ok)
currentSqrtP, ok := sdk.NewIntFromString("5602277097478614198912276234240")
currentSqrtP, ok := sdk.NewIntFromString("70710678")
s.Require().True(ok)
denom0 := "uosmo"
denom0 := "eth"
denom1 := "usdc"

s.SetupTest()

s.App.ConcentratedLiquidityKeeper.CreateNewConcentratedLiquidityPool(s.Ctx, poolId, denom0, denom1, currentSqrtP, currentTick)

_, _, err := s.App.ConcentratedLiquidityKeeper.Mint(s.Ctx, poolId, s.TestAccs[0], liquidity, lowerTick, upperTick)
asset0, asset1, err := s.App.ConcentratedLiquidityKeeper.Mint(s.Ctx, poolId, s.TestAccs[0], liquidity, lowerTick, upperTick)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt(998629), asset0) // .998629 ETH
s.Require().Equal(sdk.NewInt(5000208942), asset1) // 5000.20 USDC
}
21 changes: 20 additions & 1 deletion x/concentrated-liquidity/math.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package concentrated_liquidity

import sdk "github.com/cosmos/cosmos-sdk/types"
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// liquidity0 takes an amount of asset0 in the pool as well as the sqrtpCur and the nextPrice
// sqrtPriceA is the smaller of sqrtpCur and the nextPrice
Expand Down Expand Up @@ -52,3 +54,20 @@ func calcAmount1Delta(liq, sqrtPriceA, sqrtPriceB sdk.Dec) sdk.Dec {
diff := sqrtPriceB.Sub(sqrtPriceA)
return liq.Mul(diff)
}

func computeSwapStep(sqrtPriceCurrent, sqrtPriceTarget, liquidity, amountRemaining sdk.Dec, lte bool) (sqrtPriceNext sdk.Dec, amountIn sdk.Dec, amountOut sdk.Dec) {
czarcas7ic marked this conversation as resolved.
Show resolved Hide resolved
if lte {
priceDiff := amountRemaining.Quo(liquidity)
priceNext := sqrtPriceCurrent.Add(priceDiff)
amountIn := calcAmount1Delta(liquidity, priceNext, sqrtPriceCurrent)
amountOut := calcAmount0Delta(liquidity, priceNext, sqrtPriceCurrent)
return priceNext, amountIn, amountOut
} else {
priceNextTop := liquidity.Mul(sqrtPriceCurrent)
priceNextBot := liquidity.Add(amountRemaining.Mul(sqrtPriceCurrent))
priceNext := priceNextTop.Quo(priceNextBot)
amountIn := calcAmount0Delta(liquidity, priceNext, sqrtPriceCurrent)
amountOut := calcAmount1Delta(liquidity, priceNext, sqrtPriceCurrent)
return priceNext, amountIn, amountOut
}
}
Loading