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::{43 fungible::{Balanced, Inspect, Mutate},44 Get,45 tokens::Precision,46 },47};48pub use pallet::*;49use sp_runtime::{Perbill, traits::BlockNumberProvider};5051use sp_std::convert::TryInto;5253type BalanceOf<T> =54 <<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;5556pub const YEAR: u32 = 5_259_600; 57 58pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;59pub const START_INFLATION_PERCENT: u32 = 10;60pub const END_INFLATION_PERCENT: u32 = 4;6162#[frame_support::pallet]63pub mod pallet {64 use super::*;65 use frame_support::pallet_prelude::*;66 use frame_system::pallet_prelude::*;6768 #[pallet::config]69 pub trait Config: frame_system::Config {70 type Currency: Balanced<Self::AccountId>71 + Inspect<Self::AccountId>72 + Mutate<Self::AccountId>;73 type TreasuryAccountId: Get<Self::AccountId>;7475 76 type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;7778 79 #[pallet::constant]80 type InflationBlockInterval: Get<BlockNumberFor<Self>>;81 }8283 #[pallet::pallet]84 pub struct Pallet<T>(_);8586 87 #[pallet::storage]88 pub type StartingYearTotalIssuance<T: Config> =89 StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9091 92 #[pallet::storage]93 pub type BlockInflation<T: Config> = StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9495 96 #[pallet::storage]97 pub type NextInflationBlock<T: Config> =98 StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;99100 101 #[pallet::storage]102 pub type NextRecalculationBlock<T: Config> =103 StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;104105 106 #[pallet::storage]107 pub type StartBlock<T: Config> =108 StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;109110 #[pallet::hooks]111 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {112 fn on_initialize(_: BlockNumberFor<T>) -> Weight113 where114 BlockNumberFor<T>: From<u32>,115 {116 let mut consumed_weight = Weight::zero();117 let mut add_weight = |reads, writes, weight| {118 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);119 consumed_weight += weight;120 };121122 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);123 let current_relay_block = T::BlockNumberProvider::current_block_number();124 let next_inflation: BlockNumberFor<T> = <NextInflationBlock<T>>::get();125 add_weight(1, 0, Weight::from_parts(5_000_000, 0));126127 128 129 if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) {130 131 132 133 let next_recalculation: BlockNumberFor<T> = <NextRecalculationBlock<T>>::get();134 add_weight(1, 0, Weight::zero());135 if current_relay_block >= next_recalculation {136 Self::recalculate_inflation(next_recalculation);137 add_weight(0, 4, Weight::from_parts(5_000_000, 0));138 }139140 T::Currency::mint_into(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get())141 .ok();142143 144 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());145146 add_weight(3, 3, Weight::from_parts(10_000_000, 0));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 168 169 170 #[pallet::weight(Weight::from_parts(0, 0))]171 pub fn start_inflation(172 origin: OriginFor<T>,173 inflation_start_relay_block: BlockNumberFor<T>,174 ) -> DispatchResult175 where176 BlockNumberFor<T>: From<u32>,177 {178 ensure_root(origin)?;179180 181 if <StartBlock<T>>::get() == 0u32.into() {182 183 <StartBlock<T>>::set(inflation_start_relay_block);184185 186 Self::recalculate_inflation(inflation_start_relay_block);187 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);188 <NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());189190 191 let _ = T::Currency::deposit(192 &T::TreasuryAccountId::get(),193 <BlockInflation<T>>::get(),194 Precision::Exact,195 )?;196 }197198 Ok(())199 }200 }201}202203impl<T: Config> Pallet<T> {204 pub fn recalculate_inflation(recalculation_block: BlockNumberFor<T>) {205 let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get())206 / BlockNumberFor::<T>::from(YEAR))207 .try_into()208 .unwrap_or(0);209 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);210211 let one_percent = Perbill::from_percent(1);212213 if current_year <= TOTAL_YEARS_UNTIL_FLAT {214 let amount: BalanceOf<T> = Perbill::from_rational(215 block_interval216 * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT217 - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),218 YEAR * TOTAL_YEARS_UNTIL_FLAT,219 ) * (one_percent * T::Currency::total_issuance());220 <BlockInflation<T>>::put(amount);221 } else {222 let amount: BalanceOf<T> =223 Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR)224 * (one_percent * T::Currency::total_issuance());225 <BlockInflation<T>>::put(amount);226 }227 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());228229 230 <NextRecalculationBlock<T>>::set(recalculation_block + YEAR.into());231 }232}