diff --git a/src/services/lnd/liquidityProvider.ts b/src/services/lnd/liquidityProvider.ts index c780ff2a..3ae478c8 100644 --- a/src/services/lnd/liquidityProvider.ts +++ b/src/services/lnd/liquidityProvider.ts @@ -20,6 +20,7 @@ export class LiquidityProvider { ready = false pubDestination: string latestMaxWithdrawable: number | null = null + latestBalance: number | null = null invoicePaidCb: InvoicePaidCb connecting = false readyInterval: NodeJS.Timeout @@ -69,10 +70,20 @@ export class LiquidityProvider { }) } - GetLatestMaxWithdrawable = () => { + GetLatestMaxWithdrawable = async (fetch = false) => { + if (fetch || this.latestMaxWithdrawable === null) { + await this.CheckUserState() + } return this.latestMaxWithdrawable || 0 } + GetLatestBalance = async (fetch = false) => { + if (fetch || this.latestBalance === null) { + await this.CheckUserState() + } + return this.latestBalance || 0 + } + CheckUserState = async () => { const res = await this.client.GetUserInfo() if (res.status === 'ERROR') { @@ -80,6 +91,7 @@ export class LiquidityProvider { return } this.latestMaxWithdrawable = res.max_withdrawable + this.latestBalance = res.balance this.log("latest provider balance:", res.max_withdrawable) return res } diff --git a/src/services/main/liquidityManager.ts b/src/services/main/liquidityManager.ts index cf2d2b37..86287738 100644 --- a/src/services/main/liquidityManager.ts +++ b/src/services/main/liquidityManager.ts @@ -88,7 +88,7 @@ export class LiquidityManager { } beforeOutInvoicePayment = async (amount: number): Promise<'lnd' | 'provider'> => { - const balance = await this.liquidityProvider.GetLatestMaxWithdrawable() + const balance = await this.liquidityProvider.GetLatestMaxWithdrawable(true) if (balance > amount) { this.log("provider has enough balance for payment") return 'provider' diff --git a/src/services/main/watchdog.ts b/src/services/main/watchdog.ts index 77fd1449..07605a75 100644 --- a/src/services/main/watchdog.ts +++ b/src/services/main/watchdog.ts @@ -79,7 +79,7 @@ export class Watchdog { getLogger({ component: "debugLndBalancev3" })({ w: walletBalance, c: channelsBalance, u: usersTotal, f: this.accumulatedHtlcFees }) const totalLightningBalanceMsats = (channelsBalance.localBalance?.msat || 0n) + (channelsBalance.unsettledLocalBalance?.msat || 0n) const totalLightningBalance = Math.ceil(Number(totalLightningBalanceMsats) / 1000) - const providerBalance = this.liquidProvider.GetLatestMaxWithdrawable() + const providerBalance = await this.liquidProvider.GetLatestBalance() return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance }