diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/store.js | 1 | ||||
| -rw-r--r-- | lib/sync.js | 50 |
2 files changed, 41 insertions, 10 deletions
diff --git a/lib/store.js b/lib/store.js index 1d7e4eef..6f2e47ce 100644 --- a/lib/store.js +++ b/lib/store.js @@ -7,6 +7,7 @@ export default { exceptions: [], issuances: {}, logins: {}, + nodes: [], seen: [], sessions: {}, sockets: {}, diff --git a/lib/sync.js b/lib/sync.js index 625d128d..d4900c58 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -9,6 +9,7 @@ import read from "./read"; import { Op } from "@sequelize/core"; import store from "./store"; import { l, err, warn } from "./logging"; +import ln from "./ln"; import lnd from "./lnd"; import { getInvoices } from "lightning"; import bc from "./bitcoin"; @@ -75,11 +76,11 @@ const sync = async () => { ] }); - let missed, invoices; + let missed, invoices, recent; if (config.lnd) { ({ invoices } = await getInvoices({ lnd, limit: 1000 })); - let recent = invoices + recent = invoices .filter( i => i.is_confirmed && @@ -91,13 +92,34 @@ const sync = async () => { pr: i.request, createdAt: new Date(i.created_at) })); + } else { + ({ invoices } = await ln.listinvoices()); + recent = invoices + .filter( + i => i.status === "paid" && Date.now() - i.paid_at < 60 * 60 * 1000 + ) + .map(i => ({ + amount: parseInt(Math.round(i.msatoshi_received / 1000)), + preimage: i.payment_preimage, + pr: i.bolt11, + createdAt: new Date(i.paid_at * 1000) + })); + } + + await db.transaction(async transaction => { const twoDaysAgo = new Date(new Date().setDate(new Date().getDate() - 2)); let settled = ( - await db.Payment.findAll({ - where: { network: "lightning", createdAt: { [Op.gt]: twoDaysAgo } }, - attributes: ["preimage"] - }) + await db.Payment.findAll( + { + where: { + network: "lightning", + createdAt: { [Op.gt]: twoDaysAgo } + }, + attributes: ["preimage"] + }, + transaction + ) ).map(p => p.preimage); missed = recent.filter(i => !settled.includes(i.preimage)); @@ -111,7 +133,8 @@ const sync = async () => { include: { model: db.User, as: "user" - } + }, + transaction }); if (invoice && invoice.user_id) { @@ -120,7 +143,8 @@ const sync = async () => { user_id: invoice.user_id, asset: config.liquid.btcasset, pubkey: null - } + }, + transaction }); if (account) { @@ -141,7 +165,8 @@ const sync = async () => { updatedAt: createdAt }); - await account.increment({ balance: amount }); + await invoice.increment({ received: amount }, { transaction }); + await account.increment({ balance: amount }, { transaction }); l("lightning missing from account", account.id, amount); } @@ -155,7 +180,7 @@ const sync = async () => { ); } } - } + }); const unconfirmed = await db.Payment.findAll({ where: { @@ -167,6 +192,10 @@ const sync = async () => { as: "account" }, { + model: db.Invoice, + as: "invoice" + }, + { model: db.User, as: "user" } @@ -200,6 +229,7 @@ const sync = async () => { l("confirming payment", p.id, p.user_id, p.hash, p.address); await p.save({ transaction }); + await p.invoice.increment({ received: p.amount }, { transaction }); await p.account.increment({ balance: p.amount }, { transaction }); await p.account.decrement({ pending: p.amount }, { transaction }); }); |
