2

In substrate runtime and in many frame pallets the type Balance/BalanceOf uses u128 type everywhere. How to change that type to U256 from primitive-types crate in runtime and all frame pallets and make that type as native currency in the runtime?

1 Answer 1

2

That is impossible.

Take a look at the pallet_balances::Config::Balance's definition:

pub trait Config<I: 'static = ()>: Config {
    type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + MaybeSerializeDeserialize + Debug + MaxEncodedLen + TypeInfo + FixedPointOperand;
    ..
}

U256 must satisfy those traits bound.

AFAIK, U256 doesn't implement AtLeast32BitUnsigned.

If you want to do that, you have to write your own pallet-balances or primitives-types.


Moonbeam is an Ethereum-like chain. But it also uses u128 on the Substrate side. And use U256 in EVM side.

Use u128 and convert it to U256.

// u128 -> U256
let u_128: u128 = 0_u128;
let u_256: U256 = U256::from(u_128);

// U256 -> u128
let u_128: u128 = u_256.low_u128();


// ---

// u128 to Balance
use sp_runtime::traits::SaturatedConversion;
let balance: Balance = u_128.saturated_into::<Balance>();
4
  • This is possible. But my question is different. when I give Balance type in runtime as U256 directly instead of u128 I am facing lots of errors. Sep 29, 2022 at 12:52
  • Updated my answer.
    – AurevoirXavier
    Sep 29, 2022 at 14:03
  • Understood. What would be the changes required in pallet_balances on high level? Sep 29, 2022 at 16:09
  • 1
    Remove the traits bound that U256 doesn't implement. But you will meet a lot of questions in the future.
    – AurevoirXavier
    Sep 29, 2022 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.