1234567891011121314151617181920212223242526272829303132#![cfg_attr(not(feature = "std"), no_std)]3334#[cfg(feature = "runtime-benchmarks")]35mod benchmarking;3637#[cfg(test)]38mod tests;3940use frame_support::traits::{41 fungible::{Balanced, Inspect, Mutate},42 tokens::Precision,43 Get,44};45use frame_system::pallet_prelude::BlockNumberFor;46pub use pallet::*;47use sp_runtime::{traits::BlockNumberProvider, Perbill};48use sp_std::convert::TryInto;4950type BalanceOf<T> =51 <<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;5253pub const YEAR: u32 = 5_259_600; 54 55pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;56pub const START_INFLATION_PERCENT: u32 = 10;57pub const END_INFLATION_PERCENT: u32 = 4;5859#[frame_support::pallet]60pub mod pallet {61 use frame_support::pallet_prelude::*;62 use frame_system::pallet_prelude::*;6364 use super::*;6566 #[pallet::config]67 pub trait Config: frame_system::Config {68 type Currency: Balanced<Self::AccountId>69 + Inspect<Self::AccountId>70 + Mutate<Self::AccountId>;71 type TreasuryAccountId: Get<Self::AccountId>;7273 74 type OnInitializeBlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;7576 77 #[pallet::constant]78 type InflationBlockInterval: Get<BlockNumberFor<Self>>;79 }8081 #[pallet::pallet]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 = BlockNumberFor<T>, QueryKind = ValueQuery>;9798 99 #[pallet::storage]100 pub type NextRecalculationBlock<T: Config> =101 StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;102103 104 #[pallet::storage]105 pub type StartBlock<T: Config> =106 StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;107108 #[pallet::hooks]109 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {110 fn on_initialize(_: BlockNumberFor<T>) -> Weight111 where112 BlockNumberFor<T>: From<u32>,113 {114 let mut consumed_weight = Weight::zero();115 let mut add_weight = |reads, writes, weight| {116 consumed_weight += T::DbWeight::get().reads_writes(reads, writes);117 consumed_weight += weight;118 };119120 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);121 let current_relay_block = T::OnInitializeBlockNumberProvider::current_block_number();122 let next_inflation: BlockNumberFor<T> = <NextInflationBlock<T>>::get();123 add_weight(1, 0, Weight::from_parts(5_000_000, 0));124125 126 127 if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) {128 129 130 131 let next_recalculation: BlockNumberFor<T> = <NextRecalculationBlock<T>>::get();132 add_weight(1, 0, Weight::zero());133 if current_relay_block >= next_recalculation {134 Self::recalculate_inflation(next_recalculation);135 add_weight(0, 4, Weight::from_parts(5_000_000, 0));136 }137138 T::Currency::mint_into(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get())139 .ok();140141 142 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());143144 add_weight(3, 3, Weight::from_parts(10_000_000, 0));145 }146147 consumed_weight148 }149 }150151 #[pallet::call]152 impl<T: Config> Pallet<T> {153 154 155 156 157 158 159 160 161 162 163 164 #[pallet::call_index(0)]165 166 167 168 #[pallet::weight(Weight::from_parts(0, 0))]169 pub fn start_inflation(170 origin: OriginFor<T>,171 inflation_start_relay_block: BlockNumberFor<T>,172 ) -> DispatchResult173 where174 BlockNumberFor<T>: From<u32>,175 {176 ensure_root(origin)?;177178 179 if <StartBlock<T>>::get() == 0u32.into() {180 181 <StartBlock<T>>::set(inflation_start_relay_block);182183 184 Self::recalculate_inflation(inflation_start_relay_block);185 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);186 <NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());187188 189 let _ = T::Currency::deposit(190 &T::TreasuryAccountId::get(),191 <BlockInflation<T>>::get(),192 Precision::Exact,193 )?;194 }195196 Ok(())197 }198 }199}200201impl<T: Config> Pallet<T> {202 pub fn recalculate_inflation(recalculation_block: BlockNumberFor<T>) {203 let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get())204 / BlockNumberFor::<T>::from(YEAR))205 .try_into()206 .unwrap_or(0);207 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);208209 let one_percent = Perbill::from_percent(1);210211 if current_year <= TOTAL_YEARS_UNTIL_FLAT {212 let amount: BalanceOf<T> = Perbill::from_rational(213 block_interval214 * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT215 - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),216 YEAR * TOTAL_YEARS_UNTIL_FLAT,217 ) * (one_percent * T::Currency::total_issuance());218 <BlockInflation<T>>::put(amount);219 } else {220 let amount: BalanceOf<T> =221 Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR)222 * (one_percent * T::Currency::total_issuance());223 <BlockInflation<T>>::put(amount);224 }225 <StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());226227 228 <NextRecalculationBlock<T>>::set(recalculation_block + YEAR.into());229 }230}