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
27
	#[pallet::pallet]
27
	pub struct Pallet<T>(_);
28

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

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