Skip to content

Commit fcdb9ab

Browse files
committed
add balance subscription
redo subscription logic
1 parent 98e4ca4 commit fcdb9ab

File tree

6 files changed

+123
-14
lines changed

6 files changed

+123
-14
lines changed

lib/cryptomarket/websocket/accountClient.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Cryptomarket
55
module Websocket
6-
6+
77
# AccountClient connects via websocket to cryptomarket to get account information of the user. uses SHA256 as auth method and authenticates automatically.
88
#
99
# +string+ +apiKey+:: the user api key
@@ -12,9 +12,24 @@ module Websocket
1212

1313
class AccountClient < AuthClient
1414
include Utils
15+
1516
# Creates a new client and authenticates it to the server
1617
def initialize(apiKey:, apiSecret:)
17-
super(url:"wss://api.exchange.cryptomkt.com/api/2/ws/account", apiKey:apiKey, apiSecret:apiSecret)
18+
transaction = "transaction"
19+
balance = "balance"
20+
super(
21+
url:"wss://api.exchange.cryptomkt.com/api/2/ws/account",
22+
apiKey:apiKey,
23+
apiSecret:apiSecret,
24+
subscriptionKeys:{
25+
"unsubscribeTransactions" => transaction,
26+
"subscribeTransactions" => transaction,
27+
"updateTransaction" => transaction,
28+
29+
"unsubscribeBalance" => balance,
30+
"subscribeBalance" => balance,
31+
"balance" => balance,
32+
})
1833
end
1934

2035
# get the account balance as a list of balances. non-zero balances only
@@ -105,6 +120,31 @@ def subscribeToTransactions(callback, resultCallback:nil)
105120
def unsubscribeToTransactions(callback:nil)
106121
sendUnsubscription('unsubscribeTransactions', callback, {})
107122
end
123+
124+
# subscribes to a feed of balances
125+
#
126+
# This subscription aims to provide an easy way to be informed of the current balance state.
127+
# If the state has been changed or potentially changed the "balance" event will come with the actual state.
128+
# Please be aware that only non-zero values present.
129+
#
130+
# https://api.exchange.cryptomkt.com/#subscription-to-the-balance
131+
#
132+
# +Proc+ +callback+:: A +Proc+ to call with the result data. It takes one argument. a feed of balances
133+
# +Proc+ +resultCallback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
134+
135+
def subscribeToBalance(callback, resultCallback:nil)
136+
sendSubscription('subscribeBalance', callback, {}, resultCallback)
137+
end
138+
139+
# unsubscribe to the balance feed.
140+
#
141+
# https://api.exchange.cryptomkt.com/#subscription-to-the-balance
142+
#
143+
# +Proc+ +callback+:: Optional. A +Proc+ to call with the result data. It takes two arguments, err and result. err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...}
144+
145+
def unsubscribeToBalance(callback:nil)
146+
sendUnsubscription('unsubscribeBalance', callback, {})
147+
end
108148
end
109149
end
110150
end

lib/cryptomarket/websocket/authClient.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ module Cryptomarket
55
module Websocket
66
class AuthClient < ClientBase
77
# Creates a new client
8-
def initialize(url:, apiKey:, apiSecret:)
8+
def initialize(url:, apiKey:, apiSecret:, subscriptionKeys:)
99
@apiKey = apiKey
1010
@apiSecret = apiSecret
11-
super url:url
11+
super url:url, subscriptionKeys:subscriptionKeys
1212
@authed = false
1313
end
1414

lib/cryptomarket/websocket/publicClient.rb

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require_relative "wsClientBase"
44
require_relative"../utils"
5-
require_relative "methods"
65

76

87

@@ -15,11 +14,35 @@ module Websocket
1514

1615
class PublicClient < ClientBase
1716
include Utils
18-
include Methods
1917

2018
def initialize()
19+
orderbook = "orderbook"
20+
tickers = "tickers"
21+
trades = "trades"
22+
candles = "candles"
23+
super(
24+
url:"wss://api.exchange.cryptomkt.com/api/2/ws/public",
25+
subscriptionKeys:{
26+
"subscribeTicker" => tickers,
27+
"unsubscribeTicker" => tickers,
28+
"ticker" => tickers,
29+
30+
"subscribeOrderbook" => orderbook,
31+
"unsubscribeOrderbook" => orderbook,
32+
"snapshotOrderbook" => orderbook,
33+
"updateOrderbook" => orderbook,
34+
35+
"subscribeTrades" => trades,
36+
"unsubscribeTrades" => trades,
37+
"snapshotTrades" => trades,
38+
"updateTrades" => trades,
39+
40+
"subscribeCandles" => candles,
41+
"unsubscribeCandles" => candles,
42+
"snapshotCandles" => candles,
43+
"updateCandles" => candles
44+
})
2145
@OBCache = OrderbookCache.new
22-
super url:"wss://api.exchange.cryptomkt.com/api/2/ws/public"
2346
end
2447

2548
def handleNotification(notification)
@@ -49,7 +72,7 @@ def handleNotification(notification)
4972
end
5073

5174
def buildKey(method, params)
52-
methodKey = mapping(method)
75+
methodKey = @subscriptionKeys[method]
5376

5477
symbol = ''
5578
if params.has_key? 'symbol'
@@ -63,6 +86,19 @@ def buildKey(method, params)
6386
return key.upcase
6487
end
6588

89+
def orderbookFeed(method)
90+
return @subscriptionKeys[method] == "orderbook"
91+
end
92+
93+
def tradesFeed(method)
94+
return @subscriptionKeys[method] == "trades"
95+
end
96+
97+
def candlesFeed(method)
98+
return @subscriptionKeys[method] == "candles"
99+
end
100+
101+
66102
# Get a list all available currencies on the exchange
67103
#
68104
# https://api.exchange.cryptomkt.com/#get-currencies

lib/cryptomarket/websocket/tradingClient.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ module Websocket
1313
class TradingClient < AuthClient
1414
include Utils
1515
# Creates a new client
16+
1617
def initialize(apiKey:, apiSecret:)
17-
super(url:"wss://api.exchange.cryptomkt.com/api/2/ws/trading", apiKey:apiKey, apiSecret:apiSecret)
18+
reports = "reports"
19+
super(
20+
url:"wss://api.exchange.cryptomkt.com/api/2/ws/trading",
21+
apiKey:apiKey,
22+
apiSecret:apiSecret,
23+
subscriptionKeys:{
24+
"subscribeReports" => reports,
25+
"unsubscribeReports" => reports,
26+
"activeOrders" => reports,
27+
"report" => reports,
28+
})
1829
end
1930

2031
# Subscribe to a feed of trading events of the account

lib/cryptomarket/websocket/wsClientBase.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
require_relative "callbackCache"
2-
require_relative "methods"
32
require_relative "orderbookCache"
43
require_relative "wsManager"
54
require_relative '../exceptions'
65

76
module Cryptomarket
87
module Websocket
98
class ClientBase
10-
include Methods
11-
12-
def initialize(url:)
9+
def initialize(url:, subscriptionKeys:)
10+
@subscriptionKeys = subscriptionKeys
1311
@callbackCache = CallbackCache.new
1412
@wsmanager = WSManager.new self, url:url
1513
@onconnect = ->{}
@@ -98,7 +96,7 @@ def handle(message)
9896
end
9997

10098
def handleNotification(notification)
101-
key = "subscription"
99+
key = buildKey(notification["method"])
102100
callback = @callbackCache.getSubscriptionCallback(key)
103101
if callback.nil?
104102
return
@@ -107,6 +105,9 @@ def handleNotification(notification)
107105
end
108106

109107
def buildKey(method=nil, params=nil)
108+
if @subscriptionKeys.key? method
109+
return @subscriptionKeys[method]
110+
end
110111
return "subscription"
111112
end
112113

tests/websocket/accountSubs.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def teardown
1919
def test_transaction_subscription
2020
msg = ""
2121
callback = Proc.new {|feed|
22+
print feed
2223
if not goodTransaction(feed)
2324
msg = "bad transaction"
2425
return
@@ -29,8 +30,28 @@ def test_transaction_subscription
2930
sleep(3)
3031
@restclient.transferMoneyFromExchangeToBank("EOS", "0.1")
3132
sleep(3)
33+
@wsclient.subscribeToBalance(Proc.new {|feed|
34+
print feed
35+
})
36+
sleep(3)
3237
@restclient.transferMoneyFromBankToExchange("EOS", "0.1")
3338
sleep(3)
39+
@wsclient.unsubscribeToTransactions()
3440
assert(msg == "", msg)
3541
end
42+
43+
def test_balance_subscription
44+
msg = ""
45+
callback = Proc.new {|feed|
46+
print feed
47+
# if not goodBalances(feed)
48+
# msg = "bad balances"
49+
# return
50+
# print feed
51+
# end
52+
}
53+
@wsclient.subscribeToBalance(callback)
54+
sleep(3)
55+
@wsclient.unsubscribeToBalance()
56+
end
3657
end

0 commit comments

Comments
 (0)