Class: Binance::Client::REST

Inherits:
Object
  • Object
show all
Defined in:
lib/binance/client/rest.rb,
lib/binance/client/rest/clients.rb,
lib/binance/client/rest/methods.rb,
lib/binance/client/rest/endpoints.rb,
lib/binance/client/rest/sign_request_middleware.rb,
lib/binance/client/rest/timestamp_request_middleware.rb

Defined Under Namespace

Classes: SignRequestMiddleware, TimestampRequestMiddleware

Constant Summary collapse

BASE_URL =
'https://api.binance.com'.freeze
METHODS =
[
  # Public API Methods
  # #ping
  { name: :ping, client: :public,
    action: :get, endpoint: :ping },
  # #time
  { name: :time, client: :public,
    action: :get, endpoint: :time },
  # #exchange_info
  { name: :exchange_info, client: :public,
    action: :get, endpoint: :exchange_info },
  # #depth
  { name: :depth, client: :public,
    action: :get, endpoint: :depth },
  # #trades
  { name: :trades, client: :public,
    action: :get, endpoint: :trades },
  # #historical_trades
  { name: :historical_trades, client: :verified,
    action: :get, endpoint: :historical_trades },
  # #agg_trades
  { name: :agg_trades, client: :public,
    action: :get, endpoint: :agg_trades },
  # #klines
  { name: :klines, client: :public,
    action: :get, endpoint: :klines },
  # #twenty_four_hour
  { name: :twenty_four_hour, client: :public,
    action: :get, endpoint: :twenty_four_hour },
  # #price
  { name: :price, client: :public,
    action: :get, endpoint: :price },
  # #all_prices
  { name: :all_prices, client: :public,
    action: :get, endpoint: :price },
  # #book_ticker
  { name: :book_ticker, client: :public,
    action: :get, endpoint: :book_ticker },

  # Account API Methods
  # #create_order!
  { name: :create_order!, client: :signed,
    action: :post, endpoint: :order },
  # #create_test_order
  { name: :create_test_order, client: :signed,
    action: :post, endpoint: :test_order },
  # #query_order
  { name: :query_order, client: :signed,
    action: :get, endpoint: :order },
  # #cancel_order!
  { name: :cancel_order!, client: :signed,
    action: :delete, endpoint: :order },
  # #open_orders
  { name: :open_orders, client: :signed,
    action: :get, endpoint: :open_orders },
  # #all_orders
  { name: :all_orders, client: :signed,
    action: :get, endpoint: :all_orders },
  # #account_info
  { name: :account_info, client: :signed,
    action: :get, endpoint: :account },
  # #my_trades
  { name: :my_trades, client: :signed,
    action: :get, endpoint: :my_trades },
  # #listen_key
  { name: :listen_key, client: :verified,
    action: :post, endpoint: :user_data_stream },
  # #keep_alive_stream!
  { name: :keep_alive_stream!, client: :verified,
    action: :put, endpoint: :user_data_stream },
  # #close_stream!
  { name: :close_stream!, client: :verified,
    action: :delete, endpoint: :user_data_stream },

  # Withdraw API Methods
  # #withdraw!
  { name: :withdraw!, client: :withdraw,
    action: :post, endpoint: :withdraw },
  # #deposit_history
  { name: :deposit_history, client: :withdraw,
    action: :get, endpoint: :deposit_history },
  # #withdraw_history
  { name: :withdraw_history, client: :withdraw,
    action: :get, endpoint: :withdraw_history },
  # #deposit_address
  { name: :deposit_address, client: :withdraw,
    action: :get, endpoint: :deposit_address },
  # #account_status
  { name: :account_status, client: :withdraw,
    action: :get, endpoint: :account_status },
  # #system_status
  { name: :system_status, client: :public_withdraw,
    action: :get, endpoint: :system_status },
  # #withdraw_fee
  { name: :withdraw_fee, client: :withdraw,
    action: :get, endpoint: :withdraw_fee },
  # dust_log
  { name: :dust_log, client: :withdraw,
    action: :get, endpoint: :dust_log }
].freeze
ENDPOINTS =
{
  # Public API Endpoints
  ping:              'v1/ping',
  time:              'v1/time',
  exchange_info:     'v1/exchangeInfo',
  depth:             'v1/depth',
  trades:            'v1/trades',
  historical_trades: 'v1/historicalTrades',
  agg_trades:        'v1/aggTrades',
  klines:            'v1/klines',
  twenty_four_hour:  'v1/ticker/24hr',
  price:             'v3/ticker/price',
  book_ticker:       'v3/ticker/bookTicker',

  # Account API Endpoints
  order:            'v3/order',
  test_order:       'v3/order/test',
  open_orders:      'v3/openOrders',
  all_orders:       'v3/allOrders',
  account:          'v3/account',
  my_trades:        'v3/myTrades',
  user_data_stream: 'v1/userDataStream',

  # Withdraw API Endpoints
  withdraw:         'v3/withdraw.html',
  deposit_history:  'v3/depositHistory.html',
  withdraw_history: 'v3/withdrawHistory.html',
  deposit_address:  'v3/depositAddress.html',
  account_status:   'v3/accountStatus.html',
  system_status:    'v3/systemStatus.html',
  withdraw_fee:     'v3/withdrawFee.html',
  dust_log:         'v3/userAssetDribbletLog.html'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: '', secret_key: '', adapter: Faraday.default_adapter) ⇒ REST

Returns a new instance of REST.



14
15
16
17
18
19
20
21
22
# File 'lib/binance/client/rest.rb', line 14

def initialize(api_key: '', secret_key: '',
               adapter: Faraday.default_adapter)
  @clients = {}
  @clients[:public]   = public_client adapter
  @clients[:verified] = verified_client api_key, adapter
  @clients[:signed]   = signed_client api_key, secret_key, adapter
  @clients[:withdraw] = withdraw_client api_key, secret_key, adapter
  @clients[:public_withdraw] = public_withdraw_client adapter
end

Class Method Details

.add_query_param(query, key, value) ⇒ Object



34
35
36
37
38
# File 'lib/binance/client/rest.rb', line 34

def self.add_query_param(query, key, value)
  query = query.to_s
  query << '&' unless query.empty?
  query << "#{Faraday::Utils.escape key}=#{Faraday::Utils.escape value}"
end

Instance Method Details

#camelize(str) ⇒ Object



40
41
42
43
# File 'lib/binance/client/rest.rb', line 40

def camelize(str)
  str.split('_')
     .map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
end

#public_client(adapter) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/binance/client/rest/clients.rb', line 6

def public_client(adapter)
  Faraday.new(url: "#{BASE_URL}/api") do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    conn.adapter adapter
  end
end

#public_withdraw_client(adapter) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/binance/client/rest/clients.rb', line 33

def public_withdraw_client(adapter)
  Faraday.new(url: "#{BASE_URL}/wapi") do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    conn.adapter adapter
  end
end

#signed_client(api_key, secret_key, adapter) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/binance/client/rest/clients.rb', line 22

def signed_client(api_key, secret_key, adapter)
  Faraday.new(url: "#{BASE_URL}/api") do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    conn.headers['X-MBX-APIKEY'] = api_key
    conn.use TimestampRequestMiddleware
    conn.use SignRequestMiddleware, secret_key
    conn.adapter adapter
  end
end

#verified_client(api_key, adapter) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/binance/client/rest/clients.rb', line 14

def verified_client(api_key, adapter)
  Faraday.new(url: "#{BASE_URL}/api") do |conn|
    conn.response :json, content_type: /\bjson$/
    conn.headers['X-MBX-APIKEY'] = api_key
    conn.adapter adapter
  end
end

#withdraw_client(api_key, secret_key, adapter) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/binance/client/rest/clients.rb', line 41

def withdraw_client(api_key, secret_key, adapter)
  Faraday.new(url: "#{BASE_URL}/wapi") do |conn|
    conn.request :url_encoded
    conn.response :json, content_type: /\bjson$/
    conn.headers['X-MBX-APIKEY'] = api_key
    conn.use TimestampRequestMiddleware
    conn.use SignRequestMiddleware, secret_key
    conn.adapter adapter
  end
end