1
1
use super :: * ;
2
2
use frame_support:: pallet_prelude:: { Decode , Encode } ;
3
- use frame_support:: storage:: IterableStorageMap ;
4
- use frame_support:: IterableStorageDoubleMap ;
3
+ use frame_support:: IterableStorageMap ;
5
4
use safe_math:: * ;
6
5
use substrate_fixed:: types:: U64F64 ;
7
6
extern crate alloc;
7
+ use alloc:: collections:: BTreeMap ;
8
8
use codec:: Compact ;
9
9
10
- #[ freeze_struct( "66105c2cfec0608d " ) ]
10
+ #[ freeze_struct( "f729f2481d94a1de " ) ]
11
11
#[ derive( Decode , Encode , PartialEq , Eq , Clone , Debug , TypeInfo ) ]
12
12
pub struct DelegateInfo < AccountId : TypeInfo + Encode + Decode > {
13
13
delegate_ss58 : AccountId ,
14
14
take : Compact < u16 > ,
15
- nominators : Vec < ( AccountId , Compact < u64 > ) > , // map of nominator_ss58 to stake amount
15
+ nominators : Vec < ( AccountId , Vec < ( Compact < u16 > , Compact < u64 > ) > ) > , // map of nominator_ss58 to netuid and stake amount
16
16
owner_ss58 : AccountId ,
17
17
registrations : Vec < Compact < u16 > > , // Vec of netuid this delegate is registered on
18
18
validator_permits : Vec < Compact < u16 > > , // Vec of netuid this delegate has validator permit on
@@ -49,19 +49,38 @@ impl<T: Config> Pallet<T> {
49
49
Self :: return_per_1000_tao ( take, total_stake, emissions_per_day)
50
50
}
51
51
52
- fn get_delegate_by_existing_account ( delegate : AccountIdOf < T > ) -> DelegateInfo < T :: AccountId > {
53
- let mut nominators = Vec :: < ( T :: AccountId , Compact < u64 > ) > :: new ( ) ;
52
+ fn get_delegate_by_existing_account (
53
+ delegate : AccountIdOf < T > ,
54
+ skip_nominators : bool ,
55
+ ) -> DelegateInfo < T :: AccountId > {
56
+ let mut nominators = Vec :: < ( T :: AccountId , Vec < ( Compact < u16 > , Compact < u64 > ) > ) > :: new ( ) ;
57
+ let mut nominator_map = BTreeMap :: < T :: AccountId , Vec < ( Compact < u16 > , Compact < u64 > ) > > :: new ( ) ;
58
+
59
+ if !skip_nominators {
60
+ let mut alpha_share_pools = vec ! [ ] ;
61
+ for netuid in Self :: get_all_subnet_netuids ( ) {
62
+ let alpha_share_pool = Self :: get_alpha_share_pool ( delegate. clone ( ) , netuid) ;
63
+ alpha_share_pools. push ( alpha_share_pool) ;
64
+ }
65
+
66
+ for ( ( nominator, netuid) , alpha_stake) in Alpha :: < T > :: iter_prefix ( ( delegate. clone ( ) , ) ) {
67
+ if alpha_stake == 0 {
68
+ continue ;
69
+ }
70
+
71
+ if let Some ( alpha_share_pool) = alpha_share_pools. get ( netuid as usize ) {
72
+ let coldkey_stake = alpha_share_pool. get_value_from_shares ( alpha_stake) ;
54
73
55
- for ( nominator, stake) in
56
- <Stake < T > as IterableStorageDoubleMap < T :: AccountId , T :: AccountId , u64 > >:: iter_prefix (
57
- delegate. clone ( ) ,
58
- )
59
- {
60
- if stake == 0 {
61
- continue ;
74
+ nominator_map
75
+ . entry ( nominator. clone ( ) )
76
+ . or_insert ( Vec :: new ( ) )
77
+ . push ( ( netuid. into ( ) , coldkey_stake. into ( ) ) ) ;
78
+ }
79
+ }
80
+
81
+ for ( nominator, stakes) in nominator_map {
82
+ nominators. push ( ( nominator, stakes) ) ;
62
83
}
63
- // Only add nominators with stake
64
- nominators. push ( ( nominator. clone ( ) , stake. into ( ) ) ) ;
65
84
}
66
85
67
86
let registrations = Self :: get_registered_networks_for_hotkey ( & delegate. clone ( ) ) ;
@@ -112,7 +131,7 @@ impl<T: Config> Pallet<T> {
112
131
return None ;
113
132
}
114
133
115
- let delegate_info = Self :: get_delegate_by_existing_account ( delegate. clone ( ) ) ;
134
+ let delegate_info = Self :: get_delegate_by_existing_account ( delegate. clone ( ) , false ) ;
116
135
Some ( delegate_info)
117
136
}
118
137
@@ -121,7 +140,7 @@ impl<T: Config> Pallet<T> {
121
140
pub fn get_delegates ( ) -> Vec < DelegateInfo < T :: AccountId > > {
122
141
let mut delegates = Vec :: < DelegateInfo < T :: AccountId > > :: new ( ) ;
123
142
for delegate in <Delegates < T > as IterableStorageMap < T :: AccountId , u16 > >:: iter_keys ( ) {
124
- let delegate_info = Self :: get_delegate_by_existing_account ( delegate. clone ( ) ) ;
143
+ let delegate_info = Self :: get_delegate_by_existing_account ( delegate. clone ( ) , false ) ;
125
144
delegates. push ( delegate_info) ;
126
145
}
127
146
@@ -136,16 +155,14 @@ impl<T: Config> Pallet<T> {
136
155
let mut delegates: Vec < ( DelegateInfo < T :: AccountId > , Compact < u64 > ) > = Vec :: new ( ) ;
137
156
for delegate in <Delegates < T > as IterableStorageMap < T :: AccountId , u16 > >:: iter_keys ( ) {
138
157
// Staked to this delegate, so add to list
139
- let delegate_info = Self :: get_delegate_by_existing_account ( delegate. clone ( ) ) ;
140
- delegates. push ( (
141
- delegate_info,
142
- Self :: get_stake_for_hotkey_and_coldkey_on_subnet (
143
- & delegatee,
144
- & delegate,
145
- Self :: get_root_netuid ( ) ,
146
- )
147
- . into ( ) ,
148
- ) ) ;
158
+ for ( netuid, _) in Alpha :: < T > :: iter_prefix ( ( delegate. clone ( ) , delegatee. clone ( ) ) ) {
159
+ let delegate_info = Self :: get_delegate_by_existing_account ( delegate. clone ( ) , true ) ;
160
+ delegates. push ( (
161
+ delegate_info,
162
+ Self :: get_stake_for_hotkey_and_coldkey_on_subnet ( & delegate, & delegatee, netuid)
163
+ . into ( ) ,
164
+ ) ) ;
165
+ }
149
166
}
150
167
151
168
delegates
0 commit comments