|
| 1 | +import { ClaimStatus, prisma } from "@repo/database"; |
| 2 | +import { getRoutingFee } from "@repo/shared"; |
| 3 | +import { Mutex, type MutexInterface, withTimeout } from "async-mutex"; |
| 4 | +import { Router } from "express"; |
| 5 | +import z from "zod"; |
| 6 | +import { PAYMENT_SUCCESS_EVENT } from "./constants"; |
| 7 | +import { ee } from "./eventEmitter"; |
| 8 | +import { lnGrpcClient, promisifyGrpc, routerGrpcClient } from "./lndClient"; |
| 9 | +import type { Payment__Output } from "./protos/generated/lnrpc/Payment"; |
| 10 | + |
| 11 | +const router = Router(); |
| 12 | + |
| 13 | +const mutexes = new Map<string, MutexInterface>(); |
| 14 | + |
| 15 | +router.get("/", async (req, res) => { |
| 16 | + const { k1, pr } = z.object({ k1: z.string(), pr: z.string() }).parse(req.query); |
| 17 | + |
| 18 | + if (!mutexes.has(k1)) { |
| 19 | + mutexes.set(k1, withTimeout(new Mutex(), 15 * 1000)); // todo |
| 20 | + } |
| 21 | + |
| 22 | + // biome-ignore lint/style/noNonNullAssertion: It is set on the line above |
| 23 | + const mutex = mutexes.get(k1)!; |
| 24 | + |
| 25 | + const release = await mutex.acquire(); |
| 26 | + |
| 27 | + const claim = await prisma.claims.findUnique({ where: { id: k1 } }); |
| 28 | + |
| 29 | + if (!claim || claim.status !== ClaimStatus.PAID) { |
| 30 | + release(); |
| 31 | + |
| 32 | + res.json({ |
| 33 | + status: "ERROR", |
| 34 | + reason: "Claim not found, already claimed or not paid for yet.", |
| 35 | + }); |
| 36 | + |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const result = await promisifyGrpc(lnGrpcClient.DecodePayReq.bind(lnGrpcClient), { |
| 41 | + payReq: pr, |
| 42 | + }); |
| 43 | + |
| 44 | + if (!result) { |
| 45 | + release(); |
| 46 | + |
| 47 | + res.json({ |
| 48 | + status: "ERROR", |
| 49 | + reason: "Invalid pr.", |
| 50 | + }); |
| 51 | + |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (Number(result.numSatoshis) !== claim.receiverSatsAmount) { |
| 56 | + release(); |
| 57 | + |
| 58 | + res.json({ |
| 59 | + status: "ERROR", |
| 60 | + reason: `Claim for ${result.numSatoshis} sats doesn't match the expected claim of ${claim.receiverSatsAmount} sats.`, |
| 61 | + }); |
| 62 | + |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + res.json({ status: "OK" }); |
| 67 | + |
| 68 | + const stream = routerGrpcClient.SendPaymentV2({ |
| 69 | + paymentRequest: pr, |
| 70 | + feeLimitSat: getRoutingFee(claim.receiverSatsAmount), |
| 71 | + timeoutSeconds: 15, // todo |
| 72 | + }); |
| 73 | + |
| 74 | + stream.on("data", async (data: Payment__Output) => { |
| 75 | + if (data.status === "FAILED") { |
| 76 | + release(); |
| 77 | + } |
| 78 | + |
| 79 | + if (data.status === "SUCCEEDED") { |
| 80 | + await prisma.claims.update({ |
| 81 | + where: { id: k1 }, |
| 82 | + data: { status: ClaimStatus.CLAIMED }, |
| 83 | + }); |
| 84 | + |
| 85 | + ee.emit(PAYMENT_SUCCESS_EVENT, claim.paymentRequest); |
| 86 | + |
| 87 | + release(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + stream.on("error", (err) => { |
| 92 | + console.log("Error happened during payment stream: ", err); |
| 93 | + |
| 94 | + release(); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +router.get("/:claimId", async (req, res) => { |
| 99 | + const claim = await prisma.claims.findUnique({ where: { id: req.params.claimId } }); |
| 100 | + |
| 101 | + if (!claim || claim.status !== ClaimStatus.PAID) { |
| 102 | + res.json({ |
| 103 | + status: "ERROR", |
| 104 | + reason: "Claim not found, already claimed or not paid for yet.", |
| 105 | + }); |
| 106 | + |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const receiverMilliSats = claim.receiverSatsAmount * 1000; |
| 111 | + |
| 112 | + res.json({ |
| 113 | + tag: "withdrawRequest", |
| 114 | + callback: "https://stackorange.com/api/withdraw", |
| 115 | + k1: claim.id, |
| 116 | + defaultDescription: "Orange pill from stackorange.com", |
| 117 | + minWithdrawable: receiverMilliSats, |
| 118 | + maxWithdrawable: receiverMilliSats, |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +export { router }; |
0 commit comments