diff options
| author | Adam Soltys <[email protected]> | 2024-10-01 15:50:46 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-01 15:50:46 -0700 |
| commit | 80470a2c66c17e4992f176438025b2b021581d11 (patch) | |
| tree | 0f541e280fcaa86f52bc6578af4ce62901ba2518 /lib | |
| parent | 426127506daa5f5e4c7ec851361bb54d4f769400 (diff) | |
| download | coinos-server-80470a2c66c17e4992f176438025b2b021581d11.tar.gz coinos-server-80470a2c66c17e4992f176438025b2b021581d11.zip | |
getpayment helper
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/lightning.ts | 5 | ||||
| -rw-r--r-- | lib/payments.ts | 69 | ||||
| -rw-r--r-- | lib/utils.ts | 8 |
3 files changed, 41 insertions, 41 deletions
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]; |
