difftreelog
Add todo comment to 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::{Zero},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;50pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;51pub const START_INFLATION_PERCENT: u32 = 10;52pub const END_INFLATION_PERCENT: u32 = 4;5354pub trait Config: system::Config {55 type Currency: Currency<Self::AccountId>;56 type TreasuryAccountId: Get<Self::AccountId>;57 type InflationBlockInterval: Get<Self::BlockNumber>;58}5960decl_storage! {61 trait Store for Module<T: Config> as Inflation {62 /// starting year total issuance63 pub StartingYearTotalIssuance get(fn starting_year_total_issuance): BalanceOf<T>;6465 /// Current block inflation66 pub BlockInflation get(fn block_inflation): BalanceOf<T>;67 }68}6970decl_module! {71 pub struct Module<T: Config> for enum Call72 where73 origin: T::Origin,74 {75 const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();7677 fn on_initialize(now: T::BlockNumber) -> Weight78 {79 let mut consumed_weight = 0;80 let mut add_weight = |reads, writes, weight| {81 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);82 consumed_weight += weight;83 };8485 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);8687 // Recalculate inflation on the first block of the year (or if it is not initialized yet)88 if (now % T::BlockNumber::from(YEAR)).is_zero() || <BlockInflation<T>>::get().is_zero() {89 let current_year: u32 = (now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);9091 let one_percent = Perbill::from_percent(1);9293 if current_year <= TOTAL_YEARS_UNTIL_FLAT {94 let amount: BalanceOf<T> = Perbill::from_rational(95 block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),96 YEAR * TOTAL_YEARS_UNTIL_FLAT97 ) * ( one_percent * T::Currency::total_issuance() );98 <BlockInflation<T>>::put(amount);99 }100 else {101 let amount: BalanceOf<T> = Perbill::from_rational(102 block_interval * END_INFLATION_PERCENT,103 YEAR104 ) * (one_percent * T::Currency::total_issuance());105 <BlockInflation<T>>::put(amount);106 }107 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());108109 // First time deposit110 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());111112 add_weight(7, 6, 28_300_000);113 }114115 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account116 else if (now % T::BlockNumber::from(block_interval)).is_zero() {117 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();118119 add_weight(3, 2, 12_900_000);120 }121122 consumed_weight123 }124125 }126}1//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::{Zero},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;50pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;51pub const START_INFLATION_PERCENT: u32 = 10;52pub const END_INFLATION_PERCENT: u32 = 4;5354pub trait Config: system::Config {55 type Currency: Currency<Self::AccountId>;56 type TreasuryAccountId: Get<Self::AccountId>;57 type InflationBlockInterval: Get<Self::BlockNumber>;58}5960decl_storage! {61 trait Store for Module<T: Config> as Inflation {62 /// starting year total issuance63 pub StartingYearTotalIssuance get(fn starting_year_total_issuance): BalanceOf<T>;6465 /// Current block inflation66 pub BlockInflation get(fn block_inflation): BalanceOf<T>;67 }68}6970decl_module! {71 pub struct Module<T: Config> for enum Call72 where73 origin: T::Origin,74 {75 const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();7677 fn on_initialize(now: T::BlockNumber) -> Weight78 {79 let mut consumed_weight = 0;80 let mut add_weight = |reads, writes, weight| {81 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);82 consumed_weight += weight;83 };8485 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);8687 // TODO: Rewrite inflation to use block timestamp instead of block number88 // let _now = <timestamp::Module<T>>::get();8990 // Recalculate inflation on the first block of the year (or if it is not initialized yet)91 if (now % T::BlockNumber::from(YEAR)).is_zero() || <BlockInflation<T>>::get().is_zero() {92 let current_year: u32 = (now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);9394 let one_percent = Perbill::from_percent(1);9596 if current_year <= TOTAL_YEARS_UNTIL_FLAT {97 let amount: BalanceOf<T> = Perbill::from_rational(98 block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),99 YEAR * TOTAL_YEARS_UNTIL_FLAT100 ) * ( one_percent * T::Currency::total_issuance() );101 <BlockInflation<T>>::put(amount);102 }103 else {104 let amount: BalanceOf<T> = Perbill::from_rational(105 block_interval * END_INFLATION_PERCENT,106 YEAR107 ) * (one_percent * T::Currency::total_issuance());108 <BlockInflation<T>>::put(amount);109 }110 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());111112 // First time deposit113 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());114115 add_weight(7, 6, 28_300_000);116 }117118 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account119 else if (now % T::BlockNumber::from(block_interval)).is_zero() {120 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();121122 add_weight(3, 2, 12_900_000);123 }124125 consumed_weight126 }127128 }129}