binance_client 1.1.0 → 2.0.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: 16763512a190393e8a071e5e0840de77416ec615e91edf8f86cc1b897ca40c4b
4
+ data.tar.gz: e30373d8442ad67cd59a43bd5e4c1c97b8bf93724d2a1d0820d02363ae7387d1
5
5
  SHA512:
6
- metadata.gz: bfb4049818b8846b1e2bcb1913ff473b9d9a9c782aab2a7dfc9b7b6cd262a2144aa17530fc2aeccccd4314df0a77f074c5b9ffa5afad615ab269b645edcb9e09
7
- data.tar.gz: 55054e667ceb9219fdb4098dcbd14257126d8568a463eb8fbe28692d0b545587ce30a1d1372f30141b688b5ad06402e88469f6b484079f91c028b6884b958069
6
+ metadata.gz: a92884dc017a7a2ae3b868acd953eb7c89e95f08e537176e7298fff4112f5acb6c4582f73156fb7b4a99b9c7523b0e57a5219a423e4903ea38052f1a03ba8186
7
+ data.tar.gz: cf6a0272acc771014477ed8946a293c688a5a5679814f4f96d24cdf09fbd605843d0bfce113a2ae819f4d969f85e5331f758b72299fccbd08f32bb1c57f26672
data/CHANGELOG.md CHANGED
@@ -4,6 +4,33 @@ 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.5.0]
8
+ ### Changed
9
+ - Remove `used_weight` from responses
10
+
11
+ ### Added
12
+ - `recv_window` configuration to set `recvWindow`
13
+ - `#account` call to get the `/api/v3/account` info
14
+
15
+ ### Added
16
+ - `used_weights` to responses
17
+
18
+ ## [1.4.0] - 2021-12-09
19
+ ### Added
20
+ - Add factories for use in app development
21
+ - Add `#sub_account_deposit_address`
22
+
23
+ ## [1.3.0] - 2021-11-29
24
+ ### Added
25
+ - Add proxy support
26
+
27
+ ## [1.2.0] - 2021-11-26
28
+ ### Added
29
+ - Allow easy access to the `used_weight` header in a response by calling `#used_weight(interval)`
30
+ - `#sub_account_assets` to get the assets of a sub-account
31
+ - `#body_code` to return the value of "code" in the body, if it exists
32
+ - `#message` to return the value of "message" in the body, if it exists
33
+
7
34
  ## [1.1.0]
8
35
  ### Added
9
36
  - `order_book_depth` (`/api/v3/depth`) to get a snapshot of the order book
data/Gemfile CHANGED
@@ -3,6 +3,7 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in binance_client.gemspec
4
4
  gemspec
5
5
 
6
+ gem "factory_bot"
6
7
  gem "rake", "~> 12.0"
7
8
  gem "rspec", "~> 3.0"
8
9
  gem "pry-byebug"
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 (2.0.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,14 @@ 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
+ factory_bot (6.2.0)
41
+ activesupport (>= 5.0.0)
40
42
  ffi (1.15.4)
41
43
  gem_config (0.3.2)
42
44
  hashdiff (1.0.1)
43
- i18n (1.8.10)
45
+ i18n (1.8.11)
44
46
  concurrent-ruby (~> 1.0)
45
47
  ice_nine (0.11.2)
46
48
  method_source (1.0.0)
@@ -84,13 +86,14 @@ GEM
84
86
  addressable (>= 2.3.6)
85
87
  crack (>= 0.3.2)
86
88
  hashdiff (>= 0.4.0, < 2.0.0)
87
- zeitwerk (2.4.2)
89
+ zeitwerk (2.5.1)
88
90
 
89
91
  PLATFORMS
90
92
  ruby
91
93
 
92
94
  DEPENDENCIES
93
95
  binance_client!
96
+ factory_bot
94
97
  pry-byebug
95
98
  rake (~> 12.0)
96
99
  rspec (~> 3.0)
@@ -99,4 +102,4 @@ DEPENDENCIES
99
102
  webmock
100
103
 
101
104
  BUNDLED WITH
102
- 2.2.24
105
+ 2.2.28
data/README.md CHANGED
@@ -42,10 +42,35 @@ 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.configuration.after_response = DoSomethingAfterBinanceResponse
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 DoSomethingAfterBinanceResponse
56
+
57
+ def self.call(request, 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
48
69
 
70
+ ## Factories
71
+
72
+ To make testing easier in your app, `require "binance_client/factories"` to get access to factories of the models.
73
+
49
74
  ## Contributing
50
75
 
51
76
  Bug reports and pull requests are welcome on GitHub at https://github.com/bloom-solutions/binance_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/bloom-solutions/binance_client/blob/master/CODE_OF_CONDUCT.md).
@@ -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
 
@@ -2,20 +2,31 @@ module BinanceClient
2
2
  class Client
3
3
  include APIClientBase::Client.module(default_opts: :default_opts)
4
4
 
5
+ api_action :account
5
6
  api_action :system_status
6
7
  api_action :account_snapshot
7
8
  api_action :get_all
8
9
  api_action :book_ticker
9
10
  api_action :order_book_depth
11
+ api_action :sub_account_assets, args: [:email]
12
+ api_action :sub_account_deposit_address, args: [:email, :coin]
10
13
 
11
14
  attribute :host
12
15
  attribute :api_key
13
16
  attribute :api_secret
17
+ attribute :proxy
18
+ attribute :recv_window
14
19
 
15
20
  private
16
21
 
17
22
  def default_opts
18
- {host: host, api_key: api_key, api_secret: api_secret}
23
+ {
24
+ host: host,
25
+ api_key: api_key,
26
+ api_secret: api_secret,
27
+ proxy: proxy,
28
+ recv_window: recv_window,
29
+ }
19
30
  end
20
31
  end
21
32
  end
@@ -0,0 +1,13 @@
1
+ FactoryBot.define do
2
+
3
+ factory :binance_client_asset_balance, class: "BinanceClient::AssetBalance" do
4
+ sequence(:asset) { |n| "ASSET#{n}" }
5
+ free { rand(10_000) }
6
+ locked { rand(1_000) }
7
+
8
+ initialize_with do
9
+ new(asset: asset, free: free, locked: locked)
10
+ end
11
+ end
12
+
13
+ end
@@ -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
@@ -0,0 +1,28 @@
1
+ module BinanceClient
2
+ class DepositAddress
3
+
4
+ def self.new_from_raw(raw_hash)
5
+ args = raw_hash.each_with_object({}) do |(k, v), h|
6
+ h[k.underscore] = v
7
+ end
8
+
9
+ self.new(**args)
10
+ end
11
+
12
+ ATTRS = [
13
+ :address,
14
+ :coin,
15
+ :tag,
16
+ :url,
17
+ ]
18
+
19
+ attr_accessor(*ATTRS)
20
+
21
+ def initialize(**kwargs)
22
+ kwargs.each do |attr, value|
23
+ self.send("#{attr}=", value)
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ module BinanceClient
2
+ class AccountRequest < AuthenticatedBaseRequest
3
+ private
4
+
5
+ def path
6
+ "/api/v3/account"
7
+ end
8
+
9
+ end
10
+ 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,39 @@
1
+ module BinanceClient
2
+ class AuthenticatedBaseRequest < BaseRequest
3
+ include APIClientBase::Request.module
4
+
5
+ attribute :recv_window, Integer
6
+
7
+ def signature
8
+ OpenSSL::HMAC.hexdigest(
9
+ OpenSSL::Digest.new("sha256"),
10
+ api_secret,
11
+ params_without_signature_with_timestamp_and_recv_window.to_query,
12
+ )
13
+ end
14
+
15
+ def signature_hash
16
+ { signature: signature }
17
+ end
18
+
19
+ def timestamp
20
+ @timestamp ||= DateTime.now.strftime("%Q")
21
+ end
22
+
23
+ def params
24
+ params_without_signature_with_timestamp_and_recv_window.
25
+ merge(signature_hash).to_query
26
+ end
27
+
28
+ def params_without_signature_with_timestamp_and_recv_window
29
+ attrs_to_merge = { timestamp: timestamp }
30
+ attrs_to_merge[:recvWindow] = recv_window if recv_window.present?
31
+ params_without_signature.merge(attrs_to_merge)
32
+ end
33
+
34
+ def params_without_signature
35
+ {}
36
+ end
37
+
38
+ end
39
+ 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
@@ -0,0 +1,25 @@
1
+ module BinanceClient
2
+ class SubAccountDepositAddressRequest < AuthenticatedBaseRequest
3
+
4
+ attribute :email, String
5
+ attribute :coin, String
6
+ attribute :network, String
7
+
8
+ private
9
+
10
+ def path
11
+ "/sapi/v1/capital/deposit/subAddress"
12
+ end
13
+
14
+ def params_without_signature
15
+ {
16
+ email: email,
17
+ coin: coin,
18
+ network: network,
19
+ }.reject do |key, value|
20
+ value.nil?
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ module BinanceClient
2
+ class AccountResponse < BaseResponse
3
+
4
+ [
5
+ :maker_commission,
6
+ :taker_commission,
7
+ :buyer_commission,
8
+ :seller_commission,
9
+ :can_trade,
10
+ :can_withdraw,
11
+ :can_deposit,
12
+ :update_time,
13
+ :account_type,
14
+ :permissions,
15
+ ].each do |method_name|
16
+ define_method method_name do
17
+ key = method_name.to_s.camelcase(:lower)
18
+ body[key]
19
+ end
20
+ end
21
+
22
+ def balances
23
+ body["balances"].map do |balance_hash|
24
+ AssetBalance.new(**balance_hash.symbolize_keys)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -2,12 +2,31 @@ 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_weights
8
+ @used_weights ||= headers.each_with_object({}) do |(key, value), hash|
9
+ next if not key.include?("USED")
10
+ next if not key.include?("WEIGHT")
11
+ hash[key] = value
12
+ end
13
+ end
14
+
15
+ def message
16
+ body["message"]
17
+ end
18
+
19
+ def body_code
20
+ val = body["code"]
21
+ return nil if val.nil?
22
+ val.to_i
23
+ end
6
24
 
7
25
  private
8
26
 
9
27
  def default_body
10
28
  JSON.parse(raw_response.body)
11
29
  end
30
+
12
31
  end
13
32
  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
@@ -0,0 +1,14 @@
1
+ module BinanceClient
2
+ class SubAccountDepositAddressResponse < BaseResponse
3
+
4
+ def deposit_address
5
+ DepositAddress.new(
6
+ address: body["address"],
7
+ coin: body["coin"],
8
+ tag: body["tag"],
9
+ url: body["url"],
10
+ )
11
+ end
12
+
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module BinanceClient
2
- VERSION = "1.1.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -2,6 +2,7 @@ require "binance_client/version"
2
2
  require "api_client_base"
3
3
  require "active_support/core_ext/hash/indifferent_access"
4
4
  require "active_support/core_ext/object/to_query"
5
+ require "active_support/core_ext/string/inflections"
5
6
  require "json"
6
7
  require "openssl"
7
8
 
@@ -10,19 +11,28 @@ require "binance_client/client"
10
11
  require "binance_client/models/base_model"
11
12
  require "binance_client/models/order_book_entry"
12
13
  require "binance_client/models/order_book"
14
+ require "binance_client/models/asset_balance"
15
+ require "binance_client/models/deposit_address"
13
16
 
14
17
  require "binance_client/requests/base_request"
18
+ require "binance_client/requests/authenticated_base_request"
15
19
  require "binance_client/responses/base_response"
16
20
  require "binance_client/requests/system_status_request"
17
21
  require "binance_client/requests/account_snapshot_request"
18
22
  require "binance_client/requests/get_all_request"
19
23
  require "binance_client/requests/book_ticker_request"
20
24
  require "binance_client/requests/order_book_depth_request"
25
+ require "binance_client/requests/sub_account_assets_request"
26
+ require "binance_client/requests/sub_account_deposit_address_request"
27
+ require "binance_client/requests/account_request"
21
28
  require "binance_client/responses/system_status_response"
22
29
  require "binance_client/responses/account_snapshot_response"
23
30
  require "binance_client/responses/get_all_response"
24
31
  require "binance_client/responses/book_ticker_response"
25
32
  require "binance_client/responses/order_book_depth_response"
33
+ require "binance_client/responses/sub_account_assets_response"
34
+ require "binance_client/responses/sub_account_deposit_address_response"
35
+ require "binance_client/responses/account_response"
26
36
 
27
37
  module BinanceClient
28
38
  class Error < StandardError; end
@@ -35,5 +45,7 @@ module BinanceClient
35
45
  has :host, classes: String, default: DEFAULT_HOST
36
46
  has :api_key, classes: String
37
47
  has :api_secret, classes: String
48
+ has :recv_window, classes: [NilClass, Integer]
49
+ has :proxy
38
50
  end
39
51
  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: 2.0.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-12-10 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,30 @@ files:
116
116
  - binance_client.gemspec
117
117
  - lib/binance_client.rb
118
118
  - lib/binance_client/client.rb
119
+ - lib/binance_client/factories.rb
120
+ - lib/binance_client/models/asset_balance.rb
119
121
  - lib/binance_client/models/base_model.rb
122
+ - lib/binance_client/models/deposit_address.rb
120
123
  - lib/binance_client/models/order_book.rb
121
124
  - lib/binance_client/models/order_book_entry.rb
125
+ - lib/binance_client/requests/account_request.rb
122
126
  - lib/binance_client/requests/account_snapshot_request.rb
127
+ - lib/binance_client/requests/authenticated_base_request.rb
123
128
  - lib/binance_client/requests/base_request.rb
124
129
  - lib/binance_client/requests/book_ticker_request.rb
125
130
  - lib/binance_client/requests/get_all_request.rb
126
131
  - lib/binance_client/requests/order_book_depth_request.rb
132
+ - lib/binance_client/requests/sub_account_assets_request.rb
133
+ - lib/binance_client/requests/sub_account_deposit_address_request.rb
127
134
  - lib/binance_client/requests/system_status_request.rb
135
+ - lib/binance_client/responses/account_response.rb
128
136
  - lib/binance_client/responses/account_snapshot_response.rb
129
137
  - lib/binance_client/responses/base_response.rb
130
138
  - lib/binance_client/responses/book_ticker_response.rb
131
139
  - lib/binance_client/responses/get_all_response.rb
132
140
  - lib/binance_client/responses/order_book_depth_response.rb
141
+ - lib/binance_client/responses/sub_account_assets_response.rb
142
+ - lib/binance_client/responses/sub_account_deposit_address_response.rb
133
143
  - lib/binance_client/responses/system_status_response.rb
134
144
  - lib/binance_client/version.rb
135
145
  homepage: https://github.com/bloom-solutions/binance_client-ruby