0

i wanna to create pallet in substrate . i faced this error . how can i solve this problem ?

#![cfg_attr(not(feature = "std"), no_std)]

/// Edit this file to define custom logic or remove it if it is not needed.
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// <https://docs.substrate.io/reference/frame-pallets/>
use codec::alloc::vec;
use frame_support::{
    inherent::Vec,
    pallet_prelude::{ValueQuery, *},
    sp_runtime::{
        traits::{AccountIdConversion, AtLeast32BitUnsigned, CheckedAdd, Hash, Member, One},
        DispatchError, SaturatedConversion,
    },
    traits::{ExistenceRequirement, Get, ReservableCurrency},
    transactional, PalletId, Twox64Concat,
};
use frame_system::Config as SystemConfig;
pub use pallet::*;
use sp_runtime::traits::UniqueSaturatedFrom;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

mod functions;
mod structs;
mod types;

use structs::labels::Labels::RegisterInputLabelInfo;
use types::types::Types::{
    AccountOf, ArtistInfo, ArtistInfoDetail, BalanceOf, HashId, LabelsDetail,
};

#[frame_support::pallet]
pub mod pallet {

    use super::*;

    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;

    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    pub struct Pallet<T>(_);

    /// Configure the pallet by specifying the parameters and types on which it depends.
    #[pallet::config]
    pub trait Config: frame_system::Config {
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
        type Currency: ReservableCurrency<Self::AccountId>;

        #[pallet::constant]
        type StackLimit: Get<u32>;

        #[pallet::constant]
        type PalletId: Get<PalletId>;
    }

    
    #[pallet::storage]
    #[pallet::getter(fn something)]
    pub type Something<T> = StorageValue<_, u32>;

    #[pallet::storage]
    #[pallet::getter(fn labels)]
    pub(super) type Labels<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        AccountOf<T>,
        Twox64Concat,
        HashId<T>,
        LabelsDetail<T>,
        OptionQuery,
    >;

    // Pallets use events to inform users when important changes are made.
    // https://docs.substrate.io/main-docs/build/events-errors/
    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// Event documentation should end with an array that provides descriptive names for event
        /// parameters. [something, who]
        SomethingStored { something: u32, who: T::AccountId },
    }

    // Errors inform users that something went wrong.
    #[pallet::error]
    pub enum Error<T> {
        /// Error names should be descriptive.
        NoneValue,
        /// Errors should have helpful documentation associated with them.
        StorageOverflow,
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        #[pallet::call_index(0)]
        #[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
        pub fn register_labels(
            origin: OriginFor<T>,
            info: RegisterInputLabelInfo,
        ) -> DispatchResult {
            let owner = ensure_signed(origin)?;
            let label_id: HashId<T> = T::Hashing::hash_of(&info.label_name);
            Ok(())
        }

        #[pallet::call_index(1)]
        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
        pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
            let _who = ensure_signed(origin)?;

            // Read a value from storage.
            match <Something<T>>::get() {
                // Return an error if the value has not been set.
                None => return Err(Error::<T>::NoneValue.into()),
                Some(old) => {
                    let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
                    <Something<T>>::put(new);
                    Ok(())
                },
            }
        }
    }
}

Types.rs :

use super::*;

pub mod Types {

    pub use crate::{
        pallet::Config,
        structs::{
            artist::Artist::ArtistInfo,
            labels::Labels::{LabelInfo, OperationalContact},
        },
    };
    use frame_support::traits::Currency;
    use frame_system::Config as SystemConfig;

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

    pub type HashId<T> = <T as frame_system::Config>::Hash;

    pub type AccountOf<T> = <T as frame_system::Config>::AccountId;

    pub type LabelsDetail<T> = LabelInfo<T>;

    pub type ArtistInfoDetail<T> = ArtistInfo<AccountOf<T>>;
}

Labels.rs:

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{inherent::Vec, traits::ConstU32, BoundedVec};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;

pub mod Labels {
    use super::*;

    #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
    pub struct LabelInfo<T: crate::Config> {
        pub owner: T::AccountId,
        pub label_id: T::Hashing,
        pub label_name: BoundedVec<u8, ConstU32<128>>,
    }
}

i faced this error and i dont know how can i sovle this problem .

error[E0277]: the trait bound LabelInfo<T>: parity_scale_codec::Decode is not satisfied --> /root/Pikamo_Chain/frame/lib.rs:72:15 | 72 | #[pallet::storage]
| ^^^^^^^ the trait parity_scale_codec::Decode is not implemented for LabelInfo<T>

#[pallet::storage]
| ^^^^^^^ the trait parity_scale_codec::Decode is not implemented for LabelInfo<T>

2

1 Answer 1

1

In stead of providing a generic T: Config to your struct you can also provide generics for your fields:

#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct LabelInfo<AccountId, Hashing> {
    pub owner: AccountId,
    pub label_id: Hashing,
    pub label_name: BoundedVec<u8, ConstU32<128>>,
}

My reasoning: when bounding your generic argument to your pallet's Config trait, rust macros want this Config to have all those traits implemented.

Now you are not using the pallet's Config in any way and declaring the fields of the struct as generic. In your pallet's code you will define those fields with Config::AccountId which implements those necessary traits.

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.