From 9fd6369d512ddd9957316ed23eee38fa6801e6a2 Mon Sep 17 00:00:00 2001 From: Ross Savage <551697+dangeross@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:15:56 +0100 Subject: [PATCH] Update list payments to include chain swaps with only user lockup (#620) * Update payments query to include chain swaps without txs * Allow emitting a payment event without tx_id --- lib/core/src/chain_swap.rs | 25 ++----------------------- lib/core/src/model.rs | 18 ++++++------------ lib/core/src/persist/mod.rs | 35 +++++++++++++++++++++++++---------- lib/core/src/receive_swap.rs | 21 ++------------------- lib/core/src/send_swap.rs | 19 ++----------------- 5 files changed, 37 insertions(+), 81 deletions(-) diff --git a/lib/core/src/chain_swap.rs b/lib/core/src/chain_swap.rs index 09b95195b..5cfe2bb97 100644 --- a/lib/core/src/chain_swap.rs +++ b/lib/core/src/chain_swap.rs @@ -716,27 +716,6 @@ impl ChainSwapHandler { }) } - fn notify_swap_changes( - &self, - swap: ChainSwap, - updated_swap: ChainSwap, - ) -> Result<(), PaymentError> { - let payment_id = match swap.direction { - Direction::Incoming => updated_swap - .claim_tx_id - .clone() - .or(swap.claim_tx_id.clone()), - Direction::Outgoing => updated_swap - .user_lockup_tx_id - .clone() - .or(swap.user_lockup_tx_id.clone()), - }; - if let Some(payment_id) = payment_id { - let _ = self.subscription_notifier.send(payment_id); - } - Ok(()) - } - // Updates the swap without state transition validation pub(crate) fn update_swap(&self, updated_swap: ChainSwap) -> Result<(), PaymentError> { let swap = self.fetch_chain_swap_by_id(&updated_swap.id)?; @@ -751,7 +730,7 @@ impl ChainSwapHandler { updated_swap.refund_tx_id ); self.persister.insert_or_update_chain_swap(&updated_swap)?; - self.notify_swap_changes(swap, updated_swap)?; + let _ = self.subscription_notifier.send(updated_swap.id); } Ok(()) } @@ -767,7 +746,7 @@ impl ChainSwapHandler { self.persister.try_handle_chain_swap_update(swap_update)?; let updated_swap = self.fetch_chain_swap_by_id(&swap_update.swap_id)?; if updated_swap != swap { - self.notify_swap_changes(swap, updated_swap)?; + let _ = self.subscription_notifier.send(updated_swap.id); } Ok(()) } diff --git a/lib/core/src/model.rs b/lib/core/src/model.rs index 24afddc34..e24508f6b 100644 --- a/lib/core/src/model.rs +++ b/lib/core/src/model.rs @@ -1415,7 +1415,11 @@ pub struct Payment { pub details: PaymentDetails, } impl Payment { - pub(crate) fn from_pending_swap(swap: PaymentSwapData, payment_type: PaymentType) -> Payment { + pub(crate) fn from_pending_swap( + swap: PaymentSwapData, + payment_type: PaymentType, + payment_details: PaymentDetails, + ) -> Payment { let amount_sat = match payment_type { PaymentType::Receive => swap.receiver_amount_sat, PaymentType::Send => swap.payer_amount_sat, @@ -1430,17 +1434,7 @@ impl Payment { swapper_fees_sat: Some(swap.swapper_fees_sat), payment_type, status: swap.status, - details: PaymentDetails::Lightning { - swap_id: swap.swap_id, - preimage: swap.preimage, - bolt11: swap.bolt11, - bolt12_offer: swap.bolt12_offer, - payment_hash: swap.payment_hash, - description: swap.description, - lnurl_info: None, - refund_tx_id: swap.refund_tx_id, - refund_tx_amount_sat: swap.refund_tx_amount_sat, - }, + details: payment_details, } } diff --git a/lib/core/src/persist/mod.rs b/lib/core/src/persist/mod.rs index 9ade6ba02..30e88df42 100644 --- a/lib/core/src/persist/mod.rs +++ b/lib/core/src/persist/mod.rs @@ -384,21 +384,28 @@ impl Persister { FROM payment_tx_data AS ptx -- Payment tx (each tx results in a Payment) FULL JOIN ( SELECT * FROM receive_swaps - WHERE COALESCE(claim_tx_id, lockup_tx_id, mrh_tx_id) IS NOT NULL - ) rs -- Receive Swap data (by claim) + WHERE + COALESCE(claim_tx_id, lockup_tx_id, mrh_tx_id) IS NOT NULL + AND state NOT IN (0, 3, 4) -- Ignore Created, Failed and TimedOut + ) rs -- Receive Swap data ON ptx.tx_id in (rs.claim_tx_id, rs.mrh_tx_id) + FULL JOIN ( + SELECT * FROM chain_swaps + WHERE + COALESCE(user_lockup_tx_id, claim_tx_id) IS NOT NULL + AND state NOT IN (0, 4) -- Ignore Created and TimedOut + ) cs -- Chain Swap data + ON ptx.tx_id in (cs.user_lockup_tx_id, cs.claim_tx_id) LEFT JOIN send_swaps AS ss -- Send Swap data ON ptx.tx_id = ss.lockup_tx_id - LEFT JOIN chain_swaps AS cs -- Chain Swap data - ON ptx.tx_id in (cs.user_lockup_tx_id, cs.claim_tx_id) LEFT JOIN payment_tx_data AS rtx -- Refund tx data ON rtx.tx_id in (ss.refund_tx_id, cs.refund_tx_id) LEFT JOIN payment_details AS pd -- Payment details ON pd.tx_id = ptx.tx_id - WHERE -- Filter out refund txs from Send Swaps - ptx.tx_id NOT IN (SELECT refund_tx_id FROM send_swaps WHERE refund_tx_id NOT NULL) - AND -- Filter out refund txs from Chain Swaps - ptx.tx_id NOT IN (SELECT refund_tx_id FROM chain_swaps WHERE refund_tx_id NOT NULL) + WHERE + (ptx.tx_id IS NULL -- Filter out refund txs from Chain/Send Swaps + OR ptx.tx_id NOT IN (SELECT refund_tx_id FROM send_swaps WHERE refund_tx_id NOT NULL) + AND ptx.tx_id NOT IN (SELECT refund_tx_id FROM chain_swaps WHERE refund_tx_id NOT NULL)) AND {} ORDER BY -- Order by swap creation time or tx timestamp (in case of direct tx) COALESCE(rs.created_at, ss.created_at, cs.created_at, ptx.timestamp) DESC @@ -632,7 +639,11 @@ impl Persister { match (tx, swap.clone()) { (None, None) => Err(maybe_tx_tx_id.err().unwrap()), - (None, Some(swap)) => Ok(Payment::from_pending_swap(swap, payment_type)), + (None, Some(swap)) => Ok(Payment::from_pending_swap( + swap, + payment_type, + payment_details, + )), (Some(tx), None) => Ok(Payment::from_tx_data(tx, None, payment_details)), (Some(tx), Some(swap)) => Ok(Payment::from_tx_data(tx, Some(swap), payment_details)), } @@ -642,7 +653,11 @@ impl Persister { Ok(self .get_connection()? .query_row( - &self.select_payment_query(Some("ptx.tx_id = ?1"), None, None), + &self.select_payment_query( + Some("(ptx.tx_id = ?1 OR COALESCE(rs.id, ss.id, cs.id) = ?1)"), + None, + None, + ), params![id], |row| self.sql_row_to_payment(row), ) diff --git a/lib/core/src/receive_swap.rs b/lib/core/src/receive_swap.rs index 88c783793..4309c9d17 100644 --- a/lib/core/src/receive_swap.rs +++ b/lib/core/src/receive_swap.rs @@ -240,23 +240,6 @@ impl ReceiveSwapHandler { }) } - fn notify_swap_changes( - &self, - swap: ReceiveSwap, - updated_swap: ReceiveSwap, - ) -> Result<(), PaymentError> { - let payment_id = updated_swap - .claim_tx_id - .clone() - .or(updated_swap.mrh_tx_id.clone()) - .or(swap.claim_tx_id.clone()) - .or(swap.mrh_tx_id.clone()); - if let Some(payment_id) = payment_id { - let _ = self.subscription_notifier.send(payment_id); - } - Ok(()) - } - // Updates the swap without state transition validation pub(crate) fn update_swap(&self, updated_swap: ReceiveSwap) -> Result<(), PaymentError> { let swap = self.fetch_receive_swap_by_id(&updated_swap.id)?; @@ -267,7 +250,7 @@ impl ReceiveSwapHandler { ); self.persister .insert_or_update_receive_swap(&updated_swap)?; - self.notify_swap_changes(swap, updated_swap)?; + let _ = self.subscription_notifier.send(updated_swap.id); } Ok(()) } @@ -303,7 +286,7 @@ impl ReceiveSwapHandler { } if updated_swap != swap { - self.notify_swap_changes(swap, updated_swap)?; + let _ = self.subscription_notifier.send(updated_swap.id); } Ok(()) } diff --git a/lib/core/src/send_swap.rs b/lib/core/src/send_swap.rs index 2dd680570..e612d052d 100644 --- a/lib/core/src/send_swap.rs +++ b/lib/core/src/send_swap.rs @@ -254,21 +254,6 @@ impl SendSwapHandler { }) } - fn notify_swap_changes( - &self, - swap: SendSwap, - updated_swap: SendSwap, - ) -> Result<(), PaymentError> { - let payment_id = updated_swap - .lockup_tx_id - .clone() - .or(swap.lockup_tx_id.clone()); - if let Some(payment_id) = payment_id { - let _ = self.subscription_notifier.send(payment_id); - } - Ok(()) - } - // Updates the swap without state transition validation pub(crate) fn update_swap(&self, updated_swap: SendSwap) -> Result<(), PaymentError> { let swap = self.fetch_send_swap_by_id(&updated_swap.id)?; @@ -282,7 +267,7 @@ impl SendSwapHandler { updated_swap.refund_tx_id ); self.persister.insert_or_update_send_swap(&updated_swap)?; - self.notify_swap_changes(swap, updated_swap)?; + let _ = self.subscription_notifier.send(updated_swap.id); } Ok(()) } @@ -362,7 +347,7 @@ impl SendSwapHandler { let updated_swap = self.fetch_send_swap_by_id(swap_id)?; let lnurl_info_updated = self.update_swap_lnurl_info(&swap, &updated_swap)?; if updated_swap != swap || lnurl_info_updated { - self.notify_swap_changes(swap, updated_swap)?; + let _ = self.subscription_notifier.send(updated_swap.id); } Ok(()) }