9

I was following this guide Configuring Genesis for Balances.

In my parachain, I replaced T::Balance with BalanceOf<T>

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

    #[pallet::config]
    pub trait Config: frame_system::Config {
        type Currency: ReservableCurrency<Self::AccountId>
            + LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
    ...
    }
    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    pub struct Pallet<T>(PhantomData<T>);

    #[pallet::genesis_build]
    impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
        fn build(&self) {
            <SingleValue<T>>::put(&self.single_balance);
            for (a, b) in &self.account_map {
                <AccountMap<T>>::insert(a, b);
            }
        }
    }
    #[pallet::genesis_config]
    pub struct GenesisConfig<T: Config> {
        pub single_balance: BalanceOf<T>,
        pub account_map: Vec<(T::AccountId, BalanceOf<T>)>,
    }
    #[cfg(feature = "std")]
    impl<T: Config> Default for GenesisConfig<T> {
        fn default() -> Self {
            Self { single_balance: Default::default(), account_map: Default::default() }
        }
    }

    // https://docs.substrate.io/v3/runtime/storage#declaring-storage-items
    #[pallet::storage]
    #[pallet::getter(fn single_value)]
    pub type SingleValue<T: Config> = StorageValue<_, u32, ValueQuery>; //

    #[pallet::storage]
    #[pallet::getter(fn single_balance)]
    pub type SingleBalance<T: Config> = StorageValue<_, BalanceOf<T>>;

    // A map that has enumerable entries.
    #[pallet::storage]
    #[pallet::getter(fn accounts)]
    pub type AccountMap<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, BalanceOf<T>>;

error[E0277]: the trait bound &<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance: EncodeLike<u32> is not satisfied.

Here's the genesis configuration:

    #[pallet::genesis_build]
    impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
        fn build(&self) {
            <SingleValue<T>>::put(&self.single_balance);
            for (a, b) in &self.account_map {
                <AccountMap<T>>::insert(a, b);
            }
        }
    }

And the error I'm getting:

EncodeLike<u32> is not implemented for &<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance

122 | pub fn put<Arg: EncodeLike<Value>>(val: Arg) { | ^^^^^^^^^^^^^^^^ required by this bound in frame_support::pallet_prelude::StorageValue::<Prefix, Value, QueryKind, OnEmpty>::put help: consider introducing a where bound, but there might be an alternative better way to express this requirement

    impl<T: Config> GenesisBuild<T> for GenesisConfig<T> 
    where &<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance: EncodeLike<u32> {

What am I missing?

2 Answers 2

11

You need to change:

pub type SingleValue<T: Config> = StorageValue<_, u32, ValueQuery>;

to

pub type SingleValue<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

You tried to put the balance type into a u32 which obviously doesn't work, because BalanceOf<T> could be any type.

1
  • Wow. Thank you so much!
    – Russo
    Commented Mar 22, 2022 at 9:30
1

The error message "Trait bounds not satisfied for Balance type" typically occurs when you are trying to use a type Balance in your code but it does not fulfill the required trait bounds.

In Substrate-based blockchains, the Balance type represents the balance or monetary value associated with an account. To use the Balance type, you need to ensure that it satisfies the necessary trait bounds.

Commonly, the Balance type is an alias for a specific type that implements the currency::Amount trait. This trait provides methods for working with balances, such as addition, subtraction, and comparison operations. Therefore, to resolve the error, you need to ensure that the Balance type you are using satisfies the required trait bounds.

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.