Skip to content

Commit

Permalink
fix: handle unit conversion in cosmos to ethereum tx
Browse files Browse the repository at this point in the history
whenever ethereum tx is converted into cosmos tx by ConvertEthereumTxToCosmosTx, the value is converted to cosmos fee unit from wei.
so when convert the cosmos tx to back the original ethereum tx, we should convert the value back to wei to get original ethereum tx.
  • Loading branch information
zsystm committed Jun 19, 2024
1 parent 0d7716d commit 8cfa620
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 6 additions & 2 deletions x/evm/keeper/txutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ func (u *TxUtils) ConvertCosmosTxToEthereumTx(ctx context.Context, sdkTx sdk.Tx)

to = &contractAddr
input = data
value = types.FromEthersUnit(decimals, callMsg.Value.BigInt())
// When ethereum tx is converted into cosmos tx by ConvertEthereumTxToCosmosTx,
// the value is converted to cosmos fee unit from wei.
// So we need to convert it back to wei to get original ethereum tx and verify signature.
value = types.ToEthersUint(decimals, callMsg.Value.BigInt())
case "/minievm.evm.v1.MsgCreate":
createMsg := msg.(*types.MsgCreate)
data, err := hexutil.Decode(createMsg.Code)
Expand All @@ -283,7 +286,8 @@ func (u *TxUtils) ConvertCosmosTxToEthereumTx(ctx context.Context, sdkTx sdk.Tx)

to = nil
input = data
value = types.FromEthersUnit(decimals, createMsg.Value.BigInt())
// Same as above (MsgCall)
value = types.ToEthersUint(decimals, createMsg.Value.BigInt())
case "/minievm.evm.v1.MsgCreate2":
// create2 is not supported
return nil, nil, nil
Expand Down
16 changes: 14 additions & 2 deletions x/evm/keeper/txutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Test_DynamicFeeTxConversion(t *testing.T) {
// Convert back to ethereum tx
ethTx2, _, err := keeper.NewTxUtils(&input.EVMKeeper).ConvertCosmosTxToEthereumTx(ctx, sdkTx)
require.NoError(t, err)
require.Equal(t, signedTx.Data(), ethTx2.Data())
EqualEthTransaction(t, signedTx, ethTx2)
}

func Test_LegacyTxConversion(t *testing.T) {
Expand Down Expand Up @@ -190,5 +190,17 @@ func Test_LegacyTxConversion(t *testing.T) {
// Convert back to ethereum tx
ethTx2, _, err := keeper.NewTxUtils(&input.EVMKeeper).ConvertCosmosTxToEthereumTx(ctx, sdkTx)
require.NoError(t, err)
require.Equal(t, signedTx.Data(), ethTx2.Data())
EqualEthTransaction(t, signedTx, ethTx2)
}

func EqualEthTransaction(t *testing.T, expected, actual *coretypes.Transaction) {
require.Equal(t, expected.ChainId(), actual.ChainId())
require.Equal(t, expected.Nonce(), actual.Nonce())
require.Equal(t, expected.GasTipCap(), actual.GasTipCap())
require.Equal(t, expected.GasFeeCap(), actual.GasFeeCap())
require.Equal(t, expected.Gas(), actual.Gas())
require.Equal(t, expected.To(), actual.To())
require.Equal(t, expected.Data(), actual.Data())
require.Equal(t, expected.Value(), actual.Value())
require.Equal(t, expected.Type(), actual.Type())
}

0 comments on commit 8cfa620

Please sign in to comment.