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

perf: conver tick and exponent at price one to int64 #5190

Merged
merged 21 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 20 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
13 changes: 3 additions & 10 deletions proto/osmosis/concentrated-liquidity/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,12 @@ message Pool {
(gogoproto.moretags) = "yaml:\"spot_price\"",
(gogoproto.nullable) = false
];
string current_tick = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"current_tick\"",
(gogoproto.nullable) = false
];
int64 current_tick = 8 [ (gogoproto.moretags) = "yaml:\"current_tick\"" ];
// tick_spacing must be one of the authorized_tick_spacing values set in the
// concentrated-liquidity parameters
uint64 tick_spacing = 9 [ (gogoproto.moretags) = "yaml:\"tick_spacing\"" ];
string exponent_at_price_one = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"exponent_at_price_one\"",
(gogoproto.nullable) = false
];
int64 exponent_at_price_one = 10
[ (gogoproto.moretags) = "yaml:\"exponent_at_price_one\"" ];

// swap_fee is the ratio that is charged on the amount of token in.
string swap_fee = 11 [
Expand Down
14 changes: 7 additions & 7 deletions tests/cl-genesis-positions/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ func ConvertSubgraphToOsmosisGenesis(positionCreatorAddresses []sdk.AccAddress,
panic(err)
}

if lowerTickOsmosis.GT(upperTickOsmosis) {
if lowerTickOsmosis > upperTickOsmosis {
fmt.Printf("lowerTickOsmosis (%s) > upperTickOsmosis (%s), skipping", lowerTickOsmosis, upperTickOsmosis)
continue
}

if lowerTickOsmosis.Equal(upperTickOsmosis) {
if lowerTickOsmosis == upperTickOsmosis {
// bump up the upper tick by one. We don't care about having exactly the same tick range
// Just a roughly similar breakdown
upperTickOsmosis = upperTickOsmosis.Add(sdk.OneInt())
upperTickOsmosis = upperTickOsmosis + 1
}

depositedAmount0, failedParsing := parseStringToInt(uniV3Position.DepositedToken0)
Expand All @@ -186,8 +186,8 @@ func ConvertSubgraphToOsmosisGenesis(positionCreatorAddresses []sdk.AccAddress,
position, err := clMsgServer.CreatePosition(sdk.WrapSDKContext(osmosis.Ctx), &cltypes.MsgCreatePosition{
PoolId: poolId,
Sender: randomCreator.String(),
LowerTick: lowerTickOsmosis.Int64(),
UpperTick: upperTickOsmosis.Int64(),
LowerTick: lowerTickOsmosis,
UpperTick: upperTickOsmosis,
TokensProvided: tokensProvided,
TokenMinAmount0: sdk.ZeroInt(),
TokenMinAmount1: sdk.ZeroInt(),
Expand All @@ -208,8 +208,8 @@ func ConvertSubgraphToOsmosisGenesis(positionCreatorAddresses []sdk.AccAddress,
JoinTime: osmosis.Ctx.BlockTime().Format("2006-01-02T15:04:05Z"), // ISO 8601
Liquidity: position.LiquidityCreated.String(),
PositionID: strconv.FormatUint(position.PositionId, 10),
LowerTick: lowerTickOsmosis.String(),
UpperTick: upperTickOsmosis.String(),
LowerTick: strconv.FormatInt(lowerTickOsmosis, 10),
UpperTick: strconv.FormatInt(upperTickOsmosis, 10),
})
}

Expand Down
20 changes: 10 additions & 10 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
v16 "github.com/osmosis-labs/osmosis/v15/app/upgrades/v16"
"github.com/osmosis-labs/osmosis/v15/tests/e2e/configurer/config"
"github.com/osmosis-labs/osmosis/v15/tests/e2e/initialization"
cl "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity"
clmath "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/math"
cltypes "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types"
)

Expand Down Expand Up @@ -222,7 +222,7 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() {
concentratedPool := s.updatedPool(chainANode, poolID)

// Sanity check that pool initialized with valid parameters (the ones that we haven't explicitly specified)
s.Require().Equal(concentratedPool.GetCurrentTick(), sdk.ZeroInt())
s.Require().Equal(concentratedPool.GetCurrentTick(), int64(0))
s.Require().Equal(concentratedPool.GetCurrentSqrtPrice(), sdk.ZeroDec())
s.Require().Equal(concentratedPool.GetLiquidity(), sdk.ZeroDec())

Expand Down Expand Up @@ -365,15 +365,15 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() {
// * Uncollected fees from multiple swaps are correctly summed up and collected

// tickOffset is a tick index after the next initialized tick to which this swap needs to move the current price
tickOffset := sdk.NewInt(300)
tickOffset := int64(300)
sqrtPriceBeforeSwap = concentratedPool.GetCurrentSqrtPrice()
liquidityBeforeSwap = concentratedPool.GetLiquidity()
nextInitTick := sdk.NewInt(40000) // address1 position1's upper tick
nextInitTick := int64(40000) // address1 position1's upper tick

// Calculate sqrtPrice after and at the next initialized tick (upperTick of address1 position1 - 40000)
_, sqrtPriceAfterNextInitializedTick, err := cl.TickToSqrtPrice(nextInitTick.Add(tickOffset))
_, sqrtPriceAfterNextInitializedTick, err := clmath.TickToSqrtPrice(nextInitTick + tickOffset)
s.Require().NoError(err)
_, sqrtPriceAtNextInitializedTick, err := cl.TickToSqrtPrice(nextInitTick)
_, sqrtPriceAtNextInitializedTick, err := clmath.TickToSqrtPrice(nextInitTick)
s.Require().NoError(err)

// Calculate Δ(sqrtPrice):
Expand Down Expand Up @@ -502,20 +502,20 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() {
// * liquidity of positions that come in range are correctly kicked in

// tickOffset is a tick index after the next initialized tick to which this swap needs to move the current price
tickOffset = sdk.NewInt(300)
tickOffset = 300
sqrtPriceBeforeSwap = concentratedPool.GetCurrentSqrtPrice()
liquidityBeforeSwap = concentratedPool.GetLiquidity()
nextInitTick = sdk.NewInt(40000)
nextInitTick = 40000

// Calculate amount required to get to
// 1) next initialized tick
// 2) tick below next initialized (-300)
// Using: CalcAmount0Delta = liquidity * ((sqrtPriceB - sqrtPriceA) / (sqrtPriceB * sqrtPriceA))

// Calculate sqrtPrice after and at the next initialized tick (which is upperTick of address1 position1 - 40000)
_, sqrtPricebBelowNextInitializedTick, err := cl.TickToSqrtPrice(nextInitTick.Sub(tickOffset))
_, sqrtPricebBelowNextInitializedTick, err := clmath.TickToSqrtPrice(nextInitTick - tickOffset)
s.Require().NoError(err)
_, sqrtPriceAtNextInitializedTick, err = cl.TickToSqrtPrice(nextInitTick)
_, sqrtPriceAtNextInitializedTick, err = clmath.TickToSqrtPrice(nextInitTick)
s.Require().NoError(err)

// Calculate numerators
Expand Down
16 changes: 8 additions & 8 deletions x/concentrated-liquidity/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ func BenchmarkSwapExactAmountIn(b *testing.B) {
// Decreasing price so want to be below current tick

// minTick <= lowerTick <= currentTick
lowerTick = rand.Int63n(currentTick.Int64()-types.MinTick+1) + types.MinTick
lowerTick = rand.Int63n(currentTick-types.MinTick+1) + types.MinTick
// lowerTick <= upperTick <= currentTick
upperTick = currentTick.Int64() - rand.Int63n(int64(math.Abs(float64(currentTick.Int64()-lowerTick))))
upperTick = currentTick - rand.Int63n(int64(math.Abs(float64(currentTick-lowerTick))))
} else {
// Increasing price so want to be above current tick

// currentTick <= lowerTick <= maxTick
lowerTick := rand.Int63n(types.MaxTick-currentTick.Int64()+1) + currentTick.Int64()
lowerTick := rand.Int63n(types.MaxTick-currentTick+1) + currentTick
// lowerTick <= upperTick <= maxTick
upperTick = types.MaxTick - rand.Int63n(int64(math.Abs(float64(types.MaxTick-lowerTick))))
}
Expand Down Expand Up @@ -171,8 +171,8 @@ func BenchmarkSwapExactAmountIn(b *testing.B) {
// Within 10 ticks of the current
if tickSpacing <= 10 {
for i := 0; i < numberOfPositions; i++ {
lowerTick := currentTick.Int64() - 10
upperTick := currentTick.Int64() + 10
lowerTick := currentTick - 10
upperTick := currentTick + 10

tokenDesired0 := sdk.NewCoin(denom0, sdk.NewInt(maxAmountDeposited).MulRaw(5))
tokenDesired1 := sdk.NewCoin(denom1, sdk.NewInt(maxAmountDeposited).MulRaw(5))
Expand All @@ -190,8 +190,8 @@ func BenchmarkSwapExactAmountIn(b *testing.B) {

// Within 100 ticks of the current
for i := 0; i < numberOfPositions; i++ {
lowerTick := currentTick.Int64() - 100
upperTick := currentTick.Int64() + 100
lowerTick := currentTick - 100
upperTick := currentTick + 100
// Normalize lowerTick to be a multiple of tickSpacing
lowerTick = lowerTick + (tickSpacing - lowerTick%tickSpacing)
// Normalize upperTick to be a multiple of tickSpacing
Expand All @@ -214,7 +214,7 @@ func BenchmarkSwapExactAmountIn(b *testing.B) {
swapAmountIn := sdk.MustNewDecFromStr(amountIn).TruncateInt()
largeSwapInCoin := sdk.NewCoin(denomIn, swapAmountIn)

liquidityNet, err := clKeeper.GetTickLiquidityNetInDirection(s.Ctx, pool.GetId(), largeSwapInCoin.Denom, currentTick, sdk.Int{})
liquidityNet, err := clKeeper.GetTickLiquidityNetInDirection(s.Ctx, pool.GetId(), largeSwapInCoin.Denom, sdk.NewInt(currentTick), sdk.Int{})
noError(err)

fmt.Println("num_ticks_traversed", len(liquidityNet))
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/client/query_proto_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (q Querier) LiquidityNetInDirection(ctx sdk.Context, req clquery.LiquidityN
return nil, err
}

return &clquery.LiquidityNetInDirectionResponse{LiquidityDepths: liquidityDepths, CurrentLiquidity: pool.GetLiquidity(), CurrentTick: pool.GetCurrentTick().Int64()}, nil
return &clquery.LiquidityNetInDirectionResponse{LiquidityDepths: liquidityDepths, CurrentLiquidity: pool.GetLiquidity(), CurrentTick: pool.GetCurrentTick()}, nil
}

func (q Querier) ClaimableFees(ctx sdk.Context, req clquery.ClaimableFeesRequest) (*clquery.ClaimableFeesResponse, error) {
Expand Down
11 changes: 0 additions & 11 deletions x/concentrated-liquidity/export_internal.go

This file was deleted.

10 changes: 5 additions & 5 deletions x/concentrated-liquidity/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k Keeper) SendCoinsBetweenPoolAndUser(ctx sdk.Context, denom0, denom1 stri
return k.sendCoinsBetweenPoolAndUser(ctx, denom0, denom1, amount0, amount1, sender, receiver)
}

func (k Keeper) CalcInAmtGivenOutInternal(ctx sdk.Context, desiredTokenOut sdk.Coin, tokenInDenom string, swapFee sdk.Dec, priceLimit sdk.Dec, poolId uint64) (writeCtx func(), tokenIn, tokenOut sdk.Coin, updatedTick sdk.Int, updatedLiquidity, updatedSqrtPrice sdk.Dec, err error) {
func (k Keeper) CalcInAmtGivenOutInternal(ctx sdk.Context, desiredTokenOut sdk.Coin, tokenInDenom string, swapFee sdk.Dec, priceLimit sdk.Dec, poolId uint64) (writeCtx func(), tokenIn, tokenOut sdk.Coin, updatedTick int64, updatedLiquidity, updatedSqrtPrice sdk.Dec, err error) {
return k.calcInAmtGivenOut(ctx, desiredTokenOut, tokenInDenom, swapFee, priceLimit, poolId)
}

Expand All @@ -62,7 +62,7 @@ func (k Keeper) SwapOutAmtGivenIn(
tokenIn sdk.Coin,
tokenOutDenom string,
swapFee sdk.Dec,
priceLimit sdk.Dec) (calcTokenIn, calcTokenOut sdk.Coin, currentTick sdk.Int, liquidity, sqrtPrice sdk.Dec, err error) {
priceLimit sdk.Dec) (calcTokenIn, calcTokenOut sdk.Coin, currentTick int64, liquidity, sqrtPrice sdk.Dec, err error) {
return k.swapOutAmtGivenIn(ctx, sender, pool, tokenIn, tokenOutDenom, swapFee, priceLimit)
}

Expand All @@ -74,11 +74,11 @@ func (k Keeper) ComputeOutAmtGivenIn(
swapFee sdk.Dec,
priceLimit sdk.Dec,

) (calcTokenIn, calcTokenOut sdk.Coin, currentTick sdk.Int, liquidity, sqrtPrice sdk.Dec, err error) {
) (calcTokenIn, calcTokenOut sdk.Coin, currentTick int64, liquidity, sqrtPrice sdk.Dec, err error) {
return k.computeOutAmtGivenIn(ctx, poolId, tokenInMin, tokenOutDenom, swapFee, priceLimit)
}

func (k *Keeper) SwapInAmtGivenOut(ctx sdk.Context, sender sdk.AccAddress, pool types.ConcentratedPoolExtension, desiredTokenOut sdk.Coin, tokenInDenom string, swapFee sdk.Dec, priceLimit sdk.Dec) (calcTokenIn, calcTokenOut sdk.Coin, currentTick sdk.Int, liquidity, sqrtPrice sdk.Dec, err error) {
func (k *Keeper) SwapInAmtGivenOut(ctx sdk.Context, sender sdk.AccAddress, pool types.ConcentratedPoolExtension, desiredTokenOut sdk.Coin, tokenInDenom string, swapFee sdk.Dec, priceLimit sdk.Dec) (calcTokenIn, calcTokenOut sdk.Coin, currentTick int64, liquidity, sqrtPrice sdk.Dec, err error) {
return k.swapInAmtGivenOut(ctx, sender, pool, desiredTokenOut, tokenInDenom, swapFee, priceLimit)
}

Expand Down Expand Up @@ -281,7 +281,7 @@ func (k Keeper) GetAllPositions(ctx sdk.Context) ([]model.Position, error) {
return k.getAllPositions(ctx)
}

func (k Keeper) UpdatePoolForSwap(ctx sdk.Context, pool types.ConcentratedPoolExtension, sender sdk.AccAddress, tokenIn sdk.Coin, tokenOut sdk.Coin, newCurrentTick sdk.Int, newLiquidity sdk.Dec, newSqrtPrice sdk.Dec) error {
func (k Keeper) UpdatePoolForSwap(ctx sdk.Context, pool types.ConcentratedPoolExtension, sender sdk.AccAddress, tokenIn sdk.Coin, tokenOut sdk.Coin, newCurrentTick int64, newLiquidity sdk.Dec, newSqrtPrice sdk.Dec) error {
return k.updatePoolForSwap(ctx, pool, sender, tokenIn, tokenOut, newCurrentTick, newLiquidity, newSqrtPrice)
}

Expand Down
4 changes: 2 additions & 2 deletions x/concentrated-liquidity/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (k Keeper) getFeeGrowthOutside(ctx sdk.Context, poolId uint64, lowerTick, u
if err != nil {
return sdk.DecCoins{}, err
}
currentTick := pool.GetCurrentTick().Int64()
currentTick := pool.GetCurrentTick()

// get lower, upper tick info
lowerTickInfo, err := k.GetTickInfo(ctx, poolId, lowerTick)
Expand Down Expand Up @@ -164,7 +164,7 @@ func (k Keeper) getInitialFeeGrowthOppositeDirectionOfLastTraversalForTick(ctx s
return sdk.DecCoins{}, err
}

currentTick := pool.GetCurrentTick().Int64()
currentTick := pool.GetCurrentTick()
if currentTick >= tick {
feeAccumulator, err := k.GetFeeAccumulator(ctx, poolId)
if err != nil {
Expand Down
Loading