Skip to content

Commit

Permalink
add precision checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Jun 13, 2024
1 parent 99e4d16 commit 5aa1618
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 5 additions & 0 deletions x/evm/keeper/txutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (u *TxUtils) ConvertEthereumTxToCosmosTx(ctx context.Context, ethTx *corety
// convert value unit from wei to cosmos fee unit
value := types.FromEthersUnit(decimals, ethTx.Value())

// check if the value is correctly converted without dropping any precision
if types.ToEthersUint(decimals, value).Cmp(ethTx.Value()) != 0 {
return nil, types.ErrInvalidValue.Wrap("failed to convert value to token unit without dropping precision")
}

// signer
chainID := sdk.UnwrapSDKContext(ctx).ChainID()
ethChainID := types.ConvertCosmosChainIDToEthereumChainID(chainID)
Expand Down
1 change: 0 additions & 1 deletion x/evm/types/decimals.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func FromEthersUnit(decimals uint8, val *big.Int) *big.Int {
}

decimalDiff := EtherDecimals - decimals

exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimalDiff)), nil)
return new(big.Int).Div(val, exp)
}

0 comments on commit 5aa1618

Please sign in to comment.