pallet_staking/
inflation.rs1use sp_runtime::{curve::PiecewiseLinear, traits::AtLeast32BitUnsigned, Perbill};
24
25pub 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 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 assert_eq!(super::compute_total_payout(&I_NPOS, 0, 100_000u64, YEAR).1, 9_993);
74
75 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}