Skip to content

Commit 067c217

Browse files
committed
feat: support for muliple websocket responses
1 parent 8fefa3e commit 067c217

File tree

5 files changed

+85
-29
lines changed

5 files changed

+85
-29
lines changed

lib/cryptomarket/websocket/callbackCache.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
require_relative "reusableCallback"
2+
13
module Cryptomarket
24
module Websocket
35
class CallbackCache
46
def initialize()
5-
@callbacks = Hash.new
7+
@reusable_callbacks = Hash.new
68
@next_id = 1
79
end
810

@@ -15,35 +17,37 @@ def get_next_id
1517
return _next_id
1618
end
1719

18-
def store_callback(callback)
20+
def store_callback(callback, call_count=1)
1921
id = get_next_id()
20-
@callbacks[id] = callback
22+
@reusable_callbacks[id] = ReusableCallback.new(callback, call_count)
2123
return id
2224
end
2325

24-
def pop_callback(id)
25-
if not @callbacks.has_key? id
26+
def get_callback(id)
27+
if not @reusable_callbacks.has_key? id
2628
return nil
2729
end
28-
callback = @callbacks[id]
29-
@callbacks.delete(id)
30+
callback, done_using = @reusable_callbacks[id].get_callback()
31+
if done_using
32+
@reusable_callbacks.delete(id)
33+
end
3034
return callback
3135
end
3236

3337
def store_subscription_callback(key, callback)
34-
@callbacks[key] = callback
38+
@reusable_callbacks[key] = callback
3539
end
3640

3741
def get_subscription_callback(key)
38-
if not @callbacks.has_key? key
42+
if not @reusable_callbacks.has_key? key
3943
return nil
4044
end
41-
return @callbacks[key]
45+
return @reusable_callbacks[key]
4246
end
4347

4448
def delete_subscription_callback(key)
45-
if @callbacks.has_key? key
46-
@callbacks.delete(key)
49+
if @reusable_callbacks.has_key? key
50+
@reusable_callbacks.delete(key)
4751
end
4852
end
4953
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Cryptomarket
2+
module Websocket
3+
class ReusableCallback
4+
def initialize(callback, call_count)
5+
@call_count = call_count
6+
@callback = callback
7+
end
8+
9+
def get_callback()
10+
if @call_count < 1
11+
return [nil, false]
12+
end
13+
@call_count -= 1
14+
done_using = @call_count < 1
15+
return [@callback, done_using]
16+
end
17+
end
18+
end
19+
end

lib/cryptomarket/websocket/tradingClient.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ def create_spot_order_list(
204204
callback:nil
205205
)
206206
send_by_id('spot_new_order_list', callback, {
207-
orders:orders,
208-
contingency_type:contingency_type,
209-
order_list_id:order_list_id})
207+
orders:orders,
208+
contingency_type:contingency_type,
209+
order_list_id:order_list_id
210+
},
211+
call_count=orders.count)
210212
end
211213

212214
# cancels a spot order

lib/cryptomarket/websocket/wsClientBase.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ def send_unsubscription(method, callback, params)
8080
store_and_send(method, params, callback)
8181
end
8282

83-
def send_by_id(method, callback, params={})
84-
store_and_send(method, params, callback)
83+
def send_by_id(method, callback, params={}, call_count=1)
84+
store_and_send(method, params, callback, call_count)
8585
end
8686

87-
def store_and_send(method, params, callback_to_store=nil)
87+
def store_and_send(method, params, callback_to_store=nil, call_count=1)
8888
if !params.nil?
8989
params = params.compact
9090
end
9191
payload = {'method' => method, 'params' => params}
9292
if not callback_to_store.nil?
93-
id = @callback_cache.store_callback(callback_to_store)
93+
id = @callback_cache.store_callback(callback_to_store, call_count)
9494
payload['id'] = id
9595
end
9696
@ws_manager.send(payload)
@@ -121,7 +121,7 @@ def handle_response(response)
121121
if id.nil?
122122
return
123123
end
124-
callback = @callback_cache.pop_callback(id)
124+
callback = @callback_cache.get_callback(id)
125125
if callback.nil?
126126
return
127127
end

tests/websocket/trading.rb

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def teardown
1414
sleep(2)
1515
end
1616

17-
def gen_checker_callback(checker)
17+
def gen_list_checker_callback(checker)
1818
msg = ""
1919
callback = Proc.new {|error, result|
2020
if not error.nil?
@@ -29,7 +29,7 @@ def gen_checker_callback(checker)
2929
return {msg:msg, callback:callback}
3030
end
3131

32-
def gen_not_list_checker_callback(checker)
32+
def gen_checker_callback(checker)
3333
msg = ""
3434
callback = Proc.new {|error, result|
3535
if not error.nil?
@@ -43,15 +43,15 @@ def gen_not_list_checker_callback(checker)
4343
end
4444

4545
def test_get_spot_trading_balance
46-
hash = gen_not_list_checker_callback(->(balance) {goodBalance(balance)})
46+
hash = gen_checker_callback(->(balance) {goodBalance(balance)})
4747
@wsclient.get_spot_trading_balance(callback:hash[:callback], currency:"EOS")
4848
sleep(3)
4949
assert(hash[:msg] == "", hash[:msg])
5050
end
5151

5252

5353
def test_get_spot_trading_balances
54-
hash = gen_checker_callback(->(balance) {
54+
hash = gen_list_checker_callback(->(balance) {
5555
puts balance
5656
return goodBalance(balance)
5757
})
@@ -63,7 +63,7 @@ def test_get_spot_trading_balances
6363
def test_order_work_flow
6464
timestamp = Time.now.to_i.to_s
6565
symbol = 'EOSETH'
66-
hash = gen_not_list_checker_callback(->(order) {goodOrder(order)})
66+
hash = gen_checker_callback(->(order) {goodOrder(order)})
6767
@wsclient.create_spot_order(
6868
client_order_id:timestamp,
6969
symbol: symbol,
@@ -74,7 +74,7 @@ def test_order_work_flow
7474
)
7575
sleep(3)
7676
assert(hash[:msg] == "", hash[:msg])
77-
hash2 = gen_not_list_checker_callback(->(result) {
77+
hash2 = gen_checker_callback(->(result) {
7878
for order in result
7979
if order['client_order_id'] == timestamp
8080
return true
@@ -96,7 +96,7 @@ def test_order_work_flow
9696
)
9797
sleep(3)
9898
assert(hash[:msg] == "", hash[:msg])
99-
hash3 = gen_not_list_checker_callback(->(result) {
99+
hash3 = gen_checker_callback(->(result) {
100100
for order in result
101101
if order['client_order_id'] == new_timestamp
102102
return true
@@ -172,16 +172,47 @@ def test_cancel_spot_orders
172172
end
173173

174174
def test_get_spot_commissions
175-
hash = gen_checker_callback(->(commission) {goodTradingCommission(commission)})
175+
hash = gen_list_checker_callback(->(commission) {goodTradingCommission(commission)})
176176
@wsclient.get_spot_commissions(callback:hash[:callback])
177177
sleep(3)
178178
assert(hash[:msg] == "", hash[:msg])
179179
end
180180

181181
def test_get_spot_commission_of_symbol
182-
hash = gen_not_list_checker_callback(->(commission) {goodTradingCommission(commission)})
182+
hash = gen_checker_callback(->(commission) {goodTradingCommission(commission)})
183183
@wsclient.get_spot_commission_of_symbol(symbol:'EOS', callback:hash[:callback])
184184
sleep(3)
185185
assert(hash[:msg] == "", hash[:msg])
186186
end
187+
188+
def test_create_spot_order_list
189+
firstOrderID = Time.now.to_s
190+
@wsclient.create_spot_order_list(
191+
order_list_id: firstOrderID,
192+
contingency_type: Cryptomarket::Args::Contingency::AON,
193+
orders: [
194+
{
195+
'symbol'=>'EOSETH',
196+
'side'=>Cryptomarket::Args::Side::SELL,
197+
'quantity'=>'0.1',
198+
'time_in_force'=> Cryptomarket::Args::TimeInForce::FOK,
199+
'price'=> '1000'
200+
},
201+
{
202+
'symbol'=>'EOSUSDT',
203+
'side'=>Cryptomarket::Args::Side::SELL,
204+
'quantity'=>'0.1',
205+
'time_in_force'=> Cryptomarket::Args::TimeInForce::FOK,
206+
'price'=> '1000'
207+
}
208+
],
209+
callback: Proc.new {|error, result|
210+
if not error.nil?
211+
puts error
212+
return
213+
end
214+
puts result
215+
})
216+
sleep(5)
217+
end
187218
end

0 commit comments

Comments
 (0)