Skip to content

Commit

Permalink
Consolidate boltz_client Errors into one type
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Mar 26, 2024
1 parent 031b784 commit b95dae5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
20 changes: 3 additions & 17 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use boltz_client::network::Chain;
use boltz_client::error::Error;
use boltz_client::error::Error::*;
use boltz_client::network::Chain;
use lwk_signer::SwSigner;
use lwk_wollet::{ElectrumUrl, ElementsNetwork, WolletDescriptor};

Expand Down Expand Up @@ -65,15 +64,9 @@ pub struct SendPaymentResponse {

#[derive(thiserror::Error, Debug)]
pub enum SwapError {
#[error("Could not contact Boltz servers: {err}")]
ServersUnreachable { err: String },

#[error("Invoice amount is out of range")]
AmountOutOfRange,

#[error("Wrong response received from Boltz servers")]
BadResponse,

#[error("The specified invoice is not valid")]
InvalidInvoice,

Expand All @@ -95,15 +88,8 @@ pub enum SwapError {

impl From<Error> for SwapError {
fn from(err: Error) -> Self {
match err {
// TODO Better mapping
// TODO Since there is no more clear ServersUnreachable, do we need that type?
_ => SwapError::BoltzGeneric { err: format!("{err:?}") },
// boltz_client::util::error::ErrorKind::Network | boltz_client::util::error::ErrorKind::BoltzApi => {
// SwapError::ServersUnreachable { err: err.message }
// }
// boltz_client::util::error::ErrorKind::Input => SwapError::BadResponse,
// _ => SwapError::BoltzGeneric { err: err.message },
SwapError::BoltzGeneric {
err: format!("{err:?}"),
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ impl Wallet {
.map_err(|_| SwapError::InvalidInvoice)?;

// TODO Separate error type? Or make WalletError more generic?
let lbtc_pair = client.get_pairs()?.get_lbtc_pair().ok_or(SwapError::WalletError)?;
let lbtc_pair = client
.get_pairs()?
.get_lbtc_pair()
.ok_or(SwapError::WalletError)?;

let amount_sat = invoice
.amount_milli_satoshis()
Expand Down Expand Up @@ -241,7 +244,7 @@ impl Wallet {
absolute_fees: Option<u64>,
) -> Result<String, SwapError> {
let network_config = &self.get_network_config();
let mut rev_swap_tx = LBtcSwapTx::new_claim(
let rev_swap_tx = LBtcSwapTx::new_claim(
LBtcSwapScript::reverse_from_str(redeem_script, blinding_key)?,
self.address()
.map_err(|_| SwapError::WalletError)?
Expand Down Expand Up @@ -270,7 +273,10 @@ impl Wallet {
req: ReceivePaymentRequest,
) -> Result<SwapLbtcResponse, SwapError> {
let client = self.boltz_client();
let lbtc_pair = client.get_pairs()?.get_lbtc_pair().ok_or(SwapError::WalletError)?;
let lbtc_pair = client
.get_pairs()?
.get_lbtc_pair()
.ok_or(SwapError::WalletError)?;

let (onchain_amount_sat, invoice_amount_sat) =
match (req.onchain_amount_sat, req.invoice_amount_sat) {
Expand Down Expand Up @@ -418,7 +424,7 @@ impl Wallet {
let network_config = self.get_network_config();
debug!("{:?}", script.fetch_utxo(&network_config));

let mut tx =
let tx =
LBtcSwapTx::new_claim(script.clone(), self.address()?.to_string(), &network_config)
.expect("Expecting valid tx");
let keypair: Keypair = recovery.try_into().unwrap();
Expand Down

0 comments on commit b95dae5

Please sign in to comment.