Skip to content

Commit 5ee38d7

Browse files
committed
I'm not proud of this code
1 parent 1001557 commit 5ee38d7

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

bittensor_cli/cli.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import warnings
1313
from dataclasses import fields
1414
from pathlib import Path
15-
from typing import Coroutine, Optional
15+
from typing import Coroutine, Optional, Union
1616

1717
import numpy as np
1818
import rich
@@ -1639,15 +1639,17 @@ def wallet_ask(
16391639
wallet_hotkey: Optional[str],
16401640
ask_for: Optional[list[str]] = None,
16411641
validate: WV = WV.WALLET,
1642-
) -> Wallet:
1642+
return_wallet_and_hotkey: bool = False,
1643+
) -> Union[Wallet, tuple[Wallet, str]]:
16431644
"""
16441645
Generates a wallet object based on supplied values, validating the wallet is valid if flag is set
16451646
:param wallet_name: name of the wallet
16461647
:param wallet_path: root path of the wallets
16471648
:param wallet_hotkey: name of the wallet hotkey file
16481649
:param validate: flag whether to check for the wallet's validity
16491650
:param ask_for: aspect of the wallet (name, path, hotkey) to prompt the user for
1650-
:return: created Wallet object
1651+
:param return_wallet_and_hotkey: if specified, will return both the wallet object, and the hotkey SS58
1652+
:return: created Wallet object (or wallet, hotkey ss58)
16511653
"""
16521654
ask_for = ask_for or []
16531655
# Prompt for missing attributes specified in ask_for
@@ -1672,8 +1674,9 @@ def wallet_ask(
16721674
)
16731675
else:
16741676
wallet_hotkey = Prompt.ask(
1675-
"Enter the [blue]wallet hotkey[/blue]"
1676-
+ " [dark_sea_green3 italic](Hint: You can set this with `btcli config set --wallet-hotkey`)[/dark_sea_green3 italic]",
1677+
"Enter the [blue]wallet hotkey[/blue][dark_sea_green3 italic]"
1678+
"(Hint: You can set this with `btcli config set --wallet-hotkey`)"
1679+
"[/dark_sea_green3 italic]",
16771680
default=defaults.wallet.hotkey,
16781681
)
16791682
if wallet_path:
@@ -1691,7 +1694,8 @@ def wallet_ask(
16911694
if WO.PATH in ask_for and not wallet_path:
16921695
wallet_path = Prompt.ask(
16931696
"Enter the [blue]wallet path[/blue]"
1694-
+ " [dark_sea_green3 italic](Hint: You can set this with `btcli config set --wallet-path`)[/dark_sea_green3 italic]",
1697+
"[dark_sea_green3 italic](Hint: You can set this with `btcli config set --wallet-path`)"
1698+
"[/dark_sea_green3 italic]",
16951699
default=defaults.wallet.path,
16961700
)
16971701
# Create the Wallet object
@@ -1715,7 +1719,25 @@ def wallet_ask(
17151719
f"Please verify your wallet information: {wallet}[/red]"
17161720
)
17171721
raise typer.Exit()
1718-
return wallet
1722+
if return_wallet_and_hotkey:
1723+
valid = utils.is_valid_wallet(wallet)
1724+
if valid[1]:
1725+
return wallet, wallet.hotkey.ss58_address
1726+
else:
1727+
hotkey = (
1728+
Prompt.ask(
1729+
"Enter the SS58 of the hotkey to use for this transaction."
1730+
)
1731+
).strip()
1732+
if not is_valid_ss58_address(hotkey):
1733+
err_console.print(
1734+
f"[red]Error: {hotkey} is not valid SS58 address."
1735+
)
1736+
raise typer.Exit(1)
1737+
else:
1738+
return wallet, hotkey
1739+
else:
1740+
return wallet
17191741

17201742
def wallet_list(
17211743
self,
@@ -5834,18 +5856,13 @@ def liquidity_add(
58345856
show_default=False,
58355857
)
58365858

5837-
if not wallet_name:
5838-
wallet_name = Prompt.ask(
5839-
"Enter the [blue]wallet name[/blue]",
5840-
default=self.config.get("wallet_name") or defaults.wallet.name,
5841-
)
5842-
5843-
wallet = self.wallet_ask(
5859+
wallet, hotkey = self.wallet_ask(
58445860
wallet_name=wallet_name,
58455861
wallet_path=wallet_path,
58465862
wallet_hotkey=wallet_hotkey,
58475863
ask_for=[WO.NAME, WO.HOTKEY, WO.PATH],
5848-
validate=WV.WALLET_AND_HOTKEY,
5864+
validate=WV.WALLET,
5865+
return_wallet_and_hotkey=True,
58495866
)
58505867
# Determine the liquidity amount.
58515868
if liquidity_:
@@ -5874,6 +5891,7 @@ def liquidity_add(
58745891
liquidity.add_liquidity(
58755892
subtensor=self.initialize_chain(network),
58765893
wallet=wallet,
5894+
hotkey_ss58=hotkey,
58775895
netuid=netuid,
58785896
liquidity=liquidity_,
58795897
price_low=price_low,
@@ -5908,7 +5926,7 @@ def liquidity_list(
59085926
wallet_path=wallet_path,
59095927
wallet_hotkey=wallet_hotkey,
59105928
ask_for=[WO.NAME, WO.HOTKEY, WO.PATH],
5911-
validate=WV.WALLET_AND_HOTKEY,
5929+
validate=WV.WALLET,
59125930
)
59135931
self._run_command(
59145932
liquidity.show_liquidity_list(
@@ -5961,17 +5979,19 @@ def liquidity_remove(
59615979
show_default=False,
59625980
)
59635981

5964-
wallet = self.wallet_ask(
5982+
wallet, hotkey = self.wallet_ask(
59655983
wallet_name=wallet_name,
59665984
wallet_path=wallet_path,
59675985
wallet_hotkey=wallet_hotkey,
59685986
ask_for=[WO.NAME, WO.HOTKEY, WO.PATH],
5969-
validate=WV.WALLET_AND_HOTKEY,
5987+
validate=WV.WALLET,
5988+
return_wallet_and_hotkey=True,
59705989
)
59715990
return self._run_command(
59725991
liquidity.remove_liquidity(
59735992
subtensor=self.initialize_chain(network),
59745993
wallet=wallet,
5994+
hotkey_ss58=hotkey,
59755995
netuid=netuid,
59765996
position_id=position_id,
59775997
prompt=prompt,
@@ -6011,12 +6031,13 @@ def liquidity_modify(
60116031
f"Enter the [{COLORS.G.SUBHEAD_MAIN}]netuid[/{COLORS.G.SUBHEAD_MAIN}] to use",
60126032
)
60136033

6014-
wallet = self.wallet_ask(
6034+
wallet, hotkey = self.wallet_ask(
60156035
wallet_name=wallet_name,
60166036
wallet_path=wallet_path,
60176037
wallet_hotkey=wallet_hotkey,
60186038
ask_for=[WO.NAME, WO.HOTKEY, WO.PATH],
6019-
validate=WV.WALLET_AND_HOTKEY,
6039+
validate=WV.WALLET,
6040+
return_wallet_and_hotkey=True,
60206041
)
60216042

60226043
if not position_id:
@@ -6035,6 +6056,7 @@ def liquidity_modify(
60356056
liquidity.modify_liquidity(
60366057
subtensor=self.initialize_chain(network),
60376058
wallet=wallet,
6059+
hotkey_ss58=hotkey,
60386060
netuid=netuid,
60396061
position_id=position_id,
60406062
liquidity_delta=liquidity_delta,

bittensor_cli/src/commands/liquidity/liquidity.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
async def add_liquidity_extrinsic(
3030
subtensor: "SubtensorInterface",
3131
wallet: "Wallet",
32+
hotkey_ss58: str,
3233
netuid: int,
3334
liquidity: Balance,
3435
price_low: Balance,
@@ -42,6 +43,7 @@ async def add_liquidity_extrinsic(
4243
Arguments:
4344
subtensor: The Subtensor client instance used for blockchain interaction.
4445
wallet: The wallet used to sign the extrinsic (must be unlocked).
46+
hotkey_ss58: the SS58 of the hotkey to use for this transaction.
4547
netuid: The UID of the target subnet for which the call is being initiated.
4648
liquidity: The amount of liquidity to be added.
4749
price_low: The lower bound of the price tick range.
@@ -67,7 +69,7 @@ async def add_liquidity_extrinsic(
6769
call_module="Swap",
6870
call_function="add_liquidity",
6971
call_params={
70-
"hotkey": wallet.hotkey.ss58_address,
72+
"hotkey": hotkey_ss58,
7173
"netuid": netuid,
7274
"tick_low": tick_low,
7375
"tick_high": tick_high,
@@ -86,6 +88,7 @@ async def add_liquidity_extrinsic(
8688
async def modify_liquidity_extrinsic(
8789
subtensor: "SubtensorInterface",
8890
wallet: "Wallet",
91+
hotkey_ss58: str,
8992
netuid: int,
9093
position_id: int,
9194
liquidity_delta: Balance,
@@ -97,6 +100,7 @@ async def modify_liquidity_extrinsic(
97100
Arguments:
98101
subtensor: The Subtensor client instance used for blockchain interaction.
99102
wallet: The wallet used to sign the extrinsic (must be unlocked).
103+
hotkey_ss58: the SS58 of the hotkey to use for this transaction.
100104
netuid: The UID of the target subnet for which the call is being initiated.
101105
position_id: The id of the position record in the pool.
102106
liquidity_delta: The amount of liquidity to be added or removed (add if positive or remove if negative).
@@ -118,7 +122,7 @@ async def modify_liquidity_extrinsic(
118122
call_module="Swap",
119123
call_function="modify_position",
120124
call_params={
121-
"hotkey": wallet.hotkey.ss58_address,
125+
"hotkey": hotkey_ss58,
122126
"netuid": netuid,
123127
"position_id": position_id,
124128
"liquidity_delta": liquidity_delta.rao,
@@ -136,6 +140,7 @@ async def modify_liquidity_extrinsic(
136140
async def remove_liquidity_extrinsic(
137141
subtensor: "SubtensorInterface",
138142
wallet: "Wallet",
143+
hotkey_ss58: str,
139144
netuid: int,
140145
position_id: int,
141146
wait_for_inclusion: bool = True,
@@ -146,6 +151,7 @@ async def remove_liquidity_extrinsic(
146151
Arguments:
147152
subtensor: The Subtensor client instance used for blockchain interaction.
148153
wallet: The wallet used to sign the extrinsic (must be unlocked).
154+
hotkey_ss58: the SS58 of the hotkey to use for this transaction.
149155
netuid: The UID of the target subnet for which the call is being initiated.
150156
position_id: The id of the position record in the pool.
151157
wait_for_inclusion: Whether to wait for the extrinsic to be included in a block. Defaults to True.
@@ -166,7 +172,7 @@ async def remove_liquidity_extrinsic(
166172
call_module="Swap",
167173
call_function="remove_liquidity",
168174
call_params={
169-
"hotkey": wallet.hotkey.ss58_address,
175+
"hotkey": hotkey_ss58,
170176
"netuid": netuid,
171177
"position_id": position_id,
172178
},
@@ -224,6 +230,7 @@ async def toggle_user_liquidity_extrinsic(
224230
async def add_liquidity(
225231
subtensor: "SubtensorInterface",
226232
wallet: "Wallet",
233+
hotkey_ss58: str,
227234
netuid: Optional[int],
228235
liquidity: Optional[float],
229236
price_low: Optional[float],
@@ -512,6 +519,7 @@ async def show_liquidity_list(
512519
async def remove_liquidity(
513520
subtensor: "SubtensorInterface",
514521
wallet: "Wallet",
522+
hotkey_ss58: str,
515523
netuid: int,
516524
position_id: Optional[int] = None,
517525
prompt: Optional[bool] = None,
@@ -551,6 +559,7 @@ async def remove_liquidity(
551559
remove_liquidity_extrinsic(
552560
subtensor=subtensor,
553561
wallet=wallet,
562+
hotkey_ss58=hotkey_ss58,
554563
netuid=netuid,
555564
position_id=pos_id,
556565
)
@@ -573,6 +582,7 @@ async def remove_liquidity(
573582
async def modify_liquidity(
574583
subtensor: "SubtensorInterface",
575584
wallet: "Wallet",
585+
hotkey_ss58: str,
576586
netuid: int,
577587
position_id: int,
578588
liquidity_delta: Optional[float],
@@ -603,6 +613,7 @@ async def modify_liquidity(
603613
success, msg = await modify_liquidity_extrinsic(
604614
subtensor=subtensor,
605615
wallet=wallet,
616+
hotkey_ss58=hotkey_ss58,
606617
netuid=netuid,
607618
position_id=position_id,
608619
liquidity_delta=liquidity_delta,

0 commit comments

Comments
 (0)