aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sync.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-09-20 22:48:18 -0700
committerAdam Soltys <[email protected]>2022-09-20 22:48:18 -0700
commit4569736bac949bd2b06ce03b11299b11984e8d3c (patch)
treed4e7c71c5be97903a43e0e25f816b2a4b9fde0a5 /lib/sync.js
parentf2fb6b587bb171101b0f91722c0c29e358b386e0 (diff)
parentb53875cf6bdf47c359139aa846c6597fa3627c87 (diff)
downloadcoinos-server-4569736bac949bd2b06ce03b11299b11984e8d3c.tar.gz
coinos-server-4569736bac949bd2b06ce03b11299b11984e8d3c.zip
Merge branch 'master' into prod
Diffstat (limited to 'lib/sync.js')
-rw-r--r--lib/sync.js131
1 files changed, 67 insertions, 64 deletions
diff --git a/lib/sync.js b/lib/sync.js
index 467ce107..95abb99d 100644
--- a/lib/sync.js
+++ b/lib/sync.js
@@ -75,82 +75,85 @@ const sync = async () => {
]
});
- let { invoices } = await getInvoices({ lnd, limit: 1000 });
+ let missed, invoices;
+ if (config.lnd) {
+ ({ invoices } = await getInvoices({ lnd, limit: 1000 }));
- let recent = invoices
- .filter(
- i =>
- i.is_confirmed &&
- differenceInDays(new Date(), new Date(i.created_at)) < 2
- )
- .map(i => ({
- amount: parseInt(i.received),
- preimage: i.secret,
- pr: i.request,
- createdAt: new Date(i.created_at)
- }));
-
- 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"]
- })
- ).map(p => p.preimage);
+ let recent = invoices
+ .filter(
+ i =>
+ i.is_confirmed &&
+ differenceInDays(new Date(), new Date(i.created_at)) < 2
+ )
+ .map(i => ({
+ amount: parseInt(i.received),
+ preimage: i.secret,
+ pr: i.request,
+ createdAt: new Date(i.created_at)
+ }));
- 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];
- if (!text || !text.length) continue;
+ 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"]
+ })
+ ).map(p => p.preimage);
- try {
- let invoice = await db.Invoice.findOne({
- where: { text },
- include: {
- model: db.User,
- as: "user"
- }
- });
+ 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;
- 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("lightning missing from account", account.id, amount);
+ l("lightning missing from account", account.id, amount);
+ }
}
+ } catch (e) {
+ err(
+ "problem syncing lightning payment",
+ JSON.stringify(missed[i]),
+ e.message,
+ e.stack
+ );
}
- } catch (e) {
- err(
- "problem syncing lightning payment",
- JSON.stringify(missed[i]),
- e.message,
- e.stack
- );
}
}