1use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
20use frame_support::traits::{
21 fungible::{Balanced, Credit},
22 tokens::imbalance::ResolveTo,
23 Contains, ContainsPair, Imbalance, OnUnbalanced,
24};
25use pallet_treasury::TreasuryAccountId;
26use polkadot_primitives::Balance;
27use sp_runtime::{traits::TryConvert, Perquintill, RuntimeDebug};
28use xcm::VersionedLocation;
29
30pub struct ToAuthor<R>(core::marker::PhantomData<R>);
32impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for ToAuthor<R>
33where
34 R: pallet_balances::Config + pallet_authorship::Config,
35 <R as frame_system::Config>::AccountId: From<polkadot_primitives::AccountId>,
36 <R as frame_system::Config>::AccountId: Into<polkadot_primitives::AccountId>,
37{
38 fn on_nonzero_unbalanced(
39 amount: Credit<<R as frame_system::Config>::AccountId, pallet_balances::Pallet<R>>,
40 ) {
41 if let Some(author) = <pallet_authorship::Pallet<R>>::author() {
42 let _ = <pallet_balances::Pallet<R>>::resolve(&author, amount);
43 }
44 }
45}
46
47pub struct DealWithFees<R>(core::marker::PhantomData<R>);
48impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for DealWithFees<R>
49where
50 R: pallet_balances::Config + pallet_authorship::Config + pallet_treasury::Config,
51 <R as frame_system::Config>::AccountId: From<polkadot_primitives::AccountId>,
52 <R as frame_system::Config>::AccountId: Into<polkadot_primitives::AccountId>,
53{
54 fn on_unbalanceds(
55 mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
56 ) {
57 if let Some(fees) = fees_then_tips.next() {
58 let mut split = fees.ration(80, 20);
60 if let Some(tips) = fees_then_tips.next() {
61 tips.merge_into(&mut split.1);
63 }
64 ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(split.0);
65 <ToAuthor<R> as OnUnbalanced<_>>::on_unbalanced(split.1);
66 }
67 }
68}
69
70pub struct EraPayoutParams {
72 pub total_staked: Balance,
74 pub total_stakable: Balance,
79 pub ideal_stake: Perquintill,
81 pub max_annual_inflation: Perquintill,
83 pub min_annual_inflation: Perquintill,
85 pub falloff: Perquintill,
87 pub period_fraction: Perquintill,
89 pub legacy_auction_proportion: Option<Perquintill>,
91}
92
93pub fn relay_era_payout(params: EraPayoutParams) -> (Balance, Balance) {
96 use sp_runtime::traits::Saturating;
97
98 let EraPayoutParams {
99 total_staked,
100 total_stakable,
101 ideal_stake,
102 max_annual_inflation,
103 min_annual_inflation,
104 falloff,
105 period_fraction,
106 legacy_auction_proportion,
107 } = params;
108
109 let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);
110
111 let ideal_stake = ideal_stake.saturating_sub(legacy_auction_proportion.unwrap_or_default());
112
113 let stake = Perquintill::from_rational(total_staked, total_stakable);
114 let adjustment = pallet_staking_reward_fn::compute_inflation(stake, ideal_stake, falloff);
115 let staking_inflation =
116 min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);
117
118 let max_payout = period_fraction * max_annual_inflation * total_stakable;
119 let staking_payout = (period_fraction * staking_inflation) * total_stakable;
120 let rest = max_payout.saturating_sub(staking_payout);
121
122 let other_issuance = total_stakable.saturating_sub(total_staked);
123 if total_staked > other_issuance {
124 let _cap_rest = Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
125 }
128 (staking_payout, rest)
129}
130
131#[derive(
134 Encode,
135 Decode,
136 DecodeWithMemTracking,
137 Eq,
138 PartialEq,
139 Clone,
140 RuntimeDebug,
141 scale_info::TypeInfo,
142 MaxEncodedLen,
143)]
144pub enum VersionedLocatableAsset {
145 #[codec(index = 3)]
146 V3 { location: xcm::v3::Location, asset_id: xcm::v3::AssetId },
147 #[codec(index = 4)]
148 V4 { location: xcm::v4::Location, asset_id: xcm::v4::AssetId },
149 #[codec(index = 5)]
150 V5 { location: xcm::v5::Location, asset_id: xcm::v5::AssetId },
151}
152
153impl From<(xcm::latest::Location, xcm::latest::AssetId)> for VersionedLocatableAsset {
155 fn from(value: (xcm::latest::Location, xcm::latest::AssetId)) -> Self {
156 VersionedLocatableAsset::V5 { location: value.0, asset_id: value.1 }
157 }
158}
159
160pub struct LocatableAssetConverter;
162impl TryConvert<VersionedLocatableAsset, xcm_builder::LocatableAssetId>
163 for LocatableAssetConverter
164{
165 fn try_convert(
166 asset: VersionedLocatableAsset,
167 ) -> Result<xcm_builder::LocatableAssetId, VersionedLocatableAsset> {
168 match asset {
169 VersionedLocatableAsset::V3 { location, asset_id } => {
170 let v4_location: xcm::v4::Location =
171 location.try_into().map_err(|_| asset.clone())?;
172 let v4_asset_id: xcm::v4::AssetId =
173 asset_id.try_into().map_err(|_| asset.clone())?;
174 Ok(xcm_builder::LocatableAssetId {
175 location: v4_location.try_into().map_err(|_| asset.clone())?,
176 asset_id: v4_asset_id.try_into().map_err(|_| asset.clone())?,
177 })
178 },
179 VersionedLocatableAsset::V4 { ref location, ref asset_id } =>
180 Ok(xcm_builder::LocatableAssetId {
181 location: location.clone().try_into().map_err(|_| asset.clone())?,
182 asset_id: asset_id.clone().try_into().map_err(|_| asset.clone())?,
183 }),
184 VersionedLocatableAsset::V5 { location, asset_id } =>
185 Ok(xcm_builder::LocatableAssetId { location, asset_id }),
186 }
187 }
188}
189
190pub struct VersionedLocationConverter;
192impl TryConvert<&VersionedLocation, xcm::latest::Location> for VersionedLocationConverter {
193 fn try_convert(
194 location: &VersionedLocation,
195 ) -> Result<xcm::latest::Location, &VersionedLocation> {
196 let latest = match location.clone() {
197 VersionedLocation::V3(l) => {
198 let v4_location: xcm::v4::Location = l.try_into().map_err(|_| location)?;
199 v4_location.try_into().map_err(|_| location)?
200 },
201 VersionedLocation::V4(l) => l.try_into().map_err(|_| location)?,
202 VersionedLocation::V5(l) => l,
203 };
204 Ok(latest)
205 }
206}
207
208pub struct ContainsParts<C>(core::marker::PhantomData<C>);
211impl<C> Contains<VersionedLocatableAsset> for ContainsParts<C>
212where
213 C: ContainsPair<xcm::latest::Location, xcm::latest::Location>,
214{
215 fn contains(asset: &VersionedLocatableAsset) -> bool {
216 use VersionedLocatableAsset::*;
217 let (location, asset_id) = match asset.clone() {
218 V3 { location, asset_id } => {
219 let v4_location: xcm::v4::Location = match location.try_into() {
220 Ok(l) => l,
221 Err(_) => return false,
222 };
223 let v4_asset_id: xcm::v4::AssetId = match asset_id.try_into() {
224 Ok(a) => a,
225 Err(_) => return false,
226 };
227 match (v4_location.try_into(), v4_asset_id.try_into()) {
228 (Ok(l), Ok(a)) => (l, a),
229 _ => return false,
230 }
231 },
232 V4 { location, asset_id } => match (location.try_into(), asset_id.try_into()) {
233 (Ok(l), Ok(a)) => (l, a),
234 _ => return false,
235 },
236 V5 { location, asset_id } => (location, asset_id),
237 };
238 C::contains(&location, &asset_id.0)
239 }
240}
241
242#[cfg(feature = "runtime-benchmarks")]
243pub mod benchmarks {
244 use super::VersionedLocatableAsset;
245 use core::marker::PhantomData;
246 use frame_support::traits::Get;
247 use pallet_asset_rate::AssetKindFactory;
248 use pallet_treasury::ArgumentsFactory as TreasuryArgumentsFactory;
249 use sp_core::{ConstU32, ConstU8};
250 use xcm::prelude::*;
251
252 pub struct AssetRateArguments;
255 impl AssetKindFactory<VersionedLocatableAsset> for AssetRateArguments {
256 fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
257 (
258 Location::new(0, [Parachain(seed)]),
259 AssetId(Location::new(
260 0,
261 [PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
262 )),
263 )
264 .into()
265 }
266 }
267
268 pub struct TreasuryArguments<Parents = ConstU8<0>, ParaId = ConstU32<0>>(
272 PhantomData<(Parents, ParaId)>,
273 );
274 impl<Parents: Get<u8>, ParaId: Get<u32>>
275 TreasuryArgumentsFactory<VersionedLocatableAsset, VersionedLocation>
276 for TreasuryArguments<Parents, ParaId>
277 {
278 fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
279 (
280 Location::new(Parents::get(), [Junction::Parachain(ParaId::get())]),
281 AssetId(Location::new(
282 0,
283 [PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
284 )),
285 )
286 .into()
287 }
288 fn create_beneficiary(seed: [u8; 32]) -> VersionedLocation {
289 VersionedLocation::from(Location::new(0, [AccountId32 { network: None, id: seed }]))
290 }
291 }
292}
293
294#[cfg(test)]
295mod tests {
296 use super::*;
297 use frame_support::{
298 derive_impl,
299 dispatch::DispatchClass,
300 parameter_types,
301 traits::{
302 tokens::{PayFromAccount, UnityAssetBalanceConversion},
303 FindAuthor,
304 },
305 weights::Weight,
306 PalletId,
307 };
308 use frame_system::limits;
309 use polkadot_primitives::AccountId;
310 use sp_core::{ConstU64, H256};
311 use sp_runtime::{
312 traits::{BlakeTwo256, IdentityLookup},
313 BuildStorage, Perbill,
314 };
315
316 type Block = frame_system::mocking::MockBlock<Test>;
317 const TEST_ACCOUNT: AccountId = AccountId::new([1; 32]);
318
319 frame_support::construct_runtime!(
320 pub enum Test
321 {
322 System: frame_system,
323 Authorship: pallet_authorship,
324 Balances: pallet_balances,
325 Treasury: pallet_treasury,
326 }
327 );
328
329 parameter_types! {
330 pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
331 .base_block(Weight::from_parts(10, 0))
332 .for_class(DispatchClass::all(), |weight| {
333 weight.base_extrinsic = Weight::from_parts(100, 0);
334 })
335 .for_class(DispatchClass::non_mandatory(), |weight| {
336 weight.max_total = Some(Weight::from_parts(1024, u64::MAX));
337 })
338 .build_or_panic();
339 pub BlockLength: limits::BlockLength = limits::BlockLength::max(2 * 1024);
340 pub const AvailableBlockRatio: Perbill = Perbill::one();
341 }
342
343 #[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
344 impl frame_system::Config for Test {
345 type BaseCallFilter = frame_support::traits::Everything;
346 type RuntimeOrigin = RuntimeOrigin;
347 type Nonce = u64;
348 type RuntimeCall = RuntimeCall;
349 type Hash = H256;
350 type Hashing = BlakeTwo256;
351 type AccountId = AccountId;
352 type Lookup = IdentityLookup<Self::AccountId>;
353 type Block = Block;
354 type RuntimeEvent = RuntimeEvent;
355 type BlockLength = BlockLength;
356 type BlockWeights = BlockWeights;
357 type DbWeight = ();
358 type Version = ();
359 type PalletInfo = PalletInfo;
360 type AccountData = pallet_balances::AccountData<u64>;
361 type OnNewAccount = ();
362 type OnKilledAccount = ();
363 type SystemWeightInfo = ();
364 type SS58Prefix = ();
365 type OnSetCode = ();
366 type MaxConsumers = frame_support::traits::ConstU32<16>;
367 }
368
369 #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
370 impl pallet_balances::Config for Test {
371 type AccountStore = System;
372 }
373
374 parameter_types! {
375 pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
376 pub const MaxApprovals: u32 = 100;
377 pub TreasuryAccount: AccountId = Treasury::account_id();
378 }
379
380 impl pallet_treasury::Config for Test {
381 type Currency = pallet_balances::Pallet<Test>;
382 type RejectOrigin = frame_system::EnsureRoot<AccountId>;
383 type RuntimeEvent = RuntimeEvent;
384 type SpendPeriod = ();
385 type Burn = ();
386 type BurnDestination = ();
387 type PalletId = TreasuryPalletId;
388 type SpendFunds = ();
389 type MaxApprovals = MaxApprovals;
390 type WeightInfo = ();
391 type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u64>;
392 type AssetKind = ();
393 type Beneficiary = Self::AccountId;
394 type BeneficiaryLookup = IdentityLookup<Self::AccountId>;
395 type Paymaster = PayFromAccount<Balances, TreasuryAccount>;
396 type BalanceConverter = UnityAssetBalanceConversion;
397 type PayoutPeriod = ConstU64<0>;
398 type BlockNumberProvider = System;
399 #[cfg(feature = "runtime-benchmarks")]
400 type BenchmarkHelper = ();
401 }
402
403 pub struct OneAuthor;
404 impl FindAuthor<AccountId> for OneAuthor {
405 fn find_author<'a, I>(_: I) -> Option<AccountId>
406 where
407 I: 'a,
408 {
409 Some(TEST_ACCOUNT)
410 }
411 }
412 impl pallet_authorship::Config for Test {
413 type FindAuthor = OneAuthor;
414 type EventHandler = ();
415 }
416
417 pub fn new_test_ext() -> sp_io::TestExternalities {
418 let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
419 pallet_balances::GenesisConfig::<Test>::default()
421 .assimilate_storage(&mut t)
422 .unwrap();
423 t.into()
424 }
425
426 pub fn deprecated_era_payout(
427 total_staked: Balance,
428 total_stakable: Balance,
429 max_annual_inflation: Perquintill,
430 period_fraction: Perquintill,
431 auctioned_slots: u64,
432 ) -> (Balance, Balance) {
433 use pallet_staking_reward_fn::compute_inflation;
434 use sp_runtime::traits::Saturating;
435
436 let min_annual_inflation = Perquintill::from_rational(25u64, 1000u64);
437 let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);
438
439 let auction_proportion = Perquintill::from_rational(auctioned_slots.min(60), 200u64);
441
442 let ideal_stake = Perquintill::from_percent(75).saturating_sub(auction_proportion);
445
446 let stake = Perquintill::from_rational(total_staked, total_stakable);
447 let falloff = Perquintill::from_percent(5);
448 let adjustment = compute_inflation(stake, ideal_stake, falloff);
449 let staking_inflation =
450 min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);
451
452 let max_payout = period_fraction * max_annual_inflation * total_stakable;
453 let staking_payout = (period_fraction * staking_inflation) * total_stakable;
454 let rest = max_payout.saturating_sub(staking_payout);
455
456 let other_issuance = total_stakable.saturating_sub(total_staked);
457 if total_staked > other_issuance {
458 let _cap_rest =
459 Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
460 }
463 (staking_payout, rest)
464 }
465
466 #[test]
467 fn test_fees_and_tip_split() {
468 new_test_ext().execute_with(|| {
469 let fee =
470 <pallet_balances::Pallet<Test> as frame_support::traits::fungible::Balanced<
471 AccountId,
472 >>::issue(10);
473 let tip =
474 <pallet_balances::Pallet<Test> as frame_support::traits::fungible::Balanced<
475 AccountId,
476 >>::issue(20);
477
478 assert_eq!(Balances::free_balance(Treasury::account_id()), 0);
479 assert_eq!(Balances::free_balance(TEST_ACCOUNT), 0);
480
481 DealWithFees::on_unbalanceds(vec![fee, tip].into_iter());
482
483 assert_eq!(Balances::free_balance(TEST_ACCOUNT), 22);
485 assert_eq!(Balances::free_balance(Treasury::account_id()), 8);
487 });
488 }
489
490 #[test]
491 fn compute_inflation_should_give_sensible_results() {
492 assert_eq!(
493 pallet_staking_reward_fn::compute_inflation(
494 Perquintill::from_percent(75),
495 Perquintill::from_percent(75),
496 Perquintill::from_percent(5),
497 ),
498 Perquintill::one()
499 );
500 assert_eq!(
501 pallet_staking_reward_fn::compute_inflation(
502 Perquintill::from_percent(50),
503 Perquintill::from_percent(75),
504 Perquintill::from_percent(5),
505 ),
506 Perquintill::from_rational(2u64, 3u64)
507 );
508 assert_eq!(
509 pallet_staking_reward_fn::compute_inflation(
510 Perquintill::from_percent(80),
511 Perquintill::from_percent(75),
512 Perquintill::from_percent(5),
513 ),
514 Perquintill::from_rational(1u64, 2u64)
515 );
516 }
517
518 #[test]
519 fn era_payout_should_give_sensible_results() {
520 let payout =
521 deprecated_era_payout(75, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
522 assert_eq!(payout, (10, 0));
523
524 let payout =
525 deprecated_era_payout(80, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
526 assert_eq!(payout, (6, 4));
527 }
528
529 #[test]
530 fn relay_era_payout_should_give_sensible_results() {
531 let params = EraPayoutParams {
532 total_staked: 75,
533 total_stakable: 100,
534 ideal_stake: Perquintill::from_percent(75),
535 max_annual_inflation: Perquintill::from_percent(10),
536 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
537 falloff: Perquintill::from_percent(5),
538 period_fraction: Perquintill::one(),
539 legacy_auction_proportion: None,
540 };
541 assert_eq!(relay_era_payout(params), (10, 0));
542
543 let params = EraPayoutParams {
544 total_staked: 80,
545 total_stakable: 100,
546 ideal_stake: Perquintill::from_percent(75),
547 max_annual_inflation: Perquintill::from_percent(10),
548 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
549 falloff: Perquintill::from_percent(5),
550 period_fraction: Perquintill::one(),
551 legacy_auction_proportion: None,
552 };
553 assert_eq!(relay_era_payout(params), (6, 4));
554 }
555
556 #[test]
557 fn relay_era_payout_should_give_same_results_as_era_payout() {
558 let total_staked = 1_000_000;
559 let total_stakable = 2_000_000;
560 let max_annual_inflation = Perquintill::from_percent(10);
561 let period_fraction = Perquintill::from_percent(25);
562 let auctioned_slots = 30;
563
564 let params = EraPayoutParams {
565 total_staked,
566 total_stakable,
567 ideal_stake: Perquintill::from_percent(75),
568 max_annual_inflation,
569 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
570 falloff: Perquintill::from_percent(5),
571 period_fraction,
572 legacy_auction_proportion: Some(Perquintill::from_rational(
573 auctioned_slots.min(60),
574 200u64,
575 )),
576 };
577
578 let payout = deprecated_era_payout(
579 total_staked,
580 total_stakable,
581 max_annual_inflation,
582 period_fraction,
583 auctioned_slots,
584 );
585 assert_eq!(relay_era_payout(params), payout);
586
587 let total_staked = 1_900_000;
588 let total_stakable = 2_000_000;
589 let auctioned_slots = 60;
590
591 let params = EraPayoutParams {
592 total_staked,
593 total_stakable,
594 ideal_stake: Perquintill::from_percent(75),
595 max_annual_inflation,
596 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
597 falloff: Perquintill::from_percent(5),
598 period_fraction,
599 legacy_auction_proportion: Some(Perquintill::from_rational(
600 auctioned_slots.min(60),
601 200u64,
602 )),
603 };
604
605 let payout = deprecated_era_payout(
606 total_staked,
607 total_stakable,
608 max_annual_inflation,
609 period_fraction,
610 auctioned_slots,
611 );
612
613 assert_eq!(relay_era_payout(params), payout);
614 }
615}