From eb6924869e07ab8f90c83717380db5a1f1d25b47 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Thu, 12 Dec 2024 14:58:40 -0800 Subject: [PATCH 1/5] Adds methods to fetch stake --- bittensor/core/subtensor.py | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 441a4c033b..41c7a8b59b 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1268,6 +1268,59 @@ def neurons(self, netuid: int, block: Optional[int] = None) -> list["NeuronInfo" return neurons + def get_total_stake_for_coldkey( + self, ss58_address: str, block: Optional[int] = None + ) -> Optional["Balance"]: + """Retrieves the total stake held by a coldkey across all associated hotkeys, including delegated stakes. + + Args: + ss58_address (str): The SS58 address of the coldkey account. + block (Optional[int]): The blockchain block number at which to perform the query. + + Returns: + Optional[Balance]: The total stake amount held by the coldkey, or None if the query fails. + """ + result = self.query_subtensor("TotalColdkeyStake", block, [ss58_address]) + if not hasattr(result, "value") or result is None: + return None + return Balance.from_rao(result.value) + + def get_total_stake_for_hotkey( + self, ss58_address: str, block: Optional[int] = None + ) -> Optional["Balance"]: + """Retrieves the total stake associated with a hotkey. + + Args: + ss58_address (str): The SS58 address of the hotkey account. + block (Optional[int]): The blockchain block number at which to perform the query. + + Returns: + Optional[Balance]: The total stake amount held by the hotkey, or None if the query fails. + """ + result = self.query_subtensor("TotalHotkeyStake", block, [ss58_address]) + if not hasattr(result, "value") or result is None: + return None + return Balance.from_rao(result.value) + + def get_stake_for_coldkey_and_hotkey( + self, hotkey_ss58: str, coldkey_ss58: str, block: Optional[int] = None + ) -> Optional["Balance"]: + """Retrieves the stake amount for a specific coldkey-hotkey pair within the Bittensor network. + + Args: + hotkey_ss58 (str): The SS58 address of the hotkey account. + coldkey_ss58 (str): The SS58 address of the coldkey account. + block (Optional[int]): The blockchain block number at which to perform the query. + + Returns: + Optional[Balance]: The stake amount for the specific coldkey-hotkey pair, + or None if the query fails. + """ + result = self.query_subtensor("Stake", block, [hotkey_ss58, coldkey_ss58]) + if not hasattr(result, "value") or result is None: + return None + return Balance.from_rao(result.value) + def get_total_subnets(self, block: Optional[int] = None) -> Optional[int]: """ Retrieves the total number of subnets within the Bittensor network as of a specific blockchain block. From 70b020cc91c5152c47b5a7743f6fcd549625536c Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Thu, 12 Dec 2024 15:01:18 -0800 Subject: [PATCH 2/5] Cleanup --- bittensor/core/subtensor.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 41c7a8b59b..f39e590906 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1302,25 +1302,6 @@ def get_total_stake_for_hotkey( return None return Balance.from_rao(result.value) - def get_stake_for_coldkey_and_hotkey( - self, hotkey_ss58: str, coldkey_ss58: str, block: Optional[int] = None - ) -> Optional["Balance"]: - """Retrieves the stake amount for a specific coldkey-hotkey pair within the Bittensor network. - - Args: - hotkey_ss58 (str): The SS58 address of the hotkey account. - coldkey_ss58 (str): The SS58 address of the coldkey account. - block (Optional[int]): The blockchain block number at which to perform the query. - - Returns: - Optional[Balance]: The stake amount for the specific coldkey-hotkey pair, - or None if the query fails. - """ - result = self.query_subtensor("Stake", block, [hotkey_ss58, coldkey_ss58]) - if not hasattr(result, "value") or result is None: - return None - return Balance.from_rao(result.value) - def get_total_subnets(self, block: Optional[int] = None) -> Optional[int]: """ Retrieves the total number of subnets within the Bittensor network as of a specific blockchain block. From f4bbdbb22a9113f5e0aec196295fe03b9b69ea5f Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor <165814940+ibraheem-opentensor@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:06:24 -0800 Subject: [PATCH 3/5] Update bittensor/core/subtensor.py Co-authored-by: Benjamin Himes <37844818+thewhaleking@users.noreply.github.com> --- bittensor/core/subtensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index f39e590906..4b0a3a4103 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1281,7 +1281,7 @@ def get_total_stake_for_coldkey( Optional[Balance]: The total stake amount held by the coldkey, or None if the query fails. """ result = self.query_subtensor("TotalColdkeyStake", block, [ss58_address]) - if not hasattr(result, "value") or result is None: + if not getattr(result, "value", None) is None: return None return Balance.from_rao(result.value) From 2450d37e2d4306b15260789478e7d2e3f19f3b76 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor <165814940+ibraheem-opentensor@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:06:30 -0800 Subject: [PATCH 4/5] Update bittensor/core/subtensor.py Co-authored-by: Benjamin Himes <37844818+thewhaleking@users.noreply.github.com> --- bittensor/core/subtensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 4b0a3a4103..037f2b51bb 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1298,7 +1298,7 @@ def get_total_stake_for_hotkey( Optional[Balance]: The total stake amount held by the hotkey, or None if the query fails. """ result = self.query_subtensor("TotalHotkeyStake", block, [ss58_address]) - if not hasattr(result, "value") or result is None: + if not getattr(result, "value", None) is None: return None return Balance.from_rao(result.value) From 2f2f0b900bf2d9cb67d476f93368d933ca465bca Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Thu, 12 Dec 2024 15:08:33 -0800 Subject: [PATCH 5/5] Removes if not --- bittensor/core/subtensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 037f2b51bb..0b450ce2f0 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1281,7 +1281,7 @@ def get_total_stake_for_coldkey( Optional[Balance]: The total stake amount held by the coldkey, or None if the query fails. """ result = self.query_subtensor("TotalColdkeyStake", block, [ss58_address]) - if not getattr(result, "value", None) is None: + if getattr(result, "value", None) is None: return None return Balance.from_rao(result.value) @@ -1298,7 +1298,7 @@ def get_total_stake_for_hotkey( Optional[Balance]: The total stake amount held by the hotkey, or None if the query fails. """ result = self.query_subtensor("TotalHotkeyStake", block, [ss58_address]) - if not getattr(result, "value", None) is None: + if getattr(result, "value", None) is None: return None return Balance.from_rao(result.value)