-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathstake_info.rs
107 lines (97 loc) · 3.7 KB
/
stake_info.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use super::*;
use frame_support::pallet_prelude::{Decode, Encode};
extern crate alloc;
use codec::Compact;
use sp_core::hexdisplay::AsBytesRef;
use subtensor_macros::freeze_struct;
#[freeze_struct("c5e3871b39062f8e")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct StakeInfo<T: Config> {
hotkey: T::AccountId,
coldkey: T::AccountId,
netuid: Compact<u16>,
stake: Compact<u64>,
locked: Compact<u64>,
emission: Compact<u64>,
drain: Compact<u64>,
is_registered: bool,
}
impl<T: Config> StakeInfo<T> {
pub fn stake(&self) -> Compact<u64> {
self.stake
}
}
impl<T: Config> Pallet<T> {
fn _get_stake_info_for_coldkeys(
coldkeys: Vec<T::AccountId>,
) -> Vec<(T::AccountId, Vec<StakeInfo<T>>)> {
if coldkeys.is_empty() {
return Vec::new(); // No coldkeys to check
}
let netuids: Vec<u16> = Self::get_all_subnet_netuids();
let mut stake_info: Vec<(T::AccountId, Vec<StakeInfo<T>>)> = Vec::new();
for coldkey_i in coldkeys.iter() {
// Get all hotkeys associated with this coldkey.
let staking_hotkeys = StakingHotkeys::<T>::get(coldkey_i);
let mut stake_info_for_coldkey: Vec<StakeInfo<T>> = Vec::new();
for netuid_i in netuids.iter() {
for hotkey_i in staking_hotkeys.iter() {
let alpha: u64 = Alpha::<T>::get((hotkey_i, coldkey_i, netuid_i));
let emission: u64 = LastHotkeyColdkeyEmissionOnNetuid::<T>::get((
hotkey_i, coldkey_i, *netuid_i,
));
let drain: u64 = LastHotkeyEmissionDrain::<T>::get(hotkey_i);
let is_registered: bool =
Self::is_hotkey_registered_on_network(*netuid_i, hotkey_i);
stake_info_for_coldkey.push(StakeInfo {
hotkey: hotkey_i.clone(),
coldkey: coldkey_i.clone(),
netuid: (*netuid_i).into(),
stake: alpha.into(),
locked: 0.into(),
emission: emission.into(),
drain: drain.into(),
is_registered,
});
}
}
stake_info.push((coldkey_i.clone(), stake_info_for_coldkey));
}
stake_info
}
pub fn get_stake_info_for_coldkeys(
coldkey_account_vecs: Vec<Vec<u8>>,
) -> Vec<(T::AccountId, Vec<StakeInfo<T>>)> {
let mut coldkeys: Vec<T::AccountId> = Vec::new();
for coldkey_account_vec in coldkey_account_vecs {
if coldkey_account_vec.len() != 32 {
continue; // Invalid coldkey
}
let Ok(coldkey) = T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()) else {
continue;
};
coldkeys.push(coldkey);
}
if coldkeys.is_empty() {
return Vec::new(); // Invalid coldkey
}
Self::_get_stake_info_for_coldkeys(coldkeys)
}
pub fn get_stake_info_for_coldkey(coldkey_account_vec: Vec<u8>) -> Vec<StakeInfo<T>> {
if coldkey_account_vec.len() != 32 {
return Vec::new(); // Invalid coldkey
}
let Ok(coldkey) = T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()) else {
return Vec::new();
};
let stake_info = Self::_get_stake_info_for_coldkeys(vec![coldkey]);
if stake_info.is_empty() {
Vec::new() // Invalid coldkey
} else {
let Some(first) = stake_info.first() else {
return Vec::new();
};
first.1.clone()
}
}
}