Skip to content

Commit

Permalink
Fix/move overview args to cli (#867)
Browse files Browse the repository at this point in the history
* move cli args to CLI and fix overview

* use dot get

* fix tests

* add hotkeys/all_hotkeys to (un)stake

* fix default

* fix default in unstake

* add unstake multiple

* add add stake multiple

* move all/hotkeys back to wallet args

* convert to balance first
add catch for unstake multi

* fix ref to wallet

* fix test patch for multi hotkeys

* try to fix tests

* fix tests patch

* fix mock wallet length

* don't use new?

* fix call args get

* typo

* fix typo
  • Loading branch information
Cameron Fairchild authored Aug 15, 2022
1 parent c546d3f commit fb6be7c
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 79 deletions.
25 changes: 24 additions & 1 deletion bittensor/_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ def config() -> 'bittensor.config':
help='''Set the output width of the overview. Defaults to automatic width from terminal.''',
default=None,
)
overview_parser.add_argument(
'--sort_by',
'--wallet.sort_by',
dest='sort_by',
required=False,
action='store',
default="",
type=str,
help='''Sort the hotkeys by the specified column title (e.g. name, uid, axon).'''
)
overview_parser.add_argument(
'--sort_order',
'--wallet.sort_order',
dest="sort_order",
required=False,
action='store',
default="ascending",
type=str,
help='''Sort the hotkeys in the specified ordering. (ascending/asc or descending/desc/reverse)'''
)

bittensor.wallet.add_args( overview_parser )
bittensor.subtensor.add_args( overview_parser )

Expand Down Expand Up @@ -501,6 +522,7 @@ def config() -> 'bittensor.config':
help='''Set true to avoid prompting the user.''',
default=False,
)

bittensor.wallet.add_args( unstake_parser )
bittensor.subtensor.add_args( unstake_parser )

Expand Down Expand Up @@ -539,6 +561,7 @@ def config() -> 'bittensor.config':
help='''Set true to avoid prompting the user.''',
default=False,
)

bittensor.wallet.add_args( stake_parser )
bittensor.subtensor.add_args( stake_parser )

Expand Down Expand Up @@ -696,7 +719,7 @@ def check_unstake_config( config: 'bittensor.Config' ):
if config.wallet.get('all_hotkeys'):
hotkeys = "all hotkeys"
elif config.wallet.get('hotkeys'):
hotkeys = str(config.hotkeys).replace('[', '').replace(']', '')
hotkeys = str(config.wallet.hotkeys).replace('[', '').replace(']', '')
else:
hotkeys = str(config.wallet.hotkey)
if not Confirm.ask("Unstake all Tao from: [bold]'{}'[/bold]?".format(hotkeys)):
Expand Down
55 changes: 23 additions & 32 deletions bittensor/_cli/cli_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import os
import sys
from typing import List, Union
from typing import List, Union, Optional

from cachetools import Cache

Expand Down Expand Up @@ -259,7 +259,6 @@ def transfer( self ):
def unstake( self ):
r""" Unstake token of amount from hotkey(s).
"""
# TODO: Implement this without re-unlocking the coldkey.
config = self.config.copy()
config.hotkey = None
wallet = bittensor.wallet( config = self.config )
Expand All @@ -271,7 +270,7 @@ def unstake( self ):
all_hotkeys: List[bittensor.wallet] = self._get_hotkey_wallets_for_wallet( wallet = wallet )
# Exclude hotkeys that are specified.
wallets_to_unstake_from = [
wallet for wallet in all_hotkeys if wallet.hotkey_str not in self.config.wallet.get('hotkeys')
wallet for wallet in all_hotkeys if wallet.hotkey_str not in self.config.wallet.get('hotkeys', [])
]

elif self.config.wallet.get('hotkeys'):
Expand All @@ -284,9 +283,7 @@ def unstake( self ):
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

wallet_0: 'bittensor.wallet' = wallets_to_unstake_from[0]
# Decrypt coldkey for all wallet(s) to use
wallet_0.coldkey


final_wallets: List['bittensor.wallet'] = []
final_amounts: List[Union[float, Balance]] = []
Expand All @@ -295,9 +292,6 @@ def unstake( self ):
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

unstake_amount_tao: float = self.config.get('amount')
if self.config.get('max_stake'):
Expand All @@ -315,19 +309,17 @@ def unstake( self ):
if not self.config.no_prompt:
if not Confirm.ask("Do you want to unstake from the following keys:\n" + \
"".join([
f" [bold white]- {wallet.hotkey_str}: {amount}𝜏[/bold white]\n" for wallet, amount in zip(final_wallets, final_amounts)
f" [bold white]- {wallet.hotkey_str}: {amount.tao}𝜏[/bold white]\n" for wallet, amount in zip(final_wallets, final_amounts)
])
):
return None

for wallet, amount in zip(final_wallets, final_amounts):
subtensor.unstake( wallet, amount = None if self.config.get('unstake_all') else amount, wait_for_inclusion = True, prompt = False )

subtensor.unstake_multiple( wallets = final_wallets, amounts = None if self.config.get('unstake_all') else final_amounts, wait_for_inclusion = True, prompt = False )


def stake( self ):
r""" Stake token of amount to hotkey(s).
"""
# TODO: Implement this without re-unlocking the coldkey.
config = self.config.copy()
config.hotkey = None
wallet = bittensor.wallet( config = config )
Expand All @@ -339,7 +331,7 @@ def stake( self ):
all_hotkeys: List[bittensor.wallet] = self._get_hotkey_wallets_for_wallet( wallet = wallet )
# Exclude hotkeys that are specified.
wallets_to_stake_to = [
wallet for wallet in all_hotkeys if wallet.hotkey_str not in self.config.wallet.get('hotkeys')
wallet for wallet in all_hotkeys if wallet.hotkey_str not in self.config.wallet.get('hotkeys', [])
]

elif self.config.wallet.get('hotkeys'):
Expand Down Expand Up @@ -394,8 +386,7 @@ def stake( self ):
):
return None

for wallet, amount in zip(final_wallets, final_amounts):
subtensor.add_stake( wallet, amount = None if self.config.get('stake_all') else amount, wait_for_inclusion = True, prompt = False )
subtensor.add_stake_multiple( wallets = final_wallets, amounts = None if self.config.get('stake_all') else final_amounts, wait_for_inclusion = True, prompt = False )


def set_weights( self ):
Expand Down Expand Up @@ -604,24 +595,14 @@ def overview(self):

all_hotkeys = []
total_balance = bittensor.Balance(0)

# We are printing for every wallet.
# We are printing for every coldkey.
if self.config.all:
cold_wallets = CLI._get_coldkey_wallets_for_path(self.config.wallet.path)
for cold_wallet in tqdm(cold_wallets, desc="Pulling balances"):
if cold_wallet.coldkeypub_file.exists_on_device() and not cold_wallet.coldkeypub_file.is_encrypted():
total_balance = total_balance + subtensor.get_balance( cold_wallet.coldkeypub.ss58_address )
all_hotkeys = CLI._get_all_wallets_for_path( self.config.wallet.path )

# We are printing for a select number of hotkeys.
elif self.config.wallet.hotkeys:
# Only show hotkeys for wallets in the list
all_hotkeys = [hotkey for hotkey in all_hotkeys if hotkey.hotkey_str in self.config.wallet.hotkeys]
coldkey_wallet = bittensor.wallet( config = self.config )
if coldkey_wallet.coldkeypub_file.exists_on_device() and not coldkey_wallet.coldkeypub_file.is_encrypted():
total_balance = subtensor.get_balance( coldkey_wallet.coldkeypub.ss58_address )

# We are printing for all keys under the wallet.
else:
# We are only printing keys for a single coldkey
coldkey_wallet = bittensor.wallet( config = self.config )
Expand All @@ -632,6 +613,16 @@ def overview(self):
return
all_hotkeys = CLI._get_hotkey_wallets_for_wallet( coldkey_wallet )

# We are printing for a select number of hotkeys from all_hotkeys.

if self.config.wallet.get('hotkeys', []):
if not self.config.get('all_hotkeys', False):
# We are only showing hotkeys that are specified.
all_hotkeys = [hotkey for hotkey in all_hotkeys if hotkey.hotkey_str in self.config.wallet.hotkeys]
else:
# We are excluding the specified hotkeys from all_hotkeys.
all_hotkeys = [hotkey for hotkey in all_hotkeys if hotkey.hotkey_str not in self.config.wallet.hotkeys]

# Check we have keys to display.
if len(all_hotkeys) == 0:
console.print("[red]No wallets found.[/red]")
Expand Down Expand Up @@ -740,10 +731,10 @@ def overview(self):

console.clear()

sort_by: str = self.config.wallet.sort_by
sort_order: str = self.config.wallet.sort_order
sort_by: Optional[str] = self.config.get('sort_by', None)
sort_order: Optional[str] = self.config.get('sort_order', None)

if sort_by != "":
if sort_by is not None and sort_by != "":
column_to_sort_by: int = 0
highest_matching_ratio: int = 0
sort_descending: bool = False # Default sort_order to ascending
Expand Down
Loading

0 comments on commit fb6be7c

Please sign in to comment.