Skip to content

Commit

Permalink
Refactor event creation to types/events.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Mar 23, 2022
1 parent 6d53e1d commit b7d8bb7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
38 changes: 0 additions & 38 deletions x/gamm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion x/gamm/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/gamm/keeper/pool_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions x/gamm/keeper/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions x/gamm/types/events.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package types

import (
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
TypeEvtPoolJoined = "pool_joined"
TypeEvtPoolExited = "pool_exited"
Expand All @@ -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()),
)
}

0 comments on commit b7d8bb7

Please sign in to comment.