aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-09-08 16:29:14 -0700
committerAdam Soltys <[email protected]>2022-09-08 16:29:14 -0700
commitbe27adba90c2c52ccbfac5885d70e5b37753fc58 (patch)
tree3ea8a2e1700112782d3bd16517fff4a875d38756 /lib
parentd89458ebef267c7dc2290100ecb05b7d0891c224 (diff)
parentc60ad34877c44760263a9e93e99aa425ad0fdd99 (diff)
downloadcoinos-server-be27adba90c2c52ccbfac5885d70e5b37753fc58.tar.gz
coinos-server-be27adba90c2c52ccbfac5885d70e5b37753fc58.zip
Merge branch 'prod'
Diffstat (limited to 'lib')
-rw-r--r--lib/send.js36
1 files changed, 15 insertions, 21 deletions
diff --git a/lib/send.js b/lib/send.js
index a4280401..26709b61 100644
--- a/lib/send.js
+++ b/lib/send.js
@@ -1,3 +1,5 @@
+import config from "$config";
+import db from "$db";
import { emit } from "$lib/sockets";
import store from "./store";
import bolt11 from "bolt11";
@@ -6,9 +8,11 @@ import {
computeConversionFee,
conversionFeeReceiver
} from "../routes/lightning/conversionFee";
-import ln from "$lib/ln";
+import lnd from "$lib/lnd";
+import { pay } from "lightning";
+import { l, warn, err } from "$lib/logging";
-export const send = async (amount, memo, hash, user) => {
+export default async (amount, memo, hash, user) => {
l("attempting lightning payment", user.username, amount, hash);
let payreq = bolt11.decode(hash);
@@ -25,7 +29,7 @@ export const send = async (amount, memo, hash, user) => {
if (!amount || payreq.satoshis > amount) amount = payreq.satoshis;
- if (seen.includes(hash)) {
+ if (store.seen.includes(hash)) {
warn("attempted to pay a paid invoice", user.username);
throw new Error("Invoice has been paid, can't pay again");
}
@@ -82,21 +86,12 @@ export const send = async (amount, memo, hash, user) => {
await account.reload({ transaction });
l("debited account", account.id);
+ let tokens;
+ if (!payreq.satoshis) tokens = amount;
- if (config.lna.clightning) {
- let m = await ln.pay(hash);
- let sent;
- ({
- msatoshi: total,
- msatoshi_sent: sent,
- payment_preimage: preimage
- } = m);
-
- fee = parseInt((sent - total) / 1000);
- total = parseInt(total / 1000);
- } else {
- let m = await ln.pay({
- tokens: amount,
+ let m = await pay({
+ lnd,
+ tokens,
request: hash,
max_paths: 10,
max_fee: { fixed: Math.min(10000, account.balance) }
@@ -106,11 +101,10 @@ export const send = async (amount, memo, hash, user) => {
if (m.payment_error) throw new Error(m.payment_error);
({ fee, secret: preimage, tokens: total } = m);
- }
preimage = preimage.toString("hex");
- if (seen.includes(preimage)) {
+ if (store.seen.includes(preimage)) {
warn("duplicate payment detected", preimage);
throw new Error("Duplicate payment detected");
}
@@ -220,8 +214,8 @@ export const send = async (amount, memo, hash, user) => {
emit(user.username, "account", payment.account);
emit(user.username, "payment", payment);
- seen.push(preimage);
- seen.push(hash);
+ store.seen.push(preimage);
+ store.seen.push(hash);
l("sent lightning", user.username, total - fee, fee);
});