From 5765d6c2cc9666f1aea935738934fed61f9b62c3 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Fri, 11 Oct 2024 17:39:44 +0200 Subject: [PATCH 1/4] Adds `--all` flag to transfer --- bittensor_cli/cli.py | 19 +++++++++++++++++-- .../bittensor/async_substrate_interface.py | 4 +--- .../src/bittensor/extrinsics/transfer.py | 4 ++++ bittensor_cli/src/commands/wallets.py | 8 +++++++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index b012c989e..7d3c142b2 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -1404,9 +1404,12 @@ def wallet_transfer( None, "--amount", "-a", - prompt=True, + prompt=False, help="Amount (in TAO) to transfer.", ), + transfer_all: bool = typer.Option( + False, "--all", prompt=False, help="Transfer all available balance." + ), wallet_name: str = Options.wallet_name, wallet_path: str = Options.wallet_path, wallet_hotkey: str = Options.wallet_hotkey, @@ -1446,9 +1449,21 @@ def wallet_transfer( validate=WV.WALLET, ) subtensor = self.initialize_chain(network) + if transfer_all and amount: + err_console.print("Cannot specify an amount and `--all`") + raise typer.Exit() + elif transfer_all: + amount = None + elif not amount: + amount = FloatPrompt.ask("Enter amount (in TAO) to transfer.") return self._run_command( wallets.transfer( - wallet, subtensor, destination_ss58_address, amount, prompt + wallet, + subtensor, + destination_ss58_address, + amount, + transfer_all, + prompt, ) ) diff --git a/bittensor_cli/src/bittensor/async_substrate_interface.py b/bittensor_cli/src/bittensor/async_substrate_interface.py index 0f4dd0d43..eb289bfd4 100644 --- a/bittensor_cli/src/bittensor/async_substrate_interface.py +++ b/bittensor_cli/src/bittensor/async_substrate_interface.py @@ -1707,9 +1707,7 @@ async def rpc_request( ) result = await self._make_rpc_request(payloads, runtime=runtime) if "error" in result[payload_id][0]: - raise SubstrateRequestException( - result[payload_id][0]["error"]["message"] - ) + raise SubstrateRequestException(result[payload_id][0]["error"]["message"]) if "result" in result[payload_id][0]: return result[payload_id][0] else: diff --git a/bittensor_cli/src/bittensor/extrinsics/transfer.py b/bittensor_cli/src/bittensor/extrinsics/transfer.py index 8ae37e9b6..da9af6f6f 100644 --- a/bittensor_cli/src/bittensor/extrinsics/transfer.py +++ b/bittensor_cli/src/bittensor/extrinsics/transfer.py @@ -23,6 +23,7 @@ async def transfer_extrinsic( wallet: Wallet, destination: str, amount: Balance, + transfer_all: bool = False, wait_for_inclusion: bool = True, wait_for_finalization: bool = False, keep_alive: bool = True, @@ -34,6 +35,7 @@ async def transfer_extrinsic( :param wallet: Bittensor wallet object to make transfer from. :param destination: Destination public key address (ss58_address or ed25519) of recipient. :param amount: Amount to stake as Bittensor balance. + :param transfer_all: Whether to transfer all funds from this wallet to the destination address. :param wait_for_inclusion: If set, waits for the extrinsic to enter a block before returning `True`, or returns `False` if the extrinsic fails to enter the block within the timeout. :param wait_for_finalization: If set, waits for the extrinsic to be finalized on the chain before returning @@ -135,6 +137,8 @@ async def do_transfer() -> tuple[bool, str, str]: existential_deposit = Balance(0) # Check if we have enough balance. + if transfer_all is True: + amount = account_balance - fee - existential_deposit if account_balance < (amount + fee + existential_deposit): err_console.print( ":cross_mark: [bold red]Not enough balance[/bold red]:\n\n" diff --git a/bittensor_cli/src/commands/wallets.py b/bittensor_cli/src/commands/wallets.py index a1dd22623..162c2184e 100644 --- a/bittensor_cli/src/commands/wallets.py +++ b/bittensor_cli/src/commands/wallets.py @@ -1256,11 +1256,17 @@ async def transfer( subtensor: SubtensorInterface, destination: str, amount: float, + transfer_all: bool, prompt: bool, ): """Transfer token of amount to destination.""" await transfer_extrinsic( - subtensor, wallet, destination, Balance.from_tao(amount), prompt=prompt + subtensor, + wallet, + destination, + Balance.from_tao(amount), + transfer_all, + prompt=prompt, ) From 78abae74ab0b162a8979577b41bbef78be74909c Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Fri, 11 Oct 2024 17:41:28 +0200 Subject: [PATCH 2/4] Conflict --- bittensor_cli/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 7d3c142b2..d4565f042 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -1453,7 +1453,7 @@ def wallet_transfer( err_console.print("Cannot specify an amount and `--all`") raise typer.Exit() elif transfer_all: - amount = None + amount = 0 elif not amount: amount = FloatPrompt.ask("Enter amount (in TAO) to transfer.") return self._run_command( From 648d07ff70450a751b02c2e0d0ed5d1d276ba691 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Fri, 11 Oct 2024 15:44:33 -0700 Subject: [PATCH 3/4] Adds check for -ve balance --- bittensor_cli/src/bittensor/extrinsics/transfer.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bittensor_cli/src/bittensor/extrinsics/transfer.py b/bittensor_cli/src/bittensor/extrinsics/transfer.py index da9af6f6f..588c8684e 100644 --- a/bittensor_cli/src/bittensor/extrinsics/transfer.py +++ b/bittensor_cli/src/bittensor/extrinsics/transfer.py @@ -15,6 +15,7 @@ format_error_message, get_explorer_url_for_network, is_valid_bittensor_address_or_public_key, + print_error, ) @@ -139,6 +140,10 @@ async def do_transfer() -> tuple[bool, str, str]: # Check if we have enough balance. if transfer_all is True: amount = account_balance - fee - existential_deposit + if amount < Balance(0): + print_error("Not enough balance to transfer") + return False + if account_balance < (amount + fee + existential_deposit): err_console.print( ":cross_mark: [bold red]Not enough balance[/bold red]:\n\n" From 0e9f0e8b1143224825fecbca9064378d0eb7ae24 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Fri, 11 Oct 2024 15:51:07 -0700 Subject: [PATCH 4/4] Uses print_error --- bittensor_cli/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index d4565f042..b3cc16ff7 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -1450,7 +1450,7 @@ def wallet_transfer( ) subtensor = self.initialize_chain(network) if transfer_all and amount: - err_console.print("Cannot specify an amount and `--all`") + print_error("Cannot specify an amount and '--all' flag.") raise typer.Exit() elif transfer_all: amount = 0 @@ -3284,7 +3284,7 @@ def stake_add( self.verbosity_handler(quiet, verbose) if stake_all and amount: - err_console.print( + print_error( "Cannot specify an amount and 'stake-all'. Choose one or the other." ) raise typer.Exit()