max sendable lnurl

This commit is contained in:
boufni95 2023-10-23 21:17:50 +02:00
parent 91cf26e3b2
commit 149b009911
4 changed files with 15 additions and 5 deletions

View file

@ -29,6 +29,7 @@ export interface LightningHandler {
PayAddress(address: string, amount: number, satPerVByte: number, label?: string): Promise<SendCoinsResponse> PayAddress(address: string, amount: number, satPerVByte: number, label?: string): Promise<SendCoinsResponse>
OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string> OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string>
SetMockInvoiceAsPaid(invoice: string, amount: number): Promise<void> SetMockInvoiceAsPaid(invoice: string, amount: number): Promise<void>
ChannelBalance(): Promise<{ local: number, remote: number }>
} }
export default (settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb): LightningHandler => { export default (settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb): LightningHandler => {

View file

@ -7,7 +7,7 @@ import * as Types from '../../../proto/autogenerated/ts/types.js'
import { LightningClient } from '../../../proto/lnd/lightning.client.js' import { LightningClient } from '../../../proto/lnd/lightning.client.js'
import { InvoicesClient } from '../../../proto/lnd/invoices.client.js' import { InvoicesClient } from '../../../proto/lnd/invoices.client.js'
import { RouterClient } from '../../../proto/lnd/router.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 { OpenChannelReq } from './openChannelReq.js';
import { AddInvoiceReq } from './addInvoiceReq.js'; import { AddInvoiceReq } from './addInvoiceReq.js';
import { PayInvoiceReq } from './payInvoiceReq.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)) 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<PaidInvoice> { async PayInvoice(invoice: string, amount: number, feeLimit: number): Promise<PaidInvoice> {
await this.Health() await this.Health()
console.log(await this.lightning.channelBalance({}))
const abortController = new AbortController() const abortController = new AbortController()
const req = PayInvoiceReq(invoice, amount, feeLimit) const req = PayInvoiceReq(invoice, amount, feeLimit)
console.log("sending payment:", req)
const stream = this.router.sendPaymentV2(req, { abort: abortController.signal }) const stream = this.router.sendPaymentV2(req, { abort: abortController.signal })
return new Promise((res, rej) => { return new Promise((res, rej) => {
stream.responses.onError(error => { stream.responses.onError(error => {

View file

@ -95,6 +95,10 @@ export default class {
return { feeSat: 1, paymentPreimage: "all_good", valueSat: amt || amount } 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<EstimateFeeResponse> { async EstimateChainFees(address: string, amount: number, targetConf: number): Promise<EstimateFeeResponse> {
throw new Error("EstimateChainFees disabled in mock mode") throw new Error("EstimateChainFees disabled in mock mode")
} }

View file

@ -235,10 +235,11 @@ export default class {
async GetLnurlPayInfoFromUser(userId: string, linkedApplication: Application, baseUrl?: string): Promise<Types.LnurlPayInfoResponse> { async GetLnurlPayInfoFromUser(userId: string, linkedApplication: Application, baseUrl?: string): Promise<Types.LnurlPayInfoResponse> {
const payK1 = await this.storage.paymentStorage.AddUserEphemeralKey(userId, 'pay', linkedApplication) const payK1 = await this.storage.paymentStorage.AddUserEphemeralKey(userId, 'pay', linkedApplication)
const url = baseUrl ? baseUrl : `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle` const url = baseUrl ? baseUrl : `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle`
const { remote } = await this.lnd.ChannelBalance()
return { return {
tag: 'payRequest', tag: 'payRequest',
callback: `${url}?k1=${payK1.key}`, callback: `${url}?k1=${payK1.key}`,
maxSendable: this.GetMaxPayableInvoice(payK1.user.balance_sats, true) * 1000, maxSendable: remote * 1000,
minSendable: 10000, minSendable: 10000,
metadata: defaultLnurlPayMetadata metadata: defaultLnurlPayMetadata
} }
@ -246,10 +247,11 @@ export default class {
async GetLnurlPayInfo(payInfoK1: string): Promise<Types.LnurlPayInfoResponse> { async GetLnurlPayInfo(payInfoK1: string): Promise<Types.LnurlPayInfoResponse> {
const key = await this.storage.paymentStorage.UseUserEphemeralKey(payInfoK1, 'pay', true) const key = await this.storage.paymentStorage.UseUserEphemeralKey(payInfoK1, 'pay', true)
const { remote } = await this.lnd.ChannelBalance()
return { return {
tag: 'payRequest', tag: 'payRequest',
callback: `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle?k1=${payInfoK1}`, 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, minSendable: 10000,
metadata: defaultLnurlPayMetadata metadata: defaultLnurlPayMetadata
} }