Skip to content

Commit 4d7ca86

Browse files
committed
Improved the JSON output of stake add to include error messages.
1 parent 4b24ca8 commit 4d7ca86

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

bittensor_cli/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3316,13 +3316,13 @@ def stake_add(
33163316
"You have specified hotkeys to include and also the `--all-hotkeys` flag. The flag"
33173317
"should only be used standalone (to use all hotkeys) or with `--exclude-hotkeys`."
33183318
)
3319-
raise typer.Exit()
3319+
return
33203320

33213321
if include_hotkeys and exclude_hotkeys:
33223322
print_error(
33233323
"You have specified options for both including and excluding hotkeys. Select one or the other."
33243324
)
3325-
raise typer.Exit()
3325+
return
33263326

33273327
if not wallet_hotkey and not all_hotkeys and not include_hotkeys:
33283328
if not wallet_name:

bittensor_cli/src/commands/stake/add.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def safe_stake_extrinsic(
7272
hotkey_ss58_: str,
7373
price_limit: Balance,
7474
status=None,
75-
) -> bool:
75+
) -> tuple[bool, str]:
7676
err_out = partial(print_error, status=status)
7777
failure_prelude = (
7878
f":cross_mark: [red]Failed[/red] to stake {amount_} on Netuid {netuid_}"
@@ -104,25 +104,24 @@ async def safe_stake_extrinsic(
104104
)
105105
except SubstrateRequestException as e:
106106
if "Custom error: 8" in str(e):
107-
print_error(
108-
f"\n{failure_prelude}: Price exceeded tolerance limit. "
107+
err_msg = (
108+
f"{failure_prelude}: Price exceeded tolerance limit. "
109109
f"Transaction rejected because partial staking is disabled. "
110110
f"Either increase price tolerance or enable partial staking.",
111-
status=status,
112111
)
113-
return False
112+
print_error("\n" + err_msg, status=status)
114113
else:
115-
err_out(f"\n{failure_prelude} with error: {format_error_message(e)}")
116-
return False
114+
err_msg = f"{failure_prelude} with error: {format_error_message(e)}"
115+
err_out("\n" + err_msg)
116+
return False, err_msg
117117
if not await response.is_success:
118-
err_out(
119-
f"\n{failure_prelude} with error: {format_error_message(await response.error_message)}"
120-
)
121-
return False
118+
err_msg = f"{failure_prelude} with error: {format_error_message(await response.error_message)}"
119+
err_out("\n" + err_msg)
120+
return False, err_msg
122121
else:
123122
if json_output:
124123
# the rest of this checking is not necessary if using json_output
125-
return True
124+
return True, ""
126125
block_hash = await subtensor.substrate.get_chain_head()
127126
new_balance, new_stake = await asyncio.gather(
128127
subtensor.get_balance(wallet.coldkeypub.ss58_address, block_hash),
@@ -160,11 +159,11 @@ async def safe_stake_extrinsic(
160159
f":arrow_right: "
161160
f"[{COLOR_PALETTE['STAKE']['STAKE_AMOUNT']}]{new_stake}\n"
162161
)
163-
return True
162+
return True, ""
164163

165164
async def stake_extrinsic(
166165
netuid_i, amount_, current, staking_address_ss58, status=None
167-
) -> bool:
166+
) -> tuple[bool, str]:
168167
err_out = partial(print_error, status=status)
169168
current_balance, next_nonce, call = await asyncio.gather(
170169
subtensor.get_balance(wallet.coldkeypub.ss58_address),
@@ -190,18 +189,18 @@ async def stake_extrinsic(
190189
extrinsic, wait_for_inclusion=True, wait_for_finalization=False
191190
)
192191
except SubstrateRequestException as e:
193-
err_out(f"\n{failure_prelude} with error: {format_error_message(e)}")
194-
return False
192+
err_msg = f"{failure_prelude} with error: {format_error_message(e)}"
193+
err_out("\n" + err_msg)
194+
return False, err_msg
195195
else:
196196
if not await response.is_success:
197-
err_out(
198-
f"\n{failure_prelude} with error: {format_error_message(await response.error_message)}"
199-
)
200-
return False
197+
err_msg = f"{failure_prelude} with error: {format_error_message(await response.error_message)}"
198+
err_out("\n" + err_msg)
199+
return False, err_msg
201200
else:
202201
if json_output:
203202
# the rest of this is not necessary if using json_output
204-
return True
203+
return True, ""
205204
new_block_hash = await subtensor.substrate.get_chain_head()
206205
new_balance, new_stake = await asyncio.gather(
207206
subtensor.get_balance(
@@ -230,7 +229,7 @@ async def stake_extrinsic(
230229
f":arrow_right: "
231230
f"[{COLOR_PALETTE['STAKE']['STAKE_AMOUNT']}]{new_stake}\n"
232231
)
233-
return True
232+
return True, ""
234233

235234
netuids = (
236235
netuids if netuids is not None else await subtensor.get_all_subnet_netuids()
@@ -417,13 +416,17 @@ async def stake_extrinsic(
417416
for _, staking_address in hotkeys_to_stake_to
418417
}
419418
successes = defaultdict(dict)
419+
error_messages = defaultdict(dict)
420420
with console.status(f"\n:satellite: Staking on netuid(s): {netuids} ..."):
421421
# We can gather them all at once but balance reporting will be in race-condition.
422422
for (ni, staking_address), coroutine in stake_coroutines.items():
423-
success = await coroutine
423+
success, er_msg = await coroutine
424424
successes[ni][staking_address] = success
425+
error_messages[ni][staking_address] = er_msg
425426
if json_output:
426-
json_console.print(json.dumps({"staking_success": successes}))
427+
json_console.print(
428+
json.dumps({"staking_success": successes, "error_messages": error_messages})
429+
)
427430

428431

429432
# Helper functions

tests/e2e_tests/test_staking_sudo.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,22 @@ def test_staking(local_chain, wallet_setup):
343343
"--no-prompt",
344344
"--era",
345345
"144",
346+
"--json-output",
346347
],
347348
)
348-
assert "✅ Finalized" in add_stake_multiple.stdout, add_stake_multiple.stderr
349+
add_stake_multiple_output = json.loads(add_stake_multiple.stdout)
349350
for netuid_ in multiple_netuids:
350-
assert f"Stake added to netuid: {netuid_}" in add_stake_multiple.stdout, (
351-
add_stake_multiple.stderr
351+
assert (
352+
add_stake_multiple_output["staking_success"][str(netuid_)][
353+
wallet_alice.hotkey.ss58_address
354+
]
355+
is True
356+
)
357+
assert (
358+
add_stake_multiple_output["error_messages"][str(netuid_)][
359+
wallet_alice.hotkey.ss58_address
360+
]
361+
== ""
352362
)
353363

354364
# Fetch the hyperparameters of the subnet

0 commit comments

Comments
 (0)