aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-13 20:27:18 -0800
committerAdam Soltys <[email protected]>2023-01-13 20:27:18 -0800
commit8a114c50efaac41767042766f05390aa906da157 (patch)
treeeb65bc1e9adf3614cb30798210328f7e83e0a0ce /routes/lightning
parentc3565838644123e77881ff4f8536792f074047b9 (diff)
downloadcoinos-server-8a114c50efaac41767042766f05390aa906da157.tar.gz
coinos-server-8a114c50efaac41767042766f05390aa906da157.zip
migrating to redis
Diffstat (limited to 'routes/lightning')
-rw-r--r--routes/lightning/receive.js161
1 files changed, 54 insertions, 107 deletions
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
index a778271b..a37dc716 100644
--- a/routes/lightning/receive.js
+++ b/routes/lightning/receive.js
@@ -6,132 +6,79 @@ import { callWebhook } from "$lib/webhooks";
import { computeConversionFee } from "./conversionFee";
import ln from "$lib/ln";
import { l, err, warn } from "$lib/logging";
+import { g, s, rd } from "$lib/redis";
-const handlePayment = async msg => {
- try {
- l("incoming lightning payment", msg.received);
-
- if (!msg.secret) return;
- let account, total, user;
-
- const invoice = await db.Invoice.findOne({
- where: {
- text: msg.request
- }
- });
+let handle = async () => {
+ let {
+ payment_hash,
+ bolt11: request,
+ pay_index,
+ status,
+ msatoshi_received,
+ payment_preimage: secret
+ } = await ln.waitanyinvoice(await g("pay_index"));
- if (!invoice)
- return warn("received lightning with no invoice", msg.request);
+ await s("pay_index", pay_index);
- await db.transaction(async transaction => {
- const { text: hash, currency, memo, rate, tip, user_id } = invoice;
- const amount = parseInt(msg.received) - tip;
- if (amount < 0) throw new Error("amount out of range");
+ let received = Math.round(msatoshi_received / 1000);
- account = await db.Account.findOne({
- where: {
- user_id,
- asset: config.liquid.btcasset,
- pubkey: null
- },
- include: {
- model: db.User,
- as: "user"
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ try {
+ l("incoming lightning payment", received);
- ({ user } = account);
+ if (!secret) return;
+ let account, total, user;
- let preimage = msg.secret;
+ let invoice = await g(`invoice:${payment_hash}`);
+ if (!invoice) return warn("received lightning with no invoice", request);
- let payment = await db.Payment.create(
- {
- account_id: account.id,
- user_id,
- hash,
- memo,
- amount,
- currency,
- preimage,
- rate,
- received: true,
- confirmed: true,
- network: "lightning",
- tip,
- invoice_id: invoice.id
- },
- { transaction }
- );
+ let { text: hash, currency, memo, rate, tip, user_id } = invoice;
+ let amount = parseInt(received) - tip;
+ if (amount < 0) throw new Error("amount out of range");
- total = amount + tip;
+ let payment = {
+ user_id,
+ hash,
+ memo,
+ amount,
+ currency,
+ secret,
+ rate,
+ tip,
+ invoice_id: invoice.id
+ };
- invoice.status = "paid";
- await invoice.save({ transaction });
+ total = amount + tip;
- await invoice.increment({ received: total }, { transaction });
- await account.increment({ balance: total }, { transaction });
- // get the # of fee credits you would need to pay off this amount of bitcoin
- await account.increment(
- { lightning_credits: computeConversionFee(total) },
- { transaction }
- );
- await account.reload({ transaction });
- await invoice.reload({ transaction });
- await payment.save({ transaction });
- store.payments.push(msg.request);
+ invoice.status = "paid";
+ invoice.received += total;
+ await s(`invoice:${payment_hash}`, invoice);
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
- payment.invoice = invoice.get({ plain: true });
+ let t = async () => {
+ await rd.watch(`user:${user_id}`);
+ user = await g(`user:${user_id}`);
+ user.balance += total;
+ let m = await rd.multi();
+ await s(`user:${user.id}`, user);
+ if (!(await m.exec())) t();
+ };
+ await t();
- callWebhook(invoice, payment);
+ payment.invoice = invoice;
+ callWebhook(invoice, payment);
- emit(user.username, "payment", payment);
- emit(user.username, "account", payment.account);
- notify(user, `Received ${total} SAT`);
+ emit(user.username, "payment", payment);
+ notify(user, `Received ${total} SAT`);
- l(
- "lightning payment received",
- user.username,
- payment.amount,
- payment.tip
- );
- });
+ l("lightning payment received", user.username, payment.amount, payment.tip);
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
}
};
-const poll = async ln => {
- try {
- const wait = async i => {
- const {
- bolt11: request,
- pay_index,
- status,
- msatoshi_received,
- payment_preimage: secret
- } = await ln.waitanyinvoice(i);
-
- let settled = status === "paid";
- let received = parseInt(msatoshi_received / 1000);
-
- await handlePayment({
- request,
- received,
- secret
- });
- await wait(pay_index);
- };
-
- const { invoices } = await ln.listinvoices();
- await wait(Math.max(...invoices.map(i => i.pay_index).filter(n => n)));
- } catch (e) {
- err("problem waiting for c-lightning invoice", e.message);
- }
+let listen = async () => {
+ await handle();
+ listen();
};
-poll(ln);
+listen();