git.delta.rocks / unique-network / refs/commits / c7478c5054c7

difftreelog

Fix inflation

Greg Zaitsev2021-12-03parent: #94b124f.patch.diff
in: master

2 files changed

modifiedpallets/inflation/src/lib.rsdiffbeforeafterboth
3636
37use 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;
4242
46pub 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;
4848
49// pub const YEAR: u32 = 5_259_600; // 6-second block
50pub const YEAR: u32 = 2_629_800; // 12-second block49pub const YEAR: u32 = 5_259_600; // 6-second block
50// pub const YEAR: u32 = 2_629_800; // 12-second block
51pub 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 inflation
70 pub BlockInflation get(fn block_inflation): BalanceOf<T>;70 pub BlockInflation get(fn block_inflation): BalanceOf<T>;
71
72 /// Next (relay) block when inflation is applied. This value is approximate.
73 pub NextInflationBlock get(fn next_inflation_block): T::BlockNumber;
74
75 /// 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}
7379
89 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();
91
92 // 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);
100
101 // 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);
95104
96 let one_percent = Perbill::from_percent(1);105 let one_percent = Perbill::from_percent(1);
97106
114 // First time deposit123 // First time deposit
115 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());124 T::Currency::deposit_creating(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get());
125
126 // Update recalculation and inflation blocks
127 <NextRecalculationBlock<T>>::set(next_recalculation + YEAR.into());
128 <NextInflationBlock<T>>::set(next_recalculation + block_interval.into() + YEAR.into());
116129
117 add_weight(7, 6, 28_300_000);130 add_weight(7, 8, 28_300_000);
118 }131 }
119132
120 // 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 account
121 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();
136
137 // Update inflation block
138 <NextInflationBlock<T>>::set(next_inflation + block_interval.into());
123139
124 add_weight(3, 2, 12_900_000);140 add_weight(3, 3, 12_900_000);
125 }141 }
126142
127 consumed_weight143 consumed_weight
modifiedtests/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;