diff options
Diffstat (limited to 'lib/sync.js')
| -rw-r--r-- | lib/sync.js | 179 |
1 files changed, 101 insertions, 78 deletions
diff --git a/lib/sync.js b/lib/sync.js index e217dcd0..18ca5186 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -44,22 +44,23 @@ init(); const sync = async () => { const twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14)); - const payments = await db.Payment.findAll({ - attributes: ["hash", "confirmed", "preimage"], - where: { createdAt: { [Op.gt]: twoWeeksAgo } }, - include: [ - { - model: db.Account, - as: "account" - }, - { - model: db.User, - as: "user" - } - ] - }); try { + const payments = await db.Payment.findAll({ + attributes: ["hash", "confirmed", "preimage"], + where: { createdAt: { [Op.gt]: twoWeeksAgo } }, + include: [ + { + model: db.Account, + as: "account" + }, + { + model: db.User, + as: "user" + } + ] + }); + let { invoices } = await lnp.listInvoices({ reversed: true, num_max_invoices: 1000 @@ -89,45 +90,50 @@ const sync = async () => { let missed = recent.filter(i => !settled.includes(i.preimage)); for (let i = 0; i < missed.length; i++) { let { amount, preimage, pr: text, createdAt } = missed[i]; - let invoice = await db.Invoice.findOne({ - where: { text }, - include: { - model: db.User, - as: "user" - } - }); - if (invoice && invoice.user_id) { - let account = await db.Account.findOne({ - where: { - user_id: invoice.user_id, - asset: config.liquid.btcasset, - pubkey: null + try { + let invoice = await db.Invoice.findOne({ + where: { text }, + include: { + model: db.User, + as: "user" } }); - if (account) { - let payment = await db.Payment.create({ - 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 + if (invoice && invoice.user_id) { + let account = await db.Account.findOne({ + where: { + user_id: invoice.user_id, + asset: config.liquid.btcasset, + pubkey: null + } }); - await account.increment({ balance: amount }); + if (account) { + let payment = await db.Payment.create({ + 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 + }); + + await account.increment({ balance: amount }); - l.info("lightning missing from account", account.id, amount); + l.info("lightning missing from account", account.id, amount); + } } + } catch (e) { + l.error("problem syncing lightning payment", JSON.stringify(missed[i]), e.message, e.stack); } } @@ -152,7 +158,11 @@ const sync = async () => { const transactions = [ ...(await bc.listTransactions("*", 500)), ...(await lq.listTransactions("*", 500)) - ].filter(tx => ["send", "receive"].includes(tx.category) && new Date(tx.time * 1000) > twoWeeksAgo); + ].filter( + tx => + ["send", "receive"].includes(tx.category) && + new Date(tx.time * 1000) > twoWeeksAgo + ); missed = transactions.filter( tx => @@ -165,17 +175,21 @@ const sync = async () => { let p = unconfirmed.find(p => p.hash === missed[i].txid); p.confirmed = 1; - await db.transaction(async transaction => { - await p.save({ transaction }); + try { + await db.transaction(async transaction => { + await p.save({ transaction }); - await p.account.increment({ balance: p.amount }, { transaction }); - await p.account.decrement({ pending: p.amount }, { transaction }); - }); + await p.account.increment({ balance: p.amount }, { transaction }); + await p.account.decrement({ pending: p.amount }, { transaction }); + }); - emit(p.user.username, "account", p.account); - emit(p.user.username, "payment", p); + emit(p.user.username, "account", p.account); + emit(p.user.username, "payment", p); - l.info("unconfirmed tx", p.user_id, p.hash, p.address); + l.info("unconfirmed tx", p.user_id, p.hash, p.address); + } catch (e) { + l.error("problem confirming payment", p, e.message, e.stack); + } } unaccounted = []; @@ -250,32 +264,41 @@ const sync = async () => { amount = toSats(amount); - await db.transaction(async transaction => { - l.info("found pending transaction", user.username, amount, hash); - payments.push(hash); - let p = await db.Payment.create( - { - account_id, - address: address || unconfidential, - amount, - confirmed: false, - currency, - hash, - invoice_id, - network, - rate, - received: true, - tip, - user_id - }, - { transaction } - ); + try { + await db.transaction(async transaction => { + l.info("found pending transaction", user.username, amount, hash); + payments.push(hash); + let p = await db.Payment.create( + { + account_id, + address: address || unconfidential, + amount, + confirmed: false, + currency, + hash, + invoice_id, + network, + rate, + received: true, + tip, + user_id + }, + { transaction } + ); - await account.increment({ pending: amount }, { transaction }); + await account.increment({ pending: amount }, { transaction }); - emit(user.username, "account", account); - emit(user.username, "payment", p); - }); + emit(user.username, "account", account); + emit(user.username, "payment", p); + }); + } catch (e) { + l.error( + "problem updating pending transaction", + txid, + e.message, + e.stack + ); + } } } } catch (e) { |
