Skip to content

Commit

Permalink
Shift InsufficientFUnds error to WalletError &remove unused variant
Browse files Browse the repository at this point in the history
  • Loading branch information
KnowWhoami committed Oct 6, 2024
1 parent a4cd86b commit 7820445
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/maker/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
},
protocol::messages::TakerToMakerMessage,
utill::{monitor_log_for_completion, read_message, send_message, ConnectionType},
wallet::{FidelityError, WalletError},
wallet::WalletError,
};

use crate::maker::error::MakerError;
Expand Down Expand Up @@ -228,10 +228,10 @@ fn setup_fidelity_bond(maker: &Arc<Maker>, maker_address: &str) -> Result<(), Ma
// Wait for sufficient fund to create fidelity bond.
// Hard error if fidelity still can't be created.
Err(e) => {
if let WalletError::Fidelity(FidelityError::InsufficientFund {
if let WalletError::InsufficientFund {
available,
required,
}) = e
} = e
{
log::warn!("Insufficient fund to create fidelity bond.");
let amount = required - available;
Expand Down
11 changes: 4 additions & 7 deletions src/wallet/direct_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,10 @@ impl Wallet {

if let SendAmount::Amount(a) = send_amount {
if a + fee > total_input_value {
return Err(WalletError::Protocol(format!(

"Insufficient funds: Required (send_amount + fee): {} sats | Available: {} sats | Deficit: {} sats.",
(a + fee).to_sat(),
total_input_value.to_sat(),
(a + fee - total_input_value).to_sat(),
)));
return Err(WalletError::InsufficientFund {
available: total_input_value.to_sat(),
required: (a + fee).to_sat(),
});
}
}

Expand Down
1 change: 1 addition & 0 deletions src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum WalletError {
Locktime(bitcoin::blockdata::locktime::absolute::ConversionError),
Secp(bitcoin::secp256k1::Error),
Consensus(String),
InsufficientFund { available: u64, required: u64 },
}

impl From<std::io::Error> for WalletError {
Expand Down
20 changes: 7 additions & 13 deletions src/wallet/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use std::{
time::{Duration, SystemTime, UNIX_EPOCH},
};

use crate::{
protocol::messages::FidelityProof,
utill::redeemscript_to_scriptpubkey,
wallet::{UTXOSpendInfo, Wallet},
};
use bitcoin::{
absolute::LockTime,
bip32::{ChildNumber, DerivationPath},
Expand All @@ -19,12 +24,6 @@ use bitcoin::{
use bitcoind::bitcoincore_rpc::RpcApi;
use serde::{Deserialize, Serialize};

use crate::{
protocol::messages::FidelityProof,
utill::redeemscript_to_scriptpubkey,
wallet::{UTXOSpendInfo, Wallet},
};

use super::WalletError;

// To (strongly) disincentivize Sybil behavior, the value assessment of the bond
Expand All @@ -46,13 +45,9 @@ const FIDELITY_DERIVATION_PATH: &str = "m/84'/0'/0'/2";
#[derive(Debug)]
pub enum FidelityError {
WrongScriptType,
// TODO: Do we require this unused error variant?
BondAlreadyExists(u32),
BondDoesNotExist,
BondAlreadySpent,
CertExpired,
// TODO: Include this variant in WalletError instead.
InsufficientFund { available: u64, required: u64 },
General(String),
}

Expand Down Expand Up @@ -325,11 +320,10 @@ impl Wallet {
});

if total_input_amount < amount {
return Err((FidelityError::InsufficientFund {
return Err(WalletError::InsufficientFund {
available: total_input_amount.to_sat(),
required: amount.to_sat(),
})
.into());
});
}

let change_amount = total_input_amount.checked_sub(amount + fee);
Expand Down

0 comments on commit 7820445

Please sign in to comment.