diff options
Diffstat (limited to 'lib/sync.js')
| -rw-r--r-- | lib/sync.js | 154 |
1 files changed, 84 insertions, 70 deletions
diff --git a/lib/sync.js b/lib/sync.js index d4900c58..eeff7362 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -17,7 +17,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) { @@ -31,13 +31,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; @@ -50,9 +50,9 @@ const init = async () => { store.payments = ( await db.Payment.findAll({ - attributes: ["hash"] + attributes: ["hash"], }) - ).map(p => p.hash); + ).map((p) => p.hash); }; init(); @@ -67,13 +67,13 @@ const sync = async () => { include: [ { model: db.Account, - as: "account" + as: "account", }, { model: db.User, - as: "user" - } - ] + as: "user", + }, + ], }); let missed, invoices, recent; @@ -82,47 +82,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.status === "paid" && Date.now() - i.paid_at < 60 * 60 * 1000 + (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; @@ -132,9 +134,9 @@ const sync = async () => { where: { text }, include: { model: db.User, - as: "user" + as: "user", }, - transaction + transaction, }); if (invoice && invoice.user_id) { @@ -142,12 +144,28 @@ const sync = async () => { where: { user_id: invoice.user_id, asset: config.liquid.btcasset, - pubkey: null + pubkey: null, }, - transaction + transaction, }); if (account) { + console.log("CREATING", { + account_id: account.id, + user_id: invoice.user_id, + hash: text, + amount, + currency: invoice.currency, + preimage, + rate: invoice.rate, + received: true, + confirmed: true, + network: "lightning", + tip: invoice.tip, + invoice_id: invoice.id, + createdAt, + updatedAt: createdAt, + }); let payment = await db.Payment.create({ account_id: account.id, user_id: invoice.user_id, @@ -162,7 +180,7 @@ const sync = async () => { tip: invoice.tip, invoice_id: invoice.id, createdAt, - updatedAt: createdAt + updatedAt: createdAt, }); await invoice.increment({ received: amount }, { transaction }); @@ -172,60 +190,56 @@ const sync = async () => { } } } catch (e) { - err( - "problem syncing lightning payment", - JSON.stringify(missed[i]), - e.message, - e.stack - ); + console.log(e); + err("problem syncing lightning payment", JSON.stringify(missed[i])); } } }); 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 }); @@ -243,11 +257,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); @@ -257,34 +271,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++) { @@ -299,13 +313,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++) { @@ -314,7 +328,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( @@ -330,7 +344,7 @@ const sync = async () => { rate, received: true, tip, - user_id + user_id, }, { transaction } ); @@ -358,8 +372,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"); @@ -367,19 +381,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)) ); }); |
