Skip to content

Commit 840154d

Browse files
committed
add balance subscription
redo subscription logic
1 parent a4adbd5 commit 840154d

13 files changed

+414
-422
lines changed

lib/websocket/accountClient.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@ const { AuthClient } = require("./authClient")
55
*/
66
class AccountClient extends AuthClient {
77
constructor(apiKey, apiSecret) {
8-
super("wss://api.exchange.cryptomkt.com/api/2/ws/account", apiKey, apiSecret)
8+
super(
9+
"wss://api.exchange.cryptomkt.com/api/2/ws/account",
10+
apiKey,
11+
apiSecret,
12+
{
13+
// transaction
14+
"unsubscribeTransactions":"transaction",
15+
"subscribeTransactions":"transaction",
16+
"updateTransaction":"transaction",
17+
// balance
18+
"unsubscribeBalance":"balance",
19+
"subscribeBalance":"balance",
20+
"balance":"balance",
21+
},
22+
)
923
}
1024

1125
/**
@@ -84,6 +98,30 @@ class AccountClient extends AuthClient {
8498
return this.sendUnsubscription('unsubscribeTransactions')
8599
}
86100

101+
/**
102+
* Subscribe to a feed of the balances of the account
103+
*
104+
* https://api.exchange.cryptomkt.com/#subscription-to-the-balance
105+
*
106+
* @param {function} callback A function to call with the feed data. It takes one argument.
107+
* @return {Promise<Boolean>} A Promise of the subscription result. True if success
108+
*/
109+
subscribeToBalance(callback) {
110+
this.checkDefined({callback})
111+
return this.sendSubscription('subscribeBalance', callback)
112+
}
113+
114+
/**
115+
* unsubscribe to the balance feed.
116+
*
117+
* https://api.exchange.cryptomkt.com/#subscription-to-the-balance
118+
*
119+
* @return {Promise<Boolean>} A Promise of the unsubscription result. True if success
120+
*/
121+
unsubscribeToBalance() {
122+
return this.sendUnsubscription('unsubscribeBalance')
123+
}
124+
87125
}
88126

89127
module.exports = {

lib/websocket/authClient.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const CryptoJS = require('crypto-js')
22
const { WSClientBase } = require("./clientBase")
33

44
class AuthClient extends WSClientBase {
5-
constructor(url, apiKey, apiSecret) {
6-
super(url)
5+
constructor(url, apiKey, apiSecret, subscriptionKeys={}) {
6+
super(url, subscriptionKeys=subscriptionKeys)
77
this.apiKey = apiKey
88
this.apiSecret = apiSecret
99
}

lib/websocket/clientBase.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ const WebSocket = require('ws');
22
const { EventEmitter } = require('events');
33

44
const { ArgumentFormatException, CryptomarketAPIException, CryptomarketSDKException } = require('../exceptions')
5-
const methods = require("./methods.json");
65

76
class WSClientBase {
8-
constructor(uri) {
9-
this.uri = uri
10-
this.methods = methods
7+
constructor(uri, subscriptionKeys={}) {
8+
this.uri = uri;
9+
this.subscriptionKeys = subscriptionKeys;
1110
this.emitter = new EventEmitter();
1211
this.nextId = 1
1312
}
@@ -93,8 +92,8 @@ class WSClientBase {
9392
}
9493

9594
handleNotification(notification) {
96-
let key = this.buildKey()
97-
this.emitter.emit(key, notification["params"])
95+
let key = this.buildKey(notification.method)
96+
this.emitter.emit(key, notification.params)
9897
}
9998

10099
handleResponse(response) {
@@ -103,8 +102,9 @@ class WSClientBase {
103102
this.emitter.emit(id, response)
104103
}
105104

106-
buildKey() {
107-
return "subscription"
105+
buildKey(method, params) {
106+
if (method in this.subscriptionKeys) this.subscriptionKeys[method];
107+
return ""
108108
}
109109
}
110110

lib/websocket/methods.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/websocket/publicClient.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
const {WSClientBase} = require('./clientBase')
22
const { OrderbookCache } = require('./orderbook_cache')
3-
const methods = require("./methods.json")
43

54
/**
65
* PublicClient connects via websocket to cryptomarket to get market information of the exchange.
76
*/
87
class PublicClient extends WSClientBase {
98
constructor() {
10-
super("wss://api.exchange.cryptomkt.com/api/2/ws/public")
9+
super(
10+
"wss://api.exchange.cryptomkt.com/api/2/ws/public",
11+
{
12+
// tickers
13+
"subscribeTicker":"tickers",
14+
"unsubscribeTicker":"tickers",
15+
"ticker":"tickers",
16+
// orderbook
17+
"subscribeOrderbook":"orderbooks",
18+
"unsubscribeOrderbook":"orderbooks",
19+
"snapshotOrderbook":"orderbooks",
20+
"updateOrderbook":"orderbooks",
21+
// trades
22+
"subscribeTrades":"trades",
23+
"unsubscribeTrades":"trades",
24+
"snapshotTrades":"trades",
25+
"updateTrades":"trades",
26+
// candles
27+
"subscribeCandles":"candles",
28+
"unsubscribeCandles":"candles",
29+
"snapshotCandles":"candles",
30+
"updateCandles":"candles",
31+
}
32+
)
1133
this.obCache = new OrderbookCache()
12-
this.methods = methods
1334
}
1435

1536
handleNotification(notification) {
@@ -34,19 +55,19 @@ class PublicClient extends WSClientBase {
3455
}
3556

3657
isOrderbookFeed(method) {
37-
return this.methods[method] === "orderbooks"
58+
return this.subscriptionKeys[method] === "orderbooks"
3859
}
3960

4061
isTradesFeed(method) {
41-
return this.methods[method] === "trades"
62+
return this.subscriptionKeys[method] === "trades"
4263
}
4364

4465
isCandlesFeed(method) {
45-
return this.methods[method] === "candles"
66+
return this.subscriptionKeys[method] === "candles"
4667
}
4768

4869
buildKey(method, params) {
49-
let methodKey = this.methods[method]
70+
let methodKey = this.subscriptionKeys[method]
5071
let symbol = ('symbol' in params) ? params['symbol'] : ''
5172
let period = ('period' in params) ? params['period'] : ''
5273
let key = methodKey + ':' + symbol + ':' + period

lib/websocket/tradingClient.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ const { AuthClient } = require("./authClient")
55
*/
66
class TradingClient extends AuthClient {
77
constructor(apiKey, apiSecret) {
8-
super("wss://api.exchange.cryptomkt.com/api/2/ws/trading", apiKey, apiSecret)
8+
super(
9+
"wss://api.exchange.cryptomkt.com/api/2/ws/trading",
10+
apiKey,
11+
apiSecret,
12+
{
13+
// reports
14+
"subscribeReports":"reports",
15+
"unsubscribeReports":"reports",
16+
"activeOrders":"reports",
17+
"report":"reports",
18+
}
19+
)
920
}
1021

1122
/**

0 commit comments

Comments
 (0)