aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-24 20:12:32 -0700
committerAdam Soltys <[email protected]>2024-09-24 20:12:32 -0700
commit3ac47dfbda99f192201af63357e51efc01dde612 (patch)
tree8add213fcd414ebdd33a57e58e2a221a861c768e
parent80365c227c5a774ad65f5dad8b989e1966630160 (diff)
downloadcoinos-server-3ac47dfbda99f192201af63357e51efc01dde612.tar.gz
coinos-server-3ac47dfbda99f192201af63357e51efc01dde612.zip
handle txoutset retries
-rw-r--r--lib/payments.ts92
-rw-r--r--routes/payments.ts2
-rw-r--r--routes/users.ts2
3 files changed, 53 insertions, 43 deletions
diff --git a/lib/payments.ts b/lib/payments.ts
index 9d2d9b79..9aaccf39 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -17,6 +17,7 @@ import {
sats,
formatReceipt,
t,
+ wait,
} from "$lib/utils";
import { callWebhook } from "$lib/webhooks";
import got from "got";
@@ -442,7 +443,6 @@ export let sendOnchain = async (params) => {
let amount = total - change;
- console.log("debiting", aid, amount);
let p = await debit({
aid,
hash: txid,
@@ -769,50 +769,60 @@ export let catchUp = async () => {
};
export let reconcile = async (account) => {
- let { descriptors, id, uid, type } = account;
- let user = await getUser(uid);
- let node = rpc({ ...config[type], wallet: id });
- let { total_amount } = await node.scanTxOutSet("start", descriptors);
- let total = Math.round(total_amount * SATS);
- let { balanceAdjustment: memo } = t(user);
+ try {
+ let { descriptors, id, uid, type } = account;
+ let user = await getUser(uid);
+ let node = rpc({ ...config[type], wallet: id });
- let balance = await g(`balance:${id}`);
+ let progress = await node.scanTxOutSet("status");
+ if (progress) return setTimeout(() => reconcile(account), 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);
+ let total = Math.round(total_amount * SATS);
+ let { balanceAdjustment: memo } = t(user);
- balance += inflight;
+ let balance = await g(`balance:${id}`);
- let amount = Math.abs(total - balance);
- let hash = v4();
+ 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;
+ }
- if (total > balance) {
- let inv = {
- memo,
- type: types.reconcile,
- hash,
- amount,
- uid,
- aid: id,
- };
- await s(`invoice:${hash}`, inv);
- await credit({
- hash,
- amount,
- type: types.reconcile,
- aid: id,
- });
- } else if (total < balance) {
- await debit({
- aid: id,
- amount,
- hash: v4(),
- memo,
- user,
- type: types.reconcile,
- });
+ balance += inflight;
+
+ let amount = Math.abs(total - balance);
+ let hash = v4();
+
+ if (total > balance) {
+ let inv = {
+ memo,
+ type: types.reconcile,
+ hash,
+ amount,
+ uid,
+ aid: id,
+ };
+ await s(`invoice:${hash}`, inv);
+ await credit({
+ hash,
+ amount,
+ type: types.reconcile,
+ aid: id,
+ });
+ } else if (total < balance) {
+ await debit({
+ aid: id,
+ amount,
+ hash: v4(),
+ memo,
+ user,
+ type: types.reconcile,
+ });
+ }
+ } catch (e) {
+ if (e.message.includes("progress"))
+ return setTimeout(() => reconcile(account), 1000);
+ warn("problem reconciling", e.message, account);
}
};
diff --git a/routes/payments.ts b/routes/payments.ts
index d4993b87..81e76085 100644
--- a/routes/payments.ts
+++ b/routes/payments.ts
@@ -279,7 +279,7 @@ export default {
await s(`payment:${p.id}`, p);
if (aid) await db.sRem(`inflight:${aid}`, p.id);
} else {
- if (aid) await db.sAdd(`inflight:${aid}`, p.id);
+ if (aid) await db.sAdd(`inflight:${aid}`, p.id);
}
emit(p.uid, "payment", p);
diff --git a/routes/users.ts b/routes/users.ts
index 34d20584..26a4b8f3 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -602,7 +602,7 @@ export default {
let account = await g(`account:${id}`);
account.balance = await g(`balance:${id}`);
- if (account.seed) await reconcile(account);
+ if (account.seed) reconcile(account);
accounts.push(account);
}