Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [\#687](https://github.com/cosmos/evm/pull/687) Avoid blocking node shutdown when evm indexer is enabled, log startup failures instead of using errgroup.
- [\#689](https://github.com/cosmos/evm/pull/689) Align debug addr for hex address.
- [\#668](https://github.com/cosmos/evm/pull/668) Fix panic in legacy mempool when Reset() was called with a skipped header between old and new block.
- [\#727](https://github.com/cosmos/evm/pull/727) Avoid nil pointer for `tx evm raw` due to uninitialized EVM coin info.
- [\#730](https://github.com/cosmos/evm/pull/730) Fix panic if evm mempool not used.

### IMPROVEMENTS
Expand Down
9 changes: 6 additions & 3 deletions x/vm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ func NewRawTxCmd() *cobra.Command {
return err
}

baseDenom := types.GetEVMCoinDenom()

tx, err := msg.BuildTx(clientCtx.TxConfig.NewTxBuilder(), baseDenom)
queryClient := types.NewQueryClient(clientCtx)
params, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}
tx, err := msg.BuildTxWithEvmParams(clientCtx.TxConfig.NewTxBuilder(), params.Params)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions x/vm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ func (msg *MsgEthereumTx) Hash() common.Hash {

// BuildTx builds the canonical cosmos tx from ethereum msg
func (msg *MsgEthereumTx) BuildTx(b client.TxBuilder, evmDenom string) (signing.Tx, error) {
return msg.BuildTxWithEvmParams(b, Params{
EvmDenom: evmDenom,
ExtendedDenomOptions: &ExtendedDenomOptions{
ExtendedDenom: GetEVMCoinExtendedDenom(),
},
})
}

func (msg *MsgEthereumTx) BuildTxWithEvmParams(b client.TxBuilder, params Params) (signing.Tx, error) {
builder, ok := b.(authtx.ExtensionOptionsTxBuilder)
if !ok {
return nil, errors.New("unsupported builder")
Expand All @@ -317,8 +326,8 @@ func (msg *MsgEthereumTx) BuildTx(b client.TxBuilder, evmDenom string) (signing.
fees := make(sdk.Coins, 0, 1)
feeAmt := sdkmath.NewIntFromBigInt(msg.GetFee())
if feeAmt.Sign() > 0 {
fees = append(fees, sdk.NewCoin(evmDenom, feeAmt))
fees = ConvertCoinsDenomToExtendedDenom(fees)
fees = append(fees, sdk.NewCoin(params.EvmDenom, feeAmt))
fees = ConvertCoinsDenomToExtendedDenomWithEvmParams(fees, params)
}

builder.SetExtensionOptions(option)
Expand Down
13 changes: 13 additions & 0 deletions x/vm/types/scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ func ConvertCoinsDenomToExtendedDenom(coins sdk.Coins) sdk.Coins {
}
return convertedCoins.Sort()
}

// ConvertCoinsDenomToExtendedDenomWithEvmParams returns the given coins with the Denom of the evm
// coin converted to the extended denom using params.
func ConvertCoinsDenomToExtendedDenomWithEvmParams(coins sdk.Coins, params Params) sdk.Coins {
convertedCoins := make(sdk.Coins, len(coins))
for i, coin := range coins {
if coin.Denom == params.EvmDenom {
coin = sdk.Coin{Denom: params.ExtendedDenomOptions.ExtendedDenom, Amount: coin.Amount}
}
convertedCoins[i] = coin
}
return convertedCoins.Sort()
}
76 changes: 76 additions & 0 deletions x/vm/types/scaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,79 @@ func TestConvertAmountTo18DecimalsBigInt(t *testing.T) {
}
}
}

func TestConvertCoinsDenomToExtendedDenomWithEvmParams(t *testing.T) {
eighteenDecimalsCoinInfo := testconstants.ExampleChainCoinInfo[testconstants.ExampleChainID]
sixDecimalsCoinInfo := testconstants.ExampleChainCoinInfo[testconstants.SixDecimalsChainID]
sixDecimalsParams := evmtypes.Params{
EvmDenom: sixDecimalsCoinInfo.Denom,
ExtendedDenomOptions: &evmtypes.ExtendedDenomOptions{
ExtendedDenom: sixDecimalsCoinInfo.ExtendedDenom,
},
}
nonBaseCoin := sdk.Coin{Denom: "btc", Amount: math.NewInt(100)}
eighteenDecimalsBaseCoin := sdk.Coin{Denom: eighteenDecimalsCoinInfo.Denom, Amount: math.NewInt(1000000000000000000)}
sixDecimalsBaseCoin := sdk.Coin{Denom: sixDecimalsCoinInfo.Denom, Amount: math.NewInt(1000000)}

tcs := []struct {
name string
coins sdk.Coins
params evmtypes.Params
expected sdk.Coins
}{
{
name: "empty coins",
coins: sdk.Coins{},
params: sixDecimalsParams,
expected: sdk.Coins{},
},
{
name: "single coin - 18 decimals (no conversion)",
coins: sdk.NewCoins(eighteenDecimalsBaseCoin),
params: evmtypes.Params{
EvmDenom: eighteenDecimalsCoinInfo.Denom,
ExtendedDenomOptions: &evmtypes.ExtendedDenomOptions{
ExtendedDenom: eighteenDecimalsCoinInfo.ExtendedDenom,
},
},
expected: sdk.NewCoins(sdk.Coin{Denom: eighteenDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(1000000000000000000)}),
},
{
name: "single coin - 6 decimals conversion",
coins: sdk.NewCoins(sixDecimalsBaseCoin),
params: sixDecimalsParams,
expected: sdk.NewCoins(sdk.Coin{Denom: sixDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(1000000)}),
},
{
name: "single coin - different denom (no conversion)",
coins: sdk.NewCoins(nonBaseCoin),
params: sixDecimalsParams,
expected: sdk.NewCoins(nonBaseCoin),
},
{
name: "multiple coins - mixed denominations",
coins: sdk.NewCoins(
sixDecimalsBaseCoin,
nonBaseCoin,
).Sort(),
params: sixDecimalsParams,
expected: sdk.NewCoins(
nonBaseCoin,
sdk.Coin{Denom: sixDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(1000000)},
).Sort(),
},
{
name: "zero amount coin",
coins: sdk.NewCoins(sdk.Coin{Denom: sixDecimalsCoinInfo.Denom, Amount: math.NewInt(0)}),
params: sixDecimalsParams,
expected: sdk.NewCoins(sdk.Coin{Denom: sixDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(0)}),
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
result := evmtypes.ConvertCoinsDenomToExtendedDenomWithEvmParams(tc.coins, tc.params)
require.Equal(t, tc.expected, result)
})
}
}
Loading