aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sync.js
diff options
context:
space:
mode:
authorDerrick Schwabe <[email protected]>2022-04-04 15:37:43 -0600
committerDerrick Schwabe <[email protected]>2022-04-04 15:37:43 -0600
commit858a2a10b6790c20d65824c672dcae9bf00e3b7e (patch)
tree45f27995beaecf9b6dd578b7a4d7c5892d23f285 /lib/sync.js
parent2fd68236f649b1c27703c15e494b242b79f9527f (diff)
parentd72de475b6621fb6ef31c03c2cf4d3ca1e82f5f2 (diff)
downloadcoinos-server-858a2a10b6790c20d65824c672dcae9bf00e3b7e.tar.gz
coinos-server-858a2a10b6790c20d65824c672dcae9bf00e3b7e.zip
Merge branch 'master' into stageUpdate2
Diffstat (limited to 'lib/sync.js')
-rw-r--r--lib/sync.js97
1 files changed, 47 insertions, 50 deletions
diff --git a/lib/sync.js b/lib/sync.js
index d7bdb4c5..d6adba2e 100644
--- a/lib/sync.js
+++ b/lib/sync.js
@@ -11,13 +11,13 @@ const init = async () => {
}
try {
- const twoDays = new Date(new Date().setDate(new Date().getDate() - 2));
+ const thirtyDays = new Date(new Date().setDate(new Date().getDate() - 30));
(
await db.Invoice.findAll({
where: {
createdAt: {
- [Op.gt]: twoDays
+ [Op.gt]: thirtyDays
}
},
include: {
@@ -33,22 +33,6 @@ const init = async () => {
console.log(e);
}
- try {
- const accounts = await db.Account.findAll({
- where: { pubkey: { [Op.ne]: null } },
- include: {
- model: db.User,
- as: "user"
- }
- });
-
- accounts.map(({ address, user: { username } }) => {
- addresses[address] = username;
- });
- } catch (e) {
- console.log(e);
- }
-
payments = (
await db.Payment.findAll({
attributes: ["hash"]
@@ -166,19 +150,20 @@ const sync = async () => {
let hashes = unconfirmed.map(p => p.hash);
const transactions = [
- ...(await bc.listTransactions("*", 50)),
- ...(await lq.listTransactions("*", 50))
+ ...(await bc.listTransactions("*", 500)),
+ ...(await lq.listTransactions("*", 500))
].filter(tx => ["send", "receive"].includes(tx.category));
missed = transactions.filter(
tx =>
tx.category === "receive" &&
- tx.confirmations > 0 &&
+ 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);
+ /*
p.confirmed = 1;
await db.transaction(async transaction => {
@@ -190,8 +175,9 @@ const sync = async () => {
emit(p.user.username, "account", p.account);
emit(p.user.username, "payment", p);
+ */
- l.info("confirmed tx", p.user_id, p.hash, p.address);
+ l.info("unconfirmed tx", p.user_id, p.hash, p.address);
}
unaccounted = [];
@@ -213,7 +199,9 @@ const sync = async () => {
let receipts = unaccounted
.filter(
- tx => tx.category === "receive" && tx.asset === config.liquid.btcasset
+ tx =>
+ tx.category === "receive" &&
+ (!tx.asset || tx.asset === config.liquid.btcasset)
)
.map(tx => tx.address);
@@ -251,45 +239,54 @@ const sync = async () => {
user_id
} = invoices[i];
- let { amount, txid: hash } = unaccounted.find(
+ l.info("unaccounted", address);
+
+ let pending = unaccounted.filter(
tx => tx.address === address || tx.address === unconfidential
);
- amount = toSats(amount);
+ for (let i = 0; i < pending.length; i++) {
+ let { amount, txid: hash } = pending[i];
- await db.transaction(async transaction => {
- 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 }
- );
+ amount = toSats(amount);
+
+ await db.transaction(async transaction => {
+ 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);
- });
+ l.info("found pending balance", user.username, amount);
+
+ emit(user.username, "account", account);
+ emit(user.username, "payment", p);
+ });
+ }
}
} catch (e) {
l.error("sync check failed", e.message, e.stack);
}
+
+ setTimeout(sync, 300000);
};
-//setTimeout(sync, 5000);
-//setInterval(sync, 300000);
+setTimeout(sync, 5000);
app.get(
"/conversions",