binance-ruby 0.6 → 0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9573431110b2ce6731230f3660f9023dbbea1064bff0c508072a4dc059c90d06
4
- data.tar.gz: 0664dcff66ef95d5bdf96f8feb37d2f47fe00fc606d166df3c4846a63cee64b1
3
+ metadata.gz: e38e2e3ef1f8051d6ddda7a07dfee0e35de604ba8985861ef6041d0841e127b6
4
+ data.tar.gz: 9f147f98929be3d56108cc258c5533e9469ae153c0659faa0319358e63e0636d
5
5
  SHA512:
6
- metadata.gz: 4da7aa59ff6987dff9690848fe48df6774370e194cc10ac1a33692b18dfe9a00c7e39ccda67d13c565c6fd5ae1f15b8f22e9c45e0c4e47be92e9a15740c98fce
7
- data.tar.gz: a9ac599985d66f28656e11b609c50888ecaf93f9450f3cfddc33a6b6ba37913e650f9f98573db4b4e9bd9a50fae2c5c37155ad634c4d871ca62ab27fb7ed0af2
6
+ metadata.gz: 0c40157c4619143f0d83ab5fcfab5b63f1db7a561bbb2b5fa50dd6f2c572c7762be9bc5b81bad9824bdfb345e9ad52e9c0bad8edcb7e27474c52ff4cde473604
7
+ data.tar.gz: ef136e619f4042998197c29dc99a6072036b6169ec27843a97aeade71b7db2006ac287e9fe19d08b135bde867bd045aa788c2127683aaee8e36c039e60021067
data/lib/binance-ruby.rb CHANGED
@@ -7,6 +7,8 @@ require "binance/api/account"
7
7
  require "binance/api/configuration"
8
8
  require "binance/api/data_stream"
9
9
  require "binance/api/error"
10
+ require "binance/api/margin"
11
+ require "binance/api/margin/account"
10
12
  require "binance/api/margin/order"
11
13
  require "binance/api/order"
12
14
  require "binance/api/request"
data/lib/binance/api.rb CHANGED
@@ -2,64 +2,72 @@ module Binance
2
2
  module Api
3
3
  class << self
4
4
  # Valid limits:[5, 10, 20, 50, 100, 500, 1000]
5
- def candlesticks!(endTime: nil, interval: nil, limit: 500, startTime: nil, symbol: nil)
5
+ def candlesticks!(endTime: nil, interval: nil, limit: 500, startTime: nil, symbol: nil, api_key: nil, api_secret_key: nil)
6
6
  raise Error.new(message: "interval is required") unless interval
7
7
  raise Error.new(message: "symbol is required") unless symbol
8
8
  params = { endTime: endTime, interval: interval, limit: limit, startTime: startTime, symbol: symbol }
9
- Request.send!(api_key_type: :read_info, path: "/api/v1/klines", params: params)
9
+ Request.send!(api_key_type: :read_info, path: "/api/v1/klines", params: params,
10
+ api_key: api_key, api_secret_key: api_secret_key)
10
11
  end
11
12
 
12
- def compressed_aggregate_trades!(endTime: nil, fromId: nil, limit: 500, startTime: nil, symbol: nil)
13
+ def compressed_aggregate_trades!(endTime: nil, fromId: nil, limit: 500, startTime: nil, symbol: nil, api_key: nil, api_secret_key: nil)
13
14
  raise Error.new(message: "symbol is required") unless symbol
14
15
  params = {
15
16
  endTime: endTime, fromId: fromId, limit: limit, startTime: startTime, symbol: symbol,
16
17
  }.delete_if { |key, value| value.nil? }
17
- Request.send!(api_key_type: :read_info, path: "/api/v1/aggTrades", params: params)
18
+ Request.send!(api_key_type: :read_info, path: "/api/v1/aggTrades", params: params,
19
+ api_key: api_key, api_secret_key: api_secret_key)
18
20
  end
19
21
 
20
- def depth!(symbol: nil, limit: 100)
22
+ def depth!(symbol: nil, limit: 100, api_key: nil, api_secret_key: nil)
21
23
  raise Error.new(message: "symbol is required") unless symbol
22
24
  params = { limit: limit, symbol: symbol }
23
- Request.send!(api_key_type: :read_info, path: "/api/v1/depth", params: params)
25
+ Request.send!(api_key_type: :read_info, path: "/api/v1/depth", params: params,
26
+ api_key: api_key, api_secret_key: api_secret_key)
24
27
  end
25
28
 
26
- def exchange_info!
27
- Request.send!(api_key_type: :read_info, path: "/api/v1/exchangeInfo")
29
+ def exchange_info!(api_key: nil, api_secret_key: nil)
30
+ Request.send!(api_key_type: :read_info, path: "/api/v1/exchangeInfo",
31
+ api_key: api_key, api_secret_key: api_secret_key)
28
32
  end
29
33
 
30
- def historical_trades!(symbol: nil, limit: 500, fromId: nil)
34
+ def historical_trades!(symbol: nil, limit: 500, fromId: nil, api_key: nil, api_secret_key: nil)
31
35
  raise Error.new(message: "symbol is required") unless symbol
32
36
  params = { fromId: fromId, limit: limit, symbol: symbol }
33
- Request.send!(api_key_type: :read_info, path: "/api/v1/historicalTrades", params: params, security_type: :market_data)
37
+ Request.send!(api_key_type: :read_info, path: "/api/v1/historicalTrades", params: params,
38
+ security_type: :market_data, api_key: api_key, api_secret_key: api_secret_key)
34
39
  end
35
40
 
36
- def info!(recvWindow: nil)
41
+ def info!(recvWindow: nil, api_key: nil, api_secret_key: nil)
37
42
  timestamp = Configuration.timestamp
38
43
  params = { recvWindow: recvWindow, timestamp: timestamp }.delete_if { |key, value| value.nil? }
39
- Request.send!(api_key_type: :read_info, path: "/api/v3/account", params: params, security_type: :user_data)
44
+ Request.send!(api_key_type: :read_info, path: "/api/v3/account", params: params, security_type: :user_data,
45
+ api_key: api_key, api_secret_key: api_secret_key)
40
46
  end
41
47
 
42
48
  def ping!
43
49
  Request.send!(path: "/api/v1/ping")
44
50
  end
45
51
 
46
- def ticker!(symbol: nil, type: nil)
52
+ def ticker!(symbol: nil, type: nil, api_key: nil, api_secret_key: nil)
47
53
  ticker_type = type&.to_sym
48
54
  error_message = "type must be one of: #{ticker_types.join(", ")}. #{type} was provided."
49
55
  raise Error.new(message: error_message) unless ticker_types.include? ticker_type
50
56
  path = ticker_path(type: ticker_type)
51
57
  params = symbol ? { symbol: symbol } : {}
52
- Request.send!(api_key_type: :read_info, path: path, params: params)
58
+ Request.send!(api_key_type: :read_info, path: path, params: params,
59
+ api_key: api_key, api_secret_key: api_secret_key)
53
60
  end
54
61
 
55
62
  def time!
56
63
  Request.send!(path: "/api/v1/time")
57
64
  end
58
65
 
59
- def trades!(symbol: nil, limit: 500)
66
+ def trades!(symbol: nil, limit: 500, api_key: nil, api_secret_key: nil)
60
67
  raise Error.new(message: "symbol is required") unless symbol
61
68
  params = { limit: limit, symbol: symbol }
62
- Request.send!(api_key_type: :read_info, path: "/api/v1/trades", params: params)
69
+ Request.send!(api_key_type: :read_info, path: "/api/v1/trades", params: params,
70
+ api_key: api_key, api_secret_key: api_secret_key)
63
71
  end
64
72
 
65
73
  private
@@ -2,30 +2,30 @@ module Binance
2
2
  module Api
3
3
  class Account
4
4
  class << self
5
- def fees!(recvWindow: 5000)
5
+ def fees!(recvWindow: 5000, api_key: nil, api_secret_key: nil)
6
6
  timestamp = Configuration.timestamp
7
7
  params = { recvWindow: recvWindow, timestamp: timestamp }
8
8
  Request.send!(api_key_type: :read_info, path: "/wapi/v3/assetDetail.html",
9
9
  params: params.delete_if { |key, value| value.nil? },
10
- security_type: :user_data)
10
+ security_type: :user_data, api_key: api_key, api_secret_key: api_secret_key)
11
11
  end
12
12
 
13
- def info!(recvWindow: 5000)
13
+ def info!(recvWindow: 5000, api_key: nil, api_secret_key: nil)
14
14
  timestamp = Configuration.timestamp
15
15
  params = { recvWindow: recvWindow, timestamp: timestamp }
16
16
  Request.send!(api_key_type: :read_info, path: "/api/v3/account",
17
17
  params: params.delete_if { |key, value| value.nil? },
18
- security_type: :user_data)
18
+ security_type: :user_data, api_key: api_key, api_secret_key: api_secret_key)
19
19
  end
20
20
 
21
- def trades!(fromId: nil, limit: 500, recvWindow: 5000, symbol: nil)
21
+ def trades!(fromId: nil, limit: 500, recvWindow: 5000, symbol: nil, api_key: nil, api_secret_key: nil)
22
22
  raise Error.new(message: "max limit is 500") unless limit <= 500
23
23
  raise Error.new(message: "symbol is required") if symbol.nil?
24
24
  timestamp = Configuration.timestamp
25
25
  params = { fromId: fromId, limit: limit, recvWindow: recvWindow, symbol: symbol, timestamp: timestamp }
26
26
  Request.send!(api_key_type: :read_info, path: "/api/v3/myTrades",
27
27
  params: params.delete_if { |key, value| value.nil? },
28
- security_type: :user_data)
28
+ security_type: :user_data, api_key: api_key, api_secret_key: api_secret_key)
29
29
  end
30
30
  end
31
31
  end
@@ -24,11 +24,11 @@ module Binance
24
24
  instance_variable_get("@secret_key") || ENV["BINANCE_SECRET_KEY"]
25
25
  end
26
26
 
27
- def signed_request_signature(payload:)
27
+ def signed_request_signature(payload:, api_secret_key: nil)
28
28
  raise Error.new(message: "environment variable 'BINANCE_SECRET_KEY' is required " \
29
- "for signed requests.") unless secret_key
29
+ "for signed requests.") unless api_secret_key || secret_key
30
30
  digest = OpenSSL::Digest::SHA256.new
31
- OpenSSL::HMAC.hexdigest(digest, secret_key, payload)
31
+ OpenSSL::HMAC.hexdigest(digest, api_secret_key || secret_key, payload)
32
32
  end
33
33
 
34
34
  def timestamp
@@ -3,21 +3,22 @@ module Binance
3
3
  class DataStream
4
4
  class << self
5
5
  # It's recommended to send a ping about every 30 minutes.
6
- def keepalive!(listen_key: nil)
6
+ def keepalive!(listen_key: nil, api_key: nil, api_secret_key: nil)
7
7
  raise Error.new(message: "listen_key is required") if listen_key.nil?
8
8
  Request.send!(api_key_type: :none, method: :put, path: "/api/v1/userDataStream",
9
9
  params: { listenKey: listen_key }, security_type: :user_stream)
10
10
  end
11
11
 
12
- def start!
12
+ def start!(api_key: nil, api_secret_key: nil)
13
13
  Request.send!(api_key_type: :none, method: :post, path: "/api/v1/userDataStream",
14
- security_type: :user_stream)
14
+ security_type: :user_stream, api_key: api_key, api_secret_key: api_secret_key)
15
15
  end
16
16
 
17
- def stop!(listen_key: nil)
17
+ def stop!(listen_key: nil, api_key: nil, api_secret_key: nil)
18
18
  raise Error.new(message: "listen_key is required") if listen_key.nil?
19
19
  Request.send!(api_key_type: :none, method: :delete, path: "/api/v1/userDataStream",
20
- params: { listenKey: listen_key }, security_type: :user_stream)
20
+ params: { listenKey: listen_key }, security_type: :user_stream,
21
+ api_key: api_key, api_secret_key: api_secret_key)
21
22
  end
22
23
  end
23
24
  end
@@ -1,7 +1,7 @@
1
1
  module Binance
2
2
  module Api
3
3
  class Error < StandardError
4
- attr_reader :code, :msg
4
+ attr_reader :code, :msg, :symbol
5
5
 
6
6
  class << self
7
7
  # https://github.com/binance-exchange/binance-official-api-docs/blob/master/errors.md
@@ -10,9 +10,10 @@ module Binance
10
10
  end
11
11
  end
12
12
 
13
- def initialize(code: nil, json: {}, message: nil)
13
+ def initialize(code: nil, json: {}, message: nil, symbol: nil)
14
14
  @code = code || json[:code]
15
15
  @msg = message || json[:msg]
16
+ @symbol = message || json[:symbol]
16
17
  end
17
18
 
18
19
  def inspect
@@ -0,0 +1,45 @@
1
+ module Binance
2
+ module Api
3
+ module Margin
4
+ class << self
5
+ # Your Margin Wallet balance determines the amount of funds you can borrow,
6
+ # following a fixed rate of 5:1 (5x).
7
+ def borrow!(asset: nil, amount: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
8
+ timestamp = Configuration.timestamp
9
+ params = {
10
+ asset: asset, amount: amount, recvWindow: recvWindow, timestamp: timestamp,
11
+ }.delete_if { |_, value| value.nil? }
12
+ ensure_required_keys!(params: params)
13
+ path = "/sapi/v1/margin/loan"
14
+ Request.send!(api_key_type: :trading, method: :post, path: path,
15
+ params: params, security_type: :margin, tld: Configuration.tld,
16
+ api_key: api_key, api_secret_key: api_secret_key)
17
+ end
18
+
19
+ def repay!(asset: nil, isIsolated: nil, amount: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
20
+ timestamp = Configuration.timestamp
21
+ params = {
22
+ asset: asset, amount: amount, recvWindow: recvWindow, timestamp: timestamp,
23
+ }.delete_if { |_, value| value.nil? }
24
+ ensure_required_keys!(params: params)
25
+ path = "/sapi/v1/margin/repay"
26
+ Request.send!(api_key_type: :trading, method: :post, path: path,
27
+ params: params, security_type: :margin, tld: Configuration.tld,
28
+ api_key: api_key, api_secret_key: api_secret_key)
29
+ end
30
+
31
+ private
32
+
33
+ def ensure_required_keys!(params:)
34
+ keys = required_margin_keys.dup
35
+ missing_keys = keys.select { |key| params[key].nil? }
36
+ raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
37
+ end
38
+
39
+ def required_margin_keys
40
+ [:asset, :amount, :timestamp].freeze
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,33 @@
1
+ module Binance
2
+ module Api
3
+ module Margin
4
+ class Account
5
+ class << self
6
+ def transfer!(asset: nil, amount: nil, type: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
7
+ timestamp = Configuration.timestamp
8
+ params = {
9
+ asset: asset, amount: amount, type: type, recvWindow: recvWindow, timestamp: timestamp,
10
+ }.delete_if { |_, value| value.nil? }
11
+ ensure_required_create_keys!(params: params)
12
+ path = "/sapi/v1/margin/transfer"
13
+ Request.send!(api_key_type: :trading, method: :post, path: path,
14
+ params: params, security_type: :margin, tld: Configuration.tld,
15
+ api_key: api_key, api_secret_key: api_secret_key)
16
+ end
17
+
18
+ private
19
+
20
+ def ensure_required_create_keys!(params:)
21
+ keys = required_create_keys.dup
22
+ missing_keys = keys.select { |key| params[key].nil? }
23
+ raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
24
+ end
25
+
26
+ def required_create_keys
27
+ [:asset, :amount, :type, :timestamp].freeze
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -3,10 +3,24 @@ module Binance
3
3
  module Margin
4
4
  class Order
5
5
  class << self
6
+ def cancel!(symbol: nil, isIsolated: false, orderId: nil, origClientOrderId: nil,
7
+ newClientOrderId: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
8
+ timestamp = Configuration.timestamp
9
+ params = {
10
+ symbol: symbol, isIsolated: isIsolated, orderId: orderId, origClientOrderId: origClientOrderId,
11
+ newClientOrderId: newClientOrderId, recvWindow: recvWindow, timestamp: timestamp,
12
+ }.delete_if { |_, value| value.nil? }
13
+ ensure_required_cancel_keys!(params: params)
14
+ path = "/sapi/v1/margin/order"
15
+ Request.send!(api_key_type: :trading, method: :delete, path: path,
16
+ params: params, security_type: :margin, tld: Configuration.tld,
17
+ api_key: api_key, api_secret_key: api_secret_key)
18
+ end
19
+
6
20
  def create!(symbol: nil, isIsolated: false, side: nil, type: nil, quantity: nil,
7
21
  quoteOrderQty: nil, price: nil, stopPrice: nil, newClientOrderId: nil,
8
22
  icebergQty: nil, newOrderRespType: nil, sideEffectType: nil, timeInForce: nil,
9
- recvWindow: nil)
23
+ recvWindow: nil, api_key: nil, api_secret_key: nil)
10
24
  timestamp = Configuration.timestamp
11
25
  params = {
12
26
  symbol: symbol, isIsolated: isIsolated, side: side, type: type,
@@ -18,7 +32,8 @@ module Binance
18
32
  ensure_required_create_keys!(params: params)
19
33
  path = "/sapi/v1/margin/order"
20
34
  Request.send!(api_key_type: :trading, method: :post, path: path,
21
- params: params, security_type: :trade, tld: Configuration.tld)
35
+ params: params, security_type: :margin, tld: Configuration.tld,
36
+ api_key: api_key, api_secret_key: api_secret_key)
22
37
  end
23
38
 
24
39
  private
@@ -47,6 +62,16 @@ module Binance
47
62
  def required_create_keys
48
63
  [:symbol, :side, :type, :timestamp].freeze
49
64
  end
65
+
66
+ def ensure_required_cancel_keys!(params:)
67
+ keys = required_cancel_keys.dup
68
+ missing_keys = keys.select { |key| params[key].nil? }
69
+ raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
70
+ end
71
+
72
+ def required_cancel_keys
73
+ [:symbol, :timestamp].freeze
74
+ end
50
75
  end
51
76
  end
52
77
  end
@@ -2,25 +2,28 @@ module Binance
2
2
  module Api
3
3
  class Order
4
4
  class << self
5
- def all!(limit: 500, orderId: nil, recvWindow: 5000, symbol: nil)
5
+ def all!(limit: 500, orderId: nil, recvWindow: 5000, symbol: nil, api_key: nil, api_secret_key: nil)
6
6
  raise Error.new(message: "max limit is 500") unless limit <= 500
7
7
  raise Error.new(message: "symbol is required") if symbol.nil?
8
8
  timestamp = Configuration.timestamp
9
9
  params = { limit: limit, orderId: orderId, recvWindow: recvWindow, symbol: symbol, timestamp: timestamp }
10
10
  Request.send!(api_key_type: :read_info, path: "/api/v3/allOrders",
11
11
  params: params.delete_if { |key, value| value.nil? },
12
- security_type: :user_data, tld: Configuration.tld)
12
+ security_type: :user_data, tld: Configuration.tld, api_key: api_key,
13
+ api_secret_key: api_secret_key)
13
14
  end
14
15
 
15
16
  # Be careful when accessing without a symbol!
16
- def all_open!(recvWindow: 5000, symbol: nil)
17
+ def all_open!(recvWindow: 5000, symbol: nil, api_key: nil, api_secret_key: nil)
17
18
  timestamp = Configuration.timestamp
18
19
  params = { recvWindow: recvWindow, symbol: symbol, timestamp: timestamp }
19
20
  Request.send!(api_key_type: :read_info, path: "/api/v3/openOrders",
20
- params: params, security_type: :user_data, tld: Configuration.tld)
21
+ params: params, security_type: :user_data, tld: Configuration.tld, api_key: api_key,
22
+ api_secret_key: api_secret_key)
21
23
  end
22
24
 
23
- def cancel!(orderId: nil, originalClientOrderId: nil, newClientOrderId: nil, recvWindow: nil, symbol: nil)
25
+ def cancel!(orderId: nil, originalClientOrderId: nil, newClientOrderId: nil, recvWindow: nil, symbol: nil,
26
+ api_key: nil, api_secret_key: nil)
24
27
  raise Error.new(message: "symbol is required") if symbol.nil?
25
28
  raise Error.new(message: "either orderid or originalclientorderid " \
26
29
  "is required") if orderId.nil? && originalClientOrderId.nil?
@@ -29,12 +32,13 @@ module Binance
29
32
  newClientOrderId: newClientOrderId, recvWindow: recvWindow,
30
33
  symbol: symbol, timestamp: timestamp }.delete_if { |key, value| value.nil? }
31
34
  Request.send!(api_key_type: :trading, method: :delete, path: "/api/v3/order",
32
- params: params, security_type: :trade, tld: Configuration.tld)
35
+ params: params, security_type: :trade, tld: Configuration.tld, api_key: api_key,
36
+ api_secret_key: api_secret_key)
33
37
  end
34
38
 
35
39
  def create!(icebergQuantity: nil, newClientOrderId: nil, newOrderResponseType: nil,
36
40
  price: nil, quantity: nil, recvWindow: nil, stopPrice: nil, symbol: nil,
37
- side: nil, type: nil, timeInForce: nil, test: false)
41
+ side: nil, type: nil, timeInForce: nil, test: false, api_key: nil, api_secret_key: nil)
38
42
  timestamp = Configuration.timestamp
39
43
  params = {
40
44
  icebergQty: icebergQuantity, newClientOrderId: newClientOrderId,
@@ -45,10 +49,12 @@ module Binance
45
49
  ensure_required_create_keys!(params: params)
46
50
  path = "/api/v3/order#{"/test" if test}"
47
51
  Request.send!(api_key_type: :trading, method: :post, path: path,
48
- params: params, security_type: :trade, tld: Configuration.tld)
52
+ params: params, security_type: :trade, tld: Configuration.tld, api_key: api_key,
53
+ api_secret_key: api_secret_key)
49
54
  end
50
55
 
51
- def status!(orderId: nil, originalClientOrderId: nil, recvWindow: nil, symbol: nil)
56
+ def status!(orderId: nil, originalClientOrderId: nil, recvWindow: nil, symbol: nil,
57
+ api_key: nil, api_secret_key: nil)
52
58
  raise Error.new(message: "symbol is required") if symbol.nil?
53
59
  raise Error.new(message: "either orderid or originalclientorderid " \
54
60
  "is required") if orderId.nil? && originalClientOrderId.nil?
@@ -58,7 +64,8 @@ module Binance
58
64
  symbol: symbol, timestamp: timestamp,
59
65
  }.delete_if { |key, value| value.nil? }
60
66
  Request.send!(api_key_type: :trading, path: "/api/v3/order",
61
- params: params, security_type: :user_data, tld: Configuration.tld)
67
+ params: params, security_type: :user_data, tld: Configuration.tld, api_key: api_key,
68
+ api_secret_key: api_secret_key)
62
69
  end
63
70
 
64
71
  private
@@ -3,14 +3,15 @@ module Binance
3
3
  class Request
4
4
  include HTTParty
5
5
  class << self
6
- def send!(api_key_type: :none, headers: {}, method: :get, path: "/", params: {}, security_type: :none, tld: :com)
6
+ def send!(api_key_type: :none, headers: {}, method: :get, path: "/", params: {}, security_type: :none, tld: :com, api_key: nil, api_secret_key: nil)
7
7
  Configuration.validate_tld!(tld)
8
8
  self.base_uri "https://api.binance.#{tld}"
9
9
 
10
10
  raise Error.new(message: "invalid security type #{security_type}") unless security_types.include?(security_type)
11
- all_headers = default_headers(api_key_type: api_key_type, security_type: security_type)
11
+ all_headers = default_headers(api_key_type: api_key_type, security_type: security_type, api_key: api_key)
12
12
  params.delete_if { |k, v| v.nil? }
13
- params.merge!(signature: signed_request_signature(params: params)) if [:trade, :user_data].include?(security_type)
13
+ signature = signed_request_signature(params: params, api_secret_key: api_secret_key)
14
+ params.merge!(signature: signature) if [:trade, :user_data].include?(security_type)
14
15
  # send() is insecure so don't use it.
15
16
  case method
16
17
  when :get
@@ -29,26 +30,31 @@ module Binance
29
30
 
30
31
  private
31
32
 
32
- def default_headers(api_key_type:, security_type:)
33
+ def default_headers(api_key_type:, security_type:, api_key: nil)
33
34
  headers = {}
34
35
  headers["Content-Type"] = "application/json; charset=utf-8"
35
- headers["X-MBX-APIKEY"] = Configuration.api_key(type: api_key_type) unless security_type == :none
36
+ headers["X-MBX-APIKEY"] = (api_key || Configuration.api_key(type: api_key_type)) unless security_type == :none
36
37
  headers
37
38
  end
38
39
 
39
40
  def process!(response:)
40
- json = JSON.parse(response.body, symbolize_names: true)
41
+ json = begin
42
+ JSON.parse(response.body, symbolize_names: true)
43
+ rescue JSON::ParserError => error
44
+ # binance 500 errors are html format
45
+ raise Error.new(message: error)
46
+ end
41
47
  raise Error.new(json: json) if Error.is_error_response?(response: response)
42
48
  json
43
49
  end
44
50
 
45
51
  def security_types
46
- [:none, :trade, :user_data, :user_stream, :market_data].freeze
52
+ [:none, :trade, :user_data, :user_stream, :market_data, :margin].freeze
47
53
  end
48
54
 
49
- def signed_request_signature(params:)
55
+ def signed_request_signature(params:, api_secret_key: nil)
50
56
  payload = params.map { |key, value| "#{key}=#{value}" }.join("&")
51
- Configuration.signed_request_signature(payload: payload)
57
+ Configuration.signed_request_signature(payload: payload, api_secret_key: api_secret_key)
52
58
  end
53
59
  end
54
60
  end
@@ -1,5 +1,5 @@
1
1
  module Binance
2
2
  module Api
3
- VERSION = "0.6"
3
+ VERSION = "0.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binance-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Peterson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-04 00:00:00.000000000 Z
11
+ date: 2021-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov
@@ -177,6 +177,8 @@ files:
177
177
  - lib/binance/api/configuration.rb
178
178
  - lib/binance/api/data_stream.rb
179
179
  - lib/binance/api/error.rb
180
+ - lib/binance/api/margin.rb
181
+ - lib/binance/api/margin/account.rb
180
182
  - lib/binance/api/margin/order.rb
181
183
  - lib/binance/api/order.rb
182
184
  - lib/binance/api/request.rb
@@ -204,5 +206,5 @@ requirements: []
204
206
  rubygems_version: 3.0.8
205
207
  signing_key:
206
208
  specification_version: 4
207
- summary: binance-ruby-0.6
209
+ summary: binance-ruby-0.7
208
210
  test_files: []