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
#![cfg_attr(not(feature = "std"), no_std)]
16

            
17
pub use custom_origins::*;
18

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

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

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

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

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