fix watchdog

This commit is contained in:
boufni95 2024-06-26 20:42:16 +02:00
parent 1d381d6694
commit 29879a027c
4 changed files with 32 additions and 14 deletions

View file

@ -140,8 +140,9 @@ export class FlashsatsLSP extends LSP {
return null return null
} }
const res = await this.liquidityProvider.PayInvoice(order.payment.bolt11_invoice) const res = await this.liquidityProvider.PayInvoice(order.payment.bolt11_invoice)
this.log("paid", res.amount_paid, "to open channel") const fees = +order.payment.fee_total_sat + res.network_fee + res.service_fee
return { orderId: order.order_id, invoice: order.payment.bolt11_invoice, totalSats: +order.payment.order_total_sat, fees: +order.payment.fee_total_sat } 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 () => { getInfo = async () => {
@ -215,8 +216,9 @@ export class OlympusLSP extends LSP {
return null return null
} }
const res = await this.liquidityProvider.PayInvoice(order.payment.bolt11.invoice) const res = await this.liquidityProvider.PayInvoice(order.payment.bolt11.invoice)
this.log("paid", res.amount_paid, "to open channel") const fees = +order.payment.bolt11.fee_total_sat + res.network_fee + res.service_fee
return { orderId: order.order_id, invoice: order.payment.bolt11.invoice, totalSats: +order.payment.bolt11.order_total_sat, fees: +order.payment.bolt11.fee_total_sat } 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 () => { getInfo = async () => {
@ -307,17 +309,18 @@ export class VoltageLSP extends LSP {
await this.addPeer(info.pubkey, `${ipv4.address}:${ipv4.port}`) await this.addPeer(info.pubkey, `${ipv4.address}:${ipv4.port}`)
const invoice = await this.lnd.NewInvoice(this.settings.channelThreshold, "open channel", 60 * 60) const invoice = await this.lnd.NewInvoice(this.settings.channelThreshold, "open channel", 60 * 60)
const res = await this.proposal(invoice.payRequest, fee.id) const proposalRes = await this.proposal(invoice.payRequest, fee.id)
this.log("proposal res", res, fee.id) this.log("proposal res", proposalRes, fee.id)
const decoded = await this.lnd.DecodeInvoice(res.jit_bolt11) const decoded = await this.lnd.DecodeInvoice(proposalRes.jit_bolt11)
if (decoded.numSatoshis !== this.settings.channelThreshold + feeSats) { if (decoded.numSatoshis !== this.settings.channelThreshold + feeSats) {
this.log("invoice of amount", decoded.numSatoshis, "does not match expected amount of", this.settings.channelThreshold + feeSats) this.log("invoice of amount", decoded.numSatoshis, "does not match expected amount of", this.settings.channelThreshold + feeSats)
return null return null
} }
const invoiceRes = await this.liquidityProvider.PayInvoice(res.jit_bolt11) const res = await this.liquidityProvider.PayInvoice(proposalRes.jit_bolt11)
this.log("paid", invoiceRes.amount_paid, "to open channel") const fees = feeSats + res.network_fee + res.service_fee
return { orderId: fee.id, invoice: res.jit_bolt11, totalSats: decoded.numSatoshis, fees: feeSats } 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) => { proposal = async (bolt11: string, feeId: string) => {

View file

@ -25,6 +25,7 @@ export class LiquidityManager {
log = getLogger({ component: "liquidityManager" }) log = getLogger({ component: "liquidityManager" })
channelRequested = false channelRequested = false
channelRequesting = false channelRequesting = false
feesPaid = 0
constructor(settings: LiquiditySettings, storage: Storage, liquidityProvider: LiquidityProvider, lnd: LND) { constructor(settings: LiquiditySettings, storage: Storage, liquidityProvider: LiquidityProvider, lnd: LND) {
this.settings = settings this.settings = settings
this.storage = storage this.storage = storage
@ -34,6 +35,11 @@ export class LiquidityManager {
this.voltageLSP = new VoltageLSP(settings.lspSettings, lnd, liquidityProvider) this.voltageLSP = new VoltageLSP(settings.lspSettings, lnd, liquidityProvider)
this.flashsatsLSP = new FlashsatsLSP(settings.lspSettings, lnd, liquidityProvider) this.flashsatsLSP = new FlashsatsLSP(settings.lspSettings, lnd, liquidityProvider)
} }
GetPaidFees = () => {
return this.feesPaid
}
onNewBlock = async () => { onNewBlock = async () => {
const balance = await this.liquidityProvider.GetLatestMaxWithdrawable() const balance = await this.liquidityProvider.GetLatestMaxWithdrawable()
const { remote } = await this.lnd.ChannelBalance() const { remote } = await this.lnd.ChannelBalance()
@ -41,7 +47,9 @@ export class LiquidityManager {
this.log("draining provider balance to channel") this.log("draining provider balance to channel")
const invoice = await this.lnd.NewInvoice(balance, "liqudity provider drain", defaultInvoiceExpiry) const invoice = await this.lnd.NewInvoice(balance, "liqudity provider drain", defaultInvoiceExpiry)
const res = await this.liquidityProvider.PayInvoice(invoice.payRequest) 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.log("requested channel from olympus")
this.channelRequested = true this.channelRequested = true
this.channelRequesting = false 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 }) await this.storage.liquidityStorage.SaveLspOrder({ service_name: 'olympus', invoice: olympusOk.invoice, total_paid: olympusOk.totalSats, order_id: olympusOk.orderId, fees: olympusOk.fees })
return return
} }
@ -86,6 +95,7 @@ export class LiquidityManager {
this.log("requested channel from voltage") this.log("requested channel from voltage")
this.channelRequested = true this.channelRequested = true
this.channelRequesting = false 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 }) await this.storage.liquidityStorage.SaveLspOrder({ service_name: 'voltage', invoice: voltageOk.invoice, total_paid: voltageOk.totalSats, order_id: voltageOk.orderId, fees: voltageOk.fees })
return return
} }
@ -95,6 +105,7 @@ export class LiquidityManager {
this.log("requested channel from flashsats") this.log("requested channel from flashsats")
this.channelRequested = true this.channelRequested = true
this.channelRequesting = false 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 }) await this.storage.liquidityStorage.SaveLspOrder({ service_name: 'flashsats', invoice: flashsatsOk.invoice, total_paid: flashsatsOk.totalSats, order_id: flashsatsOk.orderId, fees: flashsatsOk.fees })
return return
} }

View file

@ -53,8 +53,8 @@ export default class {
this.storage = storage this.storage = storage
this.settings = settings this.settings = settings
this.lnd = lnd this.lnd = lnd
this.watchDog = new Watchdog(settings.watchDogSettings, lnd, storage)
this.liquidityManager = liquidityManager this.liquidityManager = liquidityManager
this.watchDog = new Watchdog(settings.watchDogSettings, this.liquidityManager, lnd, storage)
this.addressPaidCb = addressPaidCb this.addressPaidCb = addressPaidCb
this.invoicePaidCb = invoicePaidCb this.invoicePaidCb = invoicePaidCb
} }

View file

@ -5,6 +5,7 @@ import { LiquidityProvider } from "../lnd/liquidityProvider.js";
import LND from "../lnd/lnd.js"; import LND from "../lnd/lnd.js";
import { ChannelBalance } from "../lnd/settings.js"; import { ChannelBalance } from "../lnd/settings.js";
import Storage from '../storage/index.js' import Storage from '../storage/index.js'
import { LiquidityManager } from "./liquidityManager.js";
export type WatchdogSettings = { export type WatchdogSettings = {
maxDiffSats: number maxDiffSats: number
} }
@ -22,17 +23,19 @@ export class Watchdog {
accumulatedHtlcFees: number; accumulatedHtlcFees: number;
lnd: LND; lnd: LND;
liquidProvider: LiquidityProvider; liquidProvider: LiquidityProvider;
liquidityManager: LiquidityManager;
settings: WatchdogSettings; settings: WatchdogSettings;
storage: Storage; storage: Storage;
latestCheckStart = 0 latestCheckStart = 0
log = getLogger({ component: "watchdog" }) log = getLogger({ component: "watchdog" })
ready = false ready = false
interval: NodeJS.Timer; interval: NodeJS.Timer;
constructor(settings: WatchdogSettings, lnd: LND, storage: Storage) { constructor(settings: WatchdogSettings, liquidityManager: LiquidityManager, lnd: LND, storage: Storage) {
this.lnd = lnd; this.lnd = lnd;
this.settings = settings; this.settings = settings;
this.storage = storage; this.storage = storage;
this.liquidProvider = lnd.liquidProvider this.liquidProvider = lnd.liquidProvider
this.liquidityManager = liquidityManager
this.queue = new FunctionQueue("watchdog_queue", () => this.StartCheck()) 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 }) 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)
return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance const feesPaidForLiquidity = this.liquidityManager.GetPaidFees()
return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance + feesPaidForLiquidity
} }
checkBalanceUpdate = (deltaLnd: number, deltaUsers: number) => { checkBalanceUpdate = (deltaLnd: number, deltaUsers: number) => {