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, Imbalance,45 tokens::Precision,46 },47};48pub use pallet::*;49use sp_runtime::{50 Perbill,51 traits::{BlockNumberProvider, Zero},52};5354use sp_std::convert::TryInto;5556type BalanceOf<T> =57 <<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;5859pub const YEAR: u32 = 5_259_600; 60 61pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;62pub const START_INFLATION_PERCENT: u32 = 10;63pub const END_INFLATION_PERCENT: u32 = 4;6465#[frame_support::pallet]66pub mod pallet {67 use super::*;68 use frame_support::pallet_prelude::*;69 use frame_system::pallet_prelude::*;7071 #[pallet::config]72 pub trait Config: frame_system::Config {73 type Currency: Balanced<Self::AccountId>74 + Inspect<Self::AccountId>75 + Mutate<Self::AccountId>;76 type TreasuryAccountId: Get<Self::AccountId>;7778 79 type BlockNumberProvider: BlockNumberProvider<BlockNumber = Self::BlockNumber>;8081 82 #[pallet::constant]83 type InflationBlockInterval: Get<Self::BlockNumber>;84 }8586 #[pallet::pallet]87 pub struct Pallet<T>(_);8889 90 #[pallet::storage]91 pub type StartingYearTotalIssuance<T: Config> =92 StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9394 95 #[pallet::storage]96 pub type BlockInflation<T: Config> = StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9798 99 #[pallet::storage]100 pub type NextInflationBlock<T: Config> =101 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;102103 104 #[pallet::storage]105 pub type NextRecalculationBlock<T: Config> =106 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;107108 109 #[pallet::storage]110 pub type StartBlock<T: Config> = StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;111112 #[pallet::hooks]113 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {114 fn on_initialize(_: T::BlockNumber) -> Weight115 where116 <T as frame_system::Config>::BlockNumber: From<u32>,117 {118 let mut consumed_weight = Weight::zero();119 let mut add_weight = |reads, writes, weight| {120 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);121 consumed_weight += weight;122 };123124 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);125 let current_relay_block = T::BlockNumberProvider::current_block_number();126 let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();127 add_weight(1, 0, Weight::from_parts(5_000_000, 0));128129 130 131 if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) {132 133 134 135 let next_recalculation: T::BlockNumber = <NextRecalculationBlock<T>>::get();136 add_weight(1, 0, Weight::zero());137 if current_relay_block >= next_recalculation {138 Self::recalculate_inflation(next_recalculation);139 add_weight(0, 4, Weight::from_parts(5_000_000, 0));140 }141142 T::Currency::mint_into(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get())143 .ok();144145 146 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());147148 add_weight(3, 3, Weight::from_parts(10_000_000, 0));149 }150151 consumed_weight152 }153 }154155 #[pallet::call]156 impl<T: Config> Pallet<T> {157 158 159 160 161 162 163 164 165 166 167 168 #[pallet::call_index(0)]169 #[pallet::weight(0)]170 pub fn start_inflation(171 origin: OriginFor<T>,172 inflation_start_relay_block: T::BlockNumber,173 ) -> DispatchResult174 where175 <T as frame_system::Config>::BlockNumber: From<u32>,176 {177 ensure_root(origin)?;178179 180 if <StartBlock<T>>::get() == 0u32.into() {181 182 <StartBlock<T>>::set(inflation_start_relay_block);183184 185 Self::recalculate_inflation(inflation_start_relay_block);186 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);187 <NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());188189 190 let _ = T::Currency::deposit(191 &T::TreasuryAccountId::get(),192 <BlockInflation<T>>::get(),193 Precision::Exact,194 )?;195 }196197 Ok(())198 }199 }200}201202impl<T: Config> Pallet<T> {203 pub fn recalculate_inflation(recalculation_block: T::BlockNumber) {204 let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get())205 / T::BlockNumber::from(YEAR))206 .try_into()207 .unwrap_or(0);208 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);209210 let one_percent = Perbill::from_percent(1);211212 if current_year <= TOTAL_YEARS_UNTIL_FLAT {213 let amount: BalanceOf<T> = Perbill::from_rational(214 block_interval215 * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT216 - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),217 YEAR * TOTAL_YEARS_UNTIL_FLAT,218 ) * (one_percent * T::Currency::total_issuance());219 <BlockInflation<T>>::put(amount);220 } else {221 let amount: BalanceOf<T> =222 Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR)223 * (one_percent * T::Currency::total_issuance());224 <BlockInflation<T>>::put(amount);225 }226 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());227228 229 <NextRecalculationBlock<T>>::set(recalculation_block + YEAR.into());230 }231}