diff --git a/src/services/lnd/index.ts b/src/services/lnd/index.ts index 2e114c5f..0365b06d 100644 --- a/src/services/lnd/index.ts +++ b/src/services/lnd/index.ts @@ -1,5 +1,5 @@ import * as Types from '../../../proto/autogenerated/ts/types.js' -import { GetInfoResponse, NewAddressResponse, AddInvoiceResponse, PayReq, Payment, SendCoinsResponse, EstimateFeeResponse, TransactionDetails, ClosedChannelsResponse, ListChannelsResponse, PendingChannelsResponse, ListInvoiceResponse, ListPaymentsResponse, ChannelBalanceResponse } from '../../../proto/lnd/lightning.js' +import { GetInfoResponse, NewAddressResponse, AddInvoiceResponse, PayReq, Payment, SendCoinsResponse, EstimateFeeResponse, TransactionDetails, ClosedChannelsResponse, ListChannelsResponse, PendingChannelsResponse, ListInvoiceResponse, ListPaymentsResponse, ChannelBalanceResponse, WalletBalanceResponse } from '../../../proto/lnd/lightning.js' import { EnvMustBeNonEmptyString, EnvMustBeInteger, EnvCanBeBoolean } from '../helpers/envParser.js' import { AddressPaidCb, BalanceInfo, DecodedInvoice, HtlcCb, Invoice, InvoicePaidCb, LndSettings, NewBlockCb, NodeInfo, PaidInvoice } from './settings.js' import LND from './lnd.js' @@ -32,6 +32,7 @@ export interface LightningHandler { ChannelBalance(): Promise<{ local: number, remote: number }> GetTransactions(startHeight: number): Promise GetBalance(): Promise + GetWalletBalance(): Promise GetChannelBalance(): Promise ListClosedChannels(): Promise ListChannels(): Promise diff --git a/src/services/lnd/lnd.ts b/src/services/lnd/lnd.ts index 919322e3..14b4f0e8 100644 --- a/src/services/lnd/lnd.ts +++ b/src/services/lnd/lnd.ts @@ -322,6 +322,11 @@ export default class { return res.response } + async GetWalletBalance() { + const res = await this.lightning.walletBalance({}, DeadLineMetadata()) + return res.response + } + async GetBalance(): Promise { const wRes = await this.lightning.walletBalance({}, DeadLineMetadata()) const { confirmedBalance, unconfirmedBalance, totalBalance } = wRes.response diff --git a/src/services/lnd/mock.ts b/src/services/lnd/mock.ts index 83806857..07246b30 100644 --- a/src/services/lnd/mock.ts +++ b/src/services/lnd/mock.ts @@ -7,7 +7,7 @@ import * as Types from '../../../proto/autogenerated/ts/types.js' import { LightningClient } from '../../../proto/lnd/lightning.client.js' import { InvoicesClient } from '../../../proto/lnd/invoices.client.js' import { RouterClient } from '../../../proto/lnd/router.client.js' -import { GetInfoResponse, AddressType, NewAddressResponse, AddInvoiceResponse, Invoice_InvoiceState, PayReq, Payment_PaymentStatus, Payment, PaymentFailureReason, SendCoinsResponse, EstimateFeeResponse, TransactionDetails, ClosedChannelsResponse, ListChannelsResponse, PendingChannelsResponse, ListInvoiceResponse, ListPaymentsResponse, ChannelBalanceResponse } from '../../../proto/lnd/lightning.js' +import { GetInfoResponse, AddressType, NewAddressResponse, AddInvoiceResponse, Invoice_InvoiceState, PayReq, Payment_PaymentStatus, Payment, PaymentFailureReason, SendCoinsResponse, EstimateFeeResponse, TransactionDetails, ClosedChannelsResponse, ListChannelsResponse, PendingChannelsResponse, ListInvoiceResponse, ListPaymentsResponse, ChannelBalanceResponse, WalletBalanceResponse } from '../../../proto/lnd/lightning.js' import { OpenChannelReq } from './openChannelReq.js'; import { AddInvoiceReq } from './addInvoiceReq.js'; import { PayInvoiceReq } from './payInvoiceReq.js'; @@ -41,6 +41,7 @@ export default class { Stop() { } async Warmup() { } + async GetWalletBalance(): Promise { throw new Error("ListClosedChannels disabled in mock mode") } async ListClosedChannels(): Promise { throw new Error("ListClosedChannels disabled in mock mode") } async ListChannels(): Promise { throw new Error("ListChannels disabled in mock mode") } async ListPendingChannels(): Promise { throw new Error("ListPendingChannels disabled in mock mode") } diff --git a/src/services/main/watchdog.ts b/src/services/main/watchdog.ts index 378cbf63..b367a24d 100644 --- a/src/services/main/watchdog.ts +++ b/src/services/main/watchdog.ts @@ -51,24 +51,12 @@ export class Watchdog { getTotalLndBalance = async (usersTotal: number) => { - const localLog = getLogger({ appName: "debugLndBalancev2" }) - getLogger({ appName: "debugChannelBalanceRpc" })(await this.lnd.GetChannelBalance()) - const { confirmedBalance, channelsBalance } = await this.lnd.GetBalance() - this.log(confirmedBalance, "sats in chain wallet") - localLog({ c: channelsBalance, u: usersTotal }) - let totalBalance = confirmedBalance - channelsBalance.forEach(c => { - let totalBalanceInHtlcs = 0 - c.htlcs.forEach(htlc => { - if (htlc.incoming) { - totalBalanceInHtlcs += htlc.amount - } else { - //totalBalanceInHtlcs -= htlc.amount - } - }) - totalBalance += c.localBalanceSats + totalBalanceInHtlcs - }) - return totalBalance + const walletBalance = await this.lnd.GetWalletBalance() + this.log(Number(walletBalance.confirmedBalance), "sats in chain wallet") + const channelsBalance = await this.lnd.GetChannelBalance() + getLogger({ appName: "debugLndBalancev3" })({ w: walletBalance, c: channelsBalance, u: usersTotal }) + const localChannelsBalance = Number(channelsBalance.localBalance?.sat || 0) + return Number(walletBalance.confirmedBalance) + localChannelsBalance } checkBalanceUpdate = (deltaLnd: number, deltaUsers: number) => {