aboutsummaryrefslogtreecommitdiffstats
path: root/lib/payments.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-03-05 15:12:55 -0800
committerAdam Soltys <[email protected]>2024-03-05 15:12:55 -0800
commitc1641e80d3dc113e4adbc48c1bc8e54a5fd68f7c (patch)
treef98e32ce0e52ff790b3aee052126778e39387881 /lib/payments.js
parent5112b3f44d3eb1967f65d0f214a78bf9022806eb (diff)
downloadcoinos-server-c1641e80d3dc113e4adbc48c1bc8e54a5fd68f7c.tar.gz
coinos-server-c1641e80d3dc113e4adbc48c1bc8e54a5fd68f7c.zip
locks
Diffstat (limited to 'lib/payments.js')
-rw-r--r--lib/payments.js121
1 files changed, 65 insertions, 56 deletions
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) =>