Skip to content

Commit fa10630

Browse files
authored
Merge pull request #676 from opentensor/feat/thewhaleking/wallet-balance-sorting
Adds wallet balance sorting
2 parents b82e935 + 3be3dda commit fa10630

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

bittensor_cli/cli.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
Constants,
4040
COLORS,
4141
HYPERPARAMS,
42+
WalletOptions,
4243
)
4344
from bittensor_cli.src.bittensor import utils
4445
from bittensor_cli.src.bittensor.balances import Balance
@@ -92,6 +93,7 @@
9293
subnets,
9394
mechanisms as subnet_mechanisms,
9495
)
96+
from bittensor_cli.src.commands.wallets import SortByBalance
9597
from bittensor_cli.version import __version__, __version_as_int__
9698

9799
try:
@@ -1915,7 +1917,7 @@ def wallet_ask(
19151917
wallet_name: Optional[str],
19161918
wallet_path: Optional[str],
19171919
wallet_hotkey: Optional[str],
1918-
ask_for: Optional[list[Literal[WO.NAME, WO.PATH, WO.HOTKEY]]] = None,
1920+
ask_for: Optional[list[WalletOptions]] = None,
19191921
validate: WV = WV.WALLET,
19201922
return_wallet_and_hotkey: bool = False,
19211923
) -> Union[Wallet, tuple[Wallet, str]]:
@@ -3166,6 +3168,11 @@ def wallet_balance(
31663168
"-a",
31673169
help="Whether to display the balances for all the wallets.",
31683170
),
3171+
sort_by: Optional[wallets.SortByBalance] = typer.Option(
3172+
None,
3173+
"--sort",
3174+
help="When using `--all`, sorts the wallets by a given column",
3175+
),
31693176
network: Optional[list[str]] = Options.network,
31703177
quiet: bool = Options.quiet,
31713178
verbose: bool = Options.verbose,
@@ -3265,7 +3272,7 @@ def wallet_balance(
32653272
subtensor = self.initialize_chain(network)
32663273
return self._run_command(
32673274
wallets.wallet_balance(
3268-
wallet, subtensor, all_balances, ss58_addresses, json_output
3275+
wallet, subtensor, all_balances, ss58_addresses, sort_by, json_output
32693276
)
32703277
)
32713278

bittensor_cli/src/commands/wallets.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
from collections import defaultdict
6+
from enum import Enum
67
from typing import Generator, Optional, Union
78

89
import aiohttp
@@ -53,6 +54,27 @@
5354
)
5455

5556

57+
class SortByBalance(Enum):
58+
name = "name"
59+
free = "free"
60+
staked = "staked"
61+
total = "total"
62+
63+
64+
def _sort_by_balance_key(sort_by: SortByBalance):
65+
"""Get the sort key function based on the enum"""
66+
if sort_by == SortByBalance.name:
67+
return lambda row: row[0].lower() # Case-insensitive alphabetical sort
68+
elif sort_by == SortByBalance.free:
69+
return lambda row: row[2]
70+
elif sort_by == SortByBalance.staked:
71+
return lambda row: row[3]
72+
elif sort_by == SortByBalance.total:
73+
return lambda row: row[4]
74+
else:
75+
raise ValueError("Invalid sort key")
76+
77+
5678
async def associate_hotkey(
5779
wallet: Wallet,
5880
subtensor: SubtensorInterface,
@@ -565,6 +587,7 @@ async def wallet_balance(
565587
subtensor: SubtensorInterface,
566588
all_balances: bool,
567589
ss58_addresses: Optional[str] = None,
590+
sort_by: Optional[SortByBalance] = None,
568591
json_output: bool = False,
569592
):
570593
"""Retrieves the current balance of the specified wallet"""
@@ -644,14 +667,26 @@ async def wallet_balance(
644667
width=None,
645668
leading=True,
646669
)
647-
648-
for name, (coldkey, free, staked) in balances.items():
670+
balance_rows = [
671+
(name, coldkey, free, staked, free + staked)
672+
for (name, (coldkey, free, staked)) in balances.items()
673+
]
674+
sorted_balances = (
675+
sorted(
676+
balance_rows,
677+
key=_sort_by_balance_key(sort_by),
678+
reverse=(sort_by != SortByBalance.name),
679+
)
680+
if sort_by is not None
681+
else balance_rows
682+
)
683+
for name, coldkey, free, staked, total in sorted_balances:
649684
table.add_row(
650685
name,
651686
coldkey,
652687
str(free),
653688
str(staked),
654-
str(free + staked),
689+
str(total),
655690
)
656691
table.add_row()
657692
table.add_row(

0 commit comments

Comments
 (0)