Skip to content

Commit 095a935

Browse files
authored
Standardize Success Message Printing with print.success (#786)
* feat: add the print_success util * feat: refactor with print_success * fix: update with print_success and print_error * ruff format * fix: revert cli * fix: update the util for print_success * fix: update the print_error and revert print_success for remove.py file * hotfix: add the missing character * fix: change to white_check_mark * fix: revert to white_heavy_check_mark * fix: revert print_success for wallet and few consol from utils * ruff format * fix: revert in the e2e util * ruff format
1 parent 555b4ec commit 095a935

File tree

19 files changed

+122
-138
lines changed

19 files changed

+122
-138
lines changed

bittensor_cli/src/bittensor/extrinsics/registration.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
confirm_action,
3434
console,
3535
print_error,
36+
print_success,
3637
format_error_message,
3738
millify,
3839
get_human_readable,
@@ -587,8 +588,8 @@ async def get_neuron_for_pubkey_and_subnet():
587588
subtensor, netuid=netuid, hotkey_ss58=get_hotkey_pub_ss58(wallet)
588589
)
589590
if is_registered:
590-
print_error(
591-
f":white_heavy_check_mark: [dark_sea_green3]Already registered on netuid:{netuid}[/dark_sea_green3]"
591+
print_success(
592+
f"[dark_sea_green3]Already registered on netuid:{netuid}[/dark_sea_green3]"
592593
)
593594
return True
594595

@@ -630,8 +631,8 @@ async def get_neuron_for_pubkey_and_subnet():
630631
# https://github.com/opentensor/subtensor/blob/development/pallets/subtensor/src/errors.rs
631632

632633
if "HotKeyAlreadyRegisteredInSubNet" in err_msg:
633-
console.print(
634-
f":white_heavy_check_mark: [dark_sea_green3]Already Registered on "
634+
print_success(
635+
f"[dark_sea_green3]Already Registered on "
635636
f"[bold]subnet:{netuid}[/bold][/dark_sea_green3]"
636637
)
637638
return True
@@ -647,8 +648,8 @@ async def get_neuron_for_pubkey_and_subnet():
647648
hotkey_ss58=get_hotkey_pub_ss58(wallet),
648649
)
649650
if is_registered:
650-
console.print(
651-
":white_heavy_check_mark: [dark_sea_green3]Registered[/dark_sea_green3]"
651+
print_success(
652+
"[dark_sea_green3]Registered[/dark_sea_green3]"
652653
)
653654
return True
654655
else:
@@ -738,8 +739,8 @@ async def burned_register_extrinsic(
738739
era_ = {"period": era}
739740

740741
if not neuron.is_null:
742+
print_success("[dark_sea_green3]Already Registered[/dark_sea_green3]:")
741743
console.print(
742-
":white_heavy_check_mark: [dark_sea_green3]Already Registered[/dark_sea_green3]:\n"
743744
f"uid: [{COLOR_PALETTE.G.NETUID_EXTRA}]{neuron.uid}[/{COLOR_PALETTE.G.NETUID_EXTRA}]\n"
744745
f"netuid: [{COLOR_PALETTE.G.NETUID}]{neuron.netuid}[/{COLOR_PALETTE.G.NETUID}]\n"
745746
f"hotkey: [{COLOR_PALETTE.G.HK}]{neuron.hotkey}[/{COLOR_PALETTE.G.HK}]\n"
@@ -798,9 +799,7 @@ async def burned_register_extrinsic(
798799
)
799800

800801
if len(netuids_for_hotkey) > 0:
801-
console.print(
802-
f":white_heavy_check_mark: [green]Registered on netuid {netuid} with UID {my_uid}[/green]"
803-
)
802+
print_success(f"Registered on netuid {netuid} with UID {my_uid}")
804803
return True, f"Registered on {netuid} with UID {my_uid}", ext_id
805804
else:
806805
# neuron not found, try again

bittensor_cli/src/bittensor/extrinsics/root.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
confirm_action,
3333
console,
3434
print_error,
35+
print_success,
3536
u16_normalized_float,
3637
print_verbose,
3738
format_error_message,
@@ -343,9 +344,7 @@ async def root_register_extrinsic(
343344
subtensor, netuid=0, hotkey_ss58=get_hotkey_pub_ss58(wallet)
344345
)
345346
if is_registered:
346-
console.print(
347-
":white_heavy_check_mark: [green]Already registered on root network.[/green]"
348-
)
347+
print_success("Already registered on root network.")
349348
return True, "Already registered on root network", None
350349

351350
with console.status(":satellite: Registering to root network...", spinner="earth"):
@@ -377,9 +376,7 @@ async def root_register_extrinsic(
377376
params=[0, get_hotkey_pub_ss58(wallet)],
378377
)
379378
if uid is not None:
380-
console.print(
381-
f":white_heavy_check_mark: [green]Registered with UID {uid}[/green]"
382-
)
379+
print_success(f"Registered with UID {uid}")
383380
return True, f"Registered with UID {uid}", ext_id
384381
else:
385382
# neuron not found, try again
@@ -540,7 +537,7 @@ async def _do_set_weights():
540537
return True
541538

542539
if success is True:
543-
console.print(":white_heavy_check_mark: [green]Finalized[/green]")
540+
print_success("Finalized")
544541
return True
545542
else:
546543
fmt_err = format_error_message(error_message)

bittensor_cli/src/bittensor/extrinsics/serving.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
confirm_action,
1212
console,
1313
print_error,
14+
print_success,
1415
format_error_message,
1516
unlock_key,
1617
print_extrinsic_id,
@@ -112,8 +113,8 @@ async def reset_axon_extrinsic(
112113

113114
# We only wait here if we expect finalization.
114115
if not wait_for_finalization and not wait_for_inclusion:
115-
console.print(
116-
":white_heavy_check_mark: [dark_sea_green3]Axon reset successfully[/dark_sea_green3]"
116+
print_success(
117+
"[dark_sea_green3]Axon reset successfully[/dark_sea_green3]"
117118
)
118119
return True, "Not waiting for finalization or inclusion.", None
119120

@@ -125,8 +126,8 @@ async def reset_axon_extrinsic(
125126
else:
126127
ext_id = await response.get_extrinsic_identifier()
127128
await print_extrinsic_id(response)
128-
console.print(
129-
":white_heavy_check_mark: [dark_sea_green3]Axon reset successfully[/dark_sea_green3]"
129+
print_success(
130+
"[dark_sea_green3]Axon reset successfully[/dark_sea_green3]"
130131
)
131132
return True, "Axon reset successfully", ext_id
132133

@@ -230,8 +231,8 @@ async def set_axon_extrinsic(
230231

231232
# We only wait here if we expect finalization.
232233
if not wait_for_finalization and not wait_for_inclusion:
233-
console.print(
234-
f":white_heavy_check_mark: [dark_sea_green3]Axon set successfully to {ip}:{port}[/dark_sea_green3]"
234+
print_success(
235+
f"[dark_sea_green3]Axon set successfully to {ip}:{port}[/dark_sea_green3]"
235236
)
236237
return True, "Not waiting for finalization or inclusion.", None
237238

@@ -243,8 +244,8 @@ async def set_axon_extrinsic(
243244
else:
244245
ext_id = await response.get_extrinsic_identifier()
245246
await print_extrinsic_id(response)
246-
console.print(
247-
f":white_heavy_check_mark: [dark_sea_green3]Axon set successfully to {ip}:{port}[/dark_sea_green3]"
247+
print_success(
248+
f"[dark_sea_green3]Axon set successfully to {ip}:{port}[/dark_sea_green3]"
248249
)
249250
return True, f"Axon set successfully to {ip}:{port}", ext_id
250251

bittensor_cli/src/bittensor/extrinsics/transfer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
confirm_action,
1313
console,
1414
print_error,
15+
print_success,
1516
print_verbose,
1617
is_valid_bittensor_address_or_public_key,
1718
print_error,
@@ -219,11 +220,10 @@ async def do_transfer() -> tuple[bool, str, str, Optional[AsyncExtrinsicReceipt]
219220
success, block_hash, err_msg, ext_receipt = await do_transfer()
220221

221222
if success:
222-
console.print(":white_heavy_check_mark: [green]Finalized[/green]")
223-
console.print(f"[green]Block Hash: {block_hash}[/green]")
223+
print_success(f"Finalized. Block Hash: {block_hash}")
224224

225225
else:
226-
console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
226+
print_error(f"Failed: {err_msg}")
227227

228228
if success:
229229
with console.status(":satellite: Checking Balance...", spinner="aesthetic"):

bittensor_cli/src/bittensor/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ def print_error(message: str, status=None):
156156
print_console(error_message, "red", err_console)
157157

158158

159+
def print_success(message: str, status=None):
160+
"""Print success messages while temporarily pausing the status spinner."""
161+
success_message = f":white_heavy_check_mark: {message}"
162+
if status:
163+
status.stop()
164+
print_console(success_message, "green", console)
165+
status.start()
166+
else:
167+
print_console(success_message, "green", console)
168+
169+
159170
RAO_PER_TAO = 1e9
160171
U16_MAX = 65535
161172
U64_MAX = 18446744073709551615

bittensor_cli/src/commands/crowd/contribute.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
json_console,
1717
print_error,
1818
print_extrinsic_id,
19+
print_success,
1920
unlock_key,
2021
)
2122
from bittensor_cli.src.commands.crowd.view import show_crowdloan_details
@@ -89,7 +90,7 @@ async def contribute_to_crowdloan(
8990
if json_output:
9091
json_console.print(json.dumps({"success": False, "error": error_msg}))
9192
else:
92-
print_error(f"[red]{error_msg}[/red]")
93+
print_error(error_msg)
9394
return False, error_msg
9495

9596
is_valid, error_message = validate_for_contribution(
@@ -99,7 +100,7 @@ async def contribute_to_crowdloan(
99100
if json_output:
100101
json_console.print(json.dumps({"success": False, "error": error_message}))
101102
else:
102-
print_error(f"[red]{error_message}[/red]")
103+
print_error(error_message)
103104
return False, error_message
104105

105106
contributor_address = proxy or wallet.coldkeypub.ss58_address
@@ -136,15 +137,15 @@ async def contribute_to_crowdloan(
136137
if json_output:
137138
json_console.print(json.dumps({"success": False, "error": error_msg}))
138139
else:
139-
print_error(f"[red]{error_msg}[/red]")
140+
print_error(error_msg)
140141
return False, "Contribution below minimum requirement."
141142

142143
if contribution_amount > user_balance:
143144
error_msg = f"Insufficient balance. You have {user_balance} but trying to contribute {contribution_amount}."
144145
if json_output:
145146
json_console.print(json.dumps({"success": False, "error": error_msg}))
146147
else:
147-
print_error(f"[red]{error_msg}[/red]")
148+
print_error(error_msg)
148149
return False, "Insufficient balance."
149150

150151
# Auto-adjustment
@@ -245,7 +246,7 @@ async def contribute_to_crowdloan(
245246
json.dumps({"success": False, "error": unlock_status.message})
246247
)
247248
else:
248-
print_error(f"[red]{unlock_status.message}[/red]")
249+
print_error(unlock_status.message)
249250
return False, unlock_status.message
250251

251252
with console.status(f"\n:satellite: Contributing to crowdloan #{crowdloan_id}..."):
@@ -272,7 +273,7 @@ async def contribute_to_crowdloan(
272273
)
273274
)
274275
else:
275-
print_error(f"[red]Failed to contribute: {error_message}[/red]")
276+
print_error(f"Failed to contribute: {error_message}")
276277
return False, error_message or "Failed to contribute."
277278

278279
new_balance, new_contribution, updated_crowdloan = await asyncio.gather(
@@ -398,15 +399,15 @@ async def withdraw_from_crowdloan(
398399
if json_output:
399400
json_console.print(json.dumps({"success": False, "error": error_msg}))
400401
else:
401-
print_error(f"[red]{error_msg}[/red]")
402+
print_error(error_msg)
402403
return False, error_msg
403404

404405
if crowdloan.finalized:
405406
error_msg = f"Crowdloan #{crowdloan_id} is already finalized. Withdrawals are not allowed."
406407
if json_output:
407408
json_console.print(json.dumps({"success": False, "error": error_msg}))
408409
else:
409-
print_error(f"[red]{error_msg}[/red]")
410+
print_error(error_msg)
410411
return False, "Cannot withdraw from finalized crowdloan."
411412

412413
contributor_address = proxy or wallet.coldkeypub.ss58_address
@@ -424,7 +425,7 @@ async def withdraw_from_crowdloan(
424425
if json_output:
425426
json_console.print(json.dumps({"success": False, "error": error_msg}))
426427
else:
427-
print_error(f"[red]{error_msg}[/red]")
428+
print_error(error_msg)
428429
return False, "No contribution to withdraw."
429430

430431
is_creator = wallet.coldkeypub.ss58_address == crowdloan.creator
@@ -435,7 +436,7 @@ async def withdraw_from_crowdloan(
435436
if json_output:
436437
json_console.print(json.dumps({"success": False, "error": error_msg}))
437438
else:
438-
print_error(f"[red]{error_msg}[/red]")
439+
print_error(error_msg)
439440
return False, "Creator cannot withdraw deposit amount."
440441
remaining_contribution = crowdloan.deposit
441442
else:
@@ -534,7 +535,7 @@ async def withdraw_from_crowdloan(
534535
json.dumps({"success": False, "error": unlock_status.message})
535536
)
536537
else:
537-
print_error(f"[red]{unlock_status.message}[/red]")
538+
print_error(unlock_status.message)
538539
return False, unlock_status.message
539540

540541
with console.status(f"\n:satellite: Withdrawing from crowdloan #{crowdloan_id}..."):
@@ -561,9 +562,7 @@ async def withdraw_from_crowdloan(
561562
)
562563
)
563564
else:
564-
print_error(
565-
f"[red]Failed to withdraw: {error_message or 'Unknown error'}[/red]"
566-
)
565+
print_error(f"Failed to withdraw: {error_message or 'Unknown error'}")
567566
return False, error_message or "Failed to withdraw from crowdloan."
568567

569568
new_balance, updated_contribution, updated_crowdloan = await asyncio.gather(
@@ -602,9 +601,7 @@ async def withdraw_from_crowdloan(
602601
}
603602
json_console.print(json.dumps(output_dict))
604603
else:
605-
console.print(
606-
f"\n✅ [green]Successfully withdrew from crowdloan #{crowdloan_id}![/green]\n"
607-
)
604+
print_success(f"Successfully withdrew from crowdloan #{crowdloan_id}!\n")
608605

609606
console.print(
610607
f"Amount Withdrawn: [{COLORS.S.AMOUNT}]{withdrawable}[/{COLORS.S.AMOUNT}]\n"

0 commit comments

Comments
 (0)