0.0
No release in over a year
API Wrapper for the Binance cryptocurrency exchange.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 10.0
~> 3.0

Runtime

 Project Readme

Binance

Gem Version

This is a fork from gem binance and based on [craysiii/binance#37 Request 37). It is an unofficial Ruby wrapper for the Binance exchange REST and WebSocket APIs.

Installation

Add this line to your application's Gemfile:

gem 'marcel-binance'

And then execute:

$ bundle

Or install it yourself as:

$ gem install marcel-binance

Features

Current

  • Basic implementation of REST API
    • Easy to use authentication
    • Methods return parsed JSON
    • No need to generate timestamps
    • No need to generate signatures
  • Basic implementation of WebSocket API
    • Pass procs or lambdas to event handlers
    • Single and multiple streams supported
    • Runs on EventMachine
  • Binance Futures

Planned

  • Bug fixes... Example: some Endpoint do not demand parameters. These are broken, because the code assumes parameters are mandatory.
  • Exception handling with responses
  • High level abstraction
  • Documentation and examples

Getting Started

REST Client

Require Binance:

require 'marcel-binance'

Create a new instance of the REST Client:

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

Create various requests:

# Ping the server
client.ping # => {}

# Get kline data
client.klines symbol: 'NEOETH', interval: '1m', limit: 1
# => [[1511682480000, "0.08230000", "0.08230000", "0.08230000", "0.08230000", "0.00000000", 
# 1511682539999, "0.00000000", 0, "0.00000000", "0.00000000", "2885926.46000000"]]

# Create an order
client.create_order! symbol: 'XRPETH', side: 'BUY', type: 'LIMIT', 
  time_in_force: 'GTC', quantity: '100.00000000', price: '0.00055000'
# => {"symbol"=>"XRPETH", "orderId"=>918248, "clientOrderId"=>"kmUU0i6cMWzq1NElE6ZTdu", 
# "transactTime"=>1511685028420, "price"=>"0.00055000", "origQty"=>"100.00000000", 
# "executedQty"=>"100.00000000", "status"=>"FILLED", "timeInForce"=>"GTC", "type"=>"LIMIT", 
# "side"=>"BUY"}

# Get deposit address
client.deposit_address asset: 'NEO'
# => {"address"=>"AHXeTWYv8qZQhQ2WNrBza9LHyzdZtFnbaT", "success"=>true, "addressTag"=>"", "asset"=>"NEO"}

Required and optional parameters, as well as enum values, can currently be found on the Binance GitHub Page. Parameters should always be passed to client methods as keyword arguments in snake_case form.

WebSocket Client

Require Binance and EventMachine:

require 'marcel-binance'
require 'eventmachine'

Create a new instance of the WebSocket Client:

client = Binance::Client::WebSocket.new

Create various WebSocket streams, wrapping calls inside EM.run:

EM.run do
  # Create event handlers
  open    = proc { puts 'connected' }
  message = proc { |e| puts e.data }
  error   = proc { |e| puts e }
  close   = proc { puts 'closed' }

  # Bundle our event handlers into Hash
  methods = { open: open, message: message, error: error, close: close }

  # Pass a symbol and event handler Hash to connect and process events
  client.agg_trade symbol: 'XRPETH', methods: methods
  
  # kline takes an additional named parameter
  client.kline symbol: 'XRPETH', interval: '1m', methods: methods

  # As well as partial_book_depth
  client.partial_book_depth symbol: 'XRPETH', level: '5', methods: methods

  # Create a custom stream
  client.single stream: { type: 'aggTrade', symbol: 'XRPETH'}, methods: methods

  # Create multiple streams in one call
  client.multi streams: [{ type: 'aggTrade', symbol: 'XRPETH' },
                         { type: 'ticker', symbol: 'XRPETH' },
                         { type: 'kline', symbol: 'XRPETH', interval: '1m'},
                         { type: 'depth', symbol: 'XRPETH', level: '5'}],
               methods: methods 
end

User Data Stream

User data streams utilize both the REST and WebSocket APIs.

Require Binance and EventMachine:

require 'marcel-binance'
require 'eventmachine'

Create a new instance of the REST Client and WebSocket Client:

rest  = Binance::Client::REST.new api_key: 'x'
ws    = Binance::Client::WebSocket.new

Request a listen key from the REST API, and then create a WebSocket stream using it.

listen_key = rest.listen_key['listenKey']

message = proc { |e| puts e.data }

EM.run do
  ws.user_data listen_key: listen_key, methods: {message: message}
end

REST Future Client

Create a new instance of the REST Future Client:

# 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:

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:

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/craysiii/binance.

License

The gem is available as open source under the terms of the MIT License.