1919//! Implementations of public traits, namely [`DelegationInterface`] and [`OnStakingUpdate`].
2020
2121use super :: * ;
22- use sp_staking:: { DelegationInterface , DelegationMigrator , OnStakingUpdate } ;
22+ use sp_staking:: { Agent , DelegationInterface , DelegationMigrator , Delegator , OnStakingUpdate } ;
2323
2424impl < T : Config > DelegationInterface for Pallet < T > {
2525 type Balance = BalanceOf < T > ;
2626 type AccountId = T :: AccountId ;
2727
2828 /// Effective balance of the `Agent` account.
29- fn agent_balance ( who : & Self :: AccountId ) -> Self :: Balance {
30- Agent :: < T > :: get ( who )
31- . map ( |agent| agent . ledger . effective_balance ( ) )
32- . unwrap_or_default ( )
29+ fn agent_balance ( agent : Agent < Self :: AccountId > ) -> Option < Self :: Balance > {
30+ AgentLedgerOuter :: < T > :: get ( & agent . get ( ) )
31+ . map ( |a| a . ledger . effective_balance ( ) )
32+ . ok ( )
3333 }
3434
35- fn delegator_balance ( delegator : & Self :: AccountId ) -> Self :: Balance {
36- Delegation :: < T > :: get ( delegator) . map ( |d| d. amount ) . unwrap_or_default ( )
35+ fn delegator_balance ( delegator : Delegator < Self :: AccountId > ) -> Option < Self :: Balance > {
36+ Delegation :: < T > :: get ( & delegator. get ( ) ) . map ( |d| d. amount )
3737 }
3838
3939 /// Delegate funds to an `Agent`.
4040 fn delegate (
41- who : & Self :: AccountId ,
42- agent : & Self :: AccountId ,
41+ who : Delegator < Self :: AccountId > ,
42+ agent : Agent < Self :: AccountId > ,
4343 reward_account : & Self :: AccountId ,
4444 amount : Self :: Balance ,
4545 ) -> DispatchResult {
4646 Pallet :: < T > :: register_agent (
47- RawOrigin :: Signed ( agent. clone ( ) ) . into ( ) ,
47+ RawOrigin :: Signed ( agent. clone ( ) . get ( ) ) . into ( ) ,
4848 reward_account. clone ( ) ,
4949 ) ?;
5050
5151 // Delegate the funds from who to the `Agent` account.
52- Pallet :: < T > :: delegate_to_agent ( RawOrigin :: Signed ( who. clone ( ) ) . into ( ) , agent. clone ( ) , amount)
52+ Pallet :: < T > :: delegate_to_agent ( RawOrigin :: Signed ( who. get ( ) ) . into ( ) , agent. get ( ) , amount)
5353 }
5454
5555 /// Add more delegation to the `Agent` account.
5656 fn delegate_extra (
57- who : & Self :: AccountId ,
58- agent : & Self :: AccountId ,
57+ who : Delegator < Self :: AccountId > ,
58+ agent : Agent < Self :: AccountId > ,
5959 amount : Self :: Balance ,
6060 ) -> DispatchResult {
61- Pallet :: < T > :: delegate_to_agent ( RawOrigin :: Signed ( who. clone ( ) ) . into ( ) , agent. clone ( ) , amount)
61+ Pallet :: < T > :: delegate_to_agent ( RawOrigin :: Signed ( who. get ( ) ) . into ( ) , agent. get ( ) , amount)
6262 }
6363
6464 /// Withdraw delegation of `delegator` to `Agent`.
6565 ///
6666 /// If there are funds in `Agent` account that can be withdrawn, then those funds would be
6767 /// unlocked/released in the delegator's account.
6868 fn withdraw_delegation (
69- delegator : & Self :: AccountId ,
70- agent : & Self :: AccountId ,
69+ delegator : Delegator < Self :: AccountId > ,
70+ agent : Agent < Self :: AccountId > ,
7171 amount : Self :: Balance ,
7272 num_slashing_spans : u32 ,
7373 ) -> DispatchResult {
7474 Pallet :: < T > :: release_delegation (
75- RawOrigin :: Signed ( agent. clone ( ) ) . into ( ) ,
76- delegator. clone ( ) ,
75+ RawOrigin :: Signed ( agent. get ( ) ) . into ( ) ,
76+ delegator. get ( ) ,
7777 amount,
7878 num_slashing_spans,
7979 )
8080 }
8181
82- /// Returns true if the `Agent` have any slash pending to be applied.
83- fn has_pending_slash ( agent : & Self :: AccountId ) -> bool {
84- Agent :: < T > :: get ( agent)
85- . map ( |d| !d. ledger . pending_slash . is_zero ( ) )
86- . unwrap_or ( false )
82+ /// Returns pending slash of the `agent`.
83+ fn pending_slash ( agent : Agent < Self :: AccountId > ) -> Option < Self :: Balance > {
84+ AgentLedgerOuter :: < T > :: get ( & agent. get ( ) ) . map ( |d| d. ledger . pending_slash ) . ok ( )
8785 }
8886
8987 fn delegator_slash (
90- agent : & Self :: AccountId ,
91- delegator : & Self :: AccountId ,
88+ agent : Agent < Self :: AccountId > ,
89+ delegator : Delegator < Self :: AccountId > ,
9290 value : Self :: Balance ,
9391 maybe_reporter : Option < Self :: AccountId > ,
9492 ) -> sp_runtime:: DispatchResult {
95- Pallet :: < T > :: do_slash ( agent. clone ( ) , delegator. clone ( ) , value, maybe_reporter)
93+ Pallet :: < T > :: do_slash ( agent, delegator, value, maybe_reporter)
9694 }
9795}
9896
@@ -101,32 +99,29 @@ impl<T: Config> DelegationMigrator for Pallet<T> {
10199 type AccountId = T :: AccountId ;
102100
103101 fn migrate_nominator_to_agent (
104- agent : & Self :: AccountId ,
102+ agent : Agent < Self :: AccountId > ,
105103 reward_account : & Self :: AccountId ,
106104 ) -> DispatchResult {
107- Pallet :: < T > :: migrate_to_agent (
108- RawOrigin :: Signed ( agent. clone ( ) ) . into ( ) ,
109- reward_account. clone ( ) ,
110- )
105+ Pallet :: < T > :: migrate_to_agent ( RawOrigin :: Signed ( agent. get ( ) ) . into ( ) , reward_account. clone ( ) )
111106 }
112107 fn migrate_delegation (
113- agent : & Self :: AccountId ,
114- delegator : & Self :: AccountId ,
108+ agent : Agent < Self :: AccountId > ,
109+ delegator : Delegator < Self :: AccountId > ,
115110 value : Self :: Balance ,
116111 ) -> DispatchResult {
117112 Pallet :: < T > :: migrate_delegation (
118- RawOrigin :: Signed ( agent. clone ( ) ) . into ( ) ,
119- delegator. clone ( ) ,
113+ RawOrigin :: Signed ( agent. get ( ) ) . into ( ) ,
114+ delegator. get ( ) ,
120115 value,
121116 )
122117 }
123118
124119 /// Only used for testing.
125120 #[ cfg( feature = "runtime-benchmarks" ) ]
126- fn drop_agent ( agent : & T :: AccountId ) {
127- <Agents < T > >:: remove ( agent) ;
121+ fn drop_agent ( agent : Agent < Self :: AccountId > ) {
122+ <Agents < T > >:: remove ( agent. clone ( ) . get ( ) ) ;
128123 <Delegators < T > >:: iter ( )
129- . filter ( |( _, delegation) | delegation. agent == * agent)
124+ . filter ( |( _, delegation) | delegation. agent == agent. clone ( ) . get ( ) )
130125 . for_each ( |( delegator, _) | {
131126 let _ = T :: Currency :: release_all (
132127 & HoldReason :: StakingDelegation . into ( ) ,
@@ -136,7 +131,7 @@ impl<T: Config> DelegationMigrator for Pallet<T> {
136131 <Delegators < T > >:: remove ( & delegator) ;
137132 } ) ;
138133
139- T :: CoreStaking :: migrate_to_direct_staker ( agent) ;
134+ T :: CoreStaking :: migrate_to_direct_staker ( & agent. get ( ) ) ;
140135 }
141136}
142137
@@ -158,7 +153,7 @@ impl<T: Config> OnStakingUpdate<T::AccountId, BalanceOf<T>> for Pallet<T> {
158153
159154 fn on_withdraw ( stash : & T :: AccountId , amount : BalanceOf < T > ) {
160155 // if there is a withdraw to the agent, then add it to the unclaimed withdrawals.
161- let _ = Agent :: < T > :: get ( stash)
156+ let _ = AgentLedgerOuter :: < T > :: get ( stash)
162157 // can't do anything if there is an overflow error. Just raise a defensive error.
163158 . and_then ( |agent| agent. add_unclaimed_withdraw ( amount) . defensive ( ) )
164159 . map ( |agent| agent. save ( ) ) ;
0 commit comments