Skip to content

Commit

Permalink
(CL Fees): update tick feeGrowthOutside in initOrUpdateTick (#3969)
Browse files Browse the repository at this point in the history
* Init fee growth outside

* Remove Pool call
  • Loading branch information
mattverse authored Jan 11, 2023
1 parent 8c0910c commit c3580f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions x/concentrated-liquidity/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
// if we are initializing or updating an upper tick, we subtract the liquidityIn from the LiquidityNet
// if we are initializing or updating an lower tick, we add the liquidityIn from the LiquidityNet
func (k Keeper) initOrUpdateTick(ctx sdk.Context, poolId uint64, tickIndex int64, liquidityIn sdk.Dec, upper bool) (err error) {
if !k.poolExists(ctx, poolId) {
return types.PoolNotFoundError{PoolId: poolId}
pool, err := k.getPoolById(ctx, poolId)
if err != nil {
return err
}

currentTick := pool.GetCurrentTick().Int64()

tickInfo, err := k.getTickInfo(ctx, poolId, tickIndex)
if err != nil {
return err
Expand All @@ -38,6 +41,16 @@ func (k Keeper) initOrUpdateTick(ctx sdk.Context, poolId uint64, tickIndex int64
tickInfo.LiquidityNet = tickInfo.LiquidityNet.Add(liquidityIn)
}

// if given tickIndex is LTE to current tick, tick's fee growth outside is set as fee accumulator's value
if tickIndex <= currentTick {
accum, err := k.getFeeAccumulator(ctx, poolId)
if err != nil {
return err
}

tickInfo.FeeGrowthOutside = accum.GetValue()
}

k.SetTickInfo(ctx, poolId, tickIndex, tickInfo)

return nil
Expand Down
13 changes: 13 additions & 0 deletions x/concentrated-liquidity/tick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ func (s *KeeperTestSuite) TestInitOrUpdateTick() {

// Create a default CL pool
s.PrepareConcentratedPool()
_, err := s.App.ConcentratedLiquidityKeeper.GetFeeAccumulator(s.Ctx, 1)
feeAccum, err := s.App.ConcentratedLiquidityKeeper.GetFeeAccumulator(s.Ctx, 1)
s.Require().NoError(err)

// manually update accumulator for testing
defaultAccumCoins := sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(50)))
feeAccum.UpdateAccumulator(defaultAccumCoins)

// If tickExists set, initialize the specified tick with defaultLiquidityAmt
preexistingLiquidity := sdk.ZeroDec()
Expand Down Expand Up @@ -260,6 +267,12 @@ func (s *KeeperTestSuite) TestInitOrUpdateTick() {
// Check that the initialized or updated tick matches our expectation
s.Require().Equal(test.expectedLiquidityNet, tickInfo.LiquidityNet)
s.Require().Equal(test.expectedLiquidityGross, tickInfo.LiquidityGross)

if test.param.tickIndex <= 0 {
s.Require().Equal(defaultAccumCoins, tickInfo.FeeGrowthOutside)
} else {
s.Require().Equal(sdk.DecCoins(nil), tickInfo.FeeGrowthOutside)
}
})
}
}
Expand Down

0 comments on commit c3580f6

Please sign in to comment.