Skip to content

Commit 1faddeb

Browse files
committed
Childkey take
1 parent 8f0437c commit 1faddeb

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

bittensor_cli/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4128,7 +4128,7 @@ def stake_childkey_take(
41284128
netuid = IntPrompt.ask(
41294129
"Enter netuid (leave blank for all)", default=None, show_default=True
41304130
)
4131-
return self._run_command(
4131+
results: list[tuple[Optional[int], bool]] = self._run_command(
41324132
children_hotkeys.childkey_take(
41334133
wallet=wallet,
41344134
subtensor=self.initialize_chain(network),
@@ -4140,6 +4140,12 @@ def stake_childkey_take(
41404140
prompt=prompt,
41414141
)
41424142
)
4143+
if json_output:
4144+
output = {}
4145+
for netuid_, success in results:
4146+
output[netuid_] = success
4147+
json_console.print(json.dumps(output))
4148+
return results
41434149

41444150
def sudo_set(
41454151
self,

bittensor_cli/src/commands/stake/children_hotkeys.py

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44

55
from bittensor_wallet import Wallet
6-
from rich.prompt import Confirm, Prompt, IntPrompt
6+
from rich.prompt import Confirm, IntPrompt, FloatPrompt
77
from rich.table import Table
88
from rich.text import Text
99
from async_substrate_interface.errors import SubstrateRequestException
@@ -671,8 +671,13 @@ async def childkey_take(
671671
wait_for_inclusion: bool = True,
672672
wait_for_finalization: bool = True,
673673
prompt: bool = True,
674-
):
675-
"""Get or Set childkey take."""
674+
) -> list[tuple[Optional[int], bool]]:
675+
"""
676+
Get or Set childkey take.
677+
678+
Returns:
679+
List of (netuid, success) for specified netuid (or all) and their success in setting take
680+
"""
676681

677682
def validate_take_value(take_value: float) -> bool:
678683
if not (0 <= take_value <= 0.18):
@@ -682,20 +687,7 @@ def validate_take_value(take_value: float) -> bool:
682687
return False
683688
return True
684689

685-
def print_all_takes(takes: list[tuple[int, float]], ss58: str):
686-
"""Print table with netuids and Takes"""
687-
table = Table(
688-
title=f"Current Child Takes for [bright_magenta]{ss58}[/bright_magenta]"
689-
)
690-
table.add_column("Netuid", justify="center", style="cyan")
691-
table.add_column("Take (%)", justify="right", style="magenta")
692-
693-
for take_netuid, take_value in takes:
694-
table.add_row(str(take_netuid), f"{take_value:.2f}%")
695-
696-
console.print(table)
697-
698-
async def display_chk_take(ss58, take_netuid):
690+
async def display_chk_take(ss58, take_netuid) -> float:
699691
"""Print single key take for hotkey and netuid"""
700692
chk_take = await get_childkey_take(
701693
subtensor=subtensor, netuid=take_netuid, hotkey=ss58
@@ -706,6 +698,7 @@ async def display_chk_take(ss58, take_netuid):
706698
console.print(
707699
f"Child take for {ss58} is: {chk_take * 100:.2f}% on netuid {take_netuid}."
708700
)
701+
return chk_take
709702

710703
async def chk_all_subnets(ss58):
711704
"""Aggregate data for childkey take from all subnets"""
@@ -720,10 +713,18 @@ async def chk_all_subnets(ss58):
720713
if curr_take is not None:
721714
take_value = u16_to_float(curr_take)
722715
takes.append((subnet, take_value * 100))
716+
table = Table(
717+
title=f"Current Child Takes for [bright_magenta]{ss58}[/bright_magenta]"
718+
)
719+
table.add_column("Netuid", justify="center", style="cyan")
720+
table.add_column("Take (%)", justify="right", style="magenta")
721+
722+
for take_netuid, take_value in takes:
723+
table.add_row(str(take_netuid), f"{take_value:.2f}%")
723724

724-
print_all_takes(takes, ss58)
725+
console.print(table)
725726

726-
async def set_chk_take_subnet(subnet, chk_take):
727+
async def set_chk_take_subnet(subnet: int, chk_take: float) -> bool:
727728
"""Set the childkey take for a single subnet"""
728729
success, message = await set_childkey_take_extrinsic(
729730
subtensor=subtensor,
@@ -741,84 +742,82 @@ async def set_chk_take_subnet(subnet, chk_take):
741742
console.print(
742743
f"The childkey take for {wallet.hotkey.ss58_address} is now set to {take * 100:.2f}%."
743744
)
745+
return True
744746
else:
745747
console.print(
746748
f":cross_mark:[red] Unable to set childkey take.[/red] {message}"
747749
)
750+
return False
748751

749752
# Print childkey take for other user and return (dont offer to change take rate)
750-
if hotkey and hotkey != wallet.hotkey.ss58_address:
753+
if not hotkey or hotkey == wallet.hotkey.ss58_address:
754+
hotkey = wallet.hotkey.ss58_address
755+
if hotkey != wallet.hotkey.ss58_address or not take:
751756
# display childkey take for other users
752757
if netuid:
753758
await display_chk_take(hotkey, netuid)
754759
if take:
755760
console.print(
756761
f"Hotkey {hotkey} not associated with wallet {wallet.name}."
757762
)
758-
return
763+
return [(netuid, False)]
759764
else:
760-
# show childhotkey take on all subnets
765+
# show child hotkey take on all subnets
761766
await chk_all_subnets(hotkey)
762767
if take:
763768
console.print(
764769
f"Hotkey {hotkey} not associated with wallet {wallet.name}."
765770
)
766-
return
771+
return [(netuid, False)]
767772

768773
# Validate child SS58 addresses
769774
if not take:
770-
# print current Take, ask if change
771-
if netuid:
772-
await display_chk_take(wallet.hotkey.ss58_address, netuid)
773-
else:
774-
# print take from all netuids
775-
await chk_all_subnets(wallet.hotkey.ss58_address)
776-
777775
if not Confirm.ask("Would you like to change the child take?"):
778-
return
779-
new_take_str = Prompt.ask("Enter the new take value (between 0 and 0.18)")
780-
try:
781-
new_take_value = float(new_take_str)
782-
if not validate_take_value(new_take_value):
783-
return
784-
except ValueError:
785-
err_console.print(
786-
":cross_mark:[red] Invalid input. Please enter a number between 0 and 0.18.[/red]"
776+
return [(netuid, False)]
777+
new_take_value = -1.0
778+
while not validate_take_value(new_take_value):
779+
new_take_value = FloatPrompt.ask(
780+
"Enter the new take value (between 0 and 0.18)"
787781
)
788-
return
789782
take = new_take_value
790783
else:
791784
if not validate_take_value(take):
792-
return
785+
return [(netuid, False)]
793786

794787
if netuid:
795-
await set_chk_take_subnet(subnet=netuid, chk_take=take)
796-
return
788+
return [(netuid, await set_chk_take_subnet(subnet=netuid, chk_take=take))]
797789
else:
798790
new_take_netuids = IntPrompt.ask(
799791
"Enter netuid (leave blank for all)", default=None, show_default=True
800792
)
801793

802794
if new_take_netuids:
803-
await set_chk_take_subnet(subnet=new_take_netuids, chk_take=take)
804-
return
795+
return [
796+
(
797+
new_take_netuids,
798+
await set_chk_take_subnet(subnet=new_take_netuids, chk_take=take),
799+
)
800+
]
805801

806802
else:
807803
netuids = await subtensor.get_all_subnet_netuids()
808-
for netuid in netuids:
809-
if netuid == 0:
804+
output_list = []
805+
for netuid_ in netuids:
806+
if netuid_ == 0:
810807
continue
811-
console.print(f"Sending to netuid {netuid} take of {take * 100:.2f}%")
812-
await set_childkey_take_extrinsic(
808+
console.print(f"Sending to netuid {netuid_} take of {take * 100:.2f}%")
809+
result = await set_childkey_take_extrinsic(
813810
subtensor=subtensor,
814811
wallet=wallet,
815-
netuid=netuid,
812+
netuid=netuid_,
816813
hotkey=wallet.hotkey.ss58_address,
817814
take=take,
818815
prompt=prompt,
819816
wait_for_inclusion=True,
820817
wait_for_finalization=False,
821818
)
819+
output_list.append((netuid_, result))
822820
console.print(
823821
f":white_heavy_check_mark: [green]Sent childkey take of {take * 100:.2f}% to all subnets.[/green]"
824822
)
823+
return output_list

bittensor_cli/src/commands/stake/remove.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,7 @@ async def unstake(
299299
}
300300
else:
301301
func = _unstake_extrinsic
302-
specific_args = {
303-
"current_stake": op["current_stake_balance"]
304-
}
302+
specific_args = {"current_stake": op["current_stake_balance"]}
305303

306304
suc = await func(**common_args, **specific_args)
307305

0 commit comments

Comments
 (0)