diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/db.js | 17 | ||||
| -rw-r--r-- | lib/lightning.js | 12 | ||||
| -rw-r--r-- | lib/rates.js | 1 | ||||
| -rw-r--r-- | lib/sockets.js | 1 | ||||
| -rw-r--r-- | lib/utils.js | 2 |
5 files changed, 23 insertions, 10 deletions
@@ -1,8 +1,6 @@ import { createClient } from "redis"; -export const db = createClient({ - url: "redis://rd" -}); +export const db = createClient({ url: "redis://db" }); db.on("error", err => console.log("db error", err)); @@ -12,3 +10,16 @@ export default db; export let g = async k => JSON.parse(await db.get(k)); export let s = (k, v) => db.set(k, JSON.stringify(v)); + +let retries = {}; +export let t = async (k, f) => { + retries[k] = retries[k] ? retries[k]++ : 0; + await db.watch(k); + let o = f(await g(k)); + let m = await db.multi(); + await s(k, o); + + if (retries[k] < 10) { + if (!(await m.exec())) setTimeout(() => t(k, f), 100); + } else throw new Error("could not obtain lock"); +}; diff --git a/lib/lightning.js b/lib/lightning.js index 2a8e8053..be5929b7 100644 --- a/lib/lightning.js +++ b/lib/lightning.js @@ -8,8 +8,8 @@ import { g, s, db } from "$lib/db"; export async function listenForLightning() { let { - payment_hash, - bolt11: request, + payment_hash: hash, + bolt11, pay_index, status, msatoshi_received, @@ -25,10 +25,10 @@ export async function listenForLightning() { if (!preimage) return; - let invoice = await g(`invoice:${payment_hash}`); - if (!invoice) return warn("received lightning with no invoice", request); + let invoice = await g(`invoice:${hash}`); + if (!invoice) return warn("received lightning with no invoice", bolt11); - let { hash, currency, memo, rate, tip, uid } = invoice; + let { currency, memo, rate, tip, uid } = invoice; let amount = parseInt(received) - tip; if (amount < 0) throw new Error("amount out of range"); @@ -50,7 +50,7 @@ export async function listenForLightning() { invoice.status = "paid"; invoice.received += received; - await s(`invoice:${payment_hash}`, invoice); + await s(`invoice:${hash}`, invoice); let t = async () => { await db.watch(`balance:${uid}`); diff --git a/lib/rates.js b/lib/rates.js index 6f87c78e..7cc24e6b 100644 --- a/lib/rates.js +++ b/lib/rates.js @@ -21,6 +21,7 @@ let connect = () => { store.last = msg.c; } catch (e) { + console.log(e) err("binance message error", e.message); } }; diff --git a/lib/sockets.js b/lib/sockets.js index e803d5b0..a7d392a9 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -83,6 +83,7 @@ export async function socketServer(app) { let { id: uid } = jwt.decode(token); if (!uid) throw new Error("Invalid JWT token"); let user = await g(`user:${uid}`); + console.log("uid", uid) if (!user) throw new Error("User not found"); if (!store.sockets[uid]) store.sockets[uid] = {}; diff --git a/lib/utils.js b/lib/utils.js index e53bf649..8d2648f2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -26,4 +26,4 @@ export const SATS = 100000000; export const toSats = n => Math.round(n * SATS); export const uniq = (a, k) => [...new Map(a.map(x => [k(x), x])).values()]; -export const pick = (O, K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); +export const pick = (O, K) => K.reduce((o, k) => (typeof O[k] !== 'undefined' && (o[k] = O[k]), o), {}); |
