Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runtime api for stake #182

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ sp_api::decl_runtime_apis! {
fn get_subnet_info(netuid: u16) -> Vec<u8>;
fn get_subnets_info() -> Vec<u8>;
}

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>;
}
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mod weights;
pub mod delegate_info;
pub mod neuron_info;
pub mod subnet_info;
pub mod stake_info;

// apparently this is stabilized since rust 1.36
extern crate alloc;
Expand Down
75 changes: 75 additions & 0 deletions pallets/subtensor/src/stake_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use super::*;
use frame_support::pallet_prelude::{Decode, Encode};
extern crate alloc;
use alloc::vec::Vec;
use codec::Compact;
use sp_core::hexdisplay::AsBytesRef;

#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct StakeInfo<T: Config> {
hotkey: T::AccountId,
coldkey: T::AccountId,
stake: Compact<u64>,
}

impl<T: Config> Pallet<T> {
fn _get_stake_info_for_coldkeys(coldkeys: Vec<T::AccountId>) -> Vec<(T::AccountId, Vec<StakeInfo<T>>)> {
if coldkeys.len() == 0 {
return Vec::new(); // No coldkeys to check
}

let mut stake_info: Vec<(T::AccountId, Vec<StakeInfo<T>>)> = Vec::new();
for coldkey_ in coldkeys {
let mut stake_info_for_coldkey: Vec<StakeInfo<T>> = Vec::new();

for (hotkey, coldkey, stake) in <Stake<T>>::iter() {
if coldkey == coldkey_ {
stake_info_for_coldkey.push(StakeInfo {
hotkey,
coldkey,
stake: stake.into(),
});
}
}

stake_info.push((coldkey_, stake_info_for_coldkey));
}

return 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 coldkey: AccountIdOf<T> = T::AccountId::decode( &mut coldkey_account_vec.as_bytes_ref() ).unwrap();
coldkeys.push(coldkey);
}

if coldkeys.len() == 0 {
return Vec::new(); // Invalid coldkey
}

let stake_info = Self::_get_stake_info_for_coldkeys(coldkeys);

return stake_info;
}

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 coldkey: AccountIdOf<T> = T::AccountId::decode( &mut coldkey_account_vec.as_bytes_ref() ).unwrap();
let stake_info = Self::_get_stake_info_for_coldkeys(vec![coldkey]);

if stake_info.len() == 0 {
return Vec::new(); // Invalid coldkey
} else {
return stake_info.get(0).unwrap().1.clone();
}
}
}

12 changes: 12 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,18 @@ impl_runtime_apis! {
result.encode()
}
}

impl subtensor_custom_rpc_runtime_api::StakeInfoRuntimeApi<Block> for Runtime {
fn get_stake_info_for_coldkey( coldkey_account_vec: Vec<u8> ) -> Vec<u8> {
let result = SubtensorModule::get_stake_info_for_coldkey( coldkey_account_vec );
result.encode()
}

fn get_stake_info_for_coldkeys( coldkey_account_vecs: Vec<Vec<u8>> ) -> Vec<u8> {
let result = SubtensorModule::get_stake_info_for_coldkeys( coldkey_account_vecs );
result.encode()
}
}
}

#[cfg(test)]
Expand Down