aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/payments.js40
1 files changed, 21 insertions, 19 deletions
diff --git a/lib/payments.js b/lib/payments.js
index 275c6787..bfce3db8 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -232,7 +232,7 @@ export let autowithdraw = async (user) => {
let pay = async ({ amount, to, user }) => {
amount = parseInt(amount) || 0;
- let lnurl;
+ let lnurl, pr;
if (to.includes("@") && to.includes(".")) {
let [name, domain] = to.split("@");
lnurl = `https://${domain}/.well-known/lnurlp/${name}`;
@@ -242,8 +242,9 @@ let pay = async ({ amount, to, user }) => {
).toString();
}
- let pr;
+ let maxfee = getMaxFee(amount);
if (lnurl) {
+ amount -= maxfee;
let { callback } = await got(lnurl).json();
({ pr } = await got(`${callback}?amount=${amount}`).json());
} else if (to.startsWith("ln")) {
@@ -253,8 +254,8 @@ let pay = async ({ amount, to, user }) => {
} catch (e) {}
}
- if (pr) await sendLightning({ user, pr, amount });
- else await sendOnchain({ amount, address: to, user, subtract: true });
+ if (pr) await sendLightning({ user, pr, amount, maxfee });
+ else await sendOnchain({ amount, address: to, user });
};
let decode = async (hex) => {
@@ -352,6 +353,21 @@ export let sendOnchain = async (params) => {
return node.sendRawTransaction(signed);
};
+let getMaxFee = (n) =>
+ Math.round(
+ n < 100
+ ? n * 5
+ : n < 1000
+ ? n
+ : n < 10000
+ ? n * 0.5
+ : n < 100000
+ ? n * 0.1
+ : n < 1000000
+ ? n * 0.05
+ : n * 0.01,
+ );
+
export let sendLightning = async ({ user, pr, amount, maxfee, memo }) => {
let p;
let hash = pr;
@@ -365,21 +381,7 @@ export let sendLightning = async ({ user, pr, amount, maxfee, memo }) => {
let { amount_msat, payment_hash } = await ln.decode(pr);
if (amount_msat) total = Math.round(amount_msat / 1000);
- maxfee =
- parseInt(maxfee) ||
- Math.round(
- total < 100
- ? total * 5
- : total < 1000
- ? total
- : total < 10000
- ? total * 0.5
- : total < 100000
- ? total * 0.1
- : total < 1000000
- ? total * 0.05
- : total * 0.01,
- );
+ maxfee = parseInt(maxfee) || getMaxFee(total);
if (maxfee < 0) fail("Max fee cannot be negative");