referrerpolicy=no-referrer-when-downgrade

pallet_staking/
inflation.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! This module expose one function `P_NPoS` (Payout NPoS) or `compute_total_payout` which returns
19//! the total payout for the era given the era duration and the staking rate in NPoS.
20//! The staking rate in NPoS is the total amount of tokens staked by nominators and validators,
21//! divided by the total token supply.
22
23use sp_runtime::{curve::PiecewiseLinear, traits::AtLeast32BitUnsigned, Perbill};
24
25/// The total payout to all validators (and their nominators) per era and maximum payout.
26///
27/// Defined as such:
28/// `staker-payout = yearly_inflation(npos_token_staked / total_tokens) * total_tokens /
29/// era_per_year` `maximum-payout = max_yearly_inflation * total_tokens / era_per_year`
30///
31/// `era_duration` is expressed in millisecond.
32pub fn compute_total_payout<N>(
33	yearly_inflation: &PiecewiseLinear<'static>,
34	npos_token_staked: N,
35	total_tokens: N,
36	era_duration: u64,
37) -> (N, N)
38where
39	N: AtLeast32BitUnsigned + Clone,
40{
41	// Milliseconds per year for the Julian year (365.25 days).
42	const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
43
44	let portion = Perbill::from_rational(era_duration as u64, MILLISECONDS_PER_YEAR);
45	let payout = portion *
46		yearly_inflation
47			.calculate_for_fraction_times_denominator(npos_token_staked, total_tokens.clone());
48	let maximum = portion * (yearly_inflation.maximum * total_tokens);
49	(payout, maximum)
50}
51
52#[cfg(test)]
53mod test {
54	use sp_runtime::curve::PiecewiseLinear;
55
56	pallet_staking_reward_curve::build! {
57		const I_NPOS: PiecewiseLinear<'static> = curve!(
58			min_inflation: 0_025_000,
59			max_inflation: 0_100_000,
60			ideal_stake: 0_500_000,
61			falloff: 0_050_000,
62			max_piece_count: 40,
63			test_precision: 0_005_000,
64		);
65	}
66
67	#[test]
68	fn npos_curve_is_sensible() {
69		const YEAR: u64 = 365 * 24 * 60 * 60 * 1000;
70
71		// check maximum inflation.
72		// not 10_000 due to rounding error.
73		assert_eq!(super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).1, 9_993);
74
75		// super::I_NPOS.calculate_for_fraction_times_denominator(25, 100)
76		assert_eq!(super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).0, 2_498);
77		assert_eq!(super::compute_total_payout(&I_NPOS, 5_000, 100_000u64, YEAR).0, 3_248);
78		assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, YEAR).0, 6_246);
79		assert_eq!(super::compute_total_payout(&I_NPOS, 40_000, 100_000u64, YEAR).0, 8_494);
80		assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, YEAR).0, 9_993);
81		assert_eq!(super::compute_total_payout(&I_NPOS, 60_000, 100_000u64, YEAR).0, 4_379);
82		assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, YEAR).0, 2_733);
83		assert_eq!(super::compute_total_payout(&I_NPOS, 95_000, 100_000u64, YEAR).0, 2_513);
84		assert_eq!(super::compute_total_payout(&I_NPOS, 100_000, 100_000u64, YEAR).0, 2_505);
85
86		const DAY: u64 = 24 * 60 * 60 * 1000;
87		assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, DAY).0, 17);
88		assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, DAY).0, 27);
89		assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, DAY).0, 7);
90
91		const SIX_HOURS: u64 = 6 * 60 * 60 * 1000;
92		assert_eq!(super::compute_total_payout(&I_NPOS, 25_000, 100_000u64, SIX_HOURS).0, 4);
93		assert_eq!(super::compute_total_payout(&I_NPOS, 50_000, 100_000u64, SIX_HOURS).0, 7);
94		assert_eq!(super::compute_total_payout(&I_NPOS, 75_000, 100_000u64, SIX_HOURS).0, 2);
95
96		const HOUR: u64 = 60 * 60 * 1000;
97		assert_eq!(
98			super::compute_total_payout(
99				&I_NPOS,
100				2_500_000_000_000_000_000_000_000_000u128,
101				5_000_000_000_000_000_000_000_000_000u128,
102				HOUR
103			)
104			.0,
105			57_038_500_000_000_000_000_000
106		);
107	}
108}