diff options
| -rw-r--r-- | lib/migrate.js | 11 | ||||
| -rw-r--r-- | lib/payments.js | 121 |
2 files changed, 72 insertions, 60 deletions
diff --git a/lib/migrate.js b/lib/migrate.js index 1425cbac..38520ee9 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -6,14 +6,13 @@ import { wait } from "$lib/utils"; let queued = {}; let migrating = {}; async function migrate(id) { + try { if (id) id = id.replace(/\s/g, "").toLowerCase(); let user = await g(`user:${id}`); if (typeof user === "string") user = await g(`user:${user}`); - if (user) { - delete migrating[id]; - return user; - } + if (user) return user; + user = await ga(`user:${id}`); @@ -108,6 +107,10 @@ async function migrate(id) { delete migrating[id]; return user; } + } catch(e) { + delete migrating[id]; + throw e; + } } let keepMigrating = async () => { diff --git a/lib/payments.js b/lib/payments.js index cb5d4111..e83a80d8 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -349,79 +349,88 @@ export let sendOnchain = async (params) => { let node = getNode(type); let { txid } = tx; - if (config[type].walletpass) - await node.walletPassphrase(config[type].walletpass, 300); + try { + if (inflight[txid]) fail("payment in flight"); + inflight[txid] = true; - let { hex: signed } = await node.signRawTransactionWithWallet( - type === types.liquid ? await node.blindRawTransaction(hex) : hex, - ); + if (config[type].walletpass) + await node.walletPassphrase(config[type].walletpass, 300); - ({ txid } = await node.decodeRawTransaction(signed)); + let { hex: signed } = await node.signRawTransactionWithWallet( + type === types.liquid ? await node.blindRawTransaction(hex) : hex, + ); - let r = await node.testMempoolAccept([signed]); - if (!r[0].allowed) fail("transaction rejected"); + ({ txid } = await node.decodeRawTransaction(signed)); - let total = 0; - let fee = 0; - let change = 0; + let r = await node.testMempoolAccept([signed]); + if (!r[0].allowed) fail("transaction rejected"); - if (type === types.liquid) { - for (let { - asset, - scriptPubKey: { address, type }, - value, - } of tx.vout) { - if (asset !== config.liquid.btc) fail("only L-BTC supported"); - if (type === "fee") fee = sats(value); - else { - total += sats(value); + let total = 0; + let fee = 0; + let change = 0; - if (address) { - if ((await node.getAddressInfo(address)).ismine) { - change += sats(value); + if (type === types.liquid) { + for (let { + asset, + scriptPubKey: { address, type }, + value, + } of tx.vout) { + if (asset !== config.liquid.btc) fail("only L-BTC supported"); + if (type === "fee") fee = sats(value); + else { + total += sats(value); + + if (address) { + if ((await node.getAddressInfo(address)).ismine) { + change += sats(value); + } } } } - } - } else { - let totalIn = 0; - for await (let { txid, vout } of tx.vin) { - let hex = await node.getRawTransaction(txid); - let tx = await node.decodeRawTransaction(hex); - totalIn += sats(tx.vout[vout].value); - } + } else { + let totalIn = 0; + for await (let { txid, vout } of tx.vin) { + let hex = await node.getRawTransaction(txid); + let tx = await node.decodeRawTransaction(hex); + totalIn += sats(tx.vout[vout].value); + } - for (let { - scriptPubKey: { address }, - value, - } of tx.vout) { - total += sats(value); - if (await g(`invoice:${address}`)) - fail("Cannot send to internal address"); + for (let { + scriptPubKey: { address }, + value, + } of tx.vout) { + total += sats(value); + if (await g(`invoice:${address}`)) + fail("Cannot send to internal address"); - if ((await node.getAddressInfo(address)).ismine) change += sats(value); - } + if ((await node.getAddressInfo(address)).ismine) change += sats(value); + } - fee = totalIn - total; - } + fee = totalIn - total; + } - let amount = total - change; + let amount = total - change; - let p = await debit({ - hash: txid, - amount, - fee, - rate, - user, - type, - }); + let p = await debit({ + hash: txid, + amount, + fee, + rate, + user, + type, + }); - p.hex = signed; - await s(`payment:${p.id}`, p); + p.hex = signed; + await s(`payment:${p.id}`, p); - await node.sendRawTransaction(signed); + await node.sendRawTransaction(signed); - return p; + delete internal[txid]; + return p; + } catch (e) { + delete inflight[txid]; + throw e; + } }; let getMaxFee = (n) => |
