From 5c89e8ce4a21b7fe7564c9e4f556d35ce9497994 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Thu, 10 Oct 2024 14:17:43 +0200 Subject: [PATCH 1/2] Adds a utils app with a single subcommand (convert) to easily convert between rao and tao. --- bittensor_cli/cli.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 64202e17a..3571743b8 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -26,6 +26,7 @@ Constants, ) from bittensor_cli.src.bittensor import utils +from bittensor_cli.src.bittensor.balances import Balance from bittensor_cli.src.bittensor.async_substrate_interface import ( SubstrateRequestException, ) @@ -400,6 +401,7 @@ class CLIManager: root_app: typer.Typer subnets_app: typer.Typer weights_app: typer.Typer + utils_app = typer.Typer(epilog=_epilog) def __init__(self): self.config = { @@ -524,6 +526,9 @@ def __init__(self): self.weights_app, name="weight", hidden=True, no_args_is_help=True ) + # utils app + self.app.add_typer(self.utils_app, name="utils", no_args_is_help=True) + # config commands self.config_app.command("set")(self.set_config) self.config_app.command("get")(self.get_config) @@ -4333,6 +4338,32 @@ def weights_commit( ) ) + @staticmethod + @utils_app.command("convert") + def convert( + from_rao: Optional[str] = typer.Option( + None, "--rao", help="Convert amount from Rao" + ), + from_tao: Optional[float] = typer.Option( + None, "--tao", help="Convert amount from Tao" + ), + ): + if from_tao is None and from_rao is None: + err_console.print("Specify `--rao` and/or `--tao`.") + raise typer.Exit() + if from_rao is not None: + console.print( + f"{from_rao}{Balance.rao_unit}", + "=", + Balance.from_rao(int(float(from_rao))), + ) + if from_tao is not None: + console.print( + f"{Balance.unit}{from_tao}", + "=", + f"{Balance.from_tao(float(from_tao)).rao}{Balance.rao_unit}", + ) + def run(self): self.app() From 3e16772986c373117b3f3e357e7d90a8a9612e9b Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Thu, 10 Oct 2024 14:23:48 +0200 Subject: [PATCH 2/2] Better UX --- bittensor_cli/cli.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index 3571743b8..3fe783a5f 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -4348,20 +4348,25 @@ def convert( None, "--tao", help="Convert amount from Tao" ), ): + """ + Allows for converting between tao and rao using the specified flags + """ if from_tao is None and from_rao is None: err_console.print("Specify `--rao` and/or `--tao`.") raise typer.Exit() if from_rao is not None: + rao = int(float(from_rao)) console.print( - f"{from_rao}{Balance.rao_unit}", + f"{rao}{Balance.rao_unit}", "=", - Balance.from_rao(int(float(from_rao))), + Balance.from_rao(rao), ) if from_tao is not None: + tao = float(from_tao) console.print( - f"{Balance.unit}{from_tao}", + f"{Balance.unit}{tao}", "=", - f"{Balance.from_tao(float(from_tao)).rao}{Balance.rao_unit}", + f"{Balance.from_tao(tao).rao}{Balance.rao_unit}", ) def run(self):