aboutsummaryrefslogtreecommitdiffstats
path: root/src/params/uuid.js
blob: 1278e59ed4e455a68398957399d1d28835abf642 (plain) (blame)
1
export let match = (p) => p.split('-').length > 4;
: #FFF0F0 } /* Literal.String */ .highlight .na { color: #369 } /* Name.Attribute */ .highlight .nb { color: #038 } /* Name.Builtin */ .highlight .nc { color: #B06; font-weight: bold } /* Name.Class */ .highlight .no { color: #036; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555 } /* Name.Decorator */ .highlight .ne { color: #B06; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #06B; font-weight: bold } /* Name.Function */ .highlight .nl { color: #369; font-style: italic } /* Name.Label */ .highlight .nn { color: #B06; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #369; font-weight: bold } /* Name.Property */ .highlight .nt { color: #B06; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #369 } /* Name.Variable */ .highlight .ow { color: #080 } /* Operator.Word */ .highlight .w { color: #BBB } /* Text.Whitespace */ .highlight .mb { color: #00D; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #00D; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #00D; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #00D; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #00D; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #D20; background-color: #FFF0F0 } /* Literal.String.Affix */ .highlight .sb { color: #D20; background-color: #FFF0F0 } /* Literal.String.Backtick */ .highlight .sc { color: #D20; background-color: #FFF0F0 } /* Literal.String.Char */ .highlight .dl { color: #D20; background-color: #FFF0F0 } /* Literal.String.Delimiter */ .highlight .sd { color: #D20; background-color: #FFF0F0 } /* Literal.String.Doc */ .highlight .s2 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Double */ .highlight .se { color: #04D; background-color: #FFF0F0 } /* Literal.String.Escape */ .highlight .sh { color: #D20; background-color: #FFF0F0 } /* Literal.String.Heredoc */ .highlight .si { color: #33B; background-color: #FFF0F0 } /* Literal.String.Interpol */ .highlight .sx { color: #2B2; background-color: #F0FFF0 } /* Literal.String.Other */ .highlight .sr { color: #080; background-color: #FFF0FF } /* Literal.String.Regex */ .highlight .s1 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Single */ .highlight .ss { color: #A60; background-color: #FFF0F0 } /* Literal.String.Symbol */ .highlight .bp { color: #038 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #06B; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #369 } /* Name.Variable.Class */ .highlight .vg { color: #D70 } /* Name.Variable.Global */ .highlight .vi { color: #33B } /* Name.Variable.Instance */ .highlight .vm { color: #369 } /* Name.Variable.Magic */ .highlight .il { color: #00D; font-weight: bold } /* Literal.Number.Integer.Long */
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 SATS = 100000000;
const network = prod ? liquid.networks["liquid"] : liquid.networks["regtest"];

module.exports = async ({
  address,
  asset,
  amount,
  feeRate,
  replaceable,
  user
}) => {
  const { btcasset: btc } = config.liquid;

  if (!feeRate) {
    let { feerate } = await lq.estimateSmartFee(6);
    if (!feerate) feerate = 0.00002;
    feeRate = feerate * SATS;
  }
  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"
    }
  });

  let utxos = [];
  for (let i = 0; i < payments.length; i++) {
    let url = `${api}/address/${payments[i].address}/utxo`;
    try {
      utxos.push(...(await axios.get(url)).data);
    } catch (e) {
      l.error("problem fetching utxos", e.message, url);
    }
  }

  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);

  let feeChange = feeInput.value - 1000;

  let targets = [
    {
      address,
      value: amount
    }
  ];

  let { inputs, outputs, fee } = coinselect(utxos, targets, feeRate);
  if (!inputs || !outputs) {
    l.info(
      "utxos",
      amount,
      feeRate,
      utxos.map(u => u.value),
      inputs,
      outputs,
      fee
    );

    throw new Error(
      "Couldn't find inputs for transaction, check your balance/fee"
    );
  }

  let psbt = new liquid.Psbt({ network });
  let total = 0;

  let { address: changeAddress } = payments[payments.length - 1];

  outputs.map(({ address, value }) => {
    total += value;
    if (!address) {
      address = changeAddress;
      change.push(address);
    }

    psbt = psbt.addOutput({
      address,
      asset,
      value
    });
  });

  for (let i = 0; i < inputs.length; i++) {
    let { txid, vout } = inputs[i];
    psbt = psbt.addInput({
      hash: txid,
      index: vout,
      nonWitnessUtxo: Buffer.from((await lq.getTransaction(txid)).hex, "hex")
    });
  }

  if (asset !== btc) {
    psbt.addInput({
      hash: feeInput.txid,
      index: feeInput.vout,
      nonWitnessUtxo: Buffer.from(
        await lq.getTransaction(feeInput.txid).hex,
        "hex"
      )
    });

    psbt.addOutput({
      changeAddress,
      asset: btc,
      value: feeChange
    });
  }

  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();

  return { tx: psbt, feeRate };
};