aboutsummaryrefslogtreecommitdiffstats
path: root/lib/buildliquidtx.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-08 21:05:34 -0800
committerAdam Soltys <[email protected]>2023-01-08 21:05:34 -0800
commitdce07aa93b3c2f47aca0c24cea6adde9ec37f1a7 (patch)
tree6c6b8930ebe9c2cc5b8d1f759e61260f509fdd7a /lib/buildliquidtx.js
parentf7b844883506a8072213245b9b8a2b988694e4f5 (diff)
downloadcoinos-server-dce07aa93b3c2f47aca0c24cea6adde9ec37f1a7.tar.gz
coinos-server-dce07aa93b3c2f47aca0c24cea6adde9ec37f1a7.zip
tech debt jubilee
Diffstat (limited to 'lib/buildliquidtx.js')
-rw-r--r--lib/buildliquidtx.js160
1 files changed, 0 insertions, 160 deletions
diff --git a/lib/buildliquidtx.js b/lib/buildliquidtx.js
deleted file mode 100644
index 4aad18f4..00000000
--- a/lib/buildliquidtx.js
+++ /dev/null
@@ -1,160 +0,0 @@
-import config from "$config";
-import db from "$db";
-import store from "./store";
-import { prod } from "./utils";
-import axios from "axios";
-import liquid from "liquidjs-lib";
-import coinselect from "coinselect";
-import split from "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"];
-import { Op } from "@sequelize/core";
-
-export default 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;
-
- let payments;
- if (asset === btc) {
- payments = await db.Payment.findAll({
- where: {
- received: true,
- account_id: user.account_id,
- network: "liquid"
- }
- });
- } else {
- payments = await db.Payment.findAll({
- where: {
- "$account.pubkey": { [Op.ne]: null },
- received: true,
- user_id: user.id,
- network: "liquid"
- },
- include: {
- model: db.Account,
- as: "account"
- }
- });
- }
-
- 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) {
- err("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(
- "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;
- store.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 };
-};