From 957d6c595110f44e349854e82e7c02bdade9b379 Mon Sep 17 00:00:00 2001 From: xraid Date: Fri, 2 Jul 2021 13:27:18 +0200 Subject: [PATCH] Add global fee settings from config.js --- class/Paym.js | 12 ++++++------ class/User.js | 4 ++-- config.js | 2 ++ controllers/api.js | 18 ++++++++++++------ scripts/show_user.js | 2 +- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/class/Paym.js b/class/Paym.js index 3ef2815..e7cbde5 100644 --- a/class/Paym.js +++ b/class/Paym.js @@ -12,9 +12,9 @@ export class Paym { this._isPaid = null; } - static get fee() { - return 0.003; - } + // static get fee() { + // return 0.003; + // } setInvoice(bolt11) { this._bolt11 = bolt11; @@ -39,7 +39,7 @@ export class Paym { pub_key: this._decoded.destination, amt: this._decoded.num_satoshis, final_cltv_delta: 144, - fee_limit: { fixed: Math.floor(this._decoded.num_satoshis * 0.01) + 1 }, + fee_limit: { fixed: Math.floor(this._decoded.num_satoshis * forwardFee) + 1 }, }; let that = this; return new Promise(function (resolve, reject) { @@ -74,7 +74,7 @@ export class Paym { if (payment && payment.payment_route && payment.payment_route.total_amt_msat) { // paid just now this._isPaid = true; - payment.payment_route.total_fees = +payment.payment_route.total_fees + Math.floor(+payment.payment_route.total_amt * Paym.fee); + payment.payment_route.total_fees = +payment.payment_route.total_fees + Math.floor(+payment.payment_route.total_amt * internalFee); if (this._bolt11) payment.pay_req = this._bolt11; if (this._decoded) payment.decoded = this._decoded; } @@ -87,7 +87,7 @@ export class Paym { if (this._bolt11) payment.pay_req = this._bolt11; // trying to guess the fee payment.payment_route = payment.payment_route || {}; - payment.payment_route.total_fees = Math.floor(this._decoded.num_satoshis * 0.01); // we dont know the exact fee, so we use max (same as fee_limit) + payment.payment_route.total_fees = Math.floor(this._decoded.num_satoshis * forwardFee); // we dont know the exact fee, so we use max (same as fee_limit) payment.payment_route.total_amt = this._decoded.num_satoshis; } } diff --git a/class/User.js b/class/User.js index a34596c..00cfdf4 100644 --- a/class/User.js +++ b/class/User.js @@ -175,7 +175,7 @@ export class User { let lockedPayments = await this.getLockedPayments(); for (let paym of lockedPayments) { // locked payments are processed in scripts/process-locked-payments.js - calculatedBalance -= +paym.amount + /* feelimit */ Math.floor(paym.amount * 0.01); + calculatedBalance -= +paym.amount + /* feelimit */ Math.floor(paym.amount * forwardFee); } return calculatedBalance; @@ -579,7 +579,7 @@ export class User { try { json = JSON.parse(paym); result.push(json); - } catch (_) {} + } catch (_) { } } return result; diff --git a/config.js b/config.js index 5fe3360..e1da533 100644 --- a/config.js +++ b/config.js @@ -2,6 +2,8 @@ let config = { enableUpdateDescribeGraph: false, postRateLimit: 100, rateLimit: 200, + defaultForwardReserveFee: 0.01, + defaultIntraHubFee: 0.003, bitcoind: { rpc: 'http://login:password@1.1.1.1:8332/wallet/wallet.dat', }, diff --git a/controllers/api.js b/controllers/api.js index 487c201..dfc8bd8 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -15,6 +15,12 @@ redis.monitor(function (err, monitor) { }); }); +/****** START SET FEES FROM CONFIG AT STARTUP ******/ +/** GLOBALS */ +global.forwardFee = config.defaultForwardReserveFee; +global.internalFee = config.defaultIntraHubFee; +/****** END SET FEES FROM CONFIG AT STARTUP ******/ + let bitcoinclient = require('../bitcoin'); let lightning = require('../lightning'); let identity_pubkey = false; @@ -235,7 +241,7 @@ router.post('/payinvoice', async function (req, res) { logger.log('/payinvoice', [req.id, 'userBalance: ' + userBalance, 'num_satoshis: ' + info.num_satoshis]); - if (userBalance >= +info.num_satoshis + Math.floor(info.num_satoshis * 0.01)) { + if (userBalance >= +info.num_satoshis + Math.floor(info.num_satoshis * forwardFee)) { // got enough balance, including 1% of payment amount - reserve for fees if (identity_pubkey === info.destination) { @@ -262,8 +268,8 @@ router.post('/payinvoice', async function (req, res) { await u.savePaidLndInvoice({ timestamp: parseInt(+new Date() / 1000), type: 'paid_invoice', - value: +info.num_satoshis + Math.floor(info.num_satoshis * Paym.fee), - fee: Math.floor(info.num_satoshis * Paym.fee), + value: +info.num_satoshis + Math.floor(info.num_satoshis * internalFee), + fee: Math.floor(info.num_satoshis * internalFee), memo: decodeURIComponent(info.description), pay_req: req.body.invoice, }); @@ -316,7 +322,7 @@ router.post('/payinvoice', async function (req, res) { let inv = { payment_request: req.body.invoice, amt: info.num_satoshis, // amt is used only for 'tip' invoices - fee_limit: { fixed: Math.floor(info.num_satoshis * 0.005) + 1 }, + fee_limit: { fixed: Math.floor(info.num_satoshis * internalFee) + 1 }, // fee setting was 0.005 now set as internalFee }; try { await u.lockFunds(req.body.invoice, info); @@ -419,8 +425,8 @@ router.get('/gettxs', async function (req, res) { for (let locked of lockedPayments) { txs.push({ type: 'paid_invoice', - fee: Math.floor(locked.amount * 0.01) /* feelimit */, - value: locked.amount + Math.floor(locked.amount * 0.01) /* feelimit */, + fee: Math.floor(locked.amount * forwardFee) /* feelimit */, + value: locked.amount + Math.floor(locked.amount * forwardFee) /* feelimit */, timestamp: locked.timestamp, memo: 'Payment in transition', }); diff --git a/scripts/show_user.js b/scripts/show_user.js index f1be84c..9e56aa7 100644 --- a/scripts/show_user.js +++ b/scripts/show_user.js @@ -50,7 +50,7 @@ let lightning = require('../lightning'); let locked = await U.getLockedPayments(); for (let loc of locked) { - console.log('-', loc.amount + /* fee limit */ Math.floor(loc.amount * 0.01), new Date(loc.timestamp * 1000).toString(), '[locked]'); + console.log('-', loc.amount + /* fee limit */ Math.floor(loc.amount * config.defaultForwardReserveFee), new Date(loc.timestamp * 1000).toString(), '[locked]'); } console.log('\ncalculatedBalance\n================\n', calculatedBalance, await U.getCalculatedBalance());