--- a/pallets/inflation/src/lib.rs +++ b/pallets/inflation/src/lib.rs @@ -20,19 +20,19 @@ mod tests; pub use frame_support::{ - construct_runtime, decl_module, decl_storage, - ensure, - traits::{ - Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced, - Randomness, IsSubType, WithdrawReasons, - }, - weights::{ - constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, - DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight, - WeightToFeePolynomial, DispatchClass, - }, - StorageValue, - transactional, + construct_runtime, decl_module, decl_storage, + ensure, + traits::{ + Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced, + Randomness, IsSubType, WithdrawReasons, + }, + weights::{ + constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, + DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight, + WeightToFeePolynomial, DispatchClass, + }, + StorageValue, + transactional, }; // #[cfg(feature = "runtime-benchmarks")] @@ -40,7 +40,7 @@ use sp_runtime::{ Perbill, - traits::{Zero} + traits::{Zero} }; use sp_std::convert::TryInto; @@ -56,75 +56,75 @@ pub const END_INFLATION_PERCENT: u32 = 4; pub trait Config: system::Config { - type Currency: Currency; - type TreasuryAccountId: Get; - type InflationBlockInterval: Get; + type Currency: Currency; + type TreasuryAccountId: Get; + type InflationBlockInterval: Get; } decl_storage! { - trait Store for Module as Inflation { - /// starting year total issuance - pub StartingYearTotalIssuance get(fn starting_year_total_issuance): BalanceOf; + trait Store for Module as Inflation { + /// starting year total issuance + pub StartingYearTotalIssuance get(fn starting_year_total_issuance): BalanceOf; - /// Current block inflation - pub BlockInflation get(fn block_inflation): BalanceOf; - } + /// Current block inflation + pub BlockInflation get(fn block_inflation): BalanceOf; + } } decl_module! { - pub struct Module for enum Call - where - origin: T::Origin, - { - const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get(); + pub struct Module for enum Call + where + origin: T::Origin, + { + const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get(); - fn on_initialize(now: T::BlockNumber) -> Weight - { + fn on_initialize(now: T::BlockNumber) -> Weight + { let mut consumed_weight = 0; let mut add_weight = |reads, writes, weight| { consumed_weight += T::DbWeight::get().reads_writes(reads, writes); consumed_weight += weight; }; - let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0); + let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0); - // Recalculate inflation on the first block of the year (or if it is not initialized yet) - if (now % T::BlockNumber::from(YEAR)).is_zero() || >::get().is_zero() { - let current_year: u32 = (now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0); + // Recalculate inflation on the first block of the year (or if it is not initialized yet) + if (now % T::BlockNumber::from(YEAR)).is_zero() || >::get().is_zero() { + let current_year: u32 = (now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0); - let one_percent = Perbill::from_percent(1); + let one_percent = Perbill::from_percent(1); - if current_year <= TOTAL_YEARS_UNTIL_FLAT { - let amount: BalanceOf = Perbill::from_rational_approximation( - block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)), - YEAR * TOTAL_YEARS_UNTIL_FLAT - ) * ( one_percent * T::Currency::total_issuance() ); - >::put(amount); - } - else { - let amount: BalanceOf = Perbill::from_rational_approximation( - block_interval * END_INFLATION_PERCENT, - YEAR - ) * (one_percent * T::Currency::total_issuance()); - >::put(amount); - } - >::set(T::Currency::total_issuance()); + if current_year <= TOTAL_YEARS_UNTIL_FLAT { + let amount: BalanceOf = Perbill::from_rational_approximation( + block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)), + YEAR * TOTAL_YEARS_UNTIL_FLAT + ) * ( one_percent * T::Currency::total_issuance() ); + >::put(amount); + } + else { + let amount: BalanceOf = Perbill::from_rational_approximation( + block_interval * END_INFLATION_PERCENT, + YEAR + ) * (one_percent * T::Currency::total_issuance()); + >::put(amount); + } + >::set(T::Currency::total_issuance()); - // First time deposit - T::Currency::deposit_creating(&T::TreasuryAccountId::get(), >::get()); + // First time deposit + T::Currency::deposit_creating(&T::TreasuryAccountId::get(), >::get()); - add_weight(7, 6, 28_300_000); - } + add_weight(7, 6, 28_300_000); + } - // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account - else if (now % T::BlockNumber::from(block_interval)).is_zero() { - T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), >::get()).ok(); + // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account + else if (now % T::BlockNumber::from(block_interval)).is_zero() { + T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), >::get()).ok(); - add_weight(3, 2, 12_900_000); - } + add_weight(3, 2, 12_900_000); + } - consumed_weight - } + consumed_weight + } - } + } } --- a/pallets/inflation/src/tests.rs +++ b/pallets/inflation/src/tests.rs @@ -2,39 +2,39 @@ mod tests { use crate as pallet_inflation; - use frame_system; + use frame_system; use frame_support::{traits::{Currency}, parameter_types}; - use frame_support::{traits::OnInitialize}; + use frame_support::{traits::OnInitialize}; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header}; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; - const YEAR: u64 = 5_259_600; + const YEAR: u64 = 5_259_600; - parameter_types! { - pub const ExistentialDeposit: u64 = 1; - pub const MaxLocks: u32 = 50; - } - - impl pallet_balances::Config for Test { - type AccountStore = System; - type Balance = u64; - type DustRemoval = (); - type Event = (); - type ExistentialDeposit = ExistentialDeposit; - type WeightInfo = (); - type MaxLocks = MaxLocks; - } - + parameter_types! { + pub const ExistentialDeposit: u64 = 1; + pub const MaxLocks: u32 = 50; + } + + impl pallet_balances::Config for Test { + type AccountStore = System; + type Balance = u64; + type DustRemoval = (); + type Event = (); + type ExistentialDeposit = ExistentialDeposit; + type WeightInfo = (); + type MaxLocks = MaxLocks; + } + frame_support::construct_runtime!( pub enum Test where Block = Block, NodeBlock = Block, UncheckedExtrinsic = UncheckedExtrinsic, { - Balances: pallet_balances::{Module, Call, Storage}, + Balances: pallet_balances::{Module, Call, Storage}, System: frame_system::{Module, Call, Config, Storage, Event}, Inflation: pallet_inflation::{Module, Call, Storage}, } @@ -44,198 +44,198 @@ pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights::simple_max(1024); - pub const SS58Prefix: u8 = 42; - } + pub const SS58Prefix: u8 = 42; + } - impl frame_system::Config for Test { - type BaseCallFilter = (); - type BlockWeights = (); - type BlockLength = (); - type DbWeight = (); - type Origin = Origin; - type Call = Call; - type Index = u64; - type BlockNumber = u64; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = u64; - type Lookup = IdentityLookup; - type Header = Header; - type Event = (); - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - } + impl frame_system::Config for Test { + type BaseCallFilter = (); + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type Origin = Origin; + type Call = Call; + type Index = u64; + type BlockNumber = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type Event = (); + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = SS58Prefix; + } - parameter_types! { - pub TreasuryAccountId: u64 = 1234; - pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied - } - - impl pallet_inflation::Config for Test { - type Currency = Balances; - type TreasuryAccountId = TreasuryAccountId; - type InflationBlockInterval = InflationBlockInterval; - } + parameter_types! { + pub TreasuryAccountId: u64 = 1234; + pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied + } + + impl pallet_inflation::Config for Test { + type Currency = Balances; + type TreasuryAccountId = TreasuryAccountId; + type InflationBlockInterval = InflationBlockInterval; + } - // Build genesis storage according to the mock runtime. - pub fn new_test_ext() -> sp_io::TestExternalities { - frame_system::GenesisConfig::default().build_storage::().unwrap().into() - } + // Build genesis storage according to the mock runtime. + pub fn new_test_ext() -> sp_io::TestExternalities { + frame_system::GenesisConfig::default().build_storage::().unwrap().into() + } - #[test] + #[test] fn inflation_works() { new_test_ext().execute_with(|| { - // Total issuance = 1_000_000_000 - let initial_issuance: u64 = 1_000_000_000; - let _ = >::deposit_creating(&1234, initial_issuance); - assert_eq!(Balances::free_balance(1234), initial_issuance); + // Total issuance = 1_000_000_000 + let initial_issuance: u64 = 1_000_000_000; + let _ = >::deposit_creating(&1234, initial_issuance); + assert_eq!(Balances::free_balance(1234), initial_issuance); - // BlockInflation should be set after 1st block and - // first inflation deposit should be equal to BlockInflation - Inflation::on_initialize(1); - assert!(Inflation::block_inflation() > 0); - assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation()); + // BlockInflation should be set after 1st block and + // first inflation deposit should be equal to BlockInflation + Inflation::on_initialize(1); + assert!(Inflation::block_inflation() > 0); + assert_eq!(Balances::free_balance(1234) - initial_issuance, Inflation::block_inflation()); }); } - #[test] + #[test] fn inflation_second_deposit() { new_test_ext().execute_with(|| { - // Total issuance = 1_000_000_000 - let initial_issuance: u64 = 1_000_000_000; - let _ = >::deposit_creating(&1234, initial_issuance); - assert_eq!(Balances::free_balance(1234), initial_issuance); + // Total issuance = 1_000_000_000 + let initial_issuance: u64 = 1_000_000_000; + let _ = >::deposit_creating(&1234, initial_issuance); + assert_eq!(Balances::free_balance(1234), initial_issuance); Inflation::on_initialize(1); - // Next inflation deposit happens when block is multiple of InflationBlockInterval - let mut block: u32 = 2; - let balance_before: u64 = Balances::free_balance(1234); - while block % InflationBlockInterval::get() != 0 { - Inflation::on_initialize(block as u64); - block += 1; - } - let balance_just_before: u64 = Balances::free_balance(1234); - assert_eq!(balance_before, balance_just_before); + // Next inflation deposit happens when block is multiple of InflationBlockInterval + let mut block: u32 = 2; + let balance_before: u64 = Balances::free_balance(1234); + while block % InflationBlockInterval::get() != 0 { + Inflation::on_initialize(block as u64); + block += 1; + } + let balance_just_before: u64 = Balances::free_balance(1234); + assert_eq!(balance_before, balance_just_before); - // The block with inflation - Inflation::on_initialize(block as u64); - let balance_after: u64 = Balances::free_balance(1234); - assert_eq!(balance_after - balance_just_before, Inflation::block_inflation()); + // The block with inflation + Inflation::on_initialize(block as u64); + let balance_after: u64 = Balances::free_balance(1234); + assert_eq!(balance_after - balance_just_before, Inflation::block_inflation()); }); } - #[test] + #[test] fn inflation_in_1_year() { new_test_ext().execute_with(|| { - // Total issuance = 1_000_000_000 - let initial_issuance: u64 = 1_000_000_000; - let _ = >::deposit_creating(&1234, initial_issuance); - assert_eq!(Balances::free_balance(1234), initial_issuance); + // Total issuance = 1_000_000_000 + let initial_issuance: u64 = 1_000_000_000; + let _ = >::deposit_creating(&1234, initial_issuance); + assert_eq!(Balances::free_balance(1234), initial_issuance); Inflation::on_initialize(1); - let block_inflation_year_0 = Inflation::block_inflation(); + let block_inflation_year_0 = Inflation::block_inflation(); - Inflation::on_initialize(YEAR); - let block_inflation_year_1 = Inflation::block_inflation(); + Inflation::on_initialize(YEAR); + let block_inflation_year_1 = Inflation::block_inflation(); - // Assert that year 1 inflation is less than year 0 - assert!(block_inflation_year_0 > block_inflation_year_1); + // Assert that year 1 inflation is less than year 0 + assert!(block_inflation_year_0 > block_inflation_year_1); }); } - #[test] + #[test] fn inflation_in_1_to_9_years() { new_test_ext().execute_with(|| { - // Total issuance = 1_000_000_000 - let initial_issuance: u64 = 1_000_000_000; - let _ = >::deposit_creating(&1234, initial_issuance); - assert_eq!(Balances::free_balance(1234), initial_issuance); - Inflation::on_initialize(1); + // Total issuance = 1_000_000_000 + let initial_issuance: u64 = 1_000_000_000; + let _ = >::deposit_creating(&1234, initial_issuance); + assert_eq!(Balances::free_balance(1234), initial_issuance); + Inflation::on_initialize(1); - for year in 1..=9 { - let block_inflation_year_before = Inflation::block_inflation(); - Inflation::on_initialize(YEAR * year); - let block_inflation_year_after = Inflation::block_inflation(); + for year in 1..=9 { + let block_inflation_year_before = Inflation::block_inflation(); + Inflation::on_initialize(YEAR * year); + let block_inflation_year_after = Inflation::block_inflation(); - // Assert that next year inflation is less than previous year inflation - assert!(block_inflation_year_before > block_inflation_year_after); - } + // Assert that next year inflation is less than previous year inflation + assert!(block_inflation_year_before > block_inflation_year_after); + } }); } - #[test] + #[test] fn inflation_after_year_10_is_flat() { new_test_ext().execute_with(|| { - // Total issuance = 1_000_000_000 - let initial_issuance: u64 = 1_000_000_000; - let _ = >::deposit_creating(&1234, initial_issuance); - assert_eq!(Balances::free_balance(1234), initial_issuance); - Inflation::on_initialize(YEAR * 9); + // Total issuance = 1_000_000_000 + let initial_issuance: u64 = 1_000_000_000; + let _ = >::deposit_creating(&1234, initial_issuance); + assert_eq!(Balances::free_balance(1234), initial_issuance); + Inflation::on_initialize(YEAR * 9); - for year in 10..=20 { - let block_inflation_year_before = Inflation::block_inflation(); - Inflation::on_initialize(YEAR * year); - let block_inflation_year_after = Inflation::block_inflation(); + for year in 10..=20 { + let block_inflation_year_before = Inflation::block_inflation(); + Inflation::on_initialize(YEAR * year); + let block_inflation_year_after = Inflation::block_inflation(); - // Assert that next year inflation is equal to previous year inflation - assert_eq!(block_inflation_year_before, block_inflation_year_after); - } + // Assert that next year inflation is equal to previous year inflation + assert_eq!(block_inflation_year_before, block_inflation_year_after); + } }); } - #[test] + #[test] fn inflation_rate_by_year() { new_test_ext().execute_with(|| { - let payouts: u64 = YEAR / InflationBlockInterval::get() as u64; + let payouts: u64 = YEAR / InflationBlockInterval::get() as u64; - // Inflation starts at 10% and does down by 2/3% every year until year 9 (included), - // then it is flat. - let payout_by_year: [u64; 11] = [ - 1000, - 933, - 867, - 800, - 733, - 667, - 600, - 533, - 467, - 400, - 400 - ]; + // Inflation starts at 10% and does down by 2/3% every year until year 9 (included), + // then it is flat. + let payout_by_year: [u64; 11] = [ + 1000, + 933, + 867, + 800, + 733, + 667, + 600, + 533, + 467, + 400, + 400 + ]; - // For accuracy total issuance = payout0 * payouts * 10; - let initial_issuance: u64 = payout_by_year[0] * payouts * 10; - let _ = >::deposit_creating(&1234, initial_issuance); - assert_eq!(Balances::free_balance(1234), initial_issuance); + // For accuracy total issuance = payout0 * payouts * 10; + let initial_issuance: u64 = payout_by_year[0] * payouts * 10; + let _ = >::deposit_creating(&1234, initial_issuance); + assert_eq!(Balances::free_balance(1234), initial_issuance); - for year in 0..=10 { - // Year first block - Inflation::on_initialize(year*YEAR); - let mut actual_payout = Inflation::block_inflation(); - assert_eq!(actual_payout, payout_by_year[year as usize]); + for year in 0..=10 { + // Year first block + Inflation::on_initialize(year*YEAR); + let mut actual_payout = Inflation::block_inflation(); + assert_eq!(actual_payout, payout_by_year[year as usize]); - // Year second block - Inflation::on_initialize(year*YEAR+1); - actual_payout = Inflation::block_inflation(); - assert_eq!(actual_payout, payout_by_year[year as usize]); + // Year second block + Inflation::on_initialize(year*YEAR+1); + actual_payout = Inflation::block_inflation(); + assert_eq!(actual_payout, payout_by_year[year as usize]); - // Year middle block - Inflation::on_initialize(year*YEAR + YEAR/2); - actual_payout = Inflation::block_inflation(); - assert_eq!(actual_payout, payout_by_year[year as usize]); + // Year middle block + Inflation::on_initialize(year*YEAR + YEAR/2); + actual_payout = Inflation::block_inflation(); + assert_eq!(actual_payout, payout_by_year[year as usize]); - // Year last block - Inflation::on_initialize((year + 1)*YEAR-1); - actual_payout = Inflation::block_inflation(); - assert_eq!(actual_payout, payout_by_year[year as usize]); - } + // Year last block + Inflation::on_initialize((year + 1)*YEAR-1); + actual_payout = Inflation::block_inflation(); + assert_eq!(actual_payout, payout_by_year[year as usize]); + } }); } }