diff options
| author | Adam Soltys <[email protected]> | 2020-09-12 15:47:00 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-09-12 15:47:00 -0700 |
| commit | 3ae6e9e46956416164c4540881a3acb81aa0c672 (patch) | |
| tree | f28870ff7c787a3d54527909da8893380c6d66a9 /lib | |
| parent | d712bf317f17e75f335c5c9a4a172f50c0ed4449 (diff) | |
| download | coinos-server-3ae6e9e46956416164c4540881a3acb81aa0c672.tar.gz coinos-server-3ae6e9e46956416164c4540881a3acb81aa0c672.zip | |
support rbf
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/buildliquidtx.js | 56 | ||||
| -rw-r--r-- | lib/buildtx.js | 4 |
2 files changed, 37 insertions, 23 deletions
diff --git a/lib/buildliquidtx.js b/lib/buildliquidtx.js index d84bdc0b..4dd3a16c 100644 --- a/lib/buildliquidtx.js +++ b/lib/buildliquidtx.js @@ -2,11 +2,20 @@ const axios = require("axios"); const liquid = require("liquidjs-lib"); const coinselect = require("coinselect"); const split = require("coinselect/split"); -const api = prod ? "https://blockstream.info/liquid/api" : "http://localhost:3002"; +const api = prod + ? "https://blockstream.info/liquid/api" + : "http://localhost:3002"; const SATS = 100000000; const network = prod ? liquid.networks["liquid"] : liquid.networks["regtest"]; -module.exports = async ({ address, asset, amount, feeRate, user }) => { +module.exports = async ({ + address, + asset, + amount, + feeRate, + replaceable, + user +}) => { const { btcasset: btc } = config.liquid; if (!feeRate) { @@ -17,13 +26,12 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { feeRate = Math.round(feeRate / 100); if (asset !== btc) feeRate = 0; - const payments = await db.Payment.findAll({ where: { received: true, account_id: user.account_id, - network: 'liquid', - }, + network: "liquid" + } }); let utxos = []; @@ -36,18 +44,21 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { } } - let feeInput = utxos.find((utxo) => utxo.asset === btc && utxo.value > 1000); - if (!feeInput) throw new Error("Insufficient balance, couldn't find an input to cover the fee"); + let feeInput = utxos.find(utxo => utxo.asset === btc && utxo.value > 1000); + if (!feeInput) + throw new Error( + "Insufficient balance, couldn't find an input to cover the fee" + ); - utxos = utxos.filter((utxo) => utxo.asset === asset); + utxos = utxos.filter(utxo => utxo.asset === asset); let feeChange = feeInput.value - 1000; let targets = [ { address, - value: amount, - }, + value: amount + } ]; let { inputs, outputs, fee } = coinselect(utxos, targets, feeRate); @@ -56,7 +67,7 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { "utxos", amount, feeRate, - utxos.map((u) => u.value), + utxos.map(u => u.value), inputs, outputs, fee @@ -82,7 +93,7 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { psbt = psbt.addOutput({ address, asset, - value, + value }); }); @@ -91,7 +102,7 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { psbt = psbt.addInput({ hash: txid, index: vout, - nonWitnessUtxo: Buffer.from((await lq.getTransaction(txid)).hex, "hex"), + nonWitnessUtxo: Buffer.from((await lq.getTransaction(txid)).hex, "hex") }); } @@ -100,25 +111,26 @@ module.exports = async ({ address, asset, amount, feeRate, user }) => { hash: feeInput.txid, index: feeInput.vout, nonWitnessUtxo: Buffer.from( - (await lq.getTransaction(feeInput.txid).hex), + await lq.getTransaction(feeInput.txid).hex, "hex" - ), + ) }); psbt.addOutput({ changeAddress, asset: btc, - value: feeChange, + value: feeChange }); } + psbt.addOutput({ + asset: btc, + script: Buffer.alloc(0), + value: fee, + nonce: Buffer.alloc(1, 0) + }); - psbt.addOutput({ - asset: btc, - script: Buffer.alloc(0), - value: fee, - nonce: Buffer.alloc(1, 0), - }); + if (replaceable) psbt.setInputSequence(0, 0xffffffff - 2); feeRate = Math.round(feeRate * 1000); psbt = psbt.toBase64(); diff --git a/lib/buildtx.js b/lib/buildtx.js index ef24ea19..0f63f641 100644 --- a/lib/buildtx.js +++ b/lib/buildtx.js @@ -8,7 +8,7 @@ const network = prod ? bitcoin.networks["bitcoin"] : bitcoin.networks["regtest"]; -module.exports = async ({ address, amount, feeRate, user }) => { +module.exports = async ({ address, amount, feeRate, replaceable, user }) => { if (!feeRate) feeRate = (await bc.estimateSmartFee(6)).feerate * SATS; feeRate = Math.round(feeRate / 1000); @@ -69,6 +69,8 @@ module.exports = async ({ address, amount, feeRate, user }) => { }); } + if (replaceable) psbt.setInputSequence(0, 0xffffffff - 2); + feeRate = Math.round(feeRate * 1000); psbt = psbt.toBase64(); |
