diff options
| author | Adrien Hopkins <[email protected]> | 2022-06-14 16:01:21 -0500 |
|---|---|---|
| committer | Adrien Hopkins <[email protected]> | 2022-06-14 16:01:21 -0500 |
| commit | 33338d09d6cb03f089c1f053c81503c0f8c11dbc (patch) | |
| tree | a55364177265b04c67ccf66a5db76be30bfb6443 /routes/bitcoin | |
| parent | ffbd27b2e9ddcb52a37febc0855e16b4fe09dbdc (diff) | |
| download | coinos-server-33338d09d6cb03f089c1f053c81503c0f8c11dbc.tar.gz coinos-server-33338d09d6cb03f089c1f053c81503c0f8c11dbc.zip | |
Moved conversion fee calculation to a single file per currency
Diffstat (limited to 'routes/bitcoin')
| -rw-r--r-- | routes/bitcoin/conversionFee.js | 12 | ||||
| -rw-r--r-- | routes/bitcoin/receive.js | 6 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 35 |
3 files changed, 32 insertions, 21 deletions
diff --git a/routes/bitcoin/conversionFee.js b/routes/bitcoin/conversionFee.js new file mode 100644 index 00000000..8a6bfce6 --- /dev/null +++ b/routes/bitcoin/conversionFee.js @@ -0,0 +1,12 @@ +// Code for the conversion fee +// This is in a separate file so that the same function can be used by ./send and ./receive + +const conversionFeeMultiplier = 0.01; +export const conversionFeeReceiver = "coinosfees"; // account that receives the fees + +/** + * Returns the fee you have to pay for withdrawing 'amount' bitcoin. + */ +export function computeConversionFee(amount) { + return Math.floor(amount * conversionFeeMultiplier); +} diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 5bdbcb72..02deb58f 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -21,8 +21,7 @@ const network = const queue = {}; const seen = []; -// used to calculate how many credits to give -const withdrawalFeeMultiplier = 0.01; +import { computeConversionFee } from './conversionFee.js'; zmqRawTx.on("message", async (topic, message, sequence) => { const hex = message.toString("hex"); @@ -195,7 +194,8 @@ setInterval(async () => { { pending: Math.min(account.pending, total) }, { transaction } ); - await account.increment({ fee_credits: Math.floor(total * withdrawalFeeMultiplier) }, { transaction }); + // get the # of fee credits you would need to pay off this amount of bitcoin + await account.increment({ fee_credits: computeConversionFee(total) }, { transaction }); await account.reload({ transaction }); await p.save({ transaction }); diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index a9c95b31..72d34317 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 +import { computeConversionFee, conversionFeeReceiver } from './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 @@ -62,25 +61,25 @@ module.exports = ah(async (req, res) => { 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)"); + } 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)"); } // 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 }); + let conversionFeeDeduction = Math.min(account.fee_credits, conversionFee); + if (conversionFeeDeduction) { + await account.decrement({ fee_credits: conversionFeeDeduction }, { transaction }); await account.reload({ transaction }); - withdrawalFee -= withdrawalFeeDeduction; + conversionFee -= conversionFeeDeduction; } - 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], |
