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};
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 Debug,
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 },
185 VersionedLocatableAsset::V5 { location, asset_id } => {
186 Ok(xcm_builder::LocatableAssetId { location, asset_id })
187 },
188 }
189 }
190}
191
192pub struct VersionedLocationConverter;
194impl TryConvert<&VersionedLocation, xcm::latest::Location> for VersionedLocationConverter {
195 fn try_convert(
196 location: &VersionedLocation,
197 ) -> Result<xcm::latest::Location, &VersionedLocation> {
198 let latest = match location.clone() {
199 VersionedLocation::V3(l) => {
200 let v4_location: xcm::v4::Location = l.try_into().map_err(|_| location)?;
201 v4_location.try_into().map_err(|_| location)?
202 },
203 VersionedLocation::V4(l) => l.try_into().map_err(|_| location)?,
204 VersionedLocation::V5(l) => l,
205 };
206 Ok(latest)
207 }
208}
209
210pub struct ContainsParts<C>(core::marker::PhantomData<C>);
213impl<C> Contains<VersionedLocatableAsset> for ContainsParts<C>
214where
215 C: ContainsPair<xcm::latest::Location, xcm::latest::Location>,
216{
217 fn contains(asset: &VersionedLocatableAsset) -> bool {
218 use VersionedLocatableAsset::*;
219 let (location, asset_id) = match asset.clone() {
220 V3 { location, asset_id } => {
221 let v4_location: xcm::v4::Location = match location.try_into() {
222 Ok(l) => l,
223 Err(_) => return false,
224 };
225 let v4_asset_id: xcm::v4::AssetId = match asset_id.try_into() {
226 Ok(a) => a,
227 Err(_) => return false,
228 };
229 match (v4_location.try_into(), v4_asset_id.try_into()) {
230 (Ok(l), Ok(a)) => (l, a),
231 _ => return false,
232 }
233 },
234 V4 { location, asset_id } => match (location.try_into(), asset_id.try_into()) {
235 (Ok(l), Ok(a)) => (l, a),
236 _ => return false,
237 },
238 V5 { location, asset_id } => (location, asset_id),
239 };
240 C::contains(&location, &asset_id.0)
241 }
242}
243
244#[cfg(feature = "runtime-benchmarks")]
245pub mod benchmarks {
246 use super::VersionedLocatableAsset;
247 use core::marker::PhantomData;
248 use frame_support::traits::Get;
249 use pallet_asset_rate::AssetKindFactory;
250 use pallet_treasury::ArgumentsFactory as TreasuryArgumentsFactory;
251 use sp_core::{ConstU32, ConstU8};
252 use xcm::prelude::*;
253
254 pub struct AssetRateArguments;
257 impl AssetKindFactory<VersionedLocatableAsset> for AssetRateArguments {
258 fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
259 (
260 Location::new(0, [Parachain(seed)]),
261 AssetId(Location::new(
262 0,
263 [PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
264 )),
265 )
266 .into()
267 }
268 }
269
270 pub struct TreasuryArguments<Parents = ConstU8<0>, ParaId = ConstU32<0>>(
274 PhantomData<(Parents, ParaId)>,
275 );
276 impl<Parents: Get<u8>, ParaId: Get<u32>>
277 TreasuryArgumentsFactory<VersionedLocatableAsset, VersionedLocation>
278 for TreasuryArguments<Parents, ParaId>
279 {
280 fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
281 (
282 Location::new(Parents::get(), [Junction::Parachain(ParaId::get())]),
283 AssetId(Location::new(
284 0,
285 [PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
286 )),
287 )
288 .into()
289 }
290 fn create_beneficiary(seed: [u8; 32]) -> VersionedLocation {
291 VersionedLocation::from(Location::new(0, [AccountId32 { network: None, id: seed }]))
292 }
293 }
294}
295
296#[cfg(test)]
297mod tests {
298 use super::*;
299 use frame_support::{
300 derive_impl,
301 dispatch::DispatchClass,
302 parameter_types,
303 traits::{
304 tokens::{PayFromAccount, UnityAssetBalanceConversion},
305 FindAuthor,
306 },
307 weights::Weight,
308 PalletId,
309 };
310 use frame_system::limits;
311 use polkadot_primitives::AccountId;
312 use sp_core::{ConstU64, H256};
313 use sp_runtime::{
314 traits::{BlakeTwo256, IdentityLookup},
315 BuildStorage, Perbill,
316 };
317
318 type Block = frame_system::mocking::MockBlock<Test>;
319 const TEST_ACCOUNT: AccountId = AccountId::new([1; 32]);
320
321 frame_support::construct_runtime!(
322 pub enum Test
323 {
324 System: frame_system,
325 Authorship: pallet_authorship,
326 Balances: pallet_balances,
327 Treasury: pallet_treasury,
328 }
329 );
330
331 parameter_types! {
332 pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
333 .base_block(Weight::from_parts(10, 0))
334 .for_class(DispatchClass::all(), |weight| {
335 weight.base_extrinsic = Weight::from_parts(100, 0);
336 })
337 .for_class(DispatchClass::non_mandatory(), |weight| {
338 weight.max_total = Some(Weight::from_parts(1024, u64::MAX));
339 })
340 .build_or_panic();
341 pub BlockLength: limits::BlockLength = limits::BlockLength::builder()
342 .max_length(2 * 1024)
343 .build();
344 pub const AvailableBlockRatio: Perbill = Perbill::one();
345 }
346
347 #[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
348 impl frame_system::Config for Test {
349 type BaseCallFilter = frame_support::traits::Everything;
350 type RuntimeOrigin = RuntimeOrigin;
351 type Nonce = u64;
352 type RuntimeCall = RuntimeCall;
353 type Hash = H256;
354 type Hashing = BlakeTwo256;
355 type AccountId = AccountId;
356 type Lookup = IdentityLookup<Self::AccountId>;
357 type Block = Block;
358 type RuntimeEvent = RuntimeEvent;
359 type BlockLength = BlockLength;
360 type BlockWeights = BlockWeights;
361 type DbWeight = ();
362 type Version = ();
363 type PalletInfo = PalletInfo;
364 type AccountData = pallet_balances::AccountData<u64>;
365 type OnNewAccount = ();
366 type OnKilledAccount = ();
367 type SystemWeightInfo = ();
368 type SS58Prefix = ();
369 type OnSetCode = ();
370 type MaxConsumers = frame_support::traits::ConstU32<16>;
371 }
372
373 #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
374 impl pallet_balances::Config for Test {
375 type AccountStore = System;
376 }
377
378 parameter_types! {
379 pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
380 pub const MaxApprovals: u32 = 100;
381 pub TreasuryAccount: AccountId = Treasury::account_id();
382 }
383
384 impl pallet_treasury::Config for Test {
385 type Currency = pallet_balances::Pallet<Test>;
386 type RejectOrigin = frame_system::EnsureRoot<AccountId>;
387 type RuntimeEvent = RuntimeEvent;
388 type SpendPeriod = ();
389 type Burn = ();
390 type BurnDestination = ();
391 type PalletId = TreasuryPalletId;
392 type SpendFunds = ();
393 type MaxApprovals = MaxApprovals;
394 type WeightInfo = ();
395 type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u64>;
396 type AssetKind = ();
397 type Beneficiary = Self::AccountId;
398 type BeneficiaryLookup = IdentityLookup<Self::AccountId>;
399 type Paymaster = PayFromAccount<Balances, TreasuryAccount>;
400 type BalanceConverter = UnityAssetBalanceConversion;
401 type PayoutPeriod = ConstU64<0>;
402 type BlockNumberProvider = System;
403 #[cfg(feature = "runtime-benchmarks")]
404 type BenchmarkHelper = ();
405 }
406
407 pub struct OneAuthor;
408 impl FindAuthor<AccountId> for OneAuthor {
409 fn find_author<'a, I>(_: I) -> Option<AccountId>
410 where
411 I: 'a,
412 {
413 Some(TEST_ACCOUNT)
414 }
415 }
416 impl pallet_authorship::Config for Test {
417 type FindAuthor = OneAuthor;
418 type EventHandler = ();
419 }
420
421 pub fn new_test_ext() -> sp_io::TestExternalities {
422 let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
423 pallet_balances::GenesisConfig::<Test>::default()
425 .assimilate_storage(&mut t)
426 .unwrap();
427 t.into()
428 }
429
430 pub fn deprecated_era_payout(
431 total_staked: Balance,
432 total_stakable: Balance,
433 max_annual_inflation: Perquintill,
434 period_fraction: Perquintill,
435 auctioned_slots: u64,
436 ) -> (Balance, Balance) {
437 use pallet_staking_reward_fn::compute_inflation;
438 use sp_runtime::traits::Saturating;
439
440 let min_annual_inflation = Perquintill::from_rational(25u64, 1000u64);
441 let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);
442
443 let auction_proportion = Perquintill::from_rational(auctioned_slots.min(60), 200u64);
445
446 let ideal_stake = Perquintill::from_percent(75).saturating_sub(auction_proportion);
449
450 let stake = Perquintill::from_rational(total_staked, total_stakable);
451 let falloff = Perquintill::from_percent(5);
452 let adjustment = compute_inflation(stake, ideal_stake, falloff);
453 let staking_inflation =
454 min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);
455
456 let max_payout = period_fraction * max_annual_inflation * total_stakable;
457 let staking_payout = (period_fraction * staking_inflation) * total_stakable;
458 let rest = max_payout.saturating_sub(staking_payout);
459
460 let other_issuance = total_stakable.saturating_sub(total_staked);
461 if total_staked > other_issuance {
462 let _cap_rest =
463 Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
464 }
467 (staking_payout, rest)
468 }
469
470 #[test]
471 fn test_fees_and_tip_split() {
472 new_test_ext().execute_with(|| {
473 let fee =
474 <pallet_balances::Pallet<Test> as frame_support::traits::fungible::Balanced<
475 AccountId,
476 >>::issue(10);
477 let tip =
478 <pallet_balances::Pallet<Test> as frame_support::traits::fungible::Balanced<
479 AccountId,
480 >>::issue(20);
481
482 assert_eq!(Balances::free_balance(Treasury::account_id()), 0);
483 assert_eq!(Balances::free_balance(TEST_ACCOUNT), 0);
484
485 DealWithFees::on_unbalanceds(vec![fee, tip].into_iter());
486
487 assert_eq!(Balances::free_balance(TEST_ACCOUNT), 22);
489 assert_eq!(Balances::free_balance(Treasury::account_id()), 8);
491 });
492 }
493
494 #[test]
495 fn compute_inflation_should_give_sensible_results() {
496 assert_eq!(
497 pallet_staking_reward_fn::compute_inflation(
498 Perquintill::from_percent(75),
499 Perquintill::from_percent(75),
500 Perquintill::from_percent(5),
501 ),
502 Perquintill::one()
503 );
504 assert_eq!(
505 pallet_staking_reward_fn::compute_inflation(
506 Perquintill::from_percent(50),
507 Perquintill::from_percent(75),
508 Perquintill::from_percent(5),
509 ),
510 Perquintill::from_rational(2u64, 3u64)
511 );
512 assert_eq!(
513 pallet_staking_reward_fn::compute_inflation(
514 Perquintill::from_percent(80),
515 Perquintill::from_percent(75),
516 Perquintill::from_percent(5),
517 ),
518 Perquintill::from_rational(1u64, 2u64)
519 );
520 }
521
522 #[test]
523 fn era_payout_should_give_sensible_results() {
524 let payout =
525 deprecated_era_payout(75, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
526 assert_eq!(payout, (10, 0));
527
528 let payout =
529 deprecated_era_payout(80, 100, Perquintill::from_percent(10), Perquintill::one(), 0);
530 assert_eq!(payout, (6, 4));
531 }
532
533 #[test]
534 fn relay_era_payout_should_give_sensible_results() {
535 let params = EraPayoutParams {
536 total_staked: 75,
537 total_stakable: 100,
538 ideal_stake: Perquintill::from_percent(75),
539 max_annual_inflation: Perquintill::from_percent(10),
540 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
541 falloff: Perquintill::from_percent(5),
542 period_fraction: Perquintill::one(),
543 legacy_auction_proportion: None,
544 };
545 assert_eq!(relay_era_payout(params), (10, 0));
546
547 let params = EraPayoutParams {
548 total_staked: 80,
549 total_stakable: 100,
550 ideal_stake: Perquintill::from_percent(75),
551 max_annual_inflation: Perquintill::from_percent(10),
552 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
553 falloff: Perquintill::from_percent(5),
554 period_fraction: Perquintill::one(),
555 legacy_auction_proportion: None,
556 };
557 assert_eq!(relay_era_payout(params), (6, 4));
558 }
559
560 #[test]
561 fn relay_era_payout_should_give_same_results_as_era_payout() {
562 let total_staked = 1_000_000;
563 let total_stakable = 2_000_000;
564 let max_annual_inflation = Perquintill::from_percent(10);
565 let period_fraction = Perquintill::from_percent(25);
566 let auctioned_slots = 30;
567
568 let params = EraPayoutParams {
569 total_staked,
570 total_stakable,
571 ideal_stake: Perquintill::from_percent(75),
572 max_annual_inflation,
573 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
574 falloff: Perquintill::from_percent(5),
575 period_fraction,
576 legacy_auction_proportion: Some(Perquintill::from_rational(
577 auctioned_slots.min(60),
578 200u64,
579 )),
580 };
581
582 let payout = deprecated_era_payout(
583 total_staked,
584 total_stakable,
585 max_annual_inflation,
586 period_fraction,
587 auctioned_slots,
588 );
589 assert_eq!(relay_era_payout(params), payout);
590
591 let total_staked = 1_900_000;
592 let total_stakable = 2_000_000;
593 let auctioned_slots = 60;
594
595 let params = EraPayoutParams {
596 total_staked,
597 total_stakable,
598 ideal_stake: Perquintill::from_percent(75),
599 max_annual_inflation,
600 min_annual_inflation: Perquintill::from_rational(25u64, 1000u64),
601 falloff: Perquintill::from_percent(5),
602 period_fraction,
603 legacy_auction_proportion: Some(Perquintill::from_rational(
604 auctioned_slots.min(60),
605 200u64,
606 )),
607 };
608
609 let payout = deprecated_era_payout(
610 total_staked,
611 total_stakable,
612 max_annual_inflation,
613 period_fraction,
614 auctioned_slots,
615 );
616
617 assert_eq!(relay_era_payout(params), payout);
618 }
619}