Skip to content

Commit

Permalink
Update payments query to include chain swaps without txs
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Dec 19, 2024
1 parent 90b5164 commit ba8e0ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
17 changes: 6 additions & 11 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,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 @@ -1344,16 +1348,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,
refund_tx_id: swap.refund_tx_id,
refund_tx_amount_sat: swap.refund_tx_amount_sat,
},
details: payment_details,
}
}

Expand Down
29 changes: 20 additions & 9 deletions lib/core/src/persist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,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 @@ -457,7 +464,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 Down

0 comments on commit ba8e0ac

Please sign in to comment.