Skip to content

Commit

Permalink
Add stakeinfo by hk, ck, netuid
Browse files Browse the repository at this point in the history
  • Loading branch information
camfairchild committed Jan 28, 2025
1 parent 067ddd3 commit 19d35a6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sp_api::decl_runtime_apis! {
pub trait StakeInfoRuntimeApi {
fn get_stake_info_for_coldkey( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_stake_info_for_coldkeys( coldkey_account_vecs: Vec<Vec<u8>> ) -> Vec<u8>;
fn get_stake_info_for_hotkey_coldkey_netuid( hotkey_account_vec: Vec<u8>, coldkey_account_vec: Vec<u8>, netuid: u16 ) -> Vec<u8>;
}

pub trait SubnetRegistrationRuntimeApi {
Expand Down
38 changes: 38 additions & 0 deletions pallets/subtensor/src/rpc_info/stake_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,42 @@ impl<T: Config> Pallet<T> {
first.1.clone()
}
}

pub fn get_stake_info_for_hotkey_coldkey_netuid(
hotkey_account_vec: Vec<u8>,
coldkey_account_vec: Vec<u8>,
netuid: u16,
) -> Option<StakeInfo<T>> {
if coldkey_account_vec.len() != 32 {
return None; // Invalid coldkey
}

let Ok(coldkey) = T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()) else {
return None;
};

if hotkey_account_vec.len() != 32 {
return None; // Invalid hotkey
}

let Ok(hotkey) = T::AccountId::decode(&mut hotkey_account_vec.as_bytes_ref()) else {
return None;
};

let alpha: u64 =
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
let emission: u64 = AlphaDividendsPerSubnet::<T>::get(netuid, &hotkey);
let is_registered: bool = Self::is_hotkey_registered_on_network(netuid, &hotkey);

Some(StakeInfo {
hotkey: hotkey.clone(),
coldkey: coldkey.clone(),
netuid: (netuid).into(),
stake: alpha.into(),
locked: 0.into(),
emission: emission.into(),
drain: 0.into(),
is_registered,
})
}
}
5 changes: 5 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,11 @@ impl_runtime_apis! {
let result = SubtensorModule::get_stake_info_for_coldkeys( coldkey_account_vecs );
result.encode()
}

fn get_stake_for_hotkey_coldkey_netuid( hotkey_account_vec: Vec<u8>, coldkey_account_vec: Vec<u8>, netuid: u16 ) -> Vec<u8> {
let result = SubtensorModule::get_stake_info_for_hotkey_coldkey_netuid( hotkey_account_vec, coldkey_account_vec, netuid );
result.encode()
}
}

impl subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi<Block> for Runtime {
Expand Down

0 comments on commit 19d35a6

Please sign in to comment.