4

I was following this guide:

Configuring Genesis for Balances

https://docs.substrate.io/how-to-guides/v3/basics/genesis/

but got this error:

associated type Balance not found for T, associated type Balance not found

So where/what is the definition for this?

I think it is somewhere inside https://docs.substrate.io/rustdocs/latest/pallet_balances/index.html

I tried this: type Balance: balances::Config::Balance;

then failed: expected trait, found associated type balances::Config::Balance not a trait

Please advise. Thank you

1 Answer 1

3

Substrate chains are designed to be completely modular and without any assumptions about the underlying blockchain. This means that Balances are not necessarily a first class citizen within your runtime. Instead, if you want to use Balances in your chain, you need to follow a few common steps which you will see in most pallets.

  1. Configure your chain to use a Currency:

    #[pallet::config]
    pub trait Config: frame_system::Config {
        /// Currency type for this pallet.
        type Currency: ReservableCurrency<Self::AccountId>
            + LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
    
        // ...
    }
    

    Note you may not need both ReservableCurrency and LockableCurrency, it depends on how you want to play with the currency.

  2. Create a simple type alias to make it easier to access the Balance type inside of Currency:

    type BalanceOf<T> =
        <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
    
    
  3. Use this type alias wherever you want to use the Balance type:

    fn transfer(to: T::AccountId, from: T::Account, amount: BalanceOf<T>) -> DispatchResult { ... }
    
1

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.