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
64 changes: 57 additions & 7 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
u16_normalized_float,
_decode_hex_identity_dict,
Certificate,
u64_normalized_float,
)
from bittensor.utils.balance import (
Balance,
Expand Down Expand Up @@ -891,7 +892,7 @@ async def get_children(
block: Optional[int] = None,
block_hash: Optional[str] = None,
reuse_block: bool = False,
) -> tuple[bool, list, str]:
) -> tuple[bool, list[tuple[float, str]], str]:
"""
This method retrieves the children of a given hotkey and netuid. It queries the SubtensorModule's ChildKeys
storage function to get the children and formats them before returning as a tuple.
Expand Down Expand Up @@ -921,8 +922,8 @@ async def get_children(
for proportion, child in children.value:
# Convert U64 to int
formatted_child = decode_account_id(child[0])
int_proportion = int(proportion)
formatted_children.append((int_proportion, formatted_child))
normalized_proportion = u64_normalized_float(proportion)
formatted_children.append((normalized_proportion, formatted_child))
return True, formatted_children, ""
else:
return True, [], ""
Expand Down Expand Up @@ -1665,6 +1666,40 @@ async def get_stake_for_coldkey(

get_stake_info_for_coldkey = get_stake_for_coldkey

async def get_stake_for_hotkey(
self,
hotkey_ss58: str,
netuid: int,
block: Optional[int] = None,
block_hash: Optional[str] = None,
reuse_block: bool = False,
) -> Balance:
"""
Retrieves the stake information for a given hotkey.

Args:
hotkey_ss58: The SS58 address of the hotkey.
netuid: The subnet ID to query for.
block: The block number at which to query the stake information. Do not specify if also specifying
block_hash or reuse_block
block_hash: The hash of the blockchain block number for the query. Do not specify if also specifying block
or reuse_block
reuse_block: Whether to reuse for this query the last-used block. Do not specify if also specifying block
or block_hash.
"""
hotkey_alpha_query = await self.query_subtensor(
name="TotalHotkeyAlpha",
params=[hotkey_ss58, netuid],
block=block,
block_hash=block_hash,
reuse_block=reuse_block,
)
balance = Balance.from_rao(hotkey_alpha_query.value)
balance.set_unit(netuid=netuid)
return balance

get_hotkey_stake = get_stake_for_hotkey

async def get_subnet_burn_cost(
self,
block: Optional[int] = None,
Expand Down Expand Up @@ -2683,6 +2718,9 @@ async def sign_and_send_extrinsic(
wait_for_inclusion: bool = True,
wait_for_finalization: bool = False,
sign_with: str = "coldkey",
use_nonce: bool = False,
period: Optional[int] = None,
nonce_key: str = "hotkey",
) -> tuple[bool, str]:
"""
Helper method to sign and submit an extrinsic call to chain.
Expand All @@ -2697,14 +2735,26 @@ async def sign_and_send_extrinsic(
Returns:
(success, error message)
"""
if sign_with not in ("coldkey", "hotkey", "coldkeypub"):
possible_keys = ("coldkey", "hotkey", "coldkeypub")
if sign_with not in possible_keys:
raise AttributeError(
f"'sign_with' must be either 'coldkey', 'hotkey' or 'coldkeypub', not '{sign_with}'"
)
signing_keypair = getattr(wallet, sign_with)
extrinsic_data = {"call": call, "keypair": signing_keypair}
if use_nonce:
if nonce_key not in possible_keys:
raise AttributeError(
f"'nonce_key' must be either 'coldkey', 'hotkey' or 'coldkeypub', not '{nonce_key}'"
)
next_nonce = await self.substrate.get_account_next_index(
getattr(wallet, nonce_key).ss58_address
)
extrinsic_data["nonce"] = next_nonce
if period is not None:
extrinsic_data["era"] = {"period": period}

extrinsic = await self.substrate.create_signed_extrinsic(
call=call, keypair=getattr(wallet, sign_with)
)
extrinsic = await self.substrate.create_signed_extrinsic(**extrinsic_data)
try:
response = await self.substrate.submit_extrinsic(
extrinsic,
Expand Down
4 changes: 3 additions & 1 deletion bittensor/core/chain_data/dynamic_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from bittensor.core.chain_data.utils import decode_account_id

from bittensor.core.chain_data.subnet_identity import SubnetIdentity
from bittensor.utils.balance import Balance
from bittensor.utils.balance import Balance, fixed_to_float


@dataclass
Expand Down Expand Up @@ -38,6 +38,7 @@ class DynamicInfo(InfoBase):
network_registered_at: int
subnet_volume: Balance
subnet_identity: Optional[SubnetIdentity]
moving_price: float

@classmethod
def _from_dict(cls, decoded: dict) -> "DynamicInfo":
Expand Down Expand Up @@ -120,6 +121,7 @@ def _from_dict(cls, decoded: dict) -> "DynamicInfo":
network_registered_at=int(decoded["network_registered_at"]),
subnet_identity=subnet_identity,
subnet_volume=subnet_volume,
moving_price=fixed_to_float(decoded["moving_price"]),
)

def tao_to_alpha(self, tao: Union[Balance, float, int]) -> Balance:
Expand Down
16 changes: 14 additions & 2 deletions bittensor/core/extrinsics/asyncex/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ async def add_stake_extrinsic(
},
)
staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
nonce_key="coldkeypub",
sign_with="coldkey",
use_nonce=True,
)
if staking_response is True: # If we successfully staked.
# We only wait here if we expect finalization.
Expand Down Expand Up @@ -296,7 +302,13 @@ async def add_stake_multiple_extrinsic(
},
)
staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
nonce_key="coldkeypub",
sign_with="coldkey",
use_nonce=True,
)

if staking_response is True: # If we successfully staked.
Expand Down
17 changes: 15 additions & 2 deletions bittensor/core/extrinsics/asyncex/unstaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ async def unstake_extrinsic(
},
)
staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
nonce_key="coldkeypub",
sign_with="coldkey",
use_nonce=True,
)

if staking_response is True: # If we successfully unstaked.
Expand Down Expand Up @@ -261,8 +267,15 @@ async def unstake_multiple_extrinsic(
"netuid": netuid,
},
)

staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
nonce_key="coldkeypub",
sign_with="coldkey",
use_nonce=True,
)

if staking_response is True: # If we successfully unstaked.
Expand Down
67 changes: 25 additions & 42 deletions bittensor/core/extrinsics/asyncex/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,6 @@
from bittensor_wallet import Wallet
from bittensor.core.async_subtensor import AsyncSubtensor
from bittensor.utils.registration import torch
from scalecodec.types import GenericCall


async def sign_and_send_with_nonce(
subtensor: "AsyncSubtensor",
call: "GenericCall",
wallet: "Wallet",
wait_for_inclusion: bool,
wait_for_finalization: bool,
period: Optional[int] = None,
):
"""
Signs an extrinsic call with the wallet hotkey, adding an optional era for period
"""
next_nonce = await subtensor.substrate.get_account_next_index(
wallet.hotkey.ss58_address
)

extrinsic_data = {"call": call, "keypair": wallet.hotkey, "nonce": next_nonce}
if period is not None:
extrinsic_data["era"] = {"period": period}

extrinsic = await subtensor.substrate.create_signed_extrinsic(**extrinsic_data)
response = await subtensor.substrate.submit_extrinsic(
extrinsic=extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

if not wait_for_finalization and not wait_for_inclusion:
return True, None

if await response.is_success:
return True, None

return False, format_error_message(await response.error_message)


async def _do_commit_weights(
Expand Down Expand Up @@ -87,8 +51,14 @@ async def _do_commit_weights(
"commit_hash": commit_hash,
},
)
return await sign_and_send_with_nonce(
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
return await subtensor.sign_and_send_extrinsic(
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
use_nonce=True,
nonce_key="hotkey",
sign_with="hotkey",
)


Expand Down Expand Up @@ -184,8 +154,14 @@ async def _do_reveal_weights(
"version_key": version_key,
},
)
return await sign_and_send_with_nonce(
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
return await subtensor.sign_and_send_extrinsic(
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
sign_with="hotkey",
nonce_key="hotkey",
use_nonce=True,
)


Expand Down Expand Up @@ -290,8 +266,15 @@ async def _do_set_weights(
"version_key": version_key,
},
)
return await sign_and_send_with_nonce(
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization, period
return await subtensor.sign_and_send_extrinsic(
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
period=period,
use_nonce=True,
nonce_key="hotkey",
sign_with="hotkey",
)


Expand Down
45 changes: 16 additions & 29 deletions bittensor/core/extrinsics/commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,6 @@
from bittensor.core.subtensor import Subtensor


def sign_and_send_with_nonce(
subtensor: "Subtensor", call, wallet, wait_for_inclusion, wait_for_finalization
):
next_nonce = subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address)
extrinsic = subtensor.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
nonce=next_nonce,
)
response = subtensor.substrate.submit_extrinsic(
extrinsic=extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

if not wait_for_finalization and not wait_for_inclusion:
return True, None

if response.is_success:
return True, None

return False, format_error_message(response.error_message)


def _do_commit_weights(
subtensor: "Subtensor",
wallet: "Wallet",
Expand Down Expand Up @@ -68,9 +44,14 @@ def _do_commit_weights(
"commit_hash": commit_hash,
},
)

return sign_and_send_with_nonce(
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
return subtensor.sign_and_send_extrinsic(
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
use_nonce=True,
sign_with="hotkey",
nonce_key="hotkey",
)


Expand Down Expand Up @@ -164,8 +145,14 @@ def _do_reveal_weights(
"version_key": version_key,
},
)
return sign_and_send_with_nonce(
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
return subtensor.sign_and_send_extrinsic(
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
use_nonce=True,
sign_with="hotkey",
nonce_key="hotkey",
)


Expand Down
16 changes: 14 additions & 2 deletions bittensor/core/extrinsics/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ def add_stake_extrinsic(
},
)
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
use_nonce=True,
sign_with="coldkey",
nonce_key="coldkeypub",
)
if staking_response is True: # If we successfully staked.
# We only wait here if we expect finalization.
Expand Down Expand Up @@ -280,7 +286,13 @@ def add_stake_multiple_extrinsic(
},
)
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
use_nonce=True,
nonce_key="coldkeypub",
sign_with="coldkey",
)

if staking_response is True: # If we successfully staked.
Expand Down
Loading
Loading