fetch if null
This commit is contained in:
parent
4df7a328ee
commit
b160ccef68
3 changed files with 15 additions and 3 deletions
|
|
@ -20,6 +20,7 @@ export class LiquidityProvider {
|
||||||
ready = false
|
ready = false
|
||||||
pubDestination: string
|
pubDestination: string
|
||||||
latestMaxWithdrawable: number | null = null
|
latestMaxWithdrawable: number | null = null
|
||||||
|
latestBalance: number | null = null
|
||||||
invoicePaidCb: InvoicePaidCb
|
invoicePaidCb: InvoicePaidCb
|
||||||
connecting = false
|
connecting = false
|
||||||
readyInterval: NodeJS.Timeout
|
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
|
return this.latestMaxWithdrawable || 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GetLatestBalance = async (fetch = false) => {
|
||||||
|
if (fetch || this.latestBalance === null) {
|
||||||
|
await this.CheckUserState()
|
||||||
|
}
|
||||||
|
return this.latestBalance || 0
|
||||||
|
}
|
||||||
|
|
||||||
CheckUserState = async () => {
|
CheckUserState = async () => {
|
||||||
const res = await this.client.GetUserInfo()
|
const res = await this.client.GetUserInfo()
|
||||||
if (res.status === 'ERROR') {
|
if (res.status === 'ERROR') {
|
||||||
|
|
@ -80,6 +91,7 @@ export class LiquidityProvider {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.latestMaxWithdrawable = res.max_withdrawable
|
this.latestMaxWithdrawable = res.max_withdrawable
|
||||||
|
this.latestBalance = res.balance
|
||||||
this.log("latest provider balance:", res.max_withdrawable)
|
this.log("latest provider balance:", res.max_withdrawable)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ export class LiquidityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeOutInvoicePayment = async (amount: number): Promise<'lnd' | 'provider'> => {
|
beforeOutInvoicePayment = async (amount: number): Promise<'lnd' | 'provider'> => {
|
||||||
const balance = await this.liquidityProvider.GetLatestMaxWithdrawable()
|
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")
|
||||||
return 'provider'
|
return 'provider'
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ export class Watchdog {
|
||||||
getLogger({ component: "debugLndBalancev3" })({ w: walletBalance, c: channelsBalance, u: usersTotal, f: this.accumulatedHtlcFees })
|
getLogger({ component: "debugLndBalancev3" })({ w: walletBalance, c: channelsBalance, u: usersTotal, f: this.accumulatedHtlcFees })
|
||||||
const totalLightningBalanceMsats = (channelsBalance.localBalance?.msat || 0n) + (channelsBalance.unsettledLocalBalance?.msat || 0n)
|
const totalLightningBalanceMsats = (channelsBalance.localBalance?.msat || 0n) + (channelsBalance.unsettledLocalBalance?.msat || 0n)
|
||||||
const totalLightningBalance = Math.ceil(Number(totalLightningBalanceMsats) / 1000)
|
const totalLightningBalance = Math.ceil(Number(totalLightningBalanceMsats) / 1000)
|
||||||
const providerBalance = this.liquidProvider.GetLatestMaxWithdrawable()
|
const providerBalance = await this.liquidProvider.GetLatestBalance()
|
||||||
return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance
|
return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue