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
54 changes: 15 additions & 39 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 5 additions & 34 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3465,53 +3465,24 @@ 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.

Returns:
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,
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_tests/test_root_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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}")
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e_tests/test_staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/extrinsics/test_staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/extrinsics/test_unstaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading