#![cfg(any(test, feature = "runtime-benchmarks"))]
#![allow(non_snake_case)]
use frame_support::{
construct_runtime, derive_impl,
dynamic_params::{dynamic_pallet_params, dynamic_params},
traits::EnsureOriginWithArg,
};
use crate as pallet_parameters;
use crate::*;
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Runtime {
type Block = frame_system::mocking::MockBlock<Runtime>;
type AccountData = pallet_balances::AccountData<<Self as pallet_balances::Config>::Balance>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Runtime {
type AccountStore = System;
}
#[docify::export]
#[dynamic_params(RuntimeParameters, pallet_parameters::Parameters::<Runtime>)]
pub mod dynamic_params {
use super::*;
#[dynamic_pallet_params]
#[codec(index = 3)]
pub mod pallet1 {
#[codec(index = 0)]
pub static Key1: u64 = 0;
#[codec(index = 1)]
pub static Key2: u32 = 1;
#[codec(index = 2)]
pub static Key3: u128 = 2;
}
#[dynamic_pallet_params]
#[codec(index = 1)]
pub mod pallet2 {
#[codec(index = 2)]
pub static Key1: u64 = 0;
#[codec(index = 1)]
pub static Key2: u32 = 2;
#[codec(index = 0)]
pub static Key3: u128 = 4;
}
#[dynamic_pallet_params]
#[codec(index = 2)]
pub mod nis {
#[codec(index = 0)]
pub static Target: u64 = 0;
}
#[dynamic_pallet_params]
#[codec(index = 4)]
pub mod somE_weird_SPElLInG_s {
#[codec(index = 0)]
pub static V: u64 = 0;
}
}
#[docify::export(benchmarking_default)]
#[cfg(feature = "runtime-benchmarks")]
impl Default for RuntimeParameters {
fn default() -> Self {
RuntimeParameters::Pallet1(dynamic_params::pallet1::Parameters::Key1(
dynamic_params::pallet1::Key1,
Some(123),
))
}
}
#[docify::export]
mod custom_origin {
use super::*;
pub struct ParamsManager;
impl EnsureOriginWithArg<RuntimeOrigin, RuntimeParametersKey> for ParamsManager {
type Success = ();
fn try_origin(
origin: RuntimeOrigin,
key: &RuntimeParametersKey,
) -> Result<Self::Success, RuntimeOrigin> {
#[cfg(feature = "runtime-benchmarks")]
if ensure_signed(origin.clone()).is_ok_and(|acc| acc == 123) {
return Ok(());
}
match key {
RuntimeParametersKey::SomEWeirdSPElLInGS(_) |
RuntimeParametersKey::Nis(_) |
RuntimeParametersKey::Pallet1(_) => ensure_root(origin.clone()),
RuntimeParametersKey::Pallet2(_) => ensure_signed(origin.clone()).map(|_| ()),
}
.map_err(|_| origin)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(_key: &RuntimeParametersKey) -> Result<RuntimeOrigin, ()> {
Ok(RuntimeOrigin::signed(123))
}
}
}
#[docify::export(impl_config)]
#[derive_impl(pallet_parameters::config_preludes::TestDefaultConfig)]
impl Config for Runtime {
type AdminOrigin = custom_origin::ParamsManager;
}
#[docify::export(usage)]
impl pallet_example_basic::Config for Runtime {
type MagicNumber = dynamic_params::pallet1::Key1;
type WeightInfo = ();
}
construct_runtime!(
pub enum Runtime {
System: frame_system,
PalletParameters: crate,
Example: pallet_example_basic,
Balances: pallet_balances,
}
);
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut ext = sp_io::TestExternalities::new(Default::default());
ext.execute_with(|| System::set_block_number(1));
ext
}
pub(crate) fn assert_last_event(generic_event: RuntimeEvent) {
let events = frame_system::Pallet::<Runtime>::events();
let frame_system::EventRecord { event, .. } = &events.last().expect("Event expected");
assert_eq!(event, &generic_event);
}