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
pub use custom_origins::*;
16

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

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

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

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

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