common_primitives/
capacity.rs

1use crate::msa::MessageSourceId;
2use frame_support::traits::tokens::Balance;
3use scale_info::TypeInfo;
4use sp_core::{Decode, Encode, MaxEncodedLen, RuntimeDebug};
5use sp_runtime::DispatchError;
6
7/// The type of a Reward Era
8pub type RewardEra = u32;
9
10/// A trait for checking that a target MSA can be staked to.
11pub trait TargetValidator {
12	/// Checks if an MSA is a valid target.
13	fn validate(target: MessageSourceId) -> bool;
14}
15
16/// A blanket implementation
17impl TargetValidator for () {
18	fn validate(_target: MessageSourceId) -> bool {
19		false
20	}
21}
22
23/// A trait for Non-transferable asset
24pub trait Nontransferable {
25	/// Scalar type for representing balance of an account.
26	type Balance: Balance;
27
28	/// The available Capacity for an MSA.
29	fn balance(msa_id: MessageSourceId) -> Self::Balance;
30
31	/// The replenishable Capacity for an MSA (the total capacity currently issued)
32	fn replenishable_balance(msa_id: MessageSourceId) -> Self::Balance;
33
34	/// Reduce Capacity of an MSA by amount.
35	fn deduct(msa_id: MessageSourceId, capacity_amount: Self::Balance)
36		-> Result<(), DispatchError>;
37
38	/// Increase Staked Token + Capacity amounts of an MSA. (unused)
39	fn deposit(
40		msa_id: MessageSourceId,
41		token_amount: Self::Balance,
42		capacity_amount: Self::Balance,
43	) -> Result<(), DispatchError>;
44}
45
46/// A trait for replenishing Capacity.
47pub trait Replenishable {
48	/// Scalar type for representing balance of an account.
49	type Balance: Balance;
50
51	/// Replenish an MSA's Capacity by an amount.
52	fn replenish_by_amount(
53		msa_id: MessageSourceId,
54		amount: Self::Balance,
55	) -> Result<(), DispatchError>;
56
57	/// Replenish all Capacity balance for an MSA.
58	fn replenish_all_for(msa_id: MessageSourceId) -> Result<(), DispatchError>;
59
60	/// Checks if an account can be replenished.
61	fn can_replenish(msa_id: MessageSourceId) -> bool;
62}
63
64/// Result of checking a Boost History item to see if it's eligible for a reward.
65#[derive(
66	Copy, Clone, Default, Encode, Eq, Decode, RuntimeDebug, MaxEncodedLen, PartialEq, TypeInfo,
67)]
68
69pub struct UnclaimedRewardInfo<Balance, BlockNumber> {
70	/// The Reward Era for which this reward was earned
71	pub reward_era: RewardEra,
72	/// When this reward expires, i.e. can no longer be claimed
73	pub expires_at_block: BlockNumber,
74	/// The total staked in this era as of the current block
75	pub staked_amount: Balance,
76	/// The amount staked in this era that is eligible for rewards.  Does not count additional amounts
77	/// staked in this era.
78	pub eligible_amount: Balance,
79	/// The amount in token of the reward (only if it can be calculated using only on chain data)
80	pub earned_amount: Balance,
81}