Skip to content

Commit 8e7bdda

Browse files
committed
Get the nonce for staking calls from the coldkeypub rather than the hotkey
1 parent 6400f72 commit 8e7bdda

File tree

5 files changed

+77
-27
lines changed

5 files changed

+77
-27
lines changed

bittensor/core/extrinsics/asyncex/staking.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ async def add_stake_extrinsic(
115115
},
116116
)
117117
staking_response, err_msg = await sign_and_send_with_nonce(
118-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
118+
subtensor,
119+
call,
120+
wallet,
121+
wait_for_inclusion,
122+
wait_for_finalization,
123+
nonce_key="coldkeypub",
119124
)
120125
if staking_response is True: # If we successfully staked.
121126
# We only wait here if we expect finalization.
@@ -299,7 +304,12 @@ async def add_stake_multiple_extrinsic(
299304
},
300305
)
301306
staking_response, err_msg = await sign_and_send_with_nonce(
302-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
307+
subtensor,
308+
call,
309+
wallet,
310+
wait_for_inclusion,
311+
wait_for_finalization,
312+
nonce_key="coldkeypub",
303313
)
304314

305315
if staking_response is True: # If we successfully staked.

bittensor/core/extrinsics/asyncex/unstaking.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ async def unstake_extrinsic(
101101
},
102102
)
103103
staking_response, err_msg = await sign_and_send_with_nonce(
104-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
104+
subtensor,
105+
call,
106+
wallet,
107+
wait_for_inclusion,
108+
wait_for_finalization,
109+
nonce_key="coldkeypub",
105110
)
106111

107112
if staking_response is True: # If we successfully unstaked.
@@ -266,7 +271,12 @@ async def unstake_multiple_extrinsic(
266271
)
267272

268273
staking_response, err_msg = await sign_and_send_with_nonce(
269-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
274+
subtensor,
275+
call,
276+
wallet,
277+
wait_for_inclusion,
278+
wait_for_finalization,
279+
nonce_key="coldkeypub",
270280
)
271281

272282
if staking_response is True: # If we successfully unstaked.

bittensor/core/extrinsics/staking.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional, TYPE_CHECKING, Sequence
33

44
from bittensor.core.errors import StakeError, NotRegisteredError
5-
from bittensor.core.extrinsics.utils import get_old_stakes
5+
from bittensor.core.extrinsics.utils import get_old_stakes, sign_and_send_with_nonce
66
from bittensor.utils import unlock_key
77
from bittensor.utils.balance import Balance
88
from bittensor.utils.btlogging import logging
@@ -106,8 +106,13 @@ def add_stake_extrinsic(
106106
"netuid": netuid,
107107
},
108108
)
109-
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
110-
call, wallet, wait_for_inclusion, wait_for_finalization
109+
staking_response, err_msg = sign_and_send_with_nonce(
110+
subtensor,
111+
call,
112+
wallet,
113+
wait_for_inclusion,
114+
wait_for_finalization,
115+
nonce_key="coldkeypub",
111116
)
112117
if staking_response is True: # If we successfully staked.
113118
# We only wait here if we expect finalization.
@@ -279,8 +284,13 @@ def add_stake_multiple_extrinsic(
279284
"netuid": netuid,
280285
},
281286
)
282-
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
283-
call, wallet, wait_for_inclusion, wait_for_finalization
287+
staking_response, err_msg = sign_and_send_with_nonce(
288+
subtensor,
289+
call,
290+
wallet,
291+
wait_for_inclusion,
292+
wait_for_finalization,
293+
nonce_key="coldkeypub",
284294
)
285295

286296
if staking_response is True: # If we successfully staked.

bittensor/core/extrinsics/unstaking.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ def unstake_extrinsic(
9494
},
9595
)
9696
staking_response, err_msg = sign_and_send_with_nonce(
97-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
97+
subtensor,
98+
call,
99+
wallet,
100+
wait_for_inclusion,
101+
wait_for_finalization,
102+
nonce_key="coldkeypub",
98103
)
99104

100105
if staking_response is True: # If we successfully unstaked.
@@ -249,7 +254,12 @@ def unstake_multiple_extrinsic(
249254
},
250255
)
251256
staking_response, err_msg = sign_and_send_with_nonce(
252-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
257+
subtensor,
258+
call,
259+
wallet,
260+
wait_for_inclusion,
261+
wait_for_finalization,
262+
nonce_key="coldkeypub",
253263
)
254264

255265
if staking_response is True: # If we successfully unstaked.

bittensor/core/extrinsics/utils.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,48 @@ async def async_sign_and_send_with_nonce(
2727
wait_for_inclusion: bool,
2828
wait_for_finalization: bool,
2929
period: Optional[int] = None,
30+
nonce_key: str = "hotkey",
3031
):
3132
"""
32-
Signs an extrinsic call with the wallet hotkey, adding an optional era for period
33+
Signs an extrinsic call with the wallet keypair (default hotkey), adding an optional era for period
3334
"""
34-
next_nonce = await subtensor.substrate.get_account_next_index(
35-
wallet.hotkey.ss58_address
36-
)
35+
keypair = getattr(wallet, nonce_key)
36+
next_nonce = await subtensor.substrate.get_account_next_index(keypair.ss58_address)
3737

3838
extrinsic_data = {"call": call, "keypair": wallet.hotkey, "nonce": next_nonce}
3939
if period is not None:
4040
extrinsic_data["era"] = {"period": period}
4141

4242
extrinsic = await subtensor.substrate.create_signed_extrinsic(**extrinsic_data)
43-
response = await subtensor.substrate.submit_extrinsic(
44-
extrinsic=extrinsic,
45-
wait_for_inclusion=wait_for_inclusion,
46-
wait_for_finalization=wait_for_finalization,
47-
)
4843

49-
if not wait_for_finalization and not wait_for_inclusion:
50-
return True, None
44+
try:
45+
response = await subtensor.substrate.submit_extrinsic(
46+
extrinsic=extrinsic,
47+
wait_for_inclusion=wait_for_inclusion,
48+
wait_for_finalization=wait_for_finalization,
49+
)
5150

52-
if await response.is_success:
53-
return True, None
51+
if not wait_for_finalization and not wait_for_inclusion:
52+
return True, None
5453

55-
return False, format_error_message(await response.error_message)
54+
if await response.is_success:
55+
return True, None
56+
57+
return False, format_error_message(await response.error_message)
58+
except SubstrateRequestException as e:
59+
return False, format_error_message(e)
5660

5761

5862
def sign_and_send_with_nonce(
59-
subtensor: "Subtensor", call, wallet, wait_for_inclusion, wait_for_finalization
63+
subtensor: "Subtensor",
64+
call,
65+
wallet,
66+
wait_for_inclusion,
67+
wait_for_finalization,
68+
nonce_key: str = "hotkey",
6069
):
61-
next_nonce = subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address)
70+
keypair = getattr(wallet, nonce_key)
71+
next_nonce = subtensor.substrate.get_account_next_index(keypair.ss58_address)
6272
extrinsic = subtensor.substrate.create_signed_extrinsic(
6373
call=call,
6474
keypair=wallet.hotkey,

0 commit comments

Comments
 (0)