huobi_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # HuobiClient
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/huobi_client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'huobi_client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install huobi_client
22
+
23
+ ## Usage
24
+
25
+ client = HuobiClient.new(access, secret)
26
+
27
+ .kline
28
+ .ticker
29
+ .depth
30
+ .trade_detail
31
+ .trade_list
32
+ .market_detail
33
+ .
34
+ symbols
35
+ .hadax_symbols
36
+ .currencys
37
+ .hadax_currencys
38
+ .timestamp
39
+
40
+ .accounts
41
+ .balance
42
+ .hadax_balance
43
+
44
+ .place
45
+ .hadax_place
46
+ .submit_cancel
47
+ .batch_cancel
48
+ .order_detail
49
+ .match_results
50
+ .orders
51
+ .all_match_results
52
+
53
+ .dw_transfer_in
54
+ .dw_transfer_out
55
+ .margin_apply
56
+ .margin_repay
57
+ .loan_orders
58
+ .margin_balance
59
+
60
+ .withdraw
61
+ .withdraw_cancel
62
+ .withdraw_query
63
+
64
+ [中文](https://github.com/huobiapi/API_Docs/wiki/REST_authentication)
65
+ [English](https://github.com/huobiapi/API_Docs_en/wiki/REST_Reference)
66
+
67
+
68
+ ## Development
69
+
70
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
71
+
72
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/huobi_client.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "huobi_client"
5
+ # require "safe_yaml/load"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "huobi_client/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "huobi_client"
9
+ spec.version = HuobiClient::VERSION
10
+ spec.authors = ["Vcinly"]
11
+ spec.email = ["vcinly@gmail.com"]
12
+
13
+ spec.summary = %q{A Huobi api client.}
14
+ spec.description = %q{A Huobi api client.}
15
+ spec.homepage = "https://github.com/vcinly/huobi_client"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "faraday", "~> 0.15"
25
+ spec.add_dependency "faraday_middleware", "~> 0.12.2"
26
+ spec.add_dependency "activerecord", "~> 5.2.0"
27
+ spec.add_dependency "json", "~> 2.1"
28
+ spec.add_dependency "awesome_print", "~> 1.8.0"
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "pry", "~> 0.11.3"
33
+ spec.add_development_dependency "safe_yaml", "~> 1.0.4"
34
+ end
@@ -0,0 +1,165 @@
1
+ # coding: utf-8
2
+ require "awesome_print"
3
+ require 'huobi_client/request'
4
+ require 'active_support/all'
5
+
6
+ module HuobiClient
7
+ class Client
8
+ attr_reader :access, :secret, :account_id
9
+
10
+ include HuobiClient::Request
11
+
12
+ def initialize(access, secret, account_id=nil)
13
+ @access = access
14
+ @secret = secret
15
+ @account_id = account_id
16
+ end
17
+
18
+ # 行情API
19
+ def kline(symbol:, period:, size: 150) # 获取K线数据
20
+ get '/market/history/kline', fun_params(__method__, binding)
21
+ end
22
+
23
+ def ticker(symbol:)
24
+ get '/market/detail/merged', fun_params(__method__, binding)
25
+ end
26
+
27
+ def depth(symbol:, type:)
28
+ get '/market/depth', fun_params(__method__, binding)
29
+ end
30
+
31
+ def trade_detail(symbol:)
32
+ get '/market/trade', fun_params(__method__, binding)
33
+ end
34
+
35
+ def trade_list(symbol:, size: 1)
36
+ get '/market/history/trade', fun_params(__method__, binding)
37
+ end
38
+
39
+ def market_detail(symbol:)
40
+ get '/market/detail', fun_params(__method__, binding)
41
+ end
42
+
43
+ # 公共API
44
+ def symbols
45
+ get '/v1/common/symbols'
46
+ end
47
+
48
+ def hadax_symbols
49
+ get '/v1/hadax/common/symbols'
50
+ end
51
+
52
+ def currencys
53
+ get '/v1/common/currencys'
54
+ end
55
+
56
+ def hadax_currencys
57
+ get '/v1/hadax/common/currencys'
58
+ end
59
+
60
+ def timestamp
61
+ get '/v1/common/timestamp'
62
+ end
63
+
64
+ # 用户资产API
65
+ def accounts
66
+ get '/v1/account/accounts'
67
+ end
68
+
69
+ def balance(account_id: @account_id)
70
+ get "/v1/account/accounts/#{account_id}/balance"
71
+ end
72
+
73
+ def hadax_balance(account_id: @account_id)
74
+ get "/v1/hadax/account/accounts/#{account_id}/balance"
75
+ end
76
+
77
+ # 交易API
78
+ def place(account_id:, symbol:, type:, amount:, price: nil, source: 'api') # Pro站下单
79
+ # types
80
+ # buy-market:市价买,
81
+ # sell-market:市价卖,
82
+ # buy-limit:限价买,
83
+ # sell-limit:限价卖,
84
+ # buy-ioc:IOC买单,
85
+ # sell-ioc:IOC卖单
86
+ post '/v1/order/orders/place', fun_params(__method__, binding).transform_keys {|key| key.to_s.sub('_', '-')}
87
+ end
88
+
89
+ def hadax_place(account_id:, symbol:, type:, amount:, price: nil, source: 'api') # HADAX站下单
90
+ post '/v1/hadax/order/orders/place', fun_params(__method__, binding).transform_keys {|key| key.to_s.sub('_', '-')}
91
+ end
92
+
93
+ def submit_cancel(order_id:) # 申请撤销一个订单请求
94
+ post "/v1/order/orders/#{order_id}/submitcancel"
95
+ end
96
+
97
+ def batch_cancel(order_ids:) # 批量撤销订单
98
+ post '/v1/order/orders/batchcancel', fun_params(__method__, binding).transform_keys {|key| key.to_s.sub('_', '-')}
99
+ end
100
+
101
+ def order_detail # 查询某个订单详情
102
+ get "/v1/order/orders/#{options[:order_id]}"
103
+ end
104
+
105
+ def match_results # 查询某个订单的成交明细
106
+ get "/v1/order/orders/#{options[:order_id]}/matchresults"
107
+ end
108
+
109
+ def orders(symbol:, states:, types: nil, start_date: nil, end_date: nil, from: nil, direct: nil, size: nil) # 查询当前委托、历史委托
110
+ # direct false string 查询方向 prev 向前,next 向后
111
+ # states true string 查询的订单状态组合,使用','分割 pre-submitted 准备提交, submitted 已提交, partial-filled 部分成交, partial-canceled 部分成交撤销, filled 完全成交, canceled 已撤销
112
+ get '/v1/order/orders', fun_params(__method__, binding).transform_keys {|key| key.to_s.sub('_', '-')}
113
+ end
114
+
115
+ def all_match_results(symbol:, states:, types: nil, start_date: nil, end_date: nil, from: nil, direct: nil, size: nil) # 查询当前成交、历史成交
116
+ get '/v1/order/matchresults', fun_params(__method__, binding).transform_keys {|key| key.to_s.sub('_', '-')}
117
+ end
118
+
119
+ # 借贷交易API (重要:如果使用借贷资产交易,请在下单接口/v1/order/orders/place请求参数source中填写‘margin-api’)
120
+
121
+ def dw_transfer_in(symbol:, currency:, amount:) # 现货账户划入至借贷账户
122
+ post '/v1/dw/transfer-in/margin', fun_params(__method__, binding)
123
+ end
124
+
125
+ def dw_transfer_out(symbol:, currency:, amount:) # 借贷账户划出至现货账户
126
+ post '/v1/dw/transfer-out/margin', fun_params(__method__, binding)
127
+ end
128
+
129
+ def margin_apply(symbol:, currency:, amount:) # 申请借贷
130
+ post '/v1/margin/orders', fun_params(__method__, binding)
131
+ end
132
+
133
+ def margin_repay(amount:) # 归还借贷
134
+ post "/v1/margin/orders/#{options[order_id]}/repay", fun_params(__method__, binding)
135
+ end
136
+
137
+ def loan_orders(symbol:, states: nil, start_date: nil, end_date: nil, from: nil, direct: nil, size: nil) # 借贷订单
138
+ get '/v1/margin/loan-orders', fun_params(__method__, binding).transform_keys {|key| key.to_s.sub('_', '-')}
139
+ end
140
+
141
+ def margin_balance(symbol:) # 借贷账户详情
142
+ get '/v1/margin/accounts/balance', fun_params(__method__, binding)
143
+ end
144
+
145
+ # 虚拟币提现API
146
+ def withdraw(address:, amount:, currency:, fee: nil, addr_tag: nil) # 申请提现虚拟币
147
+ post '/v1/dw/withdraw/api/create', fun_params(__method__, binding).transform_keys {|key| key.to_s.sub('_', '-')}
148
+ end
149
+
150
+ def withdraw_cancel # 申请取消提现虚拟币
151
+ post '/v1/dw/withdraw-virtual/{withdraw-id}/cancel'
152
+ end
153
+
154
+ def withdraw_query(currency:, type:, from: nil, size: nil) # 查询虚拟币充提记录
155
+ # type true string 'deposit' or 'withdraw'
156
+ get '/v1/query/deposit-withdraw', fun_params(__method__, binding)
157
+ end
158
+
159
+ private
160
+ def fun_params(_method, _binding)
161
+ Hash[method(_method).parameters.map.collect { |_, name| [name, _binding.local_variable_get(name)] }]
162
+ end
163
+
164
+ end
165
+ end
@@ -0,0 +1,10 @@
1
+ require 'huobi_client/version'
2
+
3
+ module HuobiClient
4
+ module Config
5
+ USER_AGENT = "huobi client gem #{HuobiClient::VERSION}"
6
+ BASE_URL = 'https://api.huobi.pro'.freeze
7
+ SIGNATURE_VERSION = 2
8
+
9
+ end
10
+ end
File without changes
@@ -0,0 +1,8 @@
1
+
2
+ module HuobiClient
3
+ module Helpers
4
+ class << self
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'logger'
4
+
5
+ module HuobiClient
6
+ module Log
7
+ class << self
8
+ attr_accessor :logger
9
+
10
+ def logger
11
+ @logger ||= Logger.new(STDERR)
12
+ end
13
+ end
14
+ end # module Log
15
+ end
@@ -0,0 +1,69 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'huobi_client/config'
6
+ require 'huobi_client/response'
7
+
8
+ module HuobiClient
9
+ module Request
10
+ def get(path, options={})
11
+ connect(:get, path, options)
12
+ end
13
+
14
+ def post(path, options={})
15
+ # options.merge! created: created_at, access_key: @key
16
+ connect(:post, path, options)
17
+ end
18
+
19
+ private
20
+
21
+ def build_and_sign(method, path, options, params)
22
+ params.merge! options if method.upcase == :GET
23
+
24
+ str = Faraday::FlatParamsEncoder.encode(params.sort)
25
+
26
+ sign "#{method.to_s.upcase}\napi.huobi.pro\n#{path}\n#{str}"
27
+ end
28
+
29
+ def connect(method, path, options)
30
+ options.compact!
31
+ params = {
32
+ AccessKeyId: @access,
33
+ SignatureMethod: 'HmacSHA256',
34
+ SignatureVersion: HuobiClient::Config::SIGNATURE_VERSION,
35
+ Timestamp: Time.now.getutc.strftime("%Y-%m-%dT%H:%M:%S")
36
+ }.transform_keys { |key| key.to_s }
37
+
38
+ params['Signature'] = build_and_sign(method, path, options, params)
39
+
40
+ options.merge! params if method.upcase == :GET
41
+
42
+ path = "#{path}?#{Faraday::FlatParamsEncoder.encode(params)}" if method.upcase == :POST
43
+
44
+ # ap options
45
+ Response.new(connection.send(method, path, options))
46
+ end
47
+
48
+ def connection
49
+ options = {
50
+ url: HuobiClient::Config::BASE_URL,
51
+ headers: {
52
+ user_agent: HuobiClient::Config::USER_AGENT
53
+ },
54
+ }
55
+
56
+ @connection ||= Faraday.new(options) do |conn|
57
+ conn.request :json
58
+ conn.response :json, :content_type => "application/json"
59
+ conn.adapter Faraday.default_adapter
60
+ end
61
+ end
62
+
63
+ def sign(str)
64
+ Base64.encode64(OpenSSL::HMAC.digest('sha256', @secret.to_s, str)).strip()
65
+ end
66
+
67
+
68
+ end
69
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_support/core_ext/hash/keys'
4
+
5
+ module HuobiClient
6
+ class Response
7
+ SUCCESS_STATUS = [200, 201, 204]
8
+ attr_reader :original_response, :body, :success
9
+
10
+ def initialize(faraday_response)
11
+ @original_response = faraday_response
12
+ @body = faraday_response.body
13
+ @success = false
14
+
15
+ begin
16
+ @body = @body.with_indifferent_access if @body.is_a? Hash
17
+ rescue => e
18
+ p e.message
19
+ p e.backtrace.join("\n")
20
+ end
21
+
22
+ if SUCCESS_STATUS.include? original_response.status
23
+ @success = @body[:status] == 'ok'
24
+ end
25
+ end
26
+
27
+ def success?
28
+ @success
29
+ end
30
+
31
+ def headers
32
+ original_response.headers
33
+ end
34
+
35
+ def status
36
+ original_response.status
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module HuobiClient
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "huobi_client/client"
2
+ require "huobi_client/version"
3
+
4
+ module HuobiClient
5
+ class << self
6
+ def new(access, secret)
7
+ Huobi::Client.new(access, secret)
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huobi_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vcinly
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.8.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.8.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.16'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.16'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.11.3
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.11.3
139
+ - !ruby/object:Gem::Dependency
140
+ name: safe_yaml
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 1.0.4
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 1.0.4
153
+ description: A Huobi api client.
154
+ email:
155
+ - vcinly@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".travis.yml"
163
+ - Gemfile
164
+ - Gemfile.lock
165
+ - LICENSE
166
+ - README.md
167
+ - Rakefile
168
+ - bin/console
169
+ - bin/setup
170
+ - huobi_client.gemspec
171
+ - lib/huobi_client.rb
172
+ - lib/huobi_client/client.rb
173
+ - lib/huobi_client/config.rb
174
+ - lib/huobi_client/exceptions.rb
175
+ - lib/huobi_client/helpers.rb
176
+ - lib/huobi_client/log.rb
177
+ - lib/huobi_client/request.rb
178
+ - lib/huobi_client/response.rb
179
+ - lib/huobi_client/version.rb
180
+ homepage: https://github.com/vcinly/huobi_client
181
+ licenses: []
182
+ metadata: {}
183
+ post_install_message:
184
+ rdoc_options: []
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ requirements: []
198
+ rubyforge_project:
199
+ rubygems_version: 2.5.1
200
+ signing_key:
201
+ specification_version: 4
202
+ summary: A Huobi api client.
203
+ test_files: []