pallet_governance/
whitelist.rs

1use polkadot_sdk::frame_support::dispatch::DispatchResult;
2
3use crate::{AccountIdOf, application};
4
5/// Adds a key to the DAO whitelist, allowing it to register an agent.
6pub fn add_to_whitelist<T: crate::Config>(key: AccountIdOf<T>) -> DispatchResult {
7    if is_whitelisted::<T>(&key) {
8        return Err(crate::Error::<T>::AlreadyWhitelisted.into());
9    }
10
11    if application::exists_for_agent_key::<T>(&key, &application::ApplicationAction::Add) {
12        return Err(crate::Error::<T>::ApplicationKeyAlreadyUsed.into());
13    }
14
15    crate::Whitelist::<T>::insert(key.clone(), ());
16    crate::Pallet::<T>::deposit_event(crate::Event::<T>::WhitelistAdded(key));
17
18    Ok(())
19}
20
21/// Remove a key from the DAO whitelist, disallowing the key to register an
22/// agent, or de-registering an existing one.
23pub fn remove_from_whitelist<T: crate::Config>(key: AccountIdOf<T>) -> DispatchResult {
24    if !is_whitelisted::<T>(&key) {
25        return Err(crate::Error::<T>::NotWhitelisted.into());
26    }
27
28    if application::exists_for_agent_key::<T>(&key, &application::ApplicationAction::Remove) {
29        return Err(crate::Error::<T>::ApplicationKeyAlreadyUsed.into());
30    }
31
32    crate::Whitelist::<T>::remove(&key);
33    crate::Pallet::<T>::deposit_event(crate::Event::<T>::WhitelistRemoved(key));
34
35    Ok(())
36}
37
38pub fn is_whitelisted<T: crate::Config>(key: &AccountIdOf<T>) -> bool {
39    crate::Whitelist::<T>::contains_key(key)
40}