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.
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.