aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lightning.js8
-rw-r--r--lib/payments.js15
2 files changed, 14 insertions, 9 deletions
diff --git a/lib/lightning.js b/lib/lightning.js
index 1788fd12..77a429ed 100644
--- a/lib/lightning.js
+++ b/lib/lightning.js
@@ -8,7 +8,7 @@ import { g, s, t, db } from "$lib/db";
import { credit, types } from "$lib/payments";
export async function listenForLightning() {
- let event = await ln.waitanyinvoice(await g("pay_index"));
+ let event = await ln.waitanyinvoice((await g("pay_index")) || 0);
let {
bolt11,
pay_index,
@@ -31,8 +31,10 @@ export async function listenForLightning() {
let invoice = await g(`invoice:${id}`);
if (!invoice) return warn("received lightning with no invoice", bolt11);
- let payment = await g(`payment:${bolt11}`);
- if (payment) return warn("already processed", bolt11);
+ let p = await g(`payment:${bolt11}`);
+ if (typeof p === "string") p = await g(`payment:${p}`);
+
+ if (p) return warn("already processed", bolt11);
await credit(bolt11, received, invoice.memo, preimage, types.lightning);
} catch (e) {
diff --git a/lib/payments.js b/lib/payments.js
index 148a6ea1..369d0aec 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -133,7 +133,9 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
warn("rate slipped", hash, invoice.rate, equivalentRate);
}
+ let id = v4();
let p = {
+ id,
hash,
amount: parseInt(amount - tip),
uid,
@@ -153,12 +155,12 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
let balance = "balance";
if (type === types.bitcoin) {
let [txid, vout] = ref.split(":").slice(-2);
- p.id = `${txid}:${vout}`;
p.confirmed = false;
balance = "pending";
+ await s(`payment:${txid}:${vout}`, id);
} else {
- p.id = hash;
- }
+ await s(`payment:${hash}`, id);
+ }
let m = await db.multi();
@@ -192,17 +194,18 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
export let confirm = async (address, txid, vout) => {
let id = `payment:${txid}:${vout}`;
let p = await g(id);
+ if (typeof p === "string") p = await g(`payment:${p}`);
if (!p) return db.sAdd("missed", id);
if (p.confirmed) return;
p.confirmed = true;
emit(p.uid, "payment", p);
- l("confirming", txid, p.uid);
+ l("confirming", txid);
- await db
+ let r = await db
.multi()
- .set(id, JSON.stringify(p))
+ .set(`payment:${p.id}`, JSON.stringify(p))
.decrBy(`pending:${p.uid}`, p.amount)
.incrBy(`balance:${p.uid}`, p.amount)
.exec();