diff --git a/bittensor/core/async_subtensor.py b/bittensor/core/async_subtensor.py index 9998029fa3..d199c029fc 100644 --- a/bittensor/core/async_subtensor.py +++ b/bittensor/core/async_subtensor.py @@ -4191,54 +4191,30 @@ async def get_stake( reuse_block: bool = False, ) -> Balance: """ - Returns the stake under a coldkey - hotkey pairing. + Returns the amount of Alpha staked by a specific coldkey to a specific hotkey within a given subnet. Parameters: - hotkey_ss58: The SS58 address of the hotkey. - coldkey_ss58: The SS58 address of the coldkey. - netuid: The subnet ID. - block: The block number at which to query the stake information. - block_hash: The hash of the block to retrieve the stake from. Do not specify if using block - or reuse_block - reuse_block: Whether to use the last-used block. Do not set if using `block_hash` or `block`. + coldkey_ss58: The SS58 address of the coldkey that delegated the stake. This address owns the stake. + hotkey_ss58: The SS58 address of the hotkey which the stake is on. + netuid: The unique identifier of the subnet to query. + block: The specific block number at which to retrieve the stake information. + or `reuse_block`. + block_hash: The hash of the block to retrieve the stake from. Do not specify if using `block` + or `reuse_block`. + reuse_block: Whether to use the last-used block hash. Do not set if using `block_hash` or `block`. Returns: - Balance: The stake under the coldkey - hotkey pairing. + An object representing the amount of Alpha (TAO ONLY if the subnet's netuid is 0) currently staked from the + specified coldkey to the specified hotkey within the given subnet. """ block_hash = await self.determine_block_hash(block, block_hash, reuse_block) - - alpha_shares = await self.query_subtensor( - name="Alpha", - block=block, - block_hash=block_hash, - reuse_block=reuse_block, + result = await self.query_runtime_api( + runtime_api="StakeInfoRuntimeApi", + method="get_stake_info_for_hotkey_coldkey_netuid", params=[hotkey_ss58, coldkey_ss58, netuid], - ) - hotkey_alpha_result = await self.query_subtensor( - name="TotalHotkeyAlpha", - block=block, - block_hash=block_hash, - reuse_block=reuse_block, - params=[hotkey_ss58, netuid], - ) - hotkey_shares = await self.query_subtensor( - name="TotalHotkeyShares", - block=block, block_hash=block_hash, - reuse_block=reuse_block, - params=[hotkey_ss58, netuid], ) - - hotkey_alpha = getattr(hotkey_alpha_result, "value", hotkey_alpha_result) - alpha_shares_as_float = fixed_to_float(alpha_shares) - hotkey_shares_as_float = fixed_to_float(hotkey_shares) - - if hotkey_shares_as_float == 0: - return Balance.from_rao(0).set_unit(netuid=netuid) - - stake = alpha_shares_as_float / hotkey_shares_as_float * cast(int, hotkey_alpha) - - return Balance.from_rao(int(stake)).set_unit(netuid=netuid) + return StakeInfo.from_dict(result).stake async def get_stake_add_fee( self, diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index dcb6b25805..53f594e4f8 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -3465,11 +3465,10 @@ def get_stake( ) -> Balance: """ Returns the amount of Alpha staked by a specific coldkey to a specific hotkey within a given subnet. - This function retrieves the delegated stake balance, referred to as the 'Alpha' value. Parameters: coldkey_ss58: The SS58 address of the coldkey that delegated the stake. This address owns the stake. - hotkey_ss58: The ss58 address of the hotkey which the stake is on. + hotkey_ss58: The SS58 address of the hotkey which the stake is on. netuid: The unique identifier of the subnet to query. block: The specific block number at which to retrieve the stake information. @@ -3477,41 +3476,13 @@ def get_stake( An object representing the amount of Alpha (TAO ONLY if the subnet's netuid is 0) currently staked from the specified coldkey to the specified hotkey within the given subnet. """ - alpha_shares_query = self.query_module( - module="SubtensorModule", - name="Alpha", - block=block, + result = self.query_runtime_api( + runtime_api="StakeInfoRuntimeApi", + method="get_stake_info_for_hotkey_coldkey_netuid", params=[hotkey_ss58, coldkey_ss58, netuid], - ) - alpha_shares = alpha_shares_query - - hotkey_alpha_obj = cast( - ScaleObj, - self.query_module( - module="SubtensorModule", - name="TotalHotkeyAlpha", - block=block, - params=[hotkey_ss58, netuid], - ), - ) - hotkey_alpha = hotkey_alpha_obj.value - - hotkey_shares = self.query_module( - module="SubtensorModule", - name="TotalHotkeyShares", block=block, - params=[hotkey_ss58, netuid], ) - - alpha_shares_as_float = fixed_to_float(alpha_shares) - hotkey_shares_as_float = fixed_to_float(hotkey_shares) - - if hotkey_shares_as_float == 0: - return Balance.from_rao(0).set_unit(netuid=netuid) - - stake = alpha_shares_as_float / hotkey_shares_as_float * hotkey_alpha - - return Balance.from_rao(int(stake)).set_unit(netuid=netuid) + return StakeInfo.from_dict(result).stake def get_stake_for_coldkey_and_hotkey( self, diff --git a/tests/e2e_tests/test_root_claim.py b/tests/e2e_tests/test_root_claim.py index 77a87b30a2..012a0858ac 100644 --- a/tests/e2e_tests/test_root_claim.py +++ b/tests/e2e_tests/test_root_claim.py @@ -382,7 +382,7 @@ def test_root_claim_keep_with_zero_num_root_auto_claims( hotkey_ss58=alice_wallet.hotkey.ss58_address, netuid=sn2.netuid, ) - assert stake_after_charlie >= claimable_stake_before_charlie + assert stake_after_charlie >= claimable_stake_before_charlie - Balance.from_rao(1, sn2.netuid) logging.console.info(f"[blue]Charlie after:[/blue]") logging.console.info(f"RootClaimed: {claimed_after_charlie}") @@ -559,7 +559,7 @@ async def test_root_claim_keep_with_zero_num_root_auto_claims_async( hotkey_ss58=alice_wallet.hotkey.ss58_address, netuid=sn2.netuid, ) - assert stake_after_charlie >= claimable_stake_before_charlie + assert stake_after_charlie >= claimable_stake_before_charlie - Balance.from_rao(1, sn2.netuid) logging.console.info(f"[blue]Charlie after:[/blue]") logging.console.info(f"RootClaimed: {claimed_after_charlie}") diff --git a/tests/e2e_tests/test_staking.py b/tests/e2e_tests/test_staking.py index b95879993b..f0010f234f 100644 --- a/tests/e2e_tests/test_staking.py +++ b/tests/e2e_tests/test_staking.py @@ -444,8 +444,8 @@ def test_batch_operations(subtensor, alice_wallet, bob_wallet): ) assert CloseInValue( # Make sure we are within 0.0002 TAO due to tx fees - balances[bob_wallet.coldkey.ss58_address], Balance.from_rao(1_500_000) - ) == Balance.from_tao(999_999.7956) + balances[bob_wallet.coldkey.ss58_address], Balance.from_rao(5_000_000) + ) == Balance.from_tao(999_999.7979) assert balances[alice_wallet.coldkey.ss58_address] > alice_balance @@ -570,8 +570,8 @@ async def test_batch_operations_async(async_subtensor, alice_wallet, bob_wallet) ) assert CloseInValue( # Make sure we are within 0.0002 TAO due to tx fees - balances[bob_wallet.coldkey.ss58_address], Balance.from_rao(1_500_000) - ) == Balance.from_tao(999_999.7956) + balances[bob_wallet.coldkey.ss58_address], Balance.from_rao(5_000_000) + ) == Balance.from_tao(999_999.7979) assert balances[alice_wallet.coldkey.ss58_address] > alice_balance diff --git a/tests/unit_tests/extrinsics/test_staking.py b/tests/unit_tests/extrinsics/test_staking.py index c354daf0db..6ac282605b 100644 --- a/tests/unit_tests/extrinsics/test_staking.py +++ b/tests/unit_tests/extrinsics/test_staking.py @@ -181,6 +181,7 @@ def test_add_stake_multiple_extrinsic(subtensor, mocker, fake_wallet): "get_stake_info_for_coldkey", return_value=[Balance(1.1), Balance(0.3)], ) + mocker.patch.object(subtensor, "get_stake", return_value=Balance.from_tao(2.2)) mocker.patch.object(subtensor, "get_balance", return_value=Balance.from_tao(10)) mocker.patch.object( staking, "get_old_stakes", return_value=[Balance(1.1), Balance(0.3)] diff --git a/tests/unit_tests/extrinsics/test_unstaking.py b/tests/unit_tests/extrinsics/test_unstaking.py index f5c6f578a9..710d052062 100644 --- a/tests/unit_tests/extrinsics/test_unstaking.py +++ b/tests/unit_tests/extrinsics/test_unstaking.py @@ -119,7 +119,8 @@ def test_unstake_multiple_extrinsic(subtensor, fake_wallet, mocker): mocked_balance = mocker.patch.object( subtensor, "get_balance", return_value=Balance.from_tao(1.0) ) - mocked_get_stake_for_coldkey_and_hotkey = mocker.patch.object( + mocker.patch.object(subtensor, "get_stake", return_value=Balance.from_tao(8.9)) + mocker.patch.object( subtensor, "get_stake_for_coldkey_and_hotkey", return_value=[Balance(10.0)] ) mocked_unstake_extrinsic = mocker.patch.object(