diff --git a/cryptomarket-sdk.gemspec b/cryptomarket-sdk.gemspec index 31c2c47..954d129 100644 --- a/cryptomarket-sdk.gemspec +++ b/cryptomarket-sdk.gemspec @@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__) Gem::Specification.new do |s| s.name = "cryptomarket-sdk" - s.version = "3.0.0" + s.version = "3.1.0" s.platform = Gem::Platform::RUBY s.authors = ["T. Ismael Verdugo"] s.email = ["ismael@dysopsis.com"] diff --git a/lib/cryptomarket/client.rb b/lib/cryptomarket/client.rb index f7f7258..3c04aa0 100644 --- a/lib/cryptomarket/client.rb +++ b/lib/cryptomarket/client.rb @@ -39,6 +39,37 @@ def delete(endpoint, params = nil) @http_manager.make_request(method: 'delete', endpoint: endpoint, params: params) end + ########### + # aliases # + ########### + + # market data + + # alias of get ticker + alias get_ticker_by_symbol get_ticker + alias get_ticker_of_symbol get_ticker + alias get_ticker_price_by_symbol get_ticker_price + alias get_ticker_price_of_symbol get_ticker_price + alias get_trades_of_symbol get_trades_by_symbol + alias get_orderbook_by_symbol get_orderbook + alias get_orderbook_of_symbol get_orderbook + alias get_orderbook_volume_by_symbol get_orderbook_volume + alias get_orderbook_volume_of_symbol get_orderbook_volume + alias get_candles_of_symbol get_candles_by_symbol + alias get_converted_candles_of_symbol get_converted_candles_by_symbol + # spot trading + alias get_spot_trading_balance_of_currency get_spot_trading_balance + alias get_spot_trading_balance_by_currency get_spot_trading_balance + alias get_all_trading_commissions get_all_trading_commission + alias get_trading_commission get_all_trading_commission + alias get_trading_commission_of_symbol get_trading_commission + alias get_trading_commission_by_symbol get_trading_commission + # wallet management + alias get_wallet_balance_of_currency get_wallet_balance + alias get_wallet_balance_by_currency get_wallet_balance + alias get_deposit_crypto_address_of_cyrrency get_deposit_crypto_address + alias get_deposit_crypto_address_by_cyrrency get_deposit_crypto_address + ################ # public calls # ################ @@ -154,7 +185,7 @@ def get_prices(to:, from: nil) # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' # +String+ +since+:: Optional. Initial value of the queried interval # +String+ +until+:: Optional. Last value of the queried interval - # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 1. Min is 1. Max is 1000 + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 1. Min is 1. Max is 1_000 def get_price_history(to:, from: nil, till: nil, since: nil, limit: nil, period: nil, sort: nil) # rubocop:disable Metrics/ParameterLists public_get( @@ -202,7 +233,7 @@ def get_ticker_price(symbol:) # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' # +String+ +since+:: Optional. Initial value of the queried interval # +String+ +until+:: Optional. Last value of the queried interval - # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000 + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1_000 def get_trades(symbols: nil, by: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists public_get( @@ -224,8 +255,8 @@ def get_trades(symbols: nil, by: nil, sort: nil, from: nil, till: nil, limit: ni # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' # +String+ +since+:: Optional. Initial value of the queried interval # +String+ +until+:: Optional. Last value of the queried interval - # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000 - # +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100000 + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1_000 + # +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100_000 def get_trades_by_symbol(symbol: nil, by: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists public_get( @@ -280,7 +311,7 @@ def get_orderbook_volume(symbol:, volume: nil) end # Get a Hash of candles for all symbols or for specified symbols - # Candels are used for OHLC representation + # Candles are used for OHLC representation # The result contains candles with non-zero volume only (no trades = no candles) # # Requires no API key Access Rights @@ -293,7 +324,7 @@ def get_orderbook_volume(symbol:, volume: nil) # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' # +String+ +from+:: Optional. Initial value of the queried interval. As DateTime # +String+ +till+:: Optional. Last value of the queried interval. As DateTime - # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000 + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1_000 def get_candles(symbols: nil, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists public_get( @@ -303,7 +334,7 @@ def get_candles(symbols: nil, period: nil, sort: nil, from: nil, till: nil, limi end # Get candles of a symbol - # Candels are used for OHLC representation + # Candles are used for OHLC representation # The result contains candles with non-zero volume only (no trades = no candles) # # Requires no API key Access Rights @@ -316,8 +347,8 @@ def get_candles(symbols: nil, period: nil, sort: nil, from: nil, till: nil, limi # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' # +String+ +from+:: Optional. Initial value of the queried interval. As DateTime # +String+ +till+:: Optional. Last value of the queried interval. As DateTime - # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1000 - # +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100000 + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000 + # +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100_000 def get_candles_by_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists public_get( @@ -326,6 +357,61 @@ def get_candles_by_symbol(symbol:, period: nil, sort: nil, from: nil, till: nil, ) end + # Gets OHLCV data regarding the last price converted to the target currency for all symbols or for the specified symbols + # + # Candles are used for OHLC representation + # + # The result contains candles with non-zero volume only (no trades = no candles) + # + # Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid or ask price, the last price is returned. + # + # Requires no API key Access Rights + # + # https://api.exchange.cryptomkt.com/#candles + # + # +String+ +target_currency+:: Target currency for conversion + # +Array[String]+ +symbols+:: Optional. A list of symbols + # +String+ +period+:: Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30' + # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' + # +String+ +from+:: Optional. Initial value of the queried interval. As DateTime + # +String+ +till+:: Optional. Last value of the queried interval. As DateTime + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000 + + def get_converted_candles(target_currency:, symbols: nil, period: nil, sort: nil, from: nil, till: nil, limit: nil) # rubocop:disable Metrics/ParameterLists + public_get( + 'public/converted/candles', + { target_currency: target_currency, symbols: symbols, period: period, sort: sort, from: from, till: till, limit: limit } + ) + end + + # Gets OHLCV data regarding the last price converted to the target currency for the specified symbol + # + # Candles are used for OHLC representation + # + # The result contains candles with non-zero volume only (no trades = no candles) + # + # Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid or ask price, the last price is returned. + # + # Requires no API key Access Rights + # + # https://api.exchange.cryptomkt.com/#candles + # + # +String+ +target_currency+:: Target currency for conversion + # +String+ +symbol+:: A symbol id + # +String+ +period+:: Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30' + # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' + # +String+ +from+:: Optional. Initial value of the queried interval. As DateTime + # +String+ +till+:: Optional. Last value of the queried interval. As DateTime + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000 + # +Integer+ +offset+:: Optional. Default is 0. Min is 0. Max is 100_000 + + def get_converted_candles_by_symbol(target_currency:, symbol:, period: nil, sort: nil, from: nil, till: nil, limit: nil, offset: nil) # rubocop:disable Metrics/ParameterLists + public_get( + "public/converted/candles/#{symbol}", + { target_currency: target_currency, period: period, sort: sort, from: from, till: till, limit: limit, offset: offset } + ) + end + ###################### # Spot Trading calls # ###################### @@ -567,8 +653,8 @@ def get_trading_commission(symbol:) # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' # +String+ +from+:: Optional. Initial value of the queried interval # +String+ +till+:: Optional. Last value of the queried interval - # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1000 - # +Integer+ +offset+:: Optional. Default is 0. Max is 100000 + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1_000 + # +Integer+ +offset+:: Optional. Default is 0. Max is 100_000 def get_spot_orders_history( # rubocop:disable Metrics/ParameterLists client_order_id: nil, symbol: nil, sort: nil, by: nil, from: nil, @@ -594,8 +680,8 @@ def get_spot_orders_history( # rubocop:disable Metrics/ParameterLists # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' # +String+ +from+:: Optional. Initial value of the queried interval # +String+ +till+:: Optional. Last value of the queried interval - # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1000 - # +Integer+ +offset+:: Optional. Default is 0. Max is 100000 + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Max is 1_000 + # +Integer+ +offset+:: Optional. Default is 0. Max is 100_000 def get_spot_trades_history( # rubocop:disable Metrics/ParameterLists order_id: nil, symbol: nil, sort: nil, by: nil, from: nil, @@ -906,8 +992,8 @@ def transfer_money_to_another_user(currency:, amount:, by:, identifier:) # +String+ +id_from+:: Optional. Interval initial value when ordering by id. Min is 0 # +String+ +id_till+:: Optional. Interval end value when ordering by id. Min is 0 # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' - # +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1000 - # +Integer+ +offset+:: Optional. Default is 0. Max is 100000 + # +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1_000 + # +Integer+ +offset+:: Optional. Default is 0. Max is 100_000 # +bool+ +group_transactions+:: Optional. Flag indicating whether the returned transactions will be parts of a single operation. Default is false def get_transaction_history( # rubocop:disable Metrics/ParameterLists @@ -971,7 +1057,7 @@ def offchain_available?( # ==== Params # +String+ +currency+:: Optional. Currency code # +bool+ +active+:: Optional. value showing whether the lock is active - # +Integer+ +limit+:: Optional. Dafault is 100. Min is 0. Max is 1000 + # +Integer+ +limit+:: Optional. Dafault is 100. Min is 0. Max is 1_000 # +Integer+ +offset+:: Optional. Default is 0. Min is 0 # +String+ +from+:: Optional. Interval initial value. As Datetime # +String+ +till+:: Optional. Interval end value. As Datetime diff --git a/lib/cryptomarket/websocket/auth_client.rb b/lib/cryptomarket/websocket/auth_client.rb index 88f189f..93b8056 100644 --- a/lib/cryptomarket/websocket/auth_client.rb +++ b/lib/cryptomarket/websocket/auth_client.rb @@ -48,7 +48,7 @@ def wait_authed # err is None for successful calls, result is None for calls with error: Proc.new {|err, result| ...} def authenticate(callback = nil) - timestamp = Time.now.to_i * 1000 + timestamp = Time.now.to_i * 1_000 digest = OpenSSL::Digest.new 'sha256' message = timestamp.to_s message += @window.to_s unless @window.nil? diff --git a/lib/cryptomarket/websocket/market_data_client.rb b/lib/cryptomarket/websocket/market_data_client.rb index 22caac8..7e2d21b 100644 --- a/lib/cryptomarket/websocket/market_data_client.rb +++ b/lib/cryptomarket/websocket/market_data_client.rb @@ -26,7 +26,7 @@ class MarketDataClient < MarketDataClientCore # ==== Params # +Proc+ +callback+:: A +Proc+ that recieves notifications as a hash of trades indexed by symbol, and the type of notification (either 'snapshot' or 'update') # +Array[String]+ +symbols+:: A list of symbol ids - # +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1000. Default is 0 + # +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1_000. Default is 0 # +Proc+ +result_callback+:: Optional. A +Proc+ of two arguments, An exception and a result, called either with the exception or with the result, a list of subscribed symbols def subscribe_to_trades(callback:, symbols:, limit: nil, result_callback: nil) @@ -40,18 +40,15 @@ def subscribe_to_trades(callback:, symbols:, limit: nil, result_callback: nil) # # normal subscriptions have one update message per symbol # - # the first notification are n candles, with n defined by the limit argument, - # the next notification are updates, with one candle at a time - # # Requires no API key Access Rights # # https://api.exchange.cryptomkt.com/#subscribe-to-candles # # ==== Params # +Proc+ +callback+:: A +Proc+ that recieves notifications as a hash of candles indexed by symbol, and the type of notification (either 'snapshot' or 'update') - # +String+ +period+:: Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30' + # +String+ +period+:: Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). # +Array[String]+ +symbols+:: Optional. A list of symbol ids - # +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1000. Default is 0 + # +Integer+ +limit+:: Number of historical entries returned in the first feed. Min is 0. Max is 1_000. Default is 0 # +Proc+ +result_callback+:: Optional. A +Proc+ called with a list of subscribed symbols def subscribe_to_candles(callback:, period:, symbols:, limit: nil, result_callback: nil) @@ -60,6 +57,31 @@ def subscribe_to_candles(callback:, period:, symbols:, limit: nil, result_callba intercept_result_callback(result_callback), params) end + # subscribes to a feed of candles regarding the last price converted to the target currency for all symbols or for the specified symbols + # + # Candles are used for the representation of a specific symbol as an OHLC chart + # + # Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid or ask price, the last price is returned. + # + # Requires no API key Access Rights + # + # https://api.exchange.cryptomkt.com/#subscribe-to-converted-candles + # + # +Proc+ +callback+:: A +Proc+ that recieves notifications as a hash of candles indexed by symbol, and the type of notification (either 'snapshot' or 'update') + # +String+ +target_currency+:: Target currency for conversion + # +Array[String]+ +symbols+:: A list of symbols + # +String+ +period+:: A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). + # +String+ +from+:: Optional. Initial value of the queried interval. As DateTime + # +String+ +till+:: Optional. Last value of the queried interval. As DateTime + # +Integer+ +limit+:: Optional. Prices per currency pair. Defaul is 100. Min is 1. Max is 1_000 + # +Proc+ +result_callback+:: Optional. A +Proc+ called with a list of subscribed symbols + + def subscribe_to_converted_candles(callback:, target_currency:, symbols:, period:, limit: nil, result_callback: nil) # rubocop:disable Metrics/ParameterLists + params = { 'target_currency' => target_currency, 'symbols' => symbols, 'limit' => limit } + send_channel_subscription("converted/candles/#{period}", callback, + intercept_result_callback(result_callback), params) + end + # subscribe to a feed of mini tickers # # subscription is for all symbols or for the specified symbols @@ -258,7 +280,7 @@ def subscribe_to_top_of_book_in_batches(callback:, speed:, symbols: ['*'], resul # # subscription is for all currencies or for the specified currencies # - # https://api.exchange.cryptomkt.com/#subscribe-to-top-of-book-in-batches + # https://api.exchange.cryptomkt.com/#subscribe-to-price-rates # # ==== Params # +Proc+ +callback+:: A +Proc+ that recieves notifications as a hash of top of orderbooks indexed by symbol, and the type of notification (only 'data') diff --git a/lib/cryptomarket/websocket/trading_client.rb b/lib/cryptomarket/websocket/trading_client.rb index fc998c9..7bd0168 100644 --- a/lib/cryptomarket/websocket/trading_client.rb +++ b/lib/cryptomarket/websocket/trading_client.rb @@ -35,6 +35,11 @@ def build_subscription_hash 'spot_balance' => [balances, Args::NotificationType::SNAPSHOT] } end + alias get_spot_trading_balance_of_currency get_spot_trading_balance + alias get_spot_trading_balance_by_currency get_spot_trading_balance + alias get_spot_commission_of_symbol get_spot_commission + alias get_spot_commission_by_symbol get_spot_commission + # subscribe to a feed of execution reports of the user's orders # # https://api.exchange.cryptomkt.com/#socket-spot-trading diff --git a/lib/cryptomarket/websocket/wallet_client.rb b/lib/cryptomarket/websocket/wallet_client.rb index 8631baf..fe28d90 100644 --- a/lib/cryptomarket/websocket/wallet_client.rb +++ b/lib/cryptomarket/websocket/wallet_client.rb @@ -37,6 +37,9 @@ def build_subscription_hash 'wallet_balance_update' => [balance, Args::NotificationType::UPDATE] } end + alias get_wallet_balance_of_currency get_wallet_balance + alias get_wallet_balance_by_currency get_wallet_balance + # 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 # # https://api.exchange.cryptomkt.com/#subscribe-to-transactions @@ -151,8 +154,8 @@ def get_wallet_balance(currency:, callback:) # +String+ +id_till+:: Optional. Interval end value when ordering by id. Min is 0 # +String+ +order_by+:: Optional. sorting parameter.'created_at' or 'id'. Default is 'created_at' # +String+ +sort+:: Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC' - # +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1000 - # +Integer+ +offset+:: Optional. Default is 0. Max is 100000 + # +Integer+ +limit+:: Optional. Transactions per query. Defaul is 100. Max is 1_000 + # +Integer+ +offset+:: Optional. Default is 0. Max is 100_000 # +bool+ +group_transactions+:: Optional. Flag indicating whether the returned transactions will be parts of a single operation. Default is false def get_transactions( # rubocop:disable Metrics/ParameterLists diff --git a/tests/checker_generator.rb b/tests/checker_generator.rb index 908bc16..83cb490 100644 --- a/tests/checker_generator.rb +++ b/tests/checker_generator.rb @@ -91,10 +91,6 @@ def gen_check_notification_list_callback(check_fn, veredict_checker) ->(notification) { check_list(notification, check_fn, veredict_checker) } end -def |(check_fn, veredict_checker) - ->(notification, _notification_type) { check_list(notification, check_fn, veredict_checker) } -end - def gen_check_notification_hash_list_callback(check_fn, veredict_checker) ->(notification, _notification_type) { check_hash_list(notification, check_fn, veredict_checker) } end diff --git a/tests/rest/market_data.rb b/tests/rest/market_data.rb index 978a6f3..3932918 100644 --- a/tests/rest/market_data.rb +++ b/tests/rest/market_data.rb @@ -4,7 +4,7 @@ require_relative '../../lib/cryptomarket/client' require_relative '../checks' -class TestMarketDataMethods < Test::Unit::TestCase # rubocop:disable Style/Documentation +class TestMarketDataMethods < Test::Unit::TestCase # rubocop:disable Style/Documentation,Metrics/ClassLength def setup @client = Cryptomarket::Client.new end @@ -127,4 +127,18 @@ def test_get_candles_by_symbol assert(Check.good_candle(candle)) end end + + def test_get_converted_candles + symbols = %w[EOSETH BTCUSDT] + result = @client.get_converted_candles(symbols: symbols, limit: 2, target_currency: 'usdt') + result['data'].each do |symbol, candles| + assert(symbols.include?(symbol)) + candles.each { |val| assert(Check.good_candle(val)) } + end + end + + def test_get_converted_candles_by_symbol + result = @client.get_converted_candles_by_symbol(symbol: 'eoseth', limit: 2, target_currency: 'usdt') + result['data'].each { |val| assert(Check.good_candle(val)) } + end end diff --git a/tests/websocket/market_data_client.rb b/tests/websocket/market_data_client.rb index 8bfcfc5..0f4ba44 100644 --- a/tests/websocket/market_data_client.rb +++ b/tests/websocket/market_data_client.rb @@ -45,6 +45,17 @@ def test_candles_subscriptions assert(veredict_checker.good_veredict?) end + def test_converted_candles_subscription + veredict_checker = VeredictChecker.new + @wsclient.subscribe_to_converted_candles( + period: 'M1', target_currency: 'usdt', symbols: %w[eoseth ethbtc], limit: 2, + callback: gen_check_notification_hash_list_callback(WSCheck.good_ws_public_candle, veredict_checker), + result_callback: gen_result_callback(veredict_checker) + ) + sleep(10 * @@SECOND) + assert(veredict_checker.good_veredict?) + end + def test_subscribe_to_mini_ticker veredict_checker = VeredictChecker.new @wsclient.subscribe_to_mini_ticker(