aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-16 16:26:28 -0700
committerAdam Soltys <[email protected]>2024-09-16 16:26:35 -0700
commit64ab0990f390bf64335e76ee89f3c96852be6d81 (patch)
treefb72e38416fff444fb195b47c84342e5dfd77060
parente4444d467ddd38ff204b17e862649d6a047bfea0 (diff)
downloadcoinos-server-64ab0990f390bf64335e76ee89f3c96852be6d81.tar.gz
coinos-server-64ab0990f390bf64335e76ee89f3c96852be6d81.zip
poll for payment success
-rw-r--r--lib/nostr.ts21
1 files changed, 18 insertions, 3 deletions
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 = {