Skip to content

Commit

Permalink
L1-289: Api inflation (#1824)
Browse files Browse the repository at this point in the history
Co-authored-by: lesniak43 <[email protected]>
Co-authored-by: Damian Leśniak <[email protected]>
Co-authored-by: Marcin <[email protected]>
  • Loading branch information
4 people authored Oct 8, 2024
1 parent 98cbe53 commit 739bf37
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
48 changes: 36 additions & 12 deletions bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,23 @@ parameter_types! {

pub struct ExponentialEraPayout;

impl ExponentialEraPayout {
fn era_payout(total_issuance: Balance, era_duration_millis: u64) -> (Balance, Balance) {
const VALIDATOR_REWARD: Perbill = Perbill::from_percent(90);

let azero_cap = pallet_aleph::AzeroCap::<Runtime>::get();
let horizon = pallet_aleph::ExponentialInflationHorizon::<Runtime>::get();

let total_payout: Balance =
exp_helper(Perbill::from_rational(era_duration_millis, horizon))
* (azero_cap.saturating_sub(total_issuance));
let validators_payout = VALIDATOR_REWARD * total_payout;
let rest = total_payout - validators_payout;

(validators_payout, rest)
}
}

/// Calculates 1 - exp(-x) for small positive x
fn exp_helper(x: Perbill) -> Perbill {
let x2 = x * x;
Expand All @@ -505,18 +522,7 @@ impl pallet_staking::EraPayout<Balance> for ExponentialEraPayout {
total_issuance: Balance,
era_duration_millis: u64,
) -> (Balance, Balance) {
const VALIDATOR_REWARD: Perbill = Perbill::from_percent(90);

let azero_cap = pallet_aleph::AzeroCap::<Runtime>::get();
let horizon = pallet_aleph::ExponentialInflationHorizon::<Runtime>::get();

let total_payout: Balance =
exp_helper(Perbill::from_rational(era_duration_millis, horizon))
* (azero_cap.saturating_sub(total_issuance));
let validators_payout = VALIDATOR_REWARD * total_payout;
let rest = total_payout - validators_payout;

(validators_payout, rest)
ExponentialEraPayout::era_payout(total_issuance, era_duration_millis)
}
}

Expand Down Expand Up @@ -1222,6 +1228,24 @@ impl_runtime_apis! {
fn key_owner(key: AlephId) -> Option<AccountId> {
Session::key_owner(primitives::KEY_TYPE, key.as_ref())
}

fn yearly_inflation() -> Perbill {
// Milliseconds per year for the Julian year (365.25 days).
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
let total_issuance = pallet_balances::Pallet::<Runtime>::total_issuance();

let (validator_payout, rest)
= ExponentialEraPayout::era_payout(total_issuance, MILLISECONDS_PER_YEAR);

Perbill::from_rational(validator_payout + rest, total_issuance)
}

fn current_era_payout() -> (Balance, Balance) {
const MILLISECONDS_PER_ERA: u64 = 1000 * 3600 * 24;
let total_issuance = pallet_balances::Pallet::<Runtime>::total_issuance();

ExponentialEraPayout::era_payout(total_issuance, MILLISECONDS_PER_ERA)
}
}

impl pallet_nomination_pools_runtime_api::NominationPoolsApi<Block, AccountId, Balance> for Runtime {
Expand Down
4 changes: 4 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ sp_api::decl_runtime_apis! {
/// also as `aleph_key` - consensus engine's part of session keys) in the current session
/// of AlephBFT (finalisation committee).
fn key_owner(key: AuthorityId) -> Option<AccountId>;
/// Returns inflation from now to now + 1 year. Capped at 100%
fn yearly_inflation() -> Perbill;
/// Returns payout. First tuple item is a validators payout, 2nd is the rest.
fn current_era_payout() -> (Balance, Balance);
}
}

Expand Down

0 comments on commit 739bf37

Please sign in to comment.