Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve error message for 'dfx cycles convert'. #4019

Merged
merged 10 commits into from
Dec 4, 2024
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ This affects the following commands:
- `dfx canister update-settings`
- `dfx ledger fabricate-cycles`

### chore: improve `dfx cycles convert` messages.

If users run `dfx cycles convert` without enough ICP tokens, show additional messages to indicate what to do next.
```
Error explanation:
Insufficient ICP balance to finish the transfer transaction.
How to resolve the error:
Please top up your ICP balance.
Please run 'dfx ledger account-id' to get the address for receiving ICP tokens.
```

# 0.24.3

### feat: Bitcoin support in PocketIC
Expand Down
4 changes: 4 additions & 0 deletions e2e/tests-dfx/cycles-ledger.bash
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ current_time_nanoseconds() {

deploy_cycles_ledger

# Test failed to convert ICP to cycles without enough ICP.
assert_command_fail dfx cycles convert --amount 12.5 --identity alice
assert_contains "Insufficient ICP balance to finish the transfer transaction."

assert_command dfx --identity cycle-giver ledger transfer --memo 1234 --amount 100 "$(dfx ledger account-id --of-principal "$ALICE")"
assert_command dfx --identity cycle-giver ledger transfer --memo 1234 --amount 100 "$(dfx ledger account-id --of-principal "$ALICE" --subaccount "$ALICE_SUBACCT1")"

Expand Down
18 changes: 18 additions & 0 deletions src/dfx/src/lib/diagnosis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lib::error_code;
use crate::lib::ledger_types::TransferError as ICPTransferError;
use anyhow::Error as AnyhowError;
use dfx_core::error::root_key::FetchRootKeyError;
use ic_agent::agent::{RejectCode, RejectResponse};
Expand Down Expand Up @@ -72,6 +73,12 @@ pub fn diagnose(err: &AnyhowError) -> Diagnosis {
}
}

if let Some(transfer_error) = err.downcast_ref::<ICPTransferError>() {
if insufficient_icp(transfer_error) {
return diagnose_insufficient_icp();
}
}

NULL_DIAGNOSIS
}

Expand Down Expand Up @@ -262,3 +269,14 @@ fn diagnose_ledger_not_found() -> Diagnosis {

(Some(explanation.to_string()), Some(suggestion.to_string()))
}

fn insufficient_icp(err: &ICPTransferError) -> bool {
matches!(err, ICPTransferError::InsufficientFunds { balance: _ })
}

fn diagnose_insufficient_icp() -> Diagnosis {
vincent-dfinity marked this conversation as resolved.
Show resolved Hide resolved
let explanation = "Insufficient ICP balance to finish the transfer transaction.";
let suggestion = "Please top up your ICP balance.
Please run 'dfx ledger account-id' to get the address for receiving ICP tokens.";
(Some(explanation.to_string()), Some(suggestion.to_string()))
}
Loading