Skip to content

Commit

Permalink
feat: add OpenGov tracks to runtime common
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed May 3, 2024
1 parent 14b289b commit 1e743c5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod fees;
pub mod gateway;
pub mod migrations;
pub mod oracle;
pub mod origins;
pub mod remarks;
pub mod transfer_filter;
pub mod xcm;
Expand Down
81 changes: 81 additions & 0 deletions runtime/common/src/origins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2024 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

/// Custom origins for governance interventions.
pub mod gov {
pub use pallet_custom_origins::*;

#[frame_support::pallet]
pub mod pallet_custom_origins {
use frame_support::pallet_prelude::*;

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

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

#[derive(PartialEq, Eq, Clone, MaxEncodedLen, Encode, Decode, TypeInfo, RuntimeDebug)]
#[pallet::origin]
pub enum Origin {
/// Origin able to dispatch a whitelisted call.
WhitelistedCaller,
/// Origin for spending (any amount of) funds.
Treasurer,
/// Origin for pool related referenda.
PoolAdmin,
/// Origin able to cancel referenda.
ReferendumCanceller,
/// Origin able to kill referenda.
ReferendumKiller,
}

macro_rules! decl_unit_ensures {
( $name:ident: $success_type:ty = $success:expr ) => {
pub struct $name;
impl<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
Origin::$name => Ok($success),
r => Err(O::from(r)),
})
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Ok(O::from(Origin::$name))
}
}
};
( $name:ident ) => { decl_unit_ensures! { $name : () = () } };
( $name:ident: $success_type:ty = $success:expr, $( $rest:tt )* ) => {
decl_unit_ensures! { $name: $success_type = $success }
decl_unit_ensures! { $( $rest )* }
};
( $name:ident, $( $rest:tt )* ) => {
decl_unit_ensures! { $name }
decl_unit_ensures! { $( $rest )* }
};
() => {}
}

decl_unit_ensures!(
WhitelistedCaller,
PoolAdmin,
Treasurer,
ReferendumCanceller,
ReferendumKiller,
);
}
}

0 comments on commit 1e743c5

Please sign in to comment.