Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds --no-prompt, fixes bugs + tests #31

Merged
merged 3 commits into from
Aug 29, 2024
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
69 changes: 51 additions & 18 deletions bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class Options:
True,
help="If set, waits until the transaction is finalized " "on the blockchain.",
)
prompt = typer.Option(
True,
"--prompt/--no-prompt",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You actually don't need to specify this. Typer does it automatically. Not a blocker, and it's totally fine to specify. Just a tip for the future.

help="Enable or disable interactive prompts.",
)


def list_prompt(init_var: list, list_type: type, help_text: str) -> list:
Expand Down Expand Up @@ -388,6 +393,7 @@ def __init__(self):
self.root_app.command("set-weights")(self.root_set_weights)
self.root_app.command("get-weights")(self.root_get_weights)
self.root_app.command("boost")(self.root_boost)
self.root_app.command("slash")(self.root_slash)
self.root_app.command("senate")(self.root_senate)
self.root_app.command("senate-vote")(self.root_senate_vote)
self.root_app.command("register")(self.root_register)
Expand Down Expand Up @@ -486,7 +492,7 @@ def main_callback(
self,
version: Annotated[
Optional[bool], typer.Option("--version", callback=version_callback)
] = None
] = None,
):
"""
Method called before all others when using any CLI command. Gives version if that flag is set, otherwise
Expand Down Expand Up @@ -878,6 +884,7 @@ def wallet_transfer(
wallet_hotkey: str = Options.wallet_hotkey,
network: str = Options.network,
chain: str = Options.chain,
prompt: bool = Options.prompt,
):
"""
# wallet transfer
Expand All @@ -903,7 +910,7 @@ def wallet_transfer(
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
subtensor = self.initialize_chain(network, chain)
return self._run_command(
wallets.transfer(wallet, subtensor, destination, amount)
wallets.transfer(wallet, subtensor, destination, amount, prompt)
)

def wallet_swap_hotkey(
Expand Down Expand Up @@ -1683,6 +1690,8 @@ def wallet_sign(
brevity or clarity. This command is essential for users to easily prove their ownership over a coldkey or a
hotkey.
"""
if not message:
message = typer.prompt("Enter the message to encode and sign: ")
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(wallets.sign(wallet, message))

Expand Down Expand Up @@ -1741,7 +1750,7 @@ def root_set_weights(
self,
network: str = Options.network,
chain: str = Options.chain,
wallet_name: str = Options.wallet_name,
wallet_name: str = Options.wallet_name_req,
wallet_path: str = Options.wallet_path,
wallet_hotkey: str = Options.wallet_hk_req,
netuids: list[int] = typer.Option(
Expand All @@ -1751,6 +1760,7 @@ def root_set_weights(
None,
help="Weights: e.g. `0.02 0.03 0.01` ...",
),
prompt: bool = Options.prompt,
):
"""
# root set-weights
Expand Down Expand Up @@ -1778,7 +1788,7 @@ def root_set_weights(
weights = list_prompt([], float, "Weights: e.g. 0.02, 0.03, 0.01 ")
self._run_command(
root.set_weights(
wallet, self.initialize_chain(network, chain), netuids, weights
wallet, self.initialize_chain(network, chain), netuids, weights, prompt
)
)

Expand Down Expand Up @@ -1842,7 +1852,7 @@ def root_boost(
self,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: str = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
netuid: int = Options.netuid,
Expand All @@ -1854,6 +1864,7 @@ def root_boost(
prompt=True,
help="Amount (float) to boost, (e.g. 0.01)",
),
prompt: bool = Options.prompt,
):
"""
# root boost
Expand Down Expand Up @@ -1913,15 +1924,15 @@ def root_boost(
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(
root.set_boost(
wallet, self.initialize_chain(network, chain), netuid, amount
wallet, self.initialize_chain(network, chain), netuid, amount, prompt
)
)

def root_slash(
self,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: str = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
netuid: int = Options.netuid,
Expand All @@ -1933,6 +1944,7 @@ def root_slash(
prompt=True,
help="Amount (float) to boost, (e.g. 0.01)",
),
prompt: bool = Options.prompt,
):
"""
# root slash
Expand Down Expand Up @@ -1988,15 +2000,15 @@ def root_slash(
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(
root.set_slash(
wallet, self.initialize_chain(network, chain), netuid, amount
wallet, self.initialize_chain(network, chain), netuid, amount, prompt
)
)

def root_senate_vote(
self,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
proposal: str = typer.Option(
Expand All @@ -2005,6 +2017,7 @@ def root_senate_vote(
"--proposal-hash",
help="The hash of the proposal to vote on.",
),
prompt: bool = Options.prompt,
):
"""
# root senate-vote
Expand All @@ -2028,7 +2041,9 @@ def root_senate_vote(
"""
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(
root.senate_vote(wallet, self.initialize_chain(network, chain), proposal)
root.senate_vote(
wallet, self.initialize_chain(network, chain), proposal, prompt
)
)

def root_senate(
Expand Down Expand Up @@ -2062,9 +2077,10 @@ def root_register(
self,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
prompt: bool = Options.prompt,
):
"""
# root register
Expand Down Expand Up @@ -2115,7 +2131,7 @@ def root_register(
"""
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(
root.register(wallet, self.initialize_chain(network, chain))
root.register(wallet, self.initialize_chain(network, chain), prompt)
)

def root_proposals(
Expand Down Expand Up @@ -2150,7 +2166,7 @@ def root_set_take(
self,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
take: float = typer.Option(None, help="The new take value."),
Expand Down Expand Up @@ -2205,6 +2221,7 @@ def root_delegate_stake(
wallet_hotkey: Optional[str] = Options.wallet_hotkey,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
prompt: bool = Options.prompt,
):
"""
# root delegate-stake
Expand Down Expand Up @@ -2261,6 +2278,7 @@ def root_delegate_stake(
self.initialize_chain(network, chain),
float(amount),
delegate_ss58key,
prompt,
)
)

Expand All @@ -2286,6 +2304,7 @@ def root_undelegate_stake(
wallet_hotkey: Optional[str] = Options.wallet_hotkey,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
prompt: bool = Options.prompt,
):
"""
# root undelegate-stake
Expand Down Expand Up @@ -2344,6 +2363,7 @@ def root_undelegate_stake(
self.initialize_chain(network, chain),
float(amount),
delegate_ss58key,
prompt,
)
)

Expand Down Expand Up @@ -2490,13 +2510,15 @@ def root_list_delegates(

return self._run_command(root.list_delegates(sub))

# TODO: Confirm if we need a command for this - currently registering to root auto makes u delegate
def root_nominate(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
prompt: bool = Options.prompt,
):
"""
# root nominate
Expand Down Expand Up @@ -2533,7 +2555,7 @@ def root_nominate(
"""
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(
root.nominate(wallet, self.initialize_chain(network, chain))
root.nominate(wallet, self.initialize_chain(network, chain), prompt)
)

def stake_show(
Expand Down Expand Up @@ -2651,6 +2673,7 @@ def stake_add(
wallet_hotkey: str = Options.wallet_hotkey,
network: str = Options.network,
chain: str = Options.chain,
prompt: bool = Options.prompt,
):
"""
# stake add
Expand Down Expand Up @@ -2678,6 +2701,7 @@ def stake_add(
This command is critical for users who wish to distribute their stakes among different neurons (hotkeys) on the
network. It allows for a strategic allocation of tokens to enhance network participation and influence.
"""
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
if stake_all and amount:
err_console.print(
"Cannot specify an amount and 'stake-all'. Choose one or the other."
Expand All @@ -2699,7 +2723,10 @@ def stake_add(
"You have specified including and excluding hotkeys. Select one or the other."
)
raise typer.Exit()
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
if not wallet_hotkey and not all_hotkeys and not include_hotkeys:
_hotkey_str = typer.style("hotkey", fg="red")
wallet_hotkey = typer.prompt(f"Enter {_hotkey_str} name: ")
wallet = self.wallet_ask(wallet.name, wallet_path, wallet_hotkey)
return self._run_command(
stake.stake_add(
wallet,
Expand All @@ -2710,6 +2737,7 @@ def stake_add(
include_hotkeys,
exclude_hotkeys,
all_hotkeys,
prompt,
)
)

Expand Down Expand Up @@ -2757,6 +2785,7 @@ def stake_remove(
help="When set, unstakes from all hotkeys associated with the wallet. Do not use if specifying "
"hotkeys in `--include-hotkeys`.",
),
prompt: bool = Options.prompt,
):
"""
# stake remove
Expand Down Expand Up @@ -2835,6 +2864,7 @@ def stake_remove(
amount,
max_stake,
unstake_all,
prompt,
)
)

Expand Down Expand Up @@ -3312,6 +3342,7 @@ def subnets_create(
wallet_hotkey: str = Options.wallet_hotkey,
network: str = Options.network,
chain: str = Options.chain,
prompt: bool = Options.prompt,
):
"""
# subnets create
Expand Down Expand Up @@ -3362,7 +3393,7 @@ def subnets_create(
"""
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(
subnets.create(wallet, self.initialize_chain(network, chain))
subnets.create(wallet, self.initialize_chain(network, chain), prompt)
)

def subnets_pow_register(
Expand Down Expand Up @@ -3461,12 +3492,13 @@ def subnets_pow_register(

def subnets_register(
self,
wallet_name: str = Options.wallet_name,
wallet_name: str = Options.wallet_name_req,
wallet_path: str = Options.wallet_path,
wallet_hotkey: str = Options.wallet_hk_req,
chain: str = Options.chain,
network: str = Options.network,
netuid: int = Options.netuid,
prompt: bool = Options.prompt,
):
"""
# subnets register
Expand Down Expand Up @@ -3514,6 +3546,7 @@ def subnets_register(
self.wallet_ask(wallet_name, wallet_path, wallet_hotkey),
self.initialize_chain(network, chain),
netuid,
prompt,
)
)

Expand Down
25 changes: 14 additions & 11 deletions bittensor_cli/src/bittensor/extrinsics/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,20 +443,23 @@ async def _do_set_weights():
return False

try:
weight_uids, weight_vals = convert_weights_and_uids_for_emit(netuids, weights)
with console.status("Setting root weights..."):
weight_uids, weight_vals = convert_weights_and_uids_for_emit(
netuids, weights
)

success, error_message = await _do_set_weights()
console.print(success, error_message)
success, error_message = await _do_set_weights()
console.print(success, error_message)

if not wait_for_finalization and not wait_for_inclusion:
return True
if not wait_for_finalization and not wait_for_inclusion:
return True

if success is True:
console.print(":white_heavy_check_mark: [green]Finalized[/green]")
return True
else:
err_console.print(f":cross_mark: [red]Failed[/red]: {error_message}")
return False
if success is True:
console.print(":white_heavy_check_mark: [green]Finalized[/green]")
return True
else:
err_console.print(f":cross_mark: [red]Failed[/red]: {error_message}")
return False

except Exception as e:
err_console.print(":cross_mark: [red]Failed[/red]: error:{}".format(e))
Expand Down
Loading
Loading