Skip to content

Commit

Permalink
Merge pull request #589 from breez/savage-persist-failed-payment-amount
Browse files Browse the repository at this point in the history
Persist failed payment amount
  • Loading branch information
dangeross authored Nov 9, 2023
2 parents efea7a4 + d1289b4 commit acf081e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
26 changes: 21 additions & 5 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl BreezServices {
.on_payment_completed(
parsed_invoice.payee_pubkey.clone(),
Some(parsed_invoice),
req.amount_msat,
payment_res,
)
.await?;
Expand All @@ -290,7 +291,7 @@ impl BreezServices {
.map_err(Into::into)
.await;
let payment = self
.on_payment_completed(req.node_id, None, payment_res)
.on_payment_completed(req.node_id, None, Some(req.amount_msat), payment_res)
.await?;
Ok(SendPaymentResponse { payment })
}
Expand Down Expand Up @@ -367,12 +368,13 @@ impl BreezServices {
};

// Store SA (if available) + LN Address in separate table, associated to payment_hash
self.persister.insert_lnurl_payment_external_info(
self.persister.insert_payment_external_info(
&details.payment_hash,
maybe_sa_processed.as_ref(),
Some(req.data.metadata_str),
req.data.ln_address,
None,
None,
)?;

Ok(LnUrlPayResult::EndpointSuccess {
Expand Down Expand Up @@ -410,12 +412,13 @@ impl BreezServices {

if let LnUrlWithdrawResult::Ok { ref data } = res {
// If endpoint was successfully called, store the LNURL-withdraw endpoint URL as metadata linked to the invoice
self.persister.insert_lnurl_payment_external_info(
self.persister.insert_payment_external_info(
&data.invoice.payment_hash,
None,
None,
None,
Some(lnurl_w_endpoint),
None,
)?;
}

Expand Down Expand Up @@ -885,6 +888,7 @@ impl BreezServices {
&self,
node_id: String,
invoice: Option<LNInvoice>,
amount_msat: Option<u64>,
payment_res: Result<PaymentResponse, SendPaymentError>,
) -> Result<Payment, SendPaymentError> {
self.do_sync(payment_res.is_ok()).await?;
Expand All @@ -901,6 +905,16 @@ impl BreezServices {
}),
},
Err(e) => {
if let Some(inv) = invoice.clone() {
self.persister.insert_payment_external_info(
&inv.payment_hash,
None,
None,
None,
None,
amount_msat.or(inv.amount_msat),
)?;
}
self.notify_event_listeners(BreezEvent::PaymentFailed {
details: PaymentFailedData {
error: e.to_string(),
Expand Down Expand Up @@ -2161,19 +2175,21 @@ pub(crate) mod tests {
let persister = Arc::new(create_test_persister(test_config.clone()));
persister.init()?;
persister.insert_or_update_payments(&dummy_transactions)?;
persister.insert_lnurl_payment_external_info(
persister.insert_payment_external_info(
payment_hash_with_lnurl_success_action,
Some(&sa),
Some(lnurl_metadata.to_string()),
Some(test_ln_address.to_string()),
None,
None,
)?;
persister.insert_lnurl_payment_external_info(
persister.insert_payment_external_info(
payment_hash_lnurl_withdraw,
None,
None,
None,
Some(test_lnurl_withdraw_endpoint.to_string()),
None,
)?;
persister.insert_swap(swap_info.clone())?;
persister.update_swap_bolt11(
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-core/src/persist/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,6 @@ pub(crate) fn current_sync_migrations() -> Vec<&'static str> {
INSERT INTO sync_requests(changed_table) VALUES('payments_external_info');
END;
",
"ALTER TABLE payments_external_info ADD COLUMN failed_amount_msat INTEGER;",
]
}
3 changes: 2 additions & 1 deletion libs/sdk-core/src/persist/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ impl SqliteStorage {
lnurl_success_action,
ln_address,
lnurl_metadata,
lnurl_withdraw_endpoint
lnurl_withdraw_endpoint,
failed_amount_msat
FROM remote_sync.payments_external_info
WHERE payment_id NOT IN (SELECT payment_id FROM sync.payments_external_info);",
[],
Expand Down
32 changes: 22 additions & 10 deletions libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl SqliteStorage {
///
/// Note that, if a payment has details of type [LnPaymentDetails] which contain a [SuccessActionProcessed],
/// then the [LnPaymentDetails] will NOT be persisted. In that case, the [SuccessActionProcessed]
/// can be inserted separately via [SqliteStorage::insert_lnurl_payment_external_info].
/// can be inserted separately via [SqliteStorage::insert_payment_external_info].
pub fn insert_or_update_payments(&self, transactions: &[Payment]) -> PersistResult<()> {
let deleted = self.delete_pending_lightning_payments()?;
debug!("Deleted {deleted} pending payments");
Expand Down Expand Up @@ -60,14 +60,15 @@ impl SqliteStorage {
)?)
}

/// Inserts LNURL-related metadata associated with this payment
pub fn insert_lnurl_payment_external_info(
/// Inserts metadata associated with this payment
pub fn insert_payment_external_info(
&self,
payment_hash: &str,
lnurl_pay_success_action: Option<&SuccessActionProcessed>,
lnurl_metadata: Option<String>,
ln_address: Option<String>,
lnurl_withdraw_endpoint: Option<String>,
failed_amount_msat: Option<u64>,
) -> PersistResult<()> {
let con = self.get_connection()?;
let mut prep_statement = con.prepare(
Expand All @@ -77,9 +78,10 @@ impl SqliteStorage {
lnurl_success_action,
lnurl_metadata,
ln_address,
lnurl_withdraw_endpoint
lnurl_withdraw_endpoint,
failed_amount_msat
)
VALUES (?1,?2,?3,?4,?5)
VALUES (?1,?2,?3,?4,?5,?6)
",
)?;

Expand All @@ -89,6 +91,7 @@ impl SqliteStorage {
lnurl_metadata,
ln_address,
lnurl_withdraw_endpoint,
failed_amount_msat,
))?;

Ok(())
Expand Down Expand Up @@ -154,6 +157,7 @@ impl SqliteStorage {
e.lnurl_metadata,
e.ln_address,
e.lnurl_withdraw_endpoint,
e.failed_amount_msat,
o.payer_amount_msat
FROM payments p
LEFT JOIN sync.payments_external_info e
Expand Down Expand Up @@ -201,6 +205,7 @@ impl SqliteStorage {
e.lnurl_metadata,
e.ln_address,
e.lnurl_withdraw_endpoint,
e.failed_amount_msat,
o.payer_amount_msat
FROM payments p
LEFT JOIN sync.payments_external_info e
Expand Down Expand Up @@ -233,13 +238,18 @@ impl SqliteStorage {
fn sql_row_to_payment(&self, row: &Row) -> PersistResult<Payment, rusqlite::Error> {
let payment_type_str: String = row.get(1)?;
let amount_msat = row.get(3)?;
let status: PaymentStatus = row.get(5)?;
let failed_amount_msat: Option<u64> = row.get(12)?;
let mut payment = Payment {
id: row.get(0)?,
payment_type: PaymentType::from_str(payment_type_str.as_str()).unwrap(),
payment_time: row.get(2)?,
amount_msat,
amount_msat: match status {
PaymentStatus::Failed => failed_amount_msat.unwrap_or(amount_msat),
_ => amount_msat,
},
fee_msat: row.get(4)?,
status: row.get(5)?,
status,
description: row.get(6)?,
details: row.get(7)?,
};
Expand All @@ -255,7 +265,7 @@ impl SqliteStorage {
}

// In case we have a record of the open channel fee, let's use it.
let payer_amount_msat: Option<u64> = row.get(12)?;
let payer_amount_msat: Option<u64> = row.get(13)?;
if let Some(payer_amount) = payer_amount_msat {
payment.fee_msat = payer_amount - amount_msat;
}
Expand Down Expand Up @@ -518,19 +528,21 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
storage.init()?;
storage.insert_or_update_payments(&txs)?;
storage.insert_or_update_payments(&failed_txs)?;
storage.insert_lnurl_payment_external_info(
storage.insert_payment_external_info(
payment_hash_with_lnurl_success_action,
Some(&sa),
Some(lnurl_metadata.to_string()),
Some(test_ln_address.to_string()),
None,
None,
)?;
storage.insert_lnurl_payment_external_info(
storage.insert_payment_external_info(
payment_hash_with_lnurl_withdraw,
None,
None,
None,
Some(lnurl_withdraw_url.to_string()),
None,
)?;
storage.insert_swap(swap_info.clone())?;
storage.update_swap_bolt11(
Expand Down

0 comments on commit acf081e

Please sign in to comment.