From cbcac825410007d560013ac915bea5e78e5d2dcc Mon Sep 17 00:00:00 2001 From: boufni95 Date: Wed, 26 Nov 2025 17:57:17 +0000 Subject: [PATCH] cleanup --- src/services/lnd/lnd.ts | 6 +-- src/services/main/appUserManager.ts | 2 +- src/services/main/applicationManager.ts | 4 +- src/services/main/liquidityManager.ts | 2 - src/services/main/liquidityProvider.ts | 19 +------ src/services/main/paymentManager.ts | 69 +------------------------ src/tests/setupBootstrapped.ts | 1 - 7 files changed, 8 insertions(+), 95 deletions(-) diff --git a/src/services/lnd/lnd.ts b/src/services/lnd/lnd.ts index aadf9e16..4644e491 100644 --- a/src/services/lnd/lnd.ts +++ b/src/services/lnd/lnd.ts @@ -342,10 +342,6 @@ export default class { return { numSatoshis: Number(res.response.numSatoshis), paymentHash: res.response.paymentHash } } - /* GetFeeLimitAmount(amount: number): number { - return Math.ceil(amount * this.getSettings().lndSettings.feeRateLimit + this.getSettings().lndSettings.feeFixedLimit); - } */ - async ChannelBalance(): Promise<{ local: number, remote: number }> { // console.log("Getting channel balance") const res = await this.lightning.channelBalance({}) @@ -359,7 +355,7 @@ export default class { throw new Error("lnd node is currently out of sync") } if (useProvider) { - const res = await this.liquidProvider.PayInvoice(invoice, decodedAmount, from/* , feeLimit */) + const res = await this.liquidProvider.PayInvoice(invoice, decodedAmount, from) const providerDst = this.liquidProvider.GetProviderDestination() return { feeSat: res.network_fee + res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage, providerDst } } diff --git a/src/services/main/appUserManager.ts b/src/services/main/appUserManager.ts index f79a171c..75498b24 100644 --- a/src/services/main/appUserManager.ts +++ b/src/services/main/appUserManager.ts @@ -69,7 +69,7 @@ export default class { throw new Error(`app user ${ctx.user_id} not found`) // TODO: fix logs doxing } const nostrSettings = this.settings.getSettings().nostrRelaySettings - const { max, /* networkFeeBps, */ networkFeeFixed, serviceFeeBps } = this.applicationManager.paymentManager.GetMaxPayableInvoice(user.balance_sats) + const { max, networkFeeFixed, serviceFeeBps } = this.applicationManager.paymentManager.GetMaxPayableInvoice(user.balance_sats) return { userId: ctx.user_id, balance: user.balance_sats, diff --git a/src/services/main/applicationManager.ts b/src/services/main/applicationManager.ts index 8dd92847..b1befeb8 100644 --- a/src/services/main/applicationManager.ts +++ b/src/services/main/applicationManager.ts @@ -167,7 +167,7 @@ export default class { const ndebitString = ndebitEncode({ pubkey: app.nostr_public_key!, pointer: u.identifier, relay: nostrSettings.relays[0] }) log("🔗 [DEBUG] Generated ndebit for user", { userId: u.user.user_id, ndebit: ndebitString }) - const { max, /* networkFeeBps, */networkFeeFixed, serviceFeeBps } = this.paymentManager.GetMaxPayableInvoice(u.user.balance_sats) + const { max, networkFeeFixed, serviceFeeBps } = this.paymentManager.GetMaxPayableInvoice(u.user.balance_sats) return { identifier: u.identifier, info: { @@ -225,7 +225,7 @@ export default class { const app = await this.storage.applicationStorage.GetApplication(appId) const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier) const nostrSettings = this.settings.getSettings().nostrRelaySettings - const { max, /* networkFeeBps, */ networkFeeFixed, serviceFeeBps } = this.paymentManager.GetMaxPayableInvoice(user.user.balance_sats) + const { max, networkFeeFixed, serviceFeeBps } = this.paymentManager.GetMaxPayableInvoice(user.user.balance_sats) return { max_withdrawable: max, identifier: req.user_identifier, info: { userId: user.user.user_id, balance: user.user.balance_sats, diff --git a/src/services/main/liquidityManager.ts b/src/services/main/liquidityManager.ts index 153ac94f..f90631ab 100644 --- a/src/services/main/liquidityManager.ts +++ b/src/services/main/liquidityManager.ts @@ -87,7 +87,6 @@ export class LiquidityManager { if (!providerReady) { throw new Error("cannot use liquidity provider, it is not ready") } - // const feeLimit = this.liquidityProvider.CalculateExpectedFeeLimit(amount) return 'provider' } if (!providerReady) { @@ -97,7 +96,6 @@ export class LiquidityManager { if (!canHandle) { return 'lnd' } - // const feeLimit = this.liquidityProvider.CalculateExpectedFeeLimit(amount) return 'provider' } diff --git a/src/services/main/liquidityProvider.ts b/src/services/main/liquidityProvider.ts index 6f66cf39..7789a5e3 100644 --- a/src/services/main/liquidityProvider.ts +++ b/src/services/main/liquidityProvider.ts @@ -131,7 +131,6 @@ export class LiquidityProvider { return res } this.feesCache = { - // networkFeeBps: res.network_max_fee_bps, networkFeeFixed: res.network_max_fee_fixed, serviceFeeBps: res.service_fee_bps } @@ -158,9 +157,6 @@ export class LiquidityProvider { const maxWithoutFixed = Math.floor(balance / div) const fee = balance - maxWithoutFixed return balance - Math.max(fee, networkFeeFixed) - /* const totalBps = networkFeeBps + serviceFeeBps - const div = 1 + (totalBps / 10000) - return Math.floor((this.latestReceivedBalance - networkFeeFixed) / div) */ } GetLatestBalance = () => { @@ -181,15 +177,6 @@ export class LiquidityProvider { return Math.max(serviceFee, fees.networkFeeFixed) } - /* CalculateExpectedFeeLimit = (amount: number) => { - const fees = this.GetFees() - const serviceFeeRate = fees.serviceFeeBps / 10000 - const serviceFee = Math.ceil(serviceFeeRate * amount) - const networkMaxFeeRate = fees.networkFeeBps / 10000 - const networkFeeLimit = Math.ceil(amount * networkMaxFeeRate + fees.networkFeeFixed) - return serviceFee + networkFeeLimit - } */ - CanProviderPay = async (amount: number, localServiceFee: number): Promise => { if (!this.IsReady()) { this.log("provider is not ready") @@ -229,12 +216,11 @@ export class LiquidityProvider { } - PayInvoice = async (invoice: string, decodedAmount: number, from: 'user' | 'system'/* , feeLimit?: number */) => { + PayInvoice = async (invoice: string, decodedAmount: number, from: 'user' | 'system') => { try { if (!this.IsReady()) { throw new Error("liquidity provider is not ready yet, disabled or unreachable") } - // const feeLimitToUse = feeLimit ? feeLimit : this.CalculateExpectedFeeLimit(decodedAmount) const providerServiceFee = this.GetServiceFee(decodedAmount) this.pendingPayments[invoice] = decodedAmount + providerServiceFee const timeout = setTimeout(() => { @@ -245,7 +231,7 @@ export class LiquidityProvider { this.lastSeenBeacon = 0 }, 1000 * 10) this.pendingPaymentsAck[invoice] = true - const res = await this.client.PayInvoice({ invoice, amount: 0,/* fee_limit_sats: feeLimitToUse */ }) + const res = await this.client.PayInvoice({ invoice, amount: 0 }) delete this.pendingPaymentsAck[invoice] if (res.status === 'ERROR') { this.log("error paying invoice", res.reason) @@ -312,7 +298,6 @@ export class LiquidityProvider { this.log("configured to send to ") } } - // fees: { networkFeeBps: number, networkFeeFixed: number, serviceFeeBps: number } onBeaconEvent = async (beaconData: { content: string, pub: string }) => { if (beaconData.pub !== this.pubDestination) { this.log(ERROR, "got beacon from invalid pub", beaconData.pub, this.pubDestination) diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index 87b88c22..b3fd4ba4 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -205,32 +205,6 @@ export default class { } } - /* getServiceFee(action: Types.UserOperationType, amount: number, appUser: boolean): number { - switch (action) { - case Types.UserOperationType.INCOMING_TX: - return Math.ceil(this.settings.getSettings().serviceFeeSettings.incomingTxFee * amount) - case Types.UserOperationType.OUTGOING_TX: - return Math.ceil(this.settings.getSettings().serviceFeeSettings.outgoingTxFee * amount) - case Types.UserOperationType.INCOMING_INVOICE: - if (appUser) { - return Math.ceil(this.settings.getSettings().serviceFeeSettings.incomingAppUserInvoiceFee * amount) - } - return Math.ceil(this.settings.getSettings().serviceFeeSettings.incomingAppInvoiceFee * amount) - case Types.UserOperationType.OUTGOING_INVOICE: - if (appUser) { - return Math.ceil(this.settings.getSettings().serviceFeeSettings.outgoingAppUserInvoiceFee * amount) - } - return Math.ceil(this.settings.getSettings().serviceFeeSettings.outgoingAppInvoiceFee * amount) - case Types.UserOperationType.OUTGOING_USER_TO_USER || Types.UserOperationType.INCOMING_USER_TO_USER: - if (appUser) { - return Math.ceil(this.settings.getSettings().serviceFeeSettings.userToUserFee * amount) - } - return Math.ceil(this.settings.getSettings().serviceFeeSettings.appToUserFee * amount) - default: - throw new Error("Unknown service action type") - } - } */ - async SetMockInvoiceAsPaid(req: Types.SetMockInvoiceAsPaidRequest) { if (!this.settings.getSettings().lndSettings.mockLnd) { throw new Error("mock disabled, cannot set invoice as paid") @@ -279,11 +253,6 @@ export default class { GetFees = (): Types.CumulativeFees => { const { outgoingAppUserInvoiceFeeBps } = this.settings.getSettings().serviceFeeSettings - /* if (this.lnd.liquidProvider.IsReady()) { - const fees = this.lnd.liquidProvider.GetFees() - const networkFeeBps = fees.networkFeeBps + fees.serviceFeeBps - return { networkFeeBps, networkFeeFixed: fees.networkFeeFixed, serviceFeeBps: outgoingAppUserInvoiceFeeBps } - } */ const { feeFixedLimit } = this.settings.getSettings().lndSettings return { networkFeeFixed: feeFixedLimit, serviceFeeBps: outgoingAppUserInvoiceFeeBps } } @@ -295,11 +264,6 @@ export default class { const fee = balance - maxWithoutFixed const max = balance - Math.max(fee, networkFeeFixed) return { max, networkFeeFixed, serviceFeeBps } - - /* const totalBps = networkFeeBps + serviceFeeBps - const div = 1 + (totalBps / 10000) - const max = Math.floor((balance - networkFeeFixed) / div) - return { max, serviceFeeBps, networkFeeBps, networkFeeFixed } */ } async DecodeInvoice(req: Types.DecodeInvoiceRequest): Promise { const decoded = await this.lnd.DecodeInvoice(req.invoice) @@ -324,9 +288,6 @@ export default class { const payAmount = req.amount !== 0 ? req.amount : Number(decoded.numSatoshis) const isAppUserPayment = userId !== linkedApplication.owner.user_id const serviceFee = this.getSendServiceFee(Types.UserOperationType.OUTGOING_INVOICE, payAmount, isAppUserPayment) - /* if (req.fee_limit_sats && req.fee_limit_sats < serviceFee) { - throw new Error("fee limit provided is too low to cover service fees") - } */ const internalInvoice = await this.storage.paymentStorage.GetInvoiceOwner(req.invoice) if (internalInvoice && internalInvoice.paid_at_unix > 0) { throw new Error("this invoice was already paid") @@ -339,7 +300,7 @@ export default class { if (internalInvoice) { paymentInfo = await this.PayInternalInvoice(userId, internalInvoice, { payAmount, serviceFee }, linkedApplication, req.debit_npub) } else { - paymentInfo = await this.PayExternalInvoice(userId, req.invoice, { payAmount, serviceFee, amountForLnd: req.amount, /* feeLimit: req.fee_limit_sats */ }, linkedApplication, req.debit_npub, ack) + paymentInfo = await this.PayExternalInvoice(userId, req.invoice, { payAmount, serviceFee, amountForLnd: req.amount }, linkedApplication, req.debit_npub, ack) } const feeDiff = serviceFee - paymentInfo.networkFee if (isAppUserPayment && feeDiff > 0) { @@ -360,22 +321,7 @@ export default class { } } - /* getUse = async (payAmount: number, localServiceFee: number): Promise<{ use: 'lnd' | 'provider', feeLimit: number }> => { - const use = await this.liquidityManager.beforeOutInvoicePayment(payAmount, localServiceFee) - if (use === 'lnd') { - const lndFeeLimit = this.lnd.GetFeeLimitAmount(payAmount) - if (inputLimit && inputLimit < lndFeeLimit) { - this.log("WARNING requested fee limit is lower than suggested, payment might fail") - } - return { use: 'lnd', feeLimit: inputLimit || lndFeeLimit } - } - if (inputLimit && inputLimit < use.feeLimit) { - this.log("WARNING requested fee limit is lower than suggested by provider, payment might fail") - } - return { use: 'provider', feeLimit: inputLimit || use.feeLimit } - } */ - - async PayExternalInvoice(userId: string, invoice: string, amounts: { payAmount: number, serviceFee: number, amountForLnd: number, /* feeLimit?: number */ }, linkedApplication: Application, debitNpub?: string, ack?: (op: Types.UserOperation) => void) { + async PayExternalInvoice(userId: string, invoice: string, amounts: { payAmount: number, serviceFee: number, amountForLnd: number }, linkedApplication: Application, debitNpub?: string, ack?: (op: Types.UserOperation) => void) { if (this.settings.getSettings().serviceSettings.disableExternalPayments) { throw new Error("something went wrong sending payment, please try again later") } @@ -392,10 +338,6 @@ export default class { const { amountForLnd, payAmount, serviceFee } = amounts const totalAmountToDecrement = payAmount + serviceFee const use = await this.liquidityManager.beforeOutInvoicePayment(payAmount, serviceFee) - /* const routingFeeLimit = this.lnd.GetFeeLimitAmount(payAmount) - const use = await this.liquidityManager.beforeOutInvoicePayment(payAmount) */ - // const remainingLimit = amounts.feeLimit ? amounts.feeLimit - serviceFee : undefined - // const { use, feeLimit: routingFeeLimit } = await this.getUse(payAmount, remainingLimit) const provider = use === 'provider' ? this.lnd.liquidProvider.GetProviderDestination() : undefined const pendingPayment = await this.storage.StartTransaction(async tx => { await this.storage.userStorage.DecrementUserBalance(userId, totalAmountToDecrement, invoice, tx) @@ -409,13 +351,6 @@ export default class { const payment = await this.lnd.PayInvoice(invoice, amountForLnd, serviceFee, payAmount, { useProvider: use === 'provider', from: 'user' }, index => { this.storage.paymentStorage.SetExternalPaymentIndex(pendingPayment.serial_id, index) }) - /* const feeDiff = serviceFee - payment.feeSat - if (feeDiff > 0) { - // this.log("refund routing fee", routingFeeLimit, payment.feeSat, "sats") - this.log("") - await this.storage.userStorage.IncrementUserBalance(userId, routingFeeLimit - payment.feeSat, "routing_fee_refund:" + invoice) - } */ - await this.storage.paymentStorage.UpdateExternalPayment(pendingPayment.serial_id, payment.feeSat, serviceFee, true, payment.providerDst) return { preimage: payment.paymentPreimage, amtPaid: payment.valueSat, networkFee: payment.feeSat, serialId: pendingPayment.serial_id } diff --git a/src/tests/setupBootstrapped.ts b/src/tests/setupBootstrapped.ts index 3184d254..32aa3144 100644 --- a/src/tests/setupBootstrapped.ts +++ b/src/tests/setupBootstrapped.ts @@ -46,7 +46,6 @@ export const initBootstrappedInstance = async (T: TestBase) => { await new Promise(res => { const interval = setInterval(async () => { const canHandle = bootstrapped.liquidityProvider.IsReady() - console.log("can handle", canHandle) if (canHandle) { clearInterval(interval) res()