aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-09-30 15:10:05 -0700
committerAdam Soltys <[email protected]>2022-09-30 15:10:05 -0700
commit721f96b11d25eeb58342c5847b0a4bd3f5159dbb (patch)
tree2ac3b89ba3c0febe7d45e25eb4515d42e7a27188
parent4ee46fee0cbf23f51228c7c4895ee1e0565b074f (diff)
parentc3efae9d1720c6f9eb6f826c4f1c6d5f3e259407 (diff)
downloadcoinos-server-721f96b11d25eeb58342c5847b0a4bd3f5159dbb.tar.gz
coinos-server-721f96b11d25eeb58342c5847b0a4bd3f5159dbb.zip
Merge branch 'prod'
-rw-r--r--routes/bitcoin/send.js87
1 files changed, 45 insertions, 42 deletions
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index bde3ce00..5bf21f1a 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -9,63 +9,56 @@ import { l, warn, err } from "$lib/logging";
const btc = config.liquid.btcasset;
import {
computeConversionFee,
- conversionFeeReceiver
+ conversionFeeReceiver,
} from "./conversionFee.js";
export default async (req, res) => {
- let { user } = req;
- let { address, memo, tx } = req.body;
- let { hex } = tx;
+ try {
+ let { user } = req;
+ let { address, memo, tx } = req.body;
+ let { hex } = tx;
- let fee = toSats(tx.fee);
- if (fee < 0) throw new Error("fee cannot be negative");
+ let fee = toSats(tx.fee);
+ if (fee < 0) throw new Error("fee cannot be negative");
- const isChange = async ({ address }) =>
- (await bc.getAddressInfo(address)).ismine &&
- !Object.keys(store.addresses).includes(address);
+ const isChange = async ({ address }) =>
+ (await bc.getAddressInfo(address)).ismine &&
+ !Object.keys(store.addresses).includes(address);
- tx = await bc.decodeRawTransaction(hex);
+ tx = await bc.decodeRawTransaction(hex);
- let total = 0;
- let change = 0;
+ let total = 0;
+ let change = 0;
- for (let i = 0; i < tx.vout.length; i++) {
- let o = tx.vout[i];
- total += toSats(o.value);
+ for (let i = 0; i < tx.vout.length; i++) {
+ let o = tx.vout[i];
+ total += toSats(o.value);
- if (await isChange(o.scriptPubKey)) {
- change += toSats(o.value);
+ if (await isChange(o.scriptPubKey)) {
+ change += toSats(o.value);
+ }
}
- }
- total = total - change + fee;
- let amount = total - fee;
+ total = total - change + fee;
+ let amount = total - fee;
- // get conversion fee
- // 'total' refers to the total before the conversion fee
- // (i.e. the total bitcoin that leaves this server)
- let conversionFee = computeConversionFee(amount);
+ // get conversion fee
+ // 'total' refers to the total before the conversion fee
+ // (i.e. the total bitcoin that leaves this server)
+ let conversionFee = computeConversionFee(amount);
- try {
// withdraw bitcoin
- await db.transaction(async transaction => {
- let account = await db.Account.findOne({
- where: {
- id: user.account_id
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
-
+ await db.transaction(async (transaction) => {
+ let { account } = user;
if (account.asset !== btc) {
account = await db.Account.findOne({
where: {
user_id: user.id,
asset: btc,
- pubkey: null
+ pubkey: null,
},
lock: transaction.LOCK.UPDATE,
- transaction
+ transaction,
});
}
@@ -80,6 +73,16 @@ export default async (req, res) => {
conversionFee -= conversionFeeDeduction;
}
+ l(
+ "attempting to send bitcoin",
+ user.username,
+ account.id,
+ address,
+ total,
+ conversionFee,
+ conversionFeeDeduction
+ );
+
if (total > account.balance) {
err("amount exceeds balance", amount, fee, account.balance);
throw new Error("low balance");
@@ -101,16 +104,16 @@ export default async (req, res) => {
let receiverAccount = await db.Account.findOne({
where: {
- "$user.username$": conversionFeeReceiver
+ "$user.username$": conversionFeeReceiver,
},
include: [
{
model: db.User,
- as: "user"
- }
+ as: "user",
+ },
],
lock: transaction.LOCK.UPDATE,
- transaction
+ transaction,
});
let fee_payment;
@@ -132,7 +135,7 @@ export default async (req, res) => {
currency: receiverAccount.user.currency,
confirmed: true,
received: true,
- network: "COINOS"
+ network: "COINOS",
},
{ transaction }
);
@@ -152,7 +155,7 @@ export default async (req, res) => {
confirmed: true,
received: false,
network: "bitcoin",
- fee_payment_id: fee_payment_id
+ fee_payment_id: fee_payment_id,
};
if (config.bitcoin.walletpass)