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
//! Custom origins for governance interventions.
15

            
16
pub use custom_origins::*;
17

            
18
#[frame_support::pallet]
19
pub mod custom_origins {
20
	use frame_support::pallet_prelude::*;
21
	use strum_macros::EnumString;
22

            
23
	#[pallet::config]
24
	pub trait Config: frame_system::Config {}
25

            
26
22
	#[pallet::pallet]
27
	pub struct Pallet<T>(_);
28

            
29
	#[derive(
30
41
		PartialEq, Eq, Clone, MaxEncodedLen, Encode, Decode, TypeInfo, RuntimeDebug, EnumString,
31
	)]
32
	#[strum(serialize_all = "snake_case")]
33
	#[pallet::origin]
34
	pub enum Origin {
35
		/// Origin able to dispatch a whitelisted call.
36
		WhitelistedCaller,
37
		/// General admin
38
		GeneralAdmin,
39
		/// Origin able to cancel referenda.
40
		ReferendumCanceller,
41
		/// Origin able to kill referenda.
42
		ReferendumKiller,
43
		/// Fast General Admin
44
		FastGeneralAdmin,
45
	}
46

            
47
	macro_rules! decl_unit_ensures {
48
		( $name:ident: $success_type:ty = $success:expr ) => {
49
			pub struct $name;
50
			impl<O: Into<Result<Origin, O>> + From<Origin>>
51
				EnsureOrigin<O> for $name
52
			{
53
				type Success = $success_type;
54
16
				fn try_origin(o: O) -> Result<Self::Success, O> {
55
16
					o.into().and_then(|o| match o {
56
14
						Origin::$name => Ok($success),
57
						r => Err(O::from(r)),
58
16
					})
59
16
				}
60
				#[cfg(feature = "runtime-benchmarks")]
61
				fn try_successful_origin() -> Result<O, ()> {
62
					Ok(O::from(Origin::$name))
63
				}
64
			}
65
		};
66
		( $name:ident ) => { decl_unit_ensures! { $name : () = () } };
67
		( $name:ident: $success_type:ty = $success:expr, $( $rest:tt )* ) => {
68
			decl_unit_ensures! { $name: $success_type = $success }
69
			decl_unit_ensures! { $( $rest )* }
70
		};
71
		( $name:ident, $( $rest:tt )* ) => {
72
			decl_unit_ensures! { $name }
73
			decl_unit_ensures! { $( $rest )* }
74
		};
75
		() => {}
76
	}
77
	decl_unit_ensures!(
78
		ReferendumCanceller,
79
		ReferendumKiller,
80
		WhitelistedCaller,
81
		GeneralAdmin,
82
		FastGeneralAdmin,
83
	);
84
}