1

I am a beginner at learning Rust/substrate/Polkadot. I am trying to build my custom substrate pallet

My goal is, Create a custom Substrate pallet that allows only the Root origin account to control the list of group members to a set of accounts for at least two different groups, where

  • Only the Root origin account can add an account as a member of a group in on-chain storage
  • Only the Root origin account can remove an account from being a member of a group in on-chain storage

There are many Rust errors. Anyone who can teach me. I attached my working source code.

...
// The pallet's runtime storage items.
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
decl_storage! {
    trait Store for Module<T: Config> as GroupModule {
        pub Group get(fn get_group): map hasher(blake2_128_concat) T::AccountId => Vec<u8>;
    }
}
...
// Errors inform users that something went wrong.
decl_error! {
    pub enum Error for Module<T: Config> {
        AccountAlreadyAdded,
        AccountAlreadyRemoved,
        AccountNotFound,
    }
}
...
decl_module! {
    pub struct Module<T: Config> for enum Call where origin: T::Origin {
        // Errors must be initialized if they are used by the pallet.
        type Error = Error<T>;

        // Events must be initialized if they are used by the pallet.
        fn deposit_event() = default;

        #[weight = 10_000 + T::DbWeight::get().reads_writes(1, 1)]
        pub fn add_group_member(
            origin, 
            new_account: T::AccountId,          
            group_no: u8,
        ) -> dispatch::DispatchResult {

            // Can only be called by the root.
            ensure_root(origin)?;

            // Read a vector value from storage.
            let mut groups = <Group<T>>::get(&new_account);
            //match <Group<T>>::get(&new_account) {  //error: mismatched types
                //None => Err(Error::<T>::AccountNotFound)?, //error
            //  groups => {
                    if groups.contains(&group_no) {   //error[E0308]: mismatched types
                        //Err(Error::<T>::AccountAlreadyAdded)?
                    } else {  //error: expected enum `Result`, found `()`
                        // Update storage.
                        groups.push(group_no);
                        <Group<T>>::insert(&new_account, &groups);
                        // Emit an event.
                        Self::deposit_event(RawEvent::GroupMemberAdded(new_account, groups));
                        // Return a successful DispatchResult
                        Ok(())
                    }
                //},
            //}         
        }
...
    }
}

Thank you.

2
  • 1
    Run the tutorials first and try to understand what is going on. 2 focus on the errors from the compiler, edit this post and include the errors.
    – Simson
    May 10, 2022 at 13:12
  • 1
    I recommend NOT to use the old decl_*! syntax, and instead use the newest attribute macros in FRAME v2. Otherwise, yes, you need to post information about the errors you are facing.
    – Shawn Tabrizi
    May 10, 2022 at 18:15

1 Answer 1

1

This is using the decl_* macro syntax. It is out-dated and got recently removed from the Substrate code base after 6 months grace period.

We added a bunch of example pallets that show of the new syntax here and can help you get started with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.