Skip to content

Commit 85eaa90

Browse files
committed
Adds explicit type for balance inputs
1 parent b3616e6 commit 85eaa90

File tree

8 files changed

+73
-215
lines changed

8 files changed

+73
-215
lines changed

bittensor/core/async_subtensor.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,7 +2795,7 @@ async def add_stake(
27952795
wallet: "Wallet",
27962796
hotkey_ss58: Optional[str] = None,
27972797
netuid: Optional[int] = None,
2798-
amount: Optional[Union["Balance", float]] = None,
2798+
amount: Optional[Balance] = None,
27992799
wait_for_inclusion: bool = True,
28002800
wait_for_finalization: bool = False,
28012801
) -> bool:
@@ -2807,7 +2807,7 @@ async def add_stake(
28072807
Args:
28082808
wallet (bittensor_wallet.Wallet): The wallet to be used for staking.
28092809
hotkey_ss58 (Optional[str]): The ``SS58`` address of the hotkey associated with the neuron.
2810-
amount (Union[Balance, float]): The amount of TAO to stake.
2810+
amount (Balance): The amount of TAO to stake.
28112811
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
28122812
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
28132813
@@ -2832,7 +2832,7 @@ async def add_stake_multiple(
28322832
wallet: "Wallet",
28332833
hotkey_ss58s: list[str],
28342834
netuids: list[int],
2835-
amounts: Optional[list[Union["Balance", float]]] = None,
2835+
amounts: Optional[list[Balance]] = None,
28362836
wait_for_inclusion: bool = True,
28372837
wait_for_finalization: bool = False,
28382838
) -> bool:
@@ -2843,7 +2843,7 @@ async def add_stake_multiple(
28432843
Args:
28442844
wallet (bittensor_wallet.Wallet): The wallet used for staking.
28452845
hotkey_ss58s (list[str]): List of ``SS58`` addresses of hotkeys to stake to.
2846-
amounts (list[Union[Balance, float]]): Corresponding amounts of TAO to stake for each hotkey.
2846+
amounts (list[Balance]): Corresponding amounts of TAO to stake for each hotkey.
28472847
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
28482848
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
28492849
@@ -2975,7 +2975,7 @@ async def move_stake(
29752975
origin_netuid: int,
29762976
destination_hotkey: str,
29772977
destination_netuid: int,
2978-
amount: Union["Balance", float],
2978+
amount: Balance,
29792979
wait_for_inclusion: bool = True,
29802980
wait_for_finalization: bool = False,
29812981
) -> bool:
@@ -2988,15 +2988,13 @@ async def move_stake(
29882988
origin_netuid (int): The netuid of the source subnet.
29892989
destination_hotkey (str): The SS58 address of the destination hotkey.
29902990
destination_netuid (int): The netuid of the destination subnet.
2991-
amount (Union[Balance, float]): Amount of stake to move.
2991+
amount (Balance): Amount of stake to move.
29922992
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
29932993
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
29942994
29952995
Returns:
29962996
success (bool): True if the stake movement was successful.
29972997
"""
2998-
if isinstance(amount, float):
2999-
amount = Balance.from_tao(amount)
30002998

30012999
return await move_stake_extrinsic(
30023000
subtensor=self,
@@ -3420,7 +3418,7 @@ async def transfer_stake(
34203418
hotkey_ss58: str,
34213419
origin_netuid: int,
34223420
destination_netuid: int,
3423-
amount: Union["Balance", float],
3421+
amount: Balance,
34243422
wait_for_inclusion: bool = True,
34253423
wait_for_finalization: bool = False,
34263424
) -> bool:
@@ -3433,15 +3431,13 @@ async def transfer_stake(
34333431
hotkey_ss58 (str): The hotkey SS58 address associated with the stake.
34343432
origin_netuid (int): The source subnet UID.
34353433
destination_netuid (int): The destination subnet UID.
3436-
amount (Union[Balance, float, int]): Amount to transfer.
3434+
amount (Balance): Amount to transfer.
34373435
wait_for_inclusion (bool): If true, waits for inclusion before returning.
34383436
wait_for_finalization (bool): If true, waits for finalization before returning.
34393437
34403438
Returns:
34413439
success (bool): True if the transfer was successful.
34423440
"""
3443-
if isinstance(amount, float):
3444-
amount = Balance.from_tao(amount)
34453441

34463442
return await transfer_stake_extrinsic(
34473443
subtensor=self,
@@ -3481,8 +3477,6 @@ async def transfer(
34813477
Returns:
34823478
`True` if the transferring was successful, otherwise `False`.
34833479
"""
3484-
if isinstance(amount, float):
3485-
amount = Balance.from_tao(amount)
34863480

34873481
return await transfer_extrinsic(
34883482
subtensor=self,
@@ -3500,7 +3494,7 @@ async def unstake(
35003494
wallet: "Wallet",
35013495
hotkey_ss58: Optional[str] = None,
35023496
netuid: Optional[int] = None,
3503-
amount: Optional[Union["Balance", float]] = None,
3497+
amount: Optional[Balance] = None,
35043498
wait_for_inclusion: bool = True,
35053499
wait_for_finalization: bool = False,
35063500
) -> bool:
@@ -3512,7 +3506,7 @@ async def unstake(
35123506
wallet (bittensor_wallet.wallet): The wallet associated with the neuron from which the stake is being
35133507
removed.
35143508
hotkey_ss58 (Optional[str]): The ``SS58`` address of the hotkey account to unstake from.
3515-
amount (Union[Balance, float]): The amount of TAO to unstake. If not specified, unstakes all.
3509+
amount (Balance): The amount of TAO to unstake. If not specified, unstakes all.
35163510
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
35173511
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
35183512
@@ -3537,7 +3531,7 @@ async def unstake_multiple(
35373531
wallet: "Wallet",
35383532
hotkey_ss58s: list[str],
35393533
netuids: list[int],
3540-
amounts: Optional[list[Union["Balance", float]]] = None,
3534+
amounts: Optional[list[Balance]] = None,
35413535
wait_for_inclusion: bool = True,
35423536
wait_for_finalization: bool = False,
35433537
) -> bool:

bittensor/core/extrinsics/asyncex/move_stake.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union, Optional, TYPE_CHECKING
1+
from typing import TYPE_CHECKING
22

33
from bittensor.utils.balance import Balance
44
from bittensor.utils.btlogging import logging
@@ -7,21 +7,19 @@
77
from bittensor_wallet import Wallet
88
from bittensor.core.subtensor import Subtensor
99

10+
1011
async def transfer_stake_extrinsic(
1112
subtensor: "Subtensor",
1213
wallet: "Wallet",
1314
destination_coldkey_ss58: str,
1415
hotkey_ss58: str,
1516
origin_netuid: int,
1617
destination_netuid: int,
17-
amount: Optional[Union["Balance", float]] = None,
18+
amount: Balance,
1819
wait_for_inclusion: bool = True,
1920
wait_for_finalization: bool = False,
2021
) -> bool:
21-
if not isinstance(amount, Balance):
22-
amount = Balance.from_tao(amount)
2322
amount.set_unit(netuid=origin_netuid)
24-
2523
# Verify ownership
2624
hotkey_owner = await subtensor.get_hotkey_owner(hotkey_ss58)
2725
if hotkey_owner != wallet.coldkeypub.ss58_address:
@@ -107,20 +105,18 @@ async def transfer_stake_extrinsic(
107105
logging.error(f":cross_mark: [red]Failed[/red]: {str(e)}")
108106
return False
109107

108+
110109
async def swap_stake_extrinsic(
111110
subtensor: "Subtensor",
112111
wallet: "Wallet",
113112
hotkey_ss58: str,
114113
origin_netuid: int,
115114
destination_netuid: int,
116-
amount: Optional[Union["Balance", float]] = None,
115+
amount: Balance,
117116
wait_for_inclusion: bool = True,
118117
wait_for_finalization: bool = False,
119118
) -> bool:
120-
if not isinstance(amount, Balance):
121-
amount = Balance.from_tao(amount)
122119
amount.set_unit(netuid=origin_netuid)
123-
124120
# Verify ownership
125121
hotkey_owner = await subtensor.get_hotkey_owner(hotkey_ss58)
126122
if hotkey_owner != wallet.coldkeypub.ss58_address:
@@ -205,21 +201,19 @@ async def swap_stake_extrinsic(
205201
logging.error(f":cross_mark: [red]Failed[/red]: {str(e)}")
206202
return False
207203

204+
208205
async def move_stake_extrinsic(
209206
subtensor: "Subtensor",
210207
wallet: "Wallet",
211208
origin_hotkey: str,
212209
origin_netuid: int,
213210
destination_hotkey: str,
214211
destination_netuid: int,
215-
amount: Optional[Union["Balance", float]] = None,
212+
amount: Balance,
216213
wait_for_inclusion: bool = True,
217214
wait_for_finalization: bool = False,
218215
) -> bool:
219-
if not isinstance(amount, Balance):
220-
amount = Balance.from_tao(amount)
221216
amount.set_unit(netuid=origin_netuid)
222-
223217
# Verify ownership of origin hotkey
224218
origin_owner = await subtensor.get_hotkey_owner(origin_hotkey)
225219
if origin_owner != wallet.coldkeypub.ss58_address:

bittensor/core/extrinsics/asyncex/staking.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from typing import Optional, Sequence, TYPE_CHECKING, Union
2+
from typing import Optional, Sequence, TYPE_CHECKING
33

44
from bittensor.core.errors import StakeError, NotRegisteredError
55
from bittensor.utils import unlock_key
@@ -14,10 +14,10 @@
1414
async def add_stake_extrinsic(
1515
subtensor: "AsyncSubtensor",
1616
wallet: "Wallet",
17-
old_balance: Optional["Balance"] = None,
17+
old_balance: Optional[Balance] = None,
1818
hotkey_ss58: Optional[str] = None,
1919
netuid: Optional[int] = None,
20-
amount: Optional[Union["Balance", float]] = None,
20+
amount: Optional[Balance] = None,
2121
wait_for_inclusion: bool = True,
2222
wait_for_finalization: bool = False,
2323
) -> bool:
@@ -72,10 +72,9 @@ async def add_stake_extrinsic(
7272
if amount is None:
7373
# Stake it all.
7474
staking_balance = Balance.from_tao(old_balance.tao)
75-
elif not isinstance(amount, Balance):
76-
staking_balance = Balance.from_tao(amount)
7775
else:
7876
staking_balance = amount
77+
staking_balance.set_unit(netuid)
7978

8079
# Leave existential balance to keep key alive.
8180
if staking_balance > old_balance - existential_deposit:
@@ -162,8 +161,8 @@ async def add_stake_multiple_extrinsic(
162161
wallet: "Wallet",
163162
hotkey_ss58s: list[str],
164163
netuids: list[int],
165-
old_balance: Optional["Balance"] = None,
166-
amounts: Optional[list["Balance"]] = None,
164+
old_balance: Optional[Balance] = None,
165+
amounts: Optional[list[Balance]] = None,
167166
wait_for_inclusion: bool = True,
168167
wait_for_finalization: bool = False,
169168
) -> bool:
@@ -185,6 +184,7 @@ async def add_stake_multiple_extrinsic(
185184
success: `True` if extrinsic was finalized or included in the block. `True` if any wallet was staked. If we did
186185
not wait for finalization/inclusion, the response is `True`.
187186
"""
187+
188188
async def get_old_stakes() -> list[Balance]:
189189
old_stakes = []
190190
all_stakes = await subtensor.get_stake_for_coldkey(
@@ -224,8 +224,7 @@ async def get_old_stakes() -> list[Balance]:
224224
new_amounts = [None] * len(hotkey_ss58s)
225225
else:
226226
new_amounts = [
227-
Balance.from_tao(amount) if not isinstance(amount, Balance) else amount
228-
for amount in amounts
227+
amount.set_unit(netuid=netuid) for amount, netuid in zip(amounts, netuids)
229228
]
230229
if sum(amount.tao for amount in new_amounts) == 0:
231230
# Staking 0 tao
@@ -266,8 +265,7 @@ async def get_old_stakes() -> list[Balance]:
266265
# Reduce the amount to stake to each wallet to keep the balance above 1000 rao.
267266
percent_reduction = 1 - (1000 / total_staking_rao)
268267
new_amounts = [
269-
Balance.from_tao(amount.tao * percent_reduction)
270-
for amount in new_amounts
268+
Balance.from_tao(amount.tao * percent_reduction) for amount in new_amounts
271269
]
272270

273271
successful_stakes = 0

bittensor/core/extrinsics/asyncex/unstaking.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from typing import Union, Optional, TYPE_CHECKING
2+
from typing import Optional, TYPE_CHECKING
33

44
from bittensor.core.errors import StakeError, NotRegisteredError
55
from bittensor.utils import unlock_key
@@ -16,7 +16,7 @@ async def unstake_extrinsic(
1616
wallet: "Wallet",
1717
hotkey_ss58: Optional[str] = None,
1818
netuid: Optional[int] = None,
19-
amount: Optional[Union[Balance, float]] = None,
19+
amount: Optional[Balance] = None,
2020
wait_for_inclusion: bool = True,
2121
wait_for_finalization: bool = False,
2222
) -> bool:
@@ -64,10 +64,9 @@ async def unstake_extrinsic(
6464
if amount is None:
6565
# Unstake it all.
6666
unstaking_balance = old_stake
67-
elif not isinstance(amount, Balance):
68-
unstaking_balance = Balance.from_tao(amount)
6967
else:
7068
unstaking_balance = amount
69+
unstaking_balance.set_unit(netuid)
7170

7271
# Check enough to unstake.
7372
stake_on_uid = old_stake
@@ -146,7 +145,7 @@ async def unstake_multiple_extrinsic(
146145
wallet: "Wallet",
147146
hotkey_ss58s: list[str],
148147
netuids: list[int],
149-
amounts: Optional[list[Union[Balance, float]]] = None,
148+
amounts: Optional[list[Balance]] = None,
150149
wait_for_inclusion: bool = True,
151150
wait_for_finalization: bool = False,
152151
) -> bool:
@@ -212,11 +211,7 @@ async def get_old_stakes() -> list[Balance]:
212211
amounts = [None] * len(hotkey_ss58s)
213212
else:
214213
# Convert to Balance
215-
amounts = [
216-
Balance.from_tao(amount) if isinstance(amount, float) else amount
217-
for amount in amounts
218-
]
219-
214+
amounts = [amount.set_unit(netuid) for amount, netuid in zip(amounts, netuids)]
220215
if sum(amount.tao for amount in amounts) == 0:
221216
# Staking 0 tao
222217
return True
@@ -244,9 +239,7 @@ async def get_old_stakes() -> list[Balance]:
244239
# Unstake it all.
245240
unstaking_balance = old_stake
246241
else:
247-
unstaking_balance = (
248-
amount if isinstance(amount, Balance) else Balance.from_tao(amount)
249-
)
242+
unstaking_balance = amount
250243

251244
# Check enough to unstake.
252245
stake_on_uid = old_stake
@@ -267,7 +260,7 @@ async def get_old_stakes() -> list[Balance]:
267260
call_function="remove_stake",
268261
call_params={
269262
"hotkey": hotkey_ss58,
270-
"amount_unstaked": amount.rao,
263+
"amount_unstaked": unstaking_balance.rao,
271264
"netuid": netuid,
272265
},
273266
)

0 commit comments

Comments
 (0)