-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add cycles balance command #3416
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This functionality is actually not part of the design but it's a nice addition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, and I hadn't noticed that. I was looking at the arguments of
icrc1_balance_of
, which has anowner
field, but the design doc doesn't require thatdfx cycles balance
passes anything other than the user's principal.