Skip to content

Commit 688e2d7

Browse files
author
Roman
committed
improve unstake_extrinsic, unstake_multiple_extrinsic (add unstake_all argument)
1 parent 9649cc5 commit 688e2d7

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

bittensor/core/extrinsics/asyncex/unstaking.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async def unstake_extrinsic(
2525
allow_partial_stake: bool = False,
2626
rate_tolerance: float = 0.005,
2727
period: Optional[int] = None,
28+
unstake_all: bool = False,
2829
) -> bool:
2930
"""Removes stake into the wallet coldkey from the specified hotkey ``uid``.
3031
@@ -45,11 +46,15 @@ async def unstake_extrinsic(
4546
period (Optional[int]): The number of blocks during which the transaction will remain valid after it's submitted.
4647
If the transaction is not included in a block within that number of blocks, it will expire and be rejected.
4748
You can think of it as an expiration date for the transaction.
49+
unstake_all: If true, unstakes all tokens. Default is ``False``.
4850
4951
Returns:
5052
success (bool): Flag is ``True`` if extrinsic was finalized or included in the block. If we did not wait for
5153
finalization / inclusion, the response is ``True``.
5254
"""
55+
if amount and unstake_all:
56+
raise ValueError("Cannot specify both `amount` and `unstake_all`.")
57+
5358
# Decrypt keys,
5459
if not (unlock := unlock_key(wallet)).success:
5560
logging.error(unlock.message)
@@ -204,6 +209,7 @@ async def unstake_multiple_extrinsic(
204209
wait_for_inclusion: bool = True,
205210
wait_for_finalization: bool = False,
206211
period: Optional[int] = None,
212+
unstake_all: bool = False,
207213
) -> bool:
208214
"""Removes stake from each ``hotkey_ss58`` in the list, using each amount, to a common coldkey.
209215
@@ -220,11 +226,15 @@ async def unstake_multiple_extrinsic(
220226
period (Optional[int]): The number of blocks during which the transaction will remain valid after it's submitted.
221227
If the transaction is not included in a block within that number of blocks, it will expire and be rejected.
222228
You can think of it as an expiration date for the transaction.
229+
unstake_all: If true, unstakes all tokens. Default is ``False``.
223230
224231
Returns:
225232
success (bool): Flag is ``True`` if extrinsic was finalized or included in the block. Flag is ``True`` if any
226233
wallet was unstaked. If we did not wait for finalization / inclusion, the response is ``True``.
227234
"""
235+
if amounts and unstake_all:
236+
raise ValueError("Cannot specify both `amounts` and `unstake_all`.")
237+
228238
if not isinstance(hotkey_ss58s, list) or not all(
229239
isinstance(hotkey_ss58, str) for hotkey_ss58 in hotkey_ss58s
230240
):

bittensor/core/extrinsics/unstaking.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def unstake_extrinsic(
2424
allow_partial_stake: bool = False,
2525
rate_tolerance: float = 0.005,
2626
period: Optional[int] = None,
27+
unstake_all: bool = False,
2728
) -> bool:
2829
"""Removes stake into the wallet coldkey from the specified hotkey ``uid``.
2930
@@ -44,11 +45,15 @@ def unstake_extrinsic(
4445
period (Optional[int]): The number of blocks during which the transaction will remain valid after it's submitted.
4546
If the transaction is not included in a block within that number of blocks, it will expire and be rejected.
4647
You can think of it as an expiration date for the transaction.
48+
unstake_all: If true, unstakes all tokens. Default is ``False``.
4749
4850
Returns:
4951
success (bool): Flag is ``True`` if extrinsic was finalized or included in the block. If we did not wait for
5052
finalization / inclusion, the response is ``True``.
5153
"""
54+
if amount and unstake_all:
55+
raise ValueError("Cannot specify both `amount` and `unstake_all`.")
56+
5257
# Decrypt keys,
5358
if not (unlock := unlock_key(wallet)).success:
5459
logging.error(unlock.message)
@@ -102,16 +107,12 @@ def unstake_extrinsic(
102107
base_price = pool.price.rao
103108
price_with_tolerance = base_price * (1 - rate_tolerance)
104109

105-
# For logging
106-
base_rate = pool.price.tao
107-
rate_with_tolerance = base_rate * (1 - rate_tolerance)
108-
109110
logging.info(
110111
f":satellite: [magenta]Safe Unstaking from:[/magenta] "
111112
f"netuid: [green]{netuid}[/green], amount: [green]{unstaking_balance}[/green], "
112113
f"tolerance percentage: [green]{rate_tolerance * 100}%[/green], "
113-
f"price limit: [green]{rate_with_tolerance}[/green], "
114-
f"original price: [green]{base_rate}[/green], "
114+
f"price limit: [green]{price_with_tolerance}[/green], "
115+
f"original price: [green]{base_price}[/green], "
115116
f"with partial unstake: [green]{allow_partial_stake}[/green] "
116117
f"on [blue]{subtensor.network}[/blue][magenta]...[/magenta]"
117118
)
@@ -201,6 +202,7 @@ def unstake_multiple_extrinsic(
201202
wait_for_inclusion: bool = True,
202203
wait_for_finalization: bool = False,
203204
period: Optional[int] = None,
205+
unstake_all: bool = False,
204206
) -> bool:
205207
"""Removes stake from each ``hotkey_ss58`` in the list, using each amount, to a common coldkey.
206208
@@ -217,11 +219,14 @@ def unstake_multiple_extrinsic(
217219
period (Optional[int]): The number of blocks during which the transaction will remain valid after it's submitted.
218220
If the transaction is not included in a block within that number of blocks, it will expire and be rejected.
219221
You can think of it as an expiration date for the transaction.
222+
unstake_all: If true, unstakes all tokens. Default is ``False``.
220223
221224
Returns:
222225
success (bool): Flag is ``True`` if extrinsic was finalized or included in the block. Flag is ``True`` if any
223226
wallet was unstaked. If we did not wait for finalization / inclusion, the response is ``True``.
224227
"""
228+
if amounts and unstake_all:
229+
raise ValueError("Cannot specify both `amounts` and `unstake_all`.")
225230

226231
if not isinstance(hotkey_ss58s, list) or not all(
227232
isinstance(hotkey_ss58, str) for hotkey_ss58 in hotkey_ss58s

0 commit comments

Comments
 (0)