This commit is contained in:
boufni95 2024-06-28 18:10:55 +02:00
parent 29879a027c
commit 20d2afa464
7 changed files with 22 additions and 22 deletions

View file

@ -91,6 +91,9 @@ export class LiquidityProvider {
} }
GetLatestMaxWithdrawable = async (fetch = false) => { GetLatestMaxWithdrawable = async (fetch = false) => {
if (!this.pubDestination) {
return 0
}
if (this.latestMaxWithdrawable === null) { if (this.latestMaxWithdrawable === null) {
this.log("liquidity provider is not ready yet") this.log("liquidity provider is not ready yet")
return 0 return 0
@ -102,6 +105,9 @@ export class LiquidityProvider {
} }
GetLatestBalance = async (fetch = false) => { GetLatestBalance = async (fetch = false) => {
if (!this.pubDestination) {
return 0
}
if (this.latestMaxWithdrawable === null) { if (this.latestMaxWithdrawable === null) {
this.log("liquidity provider is not ready yet") this.log("liquidity provider is not ready yet")
return 0 return 0

View file

@ -191,7 +191,7 @@ export default class {
this.sendOperationToNostr(userInvoice.linkedApplication, userInvoice.user.user_id, op) this.sendOperationToNostr(userInvoice.linkedApplication, userInvoice.user.user_id, op)
this.createZapReceipt(log, userInvoice) this.createZapReceipt(log, userInvoice)
log("paid invoice processed successfully") log("paid invoice processed successfully")
await this.liquidityManager.afterInInvoicePaid() this.liquidityManager.afterInInvoicePaid()
} catch (err: any) { } catch (err: any) {
log(ERROR, "cannot process paid invoice", err.message || "") log(ERROR, "cannot process paid invoice", err.message || "")
} }

View file

@ -72,6 +72,13 @@ export class LiquidityManager {
return 'provider' return 'provider'
} }
afterInInvoicePaid = async () => { afterInInvoicePaid = async () => {
try {
await this.orderChannelIfNeeded()
} catch (e: any) {
this.log("error ordering channel", e)
}
}
orderChannelIfNeeded = async () => {
const existingOrder = await this.storage.liquidityStorage.GetLatestLspOrder() const existingOrder = await this.storage.liquidityStorage.GetLatestLspOrder()
if (existingOrder) { if (existingOrder) {
return return

View file

@ -104,10 +104,10 @@ export default class SanityChecker {
} }
if (entry.paid_at_unix === -1) { if (entry.paid_at_unix === -1) {
this.decrementEvents[invoice] = { userId, refund: amt, failure: true } this.decrementEvents[invoice] = { userId, refund: amt, failure: true }
} else { return
const refund = amt - (entry.paid_amount + entry.routing_fees + entry.service_fees)
this.decrementEvents[invoice] = { userId, refund, failure: false }
} }
const refund = amt - (entry.paid_amount + entry.routing_fees + entry.service_fees)
this.decrementEvents[invoice] = { userId, refund, failure: false }
if (!entry.internal && !entry.liquidityProvider) { if (!entry.internal && !entry.liquidityProvider) {
const lndEntry = this.payments.find(i => i.paymentRequest === invoice) const lndEntry = this.payments.find(i => i.paymentRequest === invoice)
if (!lndEntry) { if (!lndEntry) {

View file

@ -102,7 +102,7 @@ export class Watchdog {
const walletBalance = await this.lnd.GetWalletBalance() const walletBalance = await this.lnd.GetWalletBalance()
this.log(Number(walletBalance.confirmedBalance), "sats in chain wallet") this.log(Number(walletBalance.confirmedBalance), "sats in chain wallet")
const channelsBalance = await this.lnd.GetChannelBalance() const channelsBalance = await this.lnd.GetChannelBalance()
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 feesPaidForLiquidity = this.liquidityManager.GetPaidFees() const feesPaidForLiquidity = this.liquidityManager.GetPaidFees()

View file

@ -99,18 +99,10 @@ export default class Handler {
log = getLogger({ component: "nostrMiddleware" }) log = getLogger({ component: "nostrMiddleware" })
constructor(settings: NostrSettings, eventCallback: (event: NostrEvent) => void) { constructor(settings: NostrSettings, eventCallback: (event: NostrEvent) => void) {
this.settings = settings this.settings = settings
this.log( this.log("connecting to relays:", settings.relays)
{ this.settings.apps.forEach(app => {
...settings, this.log("appId:", app.appId, "pubkey:", app.publicKey, "nprofile:", encodeNprofile({ pubkey: app.publicKey, relays: settings.relays }))
apps: settings.apps.map(app => { })
const { privateKey, ...rest } = app;
return {
...rest,
nprofile: encodeNprofile({ pubkey: rest.publicKey, relays: settings.relays })
}
})
}
)
this.eventCallback = eventCallback this.eventCallback = eventCallback
this.settings.apps.forEach(app => { this.settings.apps.forEach(app => {
this.apps[app.publicKey] = app this.apps[app.publicKey] = app

View file

@ -20,21 +20,17 @@ export default class {
PushToQueue<T>(op: TxOperation<T>) { PushToQueue<T>(op: TxOperation<T>) {
if (!this.pendingTx) { if (!this.pendingTx) {
this.log("queue empty, starting transaction", this.transactionsQueue.length)
return this.execQueueItem(op) return this.execQueueItem(op)
} }
this.log("queue not empty, possibly stuck")
return new Promise<T>((res, rej) => { return new Promise<T>((res, rej) => {
this.transactionsQueue.push({ op, res, rej }) this.transactionsQueue.push({ op, res, rej })
}) })
} }
async execNextInQueue() { async execNextInQueue() {
this.log("executing next in queue")
this.pendingTx = false this.pendingTx = false
const next = this.transactionsQueue.pop() const next = this.transactionsQueue.pop()
if (!next) { if (!next) {
this.log("queue is clear")
return return
} }
try { try {
@ -51,7 +47,6 @@ export default class {
throw new Error("cannot start DB transaction") throw new Error("cannot start DB transaction")
} }
this.pendingTx = true this.pendingTx = true
this.log("starting", op.dbTx ? "db transaction" : "operation", op.description || "")
if (op.dbTx) { if (op.dbTx) {
return this.doTransaction(op.exec) return this.doTransaction(op.exec)
} }