diff options
| author | Adrien Hopkins <[email protected]> | 2022-06-16 14:48:27 -0500 |
|---|---|---|
| committer | Adrien Hopkins <[email protected]> | 2022-06-16 14:48:27 -0500 |
| commit | c66b9c6b5be4b1b524ee2675dd1f65f425d72ee9 (patch) | |
| tree | eba3a920bd31da13e76afea2835c1f6e515d1d11 /routes/bitcoin/send.js | |
| parent | ffbd27b2e9ddcb52a37febc0855e16b4fe09dbdc (diff) | |
| parent | 67e2ec07cf90cfbd1b5c1cbed18c4dffe38ea4aa (diff) | |
| download | coinos-server-c66b9c6b5be4b1b524ee2675dd1f65f425d72ee9.tar.gz coinos-server-c66b9c6b5be4b1b524ee2675dd1f65f425d72ee9.zip | |
Merge branch 'feature-withdrawal-fee'
Diffstat (limited to 'routes/bitcoin/send.js')
| -rw-r--r-- | routes/bitcoin/send.js | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index a9c95b31..634b223b 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -1,6 +1,5 @@ const btc = config.liquid.btcasset; -const withdrawalFeeMultiplier = 0.01; // 1% withdrawal fee -const withdrawalFeeReceiver = "coinosfees"; // account that receives the fees +let { computeConversionFee, conversionFeeReceiver } = require('./conversionFee.js'); module.exports = ah(async (req, res) => { let { user } = req; @@ -31,10 +30,10 @@ module.exports = ah(async (req, res) => { total = total - change + fee; let amount = total - fee; - // get withdrawal fee - // 'total' refers to the total before the withdrawal fee + // get conversion fee + // 'total' refers to the total before the conversion fee // (i.e. the total bitcoin that leaves this server) - let withdrawalFee = Math.floor(amount * withdrawalFeeMultiplier); + let conversionFee = computeConversionFee(amount); try { // withdraw bitcoin @@ -59,28 +58,28 @@ module.exports = 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.reload({ transaction }); + conversionFee -= conversionFeeDeduction; + } + 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)"); - } - - // 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; + } else if (total + conversionFee > 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 + withdrawalFee) }, { transaction }); + await account.decrement({ balance: (total + conversionFee) }, { transaction }); await account.reload({ transaction }); let receiverAccount = await db.Account.findOne({ where: { - "$user.username$": withdrawalFeeReceiver + "$user.username$": conversionFeeReceiver }, include: [ { @@ -93,13 +92,13 @@ module.exports = ah(async (req, res) => { }); let fee_payment_id = null; - if (withdrawalFee) { - await receiverAccount.increment({ balance: withdrawalFee }, { transaction }); + if (conversionFee) { + await receiverAccount.increment({ balance: conversionFee }, { transaction }); await receiverAccount.reload({ transaction }); let fee_payment = await db.Payment.create({ - amount: withdrawalFee, + amount: conversionFee, fee: 0, - memo: "Bitcoin withdrawal fee", + memo: "Bitcoin conversion fee", account_id: receiverAccount.id, user_id: receiverAccount.user_id, rate: app.get("rates")[receiverAccount.user.currency], |
