From ecf2bb16a896f790fdac3551f3d71961a4c60bf3 Mon Sep 17 00:00:00 2001 From: librelois <c@elo.tf> Date: Mon, 19 Jul 2021 20:58:10 +0200 Subject: [PATCH] ref(pallet_ud): use pallet constant for ud params --- pallets/universal-dividend/Cargo.toml | 7 +++++-- pallets/universal-dividend/src/lib.rs | 17 ++++++++++------- pallets/universal-dividend/src/mock.rs | 11 +++++++---- runtime/src/lib.rs | 11 ++++++----- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/pallets/universal-dividend/Cargo.toml b/pallets/universal-dividend/Cargo.toml index 046a9dc66..982a4ff60 100644 --- a/pallets/universal-dividend/Cargo.toml +++ b/pallets/universal-dividend/Cargo.toml @@ -63,8 +63,7 @@ tag = 'monthly-2021-07' [package.metadata.docs.rs] targets = ['x86_64-unknown-linux-gnu'] -[dev-dependencies.serde] -version = '1.0.119' + ### DEV ### @@ -73,6 +72,10 @@ default-features = false git = 'https://github.com/paritytech/substrate.git' tag = 'monthly-2021-07' +[dev-dependencies.serde] +features = ["derive"] +version = '1.0.119' + [dev-dependencies.sp-core] default-features = false git = 'https://github.com/paritytech/substrate.git' diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs index 0e1015c41..e6e55b9bb 100644 --- a/pallets/universal-dividend/src/lib.rs +++ b/pallets/universal-dividend/src/lib.rs @@ -44,13 +44,16 @@ pub mod pallet { #[pallet::config] pub trait Config: frame_system::Config { + #[pallet::constant] /// Universal dividend creation period - const UD_CREATION_PERIOD: Self::BlockNumber; + type UdCreationPeriod: Get<Self::BlockNumber>; + #[pallet::constant] /// Universal dividend reevaluation period (in number of creation period) - const UD_REEVAL_PERIOD: BalanceOf<Self>; + type UdReevalPeriod: Get<BalanceOf<Self>>; + #[pallet::constant] /// Universal dividend reevaluation period in number of blocks - /// Must be equal to UD_CREATION_PERIOD * UD_REEVAl_PERIOD - const UD_REEVAL_PERIOD_IN_BLOCKS: Self::BlockNumber; + /// Must be equal to UdReevalPeriod * UdCreationPeriod + type UdReevalPeriodInBlocks: Get<Self::BlockNumber>; // The currency type Currency: Currency<Self::AccountId>; @@ -153,9 +156,9 @@ pub mod pallet { #[pallet::hooks] impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { fn on_initialize(n: T::BlockNumber) -> Weight { - if (n % T::UD_CREATION_PERIOD).is_zero() { + if (n % T::UdCreationPeriod::get()).is_zero() { let current_members_count = T::MembersCount::get(); - if (n % T::UD_REEVAL_PERIOD_IN_BLOCKS).is_zero() { + if (n % T::UdReevalPeriodInBlocks::get()).is_zero() { Self::reeval_ud(current_members_count) + Self::create_ud(current_members_count, n) } else { @@ -221,7 +224,7 @@ pub mod pallet { T::SquareMoneyGrowthRate::get(), monetary_mass, members_count, - T::UD_REEVAL_PERIOD, + T::UdReevalPeriod::get(), ); Self::deposit_event(Event::UdReevalued( diff --git a/pallets/universal-dividend/src/mock.rs b/pallets/universal-dividend/src/mock.rs index e82ea28f1..9d1e5d8a7 100644 --- a/pallets/universal-dividend/src/mock.rs +++ b/pallets/universal-dividend/src/mock.rs @@ -29,6 +29,7 @@ use sp_runtime::{ }; type Balance = u64; +type BlockNumber = u64; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; type Block = frame_system::mocking::MockBlock<Test>; @@ -96,6 +97,9 @@ impl pallet_balances::Config for Test { parameter_types! { pub const MembersCount: u64 = 3; pub const SquareMoneyGrowthRate: Permill = Permill::from_percent(10); + pub const UdCreationPeriod: BlockNumber = 2; + pub const UdReevalPeriod: Balance = 4; + pub const UdReevalPeriodInBlocks: BlockNumber = 8; // 2 * 4 } pub struct FakeWot; @@ -106,15 +110,14 @@ impl Get<Vec<u64>> for FakeWot { } impl pallet_universal_dividend::Config for Test { - const UD_CREATION_PERIOD: Self::BlockNumber = 2; - const UD_REEVAL_PERIOD: Balance = 4; - const UD_REEVAL_PERIOD_IN_BLOCKS: Self::BlockNumber = 8; // 2 * 4 - type Currency = pallet_balances::Pallet<Test>; type Event = Event; type MembersCount = MembersCount; type MembersIds = FakeWot; type SquareMoneyGrowthRate = SquareMoneyGrowthRate; + type UdCreationPeriod = UdCreationPeriod; + type UdReevalPeriod = UdReevalPeriod; + type UdReevalPeriodInBlocks = UdReevalPeriodInBlocks; } // Build genesis storage according to the mock runtime. diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 335a7af90..0ddf09043 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -376,6 +376,9 @@ impl pallet_certification::Config for Runtime { parameter_types! { pub const SquareMoneyGrowthRate: Permill = Permill::one(); + pub const UdCreationPeriod: BlockNumber = 20; + pub const UdReevalPeriod: Balance = 10; + pub const UdReevalPeriodInBlocks: BlockNumber = 20 * 10; } pub struct UdAccountsProvider; @@ -392,16 +395,14 @@ impl Get<Vec<AccountId>> for UdAccountsProvider { /// Configure the pallet universal-dividend in pallets/universal-dividend. impl pallet_universal_dividend::Config for Runtime { - const UD_CREATION_PERIOD: Self::BlockNumber = 20; - const UD_REEVAL_PERIOD: Balance = 10; - const UD_REEVAL_PERIOD_IN_BLOCKS: Self::BlockNumber = - Self::UD_CREATION_PERIOD * Self::UD_REEVAL_PERIOD as Self::BlockNumber; - type Currency = pallet_balances::Pallet<Runtime>; type Event = Event; type MembersCount = UdAccountsProvider; type MembersIds = UdAccountsProvider; type SquareMoneyGrowthRate = SquareMoneyGrowthRate; + type UdCreationPeriod = UdCreationPeriod; + type UdReevalPeriod = UdReevalPeriod; + type UdReevalPeriodInBlocks = UdReevalPeriodInBlocks; } /// Configure the pallet ud-accounts-storage -- GitLab