cryptomarket-sdk 1.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,279 @@
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
+ alias get_spot_trading_balance_of_currency get_spot_trading_balance
39
+ alias get_spot_trading_balance_by_currency get_spot_trading_balance
40
+ alias get_spot_commission_of_symbol get_spot_commission
41
+ alias get_spot_commission_by_symbol get_spot_commission
42
+
43
+ # subscribe to a feed of execution reports of the user's orders
44
+ #
45
+ # https://api.exchange.cryptomkt.com/#socket-spot-trading
46
+ #
47
+ # ==== Params
48
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of reports, and the type of notification (either 'snapshot' or 'update')
49
+ # +Proc+ +result_callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the subscription
50
+ def subscribe_to_reports(callback:, result_callback: nil)
51
+ interceptor = proc { |notification, type|
52
+ if type == Args::NotificationType::SNAPSHOT
53
+ callback.call(notification, type)
54
+ else
55
+ callback.call([notification], type)
56
+ end
57
+ }
58
+ send_subscription('spot_subscribe', interceptor, {}, result_callback)
59
+ end
60
+
61
+ # stop recieveing the report feed subscription
62
+ #
63
+ # https://api.exchange.cryptomkt.com/#socket-spot-trading
64
+ #
65
+ # ==== Params
66
+ # +Proc+ +callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the unsubscription
67
+ def unsubscribe_to_reports(callback: nil)
68
+ send_unsubscription('spot_unsubscribe', callback, nil)
69
+ end
70
+
71
+ # subscribe to a feed of the user's spot balances
72
+ #
73
+ # only non-zero values are present
74
+ #
75
+ # https://api.exchange.cryptomkt.com/#subscribe-to-spot-balances
76
+ #
77
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of balances, the notification type is always data
78
+ # +Proc+ +result_callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the subscription
79
+ # +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
80
+ def subscribe_to_spot_balance(callback:, mode: nil, result_callback: nil)
81
+ interceptor = lambda { |notification, _type|
82
+ callback.call(notification)
83
+ }
84
+ send_subscription('spot_balance_subscribe', interceptor, { mode: mode }, result_callback)
85
+ end
86
+
87
+ # stop recieving the feed of balances changes
88
+ #
89
+ # https://api.exchange.cryptomkt.com/#subscribe-to-wallet-balance
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
+ def unsubscribe_to_spot_balance(result_callback: nil)
94
+ send_unsubscription(
95
+ 'spot_balance_unsubscribe',
96
+ result_callback,
97
+ { mode: Args::SubscriptionMode::UPDATES }
98
+ )
99
+ end
100
+
101
+ # Get the user's active spot orders
102
+ #
103
+ # https://api.exchange.cryptomkt.com/#get-active-spot-orders
104
+ #
105
+ # ==== Params
106
+ # +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
107
+ def get_active_spot_orders(callback:)
108
+ request('spot_get_orders', callback)
109
+ end
110
+
111
+ # Creates a new spot order
112
+ #
113
+ # 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
114
+ #
115
+ # https://api.exchange.cryptomkt.com/#place-new-spot-order
116
+ #
117
+ # ==== Params
118
+ # +String+ +symbol+:: Trading symbol
119
+ # +String+ +side+:: Either 'buy' or 'sell'
120
+ # +String+ +quantity+:: Order quantity
121
+ # +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
122
+ # +String+ +type+:: Optional. 'limit', 'market', 'stopLimit', 'stopMarket', 'takeProfitLimit' or 'takeProfitMarket'. Default is 'limit'
123
+ # +String+ +time_in_force+:: Optional. 'GTC', 'IOC', 'FOK', 'Day', 'GTD'. Default to 'GTC'
124
+ # +String+ +price+:: Optional. Required for 'limit' and 'stopLimit'. limit price of the order
125
+ # +String+ +stop_price+:: Optional. Required for 'stopLimit' and 'stopMarket' orders. stop price of the order
126
+ # +String+ +expire_time+:: Optional. Required for orders with timeInForce = GDT
127
+ # +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
128
+ # +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
129
+ # +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.
130
+ # +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.
131
+ # +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
132
+ def create_spot_order( # rubocop:disable Metrics/ParameterLists
133
+ symbol:, side:, quantity:, client_order_id: nil, type: nil, time_in_force: nil, price: nil, stop_price: nil,
134
+ expire_time: nil, strict_validate: nil, post_only: nil, take_rate: nil, make_rate: nil, callback: nil
135
+ )
136
+ request('spot_new_order', callback,
137
+ { client_order_id: client_order_id, symbol: symbol, side: side, quantity: quantity, type: type,
138
+ time_in_force: time_in_force, price: price, stop_price: stop_price, expire_time: expire_time,
139
+ strict_validate: strict_validate, post_only: post_only, take_rate: take_rate, make_rate: make_rate })
140
+ end
141
+
142
+ # creates a list of spot orders
143
+ #
144
+ # = Types or contingency
145
+ # - 'allOrNone' (AON)
146
+ # - 'oneCancelAnother' (OCO)
147
+ # - 'oneTriggerOther' (OTO)
148
+ # - 'oneTriggerOneCancelOther' (OTOCO)
149
+ #
150
+ # = Restriction in the number of orders:
151
+ # - An AON list must have 2 or 3 orders
152
+ # - An OCO list must have 2 or 3 orders
153
+ # - An OTO list must have 2 or 3 orders
154
+ # - An OTOCO must have 3 or 4 orders
155
+ #
156
+ # = Symbol restrictions
157
+ # - For an AON order list, the symbol code of orders must be unique for each order in the list.
158
+ # - For an OCO order list, there are no symbol code restrictions.
159
+ # - For an OTO order list, there are no symbol code restrictions.
160
+ # - 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).
161
+ #
162
+ # = OrderType restrictions
163
+ # - For an AON order list, orders must be 'limit' or 'market'
164
+ # - For an OCO order list, orders must be 'limit', 'stopLimit', 'stopMarket', takeProfitLimit or takeProfitMarket.
165
+ # - An OCO order list cannot include more than one limit order (the same
166
+ # applies to secondary orders in an OTOCO order list).
167
+ # - For OTO order list, there are no order type restrictions.
168
+ # - For an OTOCO order list, the first order must be 'limit', 'market', 'stopLimit', 'stopMarket', takeProfitLimit or takeProfitMarket.
169
+ # - For an OTOCO order list, the secondary orders have the same restrictions as an OCO order
170
+ # - Default is 'limit'
171
+ #
172
+ # https://api.exchange.cryptomkt.com/#create-new-spot-order-list
173
+ #
174
+ # ==== Params
175
+ # +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
176
+ # +String+ +contingency_type+:: order list type. 'allOrNone', 'oneCancelOther' or 'oneTriggerOneCancelOther'
177
+ # +Array[]+ +orders+:: the list of orders. aech order in the list has the same parameters of a new spot order
178
+ def create_spot_order_list(
179
+ orders:, contingency_type:, order_list_id: nil, callback: nil
180
+ )
181
+ request('spot_new_order_list', callback, {
182
+ orders: orders, contingency_type: contingency_type, order_list_id: order_list_id
183
+ },
184
+ orders.count)
185
+ end
186
+
187
+ # cancels a spot order
188
+ #
189
+ # https://api.exchange.cryptomkt.com/#cancel-spot-order-2
190
+ #
191
+ # ==== Params
192
+ # +String+ +client_order_id+:: the client order id of the order to cancel
193
+ # +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
194
+ def cancel_spot_order(client_order_id:, callback: nil)
195
+ request('spot_cancel_order', callback, { client_order_id: client_order_id })
196
+ end
197
+
198
+ # cancel all active spot orders and returns the ones that could not be canceled
199
+ #
200
+ # https://api.exchange.cryptomkt.com/#cancel-spot-orders
201
+ #
202
+ # ==== Params
203
+ # +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
204
+ def cancel_spot_orders(callback: nil)
205
+ request('spot_cancel_orders', callback)
206
+ end
207
+
208
+ # Get the user's spot trading balance for all currencies with balance
209
+ #
210
+ # Requires the "Orderbook, History, Trading balance" API key Access Right
211
+ #
212
+ # https://api.exchange.cryptomkt.com/#get-spot-trading-balance
213
+ #
214
+ # ==== Params
215
+ # +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
216
+ def get_spot_trading_balances(callback:)
217
+ request('spot_balances', callback)
218
+ end
219
+
220
+ # Get the user spot trading balance of a currency
221
+ #
222
+ # Requires the "Orderbook, History, Trading balance" API key Access Right
223
+ #
224
+ # https://api.exchange.cryptomkt.com/#get-spot-trading-balance
225
+ #
226
+ # ==== Params
227
+ # +String+ +currency+:: The currency code to query the balance
228
+ # +Proc+ +callback+:: A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a trading balance
229
+ def get_spot_trading_balance(currency:, callback:)
230
+ request('spot_balance', callback, { currency: currency })
231
+ end
232
+
233
+ # changes the parameters of an existing order, quantity or price
234
+ #
235
+ # https://api.exchange.cryptomkt.com/#cancel-replace-spot-order
236
+ #
237
+ # ==== Params
238
+ # +String+ +client_order_id+:: the client order id of the order to change
239
+ # +String+ +new_client_order_id+:: the new client order id for the modified order. must be unique within the trading day
240
+ # +String+ +quantity+:: new order quantity
241
+ # +String+ +price+:: new order price
242
+ # +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
243
+ # +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
244
+ def replace_spot_order( # rubocop:disable Metrics/ParameterLists
245
+ client_order_id:, new_client_order_id:, quantity:, price:, strict_validate: nil, callback: nil
246
+ )
247
+ request('spot_replace_order', callback, {
248
+ client_order_id: client_order_id, new_client_order_id: new_client_order_id, quantity: quantity,
249
+ price: price, strict_validate: strict_validate
250
+ })
251
+ end
252
+
253
+ # Get the personal trading commission rates for all symbols
254
+ #
255
+ # Requires the "Place/cancel orders" API key Access Right
256
+ #
257
+ # https://api.exchange.cryptomkt.com/#get-all-trading-commission
258
+ #
259
+ # ==== Params
260
+ # +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
261
+ def get_spot_commissions(callback:)
262
+ request('spot_fees', callback)
263
+ end
264
+
265
+ # Get the personal trading commission rate of a symbol
266
+ #
267
+ # Requires the "Place/cancel orders" API key Access Right
268
+ #
269
+ # https://api.exchange.cryptomkt.com/#get-trading-commission
270
+ #
271
+ # ==== Params
272
+ # +String+ +symbol+:: The symbol of the commission rate
273
+ # +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
274
+ def get_spot_commission(symbol:, callback:)
275
+ request('spot_fee', callback, { symbol: symbol })
276
+ end
277
+ end
278
+ end
279
+ end
@@ -0,0 +1,173 @@
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
+ alias get_wallet_balance_of_currency get_wallet_balance
41
+ alias get_wallet_balance_by_currency get_wallet_balance
42
+
43
+ # 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
44
+ #
45
+ # https://api.exchange.cryptomkt.com/#subscribe-to-transactions
46
+ #
47
+ # ==== Params
48
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of reports, and the type of notification (only 'update')
49
+ # +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
50
+
51
+ def subscribe_to_transactions(callback:, result_callback: nil)
52
+ interceptor = lambda { |notification, _type|
53
+ callback.call(notification)
54
+ }
55
+ send_subscription('subscribe_transactions', interceptor, nil, result_callback)
56
+ end
57
+
58
+ # stop recieving the feed of transactions changes
59
+ #
60
+ # https://api.exchange.cryptomkt.com/#subscribe-to-transactions
61
+ #
62
+ # ==== Params
63
+ # +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
64
+
65
+ def unsubscribe_to_transactions(result_callback: nil)
66
+ send_unsubscription('unsubscribe_transactions', result_callback, nil)
67
+ end
68
+
69
+ # subscribe to a feed of the user's wallet balances
70
+ #
71
+ # only non-zero values are present
72
+ #
73
+ # https://api.exchange.cryptomkt.com/#subscribe-to-wallet-balances
74
+ #
75
+ # ==== Params
76
+ # +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of balances, and the type of notification (either 'snapshot' or 'update')
77
+ # +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
78
+
79
+ def subscribe_to_wallet_balance(callback:, result_callback: nil)
80
+ interceptor = proc { |notification, type|
81
+ if type == Args::NotificationType::SNAPSHOT
82
+ callback.call(notification, type)
83
+ else
84
+ callback.call([notification], type)
85
+ end
86
+ }
87
+ send_subscription('subscribe_wallet_balances', interceptor, nil, result_callback)
88
+ end
89
+
90
+ # stop recieving the feed of balances changes
91
+ #
92
+ # https://api.exchange.cryptomkt.com/#subscribe-to-wallet-balances
93
+ #
94
+ # ==== Params
95
+ # +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
96
+
97
+ def unsubscribe_to_wallet_balance(result_callback: nil)
98
+ send_unsubscription('unsubscribe_wallet_balances', result_callback, nil)
99
+ end
100
+
101
+ # Get the user's wallet balance for all currencies with balance
102
+ #
103
+ # https://api.exchange.cryptomkt.com/#request-wallet-balance
104
+ #
105
+ # ==== Params
106
+ # +Proc+ +callback+:: A +Proc+ called with a list of the user balances
107
+
108
+ def get_wallet_balances(callback:)
109
+ request('wallet_balances', callback)
110
+ end
111
+
112
+ # Get the user's wallet balance of a currency
113
+ #
114
+ # Requires the "Payment information" API key Access Right
115
+ #
116
+ # https://api.exchange.cryptomkt.com/#request-wallet-balance
117
+ #
118
+ # ==== Params
119
+ # +String+ +currency+:: The currency code to query the balance
120
+ # +Proc+ +callback+:: A +Proc+ called with a user balance
121
+
122
+ def get_wallet_balance(currency:, callback:)
123
+ interceptor = lambda { |err, balance|
124
+ unless err.nil?
125
+ callback.call(err, nil)
126
+ return
127
+ end
128
+ balance['currency'] = currency
129
+ callback.call(err, balance)
130
+ }
131
+ request('wallet_balance', interceptor, { currency: currency })
132
+ end
133
+
134
+ # Get the transaction history of the account
135
+ # Important:
136
+ # - The list of supported transaction types may be expanded in future versions
137
+ # - Some transaction subtypes are reserved for future use and do not purport to provide any functionality on the platform
138
+ # - The list of supported transaction subtypes may be expanded in future versions
139
+ #
140
+ # Requires the "Payment information" API key Access Right
141
+ #
142
+ # https://api.exchange.cryptomkt.com/#get-transactions
143
+ #
144
+ # ==== Params
145
+ # +Proc+ +callback+:: A +Proc+ called with a list of transactions
146
+ # +Array[String]+ +tx_ids+:: Optional. List of transaction identifiers to query
147
+ # +Array[String]+ +types+:: Optional. List of types to query. valid types are: 'DEPOSIT', 'WITHDRAW', 'TRANSFER' and 'SWAP'
148
+ # +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'
149
+ # +Array[String]+ +statuses+:: Optional. List of statuses to query. valid subtypes are: 'CREATED', 'PENDING', 'FAILED', 'SUCCESS' and 'ROLLED_BACK'
150
+ # +Array[String] +currencies+:: Optional. List of currencies ids.
151
+ # +String+ +from+:: Optional. Interval initial value when ordering by 'created_at'. As Datetime
152
+ # +String+ +till+:: Optional. Interval end value when ordering by 'created_at'. As Datetime
153
+ # +String+ +id_from+:: Optional. Interval initial value when ordering by id. Min is 0
154
+ # +String+ +id_till+:: Optional. Interval end value when ordering by id. Min is 0
155
+ # +String+ +order_by+:: Optional. sorting parameter.'created_at' or 'id'. Default is 'created_at'
156
+ # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
157
+ # +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1_000
158
+ # +Integer+ +offset+:: Optional. Default is 0. Max is 100_000
159
+ # +bool+ +group_transactions+:: Optional. Flag indicating whether the returned transactions will be parts of a single operation. Default is false
160
+
161
+ def get_transactions( # rubocop:disable Metrics/ParameterLists
162
+ callback:, tx_ids: nil, types: nil, subtypes: nil, statuses: nil, currencies: nil, from: nil, till: nil,
163
+ id_from: nil, id_till: nil, order_by: nil, sort: nil, limit: nil, offset: nil, group_transactions: nil
164
+ )
165
+ request('get_transactions', callback, {
166
+ tx_ids: tx_ids, types: types, subtypes: subtypes, statuses: statuses, currencies: currencies,
167
+ from: from, till: till, id_from: id_from, id_till: id_till, order_by: order_by, sort: sort,
168
+ limit: limit, offset: offset, group_transactions: group_transactions
169
+ })
170
+ end
171
+ end
172
+ end
173
+ 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.1.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-03-13 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