Skip to content

Commit

Permalink
chore(app): conversion fixes (evmos#2937)
Browse files Browse the repository at this point in the history
* conversion fixes

* fix: remove chainid subfix

* changelog

* fix lint changelog

---------

Co-authored-by: hanchon <[email protected]>
  • Loading branch information
ramacarlucho and hanchon authored Oct 11, 2024
1 parent d71f993 commit b523bb1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (precompiles) [#2929](https://github.com/evmos/evmos/pull/2929) Distribution: scale balance change entries to the statedb journal to support different EVM denom precision.
- (precompiles) [#2927](https://github.com/evmos/evmos/pull/2927) Erc20: scale balance change entries to the statedb journal to support different EVM denom precision.


### Improvements

- (precompiles) [#2922](https://github.com/evmos/evmos/pull/2922) Add 'VoteWeighted' transaction to gov precompile.
Expand All @@ -76,11 +75,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (ante) [#2907](https://github.com/evmos/evmos/pull/2907) Add support for custom base denom decimals in the ante for EVM txs.
- (rpc) [#2908](https://github.com/evmos/evmos/pull/2908) Refactor JSON-RPC gas prices to support denom with different precision.
- (app) [#2914](https://github.com/evmos/evmos/pull/2914) Refactor App configurator to abstract `Decimals` type and use validation.
- (precompiles) [#2926](https://github.com/evmos/evmos/pull/2926) Staking: Add amount scaling on `balanceChange` entry to support different evm denom precision.
- (config) [#2932](https://github.com/evmos/evmos/pull/2932) Add chains coin info map for app initialization.
- (app) [#2932](https://github.com/evmos/evmos/pull/2932) Add chains coin info map for app initialization.
- (precompiles) [#2928](https://github.com/evmos/evmos/pull/2928) Vesting: Add amount scaling on `balanceChange` entry to support different EVM denom precision.
- (precompiles) [#2926](https://github.com/evmos/evmos/pull/2926) Staking: Add amount scaling on `balanceChange` entry to support different EVM denom precision.
- (evm) [#2936](https://github.com/evmos/evmos/pull/2936) Add query for EVM config.
- (app) [#2937](https://github.com/evmos/evmos/pull/2937) Fix conversion on the `CheckTxFee` ante handler and allow zero coins refunds.

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion app/ante/evm/04_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func CheckTxFee(txFeeInfo *tx.Fee, txFee *big.Int, txGasLimit uint64) error {
return nil
}

convertedAmount := sdkmath.NewIntFromBigInt(evmtypes.ConvertAmountTo18DecimalsBigInt(txFee))
convertedAmount := sdkmath.NewIntFromBigInt(evmtypes.ConvertAmountFrom18DecimalsBigInt(txFee))

baseDenom := evmtypes.GetEVMCoinDenom()
if !txFeeInfo.Amount.AmountOf(baseDenom).Equal(convertedAmount) {
Expand Down
5 changes: 4 additions & 1 deletion app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package app

import (
"strings"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/evmos/evmos/v20/app/eips"
Expand All @@ -29,7 +31,8 @@ func InitializeAppConfiguration(chainID string) error {
return nil
}

coinInfo, found := evmtypes.ChainsCoinInfo[chainID]
id := strings.Split(chainID, "-")[0]
coinInfo, found := evmtypes.ChainsCoinInfo[id]
if !found {
// default to mainnet coin info
coinInfo = evmtypes.ChainsCoinInfo[utils.MainnetChainID]
Expand Down
8 changes: 8 additions & 0 deletions x/evm/types/scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func ConvertAmountTo18DecimalsBigInt(amt *big.Int) *big.Int {
return new(big.Int).Mul(amt, evmCoinDecimal.ConversionFactor().BigInt())
}

// ConvertAmountFrom18DecimalsBigInt convert the given amount into a 18 decimals
// representation.
func ConvertAmountFrom18DecimalsBigInt(amt *big.Int) *big.Int {
evmCoinDecimal := GetEVMCoinDecimals()

return new(big.Int).Quo(amt, evmCoinDecimal.ConversionFactor().BigInt())
}

// ConvertBigIntFrom18DecimalsToLegacyDec converts the given amount into a LegacyDec
// with the corresponding decimals of the EVM denom.
func ConvertBigIntFrom18DecimalsToLegacyDec(amt *big.Int) sdkmath.LegacyDec {
Expand Down
3 changes: 3 additions & 0 deletions x/evm/wrappers/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func (w BankWrapper) SendCoinsFromAccountToModule(ctx context.Context, senderAdd
// the input, to its original representation.
func (w BankWrapper) SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, coins sdk.Coins) error {
convertedCoins := types.ConvertCoinsFrom18Decimals(coins)
if convertedCoins.IsZero() {
return nil
}

return w.BankKeeper.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, convertedCoins)
}

0 comments on commit b523bb1

Please sign in to comment.