diff --git a/x/gamm/keeper/keeper.go b/x/gamm/keeper/keeper.go index e02b39cf5e1..a7e15bff2f9 100644 --- a/x/gamm/keeper/keeper.go +++ b/x/gamm/keeper/keeper.go @@ -2,7 +2,6 @@ package keeper import ( "fmt" - "strconv" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -60,43 +59,6 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey sdk.StoreKey, paramSpace paramtyp } } -func (k *Keeper) createSwapEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, input sdk.Coins, output sdk.Coins) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.TypeEvtTokenSwapped, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, sender.String()), - sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), - sdk.NewAttribute(types.AttributeKeyTokensIn, input.String()), - sdk.NewAttribute(types.AttributeKeyTokensOut, output.String()), - ), - }) -} - -func (k *Keeper) createAddLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.TypeEvtPoolJoined, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, sender.String()), - sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), - sdk.NewAttribute(types.AttributeKeyTokensIn, liquidity.String()), - ), - }) -} - -func (k *Keeper) createRemoveLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.TypeEvtPoolExited, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, sender.String()), - sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), - sdk.NewAttribute(types.AttributeKeyTokensOut, liquidity.String()), - ), - }) -} - // Set the gamm hooks func (k *Keeper) SetHooks(gh types.GammHooks) *Keeper { if k.hooks != nil { diff --git a/x/gamm/keeper/pool.go b/x/gamm/keeper/pool.go index 00ed70992e3..61b780b13b8 100644 --- a/x/gamm/keeper/pool.go +++ b/x/gamm/keeper/pool.go @@ -42,7 +42,8 @@ func (k Keeper) GetPool(ctx sdk.Context, poolId uint64) (types.PoolI, error) { return pool, nil } -func (k Keeper) GetPoolForSwap(ctx sdk.Context, poolId uint64) (types.PoolI, error) { +// Get pool, and check if the pool is active / allowed to be swapped against +func (k Keeper) getPoolForSwap(ctx sdk.Context, poolId uint64) (types.PoolI, error) { pool, err := k.GetPool(ctx, poolId) if err != nil { return &balancer.Pool{}, err diff --git a/x/gamm/keeper/pool_service.go b/x/gamm/keeper/pool_service.go index 569270edecd..0b78b92acc0 100644 --- a/x/gamm/keeper/pool_service.go +++ b/x/gamm/keeper/pool_service.go @@ -196,7 +196,7 @@ func (k Keeper) JoinSwapExactAmountIn( tokensIn sdk.Coins, shareOutMinAmount sdk.Int, ) (shareOutAmount sdk.Int, err error) { - pool, err := k.GetPoolForSwap(ctx, poolId) + pool, err := k.getPoolForSwap(ctx, poolId) if err != nil { return sdk.Int{}, err } @@ -228,7 +228,7 @@ func (k Keeper) JoinSwapShareAmountOut( shareOutAmount sdk.Int, tokenInMaxAmount sdk.Int, ) (tokenInAmount sdk.Int, err error) { - pool, err := k.GetPoolForSwap(ctx, poolId) + pool, err := k.getPoolForSwap(ctx, poolId) if err != nil { return sdk.Int{}, err } diff --git a/x/gamm/keeper/share.go b/x/gamm/keeper/share.go index 0b52312e15a..bcd2b6004ac 100644 --- a/x/gamm/keeper/share.go +++ b/x/gamm/keeper/share.go @@ -22,7 +22,7 @@ func (k Keeper) applyJoinPoolStateChange(ctx sdk.Context, pool types.PoolI, join return err } - k.createAddLiquidityEvent(ctx, joiner, pool.GetId(), joinCoins) + ctx.EventManager().EmitEvent(types.CreateAddLiquidityEvent(ctx, joiner, pool.GetId(), joinCoins)) k.hooks.AfterJoinPool(ctx, joiner, pool.GetId(), joinCoins, numShares) k.RecordTotalLiquidityIncrease(ctx, joinCoins) return nil @@ -44,7 +44,7 @@ func (k Keeper) applyExitPoolStateChange(ctx sdk.Context, pool types.PoolI, exit return err } - k.createRemoveLiquidityEvent(ctx, exiter, pool.GetId(), exitCoins) + ctx.EventManager().EmitEvent(types.CreateRemoveLiquidityEvent(ctx, exiter, pool.GetId(), exitCoins)) k.hooks.AfterExitPool(ctx, exiter, pool.GetId(), numShares, exitCoins) k.RecordTotalLiquidityDecrease(ctx, exitCoins) return nil diff --git a/x/gamm/keeper/swap.go b/x/gamm/keeper/swap.go index 81f60c14ef1..c434196b32d 100644 --- a/x/gamm/keeper/swap.go +++ b/x/gamm/keeper/swap.go @@ -142,7 +142,7 @@ func (k Keeper) updatePoolForSwap( return err } - k.createSwapEvent(ctx, sender, pool.GetId(), tokensIn, tokensOut) + ctx.EventManager().EmitEvent(types.CreateSwapEvent(ctx, sender, pool.GetId(), tokensIn, tokensOut)) k.hooks.AfterSwap(ctx, sender, pool.GetId(), tokensIn, tokensOut) k.RecordTotalLiquidityIncrease(ctx, tokensIn) k.RecordTotalLiquidityDecrease(ctx, tokensOut) diff --git a/x/gamm/types/events.go b/x/gamm/types/events.go index baed080cf77..e65c07f42d1 100644 --- a/x/gamm/types/events.go +++ b/x/gamm/types/events.go @@ -1,5 +1,11 @@ package types +import ( + "strconv" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + const ( TypeEvtPoolJoined = "pool_joined" TypeEvtPoolExited = "pool_exited" @@ -12,3 +18,34 @@ const ( AttributeKeyTokensIn = "tokens_in" AttributeKeyTokensOut = "tokens_out" ) + +func CreateSwapEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, input sdk.Coins, output sdk.Coins) sdk.Event { + return sdk.NewEvent( + TypeEvtTokenSwapped, + sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, sender.String()), + sdk.NewAttribute(AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), + sdk.NewAttribute(AttributeKeyTokensIn, input.String()), + sdk.NewAttribute(AttributeKeyTokensOut, output.String()), + ) +} + +func CreateAddLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) sdk.Event { + return sdk.NewEvent( + TypeEvtPoolJoined, + sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, sender.String()), + sdk.NewAttribute(AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), + sdk.NewAttribute(AttributeKeyTokensIn, liquidity.String()), + ) +} + +func CreateRemoveLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) sdk.Event { + return sdk.NewEvent( + TypeEvtPoolExited, + sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, sender.String()), + sdk.NewAttribute(AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), + sdk.NewAttribute(AttributeKeyTokensOut, liquidity.String()), + ) +}