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
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>(); - 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.John– John2022-09-29 12:52:28 +00:00Commented Sep 29, 2022 at 12:52
-
- Understood. What would be the changes required in pallet_balances on high level?John– John2022-09-29 16:09:04 +00:00Commented Sep 29, 2022 at 16:09
- 1Remove the traits bound that
U256doesn't implement. But you will meet a lot of questions in the future.2022-09-29 17:12:12 +00:00Commented Sep 29, 2022 at 17:12