binance-ruby 0.6.4 → 1.1.1

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: e74c503800c58d470ef98deb95f32629f9954938dd64945dff998b50651d2733
4
- data.tar.gz: 5a7977d1840a0a710bdbd825934b4fbb7ed991dba686f7c8aa4aa8251a0722aa
3
+ metadata.gz: 6fb6c0237f378f2045f7db5cc48929886a39445e0ecf54afe0bb864df84792c3
4
+ data.tar.gz: 05db158fd488361b4b735b79201265e43b693eeb5d1c723e16cd9f8b0533ef1c
5
5
  SHA512:
6
- metadata.gz: ea8fe1a34c96744a54e1e67aa589e336fb63ca843ce7e7b6c6d65e14e8a3142f9fdf523ff3df311c5ad1bd483ec26bb0dbb874abd5a2054af0dbafc970fce112
7
- data.tar.gz: e39a5fe7430cd9055400eb4886a07e3ce679327af63ba2f03739c100fb9e00b29a344a2a06c7e7aaf357d84480c669072f923c30eb3c5d6c66d9aaa9d3856305
6
+ metadata.gz: 4e18e6e9058a1c885c255eea4e64dd0eb099ecc28b2ef92666e666cad7504d81b12557e9115b27d9232bd68e798511dcf447a6b8dc680d9a6d920b2ba36131cd
7
+ data.tar.gz: e73a00ff633073200642377ddb9555b20864b45ed46e9245501d8e010c50351785344922b9a0c9e7d32fbcac8bb00ded7ce552a43ec8c64984511457b00b2eae
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
@@ -9,9 +9,8 @@ module Binance
9
9
  :trading_api_key, :withdrawals_api_key
10
10
 
11
11
  def api_key(type: nil)
12
- raise Error.new(message: "invalid security_type type: #{type}.") unless type.nil? || api_key_types.include?(type)
13
- instance_api_key(type: type) || ENV["BINANCE_#{type.to_s.humanize.upcase}_API_KEY"] ||
14
- instance_api_key || ENV["BINANCE_API_KEY"]
12
+ raise Error.new(message: "invalid api_key type: #{type}.") unless type.nil? || api_key_types.include?(type)
13
+ instance_api_key(type: type) || ENV["BINANCE_#{type.to_s.humanize.upcase}_API_KEY"] || ENV["BINANCE_API_KEY"]
15
14
  end
16
15
 
17
16
  def tld
@@ -24,11 +23,11 @@ module Binance
24
23
  instance_variable_get("@secret_key") || ENV["BINANCE_SECRET_KEY"]
25
24
  end
26
25
 
27
- def signed_request_signature(payload:)
26
+ def signed_request_signature(payload:, api_secret_key: nil)
28
27
  raise Error.new(message: "environment variable 'BINANCE_SECRET_KEY' is required " \
29
- "for signed requests.") unless secret_key
28
+ "for signed requests.") unless api_secret_key || secret_key
30
29
  digest = OpenSSL::Digest::SHA256.new
31
- OpenSSL::HMAC.hexdigest(digest, secret_key, payload)
30
+ OpenSSL::HMAC.hexdigest(digest, api_secret_key || secret_key, payload)
32
31
  end
33
32
 
34
33
  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
@@ -8,6 +8,53 @@ module Binance
8
8
  def is_error_response?(response:)
9
9
  response.code >= 400
10
10
  end
11
+
12
+ # https://github.com/binance/binance-spot-api-docs/blob/master/errors.md
13
+ def localized(message)
14
+ code = message.to_s.match(/\d+/).to_s.to_i
15
+ case code
16
+ when 1000 then Unknown
17
+ when 1001 then Disconnected
18
+ when 1002 then Unauthorized
19
+ when 1003 then TooManyRequests
20
+ when 1006 then UnexpectedResponse
21
+ when 1007 then Timeout
22
+ when 1014 then UnknownOrderComposition
23
+ when 1015 then TooManyOrders
24
+ when 1016 then ServiceShuttingDown
25
+ when 1020 then UnsupportedOperation
26
+ when 1021 then InvalidTimestamp
27
+ when 1022 then InvalidSignature
28
+ when 1100 then IllegalChars
29
+ when 1101 then TooManyParameters
30
+ when 1102 then MandatoryParamEmptyOrMalformed
31
+ when 1103 then UnknownParam
32
+ when 1104 then UnreadParameters
33
+ when 1105 then ParamEmpty
34
+ when 1106 then ParamNotRequired
35
+ when 1111 then BadPrecision
36
+ when 1112 then NoDepth
37
+ when 1114 then TIFNotRequired
38
+ when 1115 then InvalidTIF
39
+ when 1116 then InvalidOrderType
40
+ when 1117 then InvalidSide
41
+ when 1118 then EmptyNewCLOrderId
42
+ when 1119 then EmptyOrgCLOrderId
43
+ when 1120 then BadInterval
44
+ when 1121 then BadSymbol
45
+ when 1125 then InvalidListenKey
46
+ when 1127 then IntervalTooLarge
47
+ when 1128 then OptionalParamsBadCombo
48
+ when 1130 then InvalidParameter
49
+ when 2010 then NewOrderRejected
50
+ when 2011 then CancelOrderRejected
51
+ when 2013 then NoSuchOrder
52
+ when 2014 then BadAPIKeyFormat
53
+ when 2015 then RejectedAPIKey
54
+ when 2016 then NoTradingWindow
55
+ else Binance::Api::Error
56
+ end
57
+ end
11
58
  end
12
59
 
13
60
  def initialize(code: nil, json: {}, message: nil, symbol: nil)
@@ -19,6 +66,7 @@ module Binance
19
66
  def inspect
20
67
  message = ""
21
68
  message += "(#{code}) " unless code.nil?
69
+ message += "@#{symbol} " unless symbol.nil?
22
70
  message += "#{msg}" unless msg.nil?
23
71
  end
24
72
 
@@ -29,6 +77,46 @@ module Binance
29
77
  def to_s
30
78
  inspect
31
79
  end
80
+
81
+ class Unknown < Error; end
82
+ class Disconnected < Error; end
83
+ class Unauthorized < Error; end
84
+ class TooManyRequests < Error; end
85
+ class UnexpectedResponse < Error; end
86
+ class Timeout < Error; end
87
+ class UnknownOrderComposition < Error; end
88
+ class TooManyOrders < Error; end
89
+ class ServiceShuttingDown < Error; end
90
+ class UnsupportedOperation < Error; end
91
+ class InvalidTimestamp < Error; end
92
+ class InvalidSignature < Error; end
93
+ class IllegalChars < Error; end
94
+ class TooManyParameters < Error; end
95
+ class MandatoryParamEmptyOrMalformed < Error; end
96
+ class UnknownParam < Error; end
97
+ class UnreadParameters < Error; end
98
+ class ParamEmpty < Error; end
99
+ class ParamNotRequired < Error; end
100
+ class BadPrecision < Error; end
101
+ class NoDepth < Error; end
102
+ class TIFNotRequired < Error; end
103
+ class InvalidTIF < Error; end
104
+ class InvalidOrderType < Error; end
105
+ class InvalidSide < Error; end
106
+ class EmptyNewCLOrderId < Error; end
107
+ class EmptyOrgCLOrderId < Error; end
108
+ class BadInterval < Error; end
109
+ class BadSymbol < Error; end
110
+ class InvalidListenKey < Error; end
111
+ class IntervalTooLarge < Error; end
112
+ class OptionalParamsBadCombo < Error; end
113
+ class InvalidParameter < Error; end
114
+ class NewOrderRejected < Error; end
115
+ class CancelOrderRejected < Error; end
116
+ class NoSuchOrder < Error; end
117
+ class BadAPIKeyFormat < Error; end
118
+ class RejectedAPIKey < Error; end
119
+ class NoTradingWindow < Error; end
32
120
  end
33
121
  end
34
122
  end
@@ -4,7 +4,7 @@ module Binance
4
4
  class << self
5
5
  # Your Margin Wallet balance determines the amount of funds you can borrow,
6
6
  # following a fixed rate of 5:1 (5x).
7
- def borrow!(asset: nil, amount: nil, recvWindow: nil)
7
+ def borrow!(asset: nil, amount: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
8
8
  timestamp = Configuration.timestamp
9
9
  params = {
10
10
  asset: asset, amount: amount, recvWindow: recvWindow, timestamp: timestamp,
@@ -12,10 +12,11 @@ module Binance
12
12
  ensure_required_keys!(params: params)
13
13
  path = "/sapi/v1/margin/loan"
14
14
  Request.send!(api_key_type: :trading, method: :post, path: path,
15
- params: params, security_type: :margin, tld: Configuration.tld)
15
+ params: params, security_type: :margin, tld: Configuration.tld,
16
+ api_key: api_key, api_secret_key: api_secret_key)
16
17
  end
17
18
 
18
- def repay!(asset: nil, isIsolated: nil, amount: nil, recvWindow: nil)
19
+ def repay!(asset: nil, isIsolated: nil, amount: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
19
20
  timestamp = Configuration.timestamp
20
21
  params = {
21
22
  asset: asset, amount: amount, recvWindow: recvWindow, timestamp: timestamp,
@@ -23,7 +24,8 @@ module Binance
23
24
  ensure_required_keys!(params: params)
24
25
  path = "/sapi/v1/margin/repay"
25
26
  Request.send!(api_key_type: :trading, method: :post, path: path,
26
- params: params, security_type: :margin, tld: Configuration.tld)
27
+ params: params, security_type: :margin, tld: Configuration.tld,
28
+ api_key: api_key, api_secret_key: api_secret_key)
27
29
  end
28
30
 
29
31
  private
@@ -3,7 +3,7 @@ module Binance
3
3
  module Margin
4
4
  class Account
5
5
  class << self
6
- def transfer!(asset: nil, amount: nil, type: nil, recvWindow: nil)
6
+ def transfer!(asset: nil, amount: nil, type: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
7
7
  timestamp = Configuration.timestamp
8
8
  params = {
9
9
  asset: asset, amount: amount, type: type, recvWindow: recvWindow, timestamp: timestamp,
@@ -11,7 +11,8 @@ module Binance
11
11
  ensure_required_create_keys!(params: params)
12
12
  path = "/sapi/v1/margin/transfer"
13
13
  Request.send!(api_key_type: :trading, method: :post, path: path,
14
- params: params, security_type: :margin, tld: Configuration.tld)
14
+ params: params, security_type: :margin, tld: Configuration.tld,
15
+ api_key: api_key, api_secret_key: api_secret_key)
15
16
  end
16
17
 
17
18
  private
@@ -4,7 +4,7 @@ module Binance
4
4
  class Order
5
5
  class << self
6
6
  def cancel!(symbol: nil, isIsolated: false, orderId: nil, origClientOrderId: nil,
7
- newClientOrderId: nil, recvWindow: nil)
7
+ newClientOrderId: nil, recvWindow: nil, api_key: nil, api_secret_key: nil)
8
8
  timestamp = Configuration.timestamp
9
9
  params = {
10
10
  symbol: symbol, isIsolated: isIsolated, orderId: orderId, origClientOrderId: origClientOrderId,
@@ -13,13 +13,14 @@ module Binance
13
13
  ensure_required_cancel_keys!(params: params)
14
14
  path = "/sapi/v1/margin/order"
15
15
  Request.send!(api_key_type: :trading, method: :delete, path: path,
16
- params: params, security_type: :margin, tld: Configuration.tld)
16
+ params: params, security_type: :margin, tld: Configuration.tld,
17
+ api_key: api_key, api_secret_key: api_secret_key)
17
18
  end
18
19
 
19
20
  def create!(symbol: nil, isIsolated: false, side: nil, type: nil, quantity: nil,
20
21
  quoteOrderQty: nil, price: nil, stopPrice: nil, newClientOrderId: nil,
21
22
  icebergQty: nil, newOrderRespType: nil, sideEffectType: nil, timeInForce: nil,
22
- recvWindow: nil)
23
+ recvWindow: nil, api_key: nil, api_secret_key: nil)
23
24
  timestamp = Configuration.timestamp
24
25
  params = {
25
26
  symbol: symbol, isIsolated: isIsolated, side: side, type: type,
@@ -31,7 +32,8 @@ module Binance
31
32
  ensure_required_create_keys!(params: params)
32
33
  path = "/sapi/v1/margin/order"
33
34
  Request.send!(api_key_type: :trading, method: :post, path: path,
34
- params: params, security_type: :margin, tld: Configuration.tld)
35
+ params: params, security_type: :margin, tld: Configuration.tld,
36
+ api_key: api_key, api_secret_key: api_secret_key)
35
37
  end
36
38
 
37
39
  private
@@ -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,26 +32,29 @@ 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
- price: nil, quantity: nil, recvWindow: nil, stopPrice: nil, symbol: nil,
37
- side: nil, type: nil, timeInForce: nil, test: false)
40
+ price: nil, quantity: nil, quoteOrderQty: nil, recvWindow: nil, stopPrice: nil, symbol: nil,
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,
41
- newOrderRespType: newOrderResponseType, price: price, quantity: quantity,
45
+ newOrderRespType: newOrderResponseType, price: price, quantity: quantity, quoteOrderQty: quoteOrderQty,
42
46
  recvWindow: recvWindow, stopPrice: stopPrice, symbol: symbol, side: side,
43
47
  type: type, timeInForce: timeInForce, timestamp: timestamp,
44
48
  }.delete_if { |key, value| value.nil? }
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
@@ -85,7 +92,7 @@ module Binance
85
92
  end
86
93
 
87
94
  def required_create_keys
88
- [:symbol, :side, :type, :quantity, :timestamp].freeze
95
+ [:symbol, :side, :type, :timestamp].freeze
89
96
  end
90
97
  end
91
98
  end
@@ -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: Configuration.tld, 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,10 +30,10 @@ 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
 
@@ -43,7 +44,7 @@ module Binance
43
44
  # binance 500 errors are html format
44
45
  raise Error.new(message: error)
45
46
  end
46
- raise Error.new(json: json) if Error.is_error_response?(response: response)
47
+ raise Error.localized(json[:code]).new(json: json) if Error.is_error_response?(response: response)
47
48
  json
48
49
  end
49
50
 
@@ -51,9 +52,9 @@ module Binance
51
52
  [:none, :trade, :user_data, :user_stream, :market_data, :margin].freeze
52
53
  end
53
54
 
54
- def signed_request_signature(params:)
55
+ def signed_request_signature(params:, api_secret_key: nil)
55
56
  payload = params.map { |key, value| "#{key}=#{value}" }.join("&")
56
- Configuration.signed_request_signature(payload: payload)
57
+ Configuration.signed_request_signature(payload: payload, api_secret_key: api_secret_key)
57
58
  end
58
59
  end
59
60
  end
@@ -1,5 +1,5 @@
1
1
  module Binance
2
2
  module Api
3
- VERSION = "0.6.4"
3
+ VERSION = "1.1.1"
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
4
+ version: 1.1.1
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-30 00:00:00.000000000 Z
11
+ date: 2021-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov
@@ -203,8 +203,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
203
  - !ruby/object:Gem::Version
204
204
  version: '0'
205
205
  requirements: []
206
- rubygems_version: 3.0.8
206
+ rubygems_version: 3.1.6
207
207
  signing_key:
208
208
  specification_version: 4
209
- summary: binance-ruby-0.6.4
209
+ summary: binance-ruby-1.1.1
210
210
  test_files: []