Skip to content

Commit a0a1e6e

Browse files
committed
validity checks
1 parent d85a1b5 commit a0a1e6e

File tree

1 file changed

+36
-22
lines changed
  • bittensor_cli/src/commands/stake

1 file changed

+36
-22
lines changed

bittensor_cli/src/commands/stake/claim.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def set_claim_type(
2929
wallet: Wallet,
3030
subtensor: "SubtensorInterface",
3131
claim_type: Optional[str] = None,
32-
netuids: Optional[list[int]] = None,
32+
netuids: Optional[str] = None,
3333
prompt: bool = True,
3434
json_output: bool = False,
3535
) -> tuple[bool, str, Optional[str]]:
@@ -45,7 +45,7 @@ async def set_claim_type(
4545
wallet: Bittensor wallet object
4646
subtensor: SubtensorInterface object
4747
claim_type: Optional claim type ("Keep" or "Swap"). If None, user will be prompted.
48-
netuids: Optional list of subnet IDs to keep (only valid with "Keep" type)
48+
netuids: Optional string of subnet IDs (e.g., "1-5,10,20-30"). Will be parsed internally.
4949
prompt: Whether to prompt for user confirmation
5050
json_output: Whether to output JSON
5151
@@ -56,12 +56,34 @@ async def set_claim_type(
5656
- Optional[str]: Extrinsic identifier if successful
5757
"""
5858

59+
if claim_type is not None:
60+
claim_type = claim_type.capitalize()
61+
if claim_type not in ["Keep", "Swap"]:
62+
msg = f"Invalid claim type: {claim_type}. Use 'Keep' or 'Swap', or omit for interactive mode."
63+
err_console.print(f"[red]{msg}[/red]")
64+
if json_output:
65+
json_console.print(json.dumps({"success": False, "message": msg}))
66+
return False, msg, None
67+
5968
current_claim_info, all_netuids = await asyncio.gather(
6069
subtensor.get_coldkey_claim_type(coldkey_ss58=wallet.coldkeypub.ss58_address),
6170
subtensor.get_all_subnet_netuids(),
6271
)
6372
all_subnets = sorted([n for n in all_netuids if n != 0])
6473

74+
selected_netuids = None
75+
if netuids is not None:
76+
try:
77+
selected_netuids = parse_subnet_range(
78+
netuids, total_subnets=len(all_subnets)
79+
)
80+
except ValueError as e:
81+
msg = f"Invalid netuid format: {e}"
82+
err_console.print(f"[red]{msg}[/red]")
83+
if json_output:
84+
json_console.print(json.dumps({"success": False, "message": msg}))
85+
return False, msg, None
86+
6587
claim_table = Table(
6688
Column("[bold white]Coldkey", style=COLORS.GENERAL.COLDKEY, justify="left"),
6789
Column(
@@ -79,7 +101,7 @@ async def set_claim_type(
79101
console.print(claim_table)
80102

81103
# Full wizard
82-
if claim_type is None and netuids is None:
104+
if claim_type is None and selected_netuids is None:
83105
new_claim_info = await _ask_for_claim_types(wallet, subtensor, all_subnets)
84106
if new_claim_info is None:
85107
msg = "Operation cancelled."
@@ -97,27 +119,18 @@ async def set_claim_type(
97119
return False, msg, None
98120

99121
# Keep netuids passed thru the cli and assume Keep type
100-
elif claim_type is None and netuids is not None:
101-
new_claim_info = {"type": "KeepSubnets", "subnets": netuids}
122+
elif claim_type is None and selected_netuids is not None:
123+
new_claim_info = {"type": "KeepSubnets", "subnets": selected_netuids}
102124

103125
else:
104-
# Keep or Swap all subnets
105-
claim_type_upper = claim_type.capitalize()
106-
if claim_type_upper not in ["Swap", "Keep"]:
107-
msg = f"Invalid claim type: {claim_type}. Use 'Swap' or 'Keep', or omit for interactive mode."
108-
err_console.print(msg)
109-
if json_output:
110-
json_console.print(json.dumps({"success": False, "message": msg}))
111-
return False, msg, None
112-
113126
# Netuids passed with Keep type
114-
if netuids is not None and claim_type_upper == "Keep":
115-
new_claim_info = {"type": "KeepSubnets", "subnets": netuids}
127+
if selected_netuids is not None and claim_type == "Keep":
128+
new_claim_info = {"type": "KeepSubnets", "subnets": selected_netuids}
116129

117130
# Netuids passed with Swap type
118-
elif netuids is not None and claim_type_upper == "Swap":
119-
keep_subnets = [n for n in all_subnets if n not in netuids]
120-
invalid = [n for n in netuids if n not in all_subnets]
131+
elif selected_netuids is not None and claim_type == "Swap":
132+
keep_subnets = [n for n in all_subnets if n not in selected_netuids]
133+
invalid = [n for n in selected_netuids if n not in all_subnets]
121134
if invalid:
122135
msg = f"Invalid subnets (not available): {group_subnets(invalid)}"
123136
err_console.print(msg)
@@ -132,11 +145,12 @@ async def set_claim_type(
132145
else:
133146
new_claim_info = {"type": "KeepSubnets", "subnets": keep_subnets}
134147
else:
135-
new_claim_info = {"type": claim_type_upper}
148+
new_claim_info = {"type": claim_type}
136149

137150
if _claim_types_equal(current_claim_info, new_claim_info):
138-
msg = f"Claim type already set to {_format_claim_type_display(new_claim_info)}. No change needed."
139-
console.print(f"[yellow]{msg}[/yellow]")
151+
console.print(
152+
f"Claim type already set to {_format_claim_type_display(new_claim_info)}. No change needed."
153+
)
140154
if json_output:
141155
json_console.print(
142156
json.dumps(

0 commit comments

Comments
 (0)