import { AxiosRequestConfig } from 'axios'; import { APIResponse, KlineInterval } from './types/shared'; import { NewSpotOrder, OrderSide, OrderTypeSpot, SpotOrderQueryById, SpotSymbolInfo, } from './types/spot'; import BaseRestClient from './util/BaseRestClient'; import { agentSource, getRestBaseUrl, RestClientOptions, REST_CLIENT_TYPE_ENUM, } from './util/requestUtils'; export class SpotClient extends BaseRestClient { /** * @public Creates an instance of the Spot REST API client. * * @param {string} key - your API key * @param {string} secret - your API secret * @param {boolean} [useLivenet=false] * @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity * @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios */ constructor( key?: string | undefined, secret?: string | undefined, useLivenet: boolean = false, restClientOptions: RestClientOptions = {}, requestOptions: AxiosRequestConfig = {} ) { super( key, secret, getRestBaseUrl(useLivenet, restClientOptions), restClientOptions, requestOptions, REST_CLIENT_TYPE_ENUM.spot ); return this; } fetchServerTime(): Promise { return this.getServerTime(); } async getServerTime(): Promise { const res = await this.get('/spot/v1/time'); return res.result.serverTime; } /** * * Market Data Endpoints * **/ getSymbols(): Promise> { return this.get('/spot/v1/symbols'); } getOrderBook(symbol: string, limit?: number): Promise> { return this.get('/spot/quote/v1/depth', { symbol, limit, }); } getMergedOrderBook( symbol: string, scale?: number, limit?: number ): Promise> { return this.get('/spot/quote/v1/depth/merged', { symbol, scale, limit, }); } getTrades(symbol: string, limit?: number): Promise> { return this.get('/spot/quote/v1/trades', { symbol, limit, }); } getCandles( symbol: string, interval: KlineInterval, limit?: number, startTime?: number, endTime?: number ): Promise> { return this.get('/spot/quote/v1/kline', { symbol, interval, limit, startTime, endTime, }); } get24hrTicker(symbol?: string): Promise> { return this.get('/spot/quote/v1/ticker/24hr', { symbol }); } getLastTradedPrice(symbol?: string): Promise> { return this.get('/spot/quote/v1/ticker/price', { symbol }); } getBestBidAskPrice(symbol?: string): Promise> { return this.get('/spot/quote/v1/ticker/book_ticker', { symbol }); } /** * Account Data Endpoints */ submitOrder(params: NewSpotOrder): Promise> { return this.postPrivate('/spot/v1/order', { ...params, agentSource, }); } getOrder(params: SpotOrderQueryById): Promise> { return this.getPrivate('/spot/v1/order', params); } cancelOrder(params: SpotOrderQueryById): Promise> { return this.deletePrivate('/spot/v1/order', params); } cancelOrderBatch(params: { symbol: string; side?: OrderSide; orderTypes: OrderTypeSpot[]; }): Promise> { const orderTypes = params.orderTypes ? params.orderTypes.join(',') : undefined; return this.deletePrivate('/spot/order/batch-cancel', { ...params, orderTypes, }); } getOpenOrders( symbol?: string, orderId?: string, limit?: number ): Promise> { return this.getPrivate('/spot/v1/open-orders', { symbol, orderId, limit, }); } getPastOrders( symbol?: string, orderId?: string, limit?: number ): Promise> { return this.getPrivate('/spot/v1/history-orders', { symbol, orderId, limit, }); } getMyTrades( symbol?: string, limit?: number, fromId?: number, toId?: number ): Promise> { return this.getPrivate('/spot/v1/myTrades', { symbol, limit, fromId, toId, }); } /** * Wallet Data Endpoints */ getBalances(): Promise> { return this.getPrivate('/spot/v1/account'); } }