Skip to content

Commit 742e479

Browse files
committed
Additional CLI commands with JSON outputs
1 parent bb28ab4 commit 742e479

File tree

4 files changed

+116
-80
lines changed

4 files changed

+116
-80
lines changed

bittensor_cli/cli.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,6 +4875,7 @@ def subnets_burn_cost(
48754875
network: Optional[list[str]] = Options.network,
48764876
quiet: bool = Options.quiet,
48774877
verbose: bool = Options.verbose,
4878+
json_output: bool = Options.json_output,
48784879
):
48794880
"""
48804881
Shows the required amount of TAO to be recycled for creating a new subnet, i.e., cost of registering a new subnet.
@@ -4885,8 +4886,10 @@ def subnets_burn_cost(
48854886
48864887
[green]$[/green] btcli subnets burn_cost
48874888
"""
4888-
self.verbosity_handler(quiet, verbose)
4889-
return self._run_command(subnets.burn_cost(self.initialize_chain(network)))
4889+
self.verbosity_handler(quiet, verbose, json_output)
4890+
return self._run_command(
4891+
subnets.burn_cost(self.initialize_chain(network), json_output)
4892+
)
48904893

48914894
def subnets_create(
48924895
self,
@@ -4919,6 +4922,7 @@ def subnets_create(
49194922
additional_info: Optional[str] = typer.Option(
49204923
None, "--additional-info", help="Additional information"
49214924
),
4925+
json_output: bool = Options.json_output,
49224926
prompt: bool = Options.prompt,
49234927
quiet: bool = Options.quiet,
49244928
verbose: bool = Options.verbose,
@@ -4937,7 +4941,7 @@ def subnets_create(
49374941
2. Create with GitHub repo and contact email:
49384942
[green]$[/green] btcli subnets create --subnet-name MySubnet --github-repo https://github.com/myorg/mysubnet --subnet-contact [email protected]
49394943
"""
4940-
self.verbosity_handler(quiet, verbose)
4944+
self.verbosity_handler(quiet, verbose, json_output)
49414945
wallet = self.wallet_ask(
49424946
wallet_name,
49434947
wallet_path,
@@ -4959,34 +4963,19 @@ def subnets_create(
49594963
description=description,
49604964
additional=additional_info,
49614965
)
4962-
success = self._run_command(
4963-
subnets.create(wallet, self.initialize_chain(network), identity, prompt),
4964-
exit_early=False,
4966+
self._run_command(
4967+
subnets.create(
4968+
wallet, self.initialize_chain(network), identity, json_output, prompt
4969+
)
49654970
)
49664971

4967-
if success and prompt:
4968-
set_id = Confirm.ask(
4969-
"[dark_sea_green3]Do you want to set/update your identity?",
4970-
default=False,
4971-
show_default=True,
4972-
)
4973-
if set_id:
4974-
self.wallet_set_id(
4975-
wallet_name=wallet.name,
4976-
wallet_hotkey=wallet.hotkey,
4977-
wallet_path=wallet.path,
4978-
network=network,
4979-
prompt=prompt,
4980-
quiet=quiet,
4981-
verbose=verbose,
4982-
)
4983-
49844972
def subnets_get_identity(
49854973
self,
49864974
network: Optional[list[str]] = Options.network,
49874975
netuid: int = Options.netuid,
49884976
quiet: bool = Options.quiet,
49894977
verbose: bool = Options.verbose,
4978+
json_output: bool = Options.json_output,
49904979
):
49914980
"""
49924981
Get the identity information for a subnet.
@@ -4995,11 +4984,10 @@ def subnets_get_identity(
49954984
49964985
[green]$[/green] btcli subnets get-identity --netuid 1
49974986
"""
4998-
self.verbosity_handler(quiet, verbose)
4987+
self.verbosity_handler(quiet, verbose, json_output)
49994988
return self._run_command(
50004989
subnets.get_identity(
5001-
self.initialize_chain(network),
5002-
netuid,
4990+
self.initialize_chain(network), netuid, json_output=json_output
50034991
)
50044992
)
50054993

@@ -5035,6 +5023,7 @@ def subnets_set_identity(
50355023
additional_info: Optional[str] = typer.Option(
50365024
None, "--additional-info", help="Additional information"
50375025
),
5026+
json_output: bool = Options.json_output,
50385027
prompt: bool = Options.prompt,
50395028
quiet: bool = Options.quiet,
50405029
verbose: bool = Options.verbose,
@@ -5052,7 +5041,7 @@ def subnets_set_identity(
50525041
2. Set subnet identity with specific values:
50535042
[green]$[/green] btcli subnets set-identity --netuid 1 --subnet-name MySubnet --github-repo https://github.com/myorg/mysubnet --subnet-contact [email protected]
50545043
"""
5055-
self.verbosity_handler(quiet, verbose)
5044+
self.verbosity_handler(quiet, verbose, json_output)
50565045
wallet = self.wallet_ask(
50575046
wallet_name,
50585047
wallet_path,
@@ -5070,7 +5059,9 @@ def subnets_set_identity(
50705059
exit_early=False,
50715060
)
50725061
if current_identity is None:
5073-
raise typer.Exit()
5062+
if json_output:
5063+
json_console.print('{"success": false}')
5064+
return
50745065

50755066
identity = prompt_for_subnet_identity(
50765067
current_identity=current_identity,
@@ -5083,15 +5074,13 @@ def subnets_set_identity(
50835074
additional=additional_info,
50845075
)
50855076

5086-
return self._run_command(
5077+
success = self._run_command(
50875078
subnets.set_identity(
5088-
wallet,
5089-
self.initialize_chain(network),
5090-
netuid,
5091-
identity,
5092-
prompt,
5079+
wallet, self.initialize_chain(network), netuid, identity, prompt
50935080
)
50945081
)
5082+
if json_output:
5083+
json_console.print(json.dumps({"success": success}))
50955084

50965085
def subnets_pow_register(
50975086
self,
@@ -5196,6 +5185,7 @@ def subnets_register(
51965185
help="Length (in blocks) for which the transaction should be valid. Note that it is possible that if you "
51975186
"use an era for this transaction that you may pay a different fee to register than the one stated.",
51985187
),
5188+
json_output: bool = Options.json_output,
51995189
prompt: bool = Options.prompt,
52005190
quiet: bool = Options.quiet,
52015191
verbose: bool = Options.verbose,
@@ -5211,7 +5201,7 @@ def subnets_register(
52115201
52125202
[green]$[/green] btcli subnets register --netuid 1
52135203
"""
5214-
self.verbosity_handler(quiet, verbose)
5204+
self.verbosity_handler(quiet, verbose, json_output)
52155205
wallet = self.wallet_ask(
52165206
wallet_name,
52175207
wallet_path,
@@ -5225,6 +5215,7 @@ def subnets_register(
52255215
self.initialize_chain(network),
52265216
netuid,
52275217
era,
5218+
json_output,
52285219
prompt,
52295220
)
52305221
)

bittensor_cli/src/bittensor/extrinsics/registration.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ async def burned_register_extrinsic(
678678
wait_for_finalization: bool = True,
679679
era: Optional[int] = None,
680680
prompt: bool = False,
681-
) -> bool:
681+
) -> tuple[bool, str]:
682682
"""Registers the wallet to chain by recycling TAO.
683683
684684
:param subtensor: The SubtensorInterface object to use for the call, initialized
@@ -691,8 +691,8 @@ async def burned_register_extrinsic(
691691
or returns `False` if the extrinsic fails to be finalized within the timeout.
692692
:param prompt: If `True`, the call waits for confirmation from the user before proceeding.
693693
694-
:return: Flag is `True` if extrinsic was finalized or included in the block. If we did not wait for
695-
finalization/inclusion, the response is `True`.
694+
:return: (success, msg), where success is `True` if extrinsic was finalized or included in the block. If we did not
695+
wait for finalization/inclusion, the response is `True`.
696696
"""
697697

698698
if not (unlock_status := unlock_key(wallet, print_out=False)).success:
@@ -740,7 +740,7 @@ async def burned_register_extrinsic(
740740
f"hotkey: [{COLOR_PALETTE['GENERAL']['HOTKEY']}]{neuron.hotkey}[/{COLOR_PALETTE['GENERAL']['HOTKEY']}]\n"
741741
f"coldkey: [{COLOR_PALETTE['GENERAL']['COLDKEY']}]{neuron.coldkey}[/{COLOR_PALETTE['GENERAL']['COLDKEY']}]"
742742
)
743-
return True
743+
return True, "Already registered"
744744

745745
with console.status(
746746
":satellite: Recycling TAO for Registration...", spinner="aesthetic"
@@ -760,7 +760,7 @@ async def burned_register_extrinsic(
760760
if not success:
761761
err_console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
762762
await asyncio.sleep(0.5)
763-
return False
763+
return False, err_msg
764764
# Successful registration, final check for neuron and pubkey
765765
else:
766766
with console.status(":satellite: Checking Balance...", spinner="aesthetic"):
@@ -788,13 +788,13 @@ async def burned_register_extrinsic(
788788
console.print(
789789
f":white_heavy_check_mark: [green]Registered on netuid {netuid} with UID {my_uid}[/green]"
790790
)
791-
return True
791+
return True, f"Registered on {netuid} with UID {my_uid}"
792792
else:
793793
# neuron not found, try again
794794
err_console.print(
795795
":cross_mark: [red]Unknown error. Neuron not found.[/red]"
796796
)
797-
return False
797+
return False, "Unknown error. Neuron not found."
798798

799799

800800
async def run_faucet_extrinsic(

bittensor_cli/src/bittensor/extrinsics/root.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ async def root_register_extrinsic(
291291
wait_for_inclusion: bool = True,
292292
wait_for_finalization: bool = True,
293293
prompt: bool = False,
294-
) -> bool:
294+
) -> tuple[bool, str]:
295295
r"""Registers the wallet to root network.
296296
297297
:param subtensor: The SubtensorInterface object
@@ -302,12 +302,12 @@ async def root_register_extrinsic(
302302
or returns `False` if the extrinsic fails to be finalized within the timeout.
303303
:param prompt: If `True`, the call waits for confirmation from the user before proceeding.
304304
305-
:return: `True` if extrinsic was finalized or included in the block. If we did not wait for finalization/inclusion,
306-
the response is `True`.
305+
:return: (success, msg), with success being `True` if extrinsic was finalized or included in the block. If we did
306+
not wait for finalization/inclusion, the response is `True`.
307307
"""
308308

309-
if not unlock_key(wallet).success:
310-
return False
309+
if not (unlock := unlock_key(wallet)).success:
310+
return False, unlock.message
311311

312312
print_verbose(f"Checking if hotkey ({wallet.hotkey_str}) is registered on root")
313313
is_registered = await is_hotkey_registered(
@@ -317,7 +317,7 @@ async def root_register_extrinsic(
317317
console.print(
318318
":white_heavy_check_mark: [green]Already registered on root network.[/green]"
319319
)
320-
return True
320+
return True, "Already registered on root network"
321321

322322
with console.status(":satellite: Registering to root network...", spinner="earth"):
323323
call = await subtensor.substrate.compose_call(
@@ -334,8 +334,8 @@ async def root_register_extrinsic(
334334

335335
if not success:
336336
err_console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
337-
time.sleep(0.5)
338-
return False
337+
await asyncio.sleep(0.5)
338+
return False, err_msg
339339

340340
# Successful registration, final check for neuron and pubkey
341341
else:
@@ -348,13 +348,13 @@ async def root_register_extrinsic(
348348
console.print(
349349
f":white_heavy_check_mark: [green]Registered with UID {uid}[/green]"
350350
)
351-
return True
351+
return True, f"Registered with UID {uid}"
352352
else:
353353
# neuron not found, try again
354354
err_console.print(
355355
":cross_mark: [red]Unknown error. Neuron not found.[/red]"
356356
)
357-
return False
357+
return False, "Unknown error. Neuron not found."
358358

359359

360360
async def set_root_weights_extrinsic(

0 commit comments

Comments
 (0)