Skip to content

Commit e0e2b84

Browse files
committed
add stake info by hk, ck, netuid
1 parent f4c2ac5 commit e0e2b84

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ sp_api::decl_runtime_apis! {
3434
pub trait StakeInfoRuntimeApi {
3535
fn get_stake_info_for_coldkey( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
3636
fn get_stake_info_for_coldkeys( coldkey_account_vecs: Vec<Vec<u8>> ) -> Vec<u8>;
37+
fn get_stake_info_for_hotkey_coldkey_netuid( hotkey_account_vec: Vec<u8>, coldkey_account_vec: Vec<u8>, netuid: u16 ) -> Vec<u8>;
3738
}
3839

3940
pub trait SubnetRegistrationRuntimeApi {

pallets/subtensor/src/rpc_info/stake_info.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,42 @@ impl<T: Config> Pallet<T> {
9999
first.1.clone()
100100
}
101101
}
102+
103+
pub fn get_stake_info_for_hotkey_coldkey_netuid(
104+
hotkey_account_vec: Vec<u8>,
105+
coldkey_account_vec: Vec<u8>,
106+
netuid: u16,
107+
) -> Option<StakeInfo<T>> {
108+
if coldkey_account_vec.len() != 32 {
109+
return None; // Invalid coldkey
110+
}
111+
112+
let Ok(coldkey) = T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()) else {
113+
return None;
114+
};
115+
116+
if hotkey_account_vec.len() != 32 {
117+
return None; // Invalid hotkey
118+
}
119+
120+
let Ok(hotkey) = T::AccountId::decode(&mut hotkey_account_vec.as_bytes_ref()) else {
121+
return None;
122+
};
123+
124+
let alpha: u64 =
125+
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
126+
let emission: u64 = AlphaDividendsPerSubnet::<T>::get(netuid, &hotkey);
127+
let is_registered: bool = Self::is_hotkey_registered_on_network(netuid, &hotkey);
128+
129+
Some(StakeInfo {
130+
hotkey: hotkey.clone(),
131+
coldkey: coldkey.clone(),
132+
netuid: (netuid).into(),
133+
stake: alpha.into(),
134+
locked: 0.into(),
135+
emission: emission.into(),
136+
drain: 0.into(),
137+
is_registered,
138+
})
139+
}
102140
}

runtime/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,11 @@ impl_runtime_apis! {
21572157
let result = SubtensorModule::get_stake_info_for_coldkeys( coldkey_account_vecs );
21582158
result.encode()
21592159
}
2160+
2161+
fn get_stake_for_hotkey_coldkey_netuid( hotkey_account_vec: Vec<u8>, coldkey_account_vec: Vec<u8>, netuid: u16 ) -> Vec<u8> {
2162+
let result = SubtensorModule::get_stake_info_for_hotkey_coldkey_netuid( hotkey_account_vec, coldkey_account_vec, netuid );
2163+
result.encode()
2164+
}
21602165
}
21612166

21622167
impl subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi<Block> for Runtime {

0 commit comments

Comments
 (0)