error instead of panic on invalid balance

This commit is contained in:
evan-schott 2024-06-25 13:40:56 -07:00
parent b9667ef190
commit f25ce4cbcc
2 changed files with 13 additions and 3 deletions

View File

@ -306,5 +306,11 @@ create_messages!(
msg: "Failed to confirm transaction".to_string(),
help: None,
}
@backtraced
invalid_balance {
args: (account: impl Display),
msg: format!("Invalid public balance for account: {account}"),
help: Some("Make sure the account has enough balance to pay for the deployment.".to_string()),
}
);

View File

@ -261,8 +261,12 @@ fn check_balance<N: Network>(
.execute(Context::new(context.path.clone(), context.home.clone(), true)?)?;
// Remove the last 3 characters since they represent the `u64` suffix.
public_balance.truncate(public_balance.len() - 3);
// This unwrap is safe as the Query will error if it cannot retrieve a public balance, and if it returns a balance that will be a u64.
let balance = public_balance.parse::<u64>().unwrap();
// Make sure the balance is valid.
let balance = if let Ok(credits) = public_balance.parse::<u64>() {
credits
} else {
return Err(CliError::invalid_balance(address).into());
};
// Compare balance.
if balance < total_cost {
Err(PackageError::insufficient_balance(address, public_balance, total_cost).into())