1use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
20use pezframe_support::traits::{
21 fungible::{Balanced, Credit},
22 tokens::imbalance::ResolveTo,
23 Contains, ContainsPair, Imbalance, OnUnbalanced,
24};
25use pezkuwi_primitives::Balance;
26use pezpallet_treasury::TreasuryAccountId;
27use pezsp_runtime::{traits::TryConvert, Perquintill, RuntimeDebug};
28use xcm::VersionedLocation;
29
30pub struct ToAuthor<R>(core::marker::PhantomData<R>);
32impl<R> OnUnbalanced<Credit<R::AccountId, pezpallet_balances::Pezpallet<R>>> for ToAuthor<R>
33where
34 R: pezpallet_balances::Config + pezpallet_authorship::Config,
35 <R as pezframe_system::Config>::AccountId: From<pezkuwi_primitives::AccountId>,
36 <R as pezframe_system::Config>::AccountId: Into<pezkuwi_primitives::AccountId>,
37{
38 fn on_nonzero_unbalanced(
39 amount: Credit<<R as pezframe_system::Config>::AccountId, pezpallet_balances::Pezpallet<R>>,
40 ) {
41 if let Some(author) = <pezpallet_authorship::Pezpallet<R>>::author() {
42 let _ = <pezpallet_balances::Pezpallet<R>>::resolve(&author, amount);
43 }
44 }
45}
46
47pub struct DealWithFees<R>(core::marker::PhantomData<R>);
48impl<R> OnUnbalanced<Credit<R::AccountId, pezpallet_balances::Pezpallet<R>>> for DealWithFees<R>
49where
50 R: pezpallet_balances::Config + pezpallet_authorship::Config + pezpallet_treasury::Config,
51 <R as pezframe_system::Config>::AccountId: From<pezkuwi_primitives::AccountId>,
52 <R as pezframe_system::Config>::AccountId: Into<pezkuwi_primitives::AccountId>,
53{
54 fn on_unbalanceds(
55 mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pezpallet_balances::Pezpallet<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>, pezpallet_balances::Pezpallet<R>>::on_unbalanced(
65 split.0,
66 );
67 <ToAuthor<R> as OnUnbalanced<_>>::on_unbalanced(split.1);
68 }
69 }
70}
71
72pub struct EraPayoutParams {
74 pub total_staked: Balance,
76 pub total_stakable: Balance,
81 pub ideal_stake: Perquintill,
83 pub max_annual_inflation: Perquintill,
85 pub min_annual_inflation: Perquintill,
87 pub falloff: Perquintill,
89 pub period_fraction: Perquintill,
91 pub legacy_auction_proportion: Option<Perquintill>,
93}
94
95pub fn relay_era_payout(params: EraPayoutParams) -> (Balance, Balance) {
98 use pezsp_runtime::traits::Saturating;
99
100 let EraPayoutParams {
101 total_staked,
102 total_stakable,
103 ideal_stake,
104 max_annual_inflation,
105 min_annual_inflation,
106 falloff,
107 period_fraction,
108 legacy_auction_proportion,
109 } = params;
110
111 let delta_annual_inflation = max_annual_inflation.saturating_sub(min_annual_inflation);
112
113 let ideal_stake = ideal_stake.saturating_sub(legacy_auction_proportion.unwrap_or_default());
114
115 let stake = Perquintill::from_rational(total_staked, total_stakable);
116 let adjustment = pezpallet_staking_reward_fn::compute_inflation(stake, ideal_stake, falloff);
117 let staking_inflation =
118 min_annual_inflation.saturating_add(delta_annual_inflation * adjustment);
119
120 let max_payout = period_fraction * max_annual_inflation * total_stakable;
121 let staking_payout = (period_fraction * staking_inflation) * total_stakable;
122 let rest = max_payout.saturating_sub(staking_payout);
123
124 let other_issuance = total_stakable.saturating_sub(total_staked);
125 if total_staked > other_issuance {
126 let _cap_rest = Perquintill::from_rational(other_issuance, total_staked) * staking_payout;
127 }
130 (staking_payout, rest)
131}
132
133#[derive(
136 Encode,
137 Decode,
138 DecodeWithMemTracking,
139 Eq,
140 PartialEq,
141 Clone,
142 RuntimeDebug,
143 scale_info::TypeInfo,
144 MaxEncodedLen,
145)]
146pub enum VersionedLocatableAsset {
147 #[codec(index = 3)]
148 V3 { location: xcm::v3::Location, asset_id: xcm::v3::AssetId },
149 #[codec(index = 4)]
150 V4 { location: xcm::v4::Location, asset_id: xcm::v4::AssetId },
151 #[codec(index = 5)]
152 V5 { location: xcm::v5::Location, asset_id: xcm::v5::AssetId },
153}
154
155impl From<(xcm::latest::Location, xcm::latest::AssetId)> for VersionedLocatableAsset {
157 fn from(value: (xcm::latest::Location, xcm::latest::AssetId)) -> Self {
158 VersionedLocatableAsset::V5 { location: value.0, asset_id: value.1 }
159 }
160}
161
162pub struct LocatableAssetConverter;
164impl TryConvert<VersionedLocatableAsset, xcm_builder::LocatableAssetId>
165 for LocatableAssetConverter
166{
167 fn try_convert(
168 asset: VersionedLocatableAsset,
169 ) -> Result<xcm_builder::LocatableAssetId, VersionedLocatableAsset> {
170 match asset {
171 VersionedLocatableAsset::V3 { location, asset_id } => {
172 let v4_location: xcm::v4::Location =
173 location.try_into().map_err(|_| asset.clone())?;
174 let v4_asset_id: xcm::v4::AssetId =
175 asset_id.try_into().map_err(|_| asset.clone())?;
176 Ok(xcm_builder::LocatableAssetId {
177 location: v4_location.try_into().map_err(|_| asset.clone())?,
178 asset_id: v4_asset_id.try_into().map_err(|_| asset.clone())?,
179 })
180 },
181 VersionedLocatableAsset::V4 { ref location, ref asset_id } => {
182 Ok(xcm_builder::LocatableAssetId {
183 location: location.clone().try_into().map_err(|_| asset.clone())?,
184 asset_id: asset_id.clone().try_into().map_err(|_| asset.clone())?,
185 })
186 },
187 VersionedLocatableAsset::V5 { location, asset_id } => {
188 Ok(xcm_builder::LocatableAssetId { location, asset_id })
189 },
190 }
191 }
192}
193
194pub struct VersionedLocationConverter;
196impl TryConvert<&VersionedLocation, xcm::latest::Location> for VersionedLocationConverter {
197 fn try_convert(
198 location: &VersionedLocation,
199 ) -> Result<xcm::latest::Location, &VersionedLocation> {
200 let latest = match location.clone() {
201 VersionedLocation::V3(l) => {
202 let v4_location: xcm::v4::Location = l.try_into().map_err(|_| location)?;
203 v4_location.try_into().map_err(|_| location)?
204 },
205 VersionedLocation::V4(l) => l.try_into().map_err(|_| location)?,
206 VersionedLocation::V5(l) => l,
207 };
208 Ok(latest)
209 }
210}
211
212pub struct ContainsParts<C>(core::marker::PhantomData<C>);
215impl<C> Contains<VersionedLocatableAsset> for ContainsParts<C>
216where
217 C: ContainsPair<xcm::latest::Location, xcm::latest::Location>,
218{
219 fn contains(asset: &VersionedLocatableAsset) -> bool {
220 use VersionedLocatableAsset::*;
221 let (location, asset_id) = match asset.clone() {
222 V3 { location, asset_id } => {
223 let v4_location: xcm::v4::Location = match location.try_into() {
224 Ok(l) => l,
225 Err(_) => return false,
226 };
227 let v4_asset_id: xcm::v4::AssetId = match asset_id.try_into() {
228 Ok(a) => a,
229 Err(_) => return false,
230 };
231 match (v4_location.try_into(), v4_asset_id.try_into()) {
232 (Ok(l), Ok(a)) => (l, a),
233 _ => return false,
234 }
235 },
236 V4 { location, asset_id } => match (location.try_into(), asset_id.try_into()) {
237 (Ok(l), Ok(a)) => (l, a),
238 _ => return false,
239 },
240 V5 { location, asset_id } => (location, asset_id),
241 };
242 C::contains(&location, &asset_id.0)
243 }
244}
245
246#[cfg(feature = "runtime-benchmarks")]
247pub mod benchmarks {
248 use super::VersionedLocatableAsset;
249 use core::marker::PhantomData;
250 use pezframe_support::traits::Get;
251 use pezpallet_asset_rate::AssetKindFactory;
252 use pezpallet_treasury::ArgumentsFactory as TreasuryArgumentsFactory;
253 use pezsp_core::{ConstU32, ConstU8};
254 use xcm::prelude::*;
255
256 pub struct AssetRateArguments;
259 impl AssetKindFactory<VersionedLocatableAsset> for AssetRateArguments {
260 fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
261 (
262 Location::new(0, [Teyrchain(seed)]),
263 AssetId(Location::new(
264 0,
265 [PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
266 )),
267 )
268 .into()
269 }
270 }
271
272 pub struct TreasuryArguments<Parents = ConstU8<0>, ParaId = ConstU32<0>>(
276 PhantomData<(Parents, ParaId)>,
277 );
278 impl<Parents: Get<u8>, ParaId: Get<u32>>
279 TreasuryArgumentsFactory<VersionedLocatableAsset, VersionedLocation>
280 for TreasuryArguments<Parents, ParaId>
281 {
282 fn create_asset_kind(seed: u32) -> VersionedLocatableAsset {
283 (
284 Location::new(Parents::get(), [Junction::Teyrchain(ParaId::get())]),
285 AssetId(Location::new(
286 0,
287 [PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())],
288 )),
289 )
290 .into()
291 }
292 fn create_beneficiary(seed: [u8; 32]) -> VersionedLocation {
293 VersionedLocation::from(Location::new(0, [AccountId32 { network: None, id: seed }]))
294 }
295 }
296}
297
298#[cfg(test)]
299mod tests {
300 use super::*;
301 use pezframe_support::{
302 derive_impl,
303 dispatch::DispatchClass,
304 parameter_types,
305 traits::{
306 tokens::{PayFromAccount, UnityAssetBalanceConversion},
307 FindAuthor,
308 },
309 weights::Weight,
310 PalletId,
311 };
312 use pezframe_system::limits;
313 use pezkuwi_primitives::AccountId;
314 use pezsp_core::{ConstU64, H256};
315 use pezsp_runtime::{
316 traits::{BlakeTwo256, IdentityLookup},
317 BuildStorage, Perbill,
318 };
319
320 type Block = pezframe_system::mocking::MockBlock<Test>;
321 const TEST_ACCOUNT: AccountId = AccountId::new([1; 32]);
322
323 pezframe_support::construct_runtime!(
324 pub enum Test
325 {
326 System: pezframe_system,
327 Authorship: pezpallet_authorship,
328 Balances: pezpallet_balances,
329 Treasury: pezpallet_treasury,
330 }
331 );
332
333 parameter_types! {
334 pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
335 .base_block(Weight::from_parts(10, 0))
336 .for_class(DispatchClass::all(), |weight| {
337 weight.base_extrinsic = Weight::from_parts(100, 0);
338 })
339 .for_class(DispatchClass::non_mandatory(), |weight| {
340 weight.max_total = Some(Weight::from_parts(1024, u64::MAX));
341 })
342 .build_or_panic();
343 pub BlockLength: limits::BlockLength = limits::BlockLength::max(2 * 1024);
344 pub const AvailableBlockRatio: Perbill = Perbill::one();
345 }
346
347 #[derive_impl(pezframe_system::config_preludes::TestDefaultConfig)]
348 impl pezframe_system::Config for Test {
349 type BaseCallFilter = pezframe_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 = pezpallet_balances::AccountData<u64>;
365 type OnNewAccount = ();
366 type OnKilledAccount = ();
367 type SystemWeightInfo = ();
368 type SS58Prefix = ();
369 type OnSetCode = ();
370 type MaxConsumers = pezframe_support::traits::ConstU32<16>;
371 }
372
373 #[derive_impl(pezpallet_balances::config_preludes::TestDefaultConfig)]
374 impl pezpallet_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 pezpallet_treasury::Config for Test {
385 type Currency = pezpallet_balances::Pezpallet<Test>;
386 type RejectOrigin = pezframe_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 = pezframe_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 pezpallet_authorship::Config for Test {
417 type FindAuthor = OneAuthor;
418 type EventHandler = ();
419 }
420
421 pub fn new_test_ext() -> pezsp_io::TestExternalities {
422 let mut t = pezframe_system::GenesisConfig::<Test>::default().build_storage().unwrap();
423 pezpallet_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 pezpallet_staking_reward_fn::compute_inflation;
438 use pezsp_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 <pezpallet_balances::Pezpallet<Test> as pezframe_support::traits::fungible::Balanced<
475 AccountId,
476 >>::issue(10);
477 let tip =
478 <pezpallet_balances::Pezpallet<Test> as pezframe_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 pezpallet_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 pezpallet_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 pezpallet_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}