Skip to content

Commit

Permalink
refactor/test: LP events for data integration (#4680)
Browse files Browse the repository at this point in the history
* 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 0c1bc13.

* 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 4e97875.

---------

Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: p0mvn <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2023
1 parent 9303268 commit 717d481
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
32 changes: 32 additions & 0 deletions x/concentrated-liquidity/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package concentrated_liquidity
import (
"errors"
"fmt"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()),
))
}
10 changes: 10 additions & 0 deletions x/concentrated-liquidity/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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)
})
}
}
Expand Down
30 changes: 5 additions & 25 deletions x/concentrated-liquidity/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions x/concentrated-liquidity/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 717d481

Please sign in to comment.