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
7 changes: 7 additions & 0 deletions src/integration/exchange/services/exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
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<Market[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -54,29 +55,41 @@ export class BinanceAdapter extends CcxtExchangeAdapter {

private async lightningWithdraw(order: LiquidityManagementOrder): Promise<CorrelationId> {
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(
`${this.exchangeService.name}: not enough balance for ${asset} (balance: ${balance}, min. requested: ${order.minAmount}, max. requested: ${order.maxAmount})`,
);
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;
}
Expand Down
Loading