fix invoice source
This commit is contained in:
parent
eb5f95ec8f
commit
85beca91c9
3 changed files with 17 additions and 23 deletions
|
|
@ -6,7 +6,6 @@ import { getLogger } from '../helpers/logger.js'
|
||||||
import { NostrEvent, NostrSend } from '../nostr/handler.js'
|
import { NostrEvent, NostrSend } from '../nostr/handler.js'
|
||||||
import { relayInit } from '../nostr/tools/relay.js'
|
import { relayInit } from '../nostr/tools/relay.js'
|
||||||
import { InvoicePaidCb } from './settings.js'
|
import { InvoicePaidCb } from './settings.js'
|
||||||
|
|
||||||
export type LiquidityRequest = { action: 'spend' | 'receive', amount: number }
|
export type LiquidityRequest = { action: 'spend' | 'receive', amount: number }
|
||||||
|
|
||||||
export type nostrCallback<T> = { startedAtMillis: number, type: 'single' | 'stream', f: (res: T) => void }
|
export type nostrCallback<T> = { startedAtMillis: number, type: 'single' | 'stream', f: (res: T) => void }
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,7 @@ export default class {
|
||||||
log = getLogger({ component: 'lndManager' })
|
log = getLogger({ component: 'lndManager' })
|
||||||
outgoingOpsLocked = false
|
outgoingOpsLocked = false
|
||||||
liquidProvider: LiquidityProvider
|
liquidProvider: LiquidityProvider
|
||||||
useOnlyLiquidityProvider = false
|
constructor(settings: LndSettings, liquidProvider: LiquidityProvider, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb) {
|
||||||
constructor(settings: LndSettings, provider: { liquidProvider: LiquidityProvider, useOnly?: boolean }, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb) {
|
|
||||||
this.settings = settings
|
this.settings = settings
|
||||||
this.addressPaidCb = addressPaidCb
|
this.addressPaidCb = addressPaidCb
|
||||||
this.invoicePaidCb = invoicePaidCb
|
this.invoicePaidCb = invoicePaidCb
|
||||||
|
|
@ -63,8 +62,7 @@ export default class {
|
||||||
this.invoices = new InvoicesClient(transport)
|
this.invoices = new InvoicesClient(transport)
|
||||||
this.router = new RouterClient(transport)
|
this.router = new RouterClient(transport)
|
||||||
this.chainNotifier = new ChainNotifierClient(transport)
|
this.chainNotifier = new ChainNotifierClient(transport)
|
||||||
this.liquidProvider = provider.liquidProvider
|
this.liquidProvider = liquidProvider
|
||||||
this.useOnlyLiquidityProvider = !!provider.useOnly
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LockOutgoingOperations(): void {
|
LockOutgoingOperations(): void {
|
||||||
|
|
@ -82,20 +80,6 @@ export default class {
|
||||||
this.liquidProvider.Stop()
|
this.liquidProvider.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
async ShouldUseLiquidityProvider(req: LiquidityRequest): Promise<boolean> {
|
|
||||||
if (this.useOnlyLiquidityProvider) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if (!this.liquidProvider.CanProviderHandle(req)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
const channels = await this.ListChannels()
|
|
||||||
if (channels.channels.length === 0) {
|
|
||||||
this.log("no channels, will use liquidity provider")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
async Warmup() {
|
async Warmup() {
|
||||||
this.SubscribeAddressPaid()
|
this.SubscribeAddressPaid()
|
||||||
this.SubscribeInvoicePaid()
|
this.SubscribeInvoicePaid()
|
||||||
|
|
@ -272,8 +256,7 @@ export default class {
|
||||||
async NewInvoice(value: number, memo: string, expiry: number, useProvider = false): Promise<Invoice> {
|
async NewInvoice(value: number, memo: string, expiry: number, useProvider = false): Promise<Invoice> {
|
||||||
this.log("generating new invoice for", value, "sats")
|
this.log("generating new invoice for", value, "sats")
|
||||||
await this.Health()
|
await this.Health()
|
||||||
const shouldUseLiquidityProvider = await this.ShouldUseLiquidityProvider({ action: 'receive', amount: value })
|
if (useProvider) {
|
||||||
if (shouldUseLiquidityProvider || useProvider) {
|
|
||||||
const invoice = await this.liquidProvider.AddInvoice(value, memo)
|
const invoice = await this.liquidProvider.AddInvoice(value, memo)
|
||||||
const providerDst = this.liquidProvider.GetProviderDestination()
|
const providerDst = this.liquidProvider.GetProviderDestination()
|
||||||
return { payRequest: invoice, providerDst }
|
return { payRequest: invoice, providerDst }
|
||||||
|
|
@ -308,8 +291,7 @@ export default class {
|
||||||
}
|
}
|
||||||
await this.Health()
|
await this.Health()
|
||||||
this.log("paying invoice", invoice, "for", amount, "sats")
|
this.log("paying invoice", invoice, "for", amount, "sats")
|
||||||
const shouldUseLiquidityProvider = await this.ShouldUseLiquidityProvider({ action: 'spend', amount })
|
if (useProvider) {
|
||||||
if (shouldUseLiquidityProvider || useProvider) {
|
|
||||||
const res = await this.liquidProvider.PayInvoice(invoice)
|
const res = await this.liquidProvider.PayInvoice(invoice)
|
||||||
const providerDst = this.liquidProvider.GetProviderDestination()
|
const providerDst = this.liquidProvider.GetProviderDestination()
|
||||||
return { feeSat: res.network_fee + res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage, providerDst }
|
return { feeSat: res.network_fee + res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage, providerDst }
|
||||||
|
|
|
||||||
|
|
@ -46,11 +46,20 @@ export class LiquidityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeInvoiceCreation = async (amount: number): Promise<'lnd' | 'provider'> => {
|
beforeInvoiceCreation = async (amount: number): Promise<'lnd' | 'provider'> => {
|
||||||
|
if (this.settings.useOnlyLiquidityProvider) {
|
||||||
|
return 'provider'
|
||||||
|
}
|
||||||
|
|
||||||
const { remote } = await this.lnd.ChannelBalance()
|
const { remote } = await this.lnd.ChannelBalance()
|
||||||
if (remote > amount) {
|
if (remote > amount) {
|
||||||
this.log("channel has enough balance for invoice")
|
this.log("channel has enough balance for invoice")
|
||||||
return 'lnd'
|
return 'lnd'
|
||||||
}
|
}
|
||||||
|
const providerCanHandle = this.liquidityProvider.CanProviderHandle({ action: 'receive', amount })
|
||||||
|
if (!providerCanHandle) {
|
||||||
|
return 'lnd'
|
||||||
|
}
|
||||||
|
|
||||||
this.log("channel does not have enough balance for invoice,suggesting provider")
|
this.log("channel does not have enough balance for invoice,suggesting provider")
|
||||||
return 'provider'
|
return 'provider'
|
||||||
}
|
}
|
||||||
|
|
@ -94,6 +103,10 @@ export class LiquidityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeOutInvoicePayment = async (amount: number): Promise<'lnd' | 'provider'> => {
|
beforeOutInvoicePayment = async (amount: number): Promise<'lnd' | 'provider'> => {
|
||||||
|
if (this.settings.useOnlyLiquidityProvider) {
|
||||||
|
return 'provider'
|
||||||
|
}
|
||||||
|
|
||||||
const balance = await this.liquidityProvider.GetLatestMaxWithdrawable(true)
|
const balance = await this.liquidityProvider.GetLatestMaxWithdrawable(true)
|
||||||
if (balance > amount) {
|
if (balance > amount) {
|
||||||
this.log("provider has enough balance for payment")
|
this.log("provider has enough balance for payment")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue