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 pub struct Pallet<T>(_);8283 84 #[pallet::storage]85 pub type StartingYearTotalIssuance<T: Config> =86 StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;8788 89 #[pallet::storage]90 pub type BlockInflation<T: Config> = StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9192 93 #[pallet::storage]94 pub type NextInflationBlock<T: Config> =95 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;9697 98 #[pallet::storage]99 pub type NextRecalculationBlock<T: Config> =100 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;101102 103 #[pallet::storage]104 pub type StartBlock<T: Config> = StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;105106 #[pallet::hooks]107 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {108 fn on_initialize(_: T::BlockNumber) -> Weight109 where110 <T as frame_system::Config>::BlockNumber: From<u32>,111 {112 let mut consumed_weight = Weight::zero();113 let mut add_weight = |reads, writes, weight| {114 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);115 consumed_weight += weight;116 };117118 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);119 let current_relay_block = T::BlockNumberProvider::current_block_number();120 let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();121 add_weight(1, 0, Weight::from_parts(5_000_000, 0));122123 124 125 if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) {126 127 128 129 let next_recalculation: T::BlockNumber = <NextRecalculationBlock<T>>::get();130 add_weight(1, 0, Weight::zero());131 if current_relay_block >= next_recalculation {132 Self::recalculate_inflation(next_recalculation);133 add_weight(0, 4, Weight::from_parts(5_000_000, 0));134 }135136 T::Currency::deposit_into_existing(137 &T::TreasuryAccountId::get(),138 <BlockInflation<T>>::get(),139 )140 .ok();141142 143 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());144145 add_weight(3, 3, Weight::from_parts(10_000_000, 0));146 }147148 consumed_weight149 }150 }151152 #[pallet::call]153 impl<T: Config> Pallet<T> {154 155 156 157 158 159 160 161 162 163 164 165 #[pallet::call_index(0)]166 #[pallet::weight(0)]167 pub fn start_inflation(168 origin: OriginFor<T>,169 inflation_start_relay_block: T::BlockNumber,170 ) -> DispatchResult171 where172 <T as frame_system::Config>::BlockNumber: From<u32>,173 {174 ensure_root(origin)?;175176 177 if <StartBlock<T>>::get() == 0u32.into() {178 179 <StartBlock<T>>::set(inflation_start_relay_block);180181 182 Self::recalculate_inflation(inflation_start_relay_block);183 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);184 <NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());185186 187 T::Currency::deposit_creating(188 &T::TreasuryAccountId::get(),189 <BlockInflation<T>>::get(),190 );191 }192193 Ok(())194 }195 }196}197198impl<T: Config> Pallet<T> {199 pub fn recalculate_inflation(recalculation_block: T::BlockNumber) {200 let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get())201 / T::BlockNumber::from(YEAR))202 .try_into()203 .unwrap_or(0);204 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);205206 let one_percent = Perbill::from_percent(1);207208 if current_year <= TOTAL_YEARS_UNTIL_FLAT {209 let amount: BalanceOf<T> = Perbill::from_rational(210 block_interval211 * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT212 - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),213 YEAR * TOTAL_YEARS_UNTIL_FLAT,214 ) * (one_percent * T::Currency::total_issuance());215 <BlockInflation<T>>::put(amount);216 } else {217 let amount: BalanceOf<T> =218 Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR)219 * (one_percent * T::Currency::total_issuance());220 <BlockInflation<T>>::put(amount);221 }222 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());223224 225 <NextRecalculationBlock<T>>::set(recalculation_block + YEAR.into());226 }227}