From 2549d087fd55191cd711e02522892092ba7e2fda Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Mon, 6 Jun 2022 08:48:26 -0500 Subject: Implemented liquid fees (?) --- routes/bitcoin/send.js | 1 - routes/liquid/send.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index cf7e89ef..aa3a7640 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -102,7 +102,6 @@ module.exports = ah(async (req, res) => { ); // record the external bitcoin transaction - // no need to record the withdrawal fee - since that is internal only let params = { amount: -amount, fee, diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 6f3616e8..1e4c62d2 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -84,6 +84,11 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { total += fee - covered; } + // get withdrawal fee + // 'total' refers to the total before the withdrawal fee + // (i.e. the total liquid bitcoin that leaves this server) + var withdrawalFee = amount * withdrawalFeeMultiplier; + if (limit && total > limit + fee) throw new Error("Tx amount exceeds authorized amount"); @@ -112,11 +117,54 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { account.ticker === "BTC" ? "SAT" : account.ticker }, have ${account.balance}` ); + } else if (total + withdrawalFee > account.balance) { + l.warn("amount plus withdrawal fee exceeds balance", { + total, + fee, + withdrawalFee, + balance: account.balance + }); + throw new Error( + `Insufficient funds, need ${total + withdrawalFee} ${ + account.ticker === "BTC" ? "SAT" : account.ticker + }, have ${account.balance}` + ); } - await account.decrement({ balance: total }, { transaction }); + await account.decrement({ balance: (total + withdrawalFee)}, { transaction }); await account.reload({ transaction }); + let receiverAccount = await db.Account.findOne({ + where: { + "$user.username$": withdrawalFeeReceiver + }, + include: [ + { + model: db.User, + as: "user" + }, + ], + lock: transaction.LOCK.UPDATE, + transaction + }); + + await receiverAccount.increment({ balance: withdrawalFee }, { transaction }); + await receiverAccount.reload({ transaction }); + let fee_payment = { + amount: withdrawalFee, + fee: 0, + memo: "Liquid 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" + }; + fee_payment.account = receiverAccount; + payments.push(fee_payment); + let payment = { amount: -amount, account_id: account.id, @@ -128,7 +176,8 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { address, confirmed: true, received: false, - network: "liquid" + network: "liquid", + fee_payment_id: fee_payment.id }; payment.account = account; -- cgit v1.2.3