Skip to content

Commit 8cb62ce

Browse files
authored
feat: improve missing ledger error message (#3995)
1 parent 1c22db8 commit 8cb62ce

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Allow setting permissions lists in init arguments just like in upgrade arguments
4141
- Module hash: f45db224b40fac516c877e3108dc809d4b22fa42d05ee8dfa5002536a3a3daed
4242
- Bump agent-js to fix error code
4343

44-
### chore!: improve the messages for the subcommands of `dfx cycles`.
44+
### chore!: improve the messages for the subcommands of `dfx cycles` and `dfx ledger`.
4545

46-
If users run subcommands of `dfx cycles` without the `--ic` flag, show below messages to indicate what to do next.
46+
If users run subcommands of `dfx cycles` or `dfx ledger` without the `--ic` flag, show below messages to indicate what to do next.
4747
```
4848
Error explanation:
4949
Cycles ledger with canister ID 'um5iw-rqaaa-aaaaq-qaaba-cai' is not installed.

e2e/tests-dfx/ledger.bash

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
load ../utils/_
44

5+
install_nns() {
6+
dfx_start_for_nns_install
7+
8+
dfx extension install nns --version 0.4.3
9+
dfx nns install --ledger-accounts 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752 22ca7edac648b814e81d7946e8bacea99280e07c5f51a04ba7a38009d8ad8e89 5a94fe181e9d411c58726cb87cbf2d016241b6c350bc3330e4869ca76e54ecbc
10+
}
11+
512
setup() {
613
standard_setup
714
install_asset ledger
815
install_shared_asset subnet_type/shared_network_settings/system
916

1017
dfx identity import --storage-mode plaintext alice alice.pem
1118
dfx identity import --storage-mode plaintext bob bob.pem
12-
13-
dfx_start_for_nns_install
14-
15-
dfx extension install nns --version 0.4.3
16-
dfx nns install --ledger-accounts 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752 22ca7edac648b814e81d7946e8bacea99280e07c5f51a04ba7a38009d8ad8e89 5a94fe181e9d411c58726cb87cbf2d016241b6c350bc3330e4869ca76e54ecbc
1719
}
1820

1921
teardown() {
@@ -27,6 +29,8 @@ current_time_nanoseconds() {
2729
}
2830

2931
@test "ledger account-id" {
32+
install_nns
33+
3034
dfx identity use alice
3135
assert_command dfx ledger account-id
3236
assert_match 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
@@ -46,6 +50,8 @@ current_time_nanoseconds() {
4650
}
4751

4852
@test "ledger balance & transfer" {
53+
install_nns
54+
4955
dfx identity use alice
5056
assert_command dfx ledger account-id
5157
assert_eq 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
@@ -104,6 +110,8 @@ current_time_nanoseconds() {
104110
}
105111

106112
@test "ledger subaccounts" {
113+
install_nns
114+
107115
subacct=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
108116
assert_command dfx ledger account-id --identity bob --subaccount "$subacct"
109117
assert_match 5a94fe181e9d411c58726cb87cbf2d016241b6c350bc3330e4869ca76e54ecbc
@@ -140,6 +148,8 @@ tc_to_num() {
140148
}
141149

142150
@test "ledger top-up" {
151+
install_nns
152+
143153
dfx identity use alice
144154
assert_command dfx ledger balance
145155
assert_match "1000000000.00000000 ICP"
@@ -198,6 +208,8 @@ tc_to_num() {
198208
}
199209

200210
@test "ledger create-canister" {
211+
install_nns
212+
201213
dfx identity use alice
202214
assert_command dfx ledger create-canister --amount=100 --subnet-type "type1" "$(dfx identity get-principal)"
203215
assert_match "Transfer sent at block height"
@@ -269,6 +281,7 @@ tc_to_num() {
269281
}
270282

271283
@test "ledger show-subnet-types" {
284+
install_nns
272285
install_asset cmc
273286

274287
dfx deploy cmc
@@ -278,3 +291,10 @@ tc_to_num() {
278291
assert_command dfx ledger show-subnet-types --cycles-minting-canister-id "$CANISTER_ID"
279292
assert_eq '["type1", "type2"]'
280293
}
294+
295+
@test "balance without ledger fails as expected" {
296+
dfx_start
297+
298+
assert_command_fail dfx ledger balance
299+
assert_contains "ICP Ledger with canister ID 'ryjl3-tyaaa-aaaaa-aaaba-cai' is not installed."
300+
}

src/dfx/src/lib/diagnosis.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub fn diagnose(err: &AnyhowError) -> Diagnosis {
5757
if cycles_ledger_not_found(err) {
5858
return diagnose_cycles_ledger_not_found();
5959
}
60+
if ledger_not_found(err) {
61+
return diagnose_ledger_not_found();
62+
}
6063
}
6164

6265
if local_replica_not_running(err) {
@@ -246,3 +249,16 @@ fn diagnose_cycles_ledger_not_found() -> Diagnosis {
246249

247250
(Some(explanation.to_string()), Some(suggestion.to_string()))
248251
}
252+
253+
fn ledger_not_found(err: &AnyhowError) -> bool {
254+
err.to_string()
255+
.contains("Canister ryjl3-tyaaa-aaaaa-aaaba-cai not found")
256+
}
257+
258+
fn diagnose_ledger_not_found() -> Diagnosis {
259+
let explanation = "ICP Ledger with canister ID 'ryjl3-tyaaa-aaaaa-aaaba-cai' is not installed.";
260+
let suggestion =
261+
"Run the command with '--ic' flag if you want to manage the ICP on the mainnet.";
262+
263+
(Some(explanation.to_string()), Some(suggestion.to_string()))
264+
}

0 commit comments

Comments
 (0)