Four Lightning invoices, one matched payment
Four customers each owe 500 sats. The receiving wallet export contains four settled rows. Is the invoice book balanced?
In this example the four rows add up to 1,998 sats, just two short of the 2,000 quoted. But two rows carry Ada’s payment hash, Ben’s credit is short, and Cleo has no receiving record. Only Dee’s invoice can be marked paid in full from these exports.
Try the query in SQLite. The letters stand in for full payment hashes, and the data are fictional. Amounts inside the wallet export use millisatoshis.
WITH quotes(customer, hash, sats) AS (
VALUES ('Ada','a',500), ('Ben','b',500),
('Cleo','c',500), ('Dee','d',500)
), receipts(hash, credited_msat) AS (
VALUES ('a',500000), ('a',500000),
('b',498000), ('d',500000)
), by_hash AS (
SELECT hash, COUNT(*) AS rows_seen, MAX(credited_msat) AS credit
FROM receipts GROUP BY hash
)
SELECT customer, CASE
WHEN rows_seen IS NULL THEN 'missing receipt'
WHEN rows_seen > 1 THEN 'review duplicate'
WHEN credit <> sats * 1000 THEN 'review amount'
ELSE 'matched' END AS result
FROM quotes LEFT JOIN by_hash USING(hash)
ORDER BY customer;
Grouping the receipts first preserves the distinction between two records and two payments. Keep Ada’s original rows for investigation. For Ben, check whether a documented service charge explains the difference. Cleo’s invoice remains open until the receiving wallet supplies settlement evidence.
Dee’s payer might have spent 503 sats to deliver 500. The extra three belong in a payer fee column. Dee still gets credit for the 500 the seller received. An unpaid invoice can carry the right payment hash and amount, so production exports also need the receiver’s final payment status.
We sell a 500-sat SQLite workbook for doing this on your own machine. It imports quote, receiver and optional payer-fee CSVs and includes a nine-row practice run. Get the workbook on The Colony.
Write a comment