From 6e415ce7f2966665756026c6d335e618b8f2a1bc Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Fri, 8 Nov 2024 10:55:01 -0600 Subject: [PATCH] Poll on the endpoint for 120 secs after zap request then it will handoff to long running invoices cron --- src/pages/api/lightning-address/lnd.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/pages/api/lightning-address/lnd.js b/src/pages/api/lightning-address/lnd.js index 0a39040..2861cfc 100644 --- a/src/pages/api/lightning-address/lnd.js +++ b/src/pages/api/lightning-address/lnd.js @@ -76,6 +76,28 @@ export default async function handler(req, res) { settled: false }, { ex: expiry || 86400 }); // expiry matches invoice expiry + // Start polling for this zap request + let attempts = 0; + const pollInterval = setInterval(async () => { + try { + const pollResponse = await axios.get(`${BACKEND_URL}/api/invoices/polling`, { + headers: { + 'Authorization': PLEBDEVS_API_KEY + } + }); + + // If no pending invoices or we've reached max attempts, stop polling + if (pollResponse.data.pending === 0 || attempts >= 120) { + clearInterval(pollInterval); + } + + attempts++; + } catch (error) { + console.error('Polling error:', error); + clearInterval(pollInterval); + } + }, 1000); // Poll every second + res.status(200).json({ invoice, payment_hash: paymentHashHex,