pallet_staking_async/
asset.rs1use crate::{BalanceOf, Config, HoldReason, NegativeImbalanceOf, PositiveImbalanceOf};
22use frame_support::traits::{
23 fungible::{
24 hold::{Balanced as FunHoldBalanced, Inspect as FunHoldInspect, Mutate as FunHoldMutate},
25 Balanced, Inspect as FunInspect,
26 },
27 tokens::{Fortitude, Precision, Preservation},
28};
29use sp_runtime::{DispatchResult, Saturating};
30
31pub fn existential_deposit<T: Config>() -> BalanceOf<T> {
33 T::Currency::minimum_balance()
34}
35
36pub fn total_issuance<T: Config>() -> BalanceOf<T> {
38 T::Currency::total_issuance()
39}
40
41pub fn total_balance<T: Config>(who: &T::AccountId) -> BalanceOf<T> {
43 T::Currency::total_balance(who)
44}
45
46pub fn stakeable_balance<T: Config>(who: &T::AccountId) -> BalanceOf<T> {
50 free_to_stake::<T>(who).saturating_add(staked::<T>(who))
51}
52
53pub fn staked<T: Config>(who: &T::AccountId) -> BalanceOf<T> {
57 T::Currency::balance_on_hold(&HoldReason::Staking.into(), who)
58}
59
60pub fn free_to_stake<T: Config>(who: &T::AccountId) -> BalanceOf<T> {
64 T::Currency::reducible_balance(who, Preservation::Preserve, Fortitude::Force)
66}
67
68pub fn update_stake<T: Config>(who: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
73 T::Currency::set_on_hold(&HoldReason::Staking.into(), who, amount)
74}
75
76pub fn kill_stake<T: Config>(who: &T::AccountId) -> DispatchResult {
80 T::Currency::release_all(&HoldReason::Staking.into(), who, Precision::BestEffort).map(|_| ())
81}
82
83pub fn slash<T: Config>(
87 who: &T::AccountId,
88 value: BalanceOf<T>,
89) -> (NegativeImbalanceOf<T>, BalanceOf<T>) {
90 T::Currency::slash(&HoldReason::Staking.into(), who, value)
91}
92
93pub fn mint_into_existing<T: Config>(
97 who: &T::AccountId,
98 value: BalanceOf<T>,
99) -> Option<PositiveImbalanceOf<T>> {
100 T::Currency::deposit(who, value, Precision::Exact).ok()
102}
103
104pub fn mint_creating<T: Config>(who: &T::AccountId, value: BalanceOf<T>) -> PositiveImbalanceOf<T> {
110 T::Currency::deposit(who, value, Precision::BestEffort).unwrap_or_default()
111}
112
113pub fn deposit_slashed<T: Config>(who: &T::AccountId, value: NegativeImbalanceOf<T>) {
115 let _ = T::Currency::resolve(who, value);
116}
117
118pub fn issue<T: Config>(value: BalanceOf<T>) -> NegativeImbalanceOf<T> {
122 T::Currency::issue(value)
123}
124
125#[cfg(feature = "runtime-benchmarks")]
127pub fn burn<T: Config>(amount: BalanceOf<T>) -> PositiveImbalanceOf<T> {
128 T::Currency::rescind(amount)
129}
130
131#[cfg(any(test, feature = "runtime-benchmarks"))]
137pub fn set_stakeable_balance<T: Config>(who: &T::AccountId, value: BalanceOf<T>) {
138 use frame_support::traits::fungible::Mutate;
139
140 let ed = existential_deposit::<T>();
142 let staked_balance = staked::<T>(who);
144
145 if value > staked_balance {
147 let _ = T::Currency::set_balance(who, value - staked_balance + ed);
148 } else {
149 update_stake::<T>(who, value).expect("can remove from what is staked");
151 let _ = T::Currency::set_balance(who, ed);
153 }
154
155 assert_eq!(stakeable_balance::<T>(who), value);
157}
158
159#[cfg(test)]
161pub fn staked_and_not<T: Config>(who: &T::AccountId) -> (BalanceOf<T>, BalanceOf<T>) {
162 (staked::<T>(who), free_to_stake::<T>(who))
163}