From 64ab0990f390bf64335e76ee89f3c96852be6d81 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 16 Sep 2024 16:26:28 -0700 Subject: poll for payment success --- lib/nostr.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/nostr.ts b/lib/nostr.ts index 3a0dafb5..d5bb3ed3 100644 --- a/lib/nostr.ts +++ b/lib/nostr.ts @@ -2,7 +2,7 @@ import config from "$config"; import { l } from "$lib/logging"; import { Relay, RelayPool, calculateId, signId, getPublicKey } from "nostr"; import { g, s, db } from "$lib/db"; -import { fail, wait } from "$lib/utils"; +import { fail, sleep, wait } from "$lib/utils"; import { nip04, nip19 } from "nostr-tools"; import { emit } from "$lib/sockets"; import { sendLightning } from "$lib/payments"; @@ -289,14 +289,29 @@ r.on("event", async (sub, ev) => { let user = await g(`user:${uid}`); if (method === "pay_invoice") { - let { ref: preimage } = await sendLightning({ + await sendLightning({ amount, user, pr, maxfee: 5000, }); - let res = { result_type: method, result: { preimage } }; + let res = { result_type: method, result: undefined, error: undefined }; + + for (let i = 0; i < 10; i++) { + let { pays } = await ln.listpays(pr); + let p = pays.find((p) => p.status === "complete"); + if (p) { + let { preimage } = p; + res.result = { preimage }; + break; + } + await sleep(2000); + } + + if (!res.result) + res.error = { code: "INTERNAL", message: "Payment timed out" }; + let content = nip04.encrypt(sk, pubkey, JSON.stringify(res)); let rev = { -- cgit v1.2.3