From fd83da7a05d4b9eed9d482605e077b42c719ad6f Mon Sep 17 00:00:00 2001 From: Robert Pirtle Date: Tue, 30 May 2023 13:06:46 -0700 Subject: [PATCH] feat(evmutil)!: emit events on MsgConvertCosmosCoinToERC20 (#1604) * better error message for mismatched events * rename evm asset conversion event types * emit message event for MsgConvertCosmosCoinToERC20 * emit convert_cosmos_coin_to_erc20 event --- CHANGELOG.md | 5 +++ x/evmutil/keeper/conversion_cosmos_native.go | 8 +++- .../keeper/conversion_cosmos_native_test.go | 22 ++++++++++ x/evmutil/keeper/msg_server.go | 8 +++- x/evmutil/keeper/msg_server_test.go | 19 +++++++- x/evmutil/spec/04_events.md | 43 ++++++++++++------- x/evmutil/testutil/suite.go | 14 +++++- x/evmutil/types/events.go | 6 ++- 8 files changed, 102 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aabec15..099928ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,9 +41,13 @@ Ref: https://keepachangelog.com/en/1.0.0/ - (evmutil) [#1591] & [#1596] Configure module to support deploying ERC20KavaWrappedCosmosCoin contracts - (evmutil) [#1598] Track deployed ERC20 contract addresses for representing cosmos coins in module state - (evmutil) [#1603] Add MsgConvertCosmosCoinToERC20 for converting an sdk.Coin to an ERC20 in the EVM +- (evmutil) [#1604] Emit events for MsgConvertCosmosCoinToERC20: `message` & `convert_cosmos_coin_to_erc20` ### Client Breaking - (evmutil) [#1603] Renamed error `ErrConversionNotEnabled` to `ErrEVMConversionNotEnabled` +- (evmutil) [#1604] Renamed event types + - `convert_erc20_to_coin` -> `convert_evm_erc20_to_coin` + - `convert_coin_to_erc20` -> `convert_evm_erc20_from_coin` ## [v0.23.0] @@ -246,6 +250,7 @@ the [changelog](https://github.com/cosmos/cosmos-sdk/blob/v0.38.4/CHANGELOG.md). - [#257](https://github.com/Kava-Labs/kava/pulls/257) Include scripts to run large-scale simulations remotely using aws-batch +[#1604]: https://github.com/Kava-Labs/kava/pull/1604 [#1603]: https://github.com/Kava-Labs/kava/pull/1603 [#1598]: https://github.com/Kava-Labs/kava/pull/1598 [#1596]: https://github.com/Kava-Labs/kava/pull/1596 diff --git a/x/evmutil/keeper/conversion_cosmos_native.go b/x/evmutil/keeper/conversion_cosmos_native.go index 6296f398..d59bdffd 100644 --- a/x/evmutil/keeper/conversion_cosmos_native.go +++ b/x/evmutil/keeper/conversion_cosmos_native.go @@ -44,7 +44,13 @@ func (k *Keeper) ConvertCosmosCoinToERC20( return err } - // TODO emit conversion event + ctx.EventManager().EmitEvent(sdk.NewEvent( + types.EventTypeConvertCosmosCoinToERC20, + sdk.NewAttribute(types.AttributeKeyInitiator, initiator.String()), + sdk.NewAttribute(types.AttributeKeyReceiver, receiver.String()), + sdk.NewAttribute(types.AttributeKeyERC20Address, contractAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyAmount, amount.String()), + )) return nil } diff --git a/x/evmutil/keeper/conversion_cosmos_native_test.go b/x/evmutil/keeper/conversion_cosmos_native_test.go index 154f79ce..4fded163 100644 --- a/x/evmutil/keeper/conversion_cosmos_native_test.go +++ b/x/evmutil/keeper/conversion_cosmos_native_test.go @@ -119,6 +119,17 @@ func (suite *ConversionCosmosNativeSuite) TestConvertCosmosCoinToERC20() { checkBalanceOf(receiver1, amount.Amount) // total supply of erc20 should have increased checkTotalSupply(amount.Amount) + + // event should be emitted + suite.EventsContains(suite.GetEvents(), + sdk.NewEvent( + types.EventTypeConvertCosmosCoinToERC20, + sdk.NewAttribute(types.AttributeKeyInitiator, initiator.String()), + sdk.NewAttribute(types.AttributeKeyReceiver, receiver1.String()), + sdk.NewAttribute(types.AttributeKeyERC20Address, contractAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyAmount, amount.String()), + ), + ) }) suite.Run("2nd deploy uses same contract", func() { @@ -147,5 +158,16 @@ func (suite *ConversionCosmosNativeSuite) TestConvertCosmosCoinToERC20() { checkBalanceOf(receiver2, amount.Amount) // total supply of erc20 should have increased checkTotalSupply(amount.Amount.MulRaw(2)) + + // event should be emitted + suite.EventsContains(suite.GetEvents(), + sdk.NewEvent( + types.EventTypeConvertCosmosCoinToERC20, + sdk.NewAttribute(types.AttributeKeyInitiator, initiator.String()), + sdk.NewAttribute(types.AttributeKeyReceiver, receiver2.String()), + sdk.NewAttribute(types.AttributeKeyERC20Address, contractAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyAmount, amount.String()), + ), + ) }) } diff --git a/x/evmutil/keeper/msg_server.go b/x/evmutil/keeper/msg_server.go index 855f48a3..e5c9c0e6 100644 --- a/x/evmutil/keeper/msg_server.go +++ b/x/evmutil/keeper/msg_server.go @@ -139,7 +139,13 @@ func (s msgServer) ConvertCosmosCoinToERC20( return nil, err } - // TODO: emit message event + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Initiator), + ), + ) return &types.MsgConvertCosmosCoinToERC20Response{}, nil } diff --git a/x/evmutil/keeper/msg_server_test.go b/x/evmutil/keeper/msg_server_test.go index 3d80a392..c13f73e3 100644 --- a/x/evmutil/keeper/msg_server_test.go +++ b/x/evmutil/keeper/msg_server_test.go @@ -388,7 +388,6 @@ func (suite *MsgServerSuite) TestConvertCosmosCoinToERC20_InitialContractDeploy( // verify success suite.NoError(err) - suite.Commit() initiator := sdk.MustAccAddressFromBech32(tc.msg.Initiator) receiver := testutil.MustNewInternalEVMAddressFromString(tc.msg.Receiver) @@ -417,6 +416,24 @@ func (suite *MsgServerSuite) TestConvertCosmosCoinToERC20_InitialContractDeploy( erc20Balance, err := suite.Keeper.QueryERC20BalanceOf(suite.Ctx, contractAddress, receiver) suite.NoError(err) suite.Equal(tc.amountConverted.BigInt(), erc20Balance, "unexpected erc20 balance for receiver") + + // msg server event + suite.EventsContains(suite.GetEvents(), + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, initiator.String()), + )) + + // keeper event + suite.EventsContains(suite.GetEvents(), + sdk.NewEvent( + types.EventTypeConvertCosmosCoinToERC20, + sdk.NewAttribute(types.AttributeKeyInitiator, initiator.String()), + sdk.NewAttribute(types.AttributeKeyReceiver, receiver.String()), + sdk.NewAttribute(types.AttributeKeyERC20Address, contractAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyAmount, tc.msg.Amount.String()), + )) }) } } diff --git a/x/evmutil/spec/04_events.md b/x/evmutil/spec/04_events.md index fb4243ff..504648b8 100644 --- a/x/evmutil/spec/04_events.md +++ b/x/evmutil/spec/04_events.md @@ -10,22 +10,33 @@ The evmutil module emits the following events: ### MsgConvertERC20ToCoin -| Type | Attribute Key | Attribute Value | -| --------------------- | ------------- | ------------------ | -| convert_erc20_to_coin | erc20_address | `{erc20 address}` | -| convert_erc20_to_coin | initiator | `{initiator}` | -| convert_erc20_to_coin | receiver | `{receiver}` | -| convert_erc20_to_coin | amount | `{amount}` | -| message | module | evmutil | -| message | sender | {'sender address}' | +| Type | Attribute Key | Attribute Value | +| ------------------------- | ------------- | ------------------ | +| convert_evm_erc20_to_coin | initiator | `{initiator}` | +| convert_evm_erc20_to_coin | receiver | `{receiver}` | +| convert_evm_erc20_to_coin | erc20_address | `{erc20_address}` | +| convert_evm_erc20_to_coin | amount | `{amount}` | +| message | module | evmutil | +| message | sender | {'sender address'} | ### MsgConvertCoinToERC20 -| Type | Attribute Key | Attribute Value | -| --------------------- | ------------- | ------------------ | -| convert_coin_to_erc20 | initiator | `{initiator}` | -| convert_coin_to_erc20 | receiver | `{receiver}` | -| convert_coin_to_erc20 | erc20_address | `{erc20_address}` | -| convert_coin_to_erc20 | amount | `{amount}` | -| message | module | evmutil | -| message | sender | {'sender address}' | +| Type | Attribute Key | Attribute Value | +| --------------------------- | ------------- | ------------------ | +| convert_evm_erc20_from_coin | initiator | `{initiator}` | +| convert_evm_erc20_from_coin | receiver | `{receiver}` | +| convert_evm_erc20_from_coin | erc20_address | `{erc20_address}` | +| convert_evm_erc20_from_coin | amount | `{amount}` | +| message | module | evmutil | +| message | sender | {'sender address'} | + +### MsgConvertCosmosCoinToERC20 + +| Type | Attribute Key | Attribute Value | +| ---------------------------- | ------------- | ------------------ | +| convert_cosmos_coin_to_erc20 | initiator | `{initiator}` | +| convert_cosmos_coin_to_erc20 | receiver | `{receiver}` | +| convert_cosmos_coin_to_erc20 | erc20_address | `{erc20_address}` | +| convert_cosmos_coin_to_erc20 | amount | `{amount}` | +| message | module | evmutil | +| message | sender | {'sender address'} | diff --git a/x/evmutil/testutil/suite.go b/x/evmutil/testutil/suite.go index e7838c95..717c68cd 100644 --- a/x/evmutil/testutil/suite.go +++ b/x/evmutil/testutil/suite.go @@ -363,15 +363,25 @@ func (suite *Suite) GetEvents() sdk.Events { // EventsContains asserts that the expected event is in the provided events func (suite *Suite) EventsContains(events sdk.Events, expectedEvent sdk.Event) { foundMatch := false + var possibleFailedMatch []sdk.Attribute + expectedAttrs := attrsToMap(expectedEvent.Attributes) + for _, event := range events { if event.Type == expectedEvent.Type { - if reflect.DeepEqual(attrsToMap(expectedEvent.Attributes), attrsToMap(event.Attributes)) { + attrs := attrsToMap(event.Attributes) + if reflect.DeepEqual(expectedAttrs, attrs) { foundMatch = true + } else { + possibleFailedMatch = attrs } } } - suite.Truef(foundMatch, "event of type %s not found or did not match", expectedEvent.Type) + if !foundMatch && possibleFailedMatch != nil { + suite.ElementsMatch(expectedAttrs, possibleFailedMatch, "unmatched attributes on event of type %s", expectedEvent.Type) + } else { + suite.Truef(foundMatch, "event of type %s not found", expectedEvent.Type) + } } // EventsDoNotContain asserts that the event is **not** is in the provided events diff --git a/x/evmutil/types/events.go b/x/evmutil/types/events.go index 9cab19e4..a9b48d20 100644 --- a/x/evmutil/types/events.go +++ b/x/evmutil/types/events.go @@ -5,8 +5,10 @@ const ( AttributeValueCategory = ModuleName // Event Types - EventTypeConvertERC20ToCoin = "convert_erc20_to_coin" - EventTypeConvertCoinToERC20 = "convert_coin_to_erc20" + EventTypeConvertERC20ToCoin = "convert_evm_erc20_to_coin" + EventTypeConvertCoinToERC20 = "convert_evm_erc20_from_coin" + + EventTypeConvertCosmosCoinToERC20 = "convert_cosmos_coin_to_erc20" // Event Attributes - Common AttributeKeyReceiver = "receiver"