Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Aug 5, 2020
1 parent 716613e commit b7e89d8
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions token-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ fn get_decimals_for_token(config: &Config, token: &Pubkey) -> Result<u8, Error>
}
}

fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
(ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
}

fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
amount as f64 / 10_usize.pow(decimals as u32) as f64
}

fn command_create_token(config: &Config, decimals: u8) -> CommmandResult {
let token = Keypair::new();
println!("Creating token {}", token.pubkey());
Expand Down Expand Up @@ -232,7 +224,7 @@ fn command_transfer(
exit(1)
}
};
let amount = ui_amount_to_amount(ui_amount, decimals);
let amount = spl_token::ui_amount_to_amount(ui_amount, decimals);

let mut transaction = Transaction::new_with_payer(
&[transfer(
Expand Down Expand Up @@ -272,7 +264,7 @@ fn command_burn(config: &Config, source: Pubkey, ui_amount: f64) -> CommmandResu
exit(1)
}
};
let amount = ui_amount_to_amount(ui_amount, decimals);
let amount = spl_token::ui_amount_to_amount(ui_amount, decimals);

let mut transaction = Transaction::new_with_payer(
&[burn(
Expand Down Expand Up @@ -319,7 +311,7 @@ fn command_mint(
exit(1)
}
};
let amount = ui_amount_to_amount(ui_amount, decimals);
let amount = spl_token::ui_amount_to_amount(ui_amount, decimals);

let mut transaction = Transaction::new_with_payer(
&[mint_to(
Expand Down Expand Up @@ -408,17 +400,7 @@ fn command_balance(config: &Config, address: Pubkey) -> CommmandResult {
.rpc_client
.get_token_account_balance_with_commitment(&address, config.commitment_config)?
.value;

let mint = config
.rpc_client
.get_token_account_with_commitment(&address, config.commitment_config)?
.value
.ok_or_else(|| format!("Failed to fetch token account"))?
.mint
.parse::<Pubkey>()?;

let decimals = get_decimals_for_token(config, &mint)?;
println!("{}", amount_to_ui_amount(dbg!(balance), dbg!(decimals)));
println!("{}", balance.ui_amount);
Ok(None)
}

Expand All @@ -428,8 +410,7 @@ fn command_supply(config: &Config, address: Pubkey) -> CommmandResult {
.get_token_supply_with_commitment(&address, config.commitment_config)?
.value;

let decimals = get_decimals_for_token(config, &address)?;
println!("{}", amount_to_ui_amount(supply, decimals));
println!("{}", supply.ui_amount);
Ok(None)
}

Expand All @@ -452,13 +433,15 @@ fn command_accounts(config: &Config, token: Option<Pubkey>) -> CommmandResult {
println!("Account Token Balance");
println!("-------------------------------------------------------------------------------------------------");
for (address, account) in accounts {
let decimals = get_decimals_for_token(config, &account.mint.parse::<Pubkey>()?)?;
println!(
"{:<44} {:<44} {}",
address,
account.mint,
amount_to_ui_amount(account.amount, decimals)
);
let balance = match config
.rpc_client
.get_token_account_balance_with_commitment(&address, config.commitment_config)
{
Ok(response) => response.value.ui_amount.to_string(),
Err(err) => format!("{}", err),
};

println!("{:<44} {:<44} {}", address, account.mint, balance);
}
Ok(None)
}
Expand Down

0 comments on commit b7e89d8

Please sign in to comment.