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

Add exchange GMO Coin #247

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix PR Advice
  • Loading branch information
develoQ authored and develoQ committed Jan 11, 2021
commit 0a254de4b972414ade7ae309aaff216a2ce27605
15 changes: 4 additions & 11 deletions __tests__/exchanges/gmocoin-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ testClient({
},
],

testConnectEvents: false,
testDisconnectEvents: false,
testReconnectionEvents: false,
testCloseEvents: false,
testConnectEvents: true,
testDisconnectEvents: true,
testReconnectionEvents: true,
testCloseEvents: true,

hasTickers: true,
hasTrades: true,
Expand Down Expand Up @@ -71,11 +71,4 @@ testClient({
hasSequenceId: false,
hasCount: false,
},

l2update: {
hasSnapshot: true,
hasTimestampMs: false,
hasSequenceId: false,
hasCount: false,
},
});
155 changes: 12 additions & 143 deletions src/exchanges/gmocoin-client.js
Original file line number Diff line number Diff line change
@@ -1,160 +1,29 @@
const BasicClient = require("../basic-client");
const BasicMultiClient = require("../basic-multiclient");
const { MarketObjectTypes } = require("../enums");
const { throttle } = require("../flowcontrol/throttle");
const Ticker = require("../ticker");
const Trade = require("../trade");
const Level2Point = require("../level2-point");
const Level2Snapshot = require("../level2-snapshot");
const moment = require("moment");
const { wait } = require("../util");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a class level comment that points to the documentation and indicates that types of subscriptions that are available to the client?

class GMOCoinClient extends BasicMultiClient {
constructor(options = {}) {
super();
this._name = "GMOCoin";
this.options = options;
this.hasTickers = true;
this.hasTrades = true;
this.hasLevel2Snapshots = true;
}

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

async unsubscribeTicker(market) {
if (!this.hasTickers) return;
let key;
this._clients.keys(k => {
if (key.remote_id === market.id && key.marketObjectType == MarketObjectTypes.ticker) {
key = k;
return;
}
});

if (this._clients.has(key)) {
(await this._clients.get(key)).unsubscribeTicker(market);
}
}

async unsubscribeTrades(market) {
if (!this.hasTrades) return;
let key;
this._clients.keys(k => {
if (key.remote_id === market.id && key.marketObjectType == MarketObjectTypes.trade) {
key = k;
return;
}
});

if (this._clients.has(key)) {
(await this._clients.get(key)).unsubscribeTrades(market);
}
}

async unsubscribeLevel2Snapshots(market) {
if (!this.hasLevel2Snapshots) return;
let key;
this._clients.keys(k => {
if (key.remote_id === market.id && key.marketObjectType == MarketObjectTypes.level2snapshot) {
key = k;
return;
}
});

if (this._clients.has(key)) {
(await this._clients.get(key)).unsubscribeLevel2Snapshots(market);
}
}

async _subscribe(market, map, marketObjectType) {
try {
let remote_id = market.id;
let client = null;
let key = null;
map.keys(k => {
if (key.remote_id === remote_id && key.marketObjectType == marketObjectType) {
key = k;
return;
}
});
if (key === null) {
key = { remote_id: remote_id, marketObjectType: marketObjectType };
}
// construct a client
if (!map.has(key)) {
let clientArgs = { auth: this.auth, market: market };
client = this._createBasicClientThrottled(clientArgs);
// we MUST store the promise in here otherwise we will stack up duplicates
map.set(key, client);
}

// wait for client to be made!
client = await map.get(key);
await wait(1000);

if (marketObjectType === MarketObjectTypes.ticker) {
let subscribed = client.subscribeTicker(market);
if (subscribed) {
client.on("ticker", (ticker, market) => {
this.emit("ticker", ticker, market);
});
}
}

if (marketObjectType === MarketObjectTypes.candle) {
let subscribed = client.subscribeCandles(market);
if (subscribed) {
client.on("candle", (candle, market) => {
this.emit("candle", candle, market);
});
}
}

if (marketObjectType === MarketObjectTypes.trade) {
let subscribed = client.subscribeTrades(market);
if (subscribed) {
client.on("trade", (trade, market) => {
this.emit("trade", trade, market);
});
}
}

if (marketObjectType === MarketObjectTypes.level2update) {
let subscribed = client.subscribeLevel2Updates(market);
if (subscribed) {
client.on("l2update", (l2update, market) => {
this.emit("l2update", l2update, market);
});
client.on("l2snapshot", (l2snapshot, market) => {
this.emit("l2snapshot", l2snapshot, market);
});
}
}

if (marketObjectType === MarketObjectTypes.level2snapshot) {
let subscribed = client.subscribeLevel2Snapshots(market);
if (subscribed) {
client.on("l2snapshot", (l2snapshot, market) => {
this.emit("l2snapshot", l2snapshot, market);
});
}
}
} catch (ex) {
this.emit("error", ex, market);
}
}
}
class GMOCoinSingleClient extends BasicClient {
constructor({ wssPath = "wss:https://api.coin.z.com/ws/public/v1", watcherMs, parent } = {}) {
class GMOCoinClient extends BasicClient {
constructor({
wssPath = "wss:https://api.coin.z.com/ws/public/v1",
throttleMs = 1000,
watcherMs,
} = {}) {
super(wssPath, "GMOCoin", undefined, watcherMs);

this.hasTickers = true;
this.hasTrades = true;
this.hasLevel2Snapshots = true;
this.parent = parent;
this._send = throttle(this._send.bind(this), throttleMs);
}

bmancini55 marked this conversation as resolved.
Show resolved Hide resolved
_send(message) {
this._wss.send(message);
}

_sendPong(id) {
this._wss.send(JSON.stringify({ pong: id }));
bmancini55 marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down