difftreelog
Fix regular inflation
in: master
1 file changed
pallets/inflation/src/lib.rsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56#![recursion_limit = "1024"]7#![cfg_attr(not(feature = "std"), no_std)]89#[cfg(feature = "std")]10pub use std::*;1112pub use serde::{Serialize, Deserialize};1314#[cfg(feature = "runtime-benchmarks")]15mod benchmarking;1617#[cfg(test)]18mod tests;1920pub use frame_support::{21 construct_runtime, decl_module, decl_storage, ensure,22 traits::{23 Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,24 Randomness, IsSubType, WithdrawReasons,25 },26 weights::{27 constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},28 DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,29 WeightToFeePolynomial, DispatchClass,30 },31 StorageValue, transactional,32};3334// #[cfg(feature = "runtime-benchmarks")]35pub use frame_support::dispatch::DispatchResult;3637use sp_runtime::{38 Perbill,39 traits::{BlockNumberProvider},40};41use sp_std::convert::TryInto;4243use frame_system::{self as system};4445/// The balance type of this module.46pub type BalanceOf<T> =47 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;4849pub const YEAR: u32 = 5_259_600; // 6-second block50// pub const YEAR: u32 = 2_629_800; // 12-second block51pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;52pub const START_INFLATION_PERCENT: u32 = 10;53pub const END_INFLATION_PERCENT: u32 = 4;5455pub trait Config: system::Config {56 type Currency: Currency<Self::AccountId>;57 type TreasuryAccountId: Get<Self::AccountId>;58 type InflationBlockInterval: Get<Self::BlockNumber>;5960 // The block number provider61 type BlockNumberProvider: BlockNumberProvider<BlockNumber = Self::BlockNumber>;62}6364decl_storage! {65 trait Store for Module<T: Config> as Inflation {66 /// starting year total issuance67 pub StartingYearTotalIssuance get(fn starting_year_total_issuance): BalanceOf<T>;6869 /// Current block inflation70 pub BlockInflation get(fn block_inflation): BalanceOf<T>;7172 /// Next (relay) block when inflation is applied. This value is approximate.73 pub NextInflationBlock get(fn next_inflation_block): T::BlockNumber;7475 /// Next (relay) block when inflation is recalculated. This value is approximate.76 pub NextRecalculationBlock get(fn next_recalculation_block): T::BlockNumber;77 }78}7980decl_module! {81 pub struct Module<T: Config> for enum Call82 where83 origin: T::Origin,84 {85 const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();8687 fn on_initialize() -> Weight88 {89 let mut consumed_weight = 0;90 let mut add_weight = |reads, writes, weight| {91 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);92 consumed_weight += weight;93 };9495 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);96 let _now = T::BlockNumberProvider::current_block_number();97 let next_recalculation: T::BlockNumber = <NextRecalculationBlock<T>>::get();98 let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();99 add_weight(2, 0, 5_000_000);100101 // Recalculate inflation on the first block of the year (or if it is not initialized yet)102 if _now >= next_recalculation {103 let current_year: u32 = (next_recalculation / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);104105 let one_percent = Perbill::from_percent(1);106107 if current_year <= TOTAL_YEARS_UNTIL_FLAT {108 let amount: BalanceOf<T> = Perbill::from_rational(109 block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),110 YEAR * TOTAL_YEARS_UNTIL_FLAT111 ) * ( one_percent * T::Currency::total_issuance() );112 <BlockInflation<T>>::put(amount);113 }114 else {115 let amount: BalanceOf<T> = Perbill::from_rational(116 block_interval * END_INFLATION_PERCENT,117 YEAR118 ) * (one_percent * T::Currency::total_issuance());119 <BlockInflation<T>>::put(amount);120 }121 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());122123 // First time deposit124 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());125126 // Update recalculation and inflation blocks127 <NextRecalculationBlock<T>>::set(next_recalculation + YEAR.into());128 <NextInflationBlock<T>>::set(next_recalculation + block_interval.into() + YEAR.into());129130 add_weight(7, 8, 28_300_000);131 }132133 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account134 else if _now >= next_inflation {135 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();136137 // Update inflation block138 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());139140 add_weight(3, 3, 12_900_000);141 }142143 consumed_weight144 }145146 }147}