binance-ruby 0.4 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/binance-ruby.rb +11 -10
- data/lib/binance/api.rb +15 -15
- data/lib/binance/api/configuration.rb +23 -12
- data/lib/binance/api/margin/order.rb +54 -0
- data/lib/binance/api/order.rb +8 -8
- data/lib/binance/api/request.rb +8 -10
- data/lib/binance/api/version.rb +1 -1
- metadata +32 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbd02bd36bb807895e727ed07261b63c4f8863862dd9016723b69e47d7033856
|
4
|
+
data.tar.gz: 64c57054923d757d07cbec602af8a6cc687a8646f65c8c62c63691e872a4e325
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01de466a3ba9a8811493029f1eef4ee059f0934ea165854b842c2d655e78fcc3677806ea6fd562142143b8af51afa338fd0f98c5875696f33b75c57efd25b288
|
7
|
+
data.tar.gz: cf17dfb48360a3a3ee089fb1461f50660f63344666dab63e3a2e42eb575a28a3bad121499795bae01f1e4d300deb385ed8c5128046a4be42cd3580ca0a8a37c5
|
data/lib/binance-ruby.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "active_support/core_ext/string"
|
2
|
+
require "awrence"
|
3
|
+
require "httparty"
|
4
4
|
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
5
|
+
require "binance/api"
|
6
|
+
require "binance/api/account"
|
7
|
+
require "binance/api/configuration"
|
8
|
+
require "binance/api/data_stream"
|
9
|
+
require "binance/api/error"
|
10
|
+
require "binance/api/margin/order"
|
11
|
+
require "binance/api/order"
|
12
|
+
require "binance/api/request"
|
12
13
|
require "binance/api/version"
|
data/lib/binance/api.rb
CHANGED
@@ -3,49 +3,49 @@ module Binance
|
|
3
3
|
class << self
|
4
4
|
# Valid limits:[5, 10, 20, 50, 100, 500, 1000]
|
5
5
|
def candlesticks!(endTime: nil, interval: nil, limit: 500, startTime: nil, symbol: nil)
|
6
|
-
raise Error.new(message:
|
7
|
-
raise Error.new(message:
|
6
|
+
raise Error.new(message: "interval is required") unless interval
|
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:
|
9
|
+
Request.send!(api_key_type: :read_info, path: "/api/v1/klines", params: params)
|
10
10
|
end
|
11
11
|
|
12
12
|
def compressed_aggregate_trades!(endTime: nil, fromId: nil, limit: 500, startTime: nil, symbol: nil)
|
13
13
|
raise Error.new(message: "symbol is required") unless symbol
|
14
14
|
params = {
|
15
|
-
endTime: endTime, fromId: fromId, limit: limit, startTime: startTime, symbol: symbol
|
15
|
+
endTime: endTime, fromId: fromId, limit: limit, startTime: startTime, symbol: symbol,
|
16
16
|
}.delete_if { |key, value| value.nil? }
|
17
|
-
Request.send!(api_key_type: :read_info, path:
|
17
|
+
Request.send!(api_key_type: :read_info, path: "/api/v1/aggTrades", params: params)
|
18
18
|
end
|
19
19
|
|
20
20
|
def depth!(symbol: nil, limit: 100)
|
21
21
|
raise Error.new(message: "symbol is required") unless symbol
|
22
22
|
params = { limit: limit, symbol: symbol }
|
23
|
-
Request.send!(api_key_type: :read_info, path:
|
23
|
+
Request.send!(api_key_type: :read_info, path: "/api/v1/depth", params: params)
|
24
24
|
end
|
25
25
|
|
26
26
|
def exchange_info!
|
27
|
-
Request.send!(api_key_type: :read_info, path:
|
27
|
+
Request.send!(api_key_type: :read_info, path: "/api/v1/exchangeInfo")
|
28
28
|
end
|
29
29
|
|
30
30
|
def historical_trades!(symbol: nil, limit: 500, fromId: nil)
|
31
31
|
raise Error.new(message: "symbol is required") unless symbol
|
32
32
|
params = { fromId: fromId, limit: limit, symbol: symbol }
|
33
|
-
Request.send!(api_key_type: :read_info, path:
|
33
|
+
Request.send!(api_key_type: :read_info, path: "/api/v1/historicalTrades", params: params, security_type: :market_data)
|
34
34
|
end
|
35
35
|
|
36
36
|
def info!(recvWindow: nil)
|
37
37
|
timestamp = Configuration.timestamp
|
38
38
|
params = { recvWindow: recvWindow, timestamp: timestamp }.delete_if { |key, value| value.nil? }
|
39
|
-
Request.send!(api_key_type: :read_info, path:
|
39
|
+
Request.send!(api_key_type: :read_info, path: "/api/v3/account", params: params, security_type: :user_data)
|
40
40
|
end
|
41
41
|
|
42
42
|
def ping!
|
43
|
-
Request.send!(path:
|
43
|
+
Request.send!(path: "/api/v1/ping")
|
44
44
|
end
|
45
45
|
|
46
46
|
def ticker!(symbol: nil, type: nil)
|
47
47
|
ticker_type = type&.to_sym
|
48
|
-
error_message = "type must be one of: #{ticker_types.join(
|
48
|
+
error_message = "type must be one of: #{ticker_types.join(", ")}. #{type} was provided."
|
49
49
|
raise Error.new(message: error_message) unless ticker_types.include? ticker_type
|
50
50
|
path = ticker_path(type: ticker_type)
|
51
51
|
params = symbol ? { symbol: symbol } : {}
|
@@ -53,13 +53,13 @@ module Binance
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def time!
|
56
|
-
Request.send!(path:
|
56
|
+
Request.send!(path: "/api/v1/time")
|
57
57
|
end
|
58
58
|
|
59
59
|
def trades!(symbol: nil, limit: 500)
|
60
60
|
raise Error.new(message: "symbol is required") unless symbol
|
61
61
|
params = { limit: limit, symbol: symbol }
|
62
|
-
Request.send!(api_key_type: :read_info, path:
|
62
|
+
Request.send!(api_key_type: :read_info, path: "/api/v1/trades", params: params)
|
63
63
|
end
|
64
64
|
|
65
65
|
private
|
@@ -67,8 +67,8 @@ module Binance
|
|
67
67
|
def ticker_path(type:)
|
68
68
|
case type
|
69
69
|
when :daily
|
70
|
-
|
71
|
-
when :price, :bookTicker
|
70
|
+
"/api/v1/ticker/24hr"
|
71
|
+
when :price, :bookTicker, :avgPrice
|
72
72
|
"/api/v3/ticker/#{type.to_s.camelize(:lower)}"
|
73
73
|
end
|
74
74
|
end
|
@@ -1,43 +1,54 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "openssl"
|
2
|
+
require "base64"
|
3
3
|
|
4
4
|
module Binance
|
5
5
|
module Api
|
6
6
|
class Configuration
|
7
7
|
class << self
|
8
|
-
attr_writer :api_key, :
|
9
|
-
|
10
|
-
|
8
|
+
attr_writer :api_key, :locale, :read_info_api_key, :secret_key,
|
9
|
+
:trading_api_key, :withdrawals_api_key
|
10
|
+
|
11
11
|
def api_key(type: nil)
|
12
12
|
raise Error.new(message: "invalid security_type type: #{type}.") unless type.nil? || api_key_types.include?(type)
|
13
13
|
instance_api_key(type: type) || ENV["BINANCE_#{type.to_s.humanize.upcase}_API_KEY"] ||
|
14
|
-
instance_api_key
|
14
|
+
instance_api_key || ENV["BINANCE_API_KEY"]
|
15
|
+
end
|
16
|
+
|
17
|
+
def tld
|
18
|
+
tld = ENV["BINANCE_TLD"]&.downcase&.to_sym || :com
|
19
|
+
error_message = "Invalid tld (top-level-domain): #{tld}. Use one of: #{allowed_tlds.join(", ")}."
|
20
|
+
raise Error.new(message: error_message) unless allowed_tlds.include?(tld)
|
21
|
+
tld
|
15
22
|
end
|
16
|
-
|
23
|
+
|
17
24
|
def secret_key
|
18
|
-
instance_variable_get("@secret_key") || ENV[
|
25
|
+
instance_variable_get("@secret_key") || ENV["BINANCE_SECRET_KEY"]
|
19
26
|
end
|
20
27
|
|
21
28
|
def signed_request_signature(payload:)
|
22
29
|
raise Error.new(message: "environment variable 'BINANCE_SECRET_KEY' is required " \
|
23
|
-
|
30
|
+
"for signed requests.") unless secret_key
|
24
31
|
digest = OpenSSL::Digest::SHA256.new
|
25
32
|
OpenSSL::HMAC.hexdigest(digest, secret_key, payload)
|
26
33
|
end
|
27
34
|
|
28
35
|
def timestamp
|
29
|
-
Time.now.utc.strftime(
|
36
|
+
Time.now.utc.strftime("%s%3N")
|
30
37
|
end
|
31
38
|
|
32
39
|
private
|
33
40
|
|
41
|
+
def allowed_tlds
|
42
|
+
[:com, :us]
|
43
|
+
end
|
44
|
+
|
34
45
|
def api_key_types
|
35
46
|
[:none, :read_info, :trading, :withdrawals].freeze
|
36
47
|
end
|
37
48
|
|
38
49
|
def instance_api_key(type: nil)
|
39
|
-
var = "#{type.to_s.downcase}_api_key".sub(/^\_/,
|
40
|
-
instance_variable_get(
|
50
|
+
var = "#{type.to_s.downcase}_api_key".sub(/^\_/, "")
|
51
|
+
instance_variable_get("@" + var)
|
41
52
|
end
|
42
53
|
end
|
43
54
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Binance
|
2
|
+
module Api
|
3
|
+
module Margin
|
4
|
+
class Order
|
5
|
+
class << self
|
6
|
+
def create!(symbol: nil, isIsolated: false, side: nil, type: nil, quantity: nil,
|
7
|
+
quoteOrderQty: nil, price: nil, stopPrice: nil, newClientOrderId: nil,
|
8
|
+
icebergQty: nil, newOrderRespType: nil, sideEffectType: nil, timeInForce: nil,
|
9
|
+
recvWindow: nil)
|
10
|
+
timestamp = Configuration.timestamp
|
11
|
+
params = {
|
12
|
+
symbol: symbol, isIsolated: isIsolated, side: side, type: type,
|
13
|
+
quantity: quantity, quoteOrderQty: quoteOrderQty, price: price,
|
14
|
+
stopPrice: stopPrice, newClientOrderId: newClientOrderId, icebergQty: icebergQty,
|
15
|
+
newOrderRespType: newOrderRespType, sideEffectType: sideEffectType,
|
16
|
+
timeInForce: timeInForce, recvWindow: recvWindow, timestamp: timestamp,
|
17
|
+
}.delete_if { |_, value| value.nil? }
|
18
|
+
ensure_required_create_keys!(params: params)
|
19
|
+
path = "/sapi/v1/margin/order"
|
20
|
+
Request.send!(api_key_type: :trading, method: :post, path: path,
|
21
|
+
params: params, security_type: :trade)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def additional_required_create_keys(type:)
|
27
|
+
case type
|
28
|
+
when :limit
|
29
|
+
[:price, :timeInForce].freeze
|
30
|
+
when :stop_loss, :take_profit
|
31
|
+
[:stopPrice].freeze
|
32
|
+
when :stop_loss_limit, :take_profit_limit
|
33
|
+
[:price, :stopPrice, :timeInForce].freeze
|
34
|
+
when :limit_maker
|
35
|
+
[:price].freeze
|
36
|
+
else
|
37
|
+
[].freeze
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def ensure_required_create_keys!(params:)
|
42
|
+
keys = required_create_keys.dup.concat(additional_required_create_keys(type: params[:type]))
|
43
|
+
missing_keys = keys.select { |key| params[key].nil? }
|
44
|
+
raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
|
45
|
+
end
|
46
|
+
|
47
|
+
def required_create_keys
|
48
|
+
[:symbol, :side, :type, :timestamp].freeze
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/binance/api/order.rb
CHANGED
@@ -16,14 +16,14 @@ module Binance
|
|
16
16
|
def all_open!(recvWindow: 5000, symbol: nil)
|
17
17
|
timestamp = Configuration.timestamp
|
18
18
|
params = { recvWindow: recvWindow, symbol: symbol, timestamp: timestamp }
|
19
|
-
Request.send!(api_key_type: :read_info, path:
|
19
|
+
Request.send!(api_key_type: :read_info, path: "/api/v3/openOrders",
|
20
20
|
params: params, security_type: :user_data)
|
21
21
|
end
|
22
22
|
|
23
23
|
def cancel!(orderId: nil, originalClientOrderId: nil, newClientOrderId: nil, recvWindow: nil, symbol: nil)
|
24
24
|
raise Error.new(message: "symbol is required") if symbol.nil?
|
25
25
|
raise Error.new(message: "either orderid or originalclientorderid " \
|
26
|
-
|
26
|
+
"is required") if orderId.nil? && originalClientOrderId.nil?
|
27
27
|
timestamp = Configuration.timestamp
|
28
28
|
params = { orderId: orderId, origClientOrderId: originalClientOrderId,
|
29
29
|
newClientOrderId: newClientOrderId, recvWindow: recvWindow,
|
@@ -36,14 +36,14 @@ module Binance
|
|
36
36
|
price: nil, quantity: nil, recvWindow: nil, stopPrice: nil, symbol: nil,
|
37
37
|
side: nil, type: nil, timeInForce: nil, test: false)
|
38
38
|
timestamp = Configuration.timestamp
|
39
|
-
params = {
|
39
|
+
params = {
|
40
40
|
icebergQty: icebergQuantity, newClientOrderId: newClientOrderId,
|
41
41
|
newOrderRespType: newOrderResponseType, price: price, quantity: quantity,
|
42
42
|
recvWindow: recvWindow, stopPrice: stopPrice, symbol: symbol, side: side,
|
43
|
-
type: type, timeInForce: timeInForce, timestamp: timestamp
|
43
|
+
type: type, timeInForce: timeInForce, timestamp: timestamp,
|
44
44
|
}.delete_if { |key, value| value.nil? }
|
45
45
|
ensure_required_create_keys!(params: params)
|
46
|
-
path = "/api/v3/order#{
|
46
|
+
path = "/api/v3/order#{"/test" if test}"
|
47
47
|
Request.send!(api_key_type: :trading, method: :post, path: path,
|
48
48
|
params: params, security_type: :trade)
|
49
49
|
end
|
@@ -51,11 +51,11 @@ module Binance
|
|
51
51
|
def status!(orderId: nil, originalClientOrderId: nil, recvWindow: nil, symbol: nil)
|
52
52
|
raise Error.new(message: "symbol is required") if symbol.nil?
|
53
53
|
raise Error.new(message: "either orderid or originalclientorderid " \
|
54
|
-
|
54
|
+
"is required") if orderId.nil? && originalClientOrderId.nil?
|
55
55
|
timestamp = Configuration.timestamp
|
56
56
|
params = {
|
57
57
|
orderId: orderId, origClientOrderId: originalClientOrderId, recvWindow: recvWindow,
|
58
|
-
symbol: symbol, timestamp: timestamp
|
58
|
+
symbol: symbol, timestamp: timestamp,
|
59
59
|
}.delete_if { |key, value| value.nil? }
|
60
60
|
Request.send!(api_key_type: :trading, path: "/api/v3/order",
|
61
61
|
params: params, security_type: :user_data)
|
@@ -81,7 +81,7 @@ module Binance
|
|
81
81
|
def ensure_required_create_keys!(params:)
|
82
82
|
keys = required_create_keys.dup.concat(additional_required_create_keys(type: params[:type]))
|
83
83
|
missing_keys = keys.select { |key| params[key].nil? }
|
84
|
-
raise Error.new(message: "required keys are missing: #{missing_keys.join(
|
84
|
+
raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
|
85
85
|
end
|
86
86
|
|
87
87
|
def required_create_keys
|
data/lib/binance/api/request.rb
CHANGED
@@ -2,16 +2,14 @@ module Binance
|
|
2
2
|
module Api
|
3
3
|
class Request
|
4
4
|
include HTTParty
|
5
|
-
|
6
|
-
base_uri 'https://api.binance.com'
|
7
|
-
|
8
5
|
class << self
|
9
|
-
def send!(api_key_type: :none, headers: {}, method: :get, path:
|
6
|
+
def send!(api_key_type: :none, headers: {}, method: :get, path: "/", params: {}, security_type: :none)
|
7
|
+
self.base_uri "https://api.binance.#{Configuration.tld}"
|
8
|
+
|
10
9
|
raise Error.new(message: "invalid security type #{security_type}") unless security_types.include?(security_type)
|
11
10
|
all_headers = default_headers(api_key_type: api_key_type, security_type: security_type)
|
12
11
|
params.delete_if { |k, v| v.nil? }
|
13
|
-
params.merge!(signature: signed_request_signature(params: params))
|
14
|
-
if [:trade, :user_data].include?(security_type)
|
12
|
+
params.merge!(signature: signed_request_signature(params: params)) if [:trade, :user_data].include?(security_type)
|
15
13
|
# send() is insecure so don't use it.
|
16
14
|
case method
|
17
15
|
when :get
|
@@ -25,15 +23,15 @@ module Binance
|
|
25
23
|
else
|
26
24
|
raise Error.new(message: "invalid http method used: #{method}")
|
27
25
|
end
|
28
|
-
process!(response: response ||
|
26
|
+
process!(response: response || "{}")
|
29
27
|
end
|
30
28
|
|
31
29
|
private
|
32
30
|
|
33
31
|
def default_headers(api_key_type:, security_type:)
|
34
32
|
headers = {}
|
35
|
-
headers[
|
36
|
-
headers[
|
33
|
+
headers["Content-Type"] = "application/json; charset=utf-8"
|
34
|
+
headers["X-MBX-APIKEY"] = Configuration.api_key(type: api_key_type) unless security_type == :none
|
37
35
|
headers
|
38
36
|
end
|
39
37
|
|
@@ -48,7 +46,7 @@ module Binance
|
|
48
46
|
end
|
49
47
|
|
50
48
|
def signed_request_signature(params:)
|
51
|
-
payload = params.map { |key, value| "#{key}=#{value}" }.join(
|
49
|
+
payload = params.map { |key, value| "#{key}=#{value}" }.join("&")
|
52
50
|
Configuration.signed_request_signature(payload: payload)
|
53
51
|
end
|
54
52
|
end
|
data/lib/binance/api/version.rb
CHANGED
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.
|
4
|
+
version: '0.5'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jake Peterson
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codecov
|
@@ -109,19 +109,39 @@ dependencies:
|
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: 0.11.3
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
112
|
+
name: simplecov
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
115
|
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
118
|
-
type: :
|
117
|
+
version: 0.20.0
|
118
|
+
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
124
|
+
version: 0.20.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: activesupport
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 5.1.0
|
132
|
+
- - "<"
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: 7.0.0
|
135
|
+
type: :runtime
|
136
|
+
prerelease: false
|
137
|
+
version_requirements: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: 5.1.0
|
142
|
+
- - "<"
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: 7.0.0
|
125
145
|
- !ruby/object:Gem::Dependency
|
126
146
|
name: awrence
|
127
147
|
requirement: !ruby/object:Gem::Requirement
|
@@ -163,6 +183,7 @@ files:
|
|
163
183
|
- lib/binance/api/configuration.rb
|
164
184
|
- lib/binance/api/data_stream.rb
|
165
185
|
- lib/binance/api/error.rb
|
186
|
+
- lib/binance/api/margin/order.rb
|
166
187
|
- lib/binance/api/order.rb
|
167
188
|
- lib/binance/api/request.rb
|
168
189
|
- lib/binance/api/version.rb
|
@@ -171,7 +192,7 @@ licenses:
|
|
171
192
|
- MIT
|
172
193
|
metadata:
|
173
194
|
allowed_push_host: https://rubygems.org
|
174
|
-
post_install_message:
|
195
|
+
post_install_message:
|
175
196
|
rdoc_options: []
|
176
197
|
require_paths:
|
177
198
|
- lib
|
@@ -186,8 +207,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
186
207
|
- !ruby/object:Gem::Version
|
187
208
|
version: '0'
|
188
209
|
requirements: []
|
189
|
-
rubygems_version: 3.0.
|
190
|
-
signing_key:
|
210
|
+
rubygems_version: 3.0.8
|
211
|
+
signing_key:
|
191
212
|
specification_version: 4
|
192
|
-
summary: binance-ruby-0.
|
213
|
+
summary: binance-ruby-0.5
|
193
214
|
test_files: []
|