diff options
| author | Adrien Hopkins <[email protected]> | 2022-06-06 14:51:48 -0500 |
|---|---|---|
| committer | Adrien Hopkins <[email protected]> | 2022-06-06 14:51:48 -0500 |
| commit | 8fcd976a04d1cfffdf84306c39a4d6c526aa0a22 (patch) | |
| tree | de731c67d32fd93d9105a2f0a3ab2776d9768334 /routes/bitcoin/send.js | |
| parent | 2549d087fd55191cd711e02522892092ba7e2fda (diff) | |
| download | coinos-server-8fcd976a04d1cfffdf84306c39a4d6c526aa0a22.tar.gz coinos-server-8fcd976a04d1cfffdf84306c39a4d6c526aa0a22.zip | |
Added withdrawal fee credits, which can pay withdrawal fees
Diffstat (limited to 'routes/bitcoin/send.js')
| -rw-r--r-- | routes/bitcoin/send.js | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index aa3a7640..efcedb38 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 + console.log(account.fee_credits); + let withdrawalFeeDeduction = Math.min(account.fee_credits, withdrawalFee); + if (withdrawalFeeDeduction) { + await account.decrement({ fee_credits: withdrawalFeeDeduction }, { transaction }); + await account.reload({ transaction }); + } + await account.decrement({ balance: (total + withdrawalFee) }, { transaction }); await account.reload({ transaction }); @@ -84,22 +92,24 @@ 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 let params = { @@ -114,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) |
