From 717d481578637b18152515ba2fca4a1d0947a72f Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 22 Mar 2023 18:22:33 -0400 Subject: [PATCH] refactor/test: LP events for data integration (#4680) * utilize new position id in position key * add position id to migration response * regen proto * modify withdraw pos in e2e * cmd order * avoid store key formatting with fmt.Sprintf * set default NextPositionId to 1 * lint * return positionId from CreatePos E2E * utilize bytes.Buffer to build keys * Revert "utilize bytes.Buffer to build keys" This reverts commit 0c1bc13ed02008213db5293b4de491f26b859303. * utilize sprintf for key * remove extra positions logic * refactor/test: LP events for data integration * remove duplicate and comment * lint * Update go.mod * Revert "Update go.mod" This reverts commit 4e9787531f5eb1938a1ec6dd879771790e7ca10a. --------- Co-authored-by: Adam Tucker Co-authored-by: p0mvn --- x/concentrated-liquidity/lp.go | 32 ++++++++++++++++++++++++ x/concentrated-liquidity/lp_test.go | 10 ++++++++ x/concentrated-liquidity/msg_server.go | 30 ++++------------------ x/concentrated-liquidity/types/events.go | 1 + 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/x/concentrated-liquidity/lp.go b/x/concentrated-liquidity/lp.go index 7e8ffc3f563..f6ec7c1bbb9 100644 --- a/x/concentrated-liquidity/lp.go +++ b/x/concentrated-liquidity/lp.go @@ -3,6 +3,7 @@ package concentrated_liquidity import ( "errors" "fmt" + "strconv" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -102,6 +103,8 @@ func (k Keeper) createPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddr // Persist the changes made to the cache context if the actual amounts of tokens 0 and 1 are greater than or equal to the given minimum amounts. writeCacheCtx() + emitLiquidityChangeEvent(ctx, types.TypeEvtCreatePosition, positionId, owner, poolId, lowerTick, upperTick, joinTime, freezeDuration, liquidityDelta, actualAmount0, actualAmount1) + return positionId, actualAmount0, actualAmount1, liquidityDelta, joinTime, nil } @@ -183,6 +186,8 @@ func (k Keeper) withdrawPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAd } } + emitLiquidityChangeEvent(ctx, types.TypeEvtWithdrawPosition, positionId, owner, poolId, lowerTick, upperTick, joinTime, freezeDuration, liquidityDelta, actualAmount0, actualAmount1) + return actualAmount0.Neg(), actualAmount1.Neg(), nil } @@ -305,3 +310,30 @@ func (k Keeper) initializeInitialPositionForPool(ctx sdk.Context, pool types.Con } return nil } + +// emitLiquidityChangeEvent emits an event for a liquidity change when creating or withdrawing a position. +// It emits all of the fields uniquely identifying a position such as: +// - position id +// - sender +// - pool id +// - join time +// - freeze duration +// - lower tick +// - upper tick +// It also emits additional attributes for the liquidity added or removed and the actual amounts of asset0 and asset1 it translates to. +func emitLiquidityChangeEvent(ctx sdk.Context, eventType string, positionId uint64, sender sdk.AccAddress, poolId uint64, lowerTick, upperTick int64, joinTime time.Time, freezeDuration time.Duration, liquidityDelta sdk.Dec, actualAmount0, actualAmount1 sdk.Int) { + ctx.EventManager().EmitEvent(sdk.NewEvent( + eventType, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(types.AttributeKeyPositionId, strconv.FormatUint(positionId, 10)), + sdk.NewAttribute(sdk.AttributeKeySender, string(sender)), + sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), + sdk.NewAttribute(types.AttributeLowerTick, strconv.FormatInt(lowerTick, 10)), + sdk.NewAttribute(types.AttributeUpperTick, strconv.FormatInt(upperTick, 10)), + sdk.NewAttribute(types.AttributeFreezeDuration, freezeDuration.String()), + sdk.NewAttribute(types.AttributeJoinTime, joinTime.String()), + sdk.NewAttribute(types.AttributeLiquidity, liquidityDelta.String()), + sdk.NewAttribute(types.AttributeAmount0, actualAmount0.String()), + sdk.NewAttribute(types.AttributeAmount1, actualAmount1.String()), + )) +} diff --git a/x/concentrated-liquidity/lp_test.go b/x/concentrated-liquidity/lp_test.go index d26224f30fa..e9c64fb6fba 100644 --- a/x/concentrated-liquidity/lp_test.go +++ b/x/concentrated-liquidity/lp_test.go @@ -217,9 +217,12 @@ func (s *KeeperTestSuite) TestCreatePosition() { s.Require().NoError(err) } + expectedNumCreatePositionEvents := 1 + // If we want to test a non-first position, we create a first position with a separate account if tc.isNotFirstPosition { s.SetupPosition(1, s.TestAccs[1], DefaultCoin0, DefaultCoin1, tc.lowerTick, tc.upperTick, defaultJoinTime, tc.freezeDuration) + expectedNumCreatePositionEvents += 1 } expectedLiquidityCreated := tc.liquidityAmount @@ -229,6 +232,7 @@ func (s *KeeperTestSuite) TestCreatePosition() { expectedLiquidityCreated = tc.liquidityAmount.QuoInt64(2) s.SetupPosition(1, s.TestAccs[0], DefaultCoin0, DefaultCoin1, tc.lowerTick, tc.upperTick, defaultJoinTime, tc.freezeDuration) + expectedNumCreatePositionEvents += 1 } // Fund test account and create the desired position @@ -290,6 +294,9 @@ func (s *KeeperTestSuite) TestCreatePosition() { // Check tick state s.validateTickUpdates(s.Ctx, tc.poolId, s.TestAccs[0], tc.lowerTick, tc.upperTick, tc.liquidityAmount, tc.expectedFeeGrowthOutsideLower, tc.expectedFeeGrowthOutsideUpper) + + // Validate events emitted. + s.AssertEventEmitted(s.Ctx, types.TypeEvtCreatePosition, expectedNumCreatePositionEvents) }) } } @@ -531,6 +538,9 @@ func (s *KeeperTestSuite) TestWithdrawPosition() { // Check tick state. s.validateTickUpdates(ctx, config.poolId, owner, config.lowerTick, config.upperTick, expectedRemainingLiquidity, config.expectedFeeGrowthOutsideLower, config.expectedFeeGrowthOutsideUpper) + + // Validate event emitted. + s.AssertEventEmitted(s.Ctx, types.TypeEvtWithdrawPosition, 1) }) } } diff --git a/x/concentrated-liquidity/msg_server.go b/x/concentrated-liquidity/msg_server.go index 8450e343377..127b83416f0 100644 --- a/x/concentrated-liquidity/msg_server.go +++ b/x/concentrated-liquidity/msg_server.go @@ -65,7 +65,7 @@ func (server msgServer) CreatePosition(goCtx context.Context, msg *types.MsgCrea return nil, err } - positionId, actualAmount0, actualAmount1, liquidityCreated, joinTime, err := server.keeper.createPosition(ctx, msg.PoolId, sender, msg.TokenDesired0.Amount, msg.TokenDesired1.Amount, msg.TokenMinAmount0, msg.TokenMinAmount1, msg.LowerTick, msg.UpperTick, msg.FreezeDuration) + _, actualAmount0, actualAmount1, liquidityCreated, joinTime, err := server.keeper.createPosition(ctx, msg.PoolId, sender, msg.TokenDesired0.Amount, msg.TokenDesired1.Amount, msg.TokenMinAmount0, msg.TokenMinAmount1, msg.LowerTick, msg.UpperTick, msg.FreezeDuration) if err != nil { return nil, err } @@ -76,21 +76,10 @@ func (server msgServer) CreatePosition(goCtx context.Context, msg *types.MsgCrea sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), ), - sdk.NewEvent( - types.TypeEvtCreatePosition, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), - sdk.NewAttribute(types.AttributeKeyPositionId, strconv.FormatUint(positionId, 10)), - sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(msg.PoolId, 10)), - sdk.NewAttribute(types.AttributeAmount0, actualAmount0.String()), - sdk.NewAttribute(types.AttributeAmount1, actualAmount1.String()), - sdk.NewAttribute(types.AttributeLiquidity, liquidityCreated.String()), - sdk.NewAttribute(types.AttributeJoinTime, joinTime.String()), - sdk.NewAttribute(types.AttributeLowerTick, strconv.FormatInt(msg.LowerTick, 10)), - sdk.NewAttribute(types.AttributeUpperTick, strconv.FormatInt(msg.UpperTick, 10)), - ), }) + // Note: create position event is emitted in keeper.createPosition(...) + return &types.MsgCreatePositionResponse{Amount0: actualAmount0, Amount1: actualAmount1, LiquidityCreated: liquidityCreated, JoinTime: joinTime}, nil } @@ -114,19 +103,10 @@ func (server msgServer) WithdrawPosition(goCtx context.Context, msg *types.MsgWi sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), ), - sdk.NewEvent( - types.TypeEvtWithdrawPosition, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), - sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(msg.PoolId, 10)), - sdk.NewAttribute(types.AttributeLiquidity, msg.LiquidityAmount.String()), - sdk.NewAttribute(types.AttributeAmount0, amount0.String()), - sdk.NewAttribute(types.AttributeAmount1, amount1.String()), - sdk.NewAttribute(types.AttributeLowerTick, strconv.FormatInt(msg.LowerTick, 10)), - sdk.NewAttribute(types.AttributeUpperTick, strconv.FormatInt(msg.UpperTick, 10)), - ), }) + // Note: wthdraw position event is emitted in keeper.withdrawPosition(...) + return &types.MsgWithdrawPositionResponse{Amount0: amount0, Amount1: amount1}, nil } diff --git a/x/concentrated-liquidity/types/events.go b/x/concentrated-liquidity/types/events.go index 7079e14075d..3fa6bf3818c 100644 --- a/x/concentrated-liquidity/types/events.go +++ b/x/concentrated-liquidity/types/events.go @@ -17,6 +17,7 @@ const ( AttributeKeyTokensOut = "tokens_out" AttributeLiquidity = "liquidity" AttributeJoinTime = "join_time" + AttributeFreezeDuration = "freeze_duration" AttributeLowerTick = "lower_tick" AttributeUpperTick = "upper_tick" TypeEvtPoolJoined = "pool_joined"