From 3ac47dfbda99f192201af63357e51efc01dde612 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 24 Sep 2024 20:12:32 -0700 Subject: handle txoutset retries --- lib/payments.ts | 96 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 43 deletions(-) (limited to 'lib') 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); - - let balance = await g(`balance:${id}`); - - 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; - } + try { + let { descriptors, id, uid, type } = account; + let user = await getUser(uid); + let node = rpc({ ...config[type], wallet: id }); - balance += inflight; + let progress = await node.scanTxOutSet("status"); + if (progress) return setTimeout(() => reconcile(account), 1000); - let amount = Math.abs(total - balance); - let hash = v4(); + let { total_amount } = await node.scanTxOutSet("start", descriptors); + let total = Math.round(total_amount * SATS); + let { balanceAdjustment: memo } = t(user); - 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, - }); + let balance = await g(`balance:${id}`); + + 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; + } + + 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); } }; -- cgit v1.2.3