polkadot-evm / frontier

Ethereum compatibility layer for Substrate.
Apache License 2.0
567 stars 479 forks source link

how to set evm pallet native token decimals? #747

Open xiaolese1 opened 2 years ago

xiaolese1 commented 2 years ago

Question

I added evm pallet to my runtime to evm-Compatible. Everything about contract was fine, but when I wanted to transfer evm native token to call some payable functionsin contract, like I transfered 1000wei to contract, it will throw "OutofFund" error. But if I transfer 10 ether, every thing will be fine. I tried other unit like Gwei, Finney. all got same error. Only ether will work. Is there something should I config about evm pallet native token decimals?

boundless-forest commented 2 years ago

No, there is no such config currently. The decimal of evm pallet is the same as the balance module in your runtime by default as 18. It is not easy work to make this configurable as you need to consider the decimal loss part when touching balance.

icodezjb commented 2 years ago

@xiaolese1 You can add an adaptor like chainx https://github.com/chainx-org/frontier/commit/07ed82e134e6c935037a6113c445dea3f1f5d904

boundless-forest commented 2 years ago

What do you do with the remaining balance below CHAINS_VALUE_ADAPTOR after div? If this part is discarded, the balance is inaccurate. @icodezjb

pub fn chainx_value_shrink(origin: U256) -> U256 {
    #[cfg(feature = "chainx-adaptor")]
    {
        origin
            .checked_div(U256::from(CHAINS_VALUE_ADAPTOR))
            .unwrap_or(U256::from(CHAINS_VALUE_ADAPTOR))
    }
    #[cfg(not(feature = "chainx-adaptor"))]
    {
        origin
    }
}
icodezjb commented 2 years ago

@AsceticBear On the etherum compatibility layer, A very small balance is discarded. but at the substrate balances layer, the ledger is correct. It's not perfect but enough. If you want the perfect solution, maybe you should start with decimals=18 on your substrate blockchain.