1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! # Equilibrium Whitelist Pallet
//!
//! Equilibrium's Whitelist Pallet is a Substrate module that manages whitelists

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

mod mock;
mod tests;

use eq_utils::eq_ensure;
use frame_support::{
    debug, decl_error, decl_event, decl_module, decl_storage, dispatch::DispatchResult,
    storage::IterableStorageMap,
};
use frame_system::ensure_root;
use sp_std::iter::Iterator;
use sp_std::prelude::*;

/// Interface for checking whitelisted accounts
pub trait CheckWhitelisted<AccountId> {
    /// Checks if `account_id` is in whitelist
    fn in_whitelist(account_id: &AccountId) -> bool;
    /// Gets a vector of all whitelisted accounts
    fn accounts() -> Vec<AccountId>;
}

/// Substrate pallet configuration trait
pub trait Trait: frame_system::Trait {
    type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}

decl_storage! {
    trait Store for Module<T: Trait> as EqWhitelists {
        /// Storage of all whitelisted `AccountId`s
        pub WhiteList get(fn whitelists): map hasher(blake2_128_concat) T::AccountId => Option<bool>;
    }
    add_extra_genesis {
        config(whitelist): Vec<T::AccountId>;
        // ^^ begin, length, amount liquid at genesis
        build(|config: &GenesisConfig<T>| {
            for &ref who in config.whitelist.iter() {
                <WhiteList<T>>::insert(who, true);
            }
        });
    }
}

decl_event!(
    /// Whitelist management events
    pub enum Event<T>
    where
        AccountId = <T as frame_system::Trait>::AccountId,
    {
        /// `AccountId` was added to whitelist
        AddedToWhitelist(AccountId),
        /// `AccountId` was removed from whitelist
        RemovedFromWhitelist(AccountId),
    }
);

decl_error! {
    /// Whitelist management errors
    pub enum Error for Module<T: Trait> {
        /// Account was not added to whitelist: already in whitelist
        AlreadyAdded,
        /// Account was not remove from whitelist: not in whitelist
        AlreadyRemoved,
    }
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        type Error = Error<T>;

        fn deposit_event() = default;

        /// Adds `who_to_add` to whitelist. Requires sudo authorization
        #[weight = 10_000]
        pub fn add_to_whitelist(origin, who_to_add: T::AccountId) -> DispatchResult
        {
            ensure_root(origin)?;

            let acc_in_whitelist = <WhiteList<T>>::get(&who_to_add);
            eq_ensure!(acc_in_whitelist.is_none(), Error::<T>::AlreadyAdded,
            "{}:{}. Account is already added in whitelist. Who: {:?}.",
            file!(), line!(), who_to_add);

            <WhiteList<T>>::insert(&who_to_add, true);

            debug::info!("😂 Added to whitelist {:?}", who_to_add);

            Self::deposit_event(RawEvent::AddedToWhitelist(who_to_add));

            Ok(())
        }

        /// Removes account `who_to_remove` from whitelist. Requires sudo authorization
        #[weight = 10_000]
        pub fn remove_from_whitelist(origin, who_to_remove: T::AccountId) -> DispatchResult
        {
            ensure_root(origin)?;

            let acc_in_whitelist = <WhiteList<T>>::get(&who_to_remove);
            eq_ensure!(acc_in_whitelist.is_some(), Error::<T>::AlreadyRemoved,
            "{}:{}. Account is already removed from whitelist. Who: {:?}.",
            file!(), line!(), who_to_remove);

            <WhiteList<T>>::remove(&who_to_remove);

            debug::info!("😂 Removed from whitelist {:?}", who_to_remove);

            Self::deposit_event(RawEvent::RemovedFromWhitelist(who_to_remove));

            Ok(())
        }


    }
}

impl<T: Trait> Module<T> {
    /// Inner function to check whitelisted status
    fn in_whitelist(account_id: &T::AccountId) -> bool {
        let acc_in_whitelist = <WhiteList<T>>::get(&account_id);
        acc_in_whitelist.is_some() && acc_in_whitelist.unwrap()
    }
}

impl<T: Trait> CheckWhitelisted<T::AccountId> for Module<T> {
    fn in_whitelist(account_id: &T::AccountId) -> bool {
        Self::in_whitelist(&account_id)
    }
    fn accounts() -> Vec<T::AccountId> {
        <WhiteList<T>>::iter()
            .filter_map(|(k, v)| if v { Option::Some(k) } else { Option::None })
            .collect()
    }
}