Skip to content

Commit

Permalink
Update list payments to include chain swaps with only user lockup (#620)
Browse files Browse the repository at this point in the history
* Update payments query to include chain swaps without txs

* Allow emitting a payment event without tx_id
  • Loading branch information
dangeross authored and hydra-yse committed Dec 24, 2024
1 parent 6af0a1b commit 9fd6369
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 81 deletions.
25 changes: 2 additions & 23 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand Down
18 changes: 6 additions & 12 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}

Expand Down
35 changes: 25 additions & 10 deletions lib/core/src/persist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)),
}
Expand All @@ -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),
)
Expand Down
21 changes: 2 additions & 19 deletions lib/core/src/receive_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down
19 changes: 2 additions & 17 deletions lib/core/src/send_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit 9fd6369

Please sign in to comment.