difftreelog
Fix inflation
in: master
2 files changed
pallets/inflation/src/lib.rsdiffbeforeafterboth363637use sp_runtime::{37use sp_runtime::{38 Perbill,38 Perbill,39 traits::{BlockNumberProvider, Zero},39 traits::{BlockNumberProvider},40};40};41use sp_std::convert::TryInto;41use sp_std::convert::TryInto;424246pub type BalanceOf<T> =46pub type BalanceOf<T> =47 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;47 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;484849// pub const YEAR: u32 = 5_259_600; // 6-second block50pub const YEAR: u32 = 2_629_800; // 12-second block49pub const YEAR: u32 = 5_259_600; // 6-second block50// pub const YEAR: u32 = 2_629_800; // 12-second block51pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;51pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;52pub const START_INFLATION_PERCENT: u32 = 10;52pub const START_INFLATION_PERCENT: u32 = 10;53pub const END_INFLATION_PERCENT: u32 = 4;53pub const END_INFLATION_PERCENT: u32 = 4;69 /// Current block inflation69 /// Current block inflation70 pub BlockInflation get(fn block_inflation): BalanceOf<T>;70 pub BlockInflation get(fn block_inflation): BalanceOf<T>;7172 /// Next (relay) block when inflation is applied. This value is approximate.73 pub NextInflationBlock get(fn next_inflation_block): T::BlockNumber;7475 /// Next (relay) block when inflation is recalculated. This value is approximate.76 pub NextRecalculationBlock get(fn next_recalculation_block): T::BlockNumber;71 }77 }72}78}737989 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);95 let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);90 let _now = T::BlockNumberProvider::current_block_number();96 let _now = T::BlockNumberProvider::current_block_number();9192 // Recalculate inflation on the first block of the year (or if it is not initialized yet)93 if (_now % T::BlockNumber::from(YEAR)).is_zero() || <BlockInflation<T>>::get().is_zero() {97 let next_recalculation: T::BlockNumber = <NextRecalculationBlock<T>>::get();98 let next_inflation: T::BlockNumber = <NextInflationBlock<T>>::get();99 add_weight(2, 0, 5_000_000);100101 // Recalculate inflation on the first block of the year (or if it is not initialized yet)102 if _now >= next_recalculation {94 let current_year: u32 = (_now / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);103 let current_year: u32 = (next_recalculation / T::BlockNumber::from(YEAR)).try_into().unwrap_or(0);9510496 let one_percent = Perbill::from_percent(1);105 let one_percent = Perbill::from_percent(1);97106114 // First time deposit123 // First time deposit115 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());124 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());125126 // Update recalculation and inflation blocks127 <NextRecalculationBlock<T>>::set(next_recalculation + YEAR.into());128 <NextInflationBlock<T>>::set(next_recalculation + block_interval.into() + YEAR.into());116129117 add_weight(7, 6, 28_300_000);130 add_weight(7, 8, 28_300_000);118 }131 }119132120 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account133 // Apply inflation every InflationBlockInterval blocks and in the 1st block to initialize Treasury account121 else if (_now % T::BlockNumber::from(block_interval)).is_zero() {134 else if _now >= next_inflation {122 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();135 T::Currency::deposit_into_existing(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get()).ok();136137 // Update inflation block138 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());123139124 add_weight(3, 2, 12_900_000);140 add_weight(3, 3, 12_900_000);125 }141 }126142127 consumed_weight143 consumed_weighttests/src/inflation.test.tsdiffbeforeafterboth--- a/tests/src/inflation.test.ts
+++ b/tests/src/inflation.test.ts
@@ -18,8 +18,8 @@
const totalIssuanceStart = (await api.query.inflation.startingYearTotalIssuance()).toBigInt();
const blockInflation = (await api.query.inflation.blockInflation()).toBigInt();
- // const YEAR = 5259600n; // 6-second block. Blocks in one year
- const YEAR = 2629800n; // 12-second block. Blocks in one year
+ const YEAR = 5259600n; // 6-second block. Blocks in one year
+ // const YEAR = 2629800n; // 12-second block. Blocks in one year
const totalExpectedInflation = totalIssuanceStart / 10n;
const totalActualInflation = blockInflation * YEAR / blockInterval;