UNPKG

kucoin-api

Version:

Complete & robust Node.js SDK for Kucoin's REST APIs and WebSockets, with TypeScript & strong end to end tests.

153 lines 5.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebsocketAPIClient = void 0; const websocket_util_js_1 = require("./lib/websocket/websocket-util.js"); const WebsocketClient_js_1 = require("./WebsocketClient.js"); /** * This is a minimal Websocket API wrapper around the WebsocketClient. * * Some methods support passing in a custom "wsKey". This is a reference to which WS connection should * be used to transmit that message. This is only useful if you wish to use an alternative wss * domain that is supported by the SDK. * * Note: To use testnet, don't set the wsKey - use `testnet: true` in * the constructor instead. * * Note: You can also directly use the sendWSAPIRequest() method to make WS API calls, but some * may find the below methods slightly more intuitive. * * Refer to the WS API promises example for a more detailed example on using sendWSAPIRequest() directly: * https://github.com/tiagosiebler/binance/blob/master/examples/WebSockets/ws-api-raw-promises.ts#L108 */ class WebsocketAPIClient { wsClient; options; constructor(options, logger) { this.wsClient = new WebsocketClient_js_1.WebsocketClient(options, logger); this.options = { attachEventListeners: true, ...options, }; this.setupDefaultEventListeners(); } getWSClient() { return this.wsClient; } /** * Submit a spot order */ submitNewSpotOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiSpotV1, 'spot.order', params); } /** * Modify a spot order */ modifySpotOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiSpotV1, 'spot.modify', params); } /** * Cancel a spot order */ cancelSpotOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiSpotV1, 'spot.cancel', params); } /** * Submit a sync spot order */ submitSyncSpotOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiSpotV1, 'spot.sync_order', params); } /** * Cancel a sync spot order */ cancelSyncSpotOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiSpotV1, 'spot.sync_cancel', params); } /** * Submit a margin order */ submitMarginOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiSpotV1, 'margin.order', params); } /** * Cancel a margin order */ cancelMarginOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiSpotV1, 'margin.cancel', params); } /** * Submit a futures order */ submitFuturesOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiFuturesV1, 'futures.order', params); } /** * Cancel a futures order */ cancelFuturesOrder(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiFuturesV1, 'futures.cancel', params); } /** * Submit multiple futures orders */ submitMultipleFuturesOrders(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiFuturesV1, 'futures.multi_order', params); } /** * Cancel multiple futures orders */ cancelMultipleFuturesOrders(params, wsKey) { return this.wsClient.sendWSAPIRequest(wsKey || websocket_util_js_1.WS_KEY_MAP.wsApiFuturesV1, 'futures.multi_cancel', params); } /** * * * * * * * * Private methods for handling some of the convenience/automation provided by the WS API Client * * * * * * * */ connectWSAPI(wsKey) { return this.getWSClient().assertIsAuthenticated(wsKey); } setupDefaultEventListeners() { if (this.options.attachEventListeners) { /** * General event handlers for monitoring the WebsocketClient */ this.wsClient .on('open', (data) => { console.log(new Date(), 'ws connected', data.wsKey); }) .on('reconnect', ({ wsKey }) => { console.log(new Date(), 'ws automatically reconnecting.... ', wsKey); }) .on('reconnected', (data) => { console.log(new Date(), 'ws has reconnected ', data?.wsKey); }) .on('authenticated', (data) => { console.info(new Date(), 'ws has authenticated ', data?.wsKey); }) .on('exception', (data) => { try { // Blind JSON.stringify can fail on circular references console.error(new Date(), 'ws exception: ', JSON.stringify(data)); } catch { console.error(new Date(), 'ws exception: ', data); } }); } } } exports.WebsocketAPIClient = WebsocketAPIClient; //# sourceMappingURL=WebsocketAPIClient.js.map