diff --git a/CHANGELOG.md b/CHANGELOG.md index c594f4f3c9..515e2de4fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,10 @@ Certain suffixes that replace a number of zeros are now supported. The (case-ins For cycles an additional `c` or `C` is also acceptable. For example: `dfx canister deposit-cycles 3TC mycanister` +### feat: `dfx ledger top-up` also accepts canister names + +Previously, `dfx ledger top-up` only accepted canister principals. Now it accepts both principals and canister names. + ### feat: added `dfx cycles` command This won't work on mainnet yet, but can work locally after installing the cycles ledger. diff --git a/docs/cli-reference/dfx-ledger.md b/docs/cli-reference/dfx-ledger.md index 25906e3a07..20f64b56a6 100644 --- a/docs/cli-reference/dfx-ledger.md +++ b/docs/cli-reference/dfx-ledger.md @@ -276,9 +276,9 @@ dfx ledger top-up [options] canister [flag] --network ic You can specify the following argument for the `dfx ledger top-up` command. -| Argument | Description | -|------------|------------------------------------------------------------------| -| `canister` | Specifies the canister identifier that you would like to top up. | +| Argument | Description | +|------------|--------------------------------------------------------------------------| +| `canister` | Specifies the canister identifier or name that you would like to top up. | ### Options diff --git a/e2e/tests-dfx/ledger.bash b/e2e/tests-dfx/ledger.bash index 56a0375a87..b22c6f0c99 100644 --- a/e2e/tests-dfx/ledger.bash +++ b/e2e/tests-dfx/ledger.bash @@ -184,6 +184,12 @@ tc_to_num() { assert_contains "Using transfer at block height $block_height" "$stdout" # shellcheck disable=SC2154 assert_contains "Canister was topped up with" "$stdout" + + # Top up canister by name instead of principal + dfx_new + assert_command dfx canister create e2e_project_backend + assert_command dfx ledger top-up e2e_project_backend --amount 5 + assert_contains "Canister was topped up with 617283500000000 cycles" } @test "ledger create-canister" { diff --git a/src/dfx/src/commands/ledger/top_up.rs b/src/dfx/src/commands/ledger/top_up.rs index 80265460f0..2c3defd80a 100644 --- a/src/dfx/src/commands/ledger/top_up.rs +++ b/src/dfx/src/commands/ledger/top_up.rs @@ -17,7 +17,7 @@ const MEMO_TOP_UP_CANISTER: u64 = 1347768404_u64; /// Top up a canister with cycles minted from ICP #[derive(Parser)] pub struct TopUpOpts { - /// Specify the canister id to top up + /// Specify the canister id or name to top up canister: String, /// Subaccount to withdraw from @@ -58,12 +58,14 @@ pub async fn exec(env: &dyn Environment, opts: TopUpOpts) -> DfxResult { let memo = Memo(MEMO_TOP_UP_CANISTER); - let to = Principal::from_text(&opts.canister).with_context(|| { - format!( - "Failed to parse {:?} as target canister principal.", - &opts.canister - ) - })?; + let to = Principal::from_text(&opts.canister) + .or_else(|_| env.get_canister_id_store()?.get(&opts.canister)) + .with_context(|| { + format!( + "Failed to parse {:?} as target canister principal or name.", + &opts.canister + ) + })?; let agent = env.get_agent();