From 29879a027c0104da673e621db34939484786109d Mon Sep 17 00:00:00 2001 From: boufni95 Date: Wed, 26 Jun 2024 20:42:16 +0200 Subject: [PATCH] fix watchdog --- src/services/lnd/lsp.ts | 23 +++++++++++++---------- src/services/main/liquidityManager.ts | 13 ++++++++++++- src/services/main/paymentManager.ts | 2 +- src/services/main/watchdog.ts | 8 ++++++-- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/services/lnd/lsp.ts b/src/services/lnd/lsp.ts index b8d85edc..82a1ac32 100644 --- a/src/services/lnd/lsp.ts +++ b/src/services/lnd/lsp.ts @@ -140,8 +140,9 @@ export class FlashsatsLSP extends LSP { return null } const res = await this.liquidityProvider.PayInvoice(order.payment.bolt11_invoice) - this.log("paid", res.amount_paid, "to open channel") - return { orderId: order.order_id, invoice: order.payment.bolt11_invoice, totalSats: +order.payment.order_total_sat, fees: +order.payment.fee_total_sat } + const fees = +order.payment.fee_total_sat + res.network_fee + res.service_fee + this.log("paid", res.amount_paid, "to open channel, and a fee of", fees) + return { orderId: order.order_id, invoice: order.payment.bolt11_invoice, totalSats: +order.payment.order_total_sat, fees } } getInfo = async () => { @@ -215,8 +216,9 @@ export class OlympusLSP extends LSP { return null } const res = await this.liquidityProvider.PayInvoice(order.payment.bolt11.invoice) - this.log("paid", res.amount_paid, "to open channel") - return { orderId: order.order_id, invoice: order.payment.bolt11.invoice, totalSats: +order.payment.bolt11.order_total_sat, fees: +order.payment.bolt11.fee_total_sat } + const fees = +order.payment.bolt11.fee_total_sat + res.network_fee + res.service_fee + this.log("paid", res.amount_paid, "to open channel, and a fee of", fees) + return { orderId: order.order_id, invoice: order.payment.bolt11.invoice, totalSats: +order.payment.bolt11.order_total_sat, fees } } getInfo = async () => { @@ -307,17 +309,18 @@ export class VoltageLSP extends LSP { await this.addPeer(info.pubkey, `${ipv4.address}:${ipv4.port}`) const invoice = await this.lnd.NewInvoice(this.settings.channelThreshold, "open channel", 60 * 60) - const res = await this.proposal(invoice.payRequest, fee.id) - this.log("proposal res", res, fee.id) - const decoded = await this.lnd.DecodeInvoice(res.jit_bolt11) + const proposalRes = await this.proposal(invoice.payRequest, fee.id) + this.log("proposal res", proposalRes, fee.id) + const decoded = await this.lnd.DecodeInvoice(proposalRes.jit_bolt11) if (decoded.numSatoshis !== this.settings.channelThreshold + feeSats) { this.log("invoice of amount", decoded.numSatoshis, "does not match expected amount of", this.settings.channelThreshold + feeSats) return null } - const invoiceRes = await this.liquidityProvider.PayInvoice(res.jit_bolt11) - this.log("paid", invoiceRes.amount_paid, "to open channel") - return { orderId: fee.id, invoice: res.jit_bolt11, totalSats: decoded.numSatoshis, fees: feeSats } + const res = await this.liquidityProvider.PayInvoice(proposalRes.jit_bolt11) + const fees = feeSats + res.network_fee + res.service_fee + this.log("paid", res.amount_paid, "to open channel, and a fee of", fees) + return { orderId: fee.id, invoice: proposalRes.jit_bolt11, totalSats: decoded.numSatoshis, fees } } proposal = async (bolt11: string, feeId: string) => { diff --git a/src/services/main/liquidityManager.ts b/src/services/main/liquidityManager.ts index 4bd4be32..044517f1 100644 --- a/src/services/main/liquidityManager.ts +++ b/src/services/main/liquidityManager.ts @@ -25,6 +25,7 @@ export class LiquidityManager { log = getLogger({ component: "liquidityManager" }) channelRequested = false channelRequesting = false + feesPaid = 0 constructor(settings: LiquiditySettings, storage: Storage, liquidityProvider: LiquidityProvider, lnd: LND) { this.settings = settings this.storage = storage @@ -34,6 +35,11 @@ export class LiquidityManager { this.voltageLSP = new VoltageLSP(settings.lspSettings, lnd, liquidityProvider) this.flashsatsLSP = new FlashsatsLSP(settings.lspSettings, lnd, liquidityProvider) } + + GetPaidFees = () => { + return this.feesPaid + } + onNewBlock = async () => { const balance = await this.liquidityProvider.GetLatestMaxWithdrawable() const { remote } = await this.lnd.ChannelBalance() @@ -41,7 +47,9 @@ export class LiquidityManager { this.log("draining provider balance to channel") const invoice = await this.lnd.NewInvoice(balance, "liqudity provider drain", defaultInvoiceExpiry) const res = await this.liquidityProvider.PayInvoice(invoice.payRequest) - this.log("drained provider balance to channel", res.amount_paid) + const fees = res.network_fee + res.service_fee + this.log("drained provider balance to channel", res.amount_paid, "fees paid:", fees) + this.feesPaid += fees } } @@ -78,6 +86,7 @@ export class LiquidityManager { this.log("requested channel from olympus") this.channelRequested = true this.channelRequesting = false + this.feesPaid += olympusOk.fees await this.storage.liquidityStorage.SaveLspOrder({ service_name: 'olympus', invoice: olympusOk.invoice, total_paid: olympusOk.totalSats, order_id: olympusOk.orderId, fees: olympusOk.fees }) return } @@ -86,6 +95,7 @@ export class LiquidityManager { this.log("requested channel from voltage") this.channelRequested = true this.channelRequesting = false + this.feesPaid += voltageOk.fees await this.storage.liquidityStorage.SaveLspOrder({ service_name: 'voltage', invoice: voltageOk.invoice, total_paid: voltageOk.totalSats, order_id: voltageOk.orderId, fees: voltageOk.fees }) return } @@ -95,6 +105,7 @@ export class LiquidityManager { this.log("requested channel from flashsats") this.channelRequested = true this.channelRequesting = false + this.feesPaid += flashsatsOk.fees await this.storage.liquidityStorage.SaveLspOrder({ service_name: 'flashsats', invoice: flashsatsOk.invoice, total_paid: flashsatsOk.totalSats, order_id: flashsatsOk.orderId, fees: flashsatsOk.fees }) return } diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index 4dd3865e..d80d7965 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -53,8 +53,8 @@ export default class { this.storage = storage this.settings = settings this.lnd = lnd - this.watchDog = new Watchdog(settings.watchDogSettings, lnd, storage) this.liquidityManager = liquidityManager + this.watchDog = new Watchdog(settings.watchDogSettings, this.liquidityManager, lnd, storage) this.addressPaidCb = addressPaidCb this.invoicePaidCb = invoicePaidCb } diff --git a/src/services/main/watchdog.ts b/src/services/main/watchdog.ts index 7fa23605..4e1b83dc 100644 --- a/src/services/main/watchdog.ts +++ b/src/services/main/watchdog.ts @@ -5,6 +5,7 @@ import { LiquidityProvider } from "../lnd/liquidityProvider.js"; import LND from "../lnd/lnd.js"; import { ChannelBalance } from "../lnd/settings.js"; import Storage from '../storage/index.js' +import { LiquidityManager } from "./liquidityManager.js"; export type WatchdogSettings = { maxDiffSats: number } @@ -22,17 +23,19 @@ export class Watchdog { accumulatedHtlcFees: number; lnd: LND; liquidProvider: LiquidityProvider; + liquidityManager: LiquidityManager; settings: WatchdogSettings; storage: Storage; latestCheckStart = 0 log = getLogger({ component: "watchdog" }) ready = false interval: NodeJS.Timer; - constructor(settings: WatchdogSettings, lnd: LND, storage: Storage) { + constructor(settings: WatchdogSettings, liquidityManager: LiquidityManager, lnd: LND, storage: Storage) { this.lnd = lnd; this.settings = settings; this.storage = storage; this.liquidProvider = lnd.liquidProvider + this.liquidityManager = liquidityManager this.queue = new FunctionQueue("watchdog_queue", () => this.StartCheck()) } @@ -102,7 +105,8 @@ 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) - return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance + const feesPaidForLiquidity = this.liquidityManager.GetPaidFees() + return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance + feesPaidForLiquidity } checkBalanceUpdate = (deltaLnd: number, deltaUsers: number) => {