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

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

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