Skip to content

Commit

Permalink
Merge pull request #57 from b-harvest/patch/issue-L-4
Browse files Browse the repository at this point in the history
refactor: Remove redundant EventTypeMessage emission in custom modules
  • Loading branch information
poorphd authored Jul 10, 2024
2 parents de0451f + 7517098 commit 95b582c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
1 change: 1 addition & 0 deletions x/coinswap/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (suite *TestSuite) SetupTest() {

suite.keeper = suite.app.CoinswapKeeper
suite.queryClient = queryClient
suite.msgServer = keeper.NewMsgServerImpl(suite.keeper)

sdk.SetCoinDenomRegex(func() string {
return `[a-zA-Z][a-zA-Z0-9/\-]{2,127}`
Expand Down
23 changes: 0 additions & 23 deletions x/coinswap/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ func (m msgServer) AddLiquidity(goCtx context.Context, msg *types.MsgAddLiquidit
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
)

return &types.MsgAddLiquidityResponse{
MintToken: &mintToken,
}, nil
Expand Down Expand Up @@ -100,14 +92,6 @@ func (m msgServer) RemoveLiquidity(goCtx context.Context, msg *types.MsgRemoveLi
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
)

var coins = make([]*sdk.Coin, 0, withdrawCoins.Len())
for _, coin := range withdrawCoins {
coins = append(coins, &coin)
Expand Down Expand Up @@ -149,13 +133,6 @@ func (m msgServer) SwapCoin(goCtx context.Context, msg *types.MsgSwapOrder) (*ty
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Input.Address),
),
)
return &types.MsgSwapCoinResponse{}, nil
}

Expand Down
44 changes: 41 additions & 3 deletions x/coinswap/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,51 @@ import (
"math"

sdkmath "cosmossdk.io/math"
"github.com/Canto-Network/Canto/v7/testutil"
"github.com/Canto-Network/Canto/v7/x/coinswap/types"
"github.com/cometbft/cometbft/crypto/tmhash"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/evmos/ethermint/crypto/ethsecp256k1"

"github.com/Canto-Network/Canto/v7/testutil"
"github.com/Canto-Network/Canto/v7/x/coinswap/types"
)

var (
sender = sdk.AccAddress(tmhash.SumTruncated([]byte("sender"))).String()
)

func (suite *TestSuite) TestEventEmit() {
params := types.Params{
Fee: sdkmath.LegacyNewDec(0),
PoolCreationFee: sdk.Coin{sdk.DefaultBondDenom, sdkmath.ZeroInt()},
TaxRate: sdkmath.LegacyNewDec(0),
MaxStandardCoinPerPool: sdkmath.NewInt(10_000_000_000),
MaxSwapAmount: sdk.NewCoins(
sdk.NewInt64Coin("btc", 10_000_000),
),
}
suite.app.CoinswapKeeper.SetParams(suite.ctx, params)

msg := types.MsgAddLiquidity{
MaxToken: sdk.NewCoin("btc", sdkmath.NewInt(1000)),
ExactStandardAmt: sdkmath.NewInt(100),
MinLiquidity: sdkmath.NewInt(100),
Deadline: 2611213344,
Sender: addrSender1.String(),
}
_, err := suite.msgServer.AddLiquidity(suite.ctx, &msg)
suite.NoError(err)

events := suite.ctx.EventManager().Events()
found, index := findEventTypeIndex(events, sdk.EventTypeMessage)
suite.Require().True(found)
suite.Require().Equal("sender", events[index].Attributes[0].Key)
suite.Require().Equal(addrSender1.String(), events[index].Attributes[0].Value)

}

func (suite *TestSuite) TestMsgSwapOrder_ValidateBasic() {
msg := types.MsgSwapOrder{}
suite.Require().Equal("/canto.coinswap.v1.MsgSwapOrder", sdk.MsgTypeURL(&msg))
Expand Down Expand Up @@ -362,3 +391,12 @@ func buildCoin(denom string, amt int64) sdk.Coin {
Amount: sdkmath.NewInt(amt),
}
}

func findEventTypeIndex(events sdk.Events, eventType string) (bool, int) {
for index, event := range events {
if event.Type == eventType {
return true, index
}
}
return false, -1
}

0 comments on commit 95b582c

Please sign in to comment.