Class: Binance::Client::REST_FUTURE

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://fapi.binance.com'.freeze
TEST_BASE_URL =
'https://testnet.binancefuture.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 },
    # #cancel_all_open_orders
    { name: :cancel_all, client: :signed,
    action: :delete, endpoint: :all_open_orders },
    # #cancel_batch_orders
    { name: :cancel_orders, client: :signed,
    action: :delete, endpoint: :batch_orders },
    # #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 },
    # #user_trades
    { name: :user_trades, client: :signed,
    action: :get, endpoint: :user_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 },

    # # margin type ISOLATED|CROSS
    { name: :margin_type, client: :signed,
    action: :post, endpoint: :margin_type },

    # Withdraw API Endpoints
    # # funding_rate
    { name: :funding_rate, client: :signed,
    action: :get, endpoint: :funding_rate },
    # # income
    { name: :income, client: :signed,
    action: :get, endpoint: :income },
    # # leverage
    { name: :leverage, client: :signed,
    action: :post, endpoint: :leverage },
    # listenKey
    { name: :listenKey, client: :signed,
    action: :post, endpoint: :listenKey },
				# # listenKey
    { name: :getlistenKey, client: :signed,
    action: :get, endpoint: :listenKey },
    # # position_risk
    { name: :position_risk, client: :signed,
    action: :get, endpoint: :position_risk },
    # # premium_index
    { name: :premium_index, client: :signed,
    action: :get, endpoint: :premium_index },
				# #withdraw!
    { name: :withdraw!, client: :withdraw,
    action: :post, endpoint: :withdraw },
				# #balance
    { name: :balance, client: :signed,
    action: :get, endpoint: :balance },

    # NOT FOUND IN FUTURE API (yet)
    # #my_trades
    { name: :my_trades, client: :signed,
    action: :get, endpoint: :my_trades },
    # #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:               'v1/ticker/price',
    book_ticker:         'v1/ticker/bookTicker',

    # Account API Endpoints
    order:               'v1/order',
    open_orders:         'v1/openOrders',
    all_orders:          'v1/allOrders',
    account:             'v1/account',
    user_trades:         'v1/userTrades',
    test_order:          'v1/order/test',
    user_data_stream:    'v1/userDataStream',
    withdraw:            'v1/withdraw.html',

    # Withdraw API Endpoints
    balance:             'v1/balance',
    funding_rate:        'v1/fundingRate',
    income:              'v1/income',
    leverage:            'v1/leverage',
    listenKey:           'v1/listenKey',
    position_risk:       'v1/positionRisk',
    premium_index:       'v1/premiumIndex',

    #not found in Future-API yet
    my_trades:           'v1/myTrades',
    deposit_history:     'v1/depositHistory.html',
    withdraw_history:    'v1/withdrawHistory.html',
    deposit_address:     'v1/depositAddress.html',
    account_status:      'v1/accountStatus.html',
    system_status:       'v1/systemStatus.html',
    withdraw_fee:        'v1/withdrawFee.html',
    dust_log:            'v1/userAssetDribbletLog.html'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: '', secret_key: '', adapter: Faraday.default_adapter, test_api: false) ⇒ REST_FUTURE

Returns a new instance of REST_FUTURE.



45
46
47
48
49
50
51
52
53
# File 'lib/binance/client/rest.rb', line 45

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

Class Method Details

.add_query_param(query, key, value) ⇒ Object



65
66
67
68
69
# File 'lib/binance/client/rest.rb', line 65

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



71
72
73
74
# File 'lib/binance/client/rest.rb', line 71

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

#public_client(adapter, test_api) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/binance/client/rest/clients.rb', line 58

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

#public_withdraw_client(adapter, test_api) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/binance/client/rest/clients.rb', line 85

def public_withdraw_client(adapter, test_api)
    Faraday.new(url: "#{(test_api)? TEST_BASE_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, test_api) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/binance/client/rest/clients.rb', line 74

def signed_client(api_key, secret_key, adapter, test_api)
    Faraday.new(url: "#{(test_api)? TEST_BASE_URL : BASE_URL}/fapi") 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, test_api) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/binance/client/rest/clients.rb', line 66

def verified_client(api_key, adapter, test_api)
    Faraday.new(url: "#{(test_api)? TEST_BASE_URL : BASE_URL}/fapi") 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, test_api) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/binance/client/rest/clients.rb', line 93

def withdraw_client(api_key, secret_key, adapter, test_api)
    Faraday.new(url: "#{(test_api)? TEST_BASE_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