Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/integration/exchange/services/exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export abstract class ExchangeService extends PricingProvider implements OnModul
const fees = await this.callApi((e) => e.fetchDepositWithdrawFees([token]));
const tokenFees = fees[token];

return tokenFees?.networks?.[network]?.fee ?? tokenFees?.withdraw?.fee ?? 0;
return (tokenFees?.networks?.[network] as any)?.withdraw?.fee ?? tokenFees?.withdraw?.fee ?? 0;
}

// --- Helper Methods --- //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ export class BinanceAdapter extends CcxtExchangeAdapter {
const asset = order.pipeline.rule.targetAsset.dexName;
const network = this.exchangeService.mapNetwork(Blockchain.LIGHTNING) || undefined;
const balance = await this.exchangeService.getAvailableBalance(asset);
const withdrawalFee = await this.exchangeService.getWithdrawalFee(asset, network);

const amount = Util.floor(Math.min(order.maxAmount, balance * 0.99, BINANCE_LIGHTNING_MAX_WITHDRAWAL_BTC), 8);
const amount = Util.floor(
Math.min(order.maxAmount, balance - withdrawalFee, BINANCE_LIGHTNING_MAX_WITHDRAWAL_BTC),
8,
);

if (amount <= 0)
throw new OrderNotProcessableException(
`${this.exchangeService.name}: not enough balance for ${asset} (balance: ${balance}, min. requested: ${order.minAmount}, max. requested: ${order.maxAmount})`,
`${this.exchangeService.name}: not enough balance for ${asset} (balance: ${balance}, fee: ${withdrawalFee}, min. requested: ${order.minAmount}, max. requested: ${order.maxAmount})`,
);

const amountSats = LightningHelper.btcToSat(amount);
Expand All @@ -78,8 +82,14 @@ export class BinanceAdapter extends CcxtExchangeAdapter {
order.inputAsset = asset;
order.outputAsset = asset;

// Send only the invoice amount — Binance deducts the fee from the balance separately
const response = await this.exchangeService.withdrawFunds(asset, amount, invoice.pr, undefined, network);
// Binance requires: withdrawal amount = invoice BTC value + withdrawal fee
const response = await this.exchangeService.withdrawFunds(
asset,
amount + withdrawalFee,
invoice.pr,
undefined,
network,
);

return response.id;
}
Expand Down
Loading