From 33338d09d6cb03f089c1f053c81503c0f8c11dbc Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Tue, 14 Jun 2022 16:01:21 -0500 Subject: Moved conversion fee calculation to a single file per currency --- routes/bitcoin/conversionFee.js | 12 ++++++++++++ routes/bitcoin/receive.js | 6 +++--- routes/bitcoin/send.js | 35 +++++++++++++++++------------------ routes/liquid/conversionFee.js | 12 ++++++++++++ routes/liquid/receive.js | 5 ++--- routes/liquid/send.js | 37 ++++++++++++++++++------------------- 6 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 routes/bitcoin/conversionFee.js create mode 100644 routes/liquid/conversionFee.js 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], diff --git a/routes/liquid/conversionFee.js b/routes/liquid/conversionFee.js new file mode 100644 index 00000000..36ba6f36 --- /dev/null +++ b/routes/liquid/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' L-BTC. + */ +export function computeConversionFee(amount) { + return Math.floor(amount * conversionFeeMultiplier); +} diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 9650e5b7..432619b3 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -13,8 +13,7 @@ const network = config.liquid.network === "mainnet" ? "liquid" : config.liquid.network ]; -// used to calculate how many credits to give -const withdrawalFeeMultiplier = 0.01; +import { computeConversionFee } from './conversionFee.js'; const getAccount = async (params, transaction) => { let account = await db.Account.findOne({ @@ -344,7 +343,7 @@ setInterval(async () => { { pending: Math.min(p.account.pending, total) }, { transaction } ); - await p.account.increment({ fee_credits: Math.floor(total * withdrawalFeeMultiplier) }, { transaction }); + await p.account.increment({ fee_credits: computeConversionFee(total) }, { transaction }); await p.account.reload({ transaction }); await p.save({ transaction }); diff --git a/routes/liquid/send.js b/routes/liquid/send.js index fcb05942..33bee9ed 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -1,8 +1,7 @@ const btc = config.liquid.btcasset; const lcad = config.liquid.cadasset; const { Transaction } = require("liquidjs-lib"); -const withdrawalFeeMultiplier = 0.01; // 1% withdrawal fee -const withdrawalFeeReceiver = "coinosfees"; // 1% withdrawal fee +import { computeConversionFee, conversionFeeReceiver } from './conversionFee.js'; sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { try { @@ -84,10 +83,10 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { total += fee - covered; } - // 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 liquid bitcoin that leaves this server) - var withdrawalFee = Math.floor(amount * withdrawalFeeMultiplier); + var conversionFee = computeConversionFee(amount); if (limit && total > limit + fee) throw new Error("Tx amount exceeds authorized amount"); @@ -117,15 +116,15 @@ 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", { + } else if (total + conversionFee > account.balance) { + l.warn("amount plus conversion fee exceeds balance", { total, fee, - withdrawalFee, + conversionFee, balance: account.balance }); throw new Error( - `Insufficient funds, need ${total + withdrawalFee} ${ + `Insufficient funds, need ${total + conversionFee} ${ account.ticker === "BTC" ? "SAT" : account.ticker }, have ${account.balance}` ); @@ -133,19 +132,19 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { // 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 }); + 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: [ { @@ -158,13 +157,13 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { }); 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 = { - amount: withdrawalFee, + amount: conversionFee, fee: 0, - memo: "Liquid withdrawal fee", + memo: "Liquid conversion fee", account_id: receiverAccount.id, user_id: receiverAccount.user_id, rate: app.get("rates")[receiverAccount.user.currency], -- cgit v1.2.3 From a9caa15ac75a6ce2939ffd90a0a2040434095d84 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 16 Jun 2022 08:23:41 -0500 Subject: Fixed imports from previous commit --- routes/bitcoin/conversionFee.js | 4 ++-- routes/bitcoin/receive.js | 2 +- routes/bitcoin/send.js | 2 +- routes/liquid/conversionFee.js | 4 ++-- routes/liquid/receive.js | 2 +- routes/liquid/send.js | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/routes/bitcoin/conversionFee.js b/routes/bitcoin/conversionFee.js index 8a6bfce6..9df60c2a 100644 --- a/routes/bitcoin/conversionFee.js +++ b/routes/bitcoin/conversionFee.js @@ -2,11 +2,11 @@ // 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 +const conversionFeeReceiver = "coinosfees"; // account that receives the fees /** * Returns the fee you have to pay for withdrawing 'amount' bitcoin. */ -export function computeConversionFee(amount) { +function computeConversionFee(amount) { return Math.floor(amount * conversionFeeMultiplier); } diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 02deb58f..8f5d5842 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -21,7 +21,7 @@ const network = const queue = {}; const seen = []; -import { computeConversionFee } from './conversionFee.js'; +const { computeConversionFee } = require('./conversionFee.js'); zmqRawTx.on("message", async (topic, message, sequence) => { const hex = message.toString("hex"); diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 72d34317..a566c6f0 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -1,5 +1,5 @@ const btc = config.liquid.btcasset; -import { computeConversionFee, conversionFeeReceiver } from './conversionFee.js'; +let { computeConversionFee, conversionFeeReceiver } = require('./conversionFee.js'); module.exports = ah(async (req, res) => { let { user } = req; diff --git a/routes/liquid/conversionFee.js b/routes/liquid/conversionFee.js index 36ba6f36..d365185b 100644 --- a/routes/liquid/conversionFee.js +++ b/routes/liquid/conversionFee.js @@ -2,11 +2,11 @@ // 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 +const conversionFeeReceiver = "coinosfees"; // account that receives the fees /** * Returns the fee you have to pay for withdrawing 'amount' L-BTC. */ -export function computeConversionFee(amount) { +function computeConversionFee(amount) { return Math.floor(amount * conversionFeeMultiplier); } diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 432619b3..5c0ca98e 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -13,7 +13,7 @@ const network = config.liquid.network === "mainnet" ? "liquid" : config.liquid.network ]; -import { computeConversionFee } from './conversionFee.js'; +const { computeConversionFee } = require('./conversionFee.js'); const getAccount = async (params, transaction) => { let account = await db.Account.findOne({ diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 33bee9ed..3e8ac946 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -1,7 +1,7 @@ const btc = config.liquid.btcasset; const lcad = config.liquid.cadasset; const { Transaction } = require("liquidjs-lib"); -import { computeConversionFee, conversionFeeReceiver } from './conversionFee.js'; +const { computeConversionFee, conversionFeeReceiver } = require('./conversionFee.js'); sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { try { -- cgit v1.2.3 From 8361c2d400145a13d9ba24299ceb7293e69d3d43 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 16 Jun 2022 09:17:24 -0500 Subject: Separated Bitcoin and Liquid conversion fee credits --- routes/bitcoin/receive.js | 2 +- routes/bitcoin/send.js | 4 ++-- routes/liquid/receive.js | 2 +- routes/liquid/send.js | 5 ++--- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 8f5d5842..45dadbbe 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -195,7 +195,7 @@ setInterval(async () => { { 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.increment({ btc_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 a566c6f0..884c0db1 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -67,9 +67,9 @@ module.exports = ah(async (req, res) => { } // use user's credits to reduce fee, if available - let conversionFeeDeduction = Math.min(account.fee_credits, conversionFee); + let conversionFeeDeduction = Math.min(account.btc_credits, conversionFee); if (conversionFeeDeduction) { - await account.decrement({ fee_credits: conversionFeeDeduction }, { transaction }); + await account.decrement({ btc_credits: conversionFeeDeduction }, { transaction }); await account.reload({ transaction }); conversionFee -= conversionFeeDeduction; } diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 5c0ca98e..2bd86805 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -343,7 +343,7 @@ setInterval(async () => { { pending: Math.min(p.account.pending, total) }, { transaction } ); - await p.account.increment({ fee_credits: computeConversionFee(total) }, { transaction }); + await p.account.increment({ liquid_credits: computeConversionFee(total) }, { transaction }); await p.account.reload({ transaction }); await p.save({ transaction }); diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 3e8ac946..5cda8ba5 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -131,10 +131,9 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { } // use user's credits to reduce fee, if available - console.log(account.fee_credits); - let conversionFeeDeduction = Math.min(account.fee_credits, conversionFee); + let conversionFeeDeduction = Math.min(account.liquid_credits, conversionFee); if (conversionFeeDeduction) { - await account.decrement({ fee_credits: conversionFeeDeduction }, { transaction }); + await account.decrement({ liquid_credits: conversionFeeDeduction }, { transaction }); await account.reload({ transaction }); conversionFee -= conversionFeeDeduction; } -- cgit v1.2.3 From 55d4763146916e7e168bdc0ffd6d97c0d82f8e83 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 16 Jun 2022 10:52:56 -0500 Subject: Added Lightning conversion fees (currently 0%) --- routes/lightning/conversionFee.js | 12 +++++++++ routes/lightning/receive.js | 4 +++ routes/lnurl.js | 52 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 routes/lightning/conversionFee.js diff --git a/routes/lightning/conversionFee.js b/routes/lightning/conversionFee.js new file mode 100644 index 00000000..8fca9b0f --- /dev/null +++ b/routes/lightning/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.00; +const conversionFeeReceiver = "coinosfees"; // account that receives the fees + +/** + * Returns the fee you have to pay for withdrawing 'amount' lightning BTC. + */ +function computeConversionFee(amount) { + return Math.floor(amount * conversionFeeMultiplier); +} diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 6723d577..6d10eb9b 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -1,3 +1,5 @@ +const { computeConversionFee } = require('./conversionFee.js'); + const handlePayment = async (msg) => { l.info("incoming lightning payment", msg.value, msg.payment_request, msg.settled); if (!msg.settled) return; @@ -60,6 +62,8 @@ const handlePayment = async (msg) => { invoice.received += total; await account.increment({ balance: total }, { transaction }); + // get the # of fee credits you would need to pay off this amount of bitcoin + await account.increment({ btc_credits: computeConversionFee(total) }, { transaction }); await account.reload({ transaction }); await invoice.save({ transaction }); await payment.save({ transaction }); diff --git a/routes/lnurl.js b/routes/lnurl.js index 0466e143..49212330 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -12,6 +12,8 @@ withdrawals = persist("data/withdrawals.json"); lnurlServer = lnurl.createServer(config.lnurl); +let { computeConversionFee, conversionFeeReceiver } = require('lightning/conversionFee.js'); + var optionalAuth = function(req, res, next) { passport.authenticate("jwt", { session: false }, function(err, user, info) { req.user = user; @@ -391,6 +393,7 @@ lnurlServer.bindToHook( try { let decoded = await lnp.decodePayReq({ pay_req: pr }); let amount = decoded.num_satoshis; + let conversionFee = computeConversionFee(amount); await db.transaction(async transaction => { let account = await db.Account.findOne({ @@ -402,8 +405,52 @@ lnurlServer.bindToHook( transaction }); + // account that receives conversion fees + let receiverAccount = await db.Account.findOne({ + where: { + "$user.username$": conversionFeeReceiver + }, + include: [ + { + model: db.User, + as: "user" + }, + ], + lock: transaction.LOCK.UPDATE, + transaction + }); + if (account.balance < amount) { throw new Error("Insufficient funds"); + } else if (account.balance < amount + conversionFee) { + throw new Error("Insufficient funds for conversion fee"); + } + + // use user's credits to reduce fee, if available + let conversionFeeDeduction = Math.min(account.lightning_credits, conversionFee); + if (conversionFeeDeduction) { + await account.decrement({ lightning_credits: conversionFeeDeduction }, { transaction }); + await account.reload({ transaction }); + conversionFee -= conversionFeeDeduction; + } + + let fee_payment_id = null; + if (conversionFee) { + await receiverAccount.increment({ balance: conversionFee }, { transaction }); + await receiverAccount.reload({ transaction }); + let fee_payment = await db.Payment.create({ + amount: conversionFee, + fee: 0, + memo: "Bitcoin conversion 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; } let payment = await db.Payment.create( @@ -415,7 +462,8 @@ lnurlServer.bindToHook( rate: app.get("rates")[user.currency], currency: user.currency, confirmed: true, - network: "lightning" + network: "lightning", + fee_payment_id: fee_payment_id }, { transaction } ); @@ -447,7 +495,7 @@ lnurlServer.bindToHook( } }, 1000); - await account.decrement({ balance: amount }, { transaction }); + await account.decrement({ balance: (amount + conversionFee) }, { transaction }); await account.reload({ transaction }); let p = payment.get({ plain: true }); -- cgit v1.2.3 From 51ed4347d0dfcaac16957abf31848e0dd9d86b40 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 16 Jun 2022 12:23:23 -0500 Subject: Whether you can pay takes into account fee deductions --- routes/bitcoin/send.js | 16 ++++++++-------- routes/liquid/send.js | 16 ++++++++-------- routes/lnurl.js | 13 ++++++------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 884c0db1..634b223b 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -58,14 +58,6 @@ 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 + 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 conversionFeeDeduction = Math.min(account.btc_credits, conversionFee); if (conversionFeeDeduction) { @@ -74,6 +66,14 @@ module.exports = ah(async (req, res) => { conversionFee -= conversionFeeDeduction; } + if (total > account.balance) { + l.error("amount exceeds balance", amount, fee, account.balance); + throw new Error("low balance"); + } 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 + conversionFee) }, { transaction }); await account.reload({ transaction }); diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 5cda8ba5..254972ad 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -105,6 +105,14 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { transaction }); + // use user's credits to reduce fee, if available + let conversionFeeDeduction = Math.min(account.liquid_credits, conversionFee); + if (conversionFeeDeduction) { + await account.decrement({ liquid_credits: conversionFeeDeduction }, { transaction }); + await account.reload({ transaction }); + conversionFee -= conversionFeeDeduction; + } + if (total > account.balance) { l.warn("amount exceeds balance", { total, @@ -130,14 +138,6 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { ); } - // use user's credits to reduce fee, if available - let conversionFeeDeduction = Math.min(account.liquid_credits, conversionFee); - if (conversionFeeDeduction) { - await account.decrement({ liquid_credits: conversionFeeDeduction }, { transaction }); - await account.reload({ transaction }); - conversionFee -= conversionFeeDeduction; - } - await account.decrement({ balance: (total + conversionFee)}, { transaction }); await account.reload({ transaction }); diff --git a/routes/lnurl.js b/routes/lnurl.js index 49212330..93d939f9 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -420,13 +420,6 @@ lnurlServer.bindToHook( transaction }); - if (account.balance < amount) { - throw new Error("Insufficient funds"); - } else if (account.balance < amount + conversionFee) { - throw new Error("Insufficient funds for conversion fee"); - } - - // use user's credits to reduce fee, if available let conversionFeeDeduction = Math.min(account.lightning_credits, conversionFee); if (conversionFeeDeduction) { await account.decrement({ lightning_credits: conversionFeeDeduction }, { transaction }); @@ -434,6 +427,12 @@ lnurlServer.bindToHook( conversionFee -= conversionFeeDeduction; } + if (account.balance < amount) { + throw new Error("Insufficient funds"); + } else if (account.balance < amount + conversionFee) { + throw new Error("Insufficient funds for conversion fee"); + } + let fee_payment_id = null; if (conversionFee) { await receiverAccount.increment({ balance: conversionFee }, { transaction }); -- cgit v1.2.3 From 67e2ec07cf90cfbd1b5c1cbed18c4dffe38ea4aa Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 16 Jun 2022 14:05:02 -0500 Subject: Set Lightning fee to 1% --- routes/lightning/conversionFee.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/lightning/conversionFee.js b/routes/lightning/conversionFee.js index 8fca9b0f..7359bb3f 100644 --- a/routes/lightning/conversionFee.js +++ b/routes/lightning/conversionFee.js @@ -1,7 +1,7 @@ // 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.00; +const conversionFeeMultiplier = 0.01; const conversionFeeReceiver = "coinosfees"; // account that receives the fees /** -- cgit v1.2.3