binance-ruby 0.5 → 0.6.4

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: bbd02bd36bb807895e727ed07261b63c4f8863862dd9016723b69e47d7033856
4
- data.tar.gz: 64c57054923d757d07cbec602af8a6cc687a8646f65c8c62c63691e872a4e325
3
+ metadata.gz: e74c503800c58d470ef98deb95f32629f9954938dd64945dff998b50651d2733
4
+ data.tar.gz: 5a7977d1840a0a710bdbd825934b4fbb7ed991dba686f7c8aa4aa8251a0722aa
5
5
  SHA512:
6
- metadata.gz: 01de466a3ba9a8811493029f1eef4ee059f0934ea165854b842c2d655e78fcc3677806ea6fd562142143b8af51afa338fd0f98c5875696f33b75c57efd25b288
7
- data.tar.gz: cf17dfb48360a3a3ee089fb1461f50660f63344666dab63e3a2e42eb575a28a3bad121499795bae01f1e4d300deb385ed8c5128046a4be42cd3580ca0a8a37c5
6
+ metadata.gz: ea8fe1a34c96744a54e1e67aa589e336fb63ca843ce7e7b6c6d65e14e8a3142f9fdf523ff3df311c5ad1bd483ec26bb0dbb874abd5a2054af0dbafc970fce112
7
+ data.tar.gz: e39a5fe7430cd9055400eb4886a07e3ce679327af63ba2f03739c100fb9e00b29a344a2a06c7e7aaf357d84480c669072f923c30eb3c5d6c66d9aaa9d3856305
@@ -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"
@@ -16,8 +16,7 @@ module Binance
16
16
 
17
17
  def tld
18
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)
19
+ validate_tld!(tld)
21
20
  tld
22
21
  end
23
22
 
@@ -36,6 +35,11 @@ module Binance
36
35
  Time.now.utc.strftime("%s%3N")
37
36
  end
38
37
 
38
+ def validate_tld!(tld)
39
+ error_message = "Invalid tld (top-level-domain): #{tld}. Use one of: #{allowed_tlds.join(", ")}."
40
+ raise Error.new(message: error_message) unless allowed_tlds.include?(tld&.to_sym)
41
+ end
42
+
39
43
  private
40
44
 
41
45
  def allowed_tlds
@@ -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,43 @@
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)
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
+ end
17
+
18
+ def repay!(asset: nil, isIsolated: nil, amount: nil, recvWindow: nil)
19
+ timestamp = Configuration.timestamp
20
+ params = {
21
+ asset: asset, amount: amount, recvWindow: recvWindow, timestamp: timestamp,
22
+ }.delete_if { |_, value| value.nil? }
23
+ ensure_required_keys!(params: params)
24
+ path = "/sapi/v1/margin/repay"
25
+ Request.send!(api_key_type: :trading, method: :post, path: path,
26
+ params: params, security_type: :margin, tld: Configuration.tld)
27
+ end
28
+
29
+ private
30
+
31
+ def ensure_required_keys!(params:)
32
+ keys = required_margin_keys.dup
33
+ missing_keys = keys.select { |key| params[key].nil? }
34
+ raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
35
+ end
36
+
37
+ def required_margin_keys
38
+ [:asset, :amount, :timestamp].freeze
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,32 @@
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)
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
+ end
16
+
17
+ private
18
+
19
+ def ensure_required_create_keys!(params:)
20
+ keys = required_create_keys.dup
21
+ missing_keys = keys.select { |key| params[key].nil? }
22
+ raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
23
+ end
24
+
25
+ def required_create_keys
26
+ [:asset, :amount, :type, :timestamp].freeze
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -3,6 +3,19 @@ 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)
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
+ end
18
+
6
19
  def create!(symbol: nil, isIsolated: false, side: nil, type: nil, quantity: nil,
7
20
  quoteOrderQty: nil, price: nil, stopPrice: nil, newClientOrderId: nil,
8
21
  icebergQty: nil, newOrderRespType: nil, sideEffectType: nil, timeInForce: nil,
@@ -18,7 +31,7 @@ module Binance
18
31
  ensure_required_create_keys!(params: params)
19
32
  path = "/sapi/v1/margin/order"
20
33
  Request.send!(api_key_type: :trading, method: :post, path: path,
21
- params: params, security_type: :trade)
34
+ params: params, security_type: :margin, tld: Configuration.tld)
22
35
  end
23
36
 
24
37
  private
@@ -47,6 +60,16 @@ module Binance
47
60
  def required_create_keys
48
61
  [:symbol, :side, :type, :timestamp].freeze
49
62
  end
63
+
64
+ def ensure_required_cancel_keys!(params:)
65
+ keys = required_cancel_keys.dup
66
+ missing_keys = keys.select { |key| params[key].nil? }
67
+ raise Error.new(message: "required keys are missing: #{missing_keys.join(", ")}") unless missing_keys.empty?
68
+ end
69
+
70
+ def required_cancel_keys
71
+ [:symbol, :timestamp].freeze
72
+ end
50
73
  end
51
74
  end
52
75
  end
@@ -9,7 +9,7 @@ module Binance
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)
12
+ security_type: :user_data, tld: Configuration.tld)
13
13
  end
14
14
 
15
15
  # Be careful when accessing without a symbol!
@@ -17,7 +17,7 @@ module Binance
17
17
  timestamp = Configuration.timestamp
18
18
  params = { recvWindow: recvWindow, symbol: symbol, timestamp: timestamp }
19
19
  Request.send!(api_key_type: :read_info, path: "/api/v3/openOrders",
20
- params: params, security_type: :user_data)
20
+ params: params, security_type: :user_data, tld: Configuration.tld)
21
21
  end
22
22
 
23
23
  def cancel!(orderId: nil, originalClientOrderId: nil, newClientOrderId: nil, recvWindow: nil, symbol: nil)
@@ -29,7 +29,7 @@ module Binance
29
29
  newClientOrderId: newClientOrderId, recvWindow: recvWindow,
30
30
  symbol: symbol, timestamp: timestamp }.delete_if { |key, value| value.nil? }
31
31
  Request.send!(api_key_type: :trading, method: :delete, path: "/api/v3/order",
32
- params: params, security_type: :trade)
32
+ params: params, security_type: :trade, tld: Configuration.tld)
33
33
  end
34
34
 
35
35
  def create!(icebergQuantity: nil, newClientOrderId: nil, newOrderResponseType: nil,
@@ -45,7 +45,7 @@ module Binance
45
45
  ensure_required_create_keys!(params: params)
46
46
  path = "/api/v3/order#{"/test" if test}"
47
47
  Request.send!(api_key_type: :trading, method: :post, path: path,
48
- params: params, security_type: :trade)
48
+ params: params, security_type: :trade, tld: Configuration.tld)
49
49
  end
50
50
 
51
51
  def status!(orderId: nil, originalClientOrderId: nil, recvWindow: nil, symbol: nil)
@@ -58,7 +58,7 @@ module Binance
58
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
- params: params, security_type: :user_data)
61
+ params: params, security_type: :user_data, tld: Configuration.tld)
62
62
  end
63
63
 
64
64
  private
@@ -3,8 +3,9 @@ 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)
7
- self.base_uri "https://api.binance.#{Configuration.tld}"
6
+ def send!(api_key_type: :none, headers: {}, method: :get, path: "/", params: {}, security_type: :none, tld: :com)
7
+ Configuration.validate_tld!(tld)
8
+ self.base_uri "https://api.binance.#{tld}"
8
9
 
9
10
  raise Error.new(message: "invalid security type #{security_type}") unless security_types.include?(security_type)
10
11
  all_headers = default_headers(api_key_type: api_key_type, security_type: security_type)
@@ -36,13 +37,18 @@ module Binance
36
37
  end
37
38
 
38
39
  def process!(response:)
39
- json = JSON.parse(response.body, symbolize_names: true)
40
+ json = begin
41
+ JSON.parse(response.body, symbolize_names: true)
42
+ rescue JSON::ParserError => error
43
+ # binance 500 errors are html format
44
+ raise Error.new(message: error)
45
+ end
40
46
  raise Error.new(json: json) if Error.is_error_response?(response: response)
41
47
  json
42
48
  end
43
49
 
44
50
  def security_types
45
- [:none, :trade, :user_data, :user_stream, :market_data].freeze
51
+ [:none, :trade, :user_data, :user_stream, :market_data, :margin].freeze
46
52
  end
47
53
 
48
54
  def signed_request_signature(params:)
@@ -1,5 +1,5 @@
1
1
  module Binance
2
2
  module Api
3
- VERSION = "0.5"
3
+ VERSION = "0.6.4"
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.5'
4
+ version: 0.6.4
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-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov
@@ -129,9 +129,6 @@ dependencies:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: 5.1.0
132
- - - "<"
133
- - !ruby/object:Gem::Version
134
- version: 7.0.0
135
132
  type: :runtime
136
133
  prerelease: false
137
134
  version_requirements: !ruby/object:Gem::Requirement
@@ -139,9 +136,6 @@ dependencies:
139
136
  - - ">="
140
137
  - !ruby/object:Gem::Version
141
138
  version: 5.1.0
142
- - - "<"
143
- - !ruby/object:Gem::Version
144
- version: 7.0.0
145
139
  - !ruby/object:Gem::Dependency
146
140
  name: awrence
147
141
  requirement: !ruby/object:Gem::Requirement
@@ -183,6 +177,8 @@ files:
183
177
  - lib/binance/api/configuration.rb
184
178
  - lib/binance/api/data_stream.rb
185
179
  - lib/binance/api/error.rb
180
+ - lib/binance/api/margin.rb
181
+ - lib/binance/api/margin/account.rb
186
182
  - lib/binance/api/margin/order.rb
187
183
  - lib/binance/api/order.rb
188
184
  - lib/binance/api/request.rb
@@ -210,5 +206,5 @@ requirements: []
210
206
  rubygems_version: 3.0.8
211
207
  signing_key:
212
208
  specification_version: 4
213
- summary: binance-ruby-0.5
209
+ summary: binance-ruby-0.6.4
214
210
  test_files: []