Skip to content
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
19 changes: 19 additions & 0 deletions pallets/subtensor/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub trait SubtensorCustomApi<BlockHash> {
fn get_delegates(&self, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "delegateInfo_getDelegate")]
fn get_delegate(&self, delegate_account_vec: Vec<u8>, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "delegateInfo_getDelegated")]
fn get_delegated(&self, delegatee_account_vec: Vec<u8>, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;

#[method(name = "neuronInfo_getNeurons")]
fn get_neurons(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
Expand Down Expand Up @@ -105,6 +107,23 @@ where
})
}

fn get_delegated(
&self,
delegatee_account_vec: Vec<u8>,
at: Option<<Block as BlockT>::Hash>
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

api.get_delegated(at, delegatee_account_vec).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to get delegated info.",
Some(e.to_string()),
)).into()
})
}

fn get_neurons(
&self,
netuid: u16,
Expand Down
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 @@ -8,6 +8,7 @@ sp_api::decl_runtime_apis! {
pub trait DelegateInfoRuntimeApi {
fn get_delegates() -> Vec<u8>;
fn get_delegate( delegate_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_delegated( delegatee_account_vec: Vec<u8> ) -> Vec<u8>;
}

pub trait NeuronInfoRuntimeApi {
Expand Down
22 changes: 22 additions & 0 deletions pallets/subtensor/src/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,27 @@ impl<T: Config> Pallet<T> {

return delegates;
}

pub fn get_delegated(delegatee_account_vec: Vec<u8>) -> Vec<(DelegateInfo<T>, Compact<u64>)> {
if delegatee_account_vec.len() != 32 {
return Vec::new(); // No delegates for invalid account
}

let delegatee: AccountIdOf<T> = T::AccountId::decode( &mut delegatee_account_vec.as_bytes_ref() ).unwrap();


let mut delegates: Vec<(DelegateInfo<T>, Compact<u64>)> = Vec::new();
for delegate in < Delegates<T> as IterableStorageMap<T::AccountId, u16> >::iter_keys().into_iter() {
let staked_to_this_delegatee = Self::get_stake_for_coldkey_and_hotkey( &delegatee.clone(), &delegate.clone() );
if staked_to_this_delegatee == 0 {
continue; // No stake to this delegate
}
// Staked to this delegate, so add to list
let delegate_info = Self::get_delegate_by_existing_account( delegate.clone() );
delegates.push( (delegate_info, staked_to_this_delegatee.into()) );
}

return delegates;
}
}

5 changes: 5 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ impl_runtime_apis! {
vec![]
}
}

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

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