Skip to content

Commit

Permalink
Removes all the raise typer.Exit() calls throughout the commands, a…
Browse files Browse the repository at this point in the history
…nd instead provides consistent returns or proper exception raising to be handled further down the line. This results in better outputs with `--verbose`
  • Loading branch information
thewhaleking committed Mar 5, 2025
1 parent 4d03716 commit c1625e4
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 75 deletions.
2 changes: 1 addition & 1 deletion bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2937,7 +2937,7 @@ def stake_add(
),
exit_early=False,
)
if selected_hotkey is None:
if not selected_hotkey:
print_error("No delegate selected. Exiting.")
raise typer.Exit()
include_hotkeys = selected_hotkey
Expand Down
4 changes: 1 addition & 3 deletions bittensor_cli/src/commands/stake/add.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
from functools import partial

import typer
from typing import TYPE_CHECKING, Optional
from rich.table import Table
from rich.prompt import Confirm, Prompt
Expand All @@ -20,7 +19,6 @@
unlock_key,
)
from bittensor_wallet import Wallet
from bittensor_wallet.errors import KeyFileError

if TYPE_CHECKING:
from bittensor_cli.src.bittensor.subtensor_interface import SubtensorInterface
Expand Down Expand Up @@ -338,7 +336,7 @@ async def stake_extrinsic(

if prompt:
if not Confirm.ask("Would you like to continue?"):
raise typer.Exit()
return False
if not unlock_key(wallet).success:
return False

Expand Down
3 changes: 1 addition & 2 deletions bittensor_cli/src/commands/stake/list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio

from typing import TYPE_CHECKING, Optional
import typer

from bittensor_wallet import Wallet
from rich.prompt import Prompt
Expand Down Expand Up @@ -428,7 +427,7 @@ def format_cell(

if not hotkeys_to_substakes:
print_error(f"No stakes found for coldkey ss58: ({coldkey_address})")
raise typer.Exit()
return

if live:
# Select one hotkey for live monitoring
Expand Down
83 changes: 48 additions & 35 deletions bittensor_cli/src/commands/stake/move.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import asyncio

from typing import TYPE_CHECKING
import typer

from bittensor_wallet import Wallet
from bittensor_wallet.errors import KeyFileError
from rich.table import Table
from rich.prompt import Confirm, Prompt

Expand Down Expand Up @@ -72,7 +70,7 @@ async def display_stake_movement_cross_subnets(

if received_amount < Balance.from_tao(0):
print_error("Not enough Alpha to pay the transaction fee.")
raise typer.Exit()
raise ValueError

ideal_amount = amount_to_move * price
total_slippage = ideal_amount - received_amount
Expand Down Expand Up @@ -228,7 +226,7 @@ async def stake_move_selection(

if not hotkey_stakes:
print_error("You have no stakes to move.")
raise typer.Exit()
raise ValueError

# Display hotkeys with stakes
table = Table(
Expand Down Expand Up @@ -457,7 +455,7 @@ async def stake_swap_selection(

if not hotkey_stakes:
print_error(f"No stakes found for hotkey: {wallet.hotkey_str}")
raise typer.Exit()
raise ValueError

# Display available stakes
table = Table(
Expand Down Expand Up @@ -540,7 +538,10 @@ async def move_stake(
prompt: bool = True,
):
if interactive_selection:
selection = await stake_move_selection(subtensor, wallet)
try:
selection = await stake_move_selection(subtensor, wallet)
except ValueError:
return False
origin_hotkey = selection["origin_hotkey"]
origin_netuid = selection["origin_netuid"]
amount = selection["amount"]
Expand Down Expand Up @@ -571,7 +572,7 @@ async def move_stake(
f"in Netuid: "
f"[{COLOR_PALETTE['GENERAL']['SUBHEADING']}]{origin_netuid}[/{COLOR_PALETTE['GENERAL']['SUBHEADING']}]"
)
raise typer.Exit()
return False

console.print(
f"\nOrigin Netuid: "
Expand Down Expand Up @@ -609,16 +610,19 @@ async def move_stake(

# Slippage warning
if prompt:
await display_stake_movement_cross_subnets(
subtensor=subtensor,
origin_netuid=origin_netuid,
destination_netuid=destination_netuid,
origin_hotkey=origin_hotkey,
destination_hotkey=destination_hotkey,
amount_to_move=amount_to_move_as_balance,
)
try:
await display_stake_movement_cross_subnets(
subtensor=subtensor,
origin_netuid=origin_netuid,
destination_netuid=destination_netuid,
origin_hotkey=origin_hotkey,
destination_hotkey=destination_hotkey,
amount_to_move=amount_to_move_as_balance,
)
except ValueError:
return False
if not Confirm.ask("Would you like to continue?"):
raise typer.Exit()
return False

# Perform moving operation.
if not unlock_key(wallet).success:
Expand Down Expand Up @@ -763,17 +767,20 @@ async def transfer_stake(

# Slippage warning
if prompt:
await display_stake_movement_cross_subnets(
subtensor=subtensor,
origin_netuid=origin_netuid,
destination_netuid=dest_netuid,
origin_hotkey=origin_hotkey,
destination_hotkey=origin_hotkey,
amount_to_move=amount_to_transfer,
)
try:
await display_stake_movement_cross_subnets(
subtensor=subtensor,
origin_netuid=origin_netuid,
destination_netuid=dest_netuid,
origin_hotkey=origin_hotkey,
destination_hotkey=origin_hotkey,
amount_to_move=amount_to_transfer,
)
except ValueError:
return False

if not Confirm.ask("Would you like to continue?"):
raise typer.Exit()
return False

# Perform transfer operation
if not unlock_key(wallet).success:
Expand Down Expand Up @@ -868,7 +875,10 @@ async def swap_stake(
"""
hotkey_ss58 = wallet.hotkey.ss58_address
if interactive_selection:
selection = await stake_swap_selection(subtensor, wallet)
try:
selection = await stake_swap_selection(subtensor, wallet)
except ValueError:
return False
origin_netuid = selection["origin_netuid"]
amount = selection["amount"]
destination_netuid = selection["destination_netuid"]
Expand Down Expand Up @@ -916,17 +926,20 @@ async def swap_stake(

# Slippage warning
if prompt:
await display_stake_movement_cross_subnets(
subtensor=subtensor,
origin_netuid=origin_netuid,
destination_netuid=destination_netuid,
origin_hotkey=hotkey_ss58,
destination_hotkey=hotkey_ss58,
amount_to_move=amount_to_swap,
)
try:
await display_stake_movement_cross_subnets(
subtensor=subtensor,
origin_netuid=origin_netuid,
destination_netuid=destination_netuid,
origin_hotkey=hotkey_ss58,
destination_hotkey=hotkey_ss58,
amount_to_move=amount_to_swap,
)
except ValueError:
return False

if not Confirm.ask("Would you like to continue?"):
raise typer.Exit()
return False

# Perform swap operation
if not unlock_key(wallet).success:
Expand Down
25 changes: 13 additions & 12 deletions bittensor_cli/src/commands/stake/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
from functools import partial

from typing import TYPE_CHECKING, Optional
import typer

from bittensor_wallet import Wallet
from bittensor_wallet.errors import KeyFileError
from rich.prompt import Confirm, Prompt
from rich.table import Table

Expand Down Expand Up @@ -67,13 +65,16 @@ async def unstake(
all_sn_dynamic_info = {info.netuid: info for info in all_sn_dynamic_info_}

if interactive:
hotkeys_to_unstake_from, unstake_all_from_hk = await _unstake_selection(
all_sn_dynamic_info,
ck_hk_identities,
old_identities,
stake_infos,
netuid=netuid,
)
try:
hotkeys_to_unstake_from, unstake_all_from_hk = await _unstake_selection(
all_sn_dynamic_info,
ck_hk_identities,
old_identities,
stake_infos,
netuid=netuid,
)
except ValueError:
return False
if unstake_all_from_hk:
hotkey_to_unstake_all = hotkeys_to_unstake_from[0]
unstake_all_alpha = Confirm.ask(
Expand Down Expand Up @@ -266,7 +267,7 @@ async def unstake(
_print_table_and_slippage(table, max_float_slippage, safe_staking)
if prompt:
if not Confirm.ask("Would you like to continue?"):
raise typer.Exit()
return False

# Execute extrinsics
if not unlock_key(wallet).success:
Expand Down Expand Up @@ -823,7 +824,7 @@ async def _unstake_selection(
):
if not stake_infos:
print_error("You have no stakes to unstake.")
raise typer.Exit()
raise ValueError

hotkey_stakes = {}
for stake_info in stake_infos:
Expand All @@ -840,7 +841,7 @@ async def _unstake_selection(
print_error(f"You have no stakes to unstake in subnet {netuid}.")
else:
print_error("You have no stakes to unstake.")
raise typer.Exit()
raise ValueError

hotkeys_info = []
for idx, (hotkey_ss58, netuid_stakes) in enumerate(hotkey_stakes.items()):
Expand Down
16 changes: 7 additions & 9 deletions bittensor_cli/src/commands/subnets/subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import json
import sqlite3
from typing import TYPE_CHECKING, Optional, cast
import typer

from bittensor_wallet import Wallet
from bittensor_wallet.errors import KeyFileError
from rich.prompt import Confirm, Prompt
from rich.console import Group
from rich.progress import Progress, BarColumn, TextColumn
Expand Down Expand Up @@ -814,7 +812,7 @@ async def show_root():
root_info = next((s for s in all_subnets if s.netuid == 0), None)
if root_info is None:
print_error("The root subnet does not exist")
raise typer.Exit()
return False

root_state, identities, old_identities = await asyncio.gather(
subtensor.get_subnet_state(netuid=0, block_hash=block_hash),
Expand Down Expand Up @@ -1017,7 +1015,7 @@ async def show_root():
async def show_subnet(netuid_: int):
if not await subtensor.subnet_exists(netuid=netuid):
err_console.print(f"[red]Subnet {netuid} does not exist[/red]")
raise typer.Exit()
return False
block_hash = await subtensor.substrate.get_chain_head()
(
subnet_info,
Expand All @@ -1036,15 +1034,15 @@ async def show_subnet(netuid_: int):
)
if subnet_state is None:
print_error(f"Subnet {netuid_} does not exist")
raise typer.Exit()
return False

if subnet_info is None:
print_error(f"Subnet {netuid_} does not exist")
raise typer.Exit()
return False

if len(subnet_state.hotkeys) == 0:
print_error(f"Subnet {netuid_} is currently empty with 0 UIDs registered.")
raise typer.Exit()
return False

# Define table properties
table = Table(
Expand Down Expand Up @@ -1381,7 +1379,7 @@ async def create(
" are you sure you wish to continue?"
):
console.print(":cross_mark: Aborted!")
raise typer.Exit()
return False

identity = prompt_for_identity(
current_identity=current_identity,
Expand Down Expand Up @@ -2135,7 +2133,7 @@ async def get_identity(subtensor: "SubtensorInterface", netuid: int, title: str

if not await subtensor.subnet_exists(netuid):
print_error(f"Subnet {netuid} does not exist.")
raise typer.Exit()
return None

with console.status(
":satellite: [bold green]Querying subnet identity...", spinner="earth"
Expand Down
5 changes: 2 additions & 3 deletions bittensor_cli/src/commands/sudo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
from typing import TYPE_CHECKING, Union, Optional

import typer
from bittensor_wallet import Wallet
from rich import box
from rich.table import Column, Table
Expand Down Expand Up @@ -593,7 +592,7 @@ async def sudo_set_hyperparameter(
if success:
console.print("\n")
print_verbose("Fetching hyperparameters")
await get_hyperparameters(subtensor, netuid=netuid)
return await get_hyperparameters(subtensor, netuid=netuid)


async def get_hyperparameters(subtensor: "SubtensorInterface", netuid: int):
Expand All @@ -606,7 +605,7 @@ async def get_hyperparameters(subtensor: "SubtensorInterface", netuid: int):
subnet_info = await subtensor.subnet(netuid)
if subnet_info is None:
print_error(f"Subnet with netuid {netuid} does not exist.")
raise typer.Exit()
return False

table = Table(
Column("[white]HYPERPARAMETER", style=COLOR_PALETTE["SUDO"]["HYPERPARAMETER"]),
Expand Down
3 changes: 1 addition & 2 deletions bittensor_cli/src/commands/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from rich.table import Column, Table
from rich.tree import Tree
from rich.padding import Padding
import typer

from bittensor_cli.src import COLOR_PALETTE
from bittensor_cli.src.bittensor import utils
Expand Down Expand Up @@ -120,7 +119,7 @@ async def regen_hotkey(
if json_path:
if not os.path.exists(json_path) or not os.path.isfile(json_path):
err_console.print(f"File {json_path} does not exist")
raise typer.Exit()
return False
with open(json_path, "r") as f:
json_str = f.read()

Expand Down
1 change: 1 addition & 0 deletions bittensor_cli/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ def version_as_int(version):
__new_signature_version__ = 360
return __version_as_int__


__version__ = "9.0.3"
__version_as_int__ = version_as_int(__version__)
1 change: 1 addition & 0 deletions tests/e2e_tests/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* btcli root undelegate-stake
"""


@pytest.mark.skip(reason="Root no longer applicable. We will update this.")
def test_root_commands(local_chain, wallet_setup):
"""
Expand Down
Loading

0 comments on commit c1625e4

Please sign in to comment.