aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-25 20:19:41 -0700
committerAdam Soltys <[email protected]>2024-09-25 20:19:41 -0700
commit8c07db5c9f944c82fab31c2af1eeb132aa2245c9 (patch)
treea5d8c59ef82e626bfa09448dfac33592f66d6799
parent4c287cfd82acd5fd2ca4ec5816e1434b00e80fdb (diff)
downloadcoinos-server-8c07db5c9f944c82fab31c2af1eeb132aa2245c9.tar.gz
coinos-server-8c07db5c9f944c82fab31c2af1eeb132aa2245c9.zip
reconcile with balance
-rw-r--r--lib/payments.ts27
-rw-r--r--routes/users.ts2
2 files changed, 14 insertions, 15 deletions
diff --git a/lib/payments.ts b/lib/payments.ts
index d20b381e..4c91266f 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -767,28 +767,27 @@ export let catchUp = async () => {
setTimeout(catchUp, 10000);
};
-export let reconcile = async (account) => {
+export let reconcile = async (account, initial = false) => {
try {
let { descriptors, id, uid, type } = account;
let user = await getUser(uid);
let node = rpc({ ...config[type], wallet: id });
- let progress = await node.scanTxOutSet("status");
- if (progress) return setTimeout(() => reconcile(account), 1000);
+ let total;
- let { total_amount } = await node.scanTxOutSet("start", descriptors);
- let total = Math.round(total_amount * SATS);
- let { balanceAdjustment: memo } = t(user);
-
- let balance = await g(`balance:${id}`);
+ if (initial) {
+ let progress = await node.scanTxOutSet("status");
+ if (progress) return setTimeout(() => reconcile(account, initial), 1000);
- let inflight = 0;
- for (let pid of await db.sMembers(`inflight:${id}`)) {
- let p = await g(`payment:${pid}`);
- inflight += Math.abs(p.amount) + p.fee;
+ let { total_amount } = await node.scanTxOutSet("start", descriptors);
+ total = Math.round(total_amount * SATS);
+ } else {
+ total = Math.round((await node.getBalance()) * SATS);
}
- balance += inflight;
+ let { balanceAdjustment: memo } = t(user);
+
+ let balance = await g(`balance:${id}`);
let amount = Math.abs(total - balance);
let hash = v4();
@@ -823,6 +822,6 @@ export let reconcile = async (account) => {
console.log(e);
warn("problem reconciling", e.message, account);
if (e.message.includes("progress"))
- return setTimeout(() => reconcile(account), 1000);
+ return setTimeout(() => reconcile(account, initial), 1000);
}
};
diff --git a/routes/users.ts b/routes/users.ts
index a7efa76d..d274d61d 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -657,7 +657,7 @@ export default {
.lPush(`${user.id}:accounts`, id)
.exec();
- await reconcile(account);
+ reconcile(account, true);
await appendFile("/bitcoin.conf", `wallet=${id}\n`);