Skip to content

Commit

Permalink
feat: dfx ledger top-up also accepts canister names (#3458)
Browse files Browse the repository at this point in the history
`dfx ledger top-up` so far only accepts canister principals, but not names. With this PR it will also accept canister names like every other command does.
  • Loading branch information
sesi200 authored Dec 1, 2023
1 parent 0626571 commit 29f3a83
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions docs/cli-reference/dfx-ledger.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions e2e/tests-dfx/ledger.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
16 changes: 9 additions & 7 deletions src/dfx/src/commands/ledger/top_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 29f3a83

Please sign in to comment.