Skip to content
This repository has been archived by the owner on Sep 9, 2023. It is now read-only.

Commit

Permalink
all: refactor to allow customized wssPath and watcherMs
Browse files Browse the repository at this point in the history
  • Loading branch information
bmancini55 committed Aug 18, 2020
1 parent 188d4f7 commit 98f392c
Show file tree
Hide file tree
Showing 39 changed files with 127 additions and 100 deletions.
11 changes: 6 additions & 5 deletions src/exchanges/bibox-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BiboxClient extends EventEmitter {
This makes like hard and we need to batch connections, which
is why we can't use the BasicMultiClient.
*/
constructor() {
constructor(options) {
super();

/**
Expand All @@ -36,6 +36,7 @@ class BiboxClient extends EventEmitter {
*/
this._clients = [];

this.options = options;
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
Expand Down Expand Up @@ -118,7 +119,7 @@ class BiboxClient extends EventEmitter {
// to create a new client.
if (!client) {
// construct a new client
client = new BiboxBasicClient();
client = new BiboxBasicClient(this.options);

// set properties
client.parent = this;
Expand Down Expand Up @@ -202,9 +203,9 @@ class BiboxBasicClient extends BasicClient {
Manages connections for a single market. A single
socket is only allowed to work for 20 markets.
*/
constructor() {
super("wss:https://push.bibox.com", "Bibox");
this._watcher = new Watcher(this, 10 * 60 * 1000); // change to 10 minutes
constructor({ wssPath = "wss:https://push.bibox.com", watcherMs = 600 * 1000 } = {}) {
super(wssPath, "Bibox");
this._watcher = new Watcher(this, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
Expand Down
6 changes: 2 additions & 4 deletions src/exchanges/binance-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BinanceBase extends BasicClient {
name,
wssPath,
restL2SnapshotPath,
watcherMs = 30000,
useAggTrades = true,
requestSnapshot = true,
socketBatchSize = 200,
Expand All @@ -44,10 +45,7 @@ class BinanceBase extends BasicClient {
l2updateSpeed = "",
l2snapshotSpeed = "",
} = {}) {
super();

this._name = name;
this._wssPath = wssPath;
super(wssPath, name, undefined, watcherMs);
this._restL2SnapshotPath = restL2SnapshotPath;

this.useAggTrades = useAggTrades;
Expand Down
2 changes: 2 additions & 0 deletions src/exchanges/binance-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class BinanceClient extends BinanceBase {
socketBatchSize = 200,
socketThrottleMs = 1000,
restThrottleMs = 1000,
watcherMs,
} = {}) {
super({
name: "Binance",
Expand All @@ -17,6 +18,7 @@ class BinanceClient extends BinanceBase {
socketBatchSize,
socketThrottleMs,
restThrottleMs,
watcherMs,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/exchanges/binance-futures-coinm-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BinanceFuturesCoinmClient extends BinanceBase {
restThrottleMs = 1000,
l2snapshotSpeed = "100ms",
l2updateSpeed = "100ms",
watcherMs,
} = {}) {
super({
name: "Binance Futures COIN-M",
Expand All @@ -23,6 +24,7 @@ class BinanceFuturesCoinmClient extends BinanceBase {
restThrottleMs,
l2snapshotSpeed,
l2updateSpeed,
watcherMs,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/exchanges/binance-futures-usdtm-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BinanceFuturesUsdtmClient extends BinanceBase {
restThrottleMs = 1000,
l2snapshotSpeed = "100ms",
l2updateSpeed = "0ms",
watcherMs,
} = {}) {
super({
name: "Binance Futures USDT-M",
Expand All @@ -23,6 +24,7 @@ class BinanceFuturesUsdtmClient extends BinanceBase {
restThrottleMs,
l2snapshotSpeed,
l2updateSpeed,
watcherMs,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/exchanges/binanceje-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class BinanceJeClient extends BinanceBase {
socketBatchSize = 200,
socketThrottleMs = 1000,
restThrottleMs = 1000,
watcherMs,
} = {}) {
super({
name: "BinanceJe",
Expand All @@ -17,6 +18,7 @@ class BinanceJeClient extends BinanceBase {
socketBatchSize,
socketThrottleMs,
restThrottleMs,
watcherMs,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/exchanges/binanceus-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class BinanceUSClient extends BinanceBase {
socketBatchSize = 200,
socketThrottleMs = 1000,
restThrottleMs = 1000,
watcherMs,
} = {}) {
super({
name: "BinanceUS",
Expand All @@ -17,6 +18,7 @@ class BinanceUSClient extends BinanceBase {
socketBatchSize,
socketThrottleMs,
restThrottleMs,
watcherMs,
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/bitfinex-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const Level3Snapshot = require("../level3-snapshot");
const Level3Update = require("../level3-update");

class BitfinexClient extends BasicClient {
constructor() {
super("wss:https://api.bitfinex.com/ws", "Bitfinex");
constructor({ wssPath = "wss:https://api.bitfinex.com/ws", watcherMs } = {}) {
super(wssPath, "Bitfinex", undefined, watcherMs);
this._channels = {};

this.hasTickers = true;
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/bitflyer-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const Level2Snapshot = require("../level2-snapshot");
const moment = require("moment");

class BitFlyerClient extends BasicClient {
constructor() {
super("wss:https://ws.lightstream.bitflyer.com/json-rpc", "BitFlyer");
constructor({ wssPath = "wss:https://ws.lightstream.bitflyer.com/json-rpc", watcherMs } = {}) {
super(wssPath, "BitFlyer", undefined, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasLevel2Updates = true;
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/bithumb-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const { debounce } = require("../flowcontrol/debounce");
const { throttle } = require("../flowcontrol/throttle");

class BithumbClient extends BasicClient {
constructor() {
super("wss:https://pubwss.bithumb.com/pub/ws", "Bithumb");
constructor({ wssPath = "wss:https://pubwss.bithumb.com/pub/ws", watcherMs } = {}) {
super(wssPath, "Bithumb", undefined, watcherMs);
this._restL2SnapshotPath = "https://api.bithumb.com/public/orderbook";
this.hasTickers = true;
this.hasTrades = true;
Expand Down
5 changes: 3 additions & 2 deletions src/exchanges/bitmex-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class BitmexClient extends BasicClient {
Documentation:
https://www.bitmex.com/app/wsAPI
*/
constructor() {
super("wss:https://www.bitmex.com/realtime", "BitMEX");
constructor({ wssPath = "wss:https://www.bitmex.com/realtime", watcherMs } = {}) {
super(wssPath, "BitMEX", undefined, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
Expand Down Expand Up @@ -376,6 +376,7 @@ class BitmexClient extends BasicClient {
}

if (!price) {
// eslint-disable-next-line no-console
console.warn("unknown price", datum);
}

Expand Down
6 changes: 2 additions & 4 deletions src/exchanges/bitstamp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ class BitstampClient extends BasicClient {
Documentation for Version 2:
https://www.bitstamp.net/websocket/v2/
*/
constructor() {
super();
this._name = "Bitstamp";
this._wssPath = "wss:https://ws.bitstamp.net";
constructor({ wssPath = "wss:https://ws.bitstamp.net", watcherMs } = {}) {
super(wssPath, "Bitstamp", undefined, watcherMs);
this.requestSnapshot = true;
this.hasTrades = true;
this.hasLevel2Snapshots = true;
Expand Down
20 changes: 12 additions & 8 deletions src/exchanges/bittrex-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const { CandlePeriod } = require("../enums");
* standard client.
*/
class BittrexClient extends BasicClient {
constructor({ watcherMs = 15000 } = {}) {
super(undefined, "Bittrex", undefined, watcherMs);
constructor({ wssPath, watcherMs = 15000 } = {}) {
super(wssPath, "Bittrex", undefined, watcherMs);

this.hasTickers = true;
this.hasTrades = true;
Expand Down Expand Up @@ -161,12 +161,16 @@ class BittrexClient extends BasicClient {

async _connectAsync() {
try {
let data = JSON.stringify([{ name: "c3" }]);
let negotiations = await https.get(
`https://socket-v3.bittrex.com/signalr/negotiate?connectionData=${data}&clientProtocol=1.5`
);
let token = encodeURIComponent(negotiations.ConnectionToken);
let wssPath = `wss:https://socket-v3.bittrex.com/signalr/connect?clientProtocol=1.5&transport=webSockets&connectionToken=${token}&connectionData=${data}&tid=10`;
let wssPath = this.wssPath;

if (!wssPath) {
let data = JSON.stringify([{ name: "c3" }]);
let negotiations = await https.get(
`https://socket-v3.bittrex.com/signalr/negotiate?connectionData=${data}&clientProtocol=1.5`
);
let token = encodeURIComponent(negotiations.ConnectionToken);
wssPath = `wss:https://socket-v3.bittrex.com/signalr/connect?clientProtocol=1.5&transport=webSockets&connectionToken=${token}&connectionData=${data}&tid=10`;
}

let wss = new SmartWss(wssPath);
this._wss = wss;
Expand Down
31 changes: 21 additions & 10 deletions src/exchanges/cex-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ function formatAmount(amount, symbol) {
class CexClient extends BasicMultiClient {
/**
* Creates a new CEX.io client using the supplied credentials
* @param {{ apiKey: string, apiSecret: string }} auth
* @param {{ apiKey: string, apiSecret: string }} options
*/
constructor(auth) {
constructor(options = {}) {
super();
this._clients = new Map();

this._name = "CEX_MULTI";
this.auth = auth;
this.options = options;
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
Expand All @@ -74,22 +74,33 @@ class CexClient extends BasicMultiClient {
}

_createBasicClient(clientArgs) {
return new SingleCexClient({ auth: this.auth, market: clientArgs.market, parent: this });
return new SingleCexClient({
...this.options,
market: clientArgs.market,
parent: this,
});
}
}

class SingleCexClient extends BasicClient {
constructor(args) {
super("wss:https://ws.cex.io/ws", "CEX");
this._watcher = new Watcher(this, 15 * 60 * 1000);
this.auth = args.auth;
this.market = args.market;
constructor({
wssPath = "wss:https://ws.cex.io/ws",
watcherMs = 900 * 1000,
apiKey,
apiSecret,
market,
parent,
}) {
super(wssPath, "CEX", undefined, watcherMs);
this._watcher = new Watcher(this, watcherMs);
this.auth = { apiKey, apiSecret };
this.market = market;
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
this.hasLevel2Snapshots = true;
this.authorized = false;
this.parent = args.parent;
this.parent = parent;
}

get candlePeriod() {
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/coinbasepro-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const Level3Point = require("../level3-point");
const Level3Update = require("../level3-update");

class CoinbaseProClient extends BasicClient {
constructor() {
super("wss:https://ws-feed.pro.coinbase.com", "CoinbasePro");
constructor({ wssPath = "wss:https://ws-feed.pro.coinbase.com", watcherMs } = {}) {
super(wssPath, "CoinbasePro", undefined, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasLevel2Spotshots = false;
Expand Down
11 changes: 6 additions & 5 deletions src/exchanges/coinex-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const Level2Update = require("../level2-update");
const { MarketObjectTypes, CandlePeriod } = require("../enums");

class CoinexClient extends BasicMultiClient {
constructor() {
constructor(options = {}) {
super();
this.options = options;
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
Expand All @@ -21,14 +22,14 @@ class CoinexClient extends BasicMultiClient {
}

_createBasicClient() {
return new CoinexSingleClient(this);
return new CoinexSingleClient({ ...this.options, parent: this });
}
}

class CoinexSingleClient extends BasicClient {
constructor(parent) {
super("wss:https://socket.coinex.com/", "Coinex");
this._watcher = new Watcher(this, 15 * 60 * 1000);
constructor({ wssPath = "wss:https://socket.coinex.com/", watcherMs = 900 * 1000, parent }) {
super(wssPath, "Coinex", undefined, watcherMs);
this._watcher = new Watcher(this, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/deribit-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const { CandlePeriod } = require("../enums");
const { throttle } = require("../flowcontrol/throttle");

class DeribitClient extends BasicClient {
constructor() {
super("wss:https://www.deribit.com/ws/api/v2", "Deribit");
constructor({ wssPath = "wss:https://www.deribit.com/ws/api/v2", watcherMs } = {}) {
super(wssPath, "Deribit", undefined, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasCandles = true;
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/digifinex-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const Level2Update = require("../level2-update");
* https://github.com/DigiFinex/api/blob/master/Websocket_API_en.md
*/
class DigifinexClient extends BasicClient {
constructor() {
super("wss:https://openapi.digifinex.com/ws/v1/", "Digifinex");
constructor({ wssPath = "wss:https://openapi.digifinex.com/ws/v1/", watcherMs } = {}) {
super(wssPath, "Digifinex", undefined, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasLevel2Updates = true;
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/ftx-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const Level2Update = require("../level2-update");
const Level2Snapshot = require("../level2-snapshot");

class FtxBaseClient extends BasicClient {
constructor({ name, wssPath }) {
super(wssPath, name);
constructor({ name, wssPath, watcherMs }) {
super(wssPath, name, undefined, watcherMs);
this.hasTickers = true;
this.hasTrades = true;
this.hasLevel2Updates = true;
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/ftx-client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const FtxBaseClient = require("./ftx-base");

class FtxClient extends FtxBaseClient {
constructor() {
super({ name: "FTX", wssPath: "wss:https://ftx.com/ws" });
constructor({ wssPath = "wss:https://ftx.com/ws", watcherMs } = {}) {
super({ name: "FTX", wssPath, watcherMs });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/ftx-us-client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const FtxBaseClient = require("./ftx-base");

class FtxUsClient extends FtxBaseClient {
constructor() {
super({ name: "FTX US", wssPath: "wss:https://ftx.us/ws" });
constructor({ wssPath = "wss:https://ftx.us/ws", watcherMs } = {}) {
super({ name: "FTX US", wssPath, watcherMs });
}
}

Expand Down
Loading

0 comments on commit 98f392c

Please sign in to comment.