referrerpolicy=no-referrer-when-downgrade

asset_hub_westend_runtime/governance/
origins.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Custom origins for governance interventions.
18
19pub use pallet_custom_origins::*;
20
21// From https://github.com/polkadot-fellows/runtimes/blob/7bbf00566d86d51fcd5582779e7e9c37a814405e/relay/polkadot/src/governance/origins.rs#L21-L154
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 able to cancel slashes and manage minimum commission.
47		StakingAdmin,
48		/// Origin for spending up to $10,000,000 DOT from the treasury as well as generally
49		/// administering it.
50		Treasurer,
51		/// Origin for managing the composition of the fellowship.
52		FellowshipAdmin,
53		/// Origin for managing the registrar and permissioned HRMP channel operations.
54		GeneralAdmin,
55		/// Origin for starting auctions.
56		AuctionAdmin,
57		/// Origin able to force slot leases.
58		LeaseAdmin,
59		/// Origin able to cancel referenda.
60		ReferendumCanceller,
61		/// Origin able to kill referenda.
62		ReferendumKiller,
63		/// Origin able to spend around $250 from the treasury at once.
64		SmallTipper,
65		/// Origin able to spend around $1,000 from the treasury at once.
66		BigTipper,
67		/// Origin able to spend around $10,000 from the treasury at once.
68		SmallSpender,
69		/// Origin able to spend around $100,000 from the treasury at once.
70		MediumSpender,
71		/// Origin able to spend up to $1,000,000 DOT from the treasury at once.
72		BigSpender,
73		/// Origin able to dispatch a whitelisted call.
74		WhitelistedCaller,
75	}
76
77	macro_rules! decl_unit_ensures {
78		( $name:ident: $success_type:ty = $success:expr ) => {
79			pub struct $name;
80			impl<O: Into<Result<Origin, O>> + From<Origin>>
81				EnsureOrigin<O> for $name
82			{
83				type Success = $success_type;
84				fn try_origin(o: O) -> Result<Self::Success, O> {
85					o.into().and_then(|o| match o {
86						Origin::$name => Ok($success),
87						r => Err(O::from(r)),
88					})
89				}
90				#[cfg(feature = "runtime-benchmarks")]
91				fn try_successful_origin() -> Result<O, ()> {
92					Ok(O::from(Origin::$name))
93				}
94			}
95		};
96		( $name:ident ) => { decl_unit_ensures! { $name : () = () } };
97		( $name:ident: $success_type:ty = $success:expr, $( $rest:tt )* ) => {
98			decl_unit_ensures! { $name: $success_type = $success }
99			decl_unit_ensures! { $( $rest )* }
100		};
101		( $name:ident, $( $rest:tt )* ) => {
102			decl_unit_ensures! { $name }
103			decl_unit_ensures! { $( $rest )* }
104		};
105		() => {}
106	}
107	decl_unit_ensures!(
108		StakingAdmin,
109		Treasurer,
110		FellowshipAdmin,
111		GeneralAdmin,
112		AuctionAdmin,
113		LeaseAdmin,
114		ReferendumCanceller,
115		ReferendumKiller,
116		WhitelistedCaller,
117	);
118
119	macro_rules! decl_ensure {
120		(
121			$vis:vis type $name:ident: EnsureOrigin<Success = $success_type:ty> {
122				$( $item:ident = $success:expr, )*
123			}
124		) => {
125			$vis struct $name;
126			impl<O: Into<Result<Origin, O>> + From<Origin>>
127				EnsureOrigin<O> for $name
128			{
129				type Success = $success_type;
130				fn try_origin(o: O) -> Result<Self::Success, O> {
131					o.into().and_then(|o| match o {
132						$(
133							Origin::$item => Ok($success),
134						)*
135						r => Err(O::from(r)),
136					})
137				}
138				#[cfg(feature = "runtime-benchmarks")]
139				fn try_successful_origin() -> Result<O, ()> {
140					// By convention the more privileged origins go later, so for greatest chance
141					// of success, we want the last one.
142					let _result: Result<O, ()> = Err(());
143					$(
144						let _result: Result<O, ()> = Ok(O::from(Origin::$item));
145					)*
146					_result
147				}
148			}
149		}
150	}
151
152	decl_ensure! {
153		pub type Spender: EnsureOrigin<Success = Balance> {
154			SmallTipper = 250 * 3 * CENTS,
155			BigTipper = 1 * GRAND,
156			SmallSpender = 10 * GRAND,
157			MediumSpender = 100 * GRAND,
158			BigSpender = 1_000 * GRAND,
159			Treasurer = 10_000 * GRAND,
160		}
161	}
162}