UNPKG

@bluefin-exchange/bluefin-v2-client

Version:

The Bluefin client Library allows traders to sign, create, retrieve and listen to orders on Bluefin Exchange.

217 lines 8.19 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sockets = void 0; /* eslint-disable no-unused-vars */ const socket_io_client_1 = require("socket.io-client"); const library_sui_1 = require("@firefly-exchange/library-sui"); class Sockets { constructor(url) { this.callbacks = {}; this.setAuthToken = (token) => { this.token = token; }; this.setApiToken = (apiToken) => __awaiter(this, void 0, void 0, function* () { this.apiToken = apiToken; }); // Emitted when any price bin on the oderbook is updated. this.onOrderBookUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.OrderbookUpdateKey, cb); }; this.onOrderBookPartialDepthUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.OrderbookDepthUpdateKey, cb); }; this.onMarketDataUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.MarketDataUpdateKey, cb); }; this.onMarketHealthChange = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.MarketHealthKey, cb); }; this.onUserOrderSentForSettlementUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.OrderSentForSettlementUpdate, cb); }; this.onUserOrderRequeueUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.OrderRequeueUpdate, cb); }; this.onUserOrderCancellationOnReversionUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.OrderCancelledOnReversionUpdate, cb); }; this.onCandleStickUpdate = (symbol, interval, cb) => { this.socketInstance.on(this.createDynamicUrl(library_sui_1.SOCKET_EVENTS.GET_LAST_KLINE_WITH_INTERVAL, { symbol, interval, }), cb); }; this.onExchangeHealthChange = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.ExchangeHealthKey, cb); }; this.onTickerUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.TickerUpdate, cb); }; // TODO: figure out what it does this.onRecentTrades = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.RecentTradesKey, cb); }; this.onUserOrderUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.OrderUpdateKey, cb); }; this.onUserOrderCancellationFailed = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.OrderCancellationFailedKey, cb); }; this.onUserPositionUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.PositionUpdateKey, cb); }; this.onCustomEvent = (cb, customEventKey) => { this.socketInstance.on(customEventKey, cb); }; this.onUserUpdates = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.UserTradeKey, cb); }; this.onUserAccountDataUpdate = (cb) => { this.socketInstance.on(library_sui_1.SOCKET_EVENTS.AccountDataUpdateKey, cb); }; this.url = url; this.token = ""; this.apiToken = ""; } createDynamicUrl(dynamicUrl, object) { // eslint-disable-next-line no-restricted-syntax, guard-for-in for (const key in object) { dynamicUrl = dynamicUrl.replace(`{${key}}`, object[key]); } return dynamicUrl; } /** * opens socket instance connection */ open() { this.socketInstance = (0, socket_io_client_1.io)(this.url, { transports: ["websocket"], }); this.onConnect(); this.onDisconnect(); } /** * closes the socket instance connection */ close() { if (this.socketInstance) { this.socketInstance.disconnect(); this.socketInstance.close(); } } subscribeGlobalUpdatesBySymbol(symbol) { if (!this.socketInstance) return false; this.socketInstance.emit("SUBSCRIBE", [ { e: library_sui_1.SOCKET_EVENTS.GLOBAL_UPDATES_ROOM, p: symbol, }, ]); return true; } unsubscribeGlobalUpdatesBySymbol(symbol) { if (!this.socketInstance) return false; this.socketInstance.emit("UNSUBSCRIBE", [ { e: library_sui_1.SOCKET_EVENTS.GLOBAL_UPDATES_ROOM, p: symbol, }, ]); return true; } /** * Assigns callbacks to desired events */ listen(event, callback) { return __awaiter(this, void 0, void 0, function* () { this.callbacks[event] = callback; }); } subscribeUserUpdateByToken(callback) { if (!this.socketInstance) return false; this.socketInstance.emit("SUBSCRIBE", [ { e: library_sui_1.SOCKET_EVENTS.UserUpdatesRoom, rt: this.apiToken ? this.apiToken : "", t: this.token, }, ], (data) => { if (callback instanceof Function) callback(data); }); return true; } unsubscribeUserUpdateByToken(callback) { if (!this.socketInstance) return false; this.socketInstance.emit("UNSUBSCRIBE", [ { e: library_sui_1.SOCKET_EVENTS.UserUpdatesRoom, rt: this.apiToken ? this.apiToken : "", t: this.token, }, ], (data) => { if (callback instanceof Function) callback(data); }); return true; } subscribeOrderBookDepthStreamBySymbol(symbol, depth = "") { if (!this.socketInstance) return false; this.socketInstance.emit("SUBSCRIBE", [ { e: library_sui_1.SOCKET_EVENTS.ORDERBOOK_DEPTH_STREAM_ROOM, p: symbol, d: depth, }, ]); return true; } unsubscribeOrderBookDepthStreamBySymbol(symbol, depth = "") { if (!this.socketInstance) return false; this.socketInstance.emit("UNSUBSCRIBE", [ { e: library_sui_1.SOCKET_EVENTS.ORDERBOOK_DEPTH_STREAM_ROOM, p: symbol, d: depth, }, ]); return true; } onDisconnect() { return __awaiter(this, void 0, void 0, function* () { this.socketInstance.on("disconnect", () => __awaiter(this, void 0, void 0, function* () { if ("disconnect" in this.callbacks && typeof this.callbacks["disconnect"] === "function") { yield this.callbacks["disconnect"](); } })); }); } onConnect() { return __awaiter(this, void 0, void 0, function* () { this.socketInstance.on("connect", () => __awaiter(this, void 0, void 0, function* () { if ("connect" in this.callbacks && typeof this.callbacks["connect"] === "function") { yield this.callbacks["connect"](); } })); }); } } exports.Sockets = Sockets; //# sourceMappingURL=sockets.js.map