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
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

//! A companion pallet to pallet-proxy
//!
//! This pallet allows you to specify proxy accounts that will exist from genesis. This
//! functionality could be moved upstream into pallet proxy eventually, but for now there are fewer
//! obstacles to including it here.

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

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

use frame_support::pallet;
pub use pallet::*;

#[pallet]
pub mod pallet {
	use frame_support::pallet_prelude::*;
	use frame_system::pallet_prelude::*;
	use sp_std::vec::Vec;

	/// Pallet for configuring proxy at genesis
	#[pallet::pallet]
	#[pallet::without_storage_info]
	pub struct Pallet<T>(PhantomData<T>);

	/// This pallet requires
	/// 1. pallet-proxy to be installed
	/// 2. its ProxyType to be serializable when built to std.
	#[pallet::config]
	pub trait Config:
		frame_system::Config + pallet_proxy::Config<ProxyType = <Self as Config>::ProxyType>
	{
		/// This MUST be the same as in pallet_proxy or it won't compile
		type ProxyType: MaybeSerializeDeserialize + Clone;
	}

	#[pallet::genesis_config]
	pub struct GenesisConfig<T: Config> {
		pub proxies: Vec<(
			T::AccountId,
			T::AccountId,
			<T as Config>::ProxyType,
			BlockNumberFor<T>,
		)>,
	}

	impl<T: Config> Default for GenesisConfig<T> {
		fn default() -> Self {
			Self {
				proxies: Vec::new(),
			}
		}
	}

	#[pallet::genesis_build]
	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
		fn build(&self) {
			for (delegator, delegatee, proxy_type, delay) in &self.proxies {
				pallet_proxy::Pallet::<T>::add_proxy_delegate(
					delegator,
					delegatee.clone(),
					proxy_type.clone(),
					*delay,
				)
				.expect("Genesis proxy could not be added");
			}
		}
	}
}