From 3fb13c720e37763c5d46fda56984c4c4c7521573 Mon Sep 17 00:00:00 2001 From: yse Date: Tue, 26 Mar 2024 01:43:24 +0100 Subject: [PATCH 1/3] feat: added OngoingSwap --- lib/src/lib.rs | 3 +- lib/src/model.rs | 59 +++++++++---- lib/src/persist/migrations.rs | 11 ++- lib/src/persist/mod.rs | 157 +++++++++++++++++++++++++--------- lib/src/wallet.rs | 64 ++++++++------ 5 files changed, 211 insertions(+), 83 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 887c21707..5852a74c9 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -62,7 +62,8 @@ mod tests { println!("Please paste the invoice to be paid: "); io::stdin().read_line(&mut invoice)?; - breez_wallet.send_payment(&invoice)?; + let prepare_response = breez_wallet.prepare_payment(&invoice)?; + breez_wallet.send_payment(&prepare_response)?; Ok(()) } diff --git a/lib/src/model.rs b/lib/src/model.rs index e8cc94027..d29b6a38f 100644 --- a/lib/src/model.rs +++ b/lib/src/model.rs @@ -102,22 +102,20 @@ pub struct WalletInfo { } #[derive(Debug)] -pub struct OngoingReceiveSwap { - pub id: String, - pub preimage: String, - pub redeem_script: String, - pub blinding_key: String, - pub invoice_amount_sat: u64, - pub onchain_amount_sat: u64, -} - -pub struct OngoingSendSwap { - pub id: String, - // pub preimage: String, - // pub redeem_script: String, - // pub blinding_key: String, - // pub invoice_amount_sat: Option, - // pub onchain_amount_sat: Option, +pub(crate) enum OngoingSwap { + Send { + id: String, + amount_sat: u64, + funding_address: String, + }, + Receive { + id: String, + preimage: String, + redeem_script: String, + blinding_key: String, + invoice_amount_sat: u64, + onchain_amount_sat: u64, + }, } #[derive(Debug)] @@ -125,6 +123,7 @@ pub enum PaymentType { Sent, Received, PendingReceive, + PendingSend, } #[derive(Debug)] @@ -134,3 +133,31 @@ pub struct Payment { pub amount_sat: u64, pub payment_type: PaymentType, } + +impl From for Payment { + fn from(swap: OngoingSwap) -> Self { + match swap { + OngoingSwap::Send { amount_sat, .. } => Payment { + id: None, + timestamp: None, + payment_type: PaymentType::PendingSend, + amount_sat, + }, + OngoingSwap::Receive { + onchain_amount_sat, .. + } => Payment { + id: None, + timestamp: None, + payment_type: PaymentType::PendingReceive, + amount_sat: onchain_amount_sat, + }, + } + } +} + +#[derive(Debug)] +pub struct PreparePaymentResponse { + pub id: String, + pub funding_amount: u64, + pub funding_address: String, +} diff --git a/lib/src/persist/migrations.rs b/lib/src/persist/migrations.rs index 3e95fa076..a64d3f642 100644 --- a/lib/src/persist/migrations.rs +++ b/lib/src/persist/migrations.rs @@ -1,12 +1,19 @@ pub(crate) fn current_migrations() -> Vec<&'static str> { vec![ - "CREATE TABLE IF NOT EXISTS ongoing_swaps ( + "CREATE TABLE IF NOT EXISTS ongoing_receive_swaps ( id TEXT NOT NULL PRIMARY KEY, preimage TEXT NOT NULL, redeem_script TEXT NOT NULL, blinding_key TEXT NOT NULL, invoice_amount_sat INTEGER NOT NULL, - onchain_amount_sat INTEGER NOT NULL + onchain_amount_sat INTEGER NOT NULL, + created_at TEXT DEFAULT CURRENT_TIMESTAMP + ) STRICT;", + "CREATE TABLE IF NOT EXISTS ongoing_send_swaps ( + id TEXT NOT NULL PRIMARY KEY, + amount_sat INTEGER NOT NULL, + funding_address TEXT NOT NULL, + created_at TEXT DEFAULT CURRENT_TIMESTAMP ) STRICT;", ] } diff --git a/lib/src/persist/mod.rs b/lib/src/persist/mod.rs index 54d60d70d..019b2424c 100644 --- a/lib/src/persist/mod.rs +++ b/lib/src/persist/mod.rs @@ -1,10 +1,10 @@ mod migrations; use anyhow::Result; -use rusqlite::{params, Connection, Row}; +use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; -use crate::OngoingReceiveSwap; +use crate::OngoingSwap; use migrations::current_migrations; @@ -13,7 +13,7 @@ pub(crate) struct Persister { } impl Persister { - pub fn new(working_dir: String) -> Self { + pub fn new(working_dir: &str) -> Self { let main_db_file = format!("{}/storage.sql", working_dir); Persister { main_db_file } } @@ -35,67 +35,144 @@ impl Persister { Ok(()) } - pub fn insert_ongoing_swaps(&self, swaps: &[OngoingReceiveSwap]) -> Result<()> { + pub fn insert_ongoing_swap(&self, swaps: &[OngoingSwap]) -> Result<()> { let con = self.get_connection()?; - let mut stmt = con.prepare( - " - INSERT INTO ongoing_swaps ( + for swap in swaps { + match swap { + OngoingSwap::Send { + id, + funding_address, + amount_sat, + } => { + let mut stmt = con.prepare( + " + INSERT INTO ongoing_send_swaps ( + id, + amount_sat, + funding_address + ) + VALUES (?, ?, ?) + ", + )?; + + _ = stmt.execute((&id, &amount_sat, &funding_address))? + } + OngoingSwap::Receive { id, preimage, redeem_script, blinding_key, invoice_amount_sat, - onchain_amount_sat - ) - VALUES (?, ?, ?, ?, ?, ?) - ", - )?; - - for swap in swaps { - _ = stmt.execute(( - &swap.id, - &swap.preimage, - &swap.redeem_script, - &swap.blinding_key, - &swap.invoice_amount_sat, - &swap.onchain_amount_sat, - ))? + onchain_amount_sat, + } => { + let mut stmt = con.prepare( + " + INSERT INTO ongoing_receive_swaps ( + id, + preimage, + redeem_script, + blinding_key, + invoice_amount_sat, + onchain_amount_sat + ) + VALUES (?, ?, ?, ?, ?, ?) + ", + )?; + + _ = stmt.execute(( + &id, + &preimage, + &redeem_script, + &blinding_key, + &invoice_amount_sat, + &onchain_amount_sat, + ))? + } + } } Ok(()) } - pub fn resolve_ongoing_swap(&self, id: String) -> Result<()> { - let con = self.get_connection()?; + pub fn resolve_ongoing_swap(&self, id: &str) -> Result<()> { + let mut con = self.get_connection()?; - con.prepare("DELETE FROM ongoing_swaps WHERE id = ?")? - .execute(params![id])?; + let tx = con.transaction()?; + tx.execute("DELETE FROM ongoing_send_swaps WHERE id = ?", params![id])?; + tx.execute( + "DELETE FROM ongoing_receive_swaps WHERE id = ?", + params![id], + )?; + tx.commit()?; Ok(()) } - pub fn list_ongoing_swaps(&self) -> Result> { + pub fn list_ongoing_swaps(&self) -> Result> { let con = self.get_connection()?; + let mut ongoing_swaps = self.list_ongoing_send(&con)?; + ongoing_swaps.append(&mut self.list_ongoing_receive(&con)?); + Ok(ongoing_swaps) + } - let mut stmt = con.prepare("SELECT * FROM ongoing_swaps")?; + fn list_ongoing_send(&self, con: &Connection) -> Result, rusqlite::Error> { + let mut stmt = con.prepare( + " + SELECT + id, + amount_sat, + funding_address, + created_at + FROM ongoing_send_swaps + ORDER BY created_at + ", + )?; - let swaps: Vec = stmt - .query_map(params![], |row| self.sql_row_to_swap(row))? + let ongoing_send = stmt + .query_map(params![], |row| { + Ok(OngoingSwap::Send { + id: row.get(0)?, + amount_sat: row.get(1)?, + funding_address: row.get(2)?, + }) + })? .map(|i| i.unwrap()) .collect(); - Ok(swaps) + Ok(ongoing_send) } - fn sql_row_to_swap(&self, row: &Row) -> Result { - Ok(OngoingReceiveSwap { - id: row.get(0)?, - preimage: row.get(1)?, - redeem_script: row.get(2)?, - blinding_key: row.get(3)?, - invoice_amount_sat: row.get(4)?, - onchain_amount_sat: row.get(5)?, - }) + fn list_ongoing_receive(&self, con: &Connection) -> Result, rusqlite::Error> { + let mut stmt = con.prepare( + " + SELECT + id, + preimage, + redeem_script, + blinding_key, + invoice_amount_sat, + onchain_amount_sat, + created_at + FROM ongoing_receive_swaps + ORDER BY created_at + ", + )?; + + let ongoing_receive = stmt + .query_map(params![], |row| { + Ok(OngoingSwap::Receive { + id: row.get(0)?, + preimage: row.get(1)?, + redeem_script: row.get(2)?, + blinding_key: row.get(3)?, + invoice_amount_sat: row.get(4)?, + onchain_amount_sat: row.get(5)?, + }) + })? + .map(|i| i.unwrap()) + .collect(); + + Ok(ongoing_receive) } } diff --git a/lib/src/wallet.rs b/lib/src/wallet.rs index a5c7ce154..1d069e20d 100644 --- a/lib/src/wallet.rs +++ b/lib/src/wallet.rs @@ -24,7 +24,7 @@ use lwk_wollet::{ }; use crate::{ - ensure_sdk, persist::Persister, Network, OngoingReceiveSwap, Payment, PaymentType, + ensure_sdk, persist::Persister, Network, OngoingSwap, Payment, PaymentType, ReceivePaymentRequest, SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo, WalletOptions, CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL, }; @@ -77,7 +77,7 @@ impl Wallet { let persister_path = opts.data_dir_path.unwrap_or(DEFAULT_DATA_DIR.to_string()); fs::create_dir_all(&persister_path)?; - let swap_persister = Persister::new(persister_path); + let swap_persister = Persister::new(&persister_path); swap_persister.init()?; let wallet = Arc::new(Wallet { @@ -104,15 +104,18 @@ impl Wallet { thread::scope(|scope| { for swap in ongoing_swaps { scope.spawn(|| { - let OngoingReceiveSwap { + if let OngoingSwap::Receive { + id, preimage, redeem_script, blinding_key, .. - } = swap; - match cloned.try_claim(&preimage, &redeem_script, &blinding_key, None) { - Ok(_) => cloned.swap_persister.resolve_ongoing_swap(swap.id).unwrap(), - Err(e) => warn!("Could not claim yet. Err: {e}"), + } = swap + { + match cloned.try_claim(&preimage, &redeem_script, &blinding_key, None) { + Ok(_) => cloned.swap_persister.resolve_ongoing_swap(&id).unwrap(), + Err(e) => warn!("Could not claim yet. Err: {}", e), + } } }); } @@ -194,7 +197,7 @@ impl Wallet { Ok(txid.to_string()) } - pub fn send_payment(&self, invoice: &str) -> Result { + pub fn prepare_payment(&self, invoice: &str) -> Result { let client = self.boltz_client(); let invoice = invoice .trim() @@ -223,15 +226,38 @@ impl Wallet { "", ))?; + let id = swap_response.get_id(); let funding_amount = swap_response.get_funding_amount()?; + let funding_address = swap_response.get_funding_address()?; - let funding_addr = swap_response.get_funding_address()?; + self.swap_persister + .insert_ongoing_swap(&[OngoingSwap::Send { + id: id.clone(), + amount_sat, + funding_address: funding_address.clone(), + }]) + .map_err(|_| SwapError::PersistError)?; + + Ok(PreparePaymentResponse { + id, + funding_address, + funding_amount, + }) + } + pub fn send_payment( + &self, + res: &PreparePaymentResponse, + ) -> Result { let signer = AnySigner::Software(self.get_signer()); let txid = self - .sign_and_send(&[signer], None, &funding_addr, funding_amount) - .map_err(|e| SwapError::SendError { err: e.to_string() })?; + .sign_and_send(&[signer], None, &res.funding_address, res.funding_amount) + .map_err(|_| SwapError::SendError)?; + + self.swap_persister + .resolve_ongoing_swap(&res.id) + .map_err(|_| SwapError::PersistError)?; Ok(SendPaymentResponse { txid }) } @@ -360,7 +386,7 @@ impl Wallet { / 1000; self.swap_persister - .insert_ongoing_swaps(dbg!(&[OngoingReceiveSwap { + .insert_ongoing_swap(dbg!(&[OngoingSwap::Receive { id: swap_id.clone(), preimage: preimage_str, blinding_key: blinding_str, @@ -401,18 +427,8 @@ impl Wallet { .collect(); if include_pending { - let pending_swaps = self.swap_persister.list_ongoing_swaps()?; - - for swap in pending_swaps { - payments.insert( - 0, - Payment { - id: None, - timestamp: None, - payment_type: PaymentType::PendingReceive, - amount_sat: swap.invoice_amount_sat, - }, - ); + for swap in self.swap_persister.list_ongoing_swaps()? { + payments.insert(0, swap.into()); } } From 9619c834bb22f129c72cc311461c7b1e21a5b728 Mon Sep 17 00:00:00 2001 From: yse Date: Tue, 26 Mar 2024 01:43:35 +0100 Subject: [PATCH 2/3] feat: reflect changes in cli --- cli/src/commands.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cli/src/commands.rs b/cli/src/commands.rs index 835b30f34..667ee1a5a 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -8,7 +8,7 @@ use rustyline::history::DefaultHistory; use rustyline::Editor; use rustyline::{hint::HistoryHinter, Completer, Helper, Hinter, Validator}; -use breez_sdk_liquid::{ReceivePaymentRequest, Wallet}; +use breez_sdk_liquid::{ReceivePaymentRequest, SendPaymentResponse, Wallet}; #[derive(Parser, Debug, Clone, PartialEq)] pub(crate) enum Command { @@ -61,13 +61,14 @@ pub(crate) fn handle_command( )) } Command::SendPayment { bolt11 } => { - let response = wallet.send_payment(&bolt11)?; + let prepare_response = wallet.prepare_payment(&bolt11)?; + let SendPaymentResponse { txid } = wallet.send_payment(&prepare_response)?; Ok(format!( r#" Successfully paid the invoice! You can view the onchain transaction at https://blockstream.info/liquidtestnet/tx/{}"#, - response.txid + txid )) } Command::GetInfo => { From 56dfb7cd7a19dc21b9c06b8da2fadf3d8d8020c7 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 3/3] fix: rename and fix structs visibility --- lib/src/lib.rs | 8 ++++--- lib/src/model.rs | 39 ++++++++++++++---------------- lib/src/wallet.rs | 61 +++++++++++++++++++++++++---------------------- 3 files changed, 56 insertions(+), 52 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 5852a74c9..db51d1652 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -26,7 +26,7 @@ mod tests { use anyhow::Result; use bip39::{Language, Mnemonic}; - use crate::{ReceivePaymentRequest, Wallet, DEFAULT_DATA_DIR}; + use crate::{Network, ReceivePaymentRequest, Wallet, DEFAULT_DATA_DIR}; const PHRASE_FILE_NAME: &str = "phrase"; @@ -56,7 +56,8 @@ mod tests { #[test] fn normal_submarine_swap() -> Result<()> { - let breez_wallet = Wallet::init(get_mnemonic()?.to_string())?; + let breez_wallet = + Wallet::init(&get_mnemonic()?.to_string(), None, Network::LiquidTestnet)?; let mut invoice = String::new(); println!("Please paste the invoice to be paid: "); @@ -70,7 +71,8 @@ mod tests { #[test] fn reverse_submarine_swap_success() -> Result<()> { - let breez_wallet = Wallet::init(get_mnemonic()?.to_string())?; + let breez_wallet = + Wallet::init(&get_mnemonic()?.to_string(), None, Network::LiquidTestnet)?; let swap_response = breez_wallet.receive_payment(ReceivePaymentRequest { onchain_amount_sat: Some(1000), diff --git a/lib/src/model.rs b/lib/src/model.rs index d29b6a38f..570fc50a0 100644 --- a/lib/src/model.rs +++ b/lib/src/model.rs @@ -3,7 +3,7 @@ use boltz_client::network::Chain; use lwk_signer::SwSigner; use lwk_wollet::{ElectrumUrl, ElementsNetwork, WolletDescriptor}; -#[derive(Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub enum Network { Liquid, LiquidTestnet, @@ -27,6 +27,7 @@ impl From for Chain { } } +#[derive(Debug)] pub struct WalletOptions { pub signer: SwSigner, pub network: Network, @@ -42,28 +43,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("Invoice amount is out of range")] AmountOutOfRange, @@ -86,9 +90,9 @@ pub enum SwapError { BoltzGeneric { err: String }, } -impl From for SwapError { +impl From for PaymentError { fn from(err: Error) -> Self { - SwapError::BoltzGeneric { + PaymentError::BoltzGeneric { err: format!("{err:?}"), } } @@ -154,10 +158,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 1d069e20d..d24854b58 100644 --- a/lib/src/wallet.rs +++ b/lib/src/wallet.rs @@ -24,9 +24,9 @@ use lwk_wollet::{ }; use crate::{ - ensure_sdk, persist::Persister, Network, OngoingSwap, Payment, PaymentType, - ReceivePaymentRequest, SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo, - WalletOptions, CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL, + ensure_sdk, persist::Persister, Network, OngoingSwap, Payment, PaymentError, PaymentType, + PreparePaymentResponse, ReceivePaymentRequest, ReceivePaymentResponse, SendPaymentResponse, + WalletInfo, WalletOptions, CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL, }; pub struct Wallet { @@ -114,7 +114,7 @@ impl Wallet { { match cloned.try_claim(&preimage, &redeem_script, &blinding_key, None) { Ok(_) => cloned.swap_persister.resolve_ongoing_swap(&id).unwrap(), - Err(e) => warn!("Could not claim yet. Err: {}", e), + Err(e) => warn!("Could not claim yet. Err: {e}"), } } }); @@ -197,28 +197,28 @@ 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)?; // TODO Separate error type? Or make WalletError more generic? let lbtc_pair = client .get_pairs()? .get_lbtc_pair() - .ok_or(SwapError::WalletError)?; + .ok_or(PaymentError::WalletError)?; 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, @@ -236,7 +236,7 @@ impl Wallet { amount_sat, funding_address: funding_address.clone(), }]) - .map_err(|_| SwapError::PersistError)?; + .map_err(|_| PaymentError::PersistError)?; Ok(PreparePaymentResponse { id, @@ -248,16 +248,18 @@ 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(|err| PaymentError::SendError { + err: err.to_string(), + })?; self.swap_persister .resolve_ongoing_swap(&res.id) - .map_err(|_| SwapError::PersistError)?; + .map_err(|_| PaymentError::PersistError)?; Ok(SendPaymentResponse { txid }) } @@ -268,17 +270,17 @@ impl Wallet { redeem_script: &str, blinding_key: &str, absolute_fees: Option, - ) -> Result { + ) -> Result { let network_config = &self.get_network_config(); let 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, )?; - let mnemonic = self.signer.mnemonic().ok_or(SwapError::WalletError)?; + let mnemonic = self.signer.mnemonic().ok_or(PaymentError::WalletError)?; let swap_key = SwapKey::from_reverse_account(&mnemonic.to_string(), "", self.network.into(), 0)?; @@ -297,12 +299,12 @@ impl Wallet { pub fn receive_payment( &self, req: ReceivePaymentRequest, - ) -> Result { + ) -> Result { let client = self.boltz_client(); let lbtc_pair = client .get_pairs()? .get_lbtc_pair() - .ok_or(SwapError::WalletError)?; + .ok_or(PaymentError::WalletError)?; let (onchain_amount_sat, invoice_amount_sat) = match (req.onchain_amount_sat, req.invoice_amount_sat) { @@ -325,15 +327,18 @@ impl Wallet { let fees_claim = CLAIM_ABSOLUTE_FEES; // lbtc_pair.fees.reverse_claim_estimate(); let fees_total = fees_boltz + fees_lockup + fees_claim; - ensure_sdk!(invoice_amount_sat > fees_total, SwapError::AmountOutOfRange); + ensure_sdk!( + invoice_amount_sat > fees_total, + PaymentError::AmountOutOfRange + ); Ok((invoice_amount_sat - fees_total, invoice_amount_sat)) } - (None, None) => Err(SwapError::AmountOutOfRange), + (None, None) => Err(PaymentError::AmountOutOfRange), // TODO The request should not allow setting both invoice and onchain amounts, so this case shouldn't be possible. // See example of how it's done in the SDK. - _ => Err(SwapError::BoltzGeneric { + _ => Err(PaymentError::BoltzGeneric { err: "Both invoice and onchain amounts were specified".into(), }), }?; @@ -342,15 +347,15 @@ impl Wallet { lbtc_pair .limits .within(invoice_amount_sat) - .map_err(|_| SwapError::AmountOutOfRange)?; + .map_err(|_| PaymentError::AmountOutOfRange)?; - let mnemonic = self.signer.mnemonic().ok_or(SwapError::WalletError)?; + let mnemonic = self.signer.mnemonic().ok_or(PaymentError::WalletError)?; let swap_key = SwapKey::from_reverse_account(&mnemonic.to_string(), "", self.network.into(), 0)?; let lsk = LiquidSwapKey::try_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() { @@ -377,12 +382,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 @@ -394,9 +399,9 @@ impl Wallet { invoice_amount_sat, onchain_amount_sat, }])) - .map_err(|_| SwapError::PersistError)?; + .map_err(|_| PaymentError::PersistError)?; - Ok(SwapLbtcResponse { + Ok(ReceivePaymentResponse { id: swap_id, invoice: invoice.to_string(), })