From 19ad32ab10a39265874be97f008d8d3283d15004 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Thu, 4 Apr 2024 11:41:25 -0500 Subject: [PATCH] Skip expired invoices --- src/invoice.rs | 8 ++++++++ src/models/invoice.rs | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/src/invoice.rs b/src/invoice.rs index 1b5a032..b015bd1 100644 --- a/src/invoice.rs +++ b/src/invoice.rs @@ -54,6 +54,14 @@ pub(crate) async fn handle_pending_invoices(state: &State) -> Result<()> { if let Some(client) = state.mm.get_federation_client(federation_id).await { let ln = client.get_first_module::(); for invoice in invoices { + // Check if invoice has expired + if invoice.bolt11().is_expired() { + state + .db + .set_invoice_state(invoice, InvoiceState::Cancelled as i32)?; + continue; + } + // Create subscription to operation if it exists if let Ok(subscription) = ln .subscribe_ln_receive(invoice.op_id.parse().expect("invalid op_id")) diff --git a/src/models/invoice.rs b/src/models/invoice.rs index 09f2550..0912d04 100644 --- a/src/models/invoice.rs +++ b/src/models/invoice.rs @@ -1,5 +1,7 @@ +use std::str::FromStr; use crate::models::schema::invoice; use diesel::prelude::*; +use fedimint_ln_common::lightning_invoice::Bolt11Invoice; use serde::{Deserialize, Serialize}; #[derive( @@ -20,6 +22,10 @@ pub struct Invoice { } impl Invoice { + pub fn bolt11(&self) -> Bolt11Invoice { + Bolt11Invoice::from_str(&self.bolt11).expect("invalid bolt11") + } + pub fn get_invoices(conn: &mut PgConnection) -> anyhow::Result> { Ok(invoice::table.load::(conn)?) }