Skip to content
This repository was archived by the owner on Feb 23, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,35 @@ EM.run do
end
```

#### REST Future Client

Create a new instance of the [REST Future Client](http://www.rubydoc.info/gems/binance):

```ruby
# If you only plan on touching public API endpoints, you can forgo any arguments
client = Binance::Client::REST_FUTURE.new
# Otherwise provide an api_key and secret_key as keyword arguments
client = Binance::Client::REST_FUTURE.new api_key: 'x', secret_key: 'y'
```

Example of order book of BTCUSDT futures and current balance:

```ruby
puts "Future depth:"
puts client.depth(symbol: 'BTCUSDT', limit: '5').inspect
puts "Future Balance:"
puts client.balance.inspect
```

#### WebSocket Future Client

Create a new instance of the [WebSocket Future Client](http://www.rubydoc.info/gems/binance):

```ruby
client = Binance::Client::WebSocketFuture.new
```


## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
33 changes: 32 additions & 1 deletion lib/binance/client/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,39 @@
module Binance
module Client
class REST
BASE_URL = 'https://api.binance.com'.freeze
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

METHODS.each do |method|
define_method(method[:name]) do |options = {}|
response = @clients[method[:client]].send(method[:action]) do |req|
req.url ENDPOINTS[method[:endpoint]]
req.params.merge! options.map { |k, v| [camelize(k.to_s), v] }.to_h
end
response.body
end
end

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

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

class REST_FUTURE
def initialize(api_key: '', secret_key: '',
adapter: Faraday.default_adapter)
@clients = {}
Expand Down
52 changes: 52 additions & 0 deletions lib/binance/client/rest/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Binance
module Client
class REST
BASE_URL = 'https://api.binance.com'.freeze

def public_client(adapter)
Faraday.new(url: "#{BASE_URL}/api") do |conn|
conn.request :json
Expand Down Expand Up @@ -49,5 +51,55 @@ def withdraw_client(api_key, secret_key, adapter)
end
end
end

class REST_FUTURE
BASE_URL = 'https://fapi.binance.com'.freeze
def public_client(adapter)
Faraday.new(url: "#{BASE_URL}/fapi") do |conn|
conn.request :json
conn.response :json, content_type: /\bjson$/
conn.adapter adapter
end
end

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

def signed_client(api_key, secret_key, adapter)
Faraday.new(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

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

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
end

end
end
116 changes: 82 additions & 34 deletions lib/binance/client/rest/endpoints.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,87 @@
module Binance
module Client
class REST
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',
module Client
class REST
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',
# 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
balance: 'v1/balance',
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
end

class REST_FUTURE
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
end

# 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
end
end
end
Loading