Skip to content

Commit 75733d9

Browse files
authored
Merge pull request #406 from opentensor/feat/thewhaleking/taylor-eras-tour
Adds `era` param for stake transactions
2 parents f33b6b8 + 46db5b2 commit 75733d9

File tree

13 files changed

+116
-27
lines changed

13 files changed

+116
-27
lines changed

bittensor_cli/cli.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ class Options:
279279
"--dashboard.path",
280280
help="Path to save the dashboard HTML file. For example: `~/.bittensor/dashboard`.",
281281
)
282+
era: int = typer.Option(
283+
3, help="Length (in blocks) for which the transaction should be valid."
284+
)
282285

283286

284287
def list_prompt(init_var: list, list_type: type, help_text: str) -> list:
@@ -1749,6 +1752,7 @@ def wallet_transfer(
17491752
transfer_all: bool = typer.Option(
17501753
False, "--all", prompt=False, help="Transfer all available balance."
17511754
),
1755+
era: int = Options.era,
17521756
wallet_name: str = Options.wallet_name,
17531757
wallet_path: str = Options.wallet_path,
17541758
wallet_hotkey: str = Options.wallet_hotkey,
@@ -1797,12 +1801,13 @@ def wallet_transfer(
17971801
amount = FloatPrompt.ask("Enter amount (in TAO) to transfer.")
17981802
return self._run_command(
17991803
wallets.transfer(
1800-
wallet,
1801-
subtensor,
1802-
destination_ss58_address,
1803-
amount,
1804-
transfer_all,
1805-
prompt,
1804+
wallet=wallet,
1805+
subtensor=subtensor,
1806+
destination=destination_ss58_address,
1807+
amount=amount,
1808+
transfer_all=transfer_all,
1809+
era=era,
1810+
prompt=prompt,
18061811
)
18071812
)
18081813

@@ -3112,6 +3117,7 @@ def stake_add(
31123117
rate_tolerance: Optional[float] = Options.rate_tolerance,
31133118
safe_staking: Optional[bool] = Options.safe_staking,
31143119
allow_partial_stake: Optional[bool] = Options.allow_partial_stake,
3120+
era: int = Options.era,
31153121
prompt: bool = Options.prompt,
31163122
quiet: bool = Options.quiet,
31173123
verbose: bool = Options.verbose,
@@ -3305,6 +3311,7 @@ def stake_add(
33053311
safe_staking,
33063312
rate_tolerance,
33073313
allow_partial_stake,
3314+
era,
33083315
)
33093316
)
33103317

@@ -3356,6 +3363,7 @@ def stake_remove(
33563363
rate_tolerance: Optional[float] = Options.rate_tolerance,
33573364
safe_staking: Optional[bool] = Options.safe_staking,
33583365
allow_partial_stake: Optional[bool] = Options.allow_partial_stake,
3366+
era: int = Options.era,
33593367
prompt: bool = Options.prompt,
33603368
interactive: bool = typer.Option(
33613369
False,
@@ -3545,6 +3553,7 @@ def stake_remove(
35453553
include_hotkeys=include_hotkeys,
35463554
exclude_hotkeys=exclude_hotkeys,
35473555
prompt=prompt,
3556+
era=era,
35483557
)
35493558
)
35503559
elif (
@@ -3599,6 +3608,7 @@ def stake_remove(
35993608
safe_staking=safe_staking,
36003609
rate_tolerance=rate_tolerance,
36013610
allow_partial_stake=allow_partial_stake,
3611+
era=era,
36023612
)
36033613
)
36043614

@@ -3626,6 +3636,7 @@ def stake_move(
36263636
stake_all: bool = typer.Option(
36273637
False, "--stake-all", "--all", help="Stake all", prompt=False
36283638
),
3639+
era: int = Options.era,
36293640
prompt: bool = Options.prompt,
36303641
quiet: bool = Options.quiet,
36313642
verbose: bool = Options.verbose,
@@ -3753,6 +3764,7 @@ def stake_move(
37533764
destination_hotkey=destination_hotkey,
37543765
amount=amount,
37553766
stake_all=stake_all,
3767+
era=era,
37563768
interactive_selection=interactive_selection,
37573769
prompt=prompt,
37583770
)
@@ -3790,6 +3802,7 @@ def stake_transfer(
37903802
stake_all: bool = typer.Option(
37913803
False, "--stake-all", "--all", help="Stake all", prompt=False
37923804
),
3805+
era: int = Options.era,
37933806
prompt: bool = Options.prompt,
37943807
quiet: bool = Options.quiet,
37953808
verbose: bool = Options.verbose,
@@ -3910,6 +3923,7 @@ def stake_transfer(
39103923
dest_netuid=dest_netuid,
39113924
dest_coldkey_ss58=dest_ss58,
39123925
amount=amount,
3926+
era=era,
39133927
interactive_selection=interactive_selection,
39143928
stake_all=stake_all,
39153929
prompt=prompt,
@@ -3948,6 +3962,7 @@ def stake_swap(
39483962
"--all",
39493963
help="Swap all available stake",
39503964
),
3965+
era: int = Options.era,
39513966
prompt: bool = Options.prompt,
39523967
wait_for_inclusion: bool = Options.wait_for_inclusion,
39533968
wait_for_finalization: bool = Options.wait_for_finalization,
@@ -4010,6 +4025,7 @@ def stake_swap(
40104025
destination_netuid=dest_netuid,
40114026
amount=amount,
40124027
swap_all=swap_all,
4028+
era=era,
40134029
interactive_selection=interactive_selection,
40144030
prompt=prompt,
40154031
wait_for_inclusion=wait_for_inclusion,
@@ -5041,6 +5057,13 @@ def subnets_register(
50415057
wallet_hotkey: str = Options.wallet_hotkey,
50425058
network: Optional[list[str]] = Options.network,
50435059
netuid: int = Options.netuid,
5060+
era: Optional[
5061+
int
5062+
] = typer.Option( # Should not be Options.era bc this needs to be an Optional[int]
5063+
None,
5064+
help="Length (in blocks) for which the transaction should be valid. Note that it is possible that if you "
5065+
"use an era for this transaction that you may pay a different fee to register than the one stated.",
5066+
),
50445067
prompt: bool = Options.prompt,
50455068
quiet: bool = Options.quiet,
50465069
verbose: bool = Options.verbose,
@@ -5069,6 +5092,7 @@ def subnets_register(
50695092
wallet,
50705093
self.initialize_chain(network),
50715094
netuid,
5095+
era,
50725096
prompt,
50735097
)
50745098
)

bittensor_cli/src/bittensor/extrinsics/registration.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ async def burned_register_extrinsic(
676676
old_balance: Balance,
677677
wait_for_inclusion: bool = True,
678678
wait_for_finalization: bool = True,
679+
era: Optional[int] = None,
679680
prompt: bool = False,
680681
) -> bool:
681682
"""Registers the wallet to chain by recycling TAO.
@@ -704,13 +705,32 @@ async def burned_register_extrinsic(
704705
my_uid = await subtensor.query(
705706
"SubtensorModule", "Uids", [netuid, wallet.hotkey.ss58_address]
706707
)
708+
block_hash = await subtensor.substrate.get_chain_head()
707709

708710
print_verbose("Checking if already registered", status)
709711
neuron = await subtensor.neuron_for_uid(
710-
uid=my_uid,
711-
netuid=netuid,
712-
block_hash=subtensor.substrate.last_block_hash,
712+
uid=my_uid, netuid=netuid, block_hash=block_hash
713713
)
714+
if not era:
715+
current_block, tempo, blocks_since_last_step = await asyncio.gather(
716+
subtensor.substrate.get_block_number(block_hash=block_hash),
717+
subtensor.get_hyperparameter(
718+
"Tempo", netuid=netuid, block_hash=block_hash
719+
),
720+
subtensor.query(
721+
"SubtensorModule",
722+
"BlocksSinceLastStep",
723+
[netuid],
724+
block_hash=block_hash,
725+
),
726+
)
727+
validity_period = tempo - blocks_since_last_step
728+
era_ = {
729+
"period": validity_period,
730+
"current": current_block,
731+
}
732+
else:
733+
era_ = {"period": era}
714734

715735
if not neuron.is_null:
716736
console.print(
@@ -734,7 +754,7 @@ async def burned_register_extrinsic(
734754
},
735755
)
736756
success, err_msg = await subtensor.sign_and_send_extrinsic(
737-
call, wallet, wait_for_inclusion, wait_for_finalization
757+
call, wallet, wait_for_inclusion, wait_for_finalization, era=era_
738758
)
739759

740760
if not success:

bittensor_cli/src/bittensor/extrinsics/transfer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async def transfer_extrinsic(
2424
wallet: Wallet,
2525
destination: str,
2626
amount: Balance,
27+
era: int = 3,
2728
transfer_all: bool = False,
2829
wait_for_inclusion: bool = True,
2930
wait_for_finalization: bool = False,
@@ -84,7 +85,7 @@ async def do_transfer() -> tuple[bool, str, str]:
8485
call_params={"dest": destination, "value": amount.rao},
8586
)
8687
extrinsic = await subtensor.substrate.create_signed_extrinsic(
87-
call=call, keypair=wallet.coldkey
88+
call=call, keypair=wallet.coldkey, era={"period": era}
8889
)
8990
response = await subtensor.substrate.submit_extrinsic(
9091
extrinsic,

bittensor_cli/src/bittensor/subtensor_interface.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,7 @@ async def sign_and_send_extrinsic(
10481048
wallet: Wallet,
10491049
wait_for_inclusion: bool = True,
10501050
wait_for_finalization: bool = False,
1051+
era: Optional[dict[str, int]] = None,
10511052
) -> tuple[bool, str]:
10521053
"""
10531054
Helper method to sign and submit an extrinsic call to chain.
@@ -1059,8 +1060,11 @@ async def sign_and_send_extrinsic(
10591060
10601061
:return: (success, error message)
10611062
"""
1063+
call_args = {"call": call, "keypair": wallet.coldkey}
1064+
if era is not None:
1065+
call_args["era"] = era
10621066
extrinsic = await self.substrate.create_signed_extrinsic(
1063-
call=call, keypair=wallet.coldkey
1067+
**call_args
10641068
) # sign with coldkey
10651069
try:
10661070
response = await self.substrate.submit_extrinsic(

bittensor_cli/src/commands/stake/add.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async def stake_add(
3838
safe_staking: bool,
3939
rate_tolerance: float,
4040
allow_partial_stake: bool,
41+
era: int,
4142
):
4243
"""
4344
Args:
@@ -53,6 +54,7 @@ async def stake_add(
5354
safe_staking: whether to use safe staking
5455
rate_tolerance: rate tolerance percentage for stake operations
5556
allow_partial_stake: whether to allow partial stake
57+
era: Blocks for which the transaction should be valid.
5658
5759
Returns:
5860
bool: True if stake operation is successful, False otherwise
@@ -86,7 +88,10 @@ async def safe_stake_extrinsic(
8688
},
8789
)
8890
extrinsic = await subtensor.substrate.create_signed_extrinsic(
89-
call=call, keypair=wallet.coldkey, nonce=next_nonce
91+
call=call,
92+
keypair=wallet.coldkey,
93+
nonce=next_nonce,
94+
era={"period": era},
9095
)
9196
try:
9297
response = await subtensor.substrate.submit_extrinsic(
@@ -105,7 +110,6 @@ async def safe_stake_extrinsic(
105110
err_out(f"\n{failure_prelude} with error: {format_error_message(e)}")
106111
return
107112
else:
108-
await response.process_events()
109113
if not await response.is_success:
110114
err_out(
111115
f"\n{failure_prelude} with error: {format_error_message(await response.error_message)}"
@@ -166,7 +170,7 @@ async def stake_extrinsic(
166170
},
167171
)
168172
extrinsic = await subtensor.substrate.create_signed_extrinsic(
169-
call=call, keypair=wallet.coldkey, nonce=next_nonce
173+
call=call, keypair=wallet.coldkey, nonce=next_nonce, era={"period": era}
170174
)
171175
try:
172176
response = await subtensor.substrate.submit_extrinsic(

bittensor_cli/src/commands/stake/move.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ async def move_stake(
444444
destination_hotkey: str,
445445
amount: float,
446446
stake_all: bool,
447+
era: int,
447448
interactive_selection: bool = False,
448449
prompt: bool = True,
449450
):
@@ -563,7 +564,7 @@ async def move_stake(
563564
},
564565
)
565566
extrinsic = await subtensor.substrate.create_signed_extrinsic(
566-
call=call, keypair=wallet.coldkey
567+
call=call, keypair=wallet.coldkey, era={"period": era}
567568
)
568569
response = await subtensor.substrate.submit_extrinsic(
569570
extrinsic, wait_for_inclusion=True, wait_for_finalization=False
@@ -622,6 +623,7 @@ async def transfer_stake(
622623
origin_netuid: int,
623624
dest_netuid: int,
624625
dest_coldkey_ss58: str,
626+
era: int,
625627
interactive_selection: bool = False,
626628
stake_all: bool = False,
627629
prompt: bool = True,
@@ -747,7 +749,7 @@ async def transfer_stake(
747749
)
748750

749751
extrinsic = await subtensor.substrate.create_signed_extrinsic(
750-
call=call, keypair=wallet.coldkey
752+
call=call, keypair=wallet.coldkey, era={"period": era}
751753
)
752754

753755
response = await subtensor.substrate.submit_extrinsic(
@@ -798,6 +800,7 @@ async def swap_stake(
798800
destination_netuid: int,
799801
amount: float,
800802
swap_all: bool = False,
803+
era: int = 3,
801804
interactive_selection: bool = False,
802805
prompt: bool = True,
803806
wait_for_inclusion: bool = True,
@@ -917,7 +920,7 @@ async def swap_stake(
917920
)
918921

919922
extrinsic = await subtensor.substrate.create_signed_extrinsic(
920-
call=call, keypair=wallet.coldkey
923+
call=call, keypair=wallet.coldkey, era={"period": era}
921924
)
922925

923926
response = await subtensor.substrate.submit_extrinsic(

0 commit comments

Comments
 (0)