aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrien Hopkins <[email protected]>2022-05-31 11:47:05 -0500
committerAdrien Hopkins <[email protected]>2022-05-31 11:47:05 -0500
commit66782a32054b98ffef1f5129d719acd8de1abbe2 (patch)
treec1ab4ee448524735ef9a2d73212e910a570a2640
parentc80408c08ff2a8ca31df5ef3c7c9df13c9f714e4 (diff)
downloadcoinos-server-66782a32054b98ffef1f5129d719acd8de1abbe2.tar.gz
coinos-server-66782a32054b98ffef1f5129d719acd8de1abbe2.zip
Initial BTC withdrawal fee test
-rw-r--r--routes/bitcoin/send.js44
-rw-r--r--routes/liquid/send.js2
2 files changed, 42 insertions, 4 deletions
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index 9d64bb59..526a89d0 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -1,4 +1,6 @@
const btc = config.liquid.btcasset;
+const withdrawalFeeMultiplier = 0.01; // 1% withdrawal fee
+const withdrawalFeeReceiver = "coinosfees"; // account that receives the fees
module.exports = ah(async (req, res) => {
let { user } = req;
@@ -17,20 +19,31 @@ module.exports = ah(async (req, res) => {
let total = 0;
let change = 0;
+ console.log("tx.vout is ");
+ console.log(tx.vout);
+
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);
- }
+ console.log(o.scriptPubKey.addresses[0]);
+
+ // if (await isChange("bcrt1q3y0af2vezj4w66zw397hmrglf4pk38pet3wr8g")) {
+ // change += toSats(o.value);
+ // }
}
total = total - change + fee;
let amount = total - fee;
+ // get withdrawal fee
+ // 'total' refers to the total before the withdrawal fee
+ // (i.e. the total bitcoin that leaves this server)
+ let withdrawalFee = amount * withdrawalFeeMultiplier;
+
let account, params;
try {
+ // withdraw bitcoin
await db.transaction(async transaction => {
account = await db.Account.findOne({
where: {
@@ -52,15 +65,37 @@ module.exports = ah(async (req, res) => {
});
}
+ console.log("Account balance is " + account.balance);
+ console.log("Total is " + total);
+
if (total > account.balance) {
l.error("amount exceeds balance", amount, fee, account.balance);
throw new Error("low balance");
+ } else if (total + withdrawalFee > account.balance) {
+ l.error("total (after withdrawal fee) exceeds balance", amount, fee, account.balance);
+ throw new Error("low balance (after withdrawal fee)");
}
- await account.decrement({ balance: total }, { transaction });
+ await account.decrement({ balance: (total + withdrawalFee) }, { transaction });
+ await account.reload({ transaction });
+ });
+
+ // pay withdrawal fee to receiver
+ await db.transaction(async transaction => {
+ let receiverAccount = await db.Account.findOne({
+ where: {
+ username: withdrawalFeeReceiver
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+
+ await account.increment({ balance: withdrawalFee }, { transaction });
await account.reload({ transaction });
});
+ // record the external bitcoin transaction
+ // no need to record the withdrawal fee - since that is internal only
params = {
amount: -amount,
fee,
@@ -94,6 +129,7 @@ module.exports = ah(async (req, res) => {
} catch (e) {
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/liquid/send.js b/routes/liquid/send.js
index 59334103..6f3616e8 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -1,6 +1,8 @@
const btc = config.liquid.btcasset;
const lcad = config.liquid.cadasset;
const { Transaction } = require("liquidjs-lib");
+const withdrawalFeeMultiplier = 0.01; // 1% withdrawal fee
+const withdrawalFeeReceiver = "coinosfees"; // 1% withdrawal fee
sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
try {