diff --git a/src/services/lnd/index.ts b/src/services/lnd/index.ts index 78831c36..ded40aaf 100644 --- a/src/services/lnd/index.ts +++ b/src/services/lnd/index.ts @@ -29,6 +29,7 @@ export interface LightningHandler { PayAddress(address: string, amount: number, satPerVByte: number, label?: string): Promise OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise SetMockInvoiceAsPaid(invoice: string, amount: number): Promise + ChannelBalance(): Promise<{ local: number, remote: number }> } export default (settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb): LightningHandler => { diff --git a/src/services/lnd/lnd.ts b/src/services/lnd/lnd.ts index 8d6d8f1a..1076f32b 100644 --- a/src/services/lnd/lnd.ts +++ b/src/services/lnd/lnd.ts @@ -7,7 +7,7 @@ import * as Types from '../../../proto/autogenerated/ts/types.js' import { LightningClient } from '../../../proto/lnd/lightning.client.js' import { InvoicesClient } from '../../../proto/lnd/invoices.client.js' import { RouterClient } from '../../../proto/lnd/router.client.js' -import { GetInfoResponse, AddressType, NewAddressResponse, AddInvoiceResponse, Invoice_InvoiceState, PayReq, Payment_PaymentStatus, Payment, PaymentFailureReason, SendCoinsResponse, EstimateFeeResponse } from '../../../proto/lnd/lightning.js' +import { GetInfoResponse, AddressType, NewAddressResponse, AddInvoiceResponse, Invoice_InvoiceState, PayReq, Payment_PaymentStatus, Payment, PaymentFailureReason, SendCoinsResponse, EstimateFeeResponse, ChannelBalanceResponse } from '../../../proto/lnd/lightning.js' import { OpenChannelReq } from './openChannelReq.js'; import { AddInvoiceReq } from './addInvoiceReq.js'; import { PayInvoiceReq } from './payInvoiceReq.js'; @@ -187,12 +187,15 @@ export default class { return Math.max(0, Math.floor(amount * (1 - this.settings.feeRateLimit) - this.settings.feeFixedLimit)) } + async ChannelBalance(): Promise<{ local: number, remote: number }> { + const res = await this.lightning.channelBalance({}) + const r = res.response + return { local: r.localBalance ? Number(r.localBalance.sat) : 0, remote: r.remoteBalance ? Number(r.remoteBalance.sat) : 0 } + } async PayInvoice(invoice: string, amount: number, feeLimit: number): Promise { await this.Health() - console.log(await this.lightning.channelBalance({})) const abortController = new AbortController() const req = PayInvoiceReq(invoice, amount, feeLimit) - console.log("sending payment:", req) const stream = this.router.sendPaymentV2(req, { abort: abortController.signal }) return new Promise((res, rej) => { stream.responses.onError(error => { diff --git a/src/services/lnd/mock.ts b/src/services/lnd/mock.ts index 6f19ef7f..2945a3f1 100644 --- a/src/services/lnd/mock.ts +++ b/src/services/lnd/mock.ts @@ -95,6 +95,10 @@ export default class { return { feeSat: 1, paymentPreimage: "all_good", valueSat: amt || amount } } + async ChannelBalance(): Promise<{ local: number, remote: number }> { + return { local: 100 * 1000 * 1000, remote: 100 * 1000 * 1000 } + } + async EstimateChainFees(address: string, amount: number, targetConf: number): Promise { throw new Error("EstimateChainFees disabled in mock mode") } diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index b6fa6eef..01a358ed 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -235,10 +235,11 @@ export default class { async GetLnurlPayInfoFromUser(userId: string, linkedApplication: Application, baseUrl?: string): Promise { const payK1 = await this.storage.paymentStorage.AddUserEphemeralKey(userId, 'pay', linkedApplication) const url = baseUrl ? baseUrl : `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle` + const { remote } = await this.lnd.ChannelBalance() return { tag: 'payRequest', callback: `${url}?k1=${payK1.key}`, - maxSendable: this.GetMaxPayableInvoice(payK1.user.balance_sats, true) * 1000, + maxSendable: remote * 1000, minSendable: 10000, metadata: defaultLnurlPayMetadata } @@ -246,10 +247,11 @@ export default class { async GetLnurlPayInfo(payInfoK1: string): Promise { const key = await this.storage.paymentStorage.UseUserEphemeralKey(payInfoK1, 'pay', true) + const { remote } = await this.lnd.ChannelBalance() return { tag: 'payRequest', callback: `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle?k1=${payInfoK1}`, - maxSendable: this.GetMaxPayableInvoice(key.user.balance_sats, true) * 1000, + maxSendable: remote * 1000, minSendable: 10000, metadata: defaultLnurlPayMetadata }