|
1 | 1 | """Module with helper functions for extrinsics."""
|
2 | 2 |
|
3 |
| -from typing import TYPE_CHECKING |
| 3 | +from typing import TYPE_CHECKING, Optional |
4 | 4 |
|
5 | 5 | from async_substrate_interface.errors import SubstrateRequestException
|
6 | 6 |
|
|
17 | 17 | )
|
18 | 18 | from bittensor.core.subtensor import Subtensor
|
19 | 19 | from bittensor.core.chain_data import StakeInfo
|
20 |
| - from scalecodec.types import GenericExtrinsic |
| 20 | + from scalecodec.types import GenericExtrinsic, GenericCall |
| 21 | + |
| 22 | + |
| 23 | +async def async_sign_and_send_with_nonce( |
| 24 | + subtensor: "AsyncSubtensor", |
| 25 | + call: "GenericCall", |
| 26 | + wallet: "Wallet", |
| 27 | + wait_for_inclusion: bool, |
| 28 | + wait_for_finalization: bool, |
| 29 | + period: Optional[int] = None, |
| 30 | +): |
| 31 | + """ |
| 32 | + Signs an extrinsic call with the wallet hotkey, adding an optional era for period |
| 33 | + """ |
| 34 | + next_nonce = await subtensor.substrate.get_account_next_index( |
| 35 | + wallet.hotkey.ss58_address |
| 36 | + ) |
| 37 | + |
| 38 | + extrinsic_data = {"call": call, "keypair": wallet.hotkey, "nonce": next_nonce} |
| 39 | + if period is not None: |
| 40 | + extrinsic_data["era"] = {"period": period} |
| 41 | + |
| 42 | + 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 | + ) |
| 48 | + |
| 49 | + if not wait_for_finalization and not wait_for_inclusion: |
| 50 | + return True, None |
| 51 | + |
| 52 | + if await response.is_success: |
| 53 | + return True, None |
| 54 | + |
| 55 | + return False, format_error_message(await response.error_message) |
| 56 | + |
| 57 | + |
| 58 | +def sign_and_send_with_nonce( |
| 59 | + subtensor: "Subtensor", call, wallet, wait_for_inclusion, wait_for_finalization |
| 60 | +): |
| 61 | + next_nonce = subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address) |
| 62 | + extrinsic = subtensor.substrate.create_signed_extrinsic( |
| 63 | + call=call, |
| 64 | + keypair=wallet.hotkey, |
| 65 | + nonce=next_nonce, |
| 66 | + ) |
| 67 | + response = subtensor.substrate.submit_extrinsic( |
| 68 | + extrinsic=extrinsic, |
| 69 | + wait_for_inclusion=wait_for_inclusion, |
| 70 | + wait_for_finalization=wait_for_finalization, |
| 71 | + ) |
| 72 | + |
| 73 | + if not wait_for_finalization and not wait_for_inclusion: |
| 74 | + return True, None |
| 75 | + |
| 76 | + if response.is_success: |
| 77 | + return True, None |
| 78 | + |
| 79 | + return False, format_error_message(response.error_message) |
21 | 80 |
|
22 | 81 |
|
23 | 82 | def submit_extrinsic(
|
|
0 commit comments