-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cycles balance command (#3416)
Adds the `dfx cycles balance` subcommand, hidden for the time being. Fixes https://dfinity.atlassian.net/browse/SDK-1160
- Loading branch information
1 parent
490d4e2
commit 5ba48e3
Showing
14 changed files
with
323 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# dfx cycles | ||
|
||
> **NOTE**: The cycles ledger is in development and the dfx cycles command is not expected to work on mainnet at this time. | ||
Use the `dfx cycles` command to manage cycles associated with an identity's principal. | ||
|
||
The basic syntax for running `dfx cycles` commands is: | ||
|
||
``` bash | ||
dfx cycles [subcommand] [options] | ||
``` | ||
|
||
The following subcommands are available: | ||
|
||
| Command | Description | | ||
|---------------------------------------|--------------------------------------------------------------------------------------| | ||
| [`balance`](#dfx-ledger-balance) | Prints the account balance of the user. | | ||
| `help` | Displays usage information message for a specified subcommand. | | ||
|
||
To view usage information for a specific subcommand, specify the subcommand and the `--help` flag. For example, to see usage information for `dfx cycles balance`, you can run the following command: | ||
|
||
`dfx cycles balance --help` | ||
|
||
## dfx cycles balance | ||
|
||
Use the `dfx cycles balance` command to print your account balance or that of another user. | ||
|
||
### Basic usage | ||
|
||
``` bash | ||
dfx cycles balance [flag] --network ic | ||
``` | ||
|
||
### Options | ||
|
||
You can specify the following arguments for the `dfx cycles balance` command. | ||
|
||
| Option | Description | | ||
|---------------------------------------------|---------------------------------------------------------------------| | ||
| `--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. | ||
|
||
``` | ||
$ dfx cycles balance --network ic | ||
89.000 TC (trillion cycles). | ||
``` | ||
|
||
To see the exact amount of cycles, you can use the `--precise` option: | ||
``` | ||
$ dfx cycles balance --network ic --precise | ||
89000000000000 cycles. | ||
``` | ||
|
||
You can use the `dfx cycles balance` command to check the balance of another principal: | ||
|
||
``` bash | ||
dfx cycles balance --owner raxcz-bidhr-evrzj-qyivt-nht5a-eltcc-24qfc-o6cvi-hfw7j-dcecz-kae --network ic | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::lib::environment::Environment; | ||
use crate::lib::error::DfxResult; | ||
use crate::lib::nns_types::account_identifier::Subaccount; | ||
use crate::lib::operations::cycles_ledger; | ||
use crate::util::{format_as_trillions, pretty_thousand_separators}; | ||
use candid::Principal; | ||
use clap::Parser; | ||
|
||
/// Get the cycle balance of the selected Identity's cycles wallet. | ||
#[derive(Parser)] | ||
pub struct CyclesBalanceOpts { | ||
/// Specifies a Principal to get the balance of | ||
#[arg(long)] | ||
owner: Option<Principal>, | ||
|
||
/// Subaccount of the selected identity to get the balance of | ||
#[arg(long)] | ||
subaccount: Option<Subaccount>, | ||
|
||
/// 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 { | ||
let agent = env.get_agent(); | ||
|
||
let owner = opts.owner.unwrap_or_else(|| { | ||
env.get_selected_identity_principal() | ||
.expect("Selected identity not instantiated.") | ||
}); | ||
|
||
let subaccount = opts.subaccount.map(|x| x.0); | ||
|
||
let balance = | ||
cycles_ledger::balance(agent, owner, subaccount, opts.cycles_ledger_canister_id).await?; | ||
|
||
if opts.precise { | ||
println!("{} cycles.", balance); | ||
} else { | ||
println!( | ||
"{} TC (trillion cycles).", | ||
pretty_thousand_separators(format_as_trillions(balance)) | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::lib::agent::create_agent_environment; | ||
use crate::lib::environment::Environment; | ||
use crate::lib::error::DfxResult; | ||
use crate::lib::network::network_opt::NetworkOpt; | ||
use clap::Parser; | ||
use tokio::runtime::Runtime; | ||
|
||
mod balance; | ||
|
||
/// Helper commands to manage the user's cycles. | ||
#[derive(Parser)] | ||
#[command(name = "wallet")] | ||
pub struct CyclesOpts { | ||
#[command(flatten)] | ||
network: NetworkOpt, | ||
|
||
#[command(subcommand)] | ||
subcmd: SubCommand, | ||
} | ||
|
||
#[derive(Parser)] | ||
enum SubCommand { | ||
Balance(balance::CyclesBalanceOpts), | ||
} | ||
|
||
pub fn exec(env: &dyn Environment, opts: CyclesOpts) -> DfxResult { | ||
let agent_env = create_agent_environment(env, opts.network.to_network_name())?; | ||
let runtime = Runtime::new().expect("Unable to create a runtime"); | ||
runtime.block_on(async { | ||
match opts.subcmd { | ||
SubCommand::Balance(v) => balance::exec(&agent_env, v).await, | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.