123456#![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};333435pub 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};444546pub type BalanceOf<T> =47 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;484950pub const YEAR: u32 = 2_629_800; 51pub 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>;59}6061decl_storage! {62 trait Store for Module<T: Config> as Inflation {63 64 pub StartingYearTotalIssuance get(fn starting_year_total_issuance): BalanceOf<T>;6566 67 pub BlockInflation get(fn block_inflation): BalanceOf<T>;68 }69}7071decl_module! {72 pub struct Module<T: Config> for enum Call73 where74 origin: T::Origin,75 {76 const InflationBlockInterval: T::BlockNumber = T::InflationBlockInterval::get();7778 fn on_initialize(now: T::BlockNumber) -> Weight79 {80 let mut consumed_weight = 0;81 let mut add_weight = |reads, writes, weight| {82 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);83 consumed_weight += weight;84 };8586 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);8788 89 9091 92 if (now % T::BlockNumber::from(YEAR)).is_zero() || <BlockInflation<T>>::get().is_zero() {93 let current_year: u32 = (now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);9495 let one_percent = Perbill::from_percent(1);9697 if current_year <= TOTAL_YEARS_UNTIL_FLAT {98 let amount: BalanceOf<T> = Perbill::from_rational(99 block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),100 YEAR * TOTAL_YEARS_UNTIL_FLAT101 ) * ( one_percent * T::Currency::total_issuance() );102 <BlockInflation<T>>::put(amount);103 }104 else {105 let amount: BalanceOf<T> = Perbill::from_rational(106 block_interval * END_INFLATION_PERCENT,107 YEAR108 ) * (one_percent * T::Currency::total_issuance());109 <BlockInflation<T>>::put(amount);110 }111 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());112113 114 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());115116 add_weight(7, 6, 28_300_000);117 }118119 120 else if (now % T::BlockNumber::from(block_interval)).is_zero() {121 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();122123 add_weight(3, 2, 12_900_000);124 }125126 consumed_weight127 }128129 }130}