From e92b0fc0cead6fda6e51563dc0bb58265a65e23d Mon Sep 17 00:00:00 2001 From: David May <85513542+davidleomay@users.noreply.github.com> Date: Mon, 2 Mar 2026 17:00:47 +0100 Subject: [PATCH] fix: Binance LN withdraw (#3310) --- .../exchange/services/exchange.service.ts | 7 ++++++ .../adapters/actions/binance.adapter.ts | 23 +++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/integration/exchange/services/exchange.service.ts b/src/integration/exchange/services/exchange.service.ts index cf7946d06d..632f4fc122 100644 --- a/src/integration/exchange/services/exchange.service.ts +++ b/src/integration/exchange/services/exchange.service.ts @@ -224,6 +224,13 @@ export abstract class ExchangeService extends PricingProvider implements OnModul return this.callApi((e) => e.fetchWithdrawals(token, this.toCcxtDate(since), 200, { limit: 200 })); } + async getWithdrawalFee(token: string, network?: string): Promise { + const fees = await this.callApi((e) => e.fetchDepositWithdrawFees([token])); + const tokenFees = fees[token]; + + return tokenFees?.networks?.[network]?.fee ?? tokenFees?.withdraw?.fee ?? 0; + } + // --- Helper Methods --- // // currency pairs private async getMarkets(): Promise { diff --git a/src/subdomains/core/liquidity-management/adapters/actions/binance.adapter.ts b/src/subdomains/core/liquidity-management/adapters/actions/binance.adapter.ts index 7677d2d9d4..02726136ae 100644 --- a/src/subdomains/core/liquidity-management/adapters/actions/binance.adapter.ts +++ b/src/subdomains/core/liquidity-management/adapters/actions/binance.adapter.ts @@ -1,4 +1,5 @@ import { Injectable } from '@nestjs/common'; +import { Blockchain } from 'src/integration/blockchain/shared/enums/blockchain.enum'; import { BinanceService } from 'src/integration/exchange/services/binance.service'; import { ExchangeRegistryService } from 'src/integration/exchange/services/exchange-registry.service'; import { LndInvoiceState } from 'src/integration/lightning/dto/lnd.dto'; @@ -54,9 +55,15 @@ export class BinanceAdapter extends CcxtExchangeAdapter { private async lightningWithdraw(order: LiquidityManagementOrder): Promise { const asset = order.pipeline.rule.targetAsset.dexName; + const network = this.exchangeService.mapNetwork(Blockchain.LIGHTNING) || undefined; const balance = await this.exchangeService.getAvailableBalance(asset); - const amount = Util.floor(Math.min(order.maxAmount, balance, BINANCE_LIGHTNING_MAX_WITHDRAWAL_BTC), 8); + const withdrawalFee = await this.exchangeService.getWithdrawalFee(asset, network); + + const amount = Util.floor( + Math.min(order.maxAmount, balance - withdrawalFee, BINANCE_LIGHTNING_MAX_WITHDRAWAL_BTC), + 8, + ); if (amount <= 0) throw new OrderNotProcessableException( @@ -64,19 +71,25 @@ export class BinanceAdapter extends CcxtExchangeAdapter { ); const amountSats = LightningHelper.btcToSat(amount); - // Generate invoice via LnBits + // Generate invoice via LnBits for the target amount (excluding fee) const invoice = await this.lightningClient.getLnBitsWalletPayment({ amount: amountSats, memo: `LM Order ${order.id}`, expirySec: 1800, // 30 min (Binance limit) }); - order.inputAmount = amount; + order.inputAmount = amount + withdrawalFee; order.inputAsset = asset; order.outputAsset = asset; - // Send invoice to Binance for withdrawal - const response = await this.exchangeService.withdrawFunds(asset, amount, invoice.pr, undefined, 'LIGHTNING'); + // Send invoice to Binance - amount must be invoice_amount + withdrawal_fee + const response = await this.exchangeService.withdrawFunds( + asset, + amount + withdrawalFee, + invoice.pr, + undefined, + network, + ); return response.id; }