aboutsummaryrefslogtreecommitdiffstats
path: root/routes/bitcoin
diff options
context:
space:
mode:
authorAdrien Hopkins <[email protected]>2022-06-13 16:16:37 -0500
committerAdrien Hopkins <[email protected]>2022-06-13 16:16:37 -0500
commitf37c8ba68418a2a325f45a7a36c139cb5bc696aa (patch)
tree98759089e8015931030877fb064507150ade7c36 /routes/bitcoin
parent9527f8a68f6a0a329c82dd8350703d10070437d5 (diff)
parent1c02aa63933a234faa45eecbfdc82dae7f6e174c (diff)
downloadcoinos-server-f37c8ba68418a2a325f45a7a36c139cb5bc696aa.tar.gz
coinos-server-f37c8ba68418a2a325f45a7a36c139cb5bc696aa.zip
Merge branch 'feature-withdrawal-fee'
Diffstat (limited to 'routes/bitcoin')
-rw-r--r--routes/bitcoin/receive.js4
-rw-r--r--routes/bitcoin/send.js47
2 files changed, 32 insertions, 19 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index 5d8e21e7..5bdbcb72 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -21,6 +21,9 @@ const network =
const queue = {};
const seen = [];
+// used to calculate how many credits to give
+const withdrawalFeeMultiplier = 0.01;
+
zmqRawTx.on("message", async (topic, message, sequence) => {
const hex = message.toString("hex");
let tx = bitcoin.Transaction.fromHex(message);
@@ -192,6 +195,7 @@ setInterval(async () => {
{ pending: Math.min(account.pending, total) },
{ transaction }
);
+ await account.increment({ fee_credits: Math.floor(total * withdrawalFeeMultiplier) }, { transaction });
await account.reload({ transaction });
await p.save({ transaction });
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index cf7e89ef..a9c95b31 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -34,7 +34,7 @@ module.exports = ah(async (req, res) => {
// 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 withdrawalFee = Math.floor(amount * withdrawalFeeMultiplier);
try {
// withdraw bitcoin
@@ -67,6 +67,14 @@ module.exports = ah(async (req, res) => {
throw new Error("low balance (after withdrawal fee)");
}
+ // use user's credits to reduce fee, if available
+ let withdrawalFeeDeduction = Math.min(account.fee_credits, withdrawalFee);
+ if (withdrawalFeeDeduction) {
+ await account.decrement({ fee_credits: withdrawalFeeDeduction }, { transaction });
+ await account.reload({ transaction });
+ withdrawalFee -= withdrawalFeeDeduction;
+ }
+
await account.decrement({ balance: (total + withdrawalFee) }, { transaction });
await account.reload({ transaction });
@@ -84,25 +92,26 @@ module.exports = ah(async (req, res) => {
transaction
});
- await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
- await receiverAccount.reload({ transaction });
- let fee_payment = await db.Payment.create({
- amount: withdrawalFee,
- fee: 0,
- memo: "Bitcoin withdrawal 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 }
- );
+ let fee_payment_id = null;
+ if (withdrawalFee) {
+ await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
+ await receiverAccount.reload({ transaction });
+ let fee_payment = await db.Payment.create({
+ amount: withdrawalFee,
+ fee: 0,
+ memo: "Bitcoin withdrawal 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;
+ }
// record the external bitcoin transaction
- // no need to record the withdrawal fee - since that is internal only
let params = {
amount: -amount,
fee,
@@ -115,7 +124,7 @@ module.exports = ah(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)