fetch if null

This commit is contained in:
boufni95 2024-06-20 18:32:00 +02:00
parent 4df7a328ee
commit b160ccef68
3 changed files with 15 additions and 3 deletions

View file

@ -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
}

View file

@ -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'

View file

@ -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
}