Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,11 @@ def initialize_chain(
network: Optional[str] = None,
) -> SubtensorInterface:
"""
Intelligently initializes a connection to the chain, depending on the supplied (or in config) values. Set's the
Intelligently initializes a connection to the chain, depending on the supplied (or in config) values. Sets the
`self.not_subtensor` object to this created connection.

:param network: Network name (e.g. finney, test, etc.)
:param chain: the chain endpoint (e.g. ws://127.0.0.1:9945, wss://entrypoint-finney.opentensor.ai:443, etc.)
:param network: Network name (e.g. finney, test, etc.) or
chain endpoint (e.g. ws://127.0.0.1:9945, wss://entrypoint-finney.opentensor.ai:443)
"""
if not self.not_subtensor:
if network:
Expand Down Expand Up @@ -897,7 +897,9 @@ def set_config(
if valid_endpoint:
if valid_endpoint in Constants.network_map.values():
known_network = next(
key for key, value in Constants.network_map.items() if value == network
key
for key, value in Constants.network_map.items()
if value == network
)
args["network"] = known_network
if not Confirm.ask(
Expand Down Expand Up @@ -2802,7 +2804,7 @@ def root_my_delegates(
wallet_path,
wallet_hotkey,
ask_for=([WO.NAME] if not all_wallets else [WO.PATH]),
validate=WV.WALLET if not all_wallets else WV.NONE
validate=WV.WALLET if not all_wallets else WV.NONE,
)
self._run_command(
root.my_delegates(wallet, self.initialize_chain(network), all_wallets)
Expand Down Expand Up @@ -2860,17 +2862,20 @@ def root_list_delegates(

[green]$[/green] btcli root list_delegates --subtensor.network finney # can also be `test` or `local`

[blue bold]NOTE[/blue bold]: This commmand is intended for use within a
[blue bold]NOTE[/blue bold]: This command is intended for use within a
console application. It prints directly to the console and does not return any value.
"""
self.verbosity_handler(quiet, verbose)

if network:
if network == "finney":
network = "wss://archive.chain.opentensor.ai:443"
elif self.config.get("network"):
if self.config.get("network") == "finney":
network = "wss://archive.chain.opentensor.ai:443"
elif (conf_net := self.config.get("network")) == "finney":
network = "wss://archive.chain.opentensor.ai:443"
elif conf_net:
network = conf_net
else:
network = "wss://archive.chain.opentensor.ai:443"

sub = self.initialize_chain(network)
return self._run_command(root.list_delegates(sub))
Expand Down
6 changes: 4 additions & 2 deletions bittensor_cli/src/bittensor/subtensor_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
console,
err_console,
decode_hex_identity_dict,
validate_chain_endpoint
validate_chain_endpoint,
)


Expand Down Expand Up @@ -84,7 +84,9 @@ def __init__(self, network):
self.chain_endpoint = network
if network in Constants.network_map.values():
self.network = next(
key for key, value in Constants.network_map.items() if value == network
key
for key, value in Constants.network_map.items()
if value == network
)
else:
self.network = "custom"
Expand Down
25 changes: 16 additions & 9 deletions bittensor_cli/src/commands/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,19 @@ async def _do_delegation(staking_balance_: Balance) -> tuple[bool, str]:
call = await subtensor.substrate.compose_call(
call_module="SubtensorModule",
call_function="add_stake",
call_params={"hotkey": delegate_ss58, "amount_staked": staking_balance_.rao},
call_params={
"hotkey": delegate_ss58,
"amount_staked": staking_balance_.rao,
},
)
else:
call = await subtensor.substrate.compose_call(
call_module="SubtensorModule",
call_function="remove_stake",
call_params={"hotkey": delegate_ss58, "amount_unstaked": staking_balance_.rao},
call_params={
"hotkey": delegate_ss58,
"amount_unstaked": staking_balance_.rao,
},
)
return await subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization
Expand Down Expand Up @@ -576,7 +582,7 @@ async def get_stake_for_coldkey_and_hotkey(
staking_balance = Balance.from_tao(amount)

# Check enough balance to stake.
if delegate_string == "delegate" and staking_balance > my_prev_coldkey_balance:
if delegate_string == "delegate" and staking_balance > my_prev_coldkey_balance:
err_console.print(
":cross_mark: [red]Not enough balance to stake[/red]:\n"
f" [bold blue]current balance[/bold blue]:{my_prev_coldkey_balance}\n"
Expand Down Expand Up @@ -726,21 +732,22 @@ async def _get_list() -> tuple:
)
return sm, rn, di, ts


with console.status(
f":satellite: Syncing with chain: [white]{subtensor}[/white] ...",
spinner="aesthetic",
):

senate_members, root_neurons, delegate_info, total_stakes = await _get_list()
total_tao = sum(float(Balance.from_rao(total_stakes[neuron.hotkey])) for neuron in root_neurons)
total_tao = sum(
float(Balance.from_rao(total_stakes[neuron.hotkey]))
for neuron in root_neurons
)

table = Table(
Column(
"[bold white]UID",
style="dark_orange",
no_wrap=True,
footer=f"[bold]{len(root_neurons)}[/bold]"
footer=f"[bold]{len(root_neurons)}[/bold]",
),
Column(
"[bold white]NAME",
Expand All @@ -757,7 +764,7 @@ async def _get_list() -> tuple:
justify="right",
style="light_goldenrod2",
no_wrap=True,
footer=f"{total_tao:.2f} (\u03c4) "
footer=f"{total_tao:.2f} (\u03c4) ",
),
Column(
"[bold white]SENATOR",
Expand All @@ -777,7 +784,7 @@ async def _get_list() -> tuple:
f"[red]Error: No neurons detected on the network:[/red] [white]{subtensor}"
)
raise typer.Exit()

sorted_root_neurons = sorted(
root_neurons,
key=lambda neuron: float(Balance.from_rao(total_stakes[neuron.hotkey])),
Expand Down
58 changes: 32 additions & 26 deletions tests/e2e_tests/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_root_commands(local_chain, wallet_setup):
command="root",
sub_command="list",
extra_args=[
"--chain",
"--network",
"ws://127.0.0.1:9945",
],
)
Expand All @@ -93,7 +93,7 @@ def test_root_commands(local_chain, wallet_setup):
command="root",
sub_command="list-delegates",
extra_args=[
"--chain",
"--network",
"ws://127.0.0.1:9945",
],
)
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_root_commands(local_chain, wallet_setup):
extra_args=[
"--wallet-path",
wallet_path_bob,
"--chain",
"--network",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet_bob.name,
Expand All @@ -150,7 +150,7 @@ def test_root_commands(local_chain, wallet_setup):
command="root",
sub_command="list-delegates",
extra_args=[
"--chain",
"--network",
"ws://127.0.0.1:9945",
],
)
Expand All @@ -168,14 +168,14 @@ def test_root_commands(local_chain, wallet_setup):
extra_args=[
"--wallet-path",
wallet_path_alice,
"--chain",
"--network",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet_alice.name,
"--delegate-ss58key",
wallet_bob.hotkey.ss58_address,
"--amount",
f"10",
"10",
"--no-prompt",
],
)
Expand All @@ -185,13 +185,17 @@ def test_root_commands(local_chain, wallet_setup):
exec_command=exec_command_alice,
wallet=wallet_alice,
delegate_ss58key=wallet_bob.hotkey.ss58_address,
delegate_amount=10
delegate_amount=10,
)

check_balance(
exec_command=exec_command_alice,
wallet=wallet_alice,
expected_balance={'free_balance': 999990.0, 'staked_balance': 10.0, 'total_balance': 1000000.0},
expected_balance={
"free_balance": 999990.0,
"staked_balance": 10.0,
"total_balance": 1000000.0,
},
)

# TODO: Ask nucleus the rate limit and wait epoch
Expand All @@ -206,14 +210,12 @@ def test_root_commands(local_chain, wallet_setup):
extra_args=[
"--wallet-path",
wallet_path_alice,
"--chain",
"--network",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet_alice.name,
"--delegate-ss58key",
wallet_bob.hotkey.ss58_address,
"--network",
"local",
"--amount",
f"10",
"--no-prompt",
Expand All @@ -224,7 +226,11 @@ def test_root_commands(local_chain, wallet_setup):
check_balance(
exec_command=exec_command_alice,
wallet=wallet_alice,
expected_balance={'free_balance': 1000000.0, 'staked_balance': 0.0, 'total_balance': 1000000.0},
expected_balance={
"free_balance": 1000000.0,
"staked_balance": 0.0,
"total_balance": 1000000.0,
},
)

# TODO: Ask nucleus the rate limit and wait epoch
Expand All @@ -239,14 +245,12 @@ def test_root_commands(local_chain, wallet_setup):
extra_args=[
"--wallet-path",
wallet_path_alice,
"--chain",
"--network",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet_alice.name,
"--delegate-ss58key",
wallet_bob.hotkey.ss58_address,
"--network",
"local",
"--all",
"--no-prompt",
],
Expand All @@ -263,7 +267,11 @@ def test_root_commands(local_chain, wallet_setup):
check_balance(
exec_command=exec_command_alice,
wallet=wallet_alice,
expected_balance={'free_balance': 0.0000005, 'staked_balance': 999999.9999995, 'total_balance': 1000000.0},
expected_balance={
"free_balance": 0.0000005,
"staked_balance": 999999.9999995,
"total_balance": 1000000.0,
},
)

# TODO: Ask nucleus the rate limit and wait epoch
Expand All @@ -278,14 +286,12 @@ def test_root_commands(local_chain, wallet_setup):
extra_args=[
"--wallet-path",
wallet_path_alice,
"--chain",
"--network",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet_alice.name,
"--delegate-ss58key",
wallet_bob.hotkey.ss58_address,
"--network",
"local",
"--all",
"--no-prompt",
],
Expand All @@ -295,7 +301,11 @@ def test_root_commands(local_chain, wallet_setup):
check_balance(
exec_command=exec_command_alice,
wallet=wallet_alice,
expected_balance={'free_balance': 1000000.0, 'staked_balance': 0.0, 'total_balance': 1000000.0},
expected_balance={
"free_balance": 1000000.0,
"staked_balance": 0.0,
"total_balance": 1000000.0,
},
)

print("✅ Passed Root commands")
Expand All @@ -309,12 +319,10 @@ def check_my_delegates(exec_command, wallet, delegate_ss58key, delegate_amount):
extra_args=[
"--wallet-path",
wallet.path,
"--chain",
"--network",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet.name,
"--network",
"local",
],
)
# First row are headers, records start from second row
Expand Down Expand Up @@ -345,12 +353,10 @@ def check_balance(exec_command, wallet, expected_balance):
extra_args=[
"--wallet-path",
wallet.path,
"--chain",
"--network",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet.name,
"--network",
"local",
],
)

Expand Down