diff options
| author | Adam Soltys <[email protected]> | 2023-09-08 23:51:37 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-09-08 23:51:37 -0700 |
| commit | e92451d8ad8ae88c5e97bb7436c06736d2dd189f (patch) | |
| tree | 81309d0e161923d11ff49960ffabb6cbf3e0fdb6 | |
| parent | a9907898057924f20a56fe1765fc572b8f0ba8a8 (diff) | |
| download | coinos-server-e92451d8ad8ae88c5e97bb7436c06736d2dd189f.tar.gz coinos-server-e92451d8ad8ae88c5e97bb7436c06736d2dd189f.zip | |
remove payment timeout
| -rw-r--r-- | routes/payments.js | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/routes/payments.js b/routes/payments.js index 43ff48ea..4b3cd25d 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -75,46 +75,42 @@ export default { p = await debit(payreq, total, maxfee, memo, user, types.lightning); - let reverse = async () => { - if ( - !(r && r.status === "complete" && r.payment_preimage) && - (await g(`payment:${p.id}`)) - ) { - await db.del(`payment:${p.id}`); + let check = async () => { + try { + let { pays } = await ln.listpays(payreq); - let credit = Math.round(total * config.fee) - p.ourfee; - warn("crediting balance", total + maxfee + p.ourfee); - await db.incrBy(`balance:${p.uid}`, total + maxfee + p.ourfee); + if ((!pays.length || pays[0].status === "failed") && (await g(`payment:${p.id}`))) { + await db.del(`payment:${p.id}`); - warn("crediting credits", credit); - await db.incrBy(`credit:${types.lightning}:${p.uid}`, credit); + let credit = Math.round(total * config.fee) - p.ourfee; + warn("crediting balance", total + maxfee + p.ourfee); + await db.incrBy(`balance:${p.uid}`, total + maxfee + p.ourfee); - warn("reversing payment", p.id); - await db.lRem(`${p.uid}:payments`, 1, p.id); + warn("crediting credits", credit); + await db.incrBy(`credit:${types.lightning}:${p.uid}`, credit); + + warn("reversing payment", p.id); + await db.lRem(`${p.uid}:payments`, 1, p.id); + } + } catch (e) { + console.log(e); + err("Failed to reverse payment", r); } }; - let timeout = setTimeout(reverse, 6000); + let interval = setInterval(check, 5000); try { l("paying lightning invoice", payreq); - let timeout = ms => - new Promise((_, f) => - setTimeout(() => f(new Error("Timed out")), ms) - ); - - r = await Promise.race([ - ln.pay({ - bolt11: payreq.replace(/\s/g, "").toLowerCase(), - amount_msat: amount_msat ? undefined : amount * 1000, - maxfee: maxfee * 1000, - retry_for: 5 - }), - timeout(5000) - ]); + r = await ln.pay({ + bolt11: payreq.replace(/\s/g, "").toLowerCase(), + amount_msat: amount_msat ? undefined : amount * 1000, + maxfee: maxfee * 1000, + retry_for: 5 + }); - if (r.status !== "complete" || !r.payment_preimage) + if (!(r.status === "complete" && r.payment_preimage)) fail("Payment did not complete"); p.amount = -amount; @@ -128,11 +124,12 @@ export default { l("refunding fee", maxfee, p.fee, maxfee - p.fee, p.ref); await db.incrBy(`balance:${p.uid}`, maxfee - p.fee); } catch (e) { - clearTimeout(timeout); warn("something went wrong", e.message); - await reverse(); + await check(); throw e; } + + clearInterval(interval); } } |
