1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

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

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

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

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

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

            
25
#[cfg(test)]
26
mod mock;
27
#[cfg(test)]
28
mod tests;
29

            
30
use frame_support::pallet;
31
pub use pallet::*;
32

            
33
60
#[pallet]
34
pub mod pallet {
35
	use frame_support::pallet_prelude::*;
36
	use frame_system::pallet_prelude::*;
37
	use sp_std::vec::Vec;
38

            
39
	/// Pallet for configuring proxy at genesis
40
70
	#[pallet::pallet]
41
	#[pallet::without_storage_info]
42
	pub struct Pallet<T>(PhantomData<T>);
43

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

            
55
	#[pallet::genesis_config]
56
	pub struct GenesisConfig<T: Config> {
57
		pub proxies: Vec<(
58
			T::AccountId,
59
			T::AccountId,
60
			<T as Config>::ProxyType,
61
			BlockNumberFor<T>,
62
		)>,
63
	}
64

            
65
	impl<T: Config> Default for GenesisConfig<T> {
66
6
		fn default() -> Self {
67
6
			Self {
68
6
				proxies: Vec::new(),
69
6
			}
70
6
		}
71
	}
72

            
73
3
	#[pallet::genesis_build]
74
	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
75
7
		fn build(&self) {
76
9
			for (delegator, delegatee, proxy_type, delay) in &self.proxies {
77
2
				pallet_proxy::Pallet::<T>::add_proxy_delegate(
78
2
					delegator,
79
2
					delegatee.clone(),
80
2
					proxy_type.clone(),
81
2
					*delay,
82
2
				)
83
2
				.expect("Genesis proxy could not be added");
84
2
			}
85
7
		}
86
	}
87
}