Skip to content

Commit d474fe1

Browse files
authored
Merge pull request #1318 from opentensor/feat/delegate-info-dtao
Feat/delegate info dtao
2 parents 38828bd + cf00f30 commit d474fe1

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

pallets/subtensor/runtime-api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ sp_api::decl_runtime_apis! {
1919
pub trait DelegateInfoRuntimeApi {
2020
fn get_delegates() -> Vec<DelegateInfo<AccountId32>>;
2121
fn get_delegate( delegate_account: AccountId32 ) -> Option<DelegateInfo<AccountId32>>;
22-
fn get_delegated( delegatee_account: AccountId32 ) -> Vec<(DelegateInfo<AccountId32>, Compact<u64>)>;
22+
fn get_delegated( delegatee_account: AccountId32 ) -> Vec<(DelegateInfo<AccountId32>, (Compact<u16>, Compact<u64>))>;
2323
}
2424

2525
pub trait NeuronInfoRuntimeApi {

pallets/subtensor/src/rpc_info/delegate_info.rs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use super::*;
22
use frame_support::pallet_prelude::{Decode, Encode};
3-
use frame_support::storage::IterableStorageMap;
4-
use frame_support::IterableStorageDoubleMap;
3+
use frame_support::IterableStorageMap;
54
use safe_math::*;
65
use substrate_fixed::types::U64F64;
76
extern crate alloc;
7+
use alloc::collections::BTreeMap;
88
use codec::Compact;
99

10-
#[freeze_struct("66105c2cfec0608d")]
10+
#[freeze_struct("f729f2481d94a1de")]
1111
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
1212
pub struct DelegateInfo<AccountId: TypeInfo + Encode + Decode> {
1313
delegate_ss58: AccountId,
1414
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
1616
owner_ss58: AccountId,
1717
registrations: Vec<Compact<u16>>, // Vec of netuid this delegate is registered on
1818
validator_permits: Vec<Compact<u16>>, // Vec of netuid this delegate has validator permit on
@@ -49,19 +49,38 @@ impl<T: Config> Pallet<T> {
4949
Self::return_per_1000_tao(take, total_stake, emissions_per_day)
5050
}
5151

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);
5473

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));
6283
}
63-
// Only add nominators with stake
64-
nominators.push((nominator.clone(), stake.into()));
6584
}
6685

6786
let registrations = Self::get_registered_networks_for_hotkey(&delegate.clone());
@@ -112,7 +131,7 @@ impl<T: Config> Pallet<T> {
112131
return None;
113132
}
114133

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);
116135
Some(delegate_info)
117136
}
118137

@@ -121,7 +140,7 @@ impl<T: Config> Pallet<T> {
121140
pub fn get_delegates() -> Vec<DelegateInfo<T::AccountId>> {
122141
let mut delegates = Vec::<DelegateInfo<T::AccountId>>::new();
123142
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);
125144
delegates.push(delegate_info);
126145
}
127146

@@ -132,20 +151,24 @@ impl<T: Config> Pallet<T> {
132151
///
133152
pub fn get_delegated(
134153
delegatee: T::AccountId,
135-
) -> Vec<(DelegateInfo<T::AccountId>, Compact<u64>)> {
136-
let mut delegates: Vec<(DelegateInfo<T::AccountId>, Compact<u64>)> = Vec::new();
154+
) -> Vec<(DelegateInfo<T::AccountId>, (Compact<u16>, Compact<u64>))> {
155+
let mut delegates: Vec<(DelegateInfo<T::AccountId>, (Compact<u16>, Compact<u64>))> =
156+
Vec::new();
137157
for delegate in <Delegates<T> as IterableStorageMap<T::AccountId, u16>>::iter_keys() {
138158
// 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-
));
159+
for (netuid, _) in Alpha::<T>::iter_prefix((delegate.clone(), delegatee.clone())) {
160+
let delegate_info = Self::get_delegate_by_existing_account(delegate.clone(), true);
161+
delegates.push((
162+
delegate_info,
163+
(
164+
netuid.into(),
165+
Self::get_stake_for_hotkey_and_coldkey_on_subnet(
166+
&delegate, &delegatee, netuid,
167+
)
168+
.into(),
169+
),
170+
));
171+
}
149172
}
150173

151174
delegates

primitives/share-pool/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ where
6363
.saturating_to_num::<u64>()
6464
}
6565

66+
pub fn get_value_from_shares(&self, current_share: U64F64) -> u64 {
67+
let shared_value: U64F64 = self.state_ops.get_shared_value();
68+
let denominator: U64F64 = self.state_ops.get_denominator();
69+
70+
let maybe_value_per_share = shared_value.checked_div(denominator);
71+
(if let Some(value_per_share) = maybe_value_per_share {
72+
value_per_share.saturating_mul(current_share)
73+
} else {
74+
shared_value
75+
.saturating_mul(current_share)
76+
.checked_div(denominator)
77+
.unwrap_or(U64F64::saturating_from_num(0))
78+
})
79+
.saturating_to_num::<u64>()
80+
}
81+
6682
pub fn try_get_value(&self, key: &K) -> Result<u64, ()> {
6783
match self.state_ops.try_get_share(key) {
6884
Ok(_) => Ok(self.get_value(key)),

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@ impl_runtime_apis! {
20622062
SubtensorModule::get_delegate(delegate_account)
20632063
}
20642064

2065-
fn get_delegated(delegatee_account: AccountId32) -> Vec<(DelegateInfo<AccountId32>, Compact<u64>)> {
2065+
fn get_delegated(delegatee_account: AccountId32) -> Vec<(DelegateInfo<AccountId32>, (Compact<u16>, Compact<u64>))> {
20662066
SubtensorModule::get_delegated(delegatee_account)
20672067
}
20682068
}

0 commit comments

Comments
 (0)