pallet_governance/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::manual_inspect)]
//! # Analog Governance Pallet
//!
//! This is custom pallet to be used for the ever extending mainnet governance.
//!
//! Currently only wraps a few important root call to lower the required privilege level
//! to a custom origin.
//!
//! See [`Call`] for a list of wrapped extrinsics.

pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[polkadot_sdk::frame_support::pallet]
pub mod pallet {
	// Import various useful types required by all FRAME pallets.
	use super::*;

	use polkadot_sdk::pallet_balances;
	use polkadot_sdk::{frame_support, frame_system};
	use polkadot_sdk::{pallet_staking, sp_runtime};

	use frame_support::pallet_prelude::*;
	use frame_system::pallet_prelude::*;

	// Additional custom imports
	use frame_system::{RawOrigin, WeightInfo as SystemWeights};

	#[cfg(feature = "develop")]
	use pallet_balances::WeightInfo as BalancesWeights;

	use pallet_staking::{ConfigOp, WeightInfo as StakingWeights};
	use sp_runtime::{Perbill, Percent};

	#[cfg(feature = "develop")]
	use sp_runtime::traits::StaticLookup;

	// Useful coupling shorthands
	type CurrencyBalanceOf<T> = <T as pallet_staking::Config>::CurrencyBalance;

	#[pallet::pallet]
	pub struct Pallet<T>(_);

	#[pallet::config]
	pub trait Config:
		polkadot_sdk::frame_system::Config + pallet_balances::Config + pallet_staking::Config
	{
		/// Allowed origin for system calls
		type SystemAdmin: EnsureOrigin<Self::RuntimeOrigin>;
		/// Allowed origin for staking calls
		type StakingAdmin: EnsureOrigin<Self::RuntimeOrigin>;
		#[cfg(feature = "develop")]
		/// Allowed origin for balances calls
		type BalancesAdmin: EnsureOrigin<Self::RuntimeOrigin>;
	}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
		// Wrapper around system pallet calls
		#[pallet::call_index(0)]
		#[pallet::weight(<T as frame_system::Config>::SystemWeightInfo::authorize_upgrade())]
		pub fn authorize_upgrade(origin: OriginFor<T>, code_hash: T::Hash) -> DispatchResult {
			T::SystemAdmin::ensure_origin(origin)?;
			frame_system::Pallet::<T>::authorize_upgrade(RawOrigin::Root.into(), code_hash)
		}

		// Wrapper around staking pallet calls
		#[pallet::call_index(1)]
		#[pallet::weight(<T as pallet_staking::Config>::WeightInfo::force_new_era())]
		pub fn force_new_era(origin: OriginFor<T>) -> DispatchResult {
			T::StakingAdmin::ensure_origin(origin)?;
			pallet_staking::Pallet::<T>::force_new_era(RawOrigin::Root.into())
		}

		#[pallet::call_index(2)]
		#[pallet::weight(<T as pallet_staking::Config>::WeightInfo::set_validator_count())]
		pub fn set_validator_count(
			origin: OriginFor<T>,
			#[pallet::compact] new: u32,
		) -> DispatchResult {
			T::StakingAdmin::ensure_origin(origin)?;
			pallet_staking::Pallet::<T>::set_validator_count(RawOrigin::Root.into(), new)
		}

		#[allow(clippy::too_many_arguments)]
		#[pallet::call_index(3)]
		#[pallet::weight(
			<T as pallet_staking::Config>::WeightInfo::set_staking_configs_all_set()
				.max(<T as pallet_staking::Config>::WeightInfo::set_staking_configs_all_remove())
		)]
		pub fn set_staking_configs(
			origin: OriginFor<T>,
			min_nominator_bond: ConfigOp<CurrencyBalanceOf<T>>,
			min_validator_bond: ConfigOp<CurrencyBalanceOf<T>>,
			max_nominator_count: ConfigOp<u32>,
			max_validator_count: ConfigOp<u32>,
			chill_threshold: ConfigOp<Percent>,
			min_commission: ConfigOp<Perbill>,
			max_staked_rewards: ConfigOp<Percent>,
		) -> DispatchResult {
			T::StakingAdmin::ensure_origin(origin)?;
			pallet_staking::Pallet::<T>::set_staking_configs(
				RawOrigin::Root.into(),
				min_nominator_bond,
				min_validator_bond,
				max_nominator_count,
				max_validator_count,
				chill_threshold,
				min_commission,
				max_staked_rewards,
			)
		}

		#[cfg(feature = "develop")]
		#[pallet::call_index(4)]
		#[pallet::weight(
			<T as pallet_balances::Config>::WeightInfo::force_set_balance_creating()
				.max(<T as pallet_balances::Config>::WeightInfo::force_set_balance_killing())
		)]
		pub fn force_set_balance(
			origin: OriginFor<T>,
			who: <T::Lookup as StaticLookup>::Source,
			#[pallet::compact] new_free: T::Balance,
		) -> DispatchResult {
			T::BalancesAdmin::ensure_origin(origin)?;
			pallet_balances::Pallet::<T>::force_set_balance(RawOrigin::Root.into(), who, new_free)
		}
	}
}