okx-api
Version:
Complete Node.js SDK for OKX's REST APIs and WebSockets, with TypeScript & end-to-end tests
177 lines • 6.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebsocketAPIClient = void 0;
const websocket_client_js_1 = require("./websocket-client.js");
/**
* This is a minimal Websocket API wrapper around the WebsocketClient. It allows you to use the WebSocket API in a promise-driven way, send a request and await the response.
*
* 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/okx-api/blob/master/examples/ws-api-trade-raw.ts
*/
class WebsocketAPIClient {
wsClient;
options;
constructor(options, logger) {
this.wsClient = new websocket_client_js_1.WebsocketClient(options, logger);
this.options = {
attachEventListeners: true,
...options,
};
this.setupDefaultEventListeners();
}
getWSClient() {
return this.wsClient;
}
setTimeOffsetMs(newOffset) {
return this.getWSClient().setTimeOffsetMs(newOffset);
}
/**
*
*
* OKX WebSocket API Methods
*
*
*/
/**
* Submit a new order
*
* https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-place-order
*/
submitNewOrder(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('private'), 'order', params);
}
/**
* Submit multiple orders in a batch
*
* https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-place-multiple-orders
*/
submitMultipleOrders(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('private'), 'batch-orders', params);
}
/**
* Cancel a single order
*
* https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-cancel-order
*/
cancelOrder(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('private'), 'cancel-order', params);
}
/**
* Cancel multiple orders
*
* https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-cancel-multiple-orders
*/
cancelMultipleOrders(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('private'), 'batch-cancel-orders', params);
}
/**
* Amend a single order
*
* https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-amend-order
*/
amendOrder(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('private'), 'amend-order', params);
}
/**
* Amend multiple orders
*
* https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-amend-multiple-orders
*/
amendMultipleOrders(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('private'), 'batch-amend-orders', params);
}
/**
* Mass cancel orders
*
* https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-mass-cancel-order
*/
massCancelOrders(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('private'), 'mass-cancel', params);
}
/**
* Submit a new spread order
*
* https://www.okx.com/docs-v5/en/#spread-trading-websocket-trade-api-ws-place-order
*/
submitSpreadOrder(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('business'), 'sprd-order', params);
}
/**
* Amend a spread order
*
* https://www.okx.com/docs-v5/en/#spread-trading-websocket-trade-api-ws-amend-order
*/
amendSpreadOrder(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('business'), 'sprd-amend-order', params);
}
/**
* Cancel a spread order
*
* https://www.okx.com/docs-v5/en/#spread-trading-websocket-trade-api-ws-cancel-order
*/
cancelSpreadOrder(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('business'), 'sprd-cancel-order', params);
}
/**
* Mass cancel spread orders
*
* https://www.okx.com/docs-v5/en/#spread-trading-websocket-trade-api-ws-cancel-all-orders
*/
massCancelSpreadOrders(params) {
return this.wsClient.sendWSAPIRequest(this.getWSClient().getMarketWsKey('business'), 'sprd-mass-cancel', params);
}
/**
*
*
*
*
*
*
*
* Private methods for handling some of the convenience/automation provided by the WS API Client
*
*
*
*
*
*
*
*/
connectWSAPI() {
return this.getWSClient().connectWSAPI();
}
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=websocket-api-client.js.map