1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2024 BOTLabs GmbH

// The KILT Blockchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The KILT Blockchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at info@botlabs.org

//! Helper methods for computing issuance based on inflation
use crate::{pallet::Config, types::BalanceOf};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_runtime::{traits::Saturating, Perquintill, RuntimeDebug};

#[derive(Eq, PartialEq, Clone, Encode, Decode, Default, RuntimeDebug, TypeInfo, Deserialize, Serialize)]
pub struct RewardRate {
	pub annual: Perquintill,
	pub per_block: Perquintill,
}

impl MaxEncodedLen for RewardRate {
	fn max_encoded_len() -> usize {
		// Perquintill is at most u128
		u128::max_encoded_len().saturating_add(u128::max_encoded_len())
	}
}

/// Convert annual reward rate to per_block.
fn annual_to_per_block(blocks_per_year: u64, rate: Perquintill) -> Perquintill {
	rate / blocks_per_year.max(1)
}

impl RewardRate {
	pub fn new(blocks_per_year: u64, rate: Perquintill) -> Self {
		RewardRate {
			annual: rate,
			per_block: annual_to_per_block(blocks_per_year, rate),
		}
	}
}

/// Staking info (staking rate and reward rate) for collators and delegators.

#[derive(Eq, PartialEq, Clone, Encode, Decode, Default, RuntimeDebug, TypeInfo, Serialize, Deserialize)]
pub struct StakingInfo {
	/// Maximum staking rate.
	pub max_rate: Perquintill,
	/// Reward rate annually and per_block.
	pub reward_rate: RewardRate,
}

impl MaxEncodedLen for StakingInfo {
	fn max_encoded_len() -> usize {
		// Perquintill is at most u128
		RewardRate::max_encoded_len().saturating_add(u128::max_encoded_len())
	}
}

impl StakingInfo {
	pub fn new(blocks_per_year: u64, max_rate: Perquintill, annual_reward_rate: Perquintill) -> Self {
		StakingInfo {
			max_rate,
			reward_rate: RewardRate::new(blocks_per_year, annual_reward_rate),
		}
	}

	/// Calculate newly minted rewards on coinbase, e.g.,
	/// reward = rewards_per_block * staking_rate.
	///
	/// NOTE: If we exceed the max staking rate, the reward will be reduced by
	/// max_rate / current_rate.
	pub fn compute_reward<T: Config>(
		&self,
		stake: BalanceOf<T>,
		current_staking_rate: Perquintill,
		authors_per_round: BalanceOf<T>,
	) -> BalanceOf<T> {
		// Perquintill automatically bounds to [0, 100]% in case staking_rate is greater
		// than self.max_rate
		let reduction = Perquintill::from_rational(self.max_rate.deconstruct(), current_staking_rate.deconstruct());
		// multiplication with perbill cannot overflow
		let reward = (self.reward_rate.per_block * stake).saturating_mul(authors_per_round);
		reduction * reward
	}
}

#[derive(
	Eq, PartialEq, Clone, Encode, Decode, Default, RuntimeDebug, TypeInfo, MaxEncodedLen, Serialize, Deserialize,
)]
pub struct InflationInfo {
	pub collator: StakingInfo,
	pub delegator: StakingInfo,
}

impl InflationInfo {
	/// Create a new inflation info from the max staking rates and annual reward
	/// rates for collators and delegators.
	///
	/// Example: InflationInfo::new(Perquintill_from_percent(10), ...)
	pub fn new(
		blocks_per_year: u64,
		collator_max_rate_percentage: Perquintill,
		collator_annual_reward_rate_percentage: Perquintill,
		delegator_max_rate_percentage: Perquintill,
		delegator_annual_reward_rate_percentage: Perquintill,
	) -> Self {
		Self {
			collator: StakingInfo::new(
				blocks_per_year,
				collator_max_rate_percentage,
				collator_annual_reward_rate_percentage,
			),
			delegator: StakingInfo::new(
				blocks_per_year,
				delegator_max_rate_percentage,
				delegator_annual_reward_rate_percentage,
			),
		}
	}

	/// Check whether the annual reward rate is approx. the per_block reward
	/// rate multiplied with the number of blocks per year
	pub fn is_valid(&self, blocks_per_year: u64) -> bool {
		self.collator.reward_rate.annual
			>= Perquintill::from_parts(
				self.collator
					.reward_rate
					.per_block
					.deconstruct()
					.saturating_mul(blocks_per_year),
			) && self.delegator.reward_rate.annual
			>= Perquintill::from_parts(
				self.delegator
					.reward_rate
					.per_block
					.deconstruct()
					.saturating_mul(blocks_per_year),
			)
	}
}

#[cfg(test)]
mod tests {
	use sp_runtime::Perbill;

	use super::*;
	use crate::mock::{almost_equal, ExtBuilder, Test, DECIMALS, MAX_COLLATOR_STAKE};

	#[test]
	fn perquintill() {
		assert_eq!(
			Perquintill::from_percent(100) * Perquintill::from_percent(50),
			Perquintill::from_percent(50)
		);
	}

	#[test]
	fn annual_to_block_rate() {
		let rate = Perquintill::one();
		assert!(almost_equal(
			rate * 10_000_000_000u128,
			Perquintill::from_parts(
				annual_to_per_block(<Test as Config>::BLOCKS_PER_YEAR, rate).deconstruct()
					* <Test as Config>::BLOCKS_PER_YEAR
			) * 10_000_000_000u128,
			Perbill::from_perthousand(1)
		));
	}

	#[test]
	fn single_block_reward_collator() {
		let inflation = InflationInfo::new(
			<Test as Config>::BLOCKS_PER_YEAR,
			Perquintill::from_percent(10),
			Perquintill::from_percent(10),
			Perquintill::from_percent(40),
			Perquintill::from_percent(8),
		);
		let reward = inflation
			.collator
			.compute_reward::<Test>(MAX_COLLATOR_STAKE, Perquintill::from_percent(9), 2);
		let expected = <Test as Config>::CurrencyBalance::from(15210282150733u64);
		assert!(
			almost_equal(reward, expected, Perbill::from_perthousand(1)),
			"left {:?}, right {:?}",
			reward,
			expected
		);
	}

	#[test]
	fn simple_block_reward_check() {
		let precision = Perbill::from_perthousand(1);
		ExtBuilder::default()
			.with_inflation(10, 15, 40, 10, 5)
			.with_balances(vec![(1, 10)])
			.with_collators(vec![(1, 10)])
			.build()
			.execute_with(|| {
				let inflation = InflationInfo::new(
					<Test as Config>::BLOCKS_PER_YEAR,
					Perquintill::from_percent(10),
					Perquintill::from_percent(15),
					Perquintill::from_percent(40),
					Perquintill::from_percent(10),
				);
				let years_u128: BalanceOf<Test> = <Test as Config>::BLOCKS_PER_YEAR as u128;

				// Dummy checks for correct instantiation
				assert!(inflation.is_valid(<Test as Config>::BLOCKS_PER_YEAR));
				assert_eq!(inflation.collator.max_rate, Perquintill::from_percent(10));
				assert_eq!(inflation.collator.reward_rate.annual, Perquintill::from_percent(15));
				assert!(
					almost_equal(
						inflation.collator.reward_rate.per_block * DECIMALS * 10_000,
						Perquintill::from_percent(15) * 10_000 * DECIMALS / years_u128,
						precision
					),
					"left = {:?}, right = {:?}",
					inflation.collator.reward_rate.per_block * 10_000 * DECIMALS,
					Perquintill::from_percent(15) * 10_000 * DECIMALS / years_u128,
				);
				assert_eq!(inflation.delegator.max_rate, Perquintill::from_percent(40));
				assert_eq!(inflation.delegator.reward_rate.annual, Perquintill::from_percent(10));
				assert!(
					almost_equal(
						inflation.delegator.reward_rate.per_block * DECIMALS * 10_000,
						Perquintill::from_percent(10) * 10_000 * DECIMALS / years_u128,
						precision
					),
					"left = {:?}, right = {:?}",
					inflation.delegator.reward_rate.per_block * DECIMALS * 10_000,
					Perquintill::from_percent(10) * 10_000 * DECIMALS / years_u128,
				);

				// Check collator reward computation
				let authors_per_round = 1u128;
				let mut current_staking_rate: Perquintill = inflation.collator.max_rate;
				assert_eq!(
					inflation
						.collator
						.compute_reward::<Test>(0, current_staking_rate, authors_per_round),
					0
				);
				current_staking_rate = Perquintill::from_rational(5000u64, 100_000u64);
				assert!(
					almost_equal(
						inflation.collator.compute_reward::<Test>(
							5000 * DECIMALS,
							current_staking_rate,
							authors_per_round
						) * years_u128,
						Perquintill::from_percent(15) * 5000 * DECIMALS,
						Perbill::from_percent(1)
					),
					"left = {:?}, right = {:?}",
					inflation
						.collator
						.compute_reward::<Test>(5000 * DECIMALS, current_staking_rate, authors_per_round)
						* years_u128,
					Perquintill::from_percent(15) * 5000 * DECIMALS,
				);
				// Check for max_rate which is 10%
				current_staking_rate = Perquintill::from_rational(10_000u64, 100_000u64);
				assert!(
					almost_equal(
						inflation.collator.compute_reward::<Test>(
							10_000 * DECIMALS,
							current_staking_rate,
							authors_per_round
						) * years_u128,
						Perquintill::from_percent(15) * 10_000 * DECIMALS,
						Perbill::from_percent(1)
					),
					"left = {:?}, right = {:?}",
					inflation.collator.compute_reward::<Test>(
						10_000 * DECIMALS,
						current_staking_rate,
						authors_per_round
					) * years_u128,
					Perquintill::from_percent(15) * 10_000 * DECIMALS,
				);

				// Check for exceeding max_rate: 50% instead of 10%
				current_staking_rate = Perquintill::from_rational(50_000u64, 100_000u64);
				assert!(
					almost_equal(
						inflation.collator.compute_reward::<Test>(
							50_000 * DECIMALS,
							current_staking_rate,
							authors_per_round
						) * years_u128,
						Perquintill::from_percent(15) * 10_000 * DECIMALS,
						Perbill::from_percent(1)
					),
					"left = {:?}, right = {:?}",
					inflation.collator.compute_reward::<Test>(
						50_000 * DECIMALS,
						current_staking_rate,
						authors_per_round
					) * years_u128,
					Perquintill::from_percent(15) * 10_000 * DECIMALS,
				);

				// Check delegator reward computation
				current_staking_rate = inflation.delegator.max_rate;
				assert_eq!(
					inflation
						.delegator
						.compute_reward::<Test>(0, current_staking_rate, authors_per_round),
					0
				);
				current_staking_rate = Perquintill::from_rational(5000u64, 100_000u64);
				assert!(
					almost_equal(
						inflation.delegator.compute_reward::<Test>(
							5000 * DECIMALS,
							current_staking_rate,
							authors_per_round
						) * years_u128,
						Perquintill::from_percent(10) * 5000 * DECIMALS,
						Perbill::from_percent(1)
					),
					"left = {:?}, right = {:?}",
					inflation.delegator.compute_reward::<Test>(
						5000 * DECIMALS,
						current_staking_rate,
						authors_per_round
					) * years_u128,
					Perquintill::from_percent(10) * 5000 * DECIMALS,
				);
				// Check for max_rate which is 40%
				current_staking_rate = Perquintill::from_rational(40_000u64, 100_000u64);
				assert!(
					almost_equal(
						inflation.delegator.compute_reward::<Test>(
							40_000 * DECIMALS,
							current_staking_rate,
							authors_per_round
						) * years_u128,
						Perquintill::from_percent(10) * 40_000 * DECIMALS,
						Perbill::from_percent(1)
					),
					"left = {:?}, right = {:?}",
					inflation.delegator.compute_reward::<Test>(
						40_000 * DECIMALS,
						current_staking_rate,
						authors_per_round
					) * years_u128,
					Perquintill::from_percent(10) * 40_000 * DECIMALS,
				);

				// Check for exceeding max_rate: 50% instead of 40%
				current_staking_rate = Perquintill::from_rational(50_000u64, 100_000u64);
				assert!(
					almost_equal(
						inflation.delegator.compute_reward::<Test>(
							50_000 * DECIMALS,
							current_staking_rate,
							authors_per_round
						) * years_u128,
						Perquintill::from_percent(8) * 50_000 * DECIMALS,
						Perbill::from_percent(1)
					),
					"left = {:?}, right = {:?}",
					inflation.delegator.compute_reward::<Test>(
						50_000 * DECIMALS,
						current_staking_rate,
						authors_per_round
					) * years_u128,
					Perquintill::from_percent(8) * 50_000 * DECIMALS,
				);
			});
	}
}