import { APIResponseWithTime, APIResponseV3, SpotOrderQueryById, OrderSide, OrderTypeSpot, SpotBalances, KlineInterval, NewSpotOrderV3, SpotMyTradesRequest, SpotLeveragedTokenPRHistoryRequest, SpotCrossMarginBorrowingInfoRequest, SpotCrossMarginRepaymentHistoryRequest, } from './types'; import { REST_CLIENT_TYPE_ENUM } from './util'; import BaseRestClient from './util/BaseRestClient'; /** * REST API client for newer Spot V3 APIs. */ export class SpotClientV3 extends BaseRestClient { getClientType() { // Follows the same authentication mechanism as other v3 APIs (e.g. USDC) return REST_CLIENT_TYPE_ENUM.v3; } async fetchServerTime(): Promise { const res = await this.getServerTime(); return Number(res.time_now); } /** * * Market Data Endpoints * */ /** Get all symbols */ getSymbols(): Promise> { return this.get('/spot/v3/public/symbols'); } /** Get orderbook for symbol */ getOrderBook(symbol: string, limit?: number): Promise> { return this.get('/spot/v3/public/quote/depth', { symbol, limit }); } /** Get merged orderbook for symbol */ getMergedOrderBook( symbol: string, scale?: number, limit?: number ): Promise> { return this.get('/spot/v3/public/quote/depth/merged', { symbol, scale, limit, }); } /** Get public trading records (raw trades) */ getTrades(symbol: string, limit?: number): Promise> { return this.get('/spot/v3/public/quote/trades', { symbol, limit }); } /** Get candles/klines */ getCandles( symbol: string, interval: KlineInterval, limit?: number, startTime?: number, endTime?: number ): Promise> { return this.get('/spot/v3/public/quote/kline', { symbol, interval, limit, startTime, endTime, }); } /** Get latest information for symbol (24hr ticker) */ get24hrTicker(symbol?: string): Promise> { return this.get('/spot/v3/public/quote/ticker/24hr', { symbol }); } /** Get last traded price */ getLastTradedPrice(symbol?: string): Promise> { return this.get('/spot/v3/public/quote/ticker/price', { symbol }); } /** Get best bid/ask price */ getBestBidAskPrice(symbol?: string): Promise> { return this.get('/spot/v3/public/quote/ticker/bookTicker', { symbol }); } /** * * Account Data Endpoints * */ /** -> Order API */ /** Create order */ submitOrder(params: NewSpotOrderV3): Promise> { return this.postPrivate('/spot/v3/private/order', params); } /** Get active order state */ getOrder(params: SpotOrderQueryById): Promise> { return this.getPrivate('/spot/v3/private/order', params); } /** Cancel order */ cancelOrder(params: SpotOrderQueryById): Promise> { return this.postPrivate('/spot/v3/private/cancel-order', params); } /** Batch cancel orders */ cancelOrderBatch(params: { symbol: string; side?: OrderSide; orderTypes: OrderTypeSpot[]; }): Promise> { const orderTypes = params.orderTypes ? params.orderTypes.join(',') : undefined; return this.postPrivate('/spot/v3/private/cancel-orders', { ...params, orderTypes, }); } /** Batch cancel up to 100 orders by ID */ cancelOrderBatchIDs(orderIds: string[]): Promise> { const orderIdsCsv = orderIds.join(','); return this.postPrivate('/spot/v3/private/cancel-orders-by-ids', { orderIds: orderIdsCsv, }); } /** Get open orders */ getOpenOrders( symbol?: string, orderId?: string, limit?: number ): Promise> { return this.getPrivate('/spot/v3/private/open-orders', { symbol, orderId, limit, }); } /** Get order history */ getPastOrders( symbol?: string, orderId?: string, limit?: number ): Promise> { return this.getPrivate('/spot/v3/private/history-orders', { symbol, orderId, limit, }); } /** * Get your trade history. * If startTime is not specified, you can only query for records in the last 7 days. * If you want to query for records older than 7 days, startTime is required. */ getMyTrades(params?: SpotMyTradesRequest): Promise> { return this.getPrivate('/spot/v3/private/my-trades', params); } /** * * Wallet Data Endpoints * */ /** Get Wallet Balance */ getBalances(): Promise> { return this.getPrivate('/spot/v3/private/account'); } /** * * API Data Endpoints * */ getServerTime(): Promise { return this.get('/v2/public/time'); } /** * * Leveraged Token Endpoints * */ /** Get all asset infos */ getLeveragedTokenAssetInfos(ltCode?: string): Promise> { return this.get('/spot/v3/public/infos', { ltCode }); } /** Get leveraged token market info */ getLeveragedTokenMarketInfo(ltCode: string): Promise> { return this.getPrivate('/spot/v3/private/reference', { ltCode }); } /** Purchase leveraged token */ purchaseLeveragedToken( ltCode: string, ltAmount: string, serialNo?: string ): Promise> { return this.postPrivate('/spot/v3/private/purchase', { ltCode, ltAmount, serialNo, }); } /** Redeem leveraged token */ redeemLeveragedToken( ltCode: string, ltAmount: string, serialNo?: string ): Promise> { return this.postPrivate('/spot/v3/private/redeem', { ltCode, ltAmount, serialNo, }); } /** Get leveraged token purchase/redemption history */ getLeveragedTokenPRHistory( params?: SpotLeveragedTokenPRHistoryRequest ): Promise> { return this.getPrivate('/spot/v3/private/record', params); } /** * * Cross Margin Trading Endpoints * */ /** Borrow margin loan */ borrowCrossMarginLoan( coin: string, qty: string ): Promise> { return this.postPrivate('/spot/v3/private/cross-margin-loan', { coin, qty, }); } /** Repay margin loan */ repayCrossMarginLoan(coin: string, qty: string): Promise> { return this.postPrivate('/spot/v3/private/cross-margin-repay', { coin, qty, }); } /** Query borrowing info */ getCrossMarginBorrowingInfo( params?: SpotCrossMarginBorrowingInfoRequest ): Promise> { return this.getPrivate('/spot/v3/private/cross-margin-orders', params); } /** Query account info */ getCrossMarginAccountInfo(): Promise> { return this.getPrivate('/spot/v3/private/cross-margin-account'); } /** Query interest & quota */ getCrossMarginInterestQuota(coin: string): Promise> { return this.getPrivate('/spot/v3/private/cross-margin-loan-info', { coin }); } /** Query repayment history */ getCrossMarginRepaymentHistory( params?: SpotCrossMarginRepaymentHistoryRequest ): Promise> { return this.getPrivate( '/spot/v3/private/cross-margin-repay-history', params ); } }