binance_client 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b8e52b3c3199ac128f970071e84b73cc25781a92f2206170b1513d523321631
4
- data.tar.gz: 81f4c9e55772be4cb4ba350c764ebca822d66d541cb560a6477fe5e6ae0bd91e
3
+ metadata.gz: 65dba4f920f7c9e017d74cee2c972ae54af6f731faa17c6eb15c9602061e0bb7
4
+ data.tar.gz: 0aa2e07fac3321d91513d0ab3904e80f09351b538507d7b915cf20145166f13f
5
5
  SHA512:
6
- metadata.gz: bfb4049818b8846b1e2bcb1913ff473b9d9a9c782aab2a7dfc9b7b6cd262a2144aa17530fc2aeccccd4314df0a77f074c5b9ffa5afad615ab269b645edcb9e09
7
- data.tar.gz: 55054e667ceb9219fdb4098dcbd14257126d8568a463eb8fbe28692d0b545587ce30a1d1372f30141b688b5ad06402e88469f6b484079f91c028b6884b958069
6
+ metadata.gz: e72bc48d33af094253cbbe395f9bf8f6d67343a7bcc8cf912e69740e4733c4161f7a1d06fc4755f1ff236f214b8026c1f896b360b99204e9338b18c02e95b906
7
+ data.tar.gz: 43f1d01d8a1ac0ab5fd540f13933e4d5cd17d34f063ab1dc924372a0a94e74599c4672fbeb411e6378c97f86182eb61a46a8ee3285c1838f98ce11d17b3706cd
data/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.2.0] - 2021-11-26
8
+ ### Added
9
+ - Allow easy access to the `used_weight` header in a response by calling `#used_weight(interval)`
10
+ - `#sub_account_assets` to get the assets of a sub-account
11
+ - `#body_code` to return the value of "code" in the body, if it exists
12
+ - `#message` to return the value of "message" in the body, if it exists
13
+
7
14
  ## [1.1.0]
8
15
  ### Added
9
16
  - `order_book_depth` (`/api/v3/depth`) to get a snapshot of the order book
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- binance_client (1.1.0)
4
+ binance_client (1.2.0)
5
5
  activesupport
6
- api_client_base
6
+ api_client_base (~> 1.11)
7
7
  typhoeus
8
8
 
9
9
  GEM
@@ -17,7 +17,7 @@ GEM
17
17
  zeitwerk (~> 2.3)
18
18
  addressable (2.7.0)
19
19
  public_suffix (>= 2.0.2, < 5.0)
20
- api_client_base (1.10.0)
20
+ api_client_base (1.11.0)
21
21
  activesupport (>= 3.0)
22
22
  gem_config (>= 0.3.1)
23
23
  virtus (>= 1.0)
@@ -35,12 +35,12 @@ GEM
35
35
  descendants_tracker (0.0.4)
36
36
  thread_safe (~> 0.3, >= 0.3.1)
37
37
  diff-lcs (1.4.4)
38
- ethon (0.14.0)
38
+ ethon (0.15.0)
39
39
  ffi (>= 1.15.0)
40
40
  ffi (1.15.4)
41
41
  gem_config (0.3.2)
42
42
  hashdiff (1.0.1)
43
- i18n (1.8.10)
43
+ i18n (1.8.11)
44
44
  concurrent-ruby (~> 1.0)
45
45
  ice_nine (0.11.2)
46
46
  method_source (1.0.0)
@@ -84,7 +84,7 @@ GEM
84
84
  addressable (>= 2.3.6)
85
85
  crack (>= 0.3.2)
86
86
  hashdiff (>= 0.4.0, < 2.0.0)
87
- zeitwerk (2.4.2)
87
+ zeitwerk (2.5.1)
88
88
 
89
89
  PLATFORMS
90
90
  ruby
@@ -99,4 +99,4 @@ DEPENDENCIES
99
99
  webmock
100
100
 
101
101
  BUNDLED WITH
102
- 2.2.24
102
+ 2.2.28
data/README.md CHANGED
@@ -42,6 +42,27 @@ client.order_book_depth(symbol: "BTCUSDT", limit: 100)
42
42
  ## Responses
43
43
  The default representation of response data is a JSON hash
44
44
 
45
+ ## Hooks
46
+ You can set a hook to do application-specific things per request. This is useful when monitoring the rate limits:
47
+
48
+ ```ruby
49
+ BinanceClient.after_request = DoSomethingAfterBinanceRequest
50
+ ```
51
+
52
+ What you assign can be a proc -- it just needs to respond to `call` and accept the `BinanceClient` response object:
53
+
54
+ ```ruby
55
+ class DoSomethingAfterBinanceRequest
56
+
57
+ def self.call(response)
58
+ one_minute_weight = response.used_weight("1m")
59
+ if one_minute_weight > 1200
60
+ Rails.logger.info "Looks like we've hit the request limit!"
61
+ end
62
+ end
63
+
64
+ end
65
+ ```
45
66
 
46
67
  ## Development
47
68
  Edit the `config.yml.sample` with your own credentials for testing
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
27
 
28
- spec.add_dependency "api_client_base"
28
+ spec.add_dependency "api_client_base", "~> 1.11"
29
29
  spec.add_dependency "activesupport"
30
30
  spec.add_dependency "typhoeus"
31
31
 
@@ -7,6 +7,7 @@ module BinanceClient
7
7
  api_action :get_all
8
8
  api_action :book_ticker
9
9
  api_action :order_book_depth
10
+ api_action :sub_account_assets, args: [:email]
10
11
 
11
12
  attribute :host
12
13
  attribute :api_key
@@ -0,0 +1,21 @@
1
+ module BinanceClient
2
+ class AssetBalance
3
+
4
+ attr_reader :asset
5
+
6
+ def initialize(asset:, free:, locked:)
7
+ @asset = asset
8
+ @free = free
9
+ @locked = locked
10
+ end
11
+
12
+ def free
13
+ @free.to_d
14
+ end
15
+
16
+ def locked
17
+ @locked.to_d
18
+ end
19
+
20
+ end
21
+ end
@@ -1,28 +1,14 @@
1
1
  module BinanceClient
2
- class AccountSnapshotRequest < BaseRequest
2
+ class AccountSnapshotRequest < AuthenticatedBaseRequest
3
3
  private
4
4
 
5
5
  def path
6
6
  "/sapi/v1/accountSnapshot"
7
7
  end
8
8
 
9
- def params
10
- type = "SPOT"
11
- limit = 30
12
- query = query(type: type, limit: limit)
13
-
14
- [
15
- query,
16
- "signature=#{signature(query)}"
17
- ].join("&")
9
+ def params_without_signature
10
+ { type: "SPOT", limit: 30 }
18
11
  end
19
12
 
20
- def query(options={})
21
- [
22
- "type=#{options[:type]}",
23
- "timestamp=#{timestamp}",
24
- "limit=#{options[:limit]}",
25
- ].join("&")
26
- end
27
13
  end
28
14
  end
@@ -0,0 +1,34 @@
1
+ module BinanceClient
2
+ class AuthenticatedBaseRequest < BaseRequest
3
+ include APIClientBase::Request.module
4
+
5
+ def signature
6
+ OpenSSL::HMAC.hexdigest(
7
+ OpenSSL::Digest.new("sha256"),
8
+ api_secret,
9
+ params_without_signature_with_timestamp.to_query,
10
+ )
11
+ end
12
+
13
+ def signature_hash
14
+ { signature: signature }
15
+ end
16
+
17
+ def timestamp
18
+ @timestamp ||= DateTime.now.strftime("%Q")
19
+ end
20
+
21
+ def params
22
+ params_without_signature_with_timestamp.merge(signature_hash).to_query
23
+ end
24
+
25
+ def params_without_signature_with_timestamp
26
+ params_without_signature.merge(timestamp: timestamp)
27
+ end
28
+
29
+ def params_without_signature
30
+ {}
31
+ end
32
+
33
+ end
34
+ end
@@ -4,7 +4,6 @@ module BinanceClient
4
4
 
5
5
  attribute :api_key, String
6
6
  attribute :api_secret, String
7
- attribute :timestamp, Integer, lazy: true, default: :default_timestamp
8
7
 
9
8
  def headers
10
9
  {
@@ -13,16 +12,5 @@ module BinanceClient
13
12
  }
14
13
  end
15
14
 
16
- def signature(query)
17
- OpenSSL::HMAC.hexdigest(
18
- OpenSSL::Digest.new("sha256"),
19
- api_secret,
20
- query,
21
- )
22
- end
23
-
24
- def default_timestamp
25
- DateTime.now.strftime("%Q")
26
- end
27
15
  end
28
16
  end
@@ -1,25 +1,10 @@
1
1
  module BinanceClient
2
- class GetAllRequest < BaseRequest
2
+ class GetAllRequest < AuthenticatedBaseRequest
3
3
  private
4
4
 
5
5
  def path
6
6
  "/sapi/v1/capital/config/getall"
7
7
  end
8
8
 
9
- def query
10
- uri = Addressable::URI.new(query_values: {
11
- timestamp: timestamp,
12
- })
13
- uri.normalized_query
14
- end
15
-
16
- def params
17
- uri = Addressable::URI.new(query: query)
18
- uri.query_values = [
19
- uri.query,
20
- ["signature", signature(query)],
21
- ]
22
- uri.normalized_query
23
- end
24
9
  end
25
10
  end
@@ -0,0 +1,15 @@
1
+ module BinanceClient
2
+ class SubAccountAssetsRequest < AuthenticatedBaseRequest
3
+ attribute :email
4
+
5
+ private
6
+
7
+ def path
8
+ "/sapi/v3/sub-account/assets"
9
+ end
10
+
11
+ def params_without_signature
12
+ {email: email}
13
+ end
14
+ end
15
+ end
@@ -2,12 +2,29 @@ module BinanceClient
2
2
  class BaseResponse
3
3
  include APIClientBase::Response.module
4
4
 
5
- attribute :body, Object, default: :default_body
5
+ attribute :body, Object, lazy: true, default: :default_body
6
+
7
+ def used_weight(interval)
8
+ val = header("X-MBX-USED-WEIGHT-#{interval}")
9
+ return nil if val.nil?
10
+ val.to_i
11
+ end
12
+
13
+ def message
14
+ body["message"]
15
+ end
16
+
17
+ def body_code
18
+ val = body["code"]
19
+ return nil if val.nil?
20
+ val.to_i
21
+ end
6
22
 
7
23
  private
8
24
 
9
25
  def default_body
10
26
  JSON.parse(raw_response.body)
11
27
  end
28
+
12
29
  end
13
30
  end
@@ -0,0 +1,16 @@
1
+ module BinanceClient
2
+ class SubAccountAssetsResponse < BaseResponse
3
+
4
+ attribute(:balances, Array[BinanceClient::AssetBalance], {
5
+ lazy: true,
6
+ default: :default_balances,
7
+ })
8
+
9
+ def default_balances
10
+ body["balances"].map do |balance_hash|
11
+ AssetBalance.new(**balance_hash.symbolize_keys)
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module BinanceClient
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -10,19 +10,23 @@ require "binance_client/client"
10
10
  require "binance_client/models/base_model"
11
11
  require "binance_client/models/order_book_entry"
12
12
  require "binance_client/models/order_book"
13
+ require "binance_client/models/asset_balance"
13
14
 
14
15
  require "binance_client/requests/base_request"
16
+ require "binance_client/requests/authenticated_base_request"
15
17
  require "binance_client/responses/base_response"
16
18
  require "binance_client/requests/system_status_request"
17
19
  require "binance_client/requests/account_snapshot_request"
18
20
  require "binance_client/requests/get_all_request"
19
21
  require "binance_client/requests/book_ticker_request"
20
22
  require "binance_client/requests/order_book_depth_request"
23
+ require "binance_client/requests/sub_account_assets_request"
21
24
  require "binance_client/responses/system_status_response"
22
25
  require "binance_client/responses/account_snapshot_response"
23
26
  require "binance_client/responses/get_all_response"
24
27
  require "binance_client/responses/book_ticker_response"
25
28
  require "binance_client/responses/order_book_depth_response"
29
+ require "binance_client/responses/sub_account_assets_response"
26
30
 
27
31
  module BinanceClient
28
32
  class Error < StandardError; end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binance_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - AJ Villalobos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-07 00:00:00.000000000 Z
11
+ date: 2021-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: api_client_base
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.11'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1.11'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -116,20 +116,24 @@ files:
116
116
  - binance_client.gemspec
117
117
  - lib/binance_client.rb
118
118
  - lib/binance_client/client.rb
119
+ - lib/binance_client/models/asset_balance.rb
119
120
  - lib/binance_client/models/base_model.rb
120
121
  - lib/binance_client/models/order_book.rb
121
122
  - lib/binance_client/models/order_book_entry.rb
122
123
  - lib/binance_client/requests/account_snapshot_request.rb
124
+ - lib/binance_client/requests/authenticated_base_request.rb
123
125
  - lib/binance_client/requests/base_request.rb
124
126
  - lib/binance_client/requests/book_ticker_request.rb
125
127
  - lib/binance_client/requests/get_all_request.rb
126
128
  - lib/binance_client/requests/order_book_depth_request.rb
129
+ - lib/binance_client/requests/sub_account_assets_request.rb
127
130
  - lib/binance_client/requests/system_status_request.rb
128
131
  - lib/binance_client/responses/account_snapshot_response.rb
129
132
  - lib/binance_client/responses/base_response.rb
130
133
  - lib/binance_client/responses/book_ticker_response.rb
131
134
  - lib/binance_client/responses/get_all_response.rb
132
135
  - lib/binance_client/responses/order_book_depth_response.rb
136
+ - lib/binance_client/responses/sub_account_assets_response.rb
133
137
  - lib/binance_client/responses/system_status_response.rb
134
138
  - lib/binance_client/version.rb
135
139
  homepage: https://github.com/bloom-solutions/binance_client-ruby