Skip to content

Commit

Permalink
feat!: update dfx cycles commands with maninet cycles-ledger cani…
Browse files Browse the repository at this point in the history
…ster ID (#3481)
  • Loading branch information
Marcin Nowak-Liebiediew authored Dec 22, 2023
1 parent b9b5c36 commit 4346ed7
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 118 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# UNRELEASED

### feat!: update `dfx cycles` commands with mainnet `cycles-ledger` canister ID

The `dfx cycles` command no longer needs nor accepts the `--cycles-ledger-canister-id <canister id>` parameter.

# 0.15.3

### fix: allow `http://localhost:*` as `connect-src` in the asset canister's CSP
Expand Down
3 changes: 0 additions & 3 deletions docs/cli-reference/dfx-cycles.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ You can specify the following arguments for the `dfx cycles balance` command.
| `--owner <principal>` | Display the balance of this principal |
| `--subaccount <subaccount>` | Display the balance of this subaccount |
| `--precise` | Displays the exact balance, without scaling to trillions of cycles. |
| `--cycles-ledger-canister-id <canister id>` | Specify the ID of the cycles ledger canister. |

### Examples

> **NOTE**: None of the examples below specify the `--cycles-ledger-canister-id` option, but it is required until the cycles ledger canister ID is known.
Check the cycles balance of the selected identity.

```
Expand Down
173 changes: 86 additions & 87 deletions e2e/tests-dfx/cycles-ledger.bash

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions src/dfx/src/commands/cycles/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ pub struct CyclesBalanceOpts {
/// Get balance raw value (without upscaling to trillions of cycles).
#[arg(long)]
precise: bool,

/// Canister ID of the cycles ledger canister.
/// If not specified, the default cycles ledger canister ID will be used.
// todo: remove this. See https://dfinity.atlassian.net/browse/SDK-1262
#[arg(long)]
cycles_ledger_canister_id: Principal,
}

pub async fn exec(env: &dyn Environment, opts: CyclesBalanceOpts) -> DfxResult {
Expand All @@ -41,8 +35,7 @@ pub async fn exec(env: &dyn Environment, opts: CyclesBalanceOpts) -> DfxResult {

let subaccount = opts.subaccount.map(|x| x.0);

let balance =
cycles_ledger::balance(agent, owner, subaccount, opts.cycles_ledger_canister_id).await?;
let balance = cycles_ledger::balance(agent, owner, subaccount).await?;

if opts.precise {
println!("{} cycles.", balance);
Expand Down
7 changes: 0 additions & 7 deletions src/dfx/src/commands/cycles/top_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ pub struct TopUpOpts {
/// https://internetcomputer.org/docs/current/developer-docs/integrations/icrc-1/#transaction-deduplication-
#[arg(long)]
created_at_time: Option<u64>,

/// Canister ID of the cycles ledger canister.
/// If not specified, the default cycles ledger canister ID will be used.
// todo: remove this. See https://dfinity.atlassian.net/browse/SDK-1262
#[arg(long)]
cycles_ledger_canister_id: Principal,
}

pub async fn exec(env: &dyn Environment, opts: TopUpOpts) -> DfxResult {
Expand All @@ -58,7 +52,6 @@ pub async fn exec(env: &dyn Environment, opts: TopUpOpts) -> DfxResult {
amount,
created_at_time,
from_subaccount,
opts.cycles_ledger_canister_id,
)
.await;
if result.is_err() && opts.created_at_time.is_none() {
Expand Down
7 changes: 0 additions & 7 deletions src/dfx/src/commands/cycles/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ pub struct TransferOpts {
/// Memo.
#[arg(long)]
memo: Option<u64>,

/// Canister ID of the cycles ledger canister.
/// If not specified, the default cycles ledger canister ID will be used.
// todo: remove this. See https://dfinity.atlassian.net/browse/SDK-1262
#[arg(long)]
cycles_ledger_canister_id: Principal,
}

pub async fn exec(env: &dyn Environment, opts: TransferOpts) -> DfxResult {
Expand Down Expand Up @@ -68,7 +62,6 @@ pub async fn exec(env: &dyn Environment, opts: TransferOpts) -> DfxResult {
to_subaccount,
created_at_time,
opts.memo,
opts.cycles_ledger_canister_id,
)
.await;
if result.is_err() && opts.created_at_time.is_none() {
Expand Down
19 changes: 13 additions & 6 deletions src/dfx/src/lib/operations/cycles_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ use slog::{info, Logger};
const ICRC1_BALANCE_OF_METHOD: &str = "icrc1_balance_of";
const ICRC1_TRANSFER_METHOD: &str = "icrc1_transfer";
const SEND_METHOD: &str = "send";
const CYCLES_LEDGER_CANISTER_ID: Principal =
Principal::from_slice(&[0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x02, 0x01, 0x01]);

pub async fn balance(
agent: &Agent,
owner: Principal,
subaccount: Option<icrc1::account::Subaccount>,
cycles_ledger_canister_id: Principal,
) -> DfxResult<u128> {
let canister = Canister::builder()
.with_agent(agent)
.with_canister_id(cycles_ledger_canister_id)
.with_canister_id(CYCLES_LEDGER_CANISTER_ID)
.build()?;
let arg = icrc1::account::Account { owner, subaccount };

Expand Down Expand Up @@ -58,11 +59,10 @@ pub async fn transfer(
to_subaccount: Option<icrc1::account::Subaccount>,
created_at_time: u64,
memo: Option<u64>,
cycles_ledger_canister_id: Principal,
) -> DfxResult<BlockIndex> {
let canister = Canister::builder()
.with_agent(agent)
.with_canister_id(cycles_ledger_canister_id)
.with_canister_id(CYCLES_LEDGER_CANISTER_ID)
.build()?;

let retry_policy = ExponentialBackoff::default();
Expand Down Expand Up @@ -118,11 +118,10 @@ pub async fn send(
amount: u128,
created_at_time: u64,
from_subaccount: Option<icrc1::account::Subaccount>,
cycles_ledger_canister_id: Principal,
) -> DfxResult<BlockIndex> {
let canister = Canister::builder()
.with_agent(agent)
.with_canister_id(cycles_ledger_canister_id)
.with_canister_id(CYCLES_LEDGER_CANISTER_ID)
.build()?;

let retry_policy = ExponentialBackoff::default();
Expand Down Expand Up @@ -170,3 +169,11 @@ pub async fn send(

Ok(block_index)
}

#[test]
fn ledger_canister_id_text_representation() {
assert_eq!(
Principal::from_text("um5iw-rqaaa-aaaaq-qaaba-cai").unwrap(),
CYCLES_LEDGER_CANISTER_ID
);
}

0 comments on commit 4346ed7

Please sign in to comment.