1234567891011121314151617181920212223242526272829303132#![cfg_attr(not(feature = "std"), no_std)]3334#[cfg(feature = "runtime-benchmarks")]35mod benchmarking;3637#[cfg(test)]38mod tests;3940use frame_support::{41 dispatch::{DispatchResult},42 traits::{Currency, Get},43};44pub use pallet::*;45use sp_runtime::{46 Perbill,47 traits::{BlockNumberProvider},48};4950use sp_std::convert::TryInto;5152type BalanceOf<T> =53 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;5455pub const YEAR: u32 = 5_259_600; 56 57pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;58pub const START_INFLATION_PERCENT: u32 = 10;59pub const END_INFLATION_PERCENT: u32 = 4;6061#[frame_support::pallet]62pub mod pallet {63 use super::*;64 use frame_support::pallet_prelude::*;65 use frame_system::pallet_prelude::*;6667 #[pallet::config]68 pub trait Config: frame_system::Config {69 type Currency: Currency<Self::AccountId>;70 type TreasuryAccountId: Get<Self::AccountId>;7172 73 type BlockNumberProvider: BlockNumberProvider<BlockNumber = Self::BlockNumber>;7475 76 #[pallet::constant]77 type InflationBlockInterval: Get<Self::BlockNumber>;78 }7980 #[pallet::pallet]81 #[pallet::generate_store(pub(super) trait Store)]82 pub struct Pallet<T>(_);8384 85 #[pallet::storage]86 pub type StartingYearTotalIssuance<T: Config> =87 StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;8889 90 #[pallet::storage]91 pub type BlockInflation<T: Config> = StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9293 94 #[pallet::storage]95 pub type NextInflationBlock<T: Config> =96 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;9798 99 #[pallet::storage]100 pub type NextRecalculationBlock<T: Config> =101 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;102103 104 #[pallet::storage]105 pub type StartBlock<T: Config> = StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;106107 #[pallet::hooks]108 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {109 fn on_initialize(_: T::BlockNumber) -> Weight110 where111 <T as frame_system::Config>::BlockNumber: From<u32>,112 {113 let mut consumed_weight = Weight::zero();114 let mut add_weight = |reads, writes, weight| {115 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);116 consumed_weight += weight;117 };118119 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);120 let current_relay_block = T::BlockNumberProvider::current_block_number();121 let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();122 add_weight(1, 0, Weight::from_ref_time(5_000_000));123124 125 126 if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) {127 128 129 130 let next_recalculation: T::BlockNumber = <NextRecalculationBlock<T>>::get();131 add_weight(1, 0, Weight::zero());132 if current_relay_block >= next_recalculation {133 Self::recalculate_inflation(next_recalculation);134 add_weight(0, 4, Weight::from_ref_time(5_000_000));135 }136137 T::Currency::deposit_into_existing(138 &T::TreasuryAccountId::get(),139 <BlockInflation<T>>::get(),140 )141 .ok();142143 144 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());145146 add_weight(3, 3, Weight::from_ref_time(10_000_000));147 }148149 consumed_weight150 }151 }152153 #[pallet::call]154 impl<T: Config> Pallet<T> {155 156 157 158 159 160 161 162 163 164 165 166 #[pallet::call_index(0)]167 #[pallet::weight(0)]168 pub fn start_inflation(169 origin: OriginFor<T>,170 inflation_start_relay_block: T::BlockNumber,171 ) -> DispatchResult172 where173 <T as frame_system::Config>::BlockNumber: From<u32>,174 {175 ensure_root(origin)?;176177 178 if <StartBlock<T>>::get() == 0u32.into() {179 180 <StartBlock<T>>::set(inflation_start_relay_block);181182 183 Self::recalculate_inflation(inflation_start_relay_block);184 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);185 <NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());186187 188 T::Currency::deposit_creating(189 &T::TreasuryAccountId::get(),190 <BlockInflation<T>>::get(),191 );192 }193194 Ok(())195 }196 }197}198199impl<T: Config> Pallet<T> {200 pub fn recalculate_inflation(recalculation_block: T::BlockNumber) {201 let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get())202 / T::BlockNumber::from(YEAR))203 .try_into()204 .unwrap_or(0);205 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);206207 let one_percent = Perbill::from_percent(1);208209 if current_year <= TOTAL_YEARS_UNTIL_FLAT {210 let amount: BalanceOf<T> = Perbill::from_rational(211 block_interval212 * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT213 - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),214 YEAR * TOTAL_YEARS_UNTIL_FLAT,215 ) * (one_percent * T::Currency::total_issuance());216 <BlockInflation<T>>::put(amount);217 } else {218 let amount: BalanceOf<T> =219 Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR)220 * (one_percent * T::Currency::total_issuance());221 <BlockInflation<T>>::put(amount);222 }223 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());224225 226 <NextRecalculationBlock<T>>::set(recalculation_block + YEAR.into());227 }228}