Skip to content

Commit

Permalink
Persist pending payment amount
Browse files Browse the repository at this point in the history
  • Loading branch information
dleutenegger committed Nov 17, 2023
1 parent 15c46a5 commit b700555
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
30 changes: 13 additions & 17 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ impl BreezServices {
.on_payment_completed(
parsed_invoice.payee_pubkey.clone(),
Some(parsed_invoice),
req.amount_msat,
payment_res,
)
.await?;
Expand All @@ -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 })
}
Expand Down Expand Up @@ -334,15 +333,14 @@ 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),
Err(e) => match e {
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,
Expand Down Expand Up @@ -394,7 +392,7 @@ impl BreezServices {
Some(req.data.metadata_str),
req.data.ln_address,
None,
None,
invoice.amount_msat,
)?;

Ok(LnUrlPayResult::EndpointSuccess {
Expand Down Expand Up @@ -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(),
Expand All @@ -938,14 +936,22 @@ impl BreezServices {
},
},
}])?;

self.persister.insert_payment_external_info(
&invoice.payment_hash,
None,
None,
None,
None,
Some(amount_msat),
)?;
Ok(())
}

async fn on_payment_completed(
&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 @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1613,10 +1613,10 @@ impl TryFrom<cln::ListpaysPays> 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,
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 @@ -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;",
]
}
2 changes: 1 addition & 1 deletion libs/sdk-core/src/persist/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);",
[],
Expand Down
16 changes: 8 additions & 8 deletions libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl SqliteStorage {
lnurl_metadata: Option<String>,
ln_address: Option<String>,
lnurl_withdraw_endpoint: Option<String>,
failed_amount_msat: Option<u64>,
attempted_amount_msat: Option<u64>,
) -> PersistResult<()> {
let con = self.get_connection()?;
let mut prep_statement = con.prepare(
Expand All @@ -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)
",
Expand All @@ -91,7 +91,7 @@ impl SqliteStorage {
lnurl_metadata,
ln_address,
lnurl_withdraw_endpoint,
failed_amount_msat,
attempted_amount_msat,
))?;

Ok(())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<u64> = row.get(12)?;
let attempted_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: 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,
Expand Down

0 comments on commit b700555

Please sign in to comment.