diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/payments.js | 49 | ||||
| -rw-r--r-- | lib/utils.js | 7 |
2 files changed, 27 insertions, 29 deletions
diff --git a/lib/payments.js b/lib/payments.js index 8965b530..fe96d2a0 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -5,7 +5,7 @@ import { v4 } from "uuid"; import { notify } from "$lib/notifications"; import { db, g, s, t } from "$lib/db"; import { l, err, warn } from "$lib/logging"; -import { btc, fail, sleep, wait, SATS, sats } from "$lib/utils"; +import { btc, fail, getInvoice, sleep, wait, SATS, sats } from "$lib/utils"; import { callWebhook } from "$lib/webhooks"; import got from "got"; import { spawn } from "child_process"; @@ -47,9 +47,7 @@ export let debit = async ({ let { id: uid, currency, username } = user; if (!rate) rate = store.rates[currency]; - let iid = await g(`invoice:${hash}`); - if (iid && iid.hash) iid = iid.hash; - let invoice = await g(`invoice:${iid}`); + let invoice = await getInvoice(hash); if (invoice) { ref = invoice.uid; @@ -91,7 +89,7 @@ export let debit = async ({ hash, ourfee: r.ourfee, memo, - iid, + iid: invoice.id, uid, confirmed: true, rate, @@ -114,24 +112,20 @@ export let debit = async ({ export let credit = async (hash, amount, memo, ref, type = types.internal) => { amount = parseInt(amount) || 0; - let iid = await g(`invoice:${hash}`); - if (iid && iid.hash) iid = iid.hash; - let invoice = await g(`invoice:${iid}`); - - if (!invoice) { - await db.sAdd("missing", hash); + let inv = await getInvoice(hash); + if (!inv) { + await db.sAdd("missing", ref.split(":")[0]); return; } - let { tip } = invoice; + let { tip } = inv; tip = parseInt(tip) || 0; - if (!memo) ({ memo } = invoice); + if (!memo) ({ memo } = inv); if (amount < 0 || tip < 0) fail("Invalid amount"); if (type === types.internal) amount += tip; - let user = await g(`user:${invoice.uid}`); - + let user = await g(`user:${inv.uid}`); let { id: uid, currency, username } = user; let rate = store.rates[currency]; @@ -140,9 +134,9 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { rate = store.rates[currency]; let equivalentRate = - invoice.rate * (store.rates[currency] / store.rates[invoice.currency]); + inv.rate * (store.rates[currency] / store.rates[inv.currency]); - if (Math.abs(invoice.rate / store.rates[invoice.currency] - 1) < 0.01) { + if (Math.abs(inv.rate / store.rates[inv.currency] - 1) < 0.01) { rate = equivalentRate; } else { // warn("rate slipped", hash, invoice.rate, equivalentRate); @@ -164,8 +158,8 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { created: Date.now(), }; - if ([types.bitcoin, types.liquid].includes(type)) invoice.pending += amount; - else invoice.received += amount; + if ([types.bitcoin, types.liquid].includes(type)) inv.pending += amount; + else inv.received += amount; let balance = "balance"; if ([types.bitcoin, types.liquid].includes(type)) { @@ -182,7 +176,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { if ([types.bitcoin, types.liquid, types.lightning].includes(type)) m.incrBy(`credit:${type}:${uid}`, Math.round(amount * config.fee)); - m.set(`invoice:${iid}`, JSON.stringify(invoice)) + m.set(`invoice:${inv.iid}`, JSON.stringify(inv)) .set(`payment:${p.id}`, JSON.stringify(p)) .lPush(`${uid}:payments`, p.id) .incrBy(`${balance}:${uid}`, amount) @@ -190,7 +184,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { emit(uid, "payment", p); notify(user, `Received ${amount} sats`); - callWebhook(invoice, p); + callWebhook(inv, p); if (user.verified && user.notify) { mail(user, "Payment received", templates.paymentReceived, { @@ -233,9 +227,7 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => { maxfee = maxfee ? parseInt(maxfee) : Math.round(total * 0.05); if (maxfee < 0) fail("Max fee cannot be negative"); - let iid = await g(`invoice:${hash}`); - if (iid && iid.hash) iid = iid.hash; - let invoice = await g(`invoice:${iid}`); + let invoice = await getInvoice(hash); if (invoice) { if (invoice.uid === user.id) fail("Cannot send to self"); @@ -284,7 +276,6 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => { clearInterval(interval); } } catch (e) { - console.log(e); err("Failed to reverse payment", r); } }; @@ -401,10 +392,8 @@ export let build = async ({ amount, address, feeRate }, user) => { return { feeRate, ourfee, fee, fees, hex: tx.hex }; }; -let seen; export let catchUp = async () => { try { - if (!seen) seen = await db.sMembers("missing"); let txns = []; for (let [type, n] of Object.entries({ bitcoin: bc, liquid: lq })) { txns.push( @@ -417,12 +406,14 @@ export let catchUp = async () => { for (let { txid, type } of txns) { try { - if (seen.includes(txid)) continue; + if (await db.zScore("seen", txid)) continue; await got.post(`http://localhost:${process.env.PORT || 3119}/confirm`, { json: { txid, wallet: config.bitcoin.wallet, type }, }); - seen.push(txid); + await db.zAdd("seen", { score: Date.now(), value: txid }); + if ((await db.zCard("seen")) > 10000) + await db.zRemRangeByRank("seen", 0, 0); } catch (e) { err("problem confirming", e.message); } diff --git a/lib/utils.js b/lib/utils.js index 866bbaf9..c68d222b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -69,3 +69,10 @@ export let getUser = async (username) => { if (username === "undefined") fail("invalid user"); return migrate(username); }; + +export let getInvoice = async (hash) => { + let iid = await g(`invoice:${hash}`); + if (iid && iid.id) iid = iid.id; + else if (iid && iid.hash) iid = iid.hash; + return await g(`invoice:${iid}`); +} |
