1

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?

Amiya Behera
  • 2,210
  • 19
  • 32

2 Answers2

1

here are few examples which should help in conversion of number of type u128 to Balance Type:

//declare following import
use frame_support::sp_runtime::SaturatedConversion;

//then saturated_into can be used to convert number into Balance type as follows
let cost_in_u128: u128 = 250; //or expression like 200 + 50;
let cost: BalanceOf<T> = cost_in_u128.saturated_into::<BalanceOf<T>>();

//convert 1010 of type u32 into Balance
let cost2: BalanceOf<T> = 1010u32.into();
//set zero balance
let cost3 = BalanceOf::<T>::zero();

where BalanceOf is defined in cfg as follows

#[cfg(feature = "std")]
type BalanceOf<T> =
        <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
K Gunjan
  • 31
  • 3