Skip to content
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

spl-token-cli: Add 'account-info' subcommand #532

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions token/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde_json = "1.0.57"
solana-account-decoder = { version = "=1.3.13" }
solana-clap-utils = { version = "=1.3.13"}
solana-cli-config = { version = "=1.3.13" }
solana-cli-output = { version = "=1.3.13" }
solana-client = { version = "=1.3.13" }
solana-logger = { version = "=1.3.13" }
solana-sdk = { version = "=1.3.13" }
Expand Down
83 changes: 81 additions & 2 deletions token/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use clap::{
};
use console::Emoji;
use solana_account_decoder::{
parse_token::{TokenAccountType, UiAccountState},
parse_token::{TokenAccountType, UiAccountState, UiTokenAmount},
UiAccountData,
};
use solana_clap_utils::{
input_parsers::pubkey_of,
input_validators::{is_amount, is_keypair, is_pubkey_or_keypair, is_url},
keypair::signer_from_path,
};
use solana_cli_output::display::println_name_value;
use solana_client::{rpc_client::RpcClient, rpc_request::TokenAccountsFilter};
use solana_sdk::{
commitment_config::CommitmentConfig,
Expand All @@ -28,7 +29,7 @@ use spl_token::{
native_mint,
state::{Account, Mint},
};
use std::process::exit;
use std::{process::exit, str::FromStr};

static WARNING: Emoji = Emoji("⚠️", "!");

Expand Down Expand Up @@ -540,6 +541,67 @@ fn command_accounts(config: &Config, token: Option<Pubkey>) -> CommandResult {
Ok(None)
}

fn stringify_ui_token_amount(amount: &UiTokenAmount) -> String {
let decimals = amount.decimals as usize;
if decimals > 0 {
let amount = u64::from_str(&amount.amount).unwrap();

// Left-pad zeros to decimals + 1, so we at least have an integer zero
let mut s = format!("{:01$}", amount, decimals + 1);

// Add the decimal point (Sorry, "," locales!)
s.insert(s.len() - decimals, '.');
s
} else {
amount.amount.clone()
}
}

fn stringify_ui_token_amount_trimmed(amount: &UiTokenAmount) -> String {
let s = stringify_ui_token_amount(amount);
let zeros_trimmed = s.trim_end_matches('0');
let decimal_trimmed = zeros_trimmed.trim_end_matches('.');
decimal_trimmed.to_string()
}

fn command_account(config: &Config, address: Pubkey) -> CommandResult {
let account = config
.rpc_client
.get_token_account_with_commitment(&address, config.commitment_config)?
.value
.unwrap();
println!();
println_name_value("Address:", &address.to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take a look and see if println_name_value would improve the output of other subcommands (mostly note to self 🙂 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I'm certain that it would!

While we're on the topic, changing the params to be AsRef<str> instead of &str might improve the ergonomics 😉

println_name_value(
"Balance:",
&stringify_ui_token_amount_trimmed(&account.token_amount),
);
let mint = format!(
"{}{}",
account.mint,
if account.is_native { " (native)" } else { "" }
);
println_name_value("Mint:", &mint);
println_name_value("Owner:", &account.owner);
println_name_value("State:", &format!("{:?}", account.state));
if let Some(delegate) = &account.delegate {
println!("Delegation:");
println_name_value(" Delegate:", delegate);
let allowance = account.delegated_amount.as_ref().unwrap();
println_name_value(
" Allowance:",
&stringify_ui_token_amount_trimmed(&allowance),
);
} else {
println_name_value("Delegation:", "");
}
println_name_value(
"Close authority:",
&account.close_authority.as_ref().unwrap_or(&String::new()),
);
Ok(None)
}

fn main() {
let default_decimals = &format!("{}", native_mint::DECIMALS);
let matches = App::new(crate_name!())
Expand Down Expand Up @@ -876,6 +938,19 @@ fn main() {
.help("The address of the token account to unwrap"),
),
)
.subcommand(
SubCommand::with_name("account-info")
.about("Query details of an SPL Token account by address")
.arg(
Arg::with_name("address")
.validator(is_pubkey_or_keypair)
.value_name("TOKEN_ACCOUNT_ADDRESS")
.takes_value(true)
.index(1)
.required(true)
.help("The address of the SPL Token account to query"),
),
)
.get_matches();

let mut wallet_manager = None;
Expand Down Expand Up @@ -1024,6 +1099,10 @@ fn main() {
let token = pubkey_of(arg_matches, "token");
command_accounts(&config, token)
}
("account-info", Some(arg_matches)) => {
let address = pubkey_of(arg_matches, "address").unwrap();
command_account(&config, address)
}
_ => unreachable!(),
}
.and_then(|transaction| {
Expand Down