cryptomarket-sdk 1.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,274 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Layout/LineLength
4
+ require_relative 'auth_client'
5
+ require_relative '../constants'
6
+
7
+ module Cryptomarket
8
+ module Websocket
9
+ # TradingClient connects via websocket to cryptomarket to enable the user to manage orders.
10
+ # uses SHA256 as auth method and authenticates automatically after ceonnection.
11
+ class TradingClient < AuthClient
12
+ # Creates a new client
13
+ # ==== Params
14
+ # +string+ +api_key+:: the user api key
15
+ # +string+ +api_secret+:: the user api secret
16
+ # +Integer+ +window+:: Maximum difference between the creation of the request and the moment of request processing in milliseconds. Max is 60_000. Defaul is 10_000
17
+ def initialize(api_key:, api_secret:, window: nil)
18
+ super(
19
+ url: 'wss://api.exchange.cryptomkt.com/api/3/ws/trading',
20
+ api_key: api_key,
21
+ api_secret: api_secret,
22
+ window: window,
23
+ subscription_keys: build_subscription_hash)
24
+ end
25
+
26
+ def build_subscription_hash
27
+ reports = 'reports'
28
+ balances = 'balances'
29
+ { 'spot_subscribe' => [reports, Args::NotificationType::COMMAND],
30
+ 'spot_unsubscribe' => [reports, Args::NotificationType::COMMAND],
31
+ 'spot_orders' => [reports, Args::NotificationType::SNAPSHOT],
32
+ 'spot_order' => [reports, Args::NotificationType::UPDATE],
33
+ 'spot_balance_subscribe' => [balances, Args::NotificationType::COMMAND],
34
+ 'spot_balance_unsubscribe' => [balances, Args::NotificationType::COMMAND],
35
+ 'spot_balance' => [balances, Args::NotificationType::SNAPSHOT] }
36
+ end
37
+
38
+ # subscribe to a feed of execution reports of the user's orders
39
+ #
40
+ # https://api.exchange.cryptomkt.com/#socket-spot-trading
41
+ #
42
+ # ==== Params
43
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of reports, and the type of notification (either 'snapshot' or 'update')
44
+ # +Proc+ +result_callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the subscription
45
+ def subscribe_to_reports(callback:, result_callback: nil)
46
+ interceptor = proc { |notification, type|
47
+ if type == Args::NotificationType::SNAPSHOT
48
+ callback.call(notification, type)
49
+ else
50
+ callback.call([notification], type)
51
+ end
52
+ }
53
+ send_subscription('spot_subscribe', interceptor, {}, result_callback)
54
+ end
55
+
56
+ # stop recieveing the report feed subscription
57
+ #
58
+ # https://api.exchange.cryptomkt.com/#socket-spot-trading
59
+ #
60
+ # ==== Params
61
+ # +Proc+ +callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the unsubscription
62
+ def unsubscribe_to_reports(callback: nil)
63
+ send_unsubscription('spot_unsubscribe', callback, nil)
64
+ end
65
+
66
+ # subscribe to a feed of the user's spot balances
67
+ #
68
+ # only non-zero values are present
69
+ #
70
+ # https://api.exchange.cryptomkt.com/#subscribe-to-spot-balances
71
+ #
72
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of balances, the notification type is always data
73
+ # +Proc+ +result_callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the subscription
74
+ # +Proc+ +String+ +mode+:: Optional. The type of subscription, Either 'updates' or 'batches'. Update messages arrive after an update. Batch messages arrive at equal intervals after a first update
75
+ def subscribe_to_spot_balance(callback:, mode: nil, result_callback: nil)
76
+ interceptor = lambda { |notification, _type|
77
+ callback.call(notification)
78
+ }
79
+ send_subscription('spot_balance_subscribe', interceptor, { mode: mode }, result_callback)
80
+ end
81
+
82
+ # stop recieving the feed of balances changes
83
+ #
84
+ # https://api.exchange.cryptomkt.com/#subscribe-to-wallet-balance
85
+ #
86
+ # ==== Params
87
+ # +Proc+ +callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a boolean value indicating the success of the unsubscription
88
+ def unsubscribe_to_spot_balance(result_callback: nil)
89
+ send_unsubscription(
90
+ 'spot_balance_unsubscribe',
91
+ result_callback,
92
+ { mode: Args::SubscriptionMode::UPDATES }
93
+ )
94
+ end
95
+
96
+ # Get the user's active spot orders
97
+ #
98
+ # https://api.exchange.cryptomkt.com/#get-active-spot-orders
99
+ #
100
+ # ==== Params
101
+ # +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a list of reports for all active spot orders
102
+ def get_active_spot_orders(callback:)
103
+ request('spot_get_orders', callback)
104
+ end
105
+
106
+ # Creates a new spot order
107
+ #
108
+ # For fee, for price accuracy and quantity, and for order status information see the api docs at https://api.exchange.cryptomkt.com/#create-new-spot-order
109
+ #
110
+ # https://api.exchange.cryptomkt.com/#place-new-spot-order
111
+ #
112
+ # ==== Params
113
+ # +String+ +symbol+:: Trading symbol
114
+ # +String+ +side+:: Either 'buy' or 'sell'
115
+ # +String+ +quantity+:: Order quantity
116
+ # +String+ +client_order_id+:: Optional. If given must be unique within the trading day, including all active orders. If not given, is generated by the server
117
+ # +String+ +type+:: Optional. 'limit', 'market', 'stopLimit', 'stopMarket', 'takeProfitLimit' or 'takeProfitMarket'. Default is 'limit'
118
+ # +String+ +time_in_force+:: Optional. 'GTC', 'IOC', 'FOK', 'Day', 'GTD'. Default to 'GTC'
119
+ # +String+ +price+:: Optional. Required for 'limit' and 'stopLimit'. limit price of the order
120
+ # +String+ +stop_price+:: Optional. Required for 'stopLimit' and 'stopMarket' orders. stop price of the order
121
+ # +String+ +expire_time+:: Optional. Required for orders with timeInForce = GDT
122
+ # +Bool+ +strict_validate+:: Optional. If False, the server rounds half down for tickerSize and quantityIncrement. Example of ETHBTC: tickSize = '0.000001', then price '0.046016' is valid, '0.0460165' is invalid
123
+ # +bool+ +post_only+:: Optional. If True, your post_only order causes a match with a pre-existing order as a taker, then the order will be cancelled
124
+ # +String+ +take_rate+:: Optional. Liquidity taker fee, a fraction of order volume, such as 0.001 (for 0.1% fee). Can only increase the fee. Used for fee markup.
125
+ # +String+ +make_rate+:: Optional. Liquidity provider fee, a fraction of order volume, such as 0.001 (for 0.1% fee). Can only increase the fee. Used for fee markup.
126
+ # +Proc+ +callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, the report of the created order
127
+ def create_spot_order( # rubocop:disable Metrics/ParameterLists
128
+ symbol:, side:, quantity:, client_order_id: nil, type: nil, time_in_force: nil, price: nil, stop_price: nil,
129
+ expire_time: nil, strict_validate: nil, post_only: nil, take_rate: nil, make_rate: nil, callback: nil
130
+ )
131
+ request('spot_new_order', callback,
132
+ { client_order_id: client_order_id, symbol: symbol, side: side, quantity: quantity, type: type,
133
+ time_in_force: time_in_force, price: price, stop_price: stop_price, expire_time: expire_time,
134
+ strict_validate: strict_validate, post_only: post_only, take_rate: take_rate, make_rate: make_rate })
135
+ end
136
+
137
+ # creates a list of spot orders
138
+ #
139
+ # = Types or contingency
140
+ # - 'allOrNone' (AON)
141
+ # - 'oneCancelAnother' (OCO)
142
+ # - 'oneTriggerOther' (OTO)
143
+ # - 'oneTriggerOneCancelOther' (OTOCO)
144
+ #
145
+ # = Restriction in the number of orders:
146
+ # - An AON list must have 2 or 3 orders
147
+ # - An OCO list must have 2 or 3 orders
148
+ # - An OTO list must have 2 or 3 orders
149
+ # - An OTOCO must have 3 or 4 orders
150
+ #
151
+ # = Symbol restrictions
152
+ # - For an AON order list, the symbol code of orders must be unique for each order in the list.
153
+ # - For an OCO order list, there are no symbol code restrictions.
154
+ # - For an OTO order list, there are no symbol code restrictions.
155
+ # - For an OTOCO order list, the symbol code of orders must be the same for all orders in the list (placing orders in different order books is not supported).
156
+ #
157
+ # = OrderType restrictions
158
+ # - For an AON order list, orders must be 'limit' or 'market'
159
+ # - For an OCO order list, orders must be 'limit', 'stopLimit', 'stopMarket', takeProfitLimit or takeProfitMarket.
160
+ # - An OCO order list cannot include more than one limit order (the same
161
+ # applies to secondary orders in an OTOCO order list).
162
+ # - For OTO order list, there are no order type restrictions.
163
+ # - For an OTOCO order list, the first order must be 'limit', 'market', 'stopLimit', 'stopMarket', takeProfitLimit or takeProfitMarket.
164
+ # - For an OTOCO order list, the secondary orders have the same restrictions as an OCO order
165
+ # - Default is 'limit'
166
+ #
167
+ # https://api.exchange.cryptomkt.com/#create-new-spot-order-list
168
+ #
169
+ # ==== Params
170
+ # +String+ +order_list_id+:: order list identifier. If ommited, it will be generated by the system. Must be equal to the client order id of the first order in the request
171
+ # +String+ +contingency_type+:: order list type. 'allOrNone', 'oneCancelOther' or 'oneTriggerOneCancelOther'
172
+ # +Array[]+ +orders+:: the list of orders. aech order in the list has the same parameters of a new spot order
173
+ def create_spot_order_list(
174
+ orders:, contingency_type:, order_list_id: nil, callback: nil
175
+ )
176
+ request('spot_new_order_list', callback, {
177
+ orders: orders, contingency_type: contingency_type, order_list_id: order_list_id
178
+ },
179
+ orders.count)
180
+ end
181
+
182
+ # cancels a spot order
183
+ #
184
+ # https://api.exchange.cryptomkt.com/#cancel-spot-order-2
185
+ #
186
+ # ==== Params
187
+ # +String+ +client_order_id+:: the client order id of the order to cancel
188
+ # +Proc+ +callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a list of reports of the canceled orders
189
+ def cancel_spot_order(client_order_id:, callback: nil)
190
+ request('spot_cancel_order', callback, { client_order_id: client_order_id })
191
+ end
192
+
193
+ # cancel all active spot orders and returns the ones that could not be canceled
194
+ #
195
+ # https://api.exchange.cryptomkt.com/#cancel-spot-orders
196
+ #
197
+ # ==== Params
198
+ # +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a report of the canceled order
199
+ def cancel_spot_orders(callback: nil)
200
+ request('spot_cancel_orders', callback)
201
+ end
202
+
203
+ # Get the user's spot trading balance for all currencies with balance
204
+ #
205
+ # Requires the "Orderbook, History, Trading balance" API key Access Right
206
+ #
207
+ # https://api.exchange.cryptomkt.com/#get-spot-trading-balance
208
+ #
209
+ # ==== Params
210
+ # +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a list of the trading balances
211
+ def get_spot_trading_balances(callback:)
212
+ request('spot_balances', callback)
213
+ end
214
+
215
+ # Get the user spot trading balance of a currency
216
+ #
217
+ # Requires the "Orderbook, History, Trading balance" API key Access Right
218
+ #
219
+ # https://api.exchange.cryptomkt.com/#get-spot-trading-balance
220
+ #
221
+ # ==== Params
222
+ # +String+ +currency+:: The currency code to query the balance
223
+ # +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a trading balance
224
+ def get_spot_trading_balance(currency:, callback:)
225
+ request('spot_balance', callback, { currency: currency })
226
+ end
227
+
228
+ # changes the parameters of an existing order, quantity or price
229
+ #
230
+ # https://api.exchange.cryptomkt.com/#cancel-replace-spot-order
231
+ #
232
+ # ==== Params
233
+ # +String+ +client_order_id+:: the client order id of the order to change
234
+ # +String+ +new_client_order_id+:: the new client order id for the modified order. must be unique within the trading day
235
+ # +String+ +quantity+:: new order quantity
236
+ # +String+ +price+:: new order price
237
+ # +Bool+ +strict_validate+:: price and quantity will be checked for the incrementation with tick size and quantity step. See symbol's tick_size and quantity_increment
238
+ # +Proc+ +callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, the new version of the order
239
+ def replace_spot_order( # rubocop:disable Metrics/ParameterLists
240
+ client_order_id:, new_client_order_id:, quantity:, price:, strict_validate: nil, callback: nil
241
+ )
242
+ request('spot_replace_order', callback, {
243
+ client_order_id: client_order_id, new_client_order_id: new_client_order_id, quantity: quantity,
244
+ price: price, strict_validate: strict_validate
245
+ })
246
+ end
247
+
248
+ # Get the personal trading commission rates for all symbols
249
+ #
250
+ # Requires the "Place/cancel orders" API key Access Right
251
+ #
252
+ # https://api.exchange.cryptomkt.com/#get-all-trading-commission
253
+ #
254
+ # ==== Params
255
+ # +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a list of commissions for the user
256
+ def get_spot_commissions(callback:)
257
+ request('spot_fees', callback)
258
+ end
259
+
260
+ # Get the personal trading commission rate of a symbol
261
+ #
262
+ # Requires the "Place/cancel orders" API key Access Right
263
+ #
264
+ # https://api.exchange.cryptomkt.com/#get-trading-commission
265
+ #
266
+ # ==== Params
267
+ # +String+ +symbol+:: The symbol of the commission rate
268
+ # +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a commission for a symbol for the user
269
+ def get_spot_commission(symbol:, callback:)
270
+ request('spot_fee', callback, { symbol: symbol })
271
+ end
272
+ end
273
+ end
274
+ end
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Layout/LineLength
4
+ require_relative 'auth_client'
5
+ require_relative '../constants'
6
+
7
+ module Cryptomarket
8
+ module Websocket
9
+ # WalletClient connects via websocket to cryptomarket to get wallet information of the user.
10
+ # Uses SHA256 as auth method and authenticates automatically.
11
+ class WalletClient < AuthClient
12
+ # Creates a new client and authenticates it to the server
13
+ # ==== Params
14
+ # +String+ +api_key+:: the user api key
15
+ # +String+ +api_secret+:: the user api secret
16
+ # +Integer+ +window+:: Maximum difference between the creation of the request and the moment of request processing in milliseconds. Max is 60_000. Defaul is 10_000
17
+
18
+ def initialize(api_key:, api_secret:, window: nil)
19
+ super(
20
+ url: 'wss://api.exchange.cryptomkt.com/api/3/ws/wallet',
21
+ api_key: api_key,
22
+ api_secret: api_secret,
23
+ window: window,
24
+ subscription_keys: build_subscription_hash)
25
+ end
26
+
27
+ def build_subscription_hash
28
+ transaction = 'transaction'
29
+ balance = 'balance'
30
+ { 'subscribe_transactions' => [transaction, Args::NotificationType::COMMAND],
31
+ 'unsubscribe_transactions' => [transaction, Args::NotificationType::COMMAND],
32
+ 'transaction_update' => [transaction, Args::NotificationType::UPDATE],
33
+
34
+ 'subscribe_wallet_balances' => [balance, Args::NotificationType::COMMAND],
35
+ 'unsubscribe_wallet_balances' => [balance, Args::NotificationType::COMMAND],
36
+ 'wallet_balances' => [balance, Args::NotificationType::SNAPSHOT],
37
+ 'wallet_balance_update' => [balance, Args::NotificationType::UPDATE] }
38
+ end
39
+
40
+ # A transaction notification occurs each time a transaction has been changed, such as creating a transaction, updating the pending state (e.g., the hash assigned) or completing a transaction
41
+ #
42
+ # https://api.exchange.cryptomkt.com/#subscribe-to-transactions
43
+ #
44
+ # ==== Params
45
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of reports, and the type of notification (only 'update')
46
+ # +Proc+ +result_callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the subscription
47
+
48
+ def subscribe_to_transactions(callback:, result_callback: nil)
49
+ interceptor = lambda { |notification, _type|
50
+ callback.call(notification)
51
+ }
52
+ send_subscription('subscribe_transactions', interceptor, nil, result_callback)
53
+ end
54
+
55
+ # stop recieving the feed of transactions changes
56
+ #
57
+ # https://api.exchange.cryptomkt.com/#subscribe-to-transactions
58
+ #
59
+ # ==== Params
60
+ # +Proc+ +callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the unsubscription
61
+
62
+ def unsubscribe_to_transactions(result_callback: nil)
63
+ send_unsubscription('unsubscribe_transactions', result_callback, nil)
64
+ end
65
+
66
+ # subscribe to a feed of the user's wallet balances
67
+ #
68
+ # only non-zero values are present
69
+ #
70
+ # https://api.exchange.cryptomkt.com/#subscribe-to-wallet-balances
71
+ #
72
+ # ==== Params
73
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of balances, and the type of notification (either 'snapshot' or 'update')
74
+ # +Proc+ +result_callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the subscription
75
+
76
+ def subscribe_to_wallet_balance(callback:, result_callback: nil)
77
+ interceptor = proc { |notification, type|
78
+ if type == Args::NotificationType::SNAPSHOT
79
+ callback.call(notification, type)
80
+ else
81
+ callback.call([notification], type)
82
+ end
83
+ }
84
+ send_subscription('subscribe_wallet_balances', interceptor, nil, result_callback)
85
+ end
86
+
87
+ # stop recieving the feed of balances changes
88
+ #
89
+ # https://api.exchange.cryptomkt.com/#subscribe-to-wallet-balances
90
+ #
91
+ # ==== Params
92
+ # +Proc+ +callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the unsubscription
93
+
94
+ def unsubscribe_to_wallet_balance(result_callback: nil)
95
+ send_unsubscription('unsubscribe_wallet_balances', result_callback, nil)
96
+ end
97
+
98
+ # Get the user's wallet balance for all currencies with balance
99
+ #
100
+ # https://api.exchange.cryptomkt.com/#request-wallet-balance
101
+ #
102
+ # ==== Params
103
+ # +Proc+ +callback+:: A +Proc+ called with a list of the user balances
104
+
105
+ def get_wallet_balances(callback:)
106
+ request('wallet_balances', callback)
107
+ end
108
+
109
+ # Get the user's wallet balance of a currency
110
+ #
111
+ # Requires the "Payment information" API key Access Right
112
+ #
113
+ # https://api.exchange.cryptomkt.com/#request-wallet-balance
114
+ #
115
+ # ==== Params
116
+ # +String+ +currency+:: The currency code to query the balance
117
+ # +Proc+ +callback+:: A +Proc+ called with a user balance
118
+
119
+ def get_wallet_balance(currency:, callback:)
120
+ interceptor = lambda { |err, balance|
121
+ unless err.nil?
122
+ callback.call(err, nil)
123
+ return
124
+ end
125
+ balance['currency'] = currency
126
+ callback.call(err, balance)
127
+ }
128
+ request('wallet_balance', interceptor, { currency: currency })
129
+ end
130
+
131
+ # Get the transaction history of the account
132
+ # Important:
133
+ # - The list of supported transaction types may be expanded in future versions
134
+ # - Some transaction subtypes are reserved for future use and do not purport to provide any functionality on the platform
135
+ # - The list of supported transaction subtypes may be expanded in future versions
136
+ #
137
+ # Requires the "Payment information" API key Access Right
138
+ #
139
+ # https://api.exchange.cryptomkt.com/#get-transactions
140
+ #
141
+ # ==== Params
142
+ # +Proc+ +callback+:: A +Proc+ called with a list of transactions
143
+ # +Array[String]+ +tx_ids+:: Optional. List of transaction identifiers to query
144
+ # +Array[String]+ +types+:: Optional. List of types to query. valid types are: 'DEPOSIT', 'WITHDRAW', 'TRANSFER' and 'SWAP'
145
+ # +Array[String]+ +subtyes+:: Optional. List of subtypes to query. valid subtypes are: 'UNCLASSIFIED', 'BLOCKCHAIN', 'AIRDROP', 'AFFILIATE', 'STAKING', 'BUY_CRYPTO', 'OFFCHAIN', 'FIAT', 'SUB_ACCOUNT', 'WALLET_TO_SPOT', 'SPOT_TO_WALLET', 'WALLET_TO_DERIVATIVES', 'DERIVATIVES_TO_WALLET', 'CHAIN_SWITCH_FROM', 'CHAIN_SWITCH_TO' and 'INSTANT_EXCHANGE'
146
+ # +Array[String]+ +statuses+:: Optional. List of statuses to query. valid subtypes are: 'CREATED', 'PENDING', 'FAILED', 'SUCCESS' and 'ROLLED_BACK'
147
+ # +Array[String] +currencies+:: Optional. List of currencies ids.
148
+ # +String+ +from+:: Optional. Interval initial value when ordering by 'created_at'. As Datetime
149
+ # +String+ +till+:: Optional. Interval end value when ordering by 'created_at'. As Datetime
150
+ # +String+ +id_from+:: Optional. Interval initial value when ordering by id. Min is 0
151
+ # +String+ +id_till+:: Optional. Interval end value when ordering by id. Min is 0
152
+ # +String+ +order_by+:: Optional. sorting parameter.'created_at' or 'id'. Default is 'created_at'
153
+ # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
154
+ # +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1000
155
+ # +Integer+ +offset+:: Optional. Default is 0. Max is 100000
156
+ # +bool+ +group_transactions+:: Optional. Flag indicating whether the returned transactions will be parts of a single operation. Default is false
157
+
158
+ def get_transactions( # rubocop:disable Metrics/ParameterLists
159
+ callback:, tx_ids: nil, types: nil, subtypes: nil, statuses: nil, currencies: nil, from: nil, till: nil,
160
+ id_from: nil, id_till: nil, order_by: nil, sort: nil, limit: nil, offset: nil, group_transactions: nil
161
+ )
162
+ request('get_transactions', callback, {
163
+ tx_ids: tx_ids, types: types, subtypes: subtypes, statuses: statuses, currencies: currencies,
164
+ from: from, till: till, id_from: id_from, id_till: id_till, order_by: order_by, sort: sort,
165
+ limit: limit, offset: offset, group_transactions: group_transactions
166
+ })
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'faye/websocket'
5
+ require 'eventmachine'
6
+
7
+ require_relative '../exceptions'
8
+
9
+ module Cryptomarket
10
+ module Websocket
11
+ # websocket connection manager.
12
+ class WSManager
13
+ def initialize(handler, url:)
14
+ @url = url
15
+ @handler = handler
16
+ @connected = false
17
+ end
18
+
19
+ def connected?
20
+ @connected
21
+ end
22
+
23
+ def connect
24
+ @thread = Thread.new do
25
+ EM.run do
26
+ @ws = Faye::WebSocket::Client.new(@url)
27
+ @ws.onopen = method(:_on_open)
28
+ @ws.onclose = method(:_on_close)
29
+ @ws.onerror = method(:_on_error)
30
+ @ws.onmessage = method(:_on_message)
31
+ end
32
+ end
33
+ end
34
+
35
+ def _on_open(_open_event)
36
+ @connected = true
37
+ @handler.on_open
38
+ end
39
+
40
+ def _on_close(_close_event)
41
+ @handler.on_close
42
+ EM.stop
43
+ end
44
+
45
+ def _on_error(error)
46
+ @handler.on_error(error)
47
+ end
48
+
49
+ def _on_message(message)
50
+ @handler.handle(JSON.parse(message.data.to_s))
51
+ end
52
+
53
+ def close
54
+ @ws.close
55
+ @connected = false
56
+ end
57
+
58
+ def send(hash)
59
+ raise Cryptomarket::SDKException.new, 'connection closed' unless @connected
60
+
61
+ @ws.send hash.to_json
62
+ end
63
+ end
64
+ end
65
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryptomarket-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - T. Ismael Verdugo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-27 00:00:00.000000000 Z
11
+ date: 2024-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -16,28 +16,84 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.1'
19
+ version: 2.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.1'
26
+ version: 2.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: faye-websocket
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.11.0
33
+ version: 0.11.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.11.0
40
+ version: 0.11.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: eventmachine
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: openssl
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.60.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.60.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.6.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.6.2
41
97
  description: Cryptomarket sdk for rest connection and websocket connection for the
42
98
  ruby language
43
99
  email:
@@ -49,21 +105,24 @@ extra_rdoc_files:
49
105
  files:
50
106
  - LICENSE.md
51
107
  - README.md
52
- - lib/cryptomarket/HttpManager.rb
53
108
  - lib/cryptomarket/client.rb
109
+ - lib/cryptomarket/constants.rb
110
+ - lib/cryptomarket/credentials_factory.rb
54
111
  - lib/cryptomarket/exceptions.rb
55
- - lib/cryptomarket/utils.rb
56
- - lib/cryptomarket/websocket/accountClient.rb
57
- - lib/cryptomarket/websocket/authClient.rb
58
- - lib/cryptomarket/websocket/callbackCache.rb
112
+ - lib/cryptomarket/http_manager.rb
113
+ - lib/cryptomarket/websocket/auth_client.rb
114
+ - lib/cryptomarket/websocket/callback_cache.rb
115
+ - lib/cryptomarket/websocket/client_base.rb
116
+ - lib/cryptomarket/websocket/market_data_client.rb
117
+ - lib/cryptomarket/websocket/market_data_client_core.rb
59
118
  - lib/cryptomarket/websocket/methods.rb
60
- - lib/cryptomarket/websocket/orderbookCache.rb
61
- - lib/cryptomarket/websocket/publicClient.rb
62
- - lib/cryptomarket/websocket/tradingClient.rb
63
- - lib/cryptomarket/websocket/wsClientBase.rb
64
- - lib/cryptomarket/websocket/wsManager.rb
119
+ - lib/cryptomarket/websocket/reusable_callback.rb
120
+ - lib/cryptomarket/websocket/trading_client.rb
121
+ - lib/cryptomarket/websocket/wallet_client.rb
122
+ - lib/cryptomarket/websocket/ws_manager.rb
65
123
  homepage: https://github.com/cryptomkt/cryptomkt-ruby
66
- licenses: []
124
+ licenses:
125
+ - Apache-2.0
67
126
  metadata: {}
68
127
  post_install_message:
69
128
  rdoc_options:
@@ -84,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
143
  - !ruby/object:Gem::Version
85
144
  version: '0'
86
145
  requirements: []
87
- rubygems_version: 3.1.2
146
+ rubygems_version: 3.0.3.1
88
147
  signing_key:
89
148
  specification_version: 4
90
149
  summary: Cryptomarket sdk for ruby