From b7005558fbb06154b50c9148697fe3f7b1171479 Mon Sep 17 00:00:00 2001 From: Dominic Leutenegger Date: Thu, 16 Nov 2023 17:55:40 +0100 Subject: [PATCH] Persist pending payment amount --- libs/sdk-core/src/breez_services.rs | 30 ++++++++++------------- libs/sdk-core/src/greenlight/node_api.rs | 4 +-- libs/sdk-core/src/persist/migrations.rs | 1 + libs/sdk-core/src/persist/sync.rs | 2 +- libs/sdk-core/src/persist/transactions.rs | 16 ++++++------ 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/libs/sdk-core/src/breez_services.rs b/libs/sdk-core/src/breez_services.rs index b0bd22896..6a356599e 100644 --- a/libs/sdk-core/src/breez_services.rs +++ b/libs/sdk-core/src/breez_services.rs @@ -283,7 +283,6 @@ impl BreezServices { .on_payment_completed( parsed_invoice.payee_pubkey.clone(), Some(parsed_invoice), - req.amount_msat, payment_res, ) .await?; @@ -304,7 +303,7 @@ impl BreezServices { .map_err(Into::into) .await; let payment = self - .on_payment_completed(req.node_id, None, Some(req.amount_msat), payment_res) + .on_payment_completed(req.node_id, None, payment_res) .await?; Ok(SendPaymentResponse { payment }) } @@ -334,6 +333,7 @@ impl BreezServices { bolt11: cb.pr.clone(), amount_msat: None, }; + let invoice = parse_invoice(cb.pr.as_str())?; let payment = match self.send_payment(pay_req).await { Ok(p) => Ok(p), @@ -341,8 +341,6 @@ impl BreezServices { SendPaymentError::InvalidInvoice { .. } => Err(e), SendPaymentError::ServiceConnectivity { .. } => Err(e), _ => { - let invoice = parse_invoice(cb.pr.as_str())?; - return Ok(LnUrlPayResult::PayError { data: LnUrlPayErrorData { payment_hash: invoice.payment_hash, @@ -394,7 +392,7 @@ impl BreezServices { Some(req.data.metadata_str), req.data.ln_address, None, - None, + invoice.amount_msat, )?; Ok(LnUrlPayResult::EndpointSuccess { @@ -918,7 +916,7 @@ impl BreezServices { id: invoice.payment_hash.clone(), payment_type: PaymentType::Sent, payment_time: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64, - amount_msat, + amount_msat: 0, fee_msat: 0, status: PaymentStatus::Pending, description: invoice.description.clone(), @@ -938,6 +936,15 @@ impl BreezServices { }, }, }])?; + + self.persister.insert_payment_external_info( + &invoice.payment_hash, + None, + None, + None, + None, + Some(amount_msat), + )?; Ok(()) } @@ -945,7 +952,6 @@ impl BreezServices { &self, node_id: String, invoice: Option, - amount_msat: Option, payment_res: Result, ) -> Result { self.do_sync(payment_res.is_ok()).await?; @@ -962,16 +968,6 @@ 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(), diff --git a/libs/sdk-core/src/greenlight/node_api.rs b/libs/sdk-core/src/greenlight/node_api.rs index b542fb7db..0a8416736 100644 --- a/libs/sdk-core/src/greenlight/node_api.rs +++ b/libs/sdk-core/src/greenlight/node_api.rs @@ -1613,10 +1613,10 @@ impl TryFrom for Payment { payment_type: PaymentType::Sent, payment_time: payment.completed_at.unwrap_or(payment.created_at) as i64, amount_msat: match status { - PaymentStatus::Failed => ln_invoice + PaymentStatus::Complete => payment_amount, + _ => ln_invoice .as_ref() .map_or(0, |i| i.amount_msat.unwrap_or_default()), - _ => payment_amount, }, fee_msat: payment_amount_sent - payment_amount, status, diff --git a/libs/sdk-core/src/persist/migrations.rs b/libs/sdk-core/src/persist/migrations.rs index 1bf16c54b..c23d8661b 100644 --- a/libs/sdk-core/src/persist/migrations.rs +++ b/libs/sdk-core/src/persist/migrations.rs @@ -531,5 +531,6 @@ pub(crate) fn current_sync_migrations() -> Vec<&'static str> { END; ", "ALTER TABLE payments_external_info ADD COLUMN failed_amount_msat INTEGER;", + "ALTER TABLE payments_external_info RENAME COLUMN failed_amount_msat TO attempted_amount_msat;", ] } diff --git a/libs/sdk-core/src/persist/sync.rs b/libs/sdk-core/src/persist/sync.rs index 55a2720a8..772d69425 100644 --- a/libs/sdk-core/src/persist/sync.rs +++ b/libs/sdk-core/src/persist/sync.rs @@ -169,7 +169,7 @@ impl SqliteStorage { ln_address, lnurl_metadata, lnurl_withdraw_endpoint, - failed_amount_msat + attempted_amount_msat FROM remote_sync.payments_external_info WHERE payment_id NOT IN (SELECT payment_id FROM sync.payments_external_info);", [], diff --git a/libs/sdk-core/src/persist/transactions.rs b/libs/sdk-core/src/persist/transactions.rs index 705942aa6..ef1eb3a42 100644 --- a/libs/sdk-core/src/persist/transactions.rs +++ b/libs/sdk-core/src/persist/transactions.rs @@ -68,7 +68,7 @@ impl SqliteStorage { lnurl_metadata: Option, ln_address: Option, lnurl_withdraw_endpoint: Option, - failed_amount_msat: Option, + attempted_amount_msat: Option, ) -> PersistResult<()> { let con = self.get_connection()?; let mut prep_statement = con.prepare( @@ -79,7 +79,7 @@ impl SqliteStorage { lnurl_metadata, ln_address, lnurl_withdraw_endpoint, - failed_amount_msat + attempted_amount_msat ) VALUES (?1,?2,?3,?4,?5,?6) ", @@ -91,7 +91,7 @@ impl SqliteStorage { lnurl_metadata, ln_address, lnurl_withdraw_endpoint, - failed_amount_msat, + attempted_amount_msat, ))?; Ok(()) @@ -157,7 +157,7 @@ impl SqliteStorage { e.lnurl_metadata, e.ln_address, e.lnurl_withdraw_endpoint, - e.failed_amount_msat, + e.attempted_amount_msat, o.payer_amount_msat FROM payments p LEFT JOIN sync.payments_external_info e @@ -205,7 +205,7 @@ impl SqliteStorage { e.lnurl_metadata, e.ln_address, e.lnurl_withdraw_endpoint, - e.failed_amount_msat, + e.attempted_amount_msat, o.payer_amount_msat FROM payments p LEFT JOIN sync.payments_external_info e @@ -239,14 +239,14 @@ impl SqliteStorage { 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 = row.get(12)?; + let attempted_amount_msat: Option = 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: match status { - PaymentStatus::Failed => failed_amount_msat.unwrap_or(amount_msat), - _ => amount_msat, + PaymentStatus::Complete => amount_msat, + _ => attempted_amount_msat.unwrap_or(amount_msat), }, fee_msat: row.get(4)?, status,