aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-10-01 15:50:46 -0700
committerAdam Soltys <[email protected]>2024-10-01 15:50:46 -0700
commit80470a2c66c17e4992f176438025b2b021581d11 (patch)
tree0f541e280fcaa86f52bc6578af4ce62901ba2518
parent426127506daa5f5e4c7ec851361bb54d4f769400 (diff)
downloadcoinos-server-80470a2c66c17e4992f176438025b2b021581d11.tar.gz
coinos-server-80470a2c66c17e4992f176438025b2b021581d11.zip
getpayment helper
-rw-r--r--index.ts3
-rw-r--r--lib/lightning.ts5
-rw-r--r--lib/payments.ts69
-rw-r--r--lib/utils.ts8
-rw-r--r--routes/payments.ts22
-rw-r--r--routes/shopify.ts4
6 files changed, 58 insertions, 53 deletions
diff --git a/index.ts b/index.ts
index 885cbf2e..76340303 100644
--- a/index.ts
+++ b/index.ts
@@ -3,7 +3,7 @@ import { auth, admin, optional } from "$lib/auth";
import { listenForLightning } from "$lib/lightning";
import { getLocations } from "$lib/locations";
-import { catchUp } from "$lib/payments";
+import { catchUp, check} from "$lib/payments";
import { getFx } from "$lib/rates";
import { sendHeartbeat } from "$lib/sockets";
import { fillPool } from "$lib/nostr";
@@ -28,6 +28,7 @@ try {
getFx();
catchUp();
nwc();
+ check();
} catch (e) {
console.log(e);
}
diff --git a/lib/lightning.ts b/lib/lightning.ts
index 74869a19..7a35c9f2 100644
--- a/lib/lightning.ts
+++ b/lib/lightning.ts
@@ -3,6 +3,7 @@ import ln from "$lib/ln";
import { err, warn } from "$lib/logging";
import { g, s } from "$lib/db";
import { credit, types } from "$lib/payments";
+import { getPayment } from "$lib/utils";
export async function listenForLightning() {
let inv = await ln.waitanyinvoice((await g("pay_index")) || 0);
@@ -35,9 +36,7 @@ export async function listenForLightning() {
}
}
- let p = await g(`payment:${bolt11}`);
- if (typeof p === "string") p = await g(`payment:${p}`);
-
+ let p = await getPayment(bolt11);
if (p) return warn("already processed", bolt11);
await credit({ hash:bolt11, amount:received, memo:invoice.memo, ref:preimage, type:types.lightning});
diff --git a/lib/payments.ts b/lib/payments.ts
index 60989924..852d6c57 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -480,7 +480,6 @@ let getMaxFee = (n) =>
: n * 0.01,
);
-let checking = {};
let inflight = {};
export let sendLightning = async ({
user,
@@ -490,8 +489,8 @@ export let sendLightning = async ({
memo = undefined,
}) => {
try {
- if (inflight[user.id]) fail("payment in flight");
- inflight[user.id] = true;
+ if (inflight[pr]) fail("payment in flight");
+ inflight[pr] = true;
let p;
let hash = pr;
@@ -534,39 +533,6 @@ export let sendLightning = async ({
type: types.lightning,
});
- let check = async () => {
- if (checking[pr]) return;
- checking[pr] = true;
-
- try {
- let { pays } = await ln.listpays(pr);
-
- let recordExists = !!(await g(`payment:${pr}`));
- let paymentFailed =
- !pays.length || pays.every((p) => p.status === "failed");
-
- if (recordExists && paymentFailed) {
- await db.del(`payment:${pr}`);
- await db.del(`payment:${p.id}`);
-
- let ourfee = p.ourfee || 0;
- let credit = Math.round(total * config.fee) - ourfee;
- await db.incrBy(`balance:${p.uid}`, total + maxfee + ourfee);
-
- await db.incrBy(`credit:${types.lightning}:${p.uid}`, credit);
- await db.lRem(`${p.uid}:payments`, 1, p.id);
-
- clearInterval(interval);
- }
- } catch (e) {
- err("Failed to reverse payment", r);
- }
-
- checking[pr] = false;
- };
-
- let interval = setInterval(check, 5000);
-
try {
l("paying lightning invoice", pr.substr(-8), total, amount, maxfee);
@@ -591,7 +557,6 @@ export let sendLightning = async ({
l("refunding fee", maxfee, p.fee, maxfee - p.fee, p.ref);
await db.incrBy(`balance:${p.uid}`, maxfee - p.fee);
} catch (e) {
- await check();
throw e;
}
@@ -826,3 +791,33 @@ export let reconcile = async (account, initial = false) => {
return setTimeout(() => reconcile(account, initial), 1000);
}
};
+
+export let check = async () => {
+ let payments = await db.sMembers("inflight");
+
+ for (let pr of payments) {
+ try {
+ let { pays } = await ln.listpays(pr);
+
+ let recordExists = !!(await g(`payment:${pr}`));
+ let paymentFailed =
+ !pays.length || pays.every((p) => p.status === "failed");
+
+ if (recordExists && paymentFailed) {
+ await db.del(`payment:${pr}`);
+ await db.del(`payment:${p.id}`);
+
+ let ourfee = p.ourfee || 0;
+ let credit = Math.round(total * config.fee) - ourfee;
+ await db.incrBy(`balance:${p.uid}`, total + maxfee + ourfee);
+
+ await db.incrBy(`credit:${types.lightning}:${p.uid}`, credit);
+ await db.lRem(`${p.uid}:payments`, 1, p.id);
+ }
+ } catch (e) {
+ err("Failed to reverse payment", r);
+ }
+
+ setTimeout(check, 2000);
+ }
+};
diff --git a/lib/utils.ts b/lib/utils.ts
index e0e1ace5..e79f459f 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -87,6 +87,12 @@ export let getInvoice = async (hash) => {
return await g(`invoice:${iid}`);
};
+export let getPayment = async (id) => {
+ let p = await g(`payment:${id}`);
+ if (typeof p === "string") p = await g(`payment:${p}`);
+ return p;
+};
+
export let f = (s, currency) =>
new Intl.NumberFormat("en-US", {
style: "currency",
@@ -161,4 +167,4 @@ export function formatReceipt(items, currency) {
.join("\n");
}
- export let t = ({ language = "en" }) => locales[language];
+export let t = ({ language = "en" }) => locales[language];
diff --git a/routes/payments.ts b/routes/payments.ts
index 5d190c7e..6ee5a23a 100644
--- a/routes/payments.ts
+++ b/routes/payments.ts
@@ -3,7 +3,15 @@ import { emit } from "$lib/sockets";
import { v4 } from "uuid";
import { db, g, s, t } from "$lib/db";
import { l, err, warn } from "$lib/logging";
-import { bail, fail, getInvoice, getUser, sats, SATS } from "$lib/utils";
+import {
+ bail,
+ fail,
+ getInvoice,
+ getPayment,
+ getUser,
+ sats,
+ SATS,
+} from "$lib/utils";
import { requirePin } from "$lib/auth";
import {
completePayment,
@@ -138,8 +146,7 @@ export default {
let {
params: { hash },
} = req;
- let p = await g(`payment:${hash}`);
- if (typeof p === "string") p = await g(`payment:${p}`);
+ let p = await getPayment(hash);
if (p.type === types.internal) p.with = await g(`user:${p.ref}`);
res.send(p);
},
@@ -271,8 +278,7 @@ export default {
if (type === types.liquid && asset !== config.liquid.btc) continue;
if (category === "send") {
- let p = await g(`payment:${txid}`);
- if (typeof p === "string") p = await g(`payment:${p}`);
+ let p = await getPayment(txid);
if (!p) continue;
if (confirmations) {
@@ -287,8 +293,7 @@ export default {
continue;
}
- let p = await g(`payment:${txid}:${vout}`);
- if (typeof p === "string") p = await g(`payment:${p}`);
+ let p = await getPayment(`${txid}:${vout}`);
if (!p) {
let invoice = await getInvoice(address);
@@ -302,8 +307,7 @@ export default {
});
} else if (confirmations >= 1) {
let id = `payment:${txid}:${vout}`;
- let p = await g(id);
- if (typeof p === "string") p = await g(`payment:${p}`);
+ let p = await getPayment(`${txid}:${vout}`);
if (!p) return db.sAdd("missed", id);
if (p.confirmed) return;
diff --git a/routes/shopify.ts b/routes/shopify.ts
index b28f556f..f5c26208 100644
--- a/routes/shopify.ts
+++ b/routes/shopify.ts
@@ -1,6 +1,7 @@
import got from "got";
import { g } from "$lib/db";
import { err } from "$lib/logging";
+import { getPayment } from "$lib/utils";
let query = `mutation orderMarkAsPaid($input: OrderMarkAsPaidInput!) {
orderMarkAsPaid(input: $input) {
@@ -17,8 +18,7 @@ export default async (req, res) => {
body: { hash },
params: { id },
} = req;
- let p = await g(`payment:${hash}`);
- if (typeof p === "string") p = await g(`payment:${p}`);
+ let p = await getPayment(hash);
let user = await g(`user:${p.uid}`);
try {