referrerpolicy=no-referrer-when-downgrade

rococo_runtime/governance/
origins.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot 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// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17//! Custom origins for governance interventions.
18
19pub use pallet_custom_origins::*;
20
21#[frame_support::pallet]
22pub mod pallet_custom_origins {
23	use crate::{Balance, CENTS, GRAND};
24	use frame_support::pallet_prelude::*;
25
26	#[pallet::config]
27	pub trait Config: frame_system::Config {}
28
29	#[pallet::pallet]
30	pub struct Pallet<T>(_);
31
32	#[derive(
33		PartialEq,
34		Eq,
35		Clone,
36		MaxEncodedLen,
37		Encode,
38		Decode,
39		DecodeWithMemTracking,
40		TypeInfo,
41		RuntimeDebug,
42	)]
43	#[pallet::origin]
44	pub enum Origin {
45		/// Origin for cancelling slashes.
46		StakingAdmin,
47		/// Origin for spending (any amount of) funds.
48		Treasurer,
49		/// Origin for managing the composition of the fellowship.
50		FellowshipAdmin,
51		/// Origin for managing the registrar.
52		GeneralAdmin,
53		/// Origin for starting auctions.
54		AuctionAdmin,
55		/// Origin able to force slot leases.
56		LeaseAdmin,
57		/// Origin able to cancel referenda.
58		ReferendumCanceller,
59		/// Origin able to kill referenda.
60		ReferendumKiller,
61		/// Origin able to spend up to 1 KSM from the treasury at once.
62		SmallTipper,
63		/// Origin able to spend up to 5 KSM from the treasury at once.
64		BigTipper,
65		/// Origin able to spend up to 50 KSM from the treasury at once.
66		SmallSpender,
67		/// Origin able to spend up to 500 KSM from the treasury at once.
68		MediumSpender,
69		/// Origin able to spend up to 5,000 KSM from the treasury at once.
70		BigSpender,
71		/// Origin able to dispatch a whitelisted call.
72		WhitelistedCaller,
73		/// Origin commanded by any members of the Polkadot Fellowship (no Dan grade needed).
74		FellowshipInitiates,
75		/// Origin commanded by Polkadot Fellows (3rd Dan fellows or greater).
76		Fellows,
77		/// Origin commanded by Polkadot Experts (5th Dan fellows or greater).
78		FellowshipExperts,
79		/// Origin commanded by Polkadot Masters (7th Dan fellows of greater).
80		FellowshipMasters,
81		/// Origin commanded by rank 1 of the Polkadot Fellowship and with a success of 1.
82		Fellowship1Dan,
83		/// Origin commanded by rank 2 of the Polkadot Fellowship and with a success of 2.
84		Fellowship2Dan,
85		/// Origin commanded by rank 3 of the Polkadot Fellowship and with a success of 3.
86		Fellowship3Dan,
87		/// Origin commanded by rank 4 of the Polkadot Fellowship and with a success of 4.
88		Fellowship4Dan,
89		/// Origin commanded by rank 5 of the Polkadot Fellowship and with a success of 5.
90		Fellowship5Dan,
91		/// Origin commanded by rank 6 of the Polkadot Fellowship and with a success of 6.
92		Fellowship6Dan,
93		/// Origin commanded by rank 7 of the Polkadot Fellowship and with a success of 7.
94		Fellowship7Dan,
95		/// Origin commanded by rank 8 of the Polkadot Fellowship and with a success of 8.
96		Fellowship8Dan,
97		/// Origin commanded by rank 9 of the Polkadot Fellowship and with a success of 9.
98		Fellowship9Dan,
99	}
100
101	macro_rules! decl_unit_ensures {
102		( $name:ident: $success_type:ty = $success:expr ) => {
103			pub struct $name;
104			impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
105			where
106				for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
107			{
108				type Success = $success_type;
109				fn try_origin(o: O) -> Result<Self::Success, O> {
110					match o.caller().try_into() {
111						Ok(Origin::$name) => return Ok($success),
112						_ => (),
113					}
114
115					Err(o)
116				}
117				#[cfg(feature = "runtime-benchmarks")]
118				fn try_successful_origin() -> Result<O, ()> {
119					Ok(O::from(Origin::$name))
120				}
121			}
122		};
123		( $name:ident ) => { decl_unit_ensures! { $name : () = () } };
124		( $name:ident: $success_type:ty = $success:expr, $( $rest:tt )* ) => {
125			decl_unit_ensures! { $name: $success_type = $success }
126			decl_unit_ensures! { $( $rest )* }
127		};
128		( $name:ident, $( $rest:tt )* ) => {
129			decl_unit_ensures! { $name }
130			decl_unit_ensures! { $( $rest )* }
131		};
132		() => {}
133	}
134	decl_unit_ensures!(
135		StakingAdmin,
136		Treasurer,
137		FellowshipAdmin,
138		GeneralAdmin,
139		AuctionAdmin,
140		LeaseAdmin,
141		ReferendumCanceller,
142		ReferendumKiller,
143		WhitelistedCaller,
144		FellowshipInitiates: u16 = 0,
145		Fellows: u16 = 3,
146		FellowshipExperts: u16 = 5,
147		FellowshipMasters: u16 = 7,
148	);
149
150	macro_rules! decl_ensure {
151		(
152			$vis:vis type $name:ident: EnsureOrigin<Success = $success_type:ty> {
153				$( $item:ident = $success:expr, )*
154			}
155		) => {
156			$vis struct $name;
157			impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
158			where
159				for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
160			{
161				type Success = $success_type;
162				fn try_origin(o: O) -> Result<Self::Success, O> {
163					match o.caller().try_into() {
164						$(
165							Ok(Origin::$item) => return Ok($success),
166						)*
167						_ => (),
168					}
169
170					Err(o)
171				}
172				#[cfg(feature = "runtime-benchmarks")]
173				fn try_successful_origin() -> Result<O, ()> {
174					// By convention the more privileged origins go later, so for greatest chance
175					// of success, we want the last one.
176					let _result: Result<O, ()> = Err(());
177					$(
178						let _result: Result<O, ()> = Ok(O::from(Origin::$item));
179					)*
180					_result
181				}
182			}
183		}
184	}
185
186	decl_ensure! {
187		pub type Spender: EnsureOrigin<Success = Balance> {
188			SmallTipper = 250 * 3 * CENTS,
189			BigTipper = 1 * GRAND,
190			SmallSpender = 10 * GRAND,
191			MediumSpender = 100 * GRAND,
192			BigSpender = 1_000 * GRAND,
193			Treasurer = 10_000 * GRAND,
194		}
195	}
196
197	decl_ensure! {
198		pub type EnsureFellowship: EnsureOrigin<Success = u16> {
199			Fellowship1Dan = 1,
200			Fellowship2Dan = 2,
201			Fellowship3Dan = 3,
202			Fellowship4Dan = 4,
203			Fellowship5Dan = 5,
204			Fellowship6Dan = 6,
205			Fellowship7Dan = 7,
206			Fellowship8Dan = 8,
207			Fellowship9Dan = 9,
208		}
209	}
210}