Revert "Make UiTokenAmount::ui_amount a String (#15447)" (#15542)

This reverts commit d14374bc9f.
This commit is contained in:
Tyera Eulberg
2021-02-25 14:53:40 -07:00
committed by GitHub
parent d521dfe63c
commit 1ad2c9f741
11 changed files with 127 additions and 152 deletions

View File

@ -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, real_number_string, spl_token_v2_0_pubkey,
pubkey_from_spl_token_v2_0, spl_token_v2_0_pubkey, token_amount_to_ui_amount,
};
use solana_client::{
client_error::{ClientError, Result as ClientResult},
@ -103,14 +103,12 @@ pub enum Error {
ClientError(#[from] ClientError),
#[error("Missing lockup authority")]
MissingLockupAuthority,
#[error("insufficient funds in {0:?}, requires {1}")]
InsufficientFunds(FundingSources, String),
#[error("insufficient funds in {0:?}, requires {1} SOL")]
InsufficientFunds(FundingSources, f64),
#[error("Program error")]
ProgramError(#[from] ProgramError),
#[error("Exit signal received")]
ExitSignal,
#[error("Cannot support mint decimals that would overflow")]
InvalidMintDecimals,
}
fn merge_allocations(allocations: &[Allocation]) -> Vec<Allocation> {
@ -275,34 +273,33 @@ fn build_messages(
Some(allocation.lockup_date.parse::<DateTime<Utc>>().unwrap())
};
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 (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 instructions = distribution_instructions(
allocation,
&new_stake_account_keypair.pubkey(),
@ -722,7 +719,7 @@ fn check_payer_balances(
if staker_balance < undistributed_tokens {
return Err(Error::InsufficientFunds(
vec![FundingSource::StakeAccount].into(),
lamports_to_sol(undistributed_tokens).to_string(),
lamports_to_sol(undistributed_tokens),
));
}
if args.fee_payer.pubkey() == unlocked_sol_source {
@ -730,7 +727,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).to_string(),
lamports_to_sol(fees + total_unlocked_sol),
));
}
} else {
@ -738,14 +735,14 @@ fn check_payer_balances(
if fee_payer_balance < fees {
return Err(Error::InsufficientFunds(
vec![FundingSource::FeePayer].into(),
lamports_to_sol(fees).to_string(),
lamports_to_sol(fees),
));
}
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).to_string(),
lamports_to_sol(total_unlocked_sol),
));
}
}
@ -754,7 +751,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).to_string(),
lamports_to_sol(fees + undistributed_tokens),
));
}
} else {
@ -762,14 +759,14 @@ fn check_payer_balances(
if fee_payer_balance < fees {
return Err(Error::InsufficientFunds(
vec![FundingSource::FeePayer].into(),
lamports_to_sol(fees).to_string(),
lamports_to_sol(fees),
));
}
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).to_string(),
lamports_to_sol(undistributed_tokens),
));
}
}
@ -1418,7 +1415,7 @@ mod tests {
sources,
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
);
assert_eq!(amount, (allocation_amount + fees_in_sol).to_string());
assert!((amount - (allocation_amount + fees_in_sol)).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1455,7 +1452,7 @@ mod tests {
sources,
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
);
assert_eq!(amount, (allocation_amount + fees_in_sol).to_string());
assert!((amount - (allocation_amount + fees_in_sol)).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1511,7 +1508,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_eq!(amount, allocation_amount.to_string());
assert!((amount - allocation_amount).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1525,7 +1522,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_eq!(amount, fees_in_sol.to_string());
assert!((amount - fees_in_sol).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1612,10 +1609,7 @@ 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_eq!(
amount,
(expensive_allocation_amount - unlocked_sol).to_string()
);
assert!((amount - (expensive_allocation_amount - unlocked_sol)).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1637,7 +1631,7 @@ mod tests {
sources,
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
);
assert_eq!(amount, (unlocked_sol + fees_in_sol).to_string());
assert!((amount - (unlocked_sol + fees_in_sol)).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1674,7 +1668,7 @@ mod tests {
sources,
vec![FundingSource::SystemAccount, FundingSource::FeePayer].into()
);
assert_eq!(amount, (unlocked_sol + fees_in_sol).to_string());
assert!((amount - (unlocked_sol + fees_in_sol)).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1737,7 +1731,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_eq!(amount, unlocked_sol.to_string());
assert!((amount - unlocked_sol).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}
@ -1751,7 +1745,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_eq!(amount, fees_in_sol.to_string());
assert!((amount - fees_in_sol).abs() < f64::EPSILON);
} else {
panic!("check_payer_balances should have errored");
}

View File

@ -4,8 +4,7 @@ use crate::{
};
use console::style;
use solana_account_decoder::parse_token::{
pubkey_from_spl_token_v2_0, real_number_string, spl_token_v2_0_pubkey,
token_amount_to_ui_amount,
pubkey_from_spl_token_v2_0, spl_token_v2_0_pubkey, token_amount_to_ui_amount,
};
use solana_client::rpc_client::RpcClient;
use solana_sdk::{instruction::Instruction, native_token::lamports_to_sol};
@ -110,7 +109,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).to_string(),
lamports_to_sol(fees + account_creation_amount),
));
}
let source_token_account = client
@ -143,12 +142,20 @@ pub fn print_token_balances(
let (actual, difference) = if let Ok(recipient_token) =
SplTokenAccount::unpack(&recipient_account.data)
{
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);
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;
(
style(format!("{:>24}", actual_ui_amount)),
format!("{:>24}", delta_string),
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
),
)
} else {
(
@ -157,11 +164,12 @@ pub fn print_token_balances(
)
};
println!(
"{:<44} {:>24} {:>24} {:>24}",
"{:<44} {:>24.4$} {:>24} {:>24}",
allocation.recipient,
real_number_string(expected, spl_token_args.decimals),
token_amount_to_ui_amount(expected, spl_token_args.decimals).ui_amount,
actual,
difference,
spl_token_args.decimals as usize
);
Ok(())
}