Skip to content

Commit 3de9c3d

Browse files
committed
feat: modify total stake coldkey func
- gets stake for coldkey on subnet and across all subnets
1 parent 573a7b1 commit 3de9c3d

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,69 @@ impl<T: Config> Pallet<T> {
379379
TotalHotkeyAlpha::<T>::get(hotkey, netuid)
380380
}
381381

382+
/// Retrieves the total stake (alpha) for a given coldkey on a specific sunbet.
383+
///
384+
/// This function performs the following steps:
385+
/// 1. Retrieves the list of hotkeys associated with the coldkey on the subnet.
386+
/// 2. Iterates through the list of hotkeys and retrieves the alpha stake for each hotkey.
387+
/// 3. Sums the alpha stakes for all hotkeys to calculate the total stake for the coldkey.
388+
/// 4. Returns the total alpha stake for the coldkey on the subnet.
389+
///
390+
/// # Arguments
391+
/// * `coldkey` - The account ID of the coldkey.
392+
/// * `netuid` - The unique identifier of the subnet.
393+
///
394+
/// # Returns
395+
/// * `u64` - The total alpha value for the coldkey on the specified subnet.
396+
///
397+
/// # Note
398+
/// This function returns the cumulative stake across all hotkeys associated with this coldkey on the subnet.
399+
/// This value represents the sum of stakes from all hotkeys associated with this coldkey.
400+
pub fn get_stake_for_coldkey_on_subnet(coldkey: &T::AccountId, netuid: u16) -> u64 {
401+
// Retrieve the list of hotkeys associated with the coldkey on the subnet.
402+
let hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::get(coldkey);
403+
404+
// Calculate the total alpha stake for the coldkey on the subnet.
405+
let total_stake: u64 = hotkeys
406+
.iter()
407+
.map(|hotkey| Self::get_stake_for_hotkey_on_subnet(hotkey, netuid))
408+
.sum();
409+
410+
// Return the total alpha stake for the coldkey on the subnet.
411+
total_stake
412+
}
413+
414+
/// Retrieves the total stake (alpha) for a given coldkey across all subnets
415+
///
416+
/// This function performs the following steps:
417+
/// 1. Retrieves the list of subnets associated with the coldkey.
418+
/// 2. Iterates through the list of subnets and retrieves the alpha stake for each subnet.
419+
/// 3. Sums the alpha stakes for all subnets to calculate the total stake for the coldkey.
420+
/// 4. Returns the total alpha stake for the coldkey across all subnets.
421+
///
422+
/// # Arguments
423+
/// * `coldkey` - The account ID of the coldkey.
424+
///
425+
/// # Returns
426+
/// * `u64` - The total alpha value for the coldkey across all subnets.
427+
///
428+
/// # Note
429+
/// This function returns the cumulative stake across all subnets for the coldkey.
430+
/// This value represents the sum of stakes from all subnets associated with this coldkey.
431+
/// This function is useful for calculating the total stake for a coldkey across all subnets.
432+
pub fn get_stake_for_coldkey(coldkey: &T::AccountId) -> u64 {
433+
// get number of subnets
434+
let netuids = Self::get_all_subnet_netuids();
435+
436+
// Calculate the total alpha stake for the coldkey across all subnets.
437+
let total_stake: u64 = netuids
438+
.iter()
439+
.map(|netuid| Self::get_stake_for_coldkey_on_subnet(coldkey, *netuid))
440+
.sum();
441+
// Return the total alpha stake for the coldkey across all subnets.
442+
total_stake
443+
}
444+
382445
/// Increase hotkey stake on a subnet.
383446
///
384447
/// The function updates share totals given current prices.

runtime/src/precompiles/staking.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,21 @@ impl StakingPrecompile {
118118
Self::dispatch(handle, call)
119119
}
120120

121-
fn get_total_coldkey_stake(data: &[u8]) -> PrecompileResult {
121+
fn get_total_coldkey_stake(data: &[u8]) -> PrecompileResult {
122122
let coldkey: AccountId32 = Self::parse_pub_key(data)?.into();
123123

124124
// get total stake of coldkey
125-
let total_stake = pallet_subtensor::Pallet::<Runtime>::get_total_stake_for_coldkey(&coldkey);
126-
let result_u256 = U256::from(total_stake);
125+
// TODO: is using the function that was written for this purpose in the pallet the right way to go about this?
126+
let total_stake = pallet_subtensor::Pallet::<Runtime>::get_stake_for_coldkey(&coldkey);
127+
// Convert to EVM decimals
128+
let stake_u256 = U256::from(total_stake);
129+
let stake_eth =
130+
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
131+
.ok_or(ExitError::InvalidRange)?;
132+
133+
// Format output
127134
let mut result = [0_u8; 32];
128-
U256::to_big_endian(&result_u256, &mut result);
135+
U256::to_big_endian(&stake_eth, &mut result);
129136

130137
Ok(PrecompileOutput {
131138
exit_status: ExitSucceed::Returned,
@@ -138,9 +145,15 @@ impl StakingPrecompile {
138145

139146
// get total stake of hotkey
140147
let total_stake = pallet_subtensor::Pallet::<Runtime>::get_total_stake_for_hotkey(&hotkey);
141-
let result_u256 = U256::from(total_stake);
148+
// Convert to EVM decimals
149+
let stake_u256 = U256::from(total_stake);
150+
let stake_eth =
151+
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
152+
.ok_or(ExitError::InvalidRange)?;
153+
154+
// Format output
142155
let mut result = [0_u8; 32];
143-
U256::to_big_endian(&result_u256, &mut result);
156+
U256::to_big_endian(&stake_eth, &mut result);
144157

145158
Ok(PrecompileOutput {
146159
exit_status: ExitSucceed::Returned,

0 commit comments

Comments
 (0)