From e866c3b076f023d8782fe32c1e1f08f5f2bdf040 Mon Sep 17 00:00:00 2001 From: yse <70684173+hydra-yse@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:00:41 +0100 Subject: [PATCH] fix: rename and fix structs visibility --- lib/src/model.rs | 42 ++++++++++++++++++++---------------------- lib/src/wallet.rs | 42 +++++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/lib/src/model.rs b/lib/src/model.rs index 8d9194c33..00d9d9528 100644 --- a/lib/src/model.rs +++ b/lib/src/model.rs @@ -2,6 +2,7 @@ use boltz_client::util::error::S5Error; use lwk_signer::SwSigner; use lwk_wollet::{ElectrumUrl, ElementsNetwork}; +#[derive(Debug)] pub enum Network { Liquid, LiquidTestnet, @@ -16,6 +17,7 @@ impl From for ElementsNetwork { } } +#[derive(Debug)] pub struct WalletOptions { pub signer: SwSigner, pub network: Network, @@ -29,28 +31,31 @@ pub struct WalletOptions { } #[derive(Debug)] -pub struct SwapLbtcResponse { - pub id: String, - pub invoice: String, +pub struct ReceivePaymentRequest { + pub invoice_amount_sat: Option, + pub onchain_amount_sat: Option, } -pub enum SwapStatus { - Created, - Mempool, - Completed, +#[derive(Debug)] +pub struct ReceivePaymentResponse { + pub id: String, + pub invoice: String, } -pub struct ReceivePaymentRequest { - pub invoice_amount_sat: Option, - pub onchain_amount_sat: Option, +#[derive(Debug)] +pub struct PreparePaymentResponse { + pub id: String, + pub funding_amount: u64, + pub funding_address: String, } +#[derive(Debug)] pub struct SendPaymentResponse { pub txid: String, } #[derive(thiserror::Error, Debug)] -pub enum SwapError { +pub enum PaymentError { #[error("Could not contact Boltz servers: {err}")] ServersUnreachable { err: String }, @@ -79,15 +84,15 @@ pub enum SwapError { BoltzGeneric { err: String }, } -impl From for SwapError { +impl From for PaymentError { fn from(err: S5Error) -> Self { match err.kind { boltz_client::util::error::ErrorKind::Network | boltz_client::util::error::ErrorKind::BoltzApi => { - SwapError::ServersUnreachable { err: err.message } + PaymentError::ServersUnreachable { err: err.message } } - boltz_client::util::error::ErrorKind::Input => SwapError::BadResponse, - _ => SwapError::BoltzGeneric { err: err.message }, + boltz_client::util::error::ErrorKind::Input => PaymentError::BadResponse, + _ => PaymentError::BoltzGeneric { err: err.message }, } } } @@ -152,10 +157,3 @@ impl From for Payment { } } } - -#[derive(Debug)] -pub struct PreparePaymentResponse { - pub id: String, - pub funding_amount: u64, - pub funding_address: String, -} diff --git a/lib/src/wallet.rs b/lib/src/wallet.rs index 75b937537..c2fbba62c 100644 --- a/lib/src/wallet.rs +++ b/lib/src/wallet.rs @@ -24,9 +24,9 @@ use lwk_wollet::{ }; use crate::{ - persist::Persister, Network, OngoingSwap, Payment, PaymentType, PreparePaymentResponse, - ReceivePaymentRequest, SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo, - WalletOptions, + persist::Persister, Network, OngoingSwap, Payment, PaymentError, PaymentType, + PreparePaymentResponse, ReceivePaymentRequest, ReceivePaymentResponse, SendPaymentResponse, + WalletInfo, WalletOptions, }; // To avoid sendrawtransaction error "min relay fee not met" @@ -213,24 +213,24 @@ impl Wallet { Ok(txid.to_string()) } - pub fn prepare_payment(&self, invoice: &str) -> Result { + pub fn prepare_payment(&self, invoice: &str) -> Result { let client = self.boltz_client(); let invoice = invoice .trim() .parse::() - .map_err(|_| SwapError::InvalidInvoice)?; + .map_err(|_| PaymentError::InvalidInvoice)?; let lbtc_pair = client.get_pairs()?.get_lbtc_pair()?; let amount_sat = invoice .amount_milli_satoshis() - .ok_or(SwapError::AmountOutOfRange)? + .ok_or(PaymentError::AmountOutOfRange)? / 1000; lbtc_pair .limits .within(amount_sat) - .map_err(|_| SwapError::AmountOutOfRange)?; + .map_err(|_| PaymentError::AmountOutOfRange)?; let swap_response = client.create_swap(CreateSwapRequest::new_lbtc_submarine( &lbtc_pair.hash, @@ -248,7 +248,7 @@ impl Wallet { amount_sat, funding_address: funding_address.clone(), }]) - .map_err(|_| SwapError::PersistError)?; + .map_err(|_| PaymentError::PersistError)?; Ok(PreparePaymentResponse { id, @@ -260,16 +260,16 @@ impl Wallet { pub fn send_payment( &self, res: &PreparePaymentResponse, - ) -> Result { + ) -> Result { let signer = AnySigner::Software(self.get_signer()); let txid = self .sign_and_send(&[signer], None, &res.funding_address, res.funding_amount) - .map_err(|_| SwapError::SendError)?; + .map_err(|_| PaymentError::SendError)?; self.swap_persister .resolve_ongoing_swap(&res.id) - .map_err(|_| SwapError::PersistError)?; + .map_err(|_| PaymentError::PersistError)?; Ok(SendPaymentResponse { txid }) } @@ -280,12 +280,12 @@ impl Wallet { redeem_script: &str, blinding_key: &str, absolute_fees: Option, - ) -> Result { + ) -> Result { let network_config = &self.get_network_config(); let mut rev_swap_tx = LBtcSwapTx::new_claim( LBtcSwapScript::reverse_from_str(redeem_script, blinding_key)?, self.address() - .map_err(|_| SwapError::WalletError)? + .map_err(|_| PaymentError::WalletError)? .to_string(), network_config, )?; @@ -309,11 +309,11 @@ impl Wallet { pub fn receive_payment( &self, req: ReceivePaymentRequest, - ) -> Result { + ) -> Result { let mut amount_sat = req .onchain_amount_sat .or(req.invoice_amount_sat) - .ok_or(SwapError::AmountOutOfRange)?; + .ok_or(PaymentError::AmountOutOfRange)?; let client = self.boltz_client(); @@ -322,7 +322,7 @@ impl Wallet { lbtc_pair .limits .within(amount_sat) - .map_err(|_| SwapError::AmountOutOfRange)?; + .map_err(|_| PaymentError::AmountOutOfRange)?; let mnemonic = self.signer.mnemonic(); let swap_key = @@ -330,7 +330,7 @@ impl Wallet { let lsk = LiquidSwapKey::from(swap_key); let preimage = Preimage::new(); - let preimage_str = preimage.to_string().ok_or(SwapError::InvalidPreimage)?; + let preimage_str = preimage.to_string().ok_or(PaymentError::InvalidPreimage)?; let preimage_hash = preimage.sha256.to_string(); let swap_response = if req.onchain_amount_sat.is_some() { @@ -358,12 +358,12 @@ impl Wallet { // Double check that the generated invoice includes our data // https://docs.boltz.exchange/v/api/dont-trust-verify#lightning-invoice-verification if invoice.payment_hash().to_string() != preimage_hash { - return Err(SwapError::InvalidInvoice); + return Err(PaymentError::InvalidInvoice); }; let invoice_amount_sat = invoice .amount_milli_satoshis() - .ok_or(SwapError::InvalidInvoice)? + .ok_or(PaymentError::InvalidInvoice)? / 1000; self.swap_persister @@ -380,9 +380,9 @@ impl Wallet { - CLAIM_ABSOLUTE_FEES ), }])) - .map_err(|_| SwapError::PersistError)?; + .map_err(|_| PaymentError::PersistError)?; - Ok(SwapLbtcResponse { + Ok(ReceivePaymentResponse { id: swap_id, invoice: invoice.to_string(), })