diff options
Diffstat (limited to 'lib/sync.js')
| -rw-r--r-- | lib/sync.js | 128 |
1 files changed, 64 insertions, 64 deletions
diff --git a/lib/sync.js b/lib/sync.js index 42291a94..97207752 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -16,7 +16,7 @@ import lq from "./liquid"; const init = async () => { try { - read(fs.createReadStream("exceptions"), (data) => + read(fs.createReadStream("exceptions"), data => store.exceptions.push(data) ); } catch (e) { @@ -30,13 +30,13 @@ const init = async () => { await db.Invoice.findAll({ where: { createdAt: { - [Op.gt]: thirtyDays, - }, + [Op.gt]: thirtyDays + } }, include: { model: db.User, - as: "user", - }, + as: "user" + } }) ).map(({ address, user, unconfidential }) => { if (address && user) store.addresses[address] = user.username; @@ -49,9 +49,9 @@ const init = async () => { store.payments = ( await db.Payment.findAll({ - attributes: ["hash"], + attributes: ["hash"] }) - ).map((p) => p.hash); + ).map(p => p.hash); }; init(); @@ -66,13 +66,13 @@ const sync = async () => { include: [ { model: db.Account, - as: "account", + as: "account" }, { model: db.User, - as: "user", - }, - ], + as: "user" + } + ] }); let missed, invoices, recent; @@ -81,49 +81,49 @@ const sync = async () => { recent = invoices .filter( - (i) => + i => i.is_confirmed && differenceInDays(new Date(), new Date(i.created_at)) < 2 ) - .map((i) => ({ + .map(i => ({ amount: parseInt(i.received), preimage: i.secret, pr: i.request, - createdAt: new Date(i.created_at), + createdAt: new Date(i.created_at) })); } else { ({ invoices } = await ln.listinvoices()); recent = invoices .filter( - (i) => + i => i.status === "paid" && Math.round(Date.now() / 1000) - i.paid_at < 60 * 60 * 48 ) - .map((i) => ({ + .map(i => ({ amount: parseInt(Math.round(i.msatoshi_received / 1000)), preimage: i.payment_preimage, pr: i.bolt11, - createdAt: new Date(i.paid_at * 1000), + createdAt: new Date(i.paid_at * 1000) })); } - await db.transaction(async (transaction) => { + 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 }, + createdAt: { [Op.gt]: twoDaysAgo } }, - attributes: ["preimage"], + attributes: ["preimage"] }, transaction ) - ).map((p) => p.preimage); + ).map(p => p.preimage); - missed = recent.filter((i) => !settled.includes(i.preimage)); + missed = recent.filter(i => !settled.includes(i.preimage)); for (let i = 0; i < missed.length; i++) { let { amount, preimage, pr: text, createdAt } = missed[i]; if (!text || !text.length) continue; @@ -133,9 +133,9 @@ const sync = async () => { where: { text }, include: { model: db.User, - as: "user", + as: "user" }, - transaction, + transaction }); if (invoice && invoice.user_id) { @@ -143,9 +143,9 @@ const sync = async () => { where: { user_id: invoice.user_id, asset: config.liquid.btcasset, - pubkey: null, + pubkey: null }, - transaction, + transaction }); if (account) { @@ -163,7 +163,7 @@ const sync = async () => { tip: invoice.tip, invoice_id: invoice.id, createdAt, - updatedAt: createdAt, + updatedAt: createdAt }); await invoice.increment({ received: amount }, { transaction }); @@ -181,48 +181,48 @@ const sync = async () => { const unconfirmed = await db.Payment.findAll({ where: { - confirmed: 0, + confirmed: 0 }, include: [ { model: db.Account, - as: "account", + as: "account" }, { model: db.Invoice, - as: "invoice", + as: "invoice" }, { model: db.User, - as: "user", - }, - ], + as: "user" + } + ] }); - let hashes = unconfirmed.map((p) => p.hash); + let hashes = unconfirmed.map(p => p.hash); const transactions = [ ...(await bc.listTransactions("*", 500)), - ...(await lq.listTransactions("*", 500)), + ...(await lq.listTransactions("*", 500)) ].filter( - (tx) => + tx => ["send", "receive"].includes(tx.category) && new Date(tx.time * 1000) > twoWeeksAgo ); missed = transactions.filter( - (tx) => + tx => tx.category === "receive" && tx.confirmations > 2 && hashes.includes(tx.txid) ); for (let i = 0; i < missed.length; i++) { - let p = unconfirmed.find((p) => p.hash === missed[i].txid); + let p = unconfirmed.find(p => p.hash === missed[i].txid); p.confirmed = 1; try { - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { l("confirming payment", p.id, p.user_id, p.hash, p.address); await p.save({ transaction }); @@ -242,11 +242,11 @@ const sync = async () => { store.unaccounted = []; - transactions.map((tx) => { + transactions.map(tx => { if ( - !payments.find((p) => p.hash === tx.txid) && + !payments.find(p => p.hash === tx.txid) && !store.exceptions.includes(tx.txid) && - !store.unaccounted.find((a) => a.txid === tx.txid) && + !store.unaccounted.find(a => a.txid === tx.txid) && new Date(tx.blocktime * 1000) >= twoWeeksAgo ) { store.unaccounted.push(tx); @@ -256,34 +256,34 @@ const sync = async () => { if (store.unaccounted.length) warn( "wallet transactions missing from database", - store.unaccounted.map((tx) => tx.txid) + store.unaccounted.map(tx => tx.txid) ); let receipts = store.unaccounted .filter( - (tx) => + tx => tx.category === "receive" && (!tx.asset || tx.asset === config.liquid.btcasset) ) - .map((tx) => tx.address); + .map(tx => tx.address); invoices = await db.Invoice.findAll({ where: { [Op.or]: { address: receipts, - unconfidential: receipts, - }, + unconfidential: receipts + } }, include: [ { model: db.Account, - as: "account", + as: "account" }, { model: db.User, - as: "user", - }, - ], + as: "user" + } + ] }); for (let i = 0; i < invoices.length; i++) { @@ -298,13 +298,13 @@ const sync = async () => { rate, tip, user, - user_id, + user_id } = invoices[i]; l("unaccounted", address); let pending = store.unaccounted.filter( - (tx) => tx.address === address || tx.address === unconfidential + tx => tx.address === address || tx.address === unconfidential ); for (let i = 0; i < pending.length; i++) { @@ -313,7 +313,7 @@ const sync = async () => { amount = toSats(amount); try { - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { l("found pending transaction", user.username, amount, hash); store.payments.push(hash); let p = await db.Payment.create( @@ -329,7 +329,7 @@ const sync = async () => { rate, received: true, tip, - user_id, + user_id }, { transaction } ); @@ -357,8 +357,8 @@ setTimeout(sync, 5000); app.get("/conversions", async (req, res) => { const transactions = [ ...(await bc.listTransactions("*", 500)), - ...(await lq.listTransactions("*", 500)), - ].filter((tx) => ["receive"].includes(tx.category)); + ...(await lq.listTransactions("*", 500)) + ].filter(tx => ["receive"].includes(tx.category)); const { addr } = req.query; const { invoices } = require("./invoices"); @@ -366,19 +366,19 @@ app.get("/conversions", async (req, res) => { let conversions = persist("data/conversions.json"); const payments = ( await db.Payment.findAll({ - attributes: ["hash"], + attributes: ["hash"] }) - ).map((p) => p.hash); + ).map(p => p.hash); res.send( Object.keys(conversions) - .filter((c) => conversions[c].address === addr) - .filter((c) => { + .filter(c => conversions[c].address === addr) + .filter(c => { return ( invoices.find( - (i) => i.payment_request === c && i.state === "SETTLED" - ) || transactions.find((tx) => tx.txid === c) + i => i.payment_request === c && i.state === "SETTLED" + ) || transactions.find(tx => tx.txid === c) ); }) - .filter((c) => !payments.includes(c)) + .filter(c => !payments.includes(c)) ); }); |
