From d3212974809efb2d171a20f9722a5fa6ff1da8a9 Mon Sep 17 00:00:00 2001 From: boufni95 Date: Tue, 1 Oct 2024 15:00:26 +0000 Subject: [PATCH] pending payment from hash --- src/services/lnd/lnd.ts | 21 ++++++++++++++++++++- src/services/main/paymentManager.ts | 23 +++++------------------ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/services/lnd/lnd.ts b/src/services/lnd/lnd.ts index 2d445d07..0cf25ba9 100644 --- a/src/services/lnd/lnd.ts +++ b/src/services/lnd/lnd.ts @@ -14,7 +14,7 @@ import { AddInvoiceReq } from './addInvoiceReq.js'; import { PayInvoiceReq } from './payInvoiceReq.js'; import { SendCoinsReq } from './sendCoinsReq.js'; import { LndSettings, AddressPaidCb, InvoicePaidCb, NodeInfo, Invoice, DecodedInvoice, PaidInvoice, NewBlockCb, HtlcCb, BalanceInfo } from './settings.js'; -import { getLogger } from '../helpers/logger.js'; +import { ERROR, getLogger } from '../helpers/logger.js'; import { HtlcEvent_EventType } from '../../../proto/lnd/router.js'; import { LiquidityProvider, LiquidityRequest } from '../main/liquidityProvider.js'; import { Utils } from '../helpers/utilsWrapper.js'; @@ -464,6 +464,25 @@ export default class { return res.response } + async GetPaymentFromHash(paymentHash: string): Promise { + const abortController = new AbortController() + const stream = this.router.trackPaymentV2({ + paymentHash: Buffer.from(paymentHash, 'hex'), + noInflightUpdates: false + }, { abort: abortController.signal }) + return new Promise((res, rej) => { + stream.responses.onError(error => { + this.log(ERROR, "error with trackPaymentV2", error.message) + rej(null) + }) + stream.responses.onMessage(payment => { + abortController.abort() + res(payment) + }) + }) + + } + async ListPeers() { const res = await this.lightning.listPeers({ latestError: true }, DeadLineMetadata()) return res.response diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index 4e0723c0..b6e30a72 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -6,7 +6,7 @@ import { MainSettings } from './settings.js' import { InboundOptionals, defaultInvoiceExpiry } from '../storage/paymentStorage.js' import LND from '../lnd/lnd.js' import { Application } from '../storage/entity/Application.js' -import { getLogger, PubLogger } from '../helpers/logger.js' +import { ERROR, getLogger, PubLogger } from '../helpers/logger.js' import { UserReceivingAddress } from '../storage/entity/UserReceivingAddress.js' import { AddressPaidCb, InvoicePaidCb, PaidInvoice } from '../lnd/settings.js' import { UserReceivingInvoice, ZapInfo } from '../storage/entity/UserReceivingInvoice.js' @@ -124,23 +124,10 @@ export default class { } checkPendingLndPayment = async (log: PubLogger, p: UserInvoicePayment) => { - if (p.paymentIndex === 0 || p.paymentIndex === -1) { - log("found a pending payment with no payment index, skipping", p.serial_id) - //const fullAmount = p.paid_amount + p.service_fees + p.routing_fees - //log("found a pending payment with no payment index, refunding", fullAmount, "sats to user", p.user.user_id) - //await this.storage.txQueue.PushToQueue({ - // dbTx: true, description: "refund failed pending payment", exec: async tx => { - // await this.storage.userStorage.IncrementUserBalance(p.user.user_id, fullAmount, "payment_refund:" + p.invoice, tx) - // await this.storage.paymentStorage.UpdateExternalPayment(p.serial_id, 0, 0, false) - // } - //}) - return - } - console.log({ p }) - const paymentRes = await this.lnd.GetPayment(p.paymentIndex) - const payment = paymentRes.payments[0] - if (!payment || Number(payment.paymentIndex) !== p.paymentIndex) { - log("lnd payment not found for pending payment", p.serial_id, "with index", p.paymentIndex) + const decoded = await this.lnd.DecodeInvoice(p.invoice) + const payment = await this.lnd.GetPaymentFromHash(decoded.paymentHash) + if (!payment || payment.paymentHash !== decoded.paymentHash) { + log(ERROR, "lnd payment not found for pending payment hash ", decoded.paymentHash) return }