referrerpolicy=no-referrer-when-downgrade

pallet_staking_async_parachain_runtime/governance/
origins.rs

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