improve cli insufficient funds error messages

This commit is contained in:
Jack May 2021-03-02 11:13:41 -08:00 committed by Michael Vines
parent 00f2b039b4
commit b20bf8ebb0
3 changed files with 25 additions and 14 deletions

View File

@ -86,9 +86,13 @@ pub fn check_account_for_spend_multiple_fees_with_commitment(
return Err(CliError::InsufficientFundsForSpendAndFee( return Err(CliError::InsufficientFundsForSpendAndFee(
lamports_to_sol(balance), lamports_to_sol(balance),
lamports_to_sol(fee), lamports_to_sol(fee),
*account_pubkey,
)); ));
} else { } else {
return Err(CliError::InsufficientFundsForFee(lamports_to_sol(fee))); return Err(CliError::InsufficientFundsForFee(
lamports_to_sol(fee),
*account_pubkey,
));
} }
} }
Ok(()) Ok(())

View File

@ -369,25 +369,25 @@ pub struct CliCommandInfo {
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum CliError { pub enum CliError {
#[error("bad parameter: {0}")] #[error("Bad parameter: {0}")]
BadParameter(String), BadParameter(String),
#[error(transparent)] #[error(transparent)]
ClientError(#[from] ClientError), ClientError(#[from] ClientError),
#[error("command not recognized: {0}")] #[error("Command not recognized: {0}")]
CommandNotRecognized(String), CommandNotRecognized(String),
#[error("insufficient funds for fee ({0} SOL)")] #[error("Account {1} has insufficient funds for fee ({0} SOL)")]
InsufficientFundsForFee(f64), InsufficientFundsForFee(f64, Pubkey),
#[error("insufficient funds for spend ({0} SOL)")] #[error("Account {1} has insufficient funds for spend ({0} SOL)")]
InsufficientFundsForSpend(f64), InsufficientFundsForSpend(f64, Pubkey),
#[error("insufficient funds for spend ({0} SOL) and fee ({1} SOL)")] #[error("Account {2} has insufficient funds for spend ({0} SOL) + fee ({1} SOL)")]
InsufficientFundsForSpendAndFee(f64, f64), InsufficientFundsForSpendAndFee(f64, f64, Pubkey),
#[error(transparent)] #[error(transparent)]
InvalidNonce(nonce_utils::Error), InvalidNonce(nonce_utils::Error),
#[error("dynamic program error: {0}")] #[error("Dynamic program error: {0}")]
DynamicProgramError(String), DynamicProgramError(String),
#[error("rpc request error: {0}")] #[error("RPC request error: {0}")]
RpcRequestError(String), RpcRequestError(String),
#[error("keypair file not found: {0}")] #[error("Keypair file not found: {0}")]
KeypairFileNotFound(String), KeypairFileNotFound(String),
} }

View File

@ -107,15 +107,22 @@ where
return Err(CliError::InsufficientFundsForSpendAndFee( return Err(CliError::InsufficientFundsForSpendAndFee(
lamports_to_sol(spend), lamports_to_sol(spend),
lamports_to_sol(fee), lamports_to_sol(fee),
*from_pubkey,
)); ));
} }
} else { } else {
if from_balance < spend { if from_balance < spend {
return Err(CliError::InsufficientFundsForSpend(lamports_to_sol(spend))); return Err(CliError::InsufficientFundsForSpend(
lamports_to_sol(spend),
*from_pubkey,
));
} }
if !check_account_for_balance_with_commitment(rpc_client, fee_pubkey, fee, commitment)? if !check_account_for_balance_with_commitment(rpc_client, fee_pubkey, fee, commitment)?
{ {
return Err(CliError::InsufficientFundsForFee(lamports_to_sol(fee))); return Err(CliError::InsufficientFundsForFee(
lamports_to_sol(fee),
*fee_pubkey,
));
} }
} }
Ok((message, spend)) Ok((message, spend))