From 9e103aa44d160573974d1b63effef63f1c95bf27 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Tue, 13 Dec 2022 16:54:46 -0500 Subject: [PATCH 1/3] notify user that hotkey is not registered --- bittensor/_cli/cli_impl.py | 118 +++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 44 deletions(-) diff --git a/bittensor/_cli/cli_impl.py b/bittensor/_cli/cli_impl.py index 846d5111ee..510d3aad6e 100644 --- a/bittensor/_cli/cli_impl.py +++ b/bittensor/_cli/cli_impl.py @@ -287,30 +287,43 @@ def unstake( self ): ] else: # Do regular unstake - subtensor.unstake( wallet, amount = None if self.config.get('unstake_all') else self.config.get('amount'), wait_for_inclusion = True, prompt = not self.config.no_prompt ) - return None - - + # Only self.config.wallet.hotkey is specified. + # so we stake to that single hotkey. + assert self.config.wallet.hotkey is not None + wallets_to_unstake_from = [ bittensor.wallet( config = self.config ) ] final_wallets: List['bittensor.wallet'] = [] final_amounts: List[Union[float, Balance]] = [] - for wallet in tqdm(wallets_to_unstake_from): - wallet: bittensor.wallet - if not wallet.is_registered(): - # Skip unregistered hotkeys. - continue - unstake_amount_tao: float = self.config.get('amount') - if self.config.get('max_stake'): - wallet_stake: Balance = wallet.get_stake() - unstake_amount_tao: float = wallet_stake.tao - self.config.get('max_stake') - self.config.amount = unstake_amount_tao - if unstake_amount_tao < 0: - # Skip if max_stake is greater than current stake. - continue - - final_wallets.append(wallet) - final_amounts.append(unstake_amount_tao) + with tqdm(wallets_to_unstake_from, desc = 'Calculating unstake amounts') as pbar: + for wallet in wallets_to_unstake_from: + wallet: bittensor.wallet + if not wallet.is_registered(): + # Skip unregistered hotkeys. + if (len(wallets_to_unstake_from) == 1): + # Only one hotkey, error + pbar.close() + bittensor.__console__.print(f"[red]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Aborting.[/red]") + return None + else: + # Otherwise, print warning and skip + bittensor.__console__.print(f"[yellow]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Skipping.[/yellow]") + continue + + unstake_amount_tao: float = self.config.get('amount') + if self.config.get('max_stake'): + wallet_stake: Balance = wallet.get_stake() + unstake_amount_tao: float = wallet_stake.tao - self.config.get('max_stake') + self.config.amount = unstake_amount_tao + if unstake_amount_tao < 0: + # Skip if max_stake is greater than current stake. + pbar.update(1) + continue + + final_wallets.append(wallet) + final_amounts.append(unstake_amount_tao) + + pbar.update(1) # Ask to unstake if not self.config.no_prompt: @@ -320,7 +333,11 @@ def unstake( self ): ]) ): return None - + + if len(final_wallets) == 1: + # Unstake from single hotkey. + return subtensor.unstake( wallet=final_wallets[0], amount = None if self.config.get('unstake_all') else final_amounts[0], wait_for_inclusion = True, prompt = not self.config.no_prompt ) + subtensor.unstake_multiple( wallets = final_wallets, amounts = None if self.config.get('unstake_all') else final_amounts, wait_for_inclusion = True, prompt = False ) @@ -362,29 +379,42 @@ def stake( self ): wallet_balance: Balance = wallet_0.get_balance() final_wallets: List['bittensor.wallet'] = [] final_amounts: List[Union[float, Balance]] = [] - for wallet in tqdm(wallets_to_stake_to): - wallet: bittensor.wallet - if not wallet.is_registered(): - # Skip unregistered hotkeys. - continue - - # Assign decrypted coldkey from wallet_0 - # so we don't have to decrypt again - wallet._coldkey = wallet_0._coldkey - - stake_amount_tao: float = self.config.get('amount') - if self.config.get('max_stake'): - wallet_stake: Balance = wallet.get_stake() - stake_amount_tao: float = self.config.get('max_stake') - wallet_stake.tao - - # If the max_stake is greater than the current wallet balance, stake the entire balance. - stake_amount_tao: float = min(stake_amount_tao, wallet_balance.tao) - if stake_amount_tao <= 0.00001: # Threshold because of fees, might create a loop otherwise - # Skip hotkey if max_stake is less than current stake. - continue - wallet_balance = Balance.from_tao(wallet_balance.tao - stake_amount_tao) - final_amounts.append(stake_amount_tao) - final_wallets.append(wallet) + + with tqdm(wallets_to_stake_to, desc="Calculating stake amounts...") as pbar: + for wallet in wallets_to_stake_to: + wallet: bittensor.wallet + if not wallet.is_registered(): + # Skip unregistered hotkeys. + if (len(wallets_to_stake_to) == 1): + # Only one hotkey, error + pbar.close() + bittensor.__console__.print(f"[red]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Aborting.[/red]") + return None + else: + # Otherwise, print warning and skip + bittensor.__console__.print(f"[yellow]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Skipping.[/yellow]") + continue + + # Assign decrypted coldkey from wallet_0 + # so we don't have to decrypt again + wallet._coldkey = wallet_0._coldkey + + stake_amount_tao: float = self.config.get('amount') + if self.config.get('max_stake'): + wallet_stake: Balance = wallet.get_stake() + stake_amount_tao: float = self.config.get('max_stake') - wallet_stake.tao + + # If the max_stake is greater than the current wallet balance, stake the entire balance. + stake_amount_tao: float = min(stake_amount_tao, wallet_balance.tao) + if stake_amount_tao <= 0.00001: # Threshold because of fees, might create a loop otherwise + # Skip hotkey if max_stake is less than current stake. + pbar.update(1) + continue + wallet_balance = Balance.from_tao(wallet_balance.tao - stake_amount_tao) + final_amounts.append(stake_amount_tao) + final_wallets.append(wallet) + + pbar.update(1) if len(final_wallets) == 0: # No wallets to stake to. From aafab403483175f976ed5646873a1fc8f4ce3711 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Mon, 23 Jan 2023 12:48:34 -0500 Subject: [PATCH 2/3] Revert "notify user that hotkey is not registered" This reverts commit 9e103aa44d160573974d1b63effef63f1c95bf27. --- bittensor/_cli/cli_impl.py | 118 ++++++++++++++----------------------- 1 file changed, 44 insertions(+), 74 deletions(-) diff --git a/bittensor/_cli/cli_impl.py b/bittensor/_cli/cli_impl.py index 510d3aad6e..846d5111ee 100644 --- a/bittensor/_cli/cli_impl.py +++ b/bittensor/_cli/cli_impl.py @@ -287,43 +287,30 @@ def unstake( self ): ] else: # Do regular unstake - # Only self.config.wallet.hotkey is specified. - # so we stake to that single hotkey. - assert self.config.wallet.hotkey is not None - wallets_to_unstake_from = [ bittensor.wallet( config = self.config ) ] + subtensor.unstake( wallet, amount = None if self.config.get('unstake_all') else self.config.get('amount'), wait_for_inclusion = True, prompt = not self.config.no_prompt ) + return None + + final_wallets: List['bittensor.wallet'] = [] final_amounts: List[Union[float, Balance]] = [] + for wallet in tqdm(wallets_to_unstake_from): + wallet: bittensor.wallet + if not wallet.is_registered(): + # Skip unregistered hotkeys. + continue - with tqdm(wallets_to_unstake_from, desc = 'Calculating unstake amounts') as pbar: - for wallet in wallets_to_unstake_from: - wallet: bittensor.wallet - if not wallet.is_registered(): - # Skip unregistered hotkeys. - if (len(wallets_to_unstake_from) == 1): - # Only one hotkey, error - pbar.close() - bittensor.__console__.print(f"[red]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Aborting.[/red]") - return None - else: - # Otherwise, print warning and skip - bittensor.__console__.print(f"[yellow]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Skipping.[/yellow]") - continue - - unstake_amount_tao: float = self.config.get('amount') - if self.config.get('max_stake'): - wallet_stake: Balance = wallet.get_stake() - unstake_amount_tao: float = wallet_stake.tao - self.config.get('max_stake') - self.config.amount = unstake_amount_tao - if unstake_amount_tao < 0: - # Skip if max_stake is greater than current stake. - pbar.update(1) - continue - - final_wallets.append(wallet) - final_amounts.append(unstake_amount_tao) - - pbar.update(1) + unstake_amount_tao: float = self.config.get('amount') + if self.config.get('max_stake'): + wallet_stake: Balance = wallet.get_stake() + unstake_amount_tao: float = wallet_stake.tao - self.config.get('max_stake') + self.config.amount = unstake_amount_tao + if unstake_amount_tao < 0: + # Skip if max_stake is greater than current stake. + continue + + final_wallets.append(wallet) + final_amounts.append(unstake_amount_tao) # Ask to unstake if not self.config.no_prompt: @@ -333,11 +320,7 @@ def unstake( self ): ]) ): return None - - if len(final_wallets) == 1: - # Unstake from single hotkey. - return subtensor.unstake( wallet=final_wallets[0], amount = None if self.config.get('unstake_all') else final_amounts[0], wait_for_inclusion = True, prompt = not self.config.no_prompt ) - + subtensor.unstake_multiple( wallets = final_wallets, amounts = None if self.config.get('unstake_all') else final_amounts, wait_for_inclusion = True, prompt = False ) @@ -379,42 +362,29 @@ def stake( self ): wallet_balance: Balance = wallet_0.get_balance() final_wallets: List['bittensor.wallet'] = [] final_amounts: List[Union[float, Balance]] = [] - - with tqdm(wallets_to_stake_to, desc="Calculating stake amounts...") as pbar: - for wallet in wallets_to_stake_to: - wallet: bittensor.wallet - if not wallet.is_registered(): - # Skip unregistered hotkeys. - if (len(wallets_to_stake_to) == 1): - # Only one hotkey, error - pbar.close() - bittensor.__console__.print(f"[red]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Aborting.[/red]") - return None - else: - # Otherwise, print warning and skip - bittensor.__console__.print(f"[yellow]Hotkey [bold]{wallet.hotkey_str}[/bold] is not registered. Skipping.[/yellow]") - continue - - # Assign decrypted coldkey from wallet_0 - # so we don't have to decrypt again - wallet._coldkey = wallet_0._coldkey - - stake_amount_tao: float = self.config.get('amount') - if self.config.get('max_stake'): - wallet_stake: Balance = wallet.get_stake() - stake_amount_tao: float = self.config.get('max_stake') - wallet_stake.tao - - # If the max_stake is greater than the current wallet balance, stake the entire balance. - stake_amount_tao: float = min(stake_amount_tao, wallet_balance.tao) - if stake_amount_tao <= 0.00001: # Threshold because of fees, might create a loop otherwise - # Skip hotkey if max_stake is less than current stake. - pbar.update(1) - continue - wallet_balance = Balance.from_tao(wallet_balance.tao - stake_amount_tao) - final_amounts.append(stake_amount_tao) - final_wallets.append(wallet) - - pbar.update(1) + for wallet in tqdm(wallets_to_stake_to): + wallet: bittensor.wallet + if not wallet.is_registered(): + # Skip unregistered hotkeys. + continue + + # Assign decrypted coldkey from wallet_0 + # so we don't have to decrypt again + wallet._coldkey = wallet_0._coldkey + + stake_amount_tao: float = self.config.get('amount') + if self.config.get('max_stake'): + wallet_stake: Balance = wallet.get_stake() + stake_amount_tao: float = self.config.get('max_stake') - wallet_stake.tao + + # If the max_stake is greater than the current wallet balance, stake the entire balance. + stake_amount_tao: float = min(stake_amount_tao, wallet_balance.tao) + if stake_amount_tao <= 0.00001: # Threshold because of fees, might create a loop otherwise + # Skip hotkey if max_stake is less than current stake. + continue + wallet_balance = Balance.from_tao(wallet_balance.tao - stake_amount_tao) + final_amounts.append(stake_amount_tao) + final_wallets.append(wallet) if len(final_wallets) == 0: # No wallets to stake to. From 9c4d3221c2eb987d58f66bbb8d5c1a17665bcc30 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Mon, 23 Jan 2023 12:55:04 -0500 Subject: [PATCH 3/3] notify if not registered --- bittensor/_cli/commands/stake.py | 12 ++++++++++++ bittensor/_cli/commands/unstake.py | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/bittensor/_cli/commands/stake.py b/bittensor/_cli/commands/stake.py index ac9b628ada..96274e4bd3 100644 --- a/bittensor/_cli/commands/stake.py +++ b/bittensor/_cli/commands/stake.py @@ -79,6 +79,18 @@ def run( cli ): final_amounts: List[Union[float, Balance]] = [] for hotkey in tqdm(hotkeys_to_stake_to): hotkey: Tuple[Optional[str], str] # (hotkey_name (or None), hotkey_ss58) + if not subtensor.is_hotkey_registered_any( hotkey_ss58 = hotkey ): + # Hotkey is not registered. + if (len(hotkeys_to_stake_to) == 1): + # Only one hotkey, error + bittensor.__console__.print(f"[red]Hotkey [bold]{hotkey}[/bold] is not registered. Aborting.[/red]") + return None + else: + # Otherwise, print warning and skip + bittensor.__console__.print(f"[yellow]Hotkey [bold]{hotkey}[/bold] is not registered. Skipping.[/yellow]") + continue + + stake_amount_tao: float = config.get('amount') if config.get('max_stake'): # Get the current stake of the hotkey from this coldkey. diff --git a/bittensor/_cli/commands/unstake.py b/bittensor/_cli/commands/unstake.py index 0d864563aa..05688434e7 100644 --- a/bittensor/_cli/commands/unstake.py +++ b/bittensor/_cli/commands/unstake.py @@ -151,6 +151,17 @@ def run( cli ): final_amounts: List[Union[float, Balance]] = [] for hotkey in tqdm(hotkeys_to_unstake_from): hotkey: Tuple[Optional[str], str] # (hotkey_name (or None), hotkey_ss58) + if not subtensor.is_hotkey_registered_any( hotkey_ss58 = hotkey ): + # Hotkey is not registered. + if (len(hotkeys_to_unstake_from) == 1): + # Only one hotkey, error + bittensor.__console__.print(f"[red]Hotkey [bold]{hotkey}[/bold] is not registered. Aborting.[/red]") + return None + else: + # Otherwise, print warning and skip + bittensor.__console__.print(f"[yellow]Hotkey [bold]{hotkey}[/bold] is not registered. Skipping.[/yellow]") + continue + unstake_amount_tao: float = cli.config.get('amount') # The amount specified to unstake. hotkey_stake: Balance = subtensor.get_stake_for_coldkey_and_hotkey( hotkey_ss58 = hotkey[1], coldkey_ss58 = wallet.coldkey.ss58_address ) if unstake_amount_tao == None: