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
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 one channnel to one websocket
  • Loading branch information
develoQ authored and develoQ committed Jan 9, 2021
commit dfb2dc533fb89e6225cd7c88cdde324aca9b1f2d
125 changes: 125 additions & 0 deletions src/exchanges/gmocoin-client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const BasicClient = require("../basic-client");
const BasicMultiClient = require("../basic-multiclient");
const { MarketObjectTypes } = require("../enums");
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 {
bmancini55 marked this conversation as resolved.
Show resolved Hide resolved
constructor(options = {}) {
Expand All @@ -19,6 +21,129 @@ class GMOCoinClient extends BasicMultiClient {
_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;
map.keys(k => {
if (
key.remote_id === remote_id &&
key.marketObjectType == MarketObjectTypes.level2snapshot
) {
key = k;
return;
}
});
// 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 } = {}) {
Expand Down