Skip to content

Commit

Permalink
perf: convert ticks to int64 (#5189)
Browse files Browse the repository at this point in the history
* perf: convert ticks to int64

* updates

* fix test

* updates

* updates

* Update x/concentrated-liquidity/math/tick.go

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>

* nit

* comment

* comment

* >=

* lint

* lint

---------

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
  • Loading branch information
p0mvn and ValarDragon authored May 17, 2023
1 parent 30fe496 commit db7ced1
Show file tree
Hide file tree
Showing 21 changed files with 433 additions and 350 deletions.
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
22 changes: 11 additions & 11 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(), int64(0))
s.Require().Equal(concentratedPool.GetCurrentSqrtPrice(), sdk.ZeroDec())
s.Require().Equal(concentratedPool.GetLiquidity(), sdk.ZeroDec())

Expand All @@ -231,7 +231,7 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() {
s.Require().Equal(concentratedPool.GetToken0(), denom0)
s.Require().Equal(concentratedPool.GetToken1(), denom1)
s.Require().Equal(concentratedPool.GetTickSpacing(), tickSpacing)
s.Require().Equal(concentratedPool.GetExponentAtPriceOne(), cltypes.ExponentAtPriceOne)
s.Require().Equal(concentratedPool.GetExponentAtPriceOne().Int64(), cltypes.ExponentAtPriceOne)
s.Require().Equal(concentratedPool.GetSwapFee(sdk.Context{}), sdk.MustNewDecFromStr(swapFee))

fundTokens := []string{"100000000uosmo", "100000000uion", "100000000stake"}
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
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
7 changes: 3 additions & 4 deletions x/concentrated-liquidity/fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,7 @@ func (suite *KeeperTestSuite) TestGetInitialFeeGrowthOppositeDirectionOfLastTrav
validPoolId = 1
)

initialPoolTickInt, err := math.PriceToTickRoundDown(DefaultAmt1.ToDec().Quo(DefaultAmt0.ToDec()), DefaultTickSpacing)
initialPoolTick := initialPoolTickInt.Int64()
initialPoolTick, err := math.PriceToTickRoundDown(DefaultAmt1.ToDec().Quo(DefaultAmt0.ToDec()), DefaultTickSpacing)
suite.Require().NoError(err)

tests := map[string]struct {
Expand Down Expand Up @@ -1580,12 +1579,12 @@ func (s *KeeperTestSuite) CollectAndAssertFees(ctx sdk.Context, poolId uint64, t

// Claim consecutive range position fees across two of four accounts
for i := 0; i < positions.numConsecutive; i++ {
totalFeesCollected = s.collectFeesAndCheckInvariance(ctx, i, DefaultExponentConsecutivePositionLowerTick.Int64(), DefaultExponentConsecutivePositionUpperTick.Int64(), positionIds[2][i], totalFeesCollected, expectedFeeDenoms[2], activeTicks)
totalFeesCollected = s.collectFeesAndCheckInvariance(ctx, i, DefaultExponentConsecutivePositionLowerTick, DefaultExponentConsecutivePositionUpperTick, positionIds[2][i], totalFeesCollected, expectedFeeDenoms[2], activeTicks)
}

// Claim overlapping range position fees on one of four accounts
for i := 0; i < positions.numOverlapping; i++ {
totalFeesCollected = s.collectFeesAndCheckInvariance(ctx, i, DefaultExponentOverlappingPositionLowerTick.Int64(), DefaultExponentOverlappingPositionUpperTick.Int64(), positionIds[3][i], totalFeesCollected, expectedFeeDenoms[3], activeTicks)
totalFeesCollected = s.collectFeesAndCheckInvariance(ctx, i, DefaultExponentOverlappingPositionLowerTick, DefaultExponentOverlappingPositionUpperTick, positionIds[3][i], totalFeesCollected, expectedFeeDenoms[3], activeTicks)
}

// Define error tolerance
Expand Down
4 changes: 2 additions & 2 deletions x/concentrated-liquidity/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func (s *KeeperTestSuite) SetupFullRangePositionAcc(poolId uint64, owner sdk.Acc
}

func (s *KeeperTestSuite) SetupConsecutiveRangePositionAcc(poolId uint64, owner sdk.AccAddress) uint64 {
_, positionId := s.SetupPosition(poolId, owner, DefaultCoins, DefaultExponentConsecutivePositionLowerTick.Int64(), DefaultExponentConsecutivePositionUpperTick.Int64(), s.Ctx.BlockTime())
_, positionId := s.SetupPosition(poolId, owner, DefaultCoins, DefaultExponentConsecutivePositionLowerTick, DefaultExponentConsecutivePositionUpperTick, s.Ctx.BlockTime())
return positionId
}

func (s *KeeperTestSuite) SetupOverlappingRangePositionAcc(poolId uint64, owner sdk.AccAddress) uint64 {
_, positionId := s.SetupPosition(poolId, owner, DefaultCoins, DefaultExponentOverlappingPositionLowerTick.Int64(), DefaultExponentOverlappingPositionUpperTick.Int64(), s.Ctx.BlockTime())
_, positionId := s.SetupPosition(poolId, owner, DefaultCoins, DefaultExponentOverlappingPositionLowerTick, DefaultExponentOverlappingPositionUpperTick, s.Ctx.BlockTime())
return positionId
}

Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (k Keeper) initializeInitialPositionForPool(ctx sdk.Context, pool types.Con
// In such a case, we do not want to round the sqrt price to 100_000_000 X/Y, but rather
// let it float within the possible tick range.
pool.SetCurrentSqrtPrice(initialCurSqrtPrice)
pool.SetCurrentTick(initialTick)
pool.SetCurrentTick(sdk.NewInt(initialTick))
err = k.setPool(ctx, pool)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions x/concentrated-liquidity/math/precompute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
)

var (
sdkOneInt = sdk.OneInt()
sdkOneDec = sdk.NewDec(1)
sdkOneDec = sdk.OneDec()
sdkNineDec = sdk.NewDec(9)
sdkTenDec = sdk.NewDec(10)
powersOfTen []sdk.Dec
Expand Down
Loading

0 comments on commit db7ced1

Please sign in to comment.