From 8177ec55cbec14c4d96c80cd78286b866c7041e9 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Tue, 8 Oct 2024 17:42:17 -0700 Subject: [PATCH 1/4] Adds support for ss58 addresses in wallet balance --- bittensor_cli/cli.py | 30 ++++++++++++++++++++------- bittensor_cli/src/commands/wallets.py | 15 +++++++++++--- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 669c354b4..6fc932497 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -129,7 +129,9 @@ class Options: flag_value=False, ) public_hex_key = typer.Option(None, help="The public key in hex format.") - ss58_address = typer.Option(None, help="The SS58 address of the coldkey.") + ss58_address = typer.Option( + None, "--ss58", "--ss58-address", help="The SS58 address of the coldkey." + ) overwrite_coldkey = typer.Option( False, help="Overwrite the old coldkey with the newly generated coldkey.", @@ -2026,6 +2028,7 @@ def wallet_balance( wallet_name: Optional[str] = Options.wallet_name, wallet_path: Optional[str] = Options.wallet_path, wallet_hotkey: Optional[str] = Options.wallet_hotkey, + ss58_address: Optional[str] = Options.ss58_address, all_balances: Optional[bool] = typer.Option( False, "--all", @@ -2055,14 +2058,27 @@ def wallet_balance( """ self.verbosity_handler(quiet, verbose) - ask_for = [WO.PATH] if all_balances else [WO.NAME, WO.PATH] - validate = WV.NONE if all_balances else WV.WALLET - wallet = self.wallet_ask( - wallet_name, wallet_path, wallet_hotkey, ask_for=ask_for, validate=validate - ) + if ss58_address: + if is_valid_ss58_address(ss58_address): + wallet = None + else: + print_error( + "You have entered an incorrect ss58 address. Please try again" + ) + raise typer.Exit() + else: + ask_for = [WO.PATH] if all_balances else [WO.NAME, WO.PATH] + validate = WV.NONE if all_balances else WV.WALLET + wallet = self.wallet_ask( + wallet_name, + wallet_path, + wallet_hotkey, + ask_for=ask_for, + validate=validate, + ) subtensor = self.initialize_chain(network) return self._run_command( - wallets.wallet_balance(wallet, subtensor, all_balances) + wallets.wallet_balance(wallet, subtensor, all_balances, ss58_address) ) def wallet_history( diff --git a/bittensor_cli/src/commands/wallets.py b/bittensor_cli/src/commands/wallets.py index dd91c1999..b95f5ee9b 100644 --- a/bittensor_cli/src/commands/wallets.py +++ b/bittensor_cli/src/commands/wallets.py @@ -221,16 +221,25 @@ def _get_coldkey_ss58_addresses_for_path(path: str) -> tuple[list[str], list[str async def wallet_balance( - wallet: Wallet, subtensor: SubtensorInterface, all_balances: bool + wallet: Optional[Wallet], + subtensor: SubtensorInterface, + all_balances: bool, + ss58_address: Optional[str] = None, ): """Retrieves the current balance of the specified wallet""" - if not all_balances: + if ss58_address: + coldkeys = [ss58_address] + wallet_names = ["Provided Address"] + + elif not all_balances: if not wallet.coldkeypub_file.exists_on_device(): err_console.print("[bold red]No wallets found.[/bold red]") return with console.status("Retrieving balances", spinner="aesthetic") as status: - if all_balances: + if ss58_address: + print_verbose(f"Fetching data for ss58 address: {ss58_address}", status) + elif all_balances: print_verbose("Fetching data for all wallets", status) coldkeys, wallet_names = _get_coldkey_ss58_addresses_for_path(wallet.path) else: From fa97cc7a5d380f85642982670808df30adb216b1 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 9 Oct 2024 11:41:23 -0700 Subject: [PATCH 2/4] Adds support for multiple coldkeys --- bittensor_cli/cli.py | 16 +++++++++++----- bittensor_cli/src/commands/wallets.py | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 6fc932497..91d2e2b78 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -2028,7 +2028,7 @@ def wallet_balance( wallet_name: Optional[str] = Options.wallet_name, wallet_path: Optional[str] = Options.wallet_path, wallet_hotkey: Optional[str] = Options.wallet_hotkey, - ss58_address: Optional[str] = Options.ss58_address, + ss58_address: Optional[list[str]] = Options.ss58_address, all_balances: Optional[bool] = typer.Option( False, "--all", @@ -2059,12 +2059,18 @@ def wallet_balance( self.verbosity_handler(quiet, verbose) if ss58_address: - if is_valid_ss58_address(ss58_address): + valid_ss58s = [ + ss58 for ss58 in set(ss58_address) if is_valid_ss58_address(ss58) + ] + + invalid_ss58s = set(ss58_address) - set(valid_ss58s) + for invalid_ss58 in invalid_ss58s: + print_error(f"Incorrect ss58 address: {invalid_ss58}. Skipping.") + + if valid_ss58s: wallet = None + ss58_address = valid_ss58s else: - print_error( - "You have entered an incorrect ss58 address. Please try again" - ) raise typer.Exit() else: ask_for = [WO.PATH] if all_balances else [WO.NAME, WO.PATH] diff --git a/bittensor_cli/src/commands/wallets.py b/bittensor_cli/src/commands/wallets.py index b95f5ee9b..f7d12456c 100644 --- a/bittensor_cli/src/commands/wallets.py +++ b/bittensor_cli/src/commands/wallets.py @@ -228,8 +228,8 @@ async def wallet_balance( ): """Retrieves the current balance of the specified wallet""" if ss58_address: - coldkeys = [ss58_address] - wallet_names = ["Provided Address"] + coldkeys = ss58_address + wallet_names = [f"Provided Address {i + 1}" for i in range(len(ss58_address))] elif not all_balances: if not wallet.coldkeypub_file.exists_on_device(): From 306fd30c6a30d33117b8d2b0b43ad9538576b60e Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 9 Oct 2024 11:46:44 -0700 Subject: [PATCH 3/4] Renames ss58_address -> ss58_addresses --- bittensor_cli/cli.py | 12 ++++++------ bittensor_cli/src/commands/wallets.py | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 91d2e2b78..cc7b55cfc 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -2028,7 +2028,7 @@ def wallet_balance( wallet_name: Optional[str] = Options.wallet_name, wallet_path: Optional[str] = Options.wallet_path, wallet_hotkey: Optional[str] = Options.wallet_hotkey, - ss58_address: Optional[list[str]] = Options.ss58_address, + ss58_addresses: Optional[list[str]] = Options.ss58_address, all_balances: Optional[bool] = typer.Option( False, "--all", @@ -2058,18 +2058,18 @@ def wallet_balance( """ self.verbosity_handler(quiet, verbose) - if ss58_address: + if ss58_addresses: valid_ss58s = [ - ss58 for ss58 in set(ss58_address) if is_valid_ss58_address(ss58) + ss58 for ss58 in set(ss58_addresses) if is_valid_ss58_address(ss58) ] - invalid_ss58s = set(ss58_address) - set(valid_ss58s) + invalid_ss58s = set(ss58_addresses) - set(valid_ss58s) for invalid_ss58 in invalid_ss58s: print_error(f"Incorrect ss58 address: {invalid_ss58}. Skipping.") if valid_ss58s: wallet = None - ss58_address = valid_ss58s + ss58_addresses = valid_ss58s else: raise typer.Exit() else: @@ -2084,7 +2084,7 @@ def wallet_balance( ) subtensor = self.initialize_chain(network) return self._run_command( - wallets.wallet_balance(wallet, subtensor, all_balances, ss58_address) + wallets.wallet_balance(wallet, subtensor, all_balances, ss58_addresses) ) def wallet_history( diff --git a/bittensor_cli/src/commands/wallets.py b/bittensor_cli/src/commands/wallets.py index f7d12456c..a1dd22623 100644 --- a/bittensor_cli/src/commands/wallets.py +++ b/bittensor_cli/src/commands/wallets.py @@ -224,12 +224,12 @@ async def wallet_balance( wallet: Optional[Wallet], subtensor: SubtensorInterface, all_balances: bool, - ss58_address: Optional[str] = None, + ss58_addresses: Optional[str] = None, ): """Retrieves the current balance of the specified wallet""" - if ss58_address: - coldkeys = ss58_address - wallet_names = [f"Provided Address {i + 1}" for i in range(len(ss58_address))] + if ss58_addresses: + coldkeys = ss58_addresses + wallet_names = [f"Provided Address {i + 1}" for i in range(len(ss58_addresses))] elif not all_balances: if not wallet.coldkeypub_file.exists_on_device(): @@ -237,8 +237,8 @@ async def wallet_balance( return with console.status("Retrieving balances", spinner="aesthetic") as status: - if ss58_address: - print_verbose(f"Fetching data for ss58 address: {ss58_address}", status) + if ss58_addresses: + print_verbose(f"Fetching data for ss58 address: {ss58_addresses}", status) elif all_balances: print_verbose("Fetching data for all wallets", status) coldkeys, wallet_names = _get_coldkey_ss58_addresses_for_path(wallet.path) From 4c8407a81c964320c78b712e86c2def112c13948 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Wed, 9 Oct 2024 12:17:22 -0700 Subject: [PATCH 4/4] Updates docs --- bittensor_cli/cli.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index cc7b55cfc..b78c831ab 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -2042,6 +2042,8 @@ def wallet_balance( """ Check the balance of the wallet. This command shows a detailed view of the wallet's coldkey balances, including free and staked balances. + You can also pass multiple ss58 addresses of coldkeys to check their balance (using --ss58). + EXAMPLES: - To display the balance of a single wallet, use the command with the `--wallet-name` argument and provide the wallet name: @@ -2055,6 +2057,11 @@ def wallet_balance( - To display the balances of all your wallets, use the `--all` argument: [green]$[/green] btcli w balance --all + + - To display the balances of ss58 addresses, use the `--ss58` argument: + + [green]$[/green] btcli w balance --ss58 --ss58 + """ self.verbosity_handler(quiet, verbose)