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

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

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