pallet_delegated_staking/
impls.rs1use super::*;
21use sp_staking::{DelegationInterface, DelegationMigrator, OnStakingUpdate};
22
23impl<T: Config> DelegationInterface for Pallet<T> {
24 type Balance = BalanceOf<T>;
25 type AccountId = T::AccountId;
26
27 fn agent_balance(agent: Agent<Self::AccountId>) -> Option<Self::Balance> {
29 AgentLedgerOuter::<T>::get(&agent.get())
30 .map(|a| a.ledger.effective_balance())
31 .ok()
32 }
33
34 fn agent_transferable_balance(agent: Agent<Self::AccountId>) -> Option<Self::Balance> {
35 AgentLedgerOuter::<T>::get(&agent.get())
36 .map(|a| a.ledger.unclaimed_withdrawals)
37 .ok()
38 }
39
40 fn delegator_balance(delegator: Delegator<Self::AccountId>) -> Option<Self::Balance> {
41 Delegation::<T>::get(&delegator.get()).map(|d| d.amount)
42 }
43
44 fn register_agent(
46 agent: Agent<Self::AccountId>,
47 reward_account: &Self::AccountId,
48 ) -> DispatchResult {
49 Pallet::<T>::register_agent(
50 RawOrigin::Signed(agent.clone().get()).into(),
51 reward_account.clone(),
52 )
53 }
54
55 fn remove_agent(agent: Agent<Self::AccountId>) -> DispatchResult {
57 Pallet::<T>::remove_agent(RawOrigin::Signed(agent.clone().get()).into())
58 }
59
60 fn delegate(
62 who: Delegator<Self::AccountId>,
63 agent: Agent<Self::AccountId>,
64 amount: Self::Balance,
65 ) -> DispatchResult {
66 Pallet::<T>::delegate_to_agent(RawOrigin::Signed(who.get()).into(), agent.get(), amount)
67 }
68
69 fn withdraw_delegation(
74 delegator: Delegator<Self::AccountId>,
75 agent: Agent<Self::AccountId>,
76 amount: Self::Balance,
77 num_slashing_spans: u32,
78 ) -> DispatchResult {
79 Pallet::<T>::release_delegation(
80 RawOrigin::Signed(agent.get()).into(),
81 delegator.get(),
82 amount,
83 num_slashing_spans,
84 )
85 }
86
87 fn pending_slash(agent: Agent<Self::AccountId>) -> Option<Self::Balance> {
89 AgentLedgerOuter::<T>::get(&agent.get()).map(|d| d.ledger.pending_slash).ok()
90 }
91
92 fn delegator_slash(
93 agent: Agent<Self::AccountId>,
94 delegator: Delegator<Self::AccountId>,
95 value: Self::Balance,
96 maybe_reporter: Option<Self::AccountId>,
97 ) -> sp_runtime::DispatchResult {
98 Pallet::<T>::do_slash(agent, delegator, value, maybe_reporter)
99 }
100}
101
102impl<T: Config> DelegationMigrator for Pallet<T> {
103 type Balance = BalanceOf<T>;
104 type AccountId = T::AccountId;
105
106 fn migrate_nominator_to_agent(
107 agent: Agent<Self::AccountId>,
108 reward_account: &Self::AccountId,
109 ) -> DispatchResult {
110 Pallet::<T>::migrate_to_agent(RawOrigin::Signed(agent.get()).into(), reward_account.clone())
111 }
112 fn migrate_delegation(
113 agent: Agent<Self::AccountId>,
114 delegator: Delegator<Self::AccountId>,
115 value: Self::Balance,
116 ) -> DispatchResult {
117 Pallet::<T>::migrate_delegation(
118 RawOrigin::Signed(agent.get()).into(),
119 delegator.get(),
120 value,
121 )
122 }
123
124 #[cfg(feature = "runtime-benchmarks")]
126 fn force_kill_agent(agent: Agent<Self::AccountId>) {
127 <Agents<T>>::remove(agent.clone().get());
128 <Delegators<T>>::iter()
129 .filter(|(_, delegation)| delegation.agent == agent.clone().get())
130 .for_each(|(delegator, _)| {
131 let _ = T::Currency::release_all(
132 &HoldReason::StakingDelegation.into(),
133 &delegator,
134 Precision::BestEffort,
135 );
136 <Delegators<T>>::remove(&delegator);
137 });
138 }
139}
140
141impl<T: Config> OnStakingUpdate<T::AccountId, BalanceOf<T>> for Pallet<T> {
142 fn on_slash(
143 who: &T::AccountId,
144 _slashed_active: BalanceOf<T>,
145 _slashed_unlocking: &alloc::collections::btree_map::BTreeMap<EraIndex, BalanceOf<T>>,
146 slashed_total: BalanceOf<T>,
147 ) {
148 <Agents<T>>::mutate(who, |maybe_register| match maybe_register {
149 Some(register) => register.pending_slash.saturating_accrue(slashed_total),
151 None => {
152 },
154 });
155 }
156
157 fn on_withdraw(stash: &T::AccountId, amount: BalanceOf<T>) {
158 let _ = AgentLedgerOuter::<T>::get(stash)
160 .and_then(|agent| agent.add_unclaimed_withdraw(amount).defensive())
162 .map(|agent| agent.save());
163 }
164}