Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce pallet-parameters to Westend to parameterize inflation #4938

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
81da185
feat: setup pallet-parameters in westend runtime
marcuspang Jul 3, 2024
e84a472
feat: introduce dynamic params for staking reward params
marcuspang Jul 3, 2024
e8d2ec5
feat: add new era relay payout function
marcuspang Jul 4, 2024
77a28cc
feat: add new era payout fn with dynamic params
marcuspang Jul 4, 2024
b010d0b
fix: total stakable value + remove NIS
marcuspang Jul 5, 2024
30caa8c
chore: remove unused weight params
marcuspang Jul 5, 2024
08b9287
build: remove unused reward staking curve pallet
marcuspang Jul 5, 2024
282aa21
chore: remove unused era_payout function
marcuspang Jul 5, 2024
ff9e782
chore: leave deprecated function in crate
marcuspang Jul 5, 2024
90f031d
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 8, 2024
ae01d45
test: add unit test to ensure payout logic is same using legacy aucti…
marcuspang Jul 10, 2024
aa563f5
fix: add allow deprecated macro for tests
marcuspang Jul 10, 2024
ae0a72d
fix: missing default trait for runtimeparameters
marcuspang Jul 10, 2024
451f622
docs: add comments for era payout params
marcuspang Jul 10, 2024
849604c
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 10, 2024
e866cd9
doc: update stakable amount description
marcuspang Jul 11, 2024
833d209
refactor: move deprecated fn to test module
marcuspang Jul 11, 2024
2ea077f
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 17, 2024
8843d73
works
kianenigma Jul 17, 2024
704e13c
add weight file
kianenigma Jul 17, 2024
4884a48
fmt toml
kianenigma Jul 17, 2024
c7ee56c
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 17, 2024
ede367a
remove api
kianenigma Jul 17, 2024
fdeaacd
Merge branch 'feat/make-westend-inflation-parameterizable' of github.…
kianenigma Jul 17, 2024
c866a69
update lock
kianenigma Jul 17, 2024
7ce4449
fix
kianenigma Jul 17, 2024
e8037de
remove more unused stuff
kianenigma Jul 17, 2024
8d62d46
add prodoc
kianenigma Jul 18, 2024
810b2b0
Merge branch 'master' of github.com:paritytech/polkadot-sdk into feat…
kianenigma Jul 18, 2024
d8760c7
add release related stuff
kianenigma Jul 18, 2024
026dcb6
revert
kianenigma Jul 18, 2024
2e128ac
".git/.scripts/commands/fmt/fmt.sh"
Jul 18, 2024
8a6d11e
remove unused
kianenigma Jul 18, 2024
6ab2d43
Merge branch 'master' of github.com:paritytech/polkadot-sdk into feat…
kianenigma Jul 18, 2024
e993b59
Merge branch 'feat/make-westend-inflation-parameterizable' of github.…
kianenigma Jul 18, 2024
e520f23
Empty-Commit
kianenigma Jul 18, 2024
28c1c2d
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 18, 2024
98d7d91
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 19, 2024
4f6316a
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 19, 2024
b972461
Merge branch 'master' into feat/make-westend-inflation-parameterizable
kianenigma Jul 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 148 additions & 6 deletions polkadot/runtime/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,62 @@ where
}
}

pub struct EraPayoutParams {
/// Total staked amount.
pub total_staked: Balance,
/// Total stakable amount.
marcuspang marked this conversation as resolved.
Show resolved Hide resolved
pub total_stakable: Balance,
/// Ideal stake ratio, which is deducted by `legacy_auction_proportion` if not `None`.
pub ideal_stake: Perquintill,
/// Maximum inflation rate.
pub max_annual_inflation: Perquintill,
/// Minimum inflation rate.
pub min_annual_inflation: Perquintill,
/// Falloff used to calculate era payouts.
pub falloff: Perquintill,
/// Fraction of the era period used to calculate era payouts.
pub period_fraction: Perquintill,
/// Legacy auction proportion, which substracts from `ideal_stake` if not `None`.
pub legacy_auction_proportion: Option<Perquintill>,
}

pub fn relay_era_payout(params: EraPayoutParams) -> (Balance, Balance) {
use sp_runtime::traits::Saturating;

let EraPayoutParams {
total_staked,
total_stakable,
ideal_stake,
max_annual_inflation,
min_annual_inflation,
falloff,
period_fraction,
legacy_auction_proportion,
} = params;

let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);

let ideal_stake = ideal_stake.saturating_sub(legacy_auction_proportion.unwrap_or_default());

let stake = Perquintill::from_rational(total_staked, total_stakable);
let adjustment = pallet_staking_reward_fn::compute_inflation(stake, ideal_stake, falloff);
let staking_inflation =
min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);

let max_payout = period_fraction * max_annual_inflation * total_stakable;
let staking_payout = (period_fraction * staking_inflation) * total_stakable;
let rest = max_payout.saturating_sub(staking_payout);

let other_issuance = total_stakable.saturating_sub(total_staked);
if total_staked > other_issuance {
let _cap_rest = Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
// We don't do anything with this, but if we wanted to, we could introduce a cap on the
// treasury amount with: `rest = rest.min(cap_rest);`
}
(staking_payout, rest)
}

#[deprecated = "use relay_era_payout instead which supports dynamic parameters"]
marcuspang marked this conversation as resolved.
Show resolved Hide resolved
pub fn era_payout(
total_staked: Balance,
total_stakable: Balance,
Expand Down Expand Up @@ -425,13 +481,99 @@ mod tests {

#[test]
fn era_payout_should_give_sensible_results() {
assert_eq!(
era_payout(75, 100, Perquintill::from_percent(10), Perquintill::one(), 0,),
(10, 0)
#[allow(deprecated)]
let payout = era_payout(75, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
assert_eq!(payout, (10, 0));
#[allow(deprecated)]
let payout = era_payout(80, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
assert_eq!(payout, (6, 4));
}

#[test]
fn relay_era_payout_should_give_sensible_results() {
marcuspang marked this conversation as resolved.
Show resolved Hide resolved
let params = EraPayoutParams {
total_staked: 75,
total_stakable: 100,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation: Perquintill::from_percent(10),
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction: Perquintill::one(),
legacy_auction_proportion: None,
};
assert_eq!(relay_era_payout(params), (10, 0));

let params = EraPayoutParams {
total_staked: 80,
total_stakable: 100,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation: Perquintill::from_percent(10),
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction: Perquintill::one(),
legacy_auction_proportion: None,
};
assert_eq!(relay_era_payout(params), (6, 4));
}

#[test]
fn relay_era_payout_should_give_same_results_as_era_payout() {
let total_staked = 1_000_000;
let total_stakable = 2_000_000;
let max_annual_inflation = Perquintill::from_percent(10);
let period_fraction = Perquintill::from_percent(25);
let auctioned_slots = 30;

let params = EraPayoutParams {
total_staked,
total_stakable,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation,
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction,
legacy_auction_proportion: Some(Perquintill::from_rational(
auctioned_slots.min(60),
200u64,
)),
};

#[allow(deprecated)]
let payout = era_payout(
total_staked,
total_stakable,
max_annual_inflation,
period_fraction,
auctioned_slots,
);
assert_eq!(
era_payout(80, 100, Perquintill::from_percent(10), Perquintill::one(), 0,),
(6, 4)
assert_eq!(relay_era_payout(params), payout);

let total_staked = 1_900_000;
let total_stakable = 2_000_000;
let auctioned_slots = 60;

let params = EraPayoutParams {
total_staked,
total_stakable,
ideal_stake: Perquintill::from_percent(75),
max_annual_inflation,
min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
falloff: Perquintill::from_percent(5),
period_fraction,
legacy_auction_proportion: Some(Perquintill::from_rational(
auctioned_slots.min(60),
200u64,
)),
};
#[allow(deprecated)]
let payout = era_payout(
total_staked,
total_stakable,
max_annual_inflation,
period_fraction,
auctioned_slots,
);

assert_eq!(relay_era_payout(params), payout);
}
}
10 changes: 8 additions & 2 deletions polkadot/runtime/westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pallet-multisig = { workspace = true }
pallet-nomination-pools = { workspace = true }
pallet-conviction-voting = { workspace = true }
pallet-offences = { workspace = true }
pallet-parameters = { workspace = true }
pallet-preimage = { workspace = true }
pallet-proxy = { workspace = true }
pallet-recovery = { workspace = true }
Expand All @@ -80,7 +81,6 @@ pallet-scheduler = { workspace = true }
pallet-session = { workspace = true }
pallet-society = { workspace = true }
pallet-staking = { workspace = true }
pallet-staking-reward-curve = { workspace = true, default-features = true }
pallet-staking-runtime-api = { workspace = true }
pallet-delegated-staking = { workspace = true }
pallet-state-trie-migration = { workspace = true }
Expand Down Expand Up @@ -174,6 +174,7 @@ std = [
"pallet-nomination-pools/std",
"pallet-offences-benchmarking?/std",
"pallet-offences/std",
"pallet-parameters/std",
"pallet-preimage/std",
"pallet-proxy/std",
"pallet-recovery/std",
Expand Down Expand Up @@ -261,6 +262,7 @@ runtime-benchmarks = [
"pallet-nomination-pools/runtime-benchmarks",
"pallet-offences-benchmarking/runtime-benchmarks",
"pallet-offences/runtime-benchmarks",
"pallet-parameters/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-recovery/runtime-benchmarks",
Expand Down Expand Up @@ -319,6 +321,7 @@ try-runtime = [
"pallet-multisig/try-runtime",
"pallet-nomination-pools/try-runtime",
"pallet-offences/try-runtime",
"pallet-parameters/try-runtime",
"pallet-preimage/try-runtime",
"pallet-proxy/try-runtime",
"pallet-recovery/try-runtime",
Expand Down Expand Up @@ -348,7 +351,10 @@ metadata-hash = ["substrate-wasm-builder/metadata-hash"]
# Set timing constants (e.g. session period) to faster versions to speed up testing.
fast-runtime = []

runtime-metrics = ["polkadot-runtime-parachains/runtime-metrics", "sp-io/with-tracing"]
runtime-metrics = [
"polkadot-runtime-parachains/runtime-metrics",
"sp-io/with-tracing",
]

# A feature that should be enabled when the runtime should be built for on-chain
# deployment. This will disable stuff that shouldn't be part of the on-chain wasm
Expand Down
Loading
Loading