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
11 changes: 11 additions & 0 deletions src/integration/exchange/dto/mexc.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,14 @@ export interface MexcMyTrade {
isMaker: boolean;
isBestMatch: boolean;
}

export interface MexcOrderResponse {
symbol: string;
orderId: string;
orderListId: number;
price: string;
origQty: string;
type: string;
side: string;
transactTime: number;
}
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 @@ -354,7 +354,7 @@ export abstract class ExchangeService extends PricingProvider implements OnModul
return this.createOrder(pair, direction, amount, price).then((o) => o.id);
}

private async createOrder(pair: string, direction: OrderSide, amount: number, price: number): Promise<Order> {
protected async createOrder(pair: string, direction: OrderSide, amount: number, price: number): Promise<Order> {
return this.callApi((e) => e.createOrder(pair, 'limit', direction, amount, price));
}

Expand Down
19 changes: 17 additions & 2 deletions src/integration/exchange/services/mexc.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { Method } from 'axios';
import { Market, mexc, OrderBook, Trade, Transaction } from 'ccxt';
import { Market, mexc, Order, OrderBook, Trade, Transaction } from 'ccxt';
import { Config, GetConfig } from 'src/config/config';
import { Blockchain } from 'src/integration/blockchain/shared/enums/blockchain.enum';
import { DfxLogger } from 'src/shared/services/dfx-logger';
Expand All @@ -12,12 +12,13 @@ import {
MexcExchangeInfo,
MexcMyTrade,
MexcOrderBook,
MexcOrderResponse,
MexcSymbol,
MexcTrade,
Withdrawal,
WithdrawalStatus,
} from '../dto/mexc.dto';
import { ExchangeService } from './exchange.service';
import { ExchangeService, OrderSide } from './exchange.service';

@Injectable()
export class MexcService extends ExchangeService {
Expand Down Expand Up @@ -288,4 +289,18 @@ export class MexcService extends ExchangeService {
fees: t.commission ? [{ cost: parseFloat(t.commission), currency: t.commissionAsset }] : [],
}));
}

protected async createOrder(pair: string, direction: OrderSide, amount: number, price: number): Promise<Order> {
const symbol = pair.replace('/', '');

const response = await this.request<MexcOrderResponse>('POST', 'order', {
symbol,
side: direction.toUpperCase(),
type: 'LIMIT',
quantity: amount.toString(),
price: price.toString(),
});

return { id: response.orderId } as Order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ export abstract class CcxtExchangeAdapter extends LiquidityActionAdapter {
const availableBalance = await this.getAvailableTradeBalance(tradeAsset, targetAssetEntity.name);
let effectiveMax = Math.min(maxSellAmount, availableBalance);

if (availableBalance < minSellAmount) {
throw new OrderNotProcessableException(
`${this.exchangeService.name}: not enough balance for ${tradeAsset} (balance: ${availableBalance}, min. requested: ${minSellAmount}, max. requested: ${maxSellAmount})`,
);
}

if (liquidityLimited) {
const { amount: liquidity, price: liquidityPrice } = await this.getBestPriceLiquidity(
tradeAsset,
Expand All @@ -157,12 +163,6 @@ export abstract class CcxtExchangeAdapter extends LiquidityActionAdapter {
effectiveMax = Math.min(effectiveMax, Util.floor(liquidity * liquidityPrice, 6));
}

if (effectiveMax < minSellAmount) {
throw new OrderNotProcessableException(
`${this.exchangeService.name}: not enough balance/liquidity for ${tradeAsset} (balance: ${effectiveMax}, min. requested: ${minSellAmount}, max. requested: ${maxSellAmount})`,
);
}

const amount = effectiveMax;

order.inputAmount = amount;
Expand Down Expand Up @@ -197,17 +197,17 @@ export abstract class CcxtExchangeAdapter extends LiquidityActionAdapter {
const availableBalance = await this.getAvailableTradeBalance(asset, tradeAsset);
let effectiveMax = Math.min(order.maxAmount, availableBalance);

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

if (liquidityLimited) {
const { amount: liquidity } = await this.getBestPriceLiquidity(asset, tradeAsset);
effectiveMax = Math.min(effectiveMax, liquidity);
}

if (effectiveMax < order.minAmount) {
throw new OrderNotProcessableException(
`${this.exchangeService.name}: not enough balance/liquidity for ${asset} (balance: ${effectiveMax}, min. requested: ${order.minAmount}, max. requested: ${order.maxAmount})`,
);
}

const amount = effectiveMax;

order.inputAmount = amount;
Expand Down
Loading