class CossApiRubyWrapper::Exchange

Attributes

recv_window[RW]

Public Class Methods

new(public_key:, private_key:, recv_window: 5000) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 7
def initialize(public_key:, private_key:, recv_window: 5000)
  @public_key = public_key
  @private_key = private_key
  @recv_window = recv_window
end

Public Instance Methods

account_balances() click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 73
def account_balances
  request(:get, '/account/balances')
end
account_details() click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 77
def account_details
  request(:get, '/account/details')
end
all_orders(symbol, from_order_id, limit = 10) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 37
def all_orders(symbol, from_order_id, limit = 10)
  request(:post, '/order/list/all', symbol: symbol.to_s.strip, from_id: from_order_id.to_s.strip, limit: limit.to_i)
end
cancel_order(symbol, order_id) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 41
def cancel_order(symbol, order_id)
  request(:delete, '/order/cancel', order_id: order_id.to_s.strip, order_symbol: symbol.to_s.strip)
end
completed_orders(symbol, limit = 10, page = 0) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 33
def completed_orders(symbol, limit = 10, page = 0)
  request(:post, '/order/list/completed', limit: limit.to_i, page: page.to_i, symbol: symbol.to_s.strip)
end
exchange_info() click to toggle source

public request

# File lib/coss_api_ruby_wrapper/exchange.rb, line 61
def exchange_info
  request(:get, '/exchange-info')
end
market_price(symbol) click to toggle source

public request

# File lib/coss_api_ruby_wrapper/exchange.rb, line 46
def market_price(symbol)
  request(:get, '/market-price', symbol: symbol.to_s.strip)
end
market_summary(symbol) click to toggle source

public request

# File lib/coss_api_ruby_wrapper/exchange.rb, line 56
def market_summary(symbol)
  request(:get, '/getmarketsummaries', symbol: symbol.to_s.strip)
end
open_orders(symbol, limit = 10, page = 0) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 29
def open_orders(symbol, limit = 10, page = 0)
  request(:post, '/order/list/open', limit: limit.to_i, page: page.to_i, symbol: symbol.to_s.strip)
end
order_details(order_id) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 21
def order_details(order_id)
  request(:post, '/order/details', order_id: order_id.to_s.strip)
end
pair_depth(symbol) click to toggle source

public request

# File lib/coss_api_ruby_wrapper/exchange.rb, line 51
def pair_depth(symbol)
  request(:get, '/dp', symbol: symbol.to_s.strip)
end
ping() click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 65
def ping
  request(:get, '/ping')
end
place_limit_order(symbol, price, side, amount) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 13
def place_limit_order(symbol, price, side, amount)
  request(:post, '/order/add', order_symbol: symbol.to_s.strip, order_price: price.to_f, order_side: side.to_s.strip, order_size: amount.to_f, type: 'limit')
end
place_market_order(symbol, price, side, amount) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 17
def place_market_order(symbol, price, side, amount)
  request(:post, '/order/add', order_symbol: symbol.to_s.strip, order_price: price.to_f, order_side: side.to_s.strip, order_size: amount.to_f, type: 'market')
end
time() click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 69
def time
  request(:get, '/time')
end
trade_detail(order_id) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 25
def trade_detail(order_id)
  request(:post, '/order/trade-detail', order_id: order_id.to_s.strip)
end

Private Instance Methods

host_url(endpoint) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 135
def host_url(endpoint)
  # At this point COSS has different hosts for some endpoints
  case endpoint
  when %r{^\/market_price.*}, %r{^\/dp.*}
    ENGINE_HOST_URL
  else
    TRADE_HOST_URL
  end
end
normalized_params(params, endpoint) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 126
def normalized_params(params, endpoint)
  if private_request?(endpoint)
    params[:recvWindow] = recv_window.to_i
    params[:timestamp] = timestamp
  end
  # Some COSS endpoints require params to be sorted alphabetically
  Hash[params.sort_by(&:first)]
end
private_request?(endpoint) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 145
def private_request?(endpoint)
  # Public requqests are not required to be signed with public/private keys
  case endpoint
  when %r{^\/market-price.*}, %r{^\/dp.*/, /^\/exchange-info.*}, %r{^\/getmarketsummaries.*}, %r{^\/ping.*}, %r{^\/time.*}
    false
  else
    true
  end
end
request(method, endpoint, params = {}) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 87
def request(method, endpoint, params = {})
  validate!(endpoint, params)

  uri = URI("#{host_url(endpoint)}#{endpoint}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  params = normalized_params(params, endpoint)

  # GET requests parameters are passed only as URL address params and POST/DELETE requests require JSON parameters
  payload = method == :get ? URI.encode_www_form(params) : params.to_json
  request_path = method == :get ? [uri.path, payload].join('?') : uri.path

  req = Net::HTTP.const_get(method.capitalize).new(request_path, 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest')
  if private_request?(endpoint)
    req['Authorization'] = @public_key
    req['Signature'] = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @private_key, payload)
  end
  req.body = payload unless method == :get # Do not set payload inside request body for GET requests - use URL address parameters
  response = http.request(req)

  # Return error for both: JSON and HTML formats
  unless response.code == '200'
    return { status: response.code, error: (begin
                                              JSON.parse(response.body)
                                            rescue StandardError
                                              response.body
                                            end) }
  end

  JSON.parse(response.body)
end
timestamp() click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 83
def timestamp
  Time.now.to_i * 1000
end
validate!(endpoint, params) click to toggle source
# File lib/coss_api_ruby_wrapper/exchange.rb, line 119
def validate!(endpoint, params)
  # Run request parameters validations
  validations = ParamsValidations.new(endpoint, params)
  validations.run!
  raise CossApiRubyWrapper::ParamsValidations::InvalidParameter, validations.errors.join("\n") unless validations.errors.empty?
end