From 1c7037e60aa4de5b6e61609be978795ad697b701 Mon Sep 17 00:00:00 2001 From: TaprootFreak <142087526+TaprootFreak@users.noreply.github.com> Date: Mon, 2 Mar 2026 22:12:07 +0100 Subject: [PATCH] fix: check Binance minimum before Lightning withdrawal to enable fallback (#3321) * fix: check Binance minimum before Lightning withdrawal to enable fallback When the Lightning withdrawal amount is below Binance's minimum (0.00002 BTC), the API rejects the request. Previously this was caught as a generic error and mapped to OrderFailedException, which immediately fails the pipeline with no fallback path. Now the minimum is checked upfront and throws OrderNotProcessableException instead, allowing the pipeline to follow the configured onFail action chain (e.g. buy liquidity via a sub-pipeline). * refactor: simplify min withdrawal check into single guard Replace two separate checks (amount <= 0 and amount < minimum) with a single check against the Binance minimum withdrawal amount. * fix: use Binance minimum withdrawal amount instead of failing When the requested amount is below Binance's Lightning minimum (0.00002 BTC), withdraw the minimum instead of rejecting the order. Only fail if the Binance balance cannot cover the minimum. --- .../adapters/actions/binance.adapter.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 c2805ef2a9..9c314b3e17 100644 --- a/src/subdomains/core/liquidity-management/adapters/actions/binance.adapter.ts +++ b/src/subdomains/core/liquidity-management/adapters/actions/binance.adapter.ts @@ -23,6 +23,7 @@ export enum BinanceAdapterCommands { } const BINANCE_LIGHTNING_MAX_WITHDRAWAL_BTC = 0.00999; +const BINANCE_LIGHTNING_MIN_WITHDRAWAL_BTC = 0.00002; @Injectable() export class BinanceAdapter extends CcxtExchangeAdapter { @@ -60,13 +61,17 @@ export class BinanceAdapter extends CcxtExchangeAdapter { const withdrawalFee = await this.exchangeService.getWithdrawalFee(asset, network); const amount = Util.floor( - Math.min(order.maxAmount, balance - withdrawalFee, BINANCE_LIGHTNING_MAX_WITHDRAWAL_BTC), + Math.min( + Math.max(order.maxAmount, BINANCE_LIGHTNING_MIN_WITHDRAWAL_BTC), + balance - withdrawalFee, + BINANCE_LIGHTNING_MAX_WITHDRAWAL_BTC, + ), 8, ); - if (amount <= 0) + if (amount < BINANCE_LIGHTNING_MIN_WITHDRAWAL_BTC) throw new OrderNotProcessableException( - `${this.exchangeService.name}: not enough balance for ${asset} (balance: ${balance}, fee: ${withdrawalFee}, min. requested: ${order.minAmount}, max. requested: ${order.maxAmount})`, + `${this.exchangeService.name}: not enough balance for ${asset} (balance: ${balance}, fee: ${withdrawalFee}, min. withdrawal: ${BINANCE_LIGHTNING_MIN_WITHDRAWAL_BTC})`, ); const amountSats = LightningHelper.btcToSat(amount);