From 1ae4c03116ce3a650af88f78a4b5d19581238c71 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 9 Mar 2020 10:59:04 -0700 Subject: fee estimation --- sendLiquid.js | 58 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 24 deletions(-) (limited to 'sendLiquid.js') diff --git a/sendLiquid.js b/sendLiquid.js index 855a2617..a86395a2 100644 --- a/sendLiquid.js +++ b/sendLiquid.js @@ -4,35 +4,43 @@ const reverse = require("buffer-reverse"); const BitcoinCore = require("bitcoin-core"); const l = require("pino")(); -const SATS = 100000000; +const toSats = n => parseInt((n * 100000000).toFixed()) const bc = new BitcoinCore(config.liquid); -module.exports = (app, db, emit) => async (req, res) => { - let { address, amount } = req.body; - let rawtx, hex, fee, total; +module.exports = (addresses, app, db, emit) => async (req, res) => { let { user } = req; + let { + address, + tx: { hex } + } = req.body; + + const isChange = async address => + !((await bc.getAddressInfo(address)).ismine) || + !Object.keys(addresses).includes(address) || + address === user.liquid; + + const unblinded = await bc.unblindRawTransaction(hex); + tx = await bc.decodeRawTransaction(unblinded.hex); + + let total = 0; + let change = 0; + let fee = 0; + + for (let i = 0; i < tx.vout.length; i++) { + let o = tx.vout[i]; + total += toSats(o.value); + if (o.scriptPubKey.type === 'fee') fee = toSats(o.value); + + if (o.scriptPubKey.addresses) { + if ((await isChange(o.scriptPubKey.addresses[0]))) { + change += toSats(o.value); + } + } + } - try { - amount = parseInt(amount); - - rawtx = await bc.createRawTransaction([], { - [address]: (amount / SATS).toFixed(8) - }); - - rawtx = await bc.fundRawTransaction(rawtx, { - subtractFeeFromOutputs: amount === user.balance ? [0] : [] - }); - - hex = await bc.blindRawTransaction(rawtx.hex); - - ({ fee } = rawtx); - fee = parseInt(fee * SATS); - total = amount + fee; - } catch (e) { - l.error("funding failed", e); - return res.status(500).send(e.message); - } + total = total - change; + let amount = total - fee; try { await db.transaction(async transaction => { @@ -61,12 +69,14 @@ module.exports = (app, db, emit) => async (req, res) => { if (config.liquid.walletpass) await bc.walletPassphrase(config.liquid.walletpass, 300); + hex = await bc.blindRawTransaction(hex); rawtx = (await bc.signRawTransactionWithWallet(hex)).hex; let txid = await bc.sendRawTransaction(rawtx); await db.transaction(async transaction => { await user.save({ transaction }); emit(user.username, "user", user); + l.info("total", total); const payment = await db.Payment.create( { -- cgit v1.2.3