UNPKG

@reactivemarkets/switchboard-sdk

Version:
188 lines (187 loc) 7.87 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrdersClient = void 0; const switchboard_api_1 = require("@reactivemarkets/switchboard-api"); const flatbuffers = __importStar(require("flatbuffers")); const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket")); const tiny_typed_emitter_1 = require("tiny-typed-emitter"); const domain_1 = require("./domain"); class OrdersClient extends tiny_typed_emitter_1.TypedEmitter { constructor(options) { super(); this.onClose = (event) => { this.emit("close", event.code, event.reason); }; this.onError = (event) => { this.emit("error", event.error); }; this.onOpen = () => { this.emit("open"); }; this.onMessage = (event) => { const byteBuffer = event.data; const bytes = new Uint8Array(byteBuffer); const buffer = new flatbuffers.ByteBuffer(bytes); const message = switchboard_api_1.Message.getRootAsMessage(buffer); const bodyType = message.bodyType(); switch (bodyType) { case switchboard_api_1.Body.ExecutionReport: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.ExecutionReport()); if (body !== null) { this.emit("execution-report", body, { sendingTime }); } break; } case switchboard_api_1.Body.Heartbeat: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.Heartbeat()); if (body !== null) { this.emit("heartbeat", body, { sendingTime }); } break; } case switchboard_api_1.Body.OrderCancelReject: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.OrderCancelReject()); if (body !== null) { this.emit("order-cancel-reject", body, { sendingTime }); } break; } case switchboard_api_1.Body.Reject: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.Reject()); if (body !== null) { this.emit("reject", body, { sendingTime }); } break; } case switchboard_api_1.Body.TestRequest: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.TestRequest()); if (body !== null) { this.emit("test-request", body, { sendingTime }); } break; } } }; const { apiKey = process.env.REACTIVE_SWITCHBOARD_API_KEY, apiUrl = "wss://or.switchboard.reactivemarkets.com/v1", minReconnectionDelay = 1000 + Math.random() * 1000, WebSocketCtor } = options, rest = __rest(options, ["apiKey", "apiUrl", "minReconnectionDelay", "WebSocketCtor"]); const url = `${apiUrl}?api_key=${apiKey}`; const protocol = "fbs"; this.websocket = new reconnecting_websocket_1.default(url, protocol, Object.assign({ WebSocket: WebSocketCtor, minReconnectionDelay }, rest)); this.websocket.binaryType = "arraybuffer"; this.websocket.onopen = this.onOpen; this.websocket.onclose = this.onClose; this.websocket.onerror = this.onError; this.websocket.onmessage = this.onMessage; } get isOpen() { return this.websocket.readyState === this.websocket.OPEN; } newOrderSingle(nos) { const bytes = (0, domain_1.newOrderSingle)() .account(nos.account) .clOrderId(nos.clOrderId) .execVenues(nos.execVenues) .farPrice(nos.farPrice) .farQty(nos.farQty) .farTenor(nos.farTenor) .orderType(nos.orderType) .price(nos.price) .priceTolerance(nos.priceTolerance) .qty(nos.qty) .quoteId(nos.quoteId) .securityType(nos.securityType) .side(nos.side) .strategy(nos.strategy) .strategyParameters(nos.strategyParameters) .symbol(nos.symbol) .tenor(nos.tenor) .text(nos.text) .timeInForce(nos.timeInForce) .venue(nos.venue) .build(); this.websocket.send(bytes); } orderCancelRequest(ocr) { const bytes = (0, domain_1.orderCancelRequest)() .account(ocr.account) .clOrderId(ocr.clOrderId) .orderId(ocr.orderId) .origClOrderId(ocr.origClOrderId) .securityType(ocr.securityType) .symbol(ocr.symbol) .tenor(ocr.tenor) .venue(ocr.venue) .build(); this.websocket.send(bytes); } orderReplaceRequest(orr) { const bytes = (0, domain_1.orderReplaceRequest)() .account(orr.account) .execInst(orr.execInst) .clOrderId(orr.clOrderId) .orderId(orr.orderId) .origClOrderId(orr.origClOrderId) .price(orr.price) .qty(orr.qty) .securityType(orr.securityType) .symbol(orr.symbol) .tenor(orr.tenor) .venue(orr.venue) .build(); this.websocket.send(bytes); } heartbeat(requestId) { const bytes = (0, domain_1.heartbeat)() .requestId(requestId) .build(); this.websocket.send(bytes); } testRequest(requestId) { const bytes = (0, domain_1.testRequest)() .requestId(requestId) .build(); this.websocket.send(bytes); } } exports.OrdersClient = OrdersClient;