Skip to content

Commit 739bf37

Browse files
kostekIVlesniak43Damian LeśniakMarcin-Radecki
authored
L1-289: Api inflation (#1824)
Co-authored-by: lesniak43 <lesniak43@gmail.com> Co-authored-by: Damian Leśniak <damian.lesniak@cardinals.cc> Co-authored-by: Marcin <marcin.radecki@cardinals.cc>
1 parent 98cbe53 commit 739bf37

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

bin/runtime/src/lib.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,23 @@ parameter_types! {
490490

491491
pub struct ExponentialEraPayout;
492492

493+
impl ExponentialEraPayout {
494+
fn era_payout(total_issuance: Balance, era_duration_millis: u64) -> (Balance, Balance) {
495+
const VALIDATOR_REWARD: Perbill = Perbill::from_percent(90);
496+
497+
let azero_cap = pallet_aleph::AzeroCap::<Runtime>::get();
498+
let horizon = pallet_aleph::ExponentialInflationHorizon::<Runtime>::get();
499+
500+
let total_payout: Balance =
501+
exp_helper(Perbill::from_rational(era_duration_millis, horizon))
502+
* (azero_cap.saturating_sub(total_issuance));
503+
let validators_payout = VALIDATOR_REWARD * total_payout;
504+
let rest = total_payout - validators_payout;
505+
506+
(validators_payout, rest)
507+
}
508+
}
509+
493510
/// Calculates 1 - exp(-x) for small positive x
494511
fn exp_helper(x: Perbill) -> Perbill {
495512
let x2 = x * x;
@@ -505,18 +522,7 @@ impl pallet_staking::EraPayout<Balance> for ExponentialEraPayout {
505522
total_issuance: Balance,
506523
era_duration_millis: u64,
507524
) -> (Balance, Balance) {
508-
const VALIDATOR_REWARD: Perbill = Perbill::from_percent(90);
509-
510-
let azero_cap = pallet_aleph::AzeroCap::<Runtime>::get();
511-
let horizon = pallet_aleph::ExponentialInflationHorizon::<Runtime>::get();
512-
513-
let total_payout: Balance =
514-
exp_helper(Perbill::from_rational(era_duration_millis, horizon))
515-
* (azero_cap.saturating_sub(total_issuance));
516-
let validators_payout = VALIDATOR_REWARD * total_payout;
517-
let rest = total_payout - validators_payout;
518-
519-
(validators_payout, rest)
525+
ExponentialEraPayout::era_payout(total_issuance, era_duration_millis)
520526
}
521527
}
522528

@@ -1222,6 +1228,24 @@ impl_runtime_apis! {
12221228
fn key_owner(key: AlephId) -> Option<AccountId> {
12231229
Session::key_owner(primitives::KEY_TYPE, key.as_ref())
12241230
}
1231+
1232+
fn yearly_inflation() -> Perbill {
1233+
// Milliseconds per year for the Julian year (365.25 days).
1234+
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
1235+
let total_issuance = pallet_balances::Pallet::<Runtime>::total_issuance();
1236+
1237+
let (validator_payout, rest)
1238+
= ExponentialEraPayout::era_payout(total_issuance, MILLISECONDS_PER_YEAR);
1239+
1240+
Perbill::from_rational(validator_payout + rest, total_issuance)
1241+
}
1242+
1243+
fn current_era_payout() -> (Balance, Balance) {
1244+
const MILLISECONDS_PER_ERA: u64 = 1000 * 3600 * 24;
1245+
let total_issuance = pallet_balances::Pallet::<Runtime>::total_issuance();
1246+
1247+
ExponentialEraPayout::era_payout(total_issuance, MILLISECONDS_PER_ERA)
1248+
}
12251249
}
12261250

12271251
impl pallet_nomination_pools_runtime_api::NominationPoolsApi<Block, AccountId, Balance> for Runtime {

primitives/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ sp_api::decl_runtime_apis! {
288288
/// also as `aleph_key` - consensus engine's part of session keys) in the current session
289289
/// of AlephBFT (finalisation committee).
290290
fn key_owner(key: AuthorityId) -> Option<AccountId>;
291+
/// Returns inflation from now to now + 1 year. Capped at 100%
292+
fn yearly_inflation() -> Perbill;
293+
/// Returns payout. First tuple item is a validators payout, 2nd is the rest.
294+
fn current_era_payout() -> (Balance, Balance);
291295
}
292296
}
293297

0 commit comments

Comments
 (0)