Skip to content

Commit b1c72c3

Browse files
committed
test: separates withdraw crypto tests
1 parent 0387fc4 commit b1c72c3

File tree

10 files changed

+92
-75
lines changed

10 files changed

+92
-75
lines changed

lib/cryptomarket/client.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def get_active_spot_order(client_order_id:)
394394
# +String+ +quantity+:: Order quantity
395395
# +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
396396
# +String+ +type+:: Optional. 'limit', 'market', 'stopLimit', 'stopMarket', 'takeProfitLimit' or 'takeProfitMarket'. Default is 'limit'
397-
# +String+ +time_in_force+:: Optional. 'GTC', 'IOC', 'FOK', 'Day', 'GTD'. Default to 'GTC'
397+
# +String+ +time_in_force+:: Optional. 'GTC', 'IOC', 'FOK', 'Day', 'GTD'. Default is 'GTC' if 'limit', 'stopLimit' or 'takeProfitLimit' order, Default is 'FOK' if 'market', 'stopMarket' or 'takeProfitMarket' order
398398
# +String+ +price+:: Optional. Required for 'limit' and 'stopLimit'. limit price of the order
399399
# +String+ +stop_price+:: Optional. Required for 'stopLimit' and 'stopMarket' orders. stop price of the order
400400
# +String+ +expire_time+:: Optional. Required for orders with timeInForce = GDT
@@ -419,10 +419,10 @@ def create_spot_order( # rubocop:disable Metrics/ParameterLists
419419
# creates a list of spot orders
420420
#
421421
# = Types or contingency
422-
# - Args::Contingency::ALL_OR_NONE (Args::Contingency::AON)
423-
# - Args::Contingency::ONE_CANCEL_OTHER (Args::Contingency::OCO)
424-
# - Args::Contingency::ONE_TRIGGER_OTHER (Args::Contingency::OTO)
425-
# - Args::Contingency::ONE_TRIGGER_ONE_CANCEL_OTHER (Args::Contingency::OTOCO)
422+
# - 'allOrNone' (AON)
423+
# - 'oneCancelAnother' (OCO)
424+
# - 'oneTriggerOther' (OTO)
425+
# - 'oneTriggerOneCancelOther' (OTOCO)
426426
#
427427
# = Restriction in the number of orders:
428428
# - An AON list must have 2 or 3 orders
@@ -437,21 +437,21 @@ def create_spot_order( # rubocop:disable Metrics/ParameterLists
437437
# - 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).
438438
#
439439
# = OrderType restrictions
440-
# - For an AON order list, orders must be Args::OrderType::LIMIT or Args::OrderType::Market
441-
# - For an OCO order list, orders must be Args::OrderType::LIMIT, Args::OrderType::STOP_LIMIT, Args::OrderType::STOP_MARKET, Args::OrderType::TAKE_PROFIT_LIMIT or Args::OrderType::TAKE_PROFIT_MARKET.
440+
# - For an AON order list, orders must be 'limit' or 'market'
441+
# - For an OCO order list, orders must be 'limit', 'stopLimit', 'stopMarket', takeProfitLimit or takeProfitMarket.
442442
# - An OCO order list cannot include more than one limit order (the same
443443
# applies to secondary orders in an OTOCO order list).
444444
# - For OTO order list, there are no order type restrictions.
445-
# - For an OTOCO order list, the first order must be Args::OrderType::LIMIT, Args::OrderType::MARKET, Args::OrderType::STOP_LIMIT, Args::OrderType::STOP_MARKET, Args::OrderType::TAKE_PROFIT_LIMIT or Args::OrderType::TAKE_PROFIT_MARKET.
445+
# - For an OTOCO order list, the first order must be 'limit', 'market', 'stopLimit', 'stopMarket', takeProfitLimit or takeProfitMarket.
446446
# - For an OTOCO order list, the secondary orders have the same restrictions as an OCO order
447-
# - Default is Args::OrderType::Limit
447+
# - Default is 'limit'
448448
#
449449
# https://api.exchange.cryptomkt.com/#create-new-spot-order-list
450450
#
451451
# ==== Params
452452
# +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
453-
# +String+ +contingency_type+:: order list type. allOrNone, oneCancelOther or oneTriggerOneCancelOther
454-
# +Array[]+ +orders+:: the list of orders
453+
# +String+ +contingency_type+:: order list type. 'allOrNone', 'oneCancelOther' or 'oneTriggerOneCancelOther'
454+
# +Array[]+ +orders+:: the list of orders. aech order in the list has the same parameters of a new spot order
455455

456456
def create_spot_order_list(
457457
contingency_type:,

lib/cryptomarket/exceptions.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ class SDKException < ::StandardError
88
class APIException < SDKException
99
def initialize(hash)
1010
@code = hash['code']
11-
@message = hash['message']
12-
@description = hash['description']
11+
raw_message = hash['message']
12+
@description = hash.key?('description') ? hash['description'] : ''
13+
@message = "#{self.class.name} (code=#{@code}): #{raw_message}. #{@description}"
1314
super
1415
end
1516

1617
attr_reader :code, :message, :description
1718

1819
def to_s
19-
"#{self.class.name} (code=#{@code}): #{@message}: #{@description}"
20+
@message
2021
end
2122
end
2223
end

lib/cryptomarket/http_manager.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ def handle_response(response)
8989
parsed_result = JSON.parse result
9090
if (response.code != 200) && !parsed_result['error'].nil?
9191
error = parsed_result['error']
92-
msg = "(code=#{error['code']}): #{error['message']}"
93-
msg += ": #{error['description']}" unless error['description'].nil?
9492
exception = Cryptomarket::APIException.new error
95-
raise exception, msg
93+
raise exception
9694
end
9795
parsed_result
9896
end

lib/cryptomarket/websocket/trading_client.rb

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module Cryptomarket
88
module Websocket
99
# TradingClient connects via websocket to cryptomarket to enable the user to manage orders.
10-
# uses SHA256 as auth method and authenticates automatically.
10+
# uses SHA256 as auth method and authenticates automatically after ceonnection.
1111
class TradingClient < AuthClient
1212
# Creates a new client
1313
# ==== Params
@@ -42,7 +42,6 @@ def build_subscription_hash
4242
# ==== Params
4343
# +Proc+ +callback+:: A +Proc+ that recieves notifications as a list of reports, and the type of notification (either 'snapshot' or 'update')
4444
# +Proc+ +result_callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the subscription
45-
4645
def subscribe_to_reports(callback:, result_callback: nil)
4746
interceptor = proc { |notification, type|
4847
if type == Args::NotificationType::SNAPSHOT
@@ -60,7 +59,6 @@ def subscribe_to_reports(callback:, result_callback: nil)
6059
#
6160
# ==== Params
6261
# +Proc+ +callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the unsubscription
63-
6462
def unsubscribe_to_reports(callback: nil)
6563
send_unsubscription('spot_unsubscribe', callback, nil)
6664
end
@@ -75,7 +73,7 @@ def unsubscribe_to_reports(callback: nil)
7573
# +Proc+ +result_callback+:: Optional. A +Proc+ called with a boolean value, indicating the success of the subscription
7674
# +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
7775
def subscribe_to_spot_balance(callback:, mode: nil, result_callback: nil)
78-
interceptor = proc { |notification, _type|
76+
interceptor = lambda { |notification, _type|
7977
callback.call(notification)
8078
}
8179
send_subscription('spot_balance_subscribe', interceptor, { mode: mode }, result_callback)
@@ -87,7 +85,6 @@ def subscribe_to_spot_balance(callback:, mode: nil, result_callback: nil)
8785
#
8886
# ==== Params
8987
# +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
90-
9188
def unsubscribe_to_spot_balance(result_callback: nil)
9289
send_unsubscription(
9390
'spot_balance_unsubscribe',
@@ -102,7 +99,6 @@ def unsubscribe_to_spot_balance(result_callback: nil)
10299
#
103100
# ==== Params
104101
# +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
105-
106102
def get_active_spot_orders(callback:)
107103
request('spot_get_orders', callback)
108104
end
@@ -128,7 +124,6 @@ def get_active_spot_orders(callback:)
128124
# +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.
129125
# +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.
130126
# +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
131-
132127
def create_spot_order( # rubocop:disable Metrics/ParameterLists
133128
symbol:, side:, quantity:, client_order_id: nil, type: nil, time_in_force: nil, price: nil, stop_price: nil,
134129
expire_time: nil, strict_validate: nil, post_only: nil, take_rate: nil, make_rate: nil, callback: nil
@@ -142,10 +137,10 @@ def create_spot_order( # rubocop:disable Metrics/ParameterLists
142137
# creates a list of spot orders
143138
#
144139
# = Types or contingency
145-
# - Contingency::ALL_OR_NONE (AON)
146-
# - Contingency::ONE_CANCEL_OTHER (OCO)
147-
# - Contingency::ONE_TRIGGER_OTHER (OTO)
148-
# - Contingency::ONE_TRIGGER_ONE_CANCEL_OTHER (OTOCO)
140+
# - 'allOrNone' (AON)
141+
# - 'oneCancelAnother' (OCO)
142+
# - 'oneTriggerOther' (OTO)
143+
# - 'oneTriggerOneCancelOther' (OTOCO)
149144
#
150145
# = Restriction in the number of orders:
151146
# - An AON list must have 2 or 3 orders
@@ -160,23 +155,21 @@ def create_spot_order( # rubocop:disable Metrics/ParameterLists
160155
# - 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).
161156
#
162157
# = OrderType restrictions
163-
# - For an AON order list, orders must be OrderType.LIMIT or OrderType.Market
164-
# - For an OCO order list, orders must be OrderType.LIMIT, OrderType.STOP_LIMIT, OrderType.STOP_MARKET, OrderType.TAKE_PROFIT_LIMIT or OrderType.TAKE_PROFIT_MARKET.
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.
165160
# - An OCO order list cannot include more than one limit order (the same
166161
# applies to secondary orders in an OTOCO order list).
167162
# - For OTO order list, there are no order type restrictions.
168-
# - For an OTOCO order list, the first order must be OrderType.LIMIT, OrderType.MARKET, OrderType.STOP_LIMIT, OrderType.STOP_MARKET, OrderType.TAKE_PROFIT_LIMIT or OrderType.TAKE_PROFIT_MARKET.
163+
# - For an OTOCO order list, the first order must be 'limit', 'market', 'stopLimit', 'stopMarket', takeProfitLimit or takeProfitMarket.
169164
# - For an OTOCO order list, the secondary orders have the same restrictions as an OCO order
170-
# - Default is OrderType.Limit
165+
# - Default is 'limit'
171166
#
172-
# https://api.exchange.cryptomkt.com/#create-new-spot-order-list-2
167+
# https://api.exchange.cryptomkt.com/#create-new-spot-order-list
173168
#
174169
# ==== Params
175170
# +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
178-
# +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 created orders
179-
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
180173
def create_spot_order_list(
181174
orders:, contingency_type:, order_list_id: nil, callback: nil
182175
)
@@ -203,7 +196,6 @@ def cancel_spot_order(client_order_id:, callback: nil)
203196
#
204197
# ==== Params
205198
# +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
206-
207199
def cancel_spot_orders(callback: nil)
208200
request('spot_cancel_orders', callback)
209201
end
@@ -274,7 +266,6 @@ def get_spot_commissions(callback:)
274266
# ==== Params
275267
# +String+ +symbol+:: The symbol of the commission rate
276268
# +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
277-
278269
def get_spot_commission(symbol:, callback:)
279270
request('spot_fee', callback, { symbol: symbol })
280271
end

lib/cryptomarket/websocket/wallet_client.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
module Cryptomarket
88
module Websocket
9-
# WalletClient connects via websocket to cryptomarket to get wallet information of the user. uses SHA256 as auth method and authenticates automatically.
9+
# WalletClient connects via websocket to cryptomarket to get wallet information of the user.
10+
# Uses SHA256 as auth method and authenticates automatically.
1011
class WalletClient < AuthClient
1112
# Creates a new client and authenticates it to the server
1213
# ==== Params
1314
# +String+ +api_key+:: the user api key
1415
# +String+ +api_secret+:: the user api secret
1516
# +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+
1618
def initialize(api_key:, api_secret:, window: nil)
1719
super(
1820
url: 'wss://api.exchange.cryptomkt.com/api/3/ws/wallet',
@@ -44,7 +46,10 @@ def build_subscription_hash
4446
# +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
4547

4648
def subscribe_to_transactions(callback:, result_callback: nil)
47-
send_subscription('subscribe_transactions', callback, nil, result_callback)
49+
interceptor = lambda { |notification, _type|
50+
callback.call(notification)
51+
}
52+
send_subscription('subscribe_transactions', interceptor, nil, result_callback)
4853
end
4954

5055
# stop recieving the feed of transactions changes

tests/checker_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def gen_check_notification_w_n_type_callback(check_fn, veredict_checker)
8383
->(notification, _notification_type) { check_val(notification, check_fn, veredict_checker) }
8484
end
8585

86+
def gen_check_notification_callback(check_fn, veredict_checker)
87+
->(notification) { check_val(notification, check_fn, veredict_checker) }
88+
end
89+
8690
def gen_check_notification_list_callback(check_fn, veredict_checker)
8791
->(notification) { check_list(notification, check_fn, veredict_checker) }
8892
end

tests/rest/wallet_management.rb

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,6 @@ def test_get_last_10_withdrawal_crypto_addresses
4545
assert(good_list(->(address) { Check.good_address(address) }, result))
4646
end
4747

48-
def test_withdraw_crypto
49-
ada_address = @client.get_deposit_crypto_address(currency: 'ADA')['address']
50-
transaction_id = @client.withdraw_crypto(currency: 'ADA', amount: '0.1', address: ada_address)
51-
assert(!transaction_id.empty?)
52-
end
53-
54-
def test_withdraw_crypto_commit
55-
ada_address = @client.get_deposit_crypto_address(currency: 'ADA')['address']
56-
transaction_id = @client.withdraw_crypto(
57-
currency: 'ADA',
58-
amount: '0.1',
59-
address: ada_address,
60-
auto_commit: false
61-
)
62-
success = @client.withdraw_crypto_commit id: transaction_id
63-
assert(success)
64-
end
65-
66-
def test_withdraw_crypto_rollback
67-
ada_address = @client.get_deposit_crypto_address(currency: 'ADA')['address']
68-
transaction_id = @client.withdraw_crypto(
69-
currency: 'ADA',
70-
amount: '0.1',
71-
address: ada_address,
72-
auto_commit: false
73-
)
74-
success = @client.withdraw_crypto_rollback id: transaction_id
75-
assert(success)
76-
end
77-
7848
def test_get_estimate_withdrawal_fees
7949
result = @client.get_estimate_withdrawal_fees [{ currency: 'ETH', amount: '12' }, { currency: 'BTC', amount: '1' }]
8050
assert(result.count == 2)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require 'test/unit'
4+
require_relative '../key_loader'
5+
require_relative '../wallet'
6+
require_relative '../checks'
7+
require_relative '../../lib/cryptomarket/client'
8+
9+
class TestRestTradingMethods < Test::Unit::TestCase # rubocop:disable Style/Documentation
10+
def setup
11+
@wallet = Wallet.new
12+
@client = Cryptomarket::Client.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
13+
end
14+
15+
def test_withdraw_crypto_commit
16+
transaction_id = @client.withdraw_crypto(
17+
currency: 'ETH',
18+
amount: '0.00001',
19+
address: @wallet.eth,
20+
auto_commit: false
21+
)
22+
success = @client.withdraw_crypto_commit id: transaction_id
23+
assert(success)
24+
end
25+
26+
def test_withdraw_crypto_rollback
27+
transaction_id = @client.withdraw_crypto(
28+
currency: 'ETH',
29+
amount: '0.00001',
30+
address: @wallet.eth,
31+
auto_commit: false
32+
)
33+
success = @client.withdraw_crypto_rollback id: transaction_id
34+
assert(success)
35+
end
36+
end

tests/wallet.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'json'
2+
3+
# loads a json with keys
4+
class Wallet
5+
def initialize
6+
file = File.open '../../wallet.json'
7+
keys = JSON.load file # rubocop:disable Security/JSONLoad
8+
@eth = keys['eth']
9+
end
10+
11+
attr_reader :eth
12+
end

tests/websocket/wallet_client_subscriptions.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require_relative '../checks'
88
require_relative '../checker_generator'
99

10-
class TestWSaccount < Test::Unit::TestCase
10+
class TestWSWalletClientSubscriptions < Test::Unit::TestCase # rubocop:disable Style/Documentation
1111
def setup
1212
@wsclient = Cryptomarket::Websocket::WalletClient.new api_key: KeyLoader.api_key, api_secret: KeyLoader.api_secret
1313
@wsclient.connect
@@ -22,12 +22,12 @@ def teardown
2222

2323
def test_transaction_and_balance_subscription # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
2424
@wsclient.subscribe_to_transactions(
25-
callback: gen_check_notification_w_n_type_callback(WSCheck.good_transaction, @veredict_checker),
25+
callback: gen_check_notification_callback(WSCheck.good_transaction, @veredict_checker),
2626
result_callback: gen_result_callback(@veredict_checker)
2727
)
2828
sleep(3)
2929
@restclient.transfer_between_wallet_and_exchange(
30-
currency: 'EOS',
30+
currency: 'CRO',
3131
amount: '0.1',
3232
source: 'spot',
3333
destination: 'wallet'
@@ -39,7 +39,7 @@ def test_transaction_and_balance_subscription # rubocop:disable Metrics/MethodLe
3939
)
4040
sleep(3)
4141
@restclient.transfer_between_wallet_and_exchange(
42-
currency: 'EOS',
42+
currency: 'CRO',
4343
amount: '0.1',
4444
source: 'wallet',
4545
destination: 'spot'

0 commit comments

Comments
 (0)