From 4a7e624531781400e70a3b7505f379e347c7da83 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Tue, 07 Dec 2021 14:37:41 +0000 Subject: [PATCH] Format inflation --- --- a/pallets/inflation/src/lib.rs +++ b/pallets/inflation/src/lib.rs @@ -19,7 +19,7 @@ pub use pallet::*; use sp_runtime::{ Perbill, - traits::{BlockNumberProvider} + traits::{BlockNumberProvider}, }; use sp_std::convert::TryInto; @@ -28,7 +28,7 @@ <::Currency as Currency<::AccountId>>::Balance; pub const YEAR: u32 = 5_259_600; // 6-second block -// pub const YEAR: u32 = 2_629_800; // 12-second block + // pub const YEAR: u32 = 2_629_800; // 12-second block pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9; pub const START_INFLATION_PERCENT: u32 = 10; pub const END_INFLATION_PERCENT: u32 = 4; @@ -43,7 +43,7 @@ pub trait Config: frame_system::Config { type Currency: Currency; type TreasuryAccountId: Get; - + // The block number provider type BlockNumberProvider: BlockNumberProvider; @@ -58,7 +58,8 @@ /// starting year total issuance #[pallet::storage] - pub type StartingYearTotalIssuance = StorageValue, QueryKind = ValueQuery>; + pub type StartingYearTotalIssuance = + StorageValue, QueryKind = ValueQuery>; /// Current inflation for `InflationBlockInterval` number of blocks #[pallet::storage] @@ -66,16 +67,19 @@ /// Next target (relay) block when inflation will be applied #[pallet::storage] - pub type NextInflationBlock = StorageValue; + pub type NextInflationBlock = + StorageValue; /// Next target (relay) block when inflation is recalculated #[pallet::storage] - pub type NextRecalculationBlock = StorageValue; + pub type NextRecalculationBlock = + StorageValue; #[pallet::hooks] impl Hooks> for Pallet { fn on_initialize(_: T::BlockNumber) -> Weight - where ::BlockNumber: From + where + ::BlockNumber: From, { let mut consumed_weight = 0; let mut add_weight = |reads, writes, weight| { @@ -91,9 +95,8 @@ // Apply inflation every InflationBlockInterval blocks // If next_inflation == 0, this means inflation wasn't yet initialized if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) { - // Recalculate inflation on the first block of the year (or if it is not initialized yet) - // Do the "current_relay_block >= next_recalculation" check in the "current_relay_block >= next_inflation" + // Do the "current_relay_block >= next_recalculation" check in the "current_relay_block >= next_inflation" // block because it saves InflationBlockInterval DB reads for NextRecalculationBlock. let next_recalculation: T::BlockNumber = >::get(); add_weight(1, 0, 0); @@ -102,7 +105,11 @@ add_weight(0, 4, 5_000_000); } - T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), >::get()).ok(); + T::Currency::deposit_into_existing( + &T::TreasuryAccountId::get(), + >::get(), + ) + .ok(); // Update inflation block >::set(next_inflation + block_interval.into()); @@ -117,7 +124,7 @@ #[pallet::call] impl Pallet { /// This method sets the inflation start date. Can be only called once. - /// Inflation start block can be backdated and will catch up. The method will create Treasury + /// Inflation start block can be backdated and will catch up. The method will create Treasury /// account if it does not exist and perform the first inflation deposit. /// /// # Permissions @@ -128,8 +135,13 @@ /// /// * inflation_start_relay_block: The relay chain block at which inflation should start #[pallet::weight(0)] - pub fn start_inflation(origin: OriginFor, inflation_start_relay_block: T::BlockNumber) -> DispatchResult - where ::BlockNumber: From { + pub fn start_inflation( + origin: OriginFor, + inflation_start_relay_block: T::BlockNumber, + ) -> DispatchResult + where + ::BlockNumber: From, + { ensure_root(origin)?; // Start inflation if it has not been yet initialized @@ -141,7 +153,10 @@ >::set(inflation_start_relay_block + block_interval.into()); // First time deposit - create Treasury account so that we can call deposit_into_existing everywhere else - T::Currency::deposit_creating(&T::TreasuryAccountId::get(), >::get()); + T::Currency::deposit_creating( + &T::TreasuryAccountId::get(), + >::get(), + ); } Ok(()) @@ -151,29 +166,30 @@ impl Pallet { pub fn recalculate_inflation(recalculation_block: T::BlockNumber) { - - let current_year: u32 = (recalculation_block / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0); + let current_year: u32 = (recalculation_block / T::BlockNumber::from(YEAR)) + .try_into() + .unwrap_or(0); let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0); let one_percent = Perbill::from_percent(1); if current_year <= TOTAL_YEARS_UNTIL_FLAT { let amount: BalanceOf = Perbill::from_rational( - block_interval * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)), - YEAR * TOTAL_YEARS_UNTIL_FLAT - ) * ( one_percent * T::Currency::total_issuance() ); - >::put(amount); - } - else { - let amount: BalanceOf = Perbill::from_rational( - block_interval * END_INFLATION_PERCENT, - YEAR + block_interval + * (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT + - current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)), + YEAR * TOTAL_YEARS_UNTIL_FLAT, ) * (one_percent * T::Currency::total_issuance()); >::put(amount); + } else { + let amount: BalanceOf = + Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR) + * (one_percent * T::Currency::total_issuance()); + >::put(amount); } >::set(T::Currency::total_issuance()); // Update recalculation and inflation blocks >::set(recalculation_block + YEAR.into()); } -} \ No newline at end of file +} -- gitstuff