Skip to content

Commit

Permalink
PR Feedback — added docstrings.
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhaleking committed Aug 13, 2024
1 parent 3464323 commit a9ab679
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class Options:


def list_prompt(init_var: list, list_type: type, help_text: str) -> list:
"""
Serves a similar purpose to rich.FloatPrompt or rich.Prompt, but for creating a list of those variables for
a given type
:param init_var: starting variable, this will generally be `None` if you intend to get something out of this
prompt, if it is not empty, it will return the same
:param list_type: the type for each item in the list you're creating
:param help_text: the helper text to display to the user in the prompt
:return: list of the specified type of the user inputs
"""
while not init_var:
prompt = Prompt.ask(help_text)
init_var = [list_type(x) for x in re.split(r"[ ,]+", prompt) if x]
Expand Down
1 change: 1 addition & 0 deletions src/commands/stake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,7 @@ async def set_children(
children: list[str],
proportions: list[float],
):
"""Set children hotkeys."""
# Validate children SS58 addresses
for child in children:
if not is_valid_ss58_address(child):
Expand Down
27 changes: 26 additions & 1 deletion src/subtensor_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,15 @@ async def is_hotkey_delegate(

async def get_delegates(
self, block_hash: Optional[str] = None, reuse_block: Optional[bool] = False
):
) -> list[DelegateInfo]:
"""
Fetches all delegates on the chain
:param block_hash: hash of the blockchain block number for the query.
:param reuse_block: whether to reuse the last-used block hash.
:return: List of DelegateInfo objects, or an empty list if there are no delegates.
"""
json_body = await self.substrate.rpc_request(
method="delegateInfo_getDelegates", # custom rpc method
params=[block_hash] if block_hash else [],
Expand Down Expand Up @@ -184,6 +192,13 @@ async def get_stake_info_for_coldkey(
async def get_stake_for_coldkey_and_hotkey(
self, hotkey_ss58: str, coldkey_ss58: str, block_hash: Optional[str]
) -> Balance:
"""
Retrieves stake information associated with a specific coldkey and hotkey.
:param hotkey_ss58: the hotkey SS58 address to query
:param coldkey_ss58: the coldkey SS58 address to query
:param block_hash: the hash of the blockchain block number for the query.
:return: Stake Balance for the given coldkey and hotkey
"""
_result = await self.substrate.query(
module="SubtensorModule",
storage_function="Stake",
Expand Down Expand Up @@ -738,6 +753,16 @@ async def sign_and_send_extrinsic(
wait_for_inclusion: bool = True,
wait_for_finalization: bool = False,
) -> tuple[bool, str]:
"""
Helper method to sign and submit an extrinsic call to chain.
:param call: a prepared Call object
:param wallet: the wallet whose coldkey will be used to sign the extrinsic
:param wait_for_inclusion: whether to wait until the extrinsic call is included on the chain
:param wait_for_finalization: whether to wait until the extrinsic call is finalized on the chain
:return: (success, error message)
"""
extrinsic = await self.substrate.create_signed_extrinsic(
call=call, keypair=wallet.coldkey
) # sign with coldkey
Expand Down
18 changes: 16 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import math
from pathlib import Path
from typing import Union, Any, Collection
from typing import Union, Any, Collection, Optional

import aiohttp
import scalecodec
Expand All @@ -26,10 +26,12 @@


def u16_normalized_float(x: int) -> float:
"""Converts a u16 int to a float"""
return float(x) / float(U16_MAX)


def float_to_u64(value: float) -> int:
"""Converts a float to a u64 int"""
# Ensure the input is within the expected range
if not (0 <= value < 1):
raise ValueError("Input value must be between 0 and 1")
Expand Down Expand Up @@ -116,7 +118,16 @@ def convert_root_weight_uids_and_vals_to_tensor(

def get_hotkey_wallets_for_wallet(
wallet: Wallet, show_nulls: bool = False
) -> list[Wallet]:
) -> list[Optional[Wallet]]:
"""
Returns wallet objects with hotkeys for a single given wallet
:param wallet: Wallet object to use for the path
:param show_nulls: will add `None` into the output if a hotkey is encrypted or not on the device
:return: a list of wallets (with Nones included for cases of a hotkey being encrypted or not on the device, if
`show_nulls` is set to `True`)
"""
hotkey_wallets = []
wallet_path = Path(wallet.path).expanduser()
hotkeys_path = wallet_path / wallet.name / "hotkeys"
Expand Down Expand Up @@ -146,6 +157,7 @@ def get_hotkey_wallets_for_wallet(


def get_coldkey_wallets_for_path(path: str) -> list[Wallet]:
"""Gets all wallets with coldkeys from a given path"""
wallet_path = Path(path).expanduser()
wallets = [
Wallet(name=directory.name, path=path)
Expand All @@ -156,6 +168,7 @@ def get_coldkey_wallets_for_path(path: str) -> list[Wallet]:


def get_all_wallets_for_path(path: str) -> list[Wallet]:
"""Gets all wallets from a given path."""
all_wallets = []
cold_wallets = get_coldkey_wallets_for_path(path)
for cold_wallet in cold_wallets:
Expand Down Expand Up @@ -259,6 +272,7 @@ def is_valid_bittensor_address_or_public_key(address: Union[str, bytes]) -> bool


def decode_scale_bytes(return_type, scale_bytes, custom_rpc_type_registry):
"""Decodes a ScaleBytes object using our type registry and return type"""
rpc_runtime_config = RuntimeConfiguration()
rpc_runtime_config.update_type_registry(load_type_registry_preset("legacy"))
rpc_runtime_config.update_type_registry(custom_rpc_type_registry)
Expand Down

0 comments on commit a9ab679

Please sign in to comment.