Deprecate UiTokenAmount::ui_amount (#15616)
* Add TokenAmount::ui_amount_string * Fixup solana-tokens * Update docs
This commit is contained in:
@ -12,7 +12,7 @@ use indicatif::{ProgressBar, ProgressStyle};
|
||||
use pickledb::PickleDb;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_account_decoder::parse_token::{
|
||||
pubkey_from_spl_token_v2_0, spl_token_v2_0_pubkey, token_amount_to_ui_amount,
|
||||
pubkey_from_spl_token_v2_0, real_number_string, spl_token_v2_0_pubkey,
|
||||
};
|
||||
use solana_client::{
|
||||
client_error::{ClientError, Result as ClientResult},
|
||||
@ -103,8 +103,8 @@ pub enum Error {
|
||||
ClientError(#[from] ClientError),
|
||||
#[error("Missing lockup authority")]
|
||||
MissingLockupAuthority,
|
||||
#[error("insufficient funds in {0:?}, requires {1} SOL")]
|
||||
InsufficientFunds(FundingSources, f64),
|
||||
#[error("insufficient funds in {0:?}, requires {1}")]
|
||||
InsufficientFunds(FundingSources, String),
|
||||
#[error("Program error")]
|
||||
ProgramError(#[from] ProgramError),
|
||||
#[error("Exit signal received")]
|
||||
@ -273,33 +273,34 @@ fn build_messages(
|
||||
Some(allocation.lockup_date.parse::<DateTime<Utc>>().unwrap())
|
||||
};
|
||||
|
||||
let (display_amount, decimals, do_create_associated_token_account) =
|
||||
if let Some(spl_token_args) = &args.spl_token_args {
|
||||
let wallet_address = allocation.recipient.parse().unwrap();
|
||||
let associated_token_address = get_associated_token_address(
|
||||
&wallet_address,
|
||||
&spl_token_v2_0_pubkey(&spl_token_args.mint),
|
||||
);
|
||||
let do_create_associated_token_account =
|
||||
client.get_multiple_accounts(&[pubkey_from_spl_token_v2_0(
|
||||
&associated_token_address,
|
||||
)])?[0]
|
||||
.is_none();
|
||||
if do_create_associated_token_account {
|
||||
*created_accounts += 1;
|
||||
}
|
||||
(
|
||||
token_amount_to_ui_amount(allocation.amount, spl_token_args.decimals).ui_amount,
|
||||
spl_token_args.decimals as usize,
|
||||
do_create_associated_token_account,
|
||||
)
|
||||
} else {
|
||||
(lamports_to_sol(allocation.amount), 9, false)
|
||||
};
|
||||
println!(
|
||||
"{:<44} {:>24.2$}",
|
||||
allocation.recipient, display_amount, decimals
|
||||
);
|
||||
let do_create_associated_token_account = if let Some(spl_token_args) = &args.spl_token_args
|
||||
{
|
||||
let wallet_address = allocation.recipient.parse().unwrap();
|
||||
let associated_token_address = get_associated_token_address(
|
||||
&wallet_address,
|
||||
&spl_token_v2_0_pubkey(&spl_token_args.mint),
|
||||
);
|
||||
let do_create_associated_token_account = client
|
||||
.get_multiple_accounts(&[pubkey_from_spl_token_v2_0(&associated_token_address)])?
|
||||
[0]
|
||||
.is_none();
|
||||
if do_create_associated_token_account {
|
||||
*created_accounts += 1;
|
||||
}
|
||||
println!(
|
||||
"{:<44} {:>24}",
|
||||
allocation.recipient,
|
||||
real_number_string(allocation.amount, spl_token_args.decimals)
|
||||
);
|
||||
do_create_associated_token_account
|
||||
} else {
|
||||
println!(
|
||||
"{:<44} {:>24.9}",
|
||||
allocation.recipient,
|
||||
lamports_to_sol(allocation.amount)
|
||||
);
|
||||
false
|
||||
};
|
||||
let instructions = distribution_instructions(
|
||||
allocation,
|
||||
&new_stake_account_keypair.pubkey(),
|
||||
@ -719,7 +720,7 @@ fn check_payer_balances(
|
||||
if staker_balance < undistributed_tokens {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::StakeAccount].into(),
|
||||
lamports_to_sol(undistributed_tokens),
|
||||
lamports_to_sol(undistributed_tokens).to_string(),
|
||||
));
|
||||
}
|
||||
if args.fee_payer.pubkey() == unlocked_sol_source {
|
||||
@ -727,7 +728,7 @@ fn check_payer_balances(
|
||||
if balance < fees + total_unlocked_sol {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into(),
|
||||
lamports_to_sol(fees + total_unlocked_sol),
|
||||
lamports_to_sol(fees + total_unlocked_sol).to_string(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
@ -735,14 +736,14 @@ fn check_payer_balances(
|
||||
if fee_payer_balance < fees {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::FeePayer].into(),
|
||||
lamports_to_sol(fees),
|
||||
lamports_to_sol(fees).to_string(),
|
||||
));
|
||||
}
|
||||
let unlocked_sol_balance = client.get_balance(&unlocked_sol_source)?;
|
||||
if unlocked_sol_balance < total_unlocked_sol {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::SystemAccount].into(),
|
||||
lamports_to_sol(total_unlocked_sol),
|
||||
lamports_to_sol(total_unlocked_sol).to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -751,7 +752,7 @@ fn check_payer_balances(
|
||||
if balance < fees + undistributed_tokens {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into(),
|
||||
lamports_to_sol(fees + undistributed_tokens),
|
||||
lamports_to_sol(fees + undistributed_tokens).to_string(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
@ -759,14 +760,14 @@ fn check_payer_balances(
|
||||
if fee_payer_balance < fees {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::FeePayer].into(),
|
||||
lamports_to_sol(fees),
|
||||
lamports_to_sol(fees).to_string(),
|
||||
));
|
||||
}
|
||||
let sender_balance = client.get_balance(&distribution_source)?;
|
||||
if sender_balance < undistributed_tokens {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::SystemAccount].into(),
|
||||
lamports_to_sol(undistributed_tokens),
|
||||
lamports_to_sol(undistributed_tokens).to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -1415,7 +1416,7 @@ mod tests {
|
||||
sources,
|
||||
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
|
||||
);
|
||||
assert!((amount - (allocation_amount + fees_in_sol)).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, (allocation_amount + fees_in_sol).to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1452,7 +1453,7 @@ mod tests {
|
||||
sources,
|
||||
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
|
||||
);
|
||||
assert!((amount - (allocation_amount + fees_in_sol)).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, (allocation_amount + fees_in_sol).to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1508,7 +1509,7 @@ mod tests {
|
||||
let err_result = check_payer_balances(1, &allocations, &client, &args).unwrap_err();
|
||||
if let Error::InsufficientFunds(sources, amount) = err_result {
|
||||
assert_eq!(sources, vec![FundingSource::SystemAccount].into());
|
||||
assert!((amount - allocation_amount).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, allocation_amount.to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1522,7 +1523,7 @@ mod tests {
|
||||
let err_result = check_payer_balances(1, &allocations, &client, &args).unwrap_err();
|
||||
if let Error::InsufficientFunds(sources, amount) = err_result {
|
||||
assert_eq!(sources, vec![FundingSource::FeePayer].into());
|
||||
assert!((amount - fees_in_sol).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, fees_in_sol.to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1609,7 +1610,10 @@ mod tests {
|
||||
check_payer_balances(1, &expensive_allocations, &client, &args).unwrap_err();
|
||||
if let Error::InsufficientFunds(sources, amount) = err_result {
|
||||
assert_eq!(sources, vec![FundingSource::StakeAccount].into());
|
||||
assert!((amount - (expensive_allocation_amount - unlocked_sol)).abs() < f64::EPSILON);
|
||||
assert_eq!(
|
||||
amount,
|
||||
(expensive_allocation_amount - unlocked_sol).to_string()
|
||||
);
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1631,7 +1635,7 @@ mod tests {
|
||||
sources,
|
||||
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
|
||||
);
|
||||
assert!((amount - (unlocked_sol + fees_in_sol)).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, (unlocked_sol + fees_in_sol).to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1668,7 +1672,7 @@ mod tests {
|
||||
sources,
|
||||
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
|
||||
);
|
||||
assert!((amount - (unlocked_sol + fees_in_sol)).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, (unlocked_sol + fees_in_sol).to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1731,7 +1735,7 @@ mod tests {
|
||||
let err_result = check_payer_balances(1, &allocations, &client, &args).unwrap_err();
|
||||
if let Error::InsufficientFunds(sources, amount) = err_result {
|
||||
assert_eq!(sources, vec![FundingSource::SystemAccount].into());
|
||||
assert!((amount - unlocked_sol).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, unlocked_sol.to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
@ -1745,7 +1749,7 @@ mod tests {
|
||||
let err_result = check_payer_balances(1, &allocations, &client, &args).unwrap_err();
|
||||
if let Error::InsufficientFunds(sources, amount) = err_result {
|
||||
assert_eq!(sources, vec![FundingSource::FeePayer].into());
|
||||
assert!((amount - fees_in_sol).abs() < f64::EPSILON);
|
||||
assert_eq!(amount, fees_in_sol.to_string());
|
||||
} else {
|
||||
panic!("check_payer_balances should have errored");
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ use crate::{
|
||||
};
|
||||
use console::style;
|
||||
use solana_account_decoder::parse_token::{
|
||||
pubkey_from_spl_token_v2_0, spl_token_v2_0_pubkey, token_amount_to_ui_amount,
|
||||
pubkey_from_spl_token_v2_0, real_number_string, real_number_string_trimmed,
|
||||
spl_token_v2_0_pubkey,
|
||||
};
|
||||
use solana_client::rpc_client::RpcClient;
|
||||
use solana_sdk::{instruction::Instruction, native_token::lamports_to_sol};
|
||||
@ -109,7 +110,7 @@ pub fn check_spl_token_balances(
|
||||
if fee_payer_balance < fees + account_creation_amount {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::FeePayer].into(),
|
||||
lamports_to_sol(fees + account_creation_amount),
|
||||
lamports_to_sol(fees + account_creation_amount).to_string(),
|
||||
));
|
||||
}
|
||||
let source_token_account = client
|
||||
@ -119,7 +120,7 @@ pub fn check_spl_token_balances(
|
||||
if source_token.amount < allocation_amount {
|
||||
return Err(Error::InsufficientFunds(
|
||||
vec![FundingSource::SplTokenAccount].into(),
|
||||
token_amount_to_ui_amount(allocation_amount, spl_token_args.decimals).ui_amount,
|
||||
real_number_string_trimmed(allocation_amount, spl_token_args.decimals),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
@ -142,20 +143,12 @@ pub fn print_token_balances(
|
||||
let (actual, difference) = if let Ok(recipient_token) =
|
||||
SplTokenAccount::unpack(&recipient_account.data)
|
||||
{
|
||||
let actual_ui_amount =
|
||||
token_amount_to_ui_amount(recipient_token.amount, spl_token_args.decimals).ui_amount;
|
||||
let expected_ui_amount =
|
||||
token_amount_to_ui_amount(expected, spl_token_args.decimals).ui_amount;
|
||||
let actual_ui_amount = real_number_string(recipient_token.amount, spl_token_args.decimals);
|
||||
let delta_string =
|
||||
real_number_string(recipient_token.amount - expected, spl_token_args.decimals);
|
||||
(
|
||||
style(format!(
|
||||
"{:>24.1$}",
|
||||
actual_ui_amount, spl_token_args.decimals as usize
|
||||
)),
|
||||
format!(
|
||||
"{:>24.1$}",
|
||||
actual_ui_amount - expected_ui_amount,
|
||||
spl_token_args.decimals as usize
|
||||
),
|
||||
style(format!("{:>24}", actual_ui_amount)),
|
||||
format!("{:>24}", delta_string),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
@ -164,12 +157,11 @@ pub fn print_token_balances(
|
||||
)
|
||||
};
|
||||
println!(
|
||||
"{:<44} {:>24.4$} {:>24} {:>24}",
|
||||
"{:<44} {:>24} {:>24} {:>24}",
|
||||
allocation.recipient,
|
||||
token_amount_to_ui_amount(expected, spl_token_args.decimals).ui_amount,
|
||||
real_number_string(expected, spl_token_args.decimals),
|
||||
actual,
|
||||
difference,
|
||||
spl_token_args.decimals as usize
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use solana_account_decoder::parse_token::token_amount_to_ui_amount;
|
||||
use solana_account_decoder::parse_token::real_number_string_trimmed;
|
||||
use solana_sdk::native_token::lamports_to_sol;
|
||||
use std::{
|
||||
fmt::{Debug, Display, Formatter, Result},
|
||||
@ -27,7 +27,7 @@ impl Token {
|
||||
write!(f, "{}{}", SOL_SYMBOL, amount)
|
||||
}
|
||||
TokenType::SplToken => {
|
||||
let amount = token_amount_to_ui_amount(self.amount, self.decimals).ui_amount;
|
||||
let amount = real_number_string_trimmed(self.amount, self.decimals);
|
||||
write!(f, "{} tokens", amount)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user