aboutsummaryrefslogtreecommitdiffstats
path: root/routes/bitcoin
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-09-02 14:47:08 -0700
committerAdam Soltys <[email protected]>2022-09-02 14:47:14 -0700
commit3161dd7ee95b6c7d70f8341298dd4fab84cda8b0 (patch)
tree16cd6c55d5dc481331b1e8b640d179f193e7ea13 /routes/bitcoin
parent7a8d64833de8873afb92982b3f5bfff59b30c542 (diff)
downloadcoinos-server-3161dd7ee95b6c7d70f8341298dd4fab84cda8b0.tar.gz
coinos-server-3161dd7ee95b6c7d70f8341298dd4fab84cda8b0.zip
es6 migration
Diffstat (limited to 'routes/bitcoin')
-rw-r--r--routes/bitcoin/broadcast.js4
-rw-r--r--routes/bitcoin/conversionFee.js7
-rw-r--r--routes/bitcoin/fee.js27
-rw-r--r--routes/bitcoin/generate.js4
-rw-r--r--routes/bitcoin/index.js13
-rw-r--r--routes/bitcoin/receive.js4
-rw-r--r--routes/bitcoin/send.js65
-rw-r--r--routes/bitcoin/sweep.js15
8 files changed, 91 insertions, 48 deletions
diff --git a/routes/bitcoin/broadcast.js b/routes/bitcoin/broadcast.js
index 5ee7d6bb..157d60a4 100644
--- a/routes/bitcoin/broadcast.js
+++ b/routes/bitcoin/broadcast.js
@@ -1,4 +1,4 @@
-export default ah(async (req, res) => {
+export default async (req, res) => {
try {
const { user } = req;
let hash = await bc.sendRawTransaction(req.body.tx);
@@ -47,4 +47,4 @@ export default ah(async (req, res) => {
} catch (e) {
res.status(500).send(e.message);
}
-});
+};
diff --git a/routes/bitcoin/conversionFee.js b/routes/bitcoin/conversionFee.js
index fa7904b1..8a6bfce6 100644
--- a/routes/bitcoin/conversionFee.js
+++ b/routes/bitcoin/conversionFee.js
@@ -2,14 +2,11 @@
// This is in a separate file so that the same function can be used by ./send and ./receive
const conversionFeeMultiplier = 0.01;
-const conversionFeeReceiver = "coinosfees"; // account that receives the fees
+export const conversionFeeReceiver = "coinosfees"; // account that receives the fees
/**
* Returns the fee you have to pay for withdrawing 'amount' bitcoin.
*/
-function computeConversionFee(amount) {
+export function computeConversionFee(amount) {
return Math.floor(amount * conversionFeeMultiplier);
}
-
-module.exports.conversionFeeReceiver = conversionFeeReceiver;
-module.exports.computeConversionFee = computeConversionFee;
diff --git a/routes/bitcoin/fee.js b/routes/bitcoin/fee.js
index 20dc8e9a..8efb59f8 100644
--- a/routes/bitcoin/fee.js
+++ b/routes/bitcoin/fee.js
@@ -1,6 +1,6 @@
-import buildTx from '../../lib/buildtx';
+import buildTx from "../../lib/buildtx.js";
-export default ah(async (req, res) => {
+export default async (req, res) => {
let { user } = req;
let { address, amount, feeRate, replaceable } = req.body;
let tx, fee;
@@ -9,9 +9,9 @@ export default ah(async (req, res) => {
try {
let psbt = await buildTx({ address, amount, feeRate, replaceable, user });
return res.send(psbt);
- } catch(e) {
+ } catch (e) {
return res.status(500).send(e.message);
- }
+ }
}
let invoice = await db.Invoice.findOne({
@@ -19,8 +19,8 @@ export default ah(async (req, res) => {
include: {
attributes: ["username"],
model: db.User,
- as: "user",
- },
+ as: "user"
+ }
});
if (invoice) {
@@ -34,13 +34,18 @@ export default ah(async (req, res) => {
try {
amount = parseInt(amount);
- let partial = await bc.createRawTransaction([], {
- [address]: (amount / SATS).toFixed(8),
- }, 0, replaceable);
+ let partial = await bc.createRawTransaction(
+ [],
+ {
+ [address]: (amount / SATS).toFixed(8)
+ },
+ 0,
+ replaceable
+ );
let params = {
subtractFeeFromOutputs: amount === user.balance ? [0] : [],
- replaceable,
+ replaceable
};
if (feeRate) params.feeRate = (feeRate / SATS).toFixed(8);
@@ -59,4 +64,4 @@ export default ah(async (req, res) => {
l.error("bitcoin fee estimation error", e);
return res.status(500).send(e.message);
}
-});
+};
diff --git a/routes/bitcoin/generate.js b/routes/bitcoin/generate.js
index 92df1e12..1b6c1aa4 100644
--- a/routes/bitcoin/generate.js
+++ b/routes/bitcoin/generate.js
@@ -1,5 +1,5 @@
-export default ah(async (req, res) => {
+export default async (req, res) => {
let address = await bc.getNewAddress();
await bc.generateToAddress(1, address);
res.end();
-});
+};
diff --git a/routes/bitcoin/index.js b/routes/bitcoin/index.js
new file mode 100644
index 00000000..9d096e8f
--- /dev/null
+++ b/routes/bitcoin/index.js
@@ -0,0 +1,13 @@
+import broadcast from "./broadcast.js";
+import generate from "./generate.js";
+import sweep from "./sweep.js";
+import fee from "./fee.js";
+import send from "./send.js";
+
+export default {
+ broadcast,
+ generate,
+ sweep,
+ fee,
+ send
+};
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index ac9aa1e7..65e96883 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -1,6 +1,8 @@
+import { notify } from "../lib/notifications.js";
+import { callWebhook } from "../lib/webhooks.js";
import reverse from 'buffer-reverse';
import zmq from 'zeromq/v5-compat';
-import { Op } from 'sequelize';
+import { Op } from '@sequelize/core';
import { fromBase58 } from 'bip32';
import bitcoin from 'bitcoinjs-lib';
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index 85117dbb..d8406075 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -1,7 +1,11 @@
+import config from "../../config/index.js";
const btc = config.liquid.btcasset;
-import { computeConversionFee, conversionFeeReceiver } from './conversionFee.js';
+import {
+ computeConversionFee,
+ conversionFeeReceiver
+} from "./conversionFee.js";
-export default ah(async (req, res) => {
+export default async (req, res) => {
let { user } = req;
let { address, memo, tx } = req.body;
let { hex } = tx;
@@ -61,7 +65,10 @@ export default ah(async (req, res) => {
// use user's credits to reduce fee, if available
let conversionFeeDeduction = Math.min(account.btc_credits, conversionFee);
if (conversionFeeDeduction) {
- await account.decrement({ btc_credits: conversionFeeDeduction }, { transaction });
+ await account.decrement(
+ { btc_credits: conversionFeeDeduction },
+ { transaction }
+ );
await account.reload({ transaction });
conversionFee -= conversionFeeDeduction;
}
@@ -70,11 +77,19 @@ export default ah(async (req, res) => {
l.error("amount exceeds balance", amount, fee, account.balance);
throw new Error("low balance");
} else if (total + conversionFee > account.balance) {
- l.error("total (after conversion fee) exceeds balance", amount, fee, account.balance);
+ l.error(
+ "total (after conversion fee) exceeds balance",
+ amount,
+ fee,
+ account.balance
+ );
throw new Error("low balance (after conversion fee)");
}
- await account.decrement({ balance: (total + conversionFee) }, { transaction });
+ await account.decrement(
+ { balance: total + conversionFee },
+ { transaction }
+ );
await account.reload({ transaction });
let receiverAccount = await db.Account.findOne({
@@ -85,7 +100,7 @@ export default ah(async (req, res) => {
{
model: db.User,
as: "user"
- },
+ }
],
lock: transaction.LOCK.UPDATE,
transaction
@@ -94,20 +109,26 @@ export default ah(async (req, res) => {
let fee_payment;
let fee_payment_id = null;
if (conversionFee) {
- await receiverAccount.increment({ balance: conversionFee }, { transaction });
+ await receiverAccount.increment(
+ { balance: conversionFee },
+ { transaction }
+ );
await receiverAccount.reload({ transaction });
- fee_payment = await db.Payment.create({
- amount: conversionFee,
- fee: 0,
- memo: "Bitcoin conversion fee",
- account_id: receiverAccount.id,
- user_id: receiverAccount.user_id,
- rate: app.get("rates")[receiverAccount.user.currency],
- currency: receiverAccount.user.currency,
- confirmed: true,
- received: true,
- network: "COINOS"
- }, { transaction });
+ fee_payment = await db.Payment.create(
+ {
+ amount: conversionFee,
+ fee: 0,
+ memo: "Bitcoin conversion fee",
+ account_id: receiverAccount.id,
+ user_id: receiverAccount.user_id,
+ rate: app.get("rates")[receiverAccount.user.currency],
+ currency: receiverAccount.user.currency,
+ confirmed: true,
+ received: true,
+ network: "COINOS"
+ },
+ { transaction }
+ );
fee_payment_id = fee_payment.id;
}
@@ -146,9 +167,11 @@ export default ah(async (req, res) => {
l.info("sent bitcoin", user.username, total);
});
} catch (e) {
- if (e.message.includes("Insufficient")) e.message = "The coinos server hot wallet has insufficient funds to complete the payment, try again later";
+ if (e.message.includes("Insufficient"))
+ e.message =
+ "The coinos server hot wallet has insufficient funds to complete the payment, try again later";
l.error("error sending bitcoin", e.message);
console.log(e);
return res.status(500).send(e.message);
}
-});
+};
diff --git a/routes/bitcoin/sweep.js b/routes/bitcoin/sweep.js
index f056bdeb..6c0ca580 100644
--- a/routes/bitcoin/sweep.js
+++ b/routes/bitcoin/sweep.js
@@ -1,14 +1,17 @@
-import axios from 'axios';
-import bitcoin from 'bitcoinjs-lib';
-import coinselect from 'coinselect';
-import split from 'coinselect/split';
+import config from "../../config/index.js";
+import { prod } from "../../lib/utils.js";
+import axios from "axios";
+import bitcoin from "bitcoinjs-lib";
+import coinselect from "coinselect";
+import split from "coinselect/split.js";
+
const api = prod ? "https://blockstream.info/api" : config.bitcoin.electrs;
const SATS = 100000000;
const network = prod
? bitcoin.networks["bitcoin"]
: bitcoin.networks["regtest"];
-export default ah(async (req, res) => {
+export default async (req, res) => {
let { address: from, amount, feeRate, target } = req.body;
let utxos;
if (!feeRate) feeRate = (await bc.estimateSmartFee(6)).feerate * SATS;
@@ -81,4 +84,4 @@ export default ah(async (req, res) => {
} catch (e) {
l.error("problem getting address stats", from, e.message);
}
-});
+};