Search code examples
substratepolkadot

How to convert u128 to Balance?


How to convert u128 to Balance?

use frame_support::{
        traits::{Currency, ExistenceRequirement, Get, ReservableCurrency, WithdrawReasons},
    };

type BalanceOf<T> = <<T as Config>::Currency as Currency<AccountIdOf<T>>>::Balance;

#[pallet::type_value]
pub fn DefaultRegistrationFees<T: Config>() -> BalanceOf<T> { 100 }

Error:
lib.rs(79, 67): expected associated type, found integer

Tried this method: How do you convert between Substrate specific types and Rust primitive types?

100.into() doesn't work.

Will I have to declare on runtime using configurable pallet constant?


Solution

  • With this https://crates.parity.io/sp_arithmetic/traits/trait.SaturatedConversion.html#method.saturated_into you can easily cast num to Balance.

    Example https://sourcegraph.com/github.com/paritytech/substrate/-/blob/frame/transaction-payment/src/lib.rs?L584.

    Also, recommend to read this How do you convert between Substrate specific types and Rust primitive types?