aboutsummaryrefslogtreecommitdiffstats
path: root/estimateLiquidFee.js
blob: fc1655b60547907cb4e645b7a27234d9252154bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const config = require("./config");
const BitcoinCore = require("bitcoin-core");

const l = require("pino")();
const SATS = 100000000;

const bc = new BitcoinCore(config.liquid);

module.exports = (app, db, emit) => async (req, res) => {
  let { user } = req;
  let { address, amount, confTarget, feeRate, mode } = req.body;
  let tx, fee;

  try {
    amount = parseInt(amount);

    tx = await bc.createRawTransaction([], {
      [address]: (amount / SATS).toFixed(8)
    });

    let params = {
      subtractFeeFromOutputs: amount === user.balance ? [0] : []
    } 

    if (feeRate) params["feeRate"] = (parseInt(feeRate) / SATS * 1000).toFixed(8);
    if (confTarget) params["conf_target"] = parseInt(confTarget);
    if (mode) params["estimate_mode"] = mode;

    tx = await bc.fundRawTransaction(tx, params);
    res.send({ tx });
  } catch (e) {
    l.error(e);
    return res.status(500).send(e.message);
  }
};