Skip to content

Commit be873ff

Browse files
authored
Merge pull request #412 from opentensor/feat/thewhaleking/more-json-outputs
More json outputs
2 parents 0e59530 + c7b89a2 commit be873ff

File tree

6 files changed

+245
-93
lines changed

6 files changed

+245
-93
lines changed

bittensor_cli/cli.py

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4878,6 +4878,7 @@ def subnets_burn_cost(
48784878
network: Optional[list[str]] = Options.network,
48794879
quiet: bool = Options.quiet,
48804880
verbose: bool = Options.verbose,
4881+
json_output: bool = Options.json_output,
48814882
):
48824883
"""
48834884
Shows the required amount of TAO to be recycled for creating a new subnet, i.e., cost of registering a new subnet.
@@ -4888,8 +4889,10 @@ def subnets_burn_cost(
48884889
48894890
[green]$[/green] btcli subnets burn_cost
48904891
"""
4891-
self.verbosity_handler(quiet, verbose)
4892-
return self._run_command(subnets.burn_cost(self.initialize_chain(network)))
4892+
self.verbosity_handler(quiet, verbose, json_output)
4893+
return self._run_command(
4894+
subnets.burn_cost(self.initialize_chain(network), json_output)
4895+
)
48934896

48944897
def subnets_create(
48954898
self,
@@ -4922,6 +4925,7 @@ def subnets_create(
49224925
additional_info: Optional[str] = typer.Option(
49234926
None, "--additional-info", help="Additional information"
49244927
),
4928+
json_output: bool = Options.json_output,
49254929
prompt: bool = Options.prompt,
49264930
quiet: bool = Options.quiet,
49274931
verbose: bool = Options.verbose,
@@ -4940,7 +4944,7 @@ def subnets_create(
49404944
2. Create with GitHub repo and contact email:
49414945
[green]$[/green] btcli subnets create --subnet-name MySubnet --github-repo https://github.com/myorg/mysubnet --subnet-contact [email protected]
49424946
"""
4943-
self.verbosity_handler(quiet, verbose)
4947+
self.verbosity_handler(quiet, verbose, json_output)
49444948
wallet = self.wallet_ask(
49454949
wallet_name,
49464950
wallet_path,
@@ -4962,34 +4966,19 @@ def subnets_create(
49624966
description=description,
49634967
additional=additional_info,
49644968
)
4965-
success = self._run_command(
4966-
subnets.create(wallet, self.initialize_chain(network), identity, prompt),
4967-
exit_early=False,
4969+
self._run_command(
4970+
subnets.create(
4971+
wallet, self.initialize_chain(network), identity, json_output, prompt
4972+
)
49684973
)
49694974

4970-
if success and prompt:
4971-
set_id = Confirm.ask(
4972-
"[dark_sea_green3]Do you want to set/update your identity?",
4973-
default=False,
4974-
show_default=True,
4975-
)
4976-
if set_id:
4977-
self.wallet_set_id(
4978-
wallet_name=wallet.name,
4979-
wallet_hotkey=wallet.hotkey,
4980-
wallet_path=wallet.path,
4981-
network=network,
4982-
prompt=prompt,
4983-
quiet=quiet,
4984-
verbose=verbose,
4985-
)
4986-
49874975
def subnets_get_identity(
49884976
self,
49894977
network: Optional[list[str]] = Options.network,
49904978
netuid: int = Options.netuid,
49914979
quiet: bool = Options.quiet,
49924980
verbose: bool = Options.verbose,
4981+
json_output: bool = Options.json_output,
49934982
):
49944983
"""
49954984
Get the identity information for a subnet.
@@ -4998,11 +4987,10 @@ def subnets_get_identity(
49984987
49994988
[green]$[/green] btcli subnets get-identity --netuid 1
50004989
"""
5001-
self.verbosity_handler(quiet, verbose)
4990+
self.verbosity_handler(quiet, verbose, json_output)
50024991
return self._run_command(
50034992
subnets.get_identity(
5004-
self.initialize_chain(network),
5005-
netuid,
4993+
self.initialize_chain(network), netuid, json_output=json_output
50064994
)
50074995
)
50084996

@@ -5038,6 +5026,7 @@ def subnets_set_identity(
50385026
additional_info: Optional[str] = typer.Option(
50395027
None, "--additional-info", help="Additional information"
50405028
),
5029+
json_output: bool = Options.json_output,
50415030
prompt: bool = Options.prompt,
50425031
quiet: bool = Options.quiet,
50435032
verbose: bool = Options.verbose,
@@ -5055,7 +5044,7 @@ def subnets_set_identity(
50555044
2. Set subnet identity with specific values:
50565045
[green]$[/green] btcli subnets set-identity --netuid 1 --subnet-name MySubnet --github-repo https://github.com/myorg/mysubnet --subnet-contact [email protected]
50575046
"""
5058-
self.verbosity_handler(quiet, verbose)
5047+
self.verbosity_handler(quiet, verbose, json_output)
50595048
wallet = self.wallet_ask(
50605049
wallet_name,
50615050
wallet_path,
@@ -5073,7 +5062,9 @@ def subnets_set_identity(
50735062
exit_early=False,
50745063
)
50755064
if current_identity is None:
5076-
raise typer.Exit()
5065+
if json_output:
5066+
json_console.print('{"success": false}')
5067+
return
50775068

50785069
identity = prompt_for_subnet_identity(
50795070
current_identity=current_identity,
@@ -5086,15 +5077,13 @@ def subnets_set_identity(
50865077
additional=additional_info,
50875078
)
50885079

5089-
return self._run_command(
5080+
success = self._run_command(
50905081
subnets.set_identity(
5091-
wallet,
5092-
self.initialize_chain(network),
5093-
netuid,
5094-
identity,
5095-
prompt,
5082+
wallet, self.initialize_chain(network), netuid, identity, prompt
50965083
)
50975084
)
5085+
if json_output:
5086+
json_console.print(json.dumps({"success": success}))
50985087

50995088
def subnets_pow_register(
51005089
self,
@@ -5199,6 +5188,7 @@ def subnets_register(
51995188
help="Length (in blocks) for which the transaction should be valid. Note that it is possible that if you "
52005189
"use an era for this transaction that you may pay a different fee to register than the one stated.",
52015190
),
5191+
json_output: bool = Options.json_output,
52025192
prompt: bool = Options.prompt,
52035193
quiet: bool = Options.quiet,
52045194
verbose: bool = Options.verbose,
@@ -5214,7 +5204,7 @@ def subnets_register(
52145204
52155205
[green]$[/green] btcli subnets register --netuid 1
52165206
"""
5217-
self.verbosity_handler(quiet, verbose)
5207+
self.verbosity_handler(quiet, verbose, json_output)
52185208
wallet = self.wallet_ask(
52195209
wallet_name,
52205210
wallet_path,
@@ -5228,6 +5218,7 @@ def subnets_register(
52285218
self.initialize_chain(network),
52295219
netuid,
52305220
era,
5221+
json_output,
52315222
prompt,
52325223
)
52335224
)
@@ -5353,6 +5344,7 @@ def weights_reveal(
53535344
"-s",
53545345
help="Corresponding salt for the hash function, e.g. -s 163,241,217 ...",
53555346
),
5347+
json_output: bool = Options.json_output,
53565348
quiet: bool = Options.quiet,
53575349
verbose: bool = Options.verbose,
53585350
prompt: bool = Options.prompt,
@@ -5366,7 +5358,7 @@ def weights_reveal(
53665358
53675359
[green]$[/green] btcli wt reveal --netuid 1 --uids 1,2,3,4 --weights 0.1,0.2,0.3,0.4 --salt 163,241,217,11,161,142,147,189
53685360
"""
5369-
self.verbosity_handler(quiet, verbose)
5361+
self.verbosity_handler(quiet, verbose, json_output)
53705362
uids = list_prompt(uids, int, "UIDs of interest for the specified netuid")
53715363
weights = list_prompt(
53725364
weights, float, "Corresponding weights for the specified UIDs"
@@ -5399,7 +5391,7 @@ def weights_reveal(
53995391
err_console.print(
54005392
"The number of UIDs you specify must match up with the specified number of weights"
54015393
)
5402-
raise typer.Exit()
5394+
return
54035395

54045396
if salt:
54055397
salt = parse_to_list(
@@ -5428,6 +5420,7 @@ def weights_reveal(
54285420
salt,
54295421
__version_as_int__,
54305422
prompt=prompt,
5423+
json_output=json_output,
54315424
)
54325425
)
54335426

@@ -5451,6 +5444,7 @@ def weights_commit(
54515444
"-s",
54525445
help="Corresponding salt for the hash function, e.g. -s 163 -s 241 -s 217 ...",
54535446
),
5447+
json_output: bool = Options.json_output,
54545448
quiet: bool = Options.quiet,
54555449
verbose: bool = Options.verbose,
54565450
prompt: bool = Options.prompt,
@@ -5468,7 +5462,7 @@ def weights_commit(
54685462
[italic]Note[/italic]: This command is used to commit weights for a specific subnet and requires the user to have the necessary
54695463
permissions.
54705464
"""
5471-
self.verbosity_handler(quiet, verbose)
5465+
self.verbosity_handler(quiet, verbose, json_output)
54725466

54735467
if uids:
54745468
uids = parse_to_list(
@@ -5497,7 +5491,7 @@ def weights_commit(
54975491
err_console.print(
54985492
"The number of UIDs you specify must match up with the specified number of weights"
54995493
)
5500-
raise typer.Exit()
5494+
return
55015495

55025496
if salt:
55035497
salt = parse_to_list(
@@ -5524,6 +5518,7 @@ def weights_commit(
55245518
weights,
55255519
salt,
55265520
__version_as_int__,
5521+
json_output=json_output,
55275522
prompt=prompt,
55285523
)
55295524
)

bittensor_cli/src/bittensor/extrinsics/registration.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ async def burned_register_extrinsic(
679679
wait_for_finalization: bool = True,
680680
era: Optional[int] = None,
681681
prompt: bool = False,
682-
) -> bool:
682+
) -> tuple[bool, str]:
683683
"""Registers the wallet to chain by recycling TAO.
684684
685685
:param subtensor: The SubtensorInterface object to use for the call, initialized
@@ -693,8 +693,8 @@ async def burned_register_extrinsic(
693693
:param era: the period (in blocks) for which the transaction should remain valid.
694694
:param prompt: If `True`, the call waits for confirmation from the user before proceeding.
695695
696-
:return: Flag is `True` if extrinsic was finalized or included in the block. If we did not wait for
697-
finalization/inclusion, the response is `True`.
696+
:return: (success, msg), where success is `True` if extrinsic was finalized or included in the block. If we did not
697+
wait for finalization/inclusion, the response is `True`.
698698
"""
699699

700700
if not (unlock_status := unlock_key(wallet, print_out=False)).success:
@@ -742,7 +742,7 @@ async def burned_register_extrinsic(
742742
f"hotkey: [{COLOR_PALETTE.G.HK}]{neuron.hotkey}[/{COLOR_PALETTE.G.HK}]\n"
743743
f"coldkey: [{COLOR_PALETTE.G.CK}]{neuron.coldkey}[/{COLOR_PALETTE.G.CK}]"
744744
)
745-
return True
745+
return True, "Already registered"
746746

747747
with console.status(
748748
":satellite: Recycling TAO for Registration...", spinner="aesthetic"
@@ -762,7 +762,7 @@ async def burned_register_extrinsic(
762762
if not success:
763763
err_console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
764764
await asyncio.sleep(0.5)
765-
return False
765+
return False, err_msg
766766
# Successful registration, final check for neuron and pubkey
767767
else:
768768
with console.status(":satellite: Checking Balance...", spinner="aesthetic"):
@@ -791,13 +791,13 @@ async def burned_register_extrinsic(
791791
console.print(
792792
f":white_heavy_check_mark: [green]Registered on netuid {netuid} with UID {my_uid}[/green]"
793793
)
794-
return True
794+
return True, f"Registered on {netuid} with UID {my_uid}"
795795
else:
796796
# neuron not found, try again
797797
err_console.print(
798798
":cross_mark: [red]Unknown error. Neuron not found.[/red]"
799799
)
800-
return False
800+
return False, "Unknown error. Neuron not found."
801801

802802

803803
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)