huobi_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +39 -0
- data/LICENSE.md +7 -0
- data/README.md +66 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/huobi_api.gemspec +30 -0
- data/lib/huobi_api.rb +54 -0
- data/lib/huobi_api/accounts.rb +8 -0
- data/lib/huobi_api/client.rb +11 -0
- data/lib/huobi_api/network.rb +77 -0
- data/lib/huobi_api/orders.rb +27 -0
- data/lib/huobi_api/version.rb +3 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3f1494c13c7bd207edfc4e859ee3dc0a9995e121
|
4
|
+
data.tar.gz: a0762e37e4a1c0af385ba0bbc6773181e624e5d5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd28473573334c3a8393d38e845c84511b785b7a4e09c172d94d91843fefc7b688456118e81b778c9800541523c5f9ac9d3e161af1aacc55b6e2961feb2e7592
|
7
|
+
data.tar.gz: 9b4e1c0179c28fb1c72247be29203d097af6f66cbd4a6efb336e3bfad42b62f33c2b6736eede8edc8c243947e6db439c883a655cac8b09eab08416e86050a880
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
huobi_api (0.1.0)
|
5
|
+
openssl
|
6
|
+
rack
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.3)
|
12
|
+
openssl (2.1.0)
|
13
|
+
rack (2.0.4)
|
14
|
+
rake (10.5.0)
|
15
|
+
rspec (3.7.0)
|
16
|
+
rspec-core (~> 3.7.0)
|
17
|
+
rspec-expectations (~> 3.7.0)
|
18
|
+
rspec-mocks (~> 3.7.0)
|
19
|
+
rspec-core (3.7.1)
|
20
|
+
rspec-support (~> 3.7.0)
|
21
|
+
rspec-expectations (3.7.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-mocks (3.7.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.7.0)
|
27
|
+
rspec-support (3.7.0)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
bundler (~> 1.16.a)
|
34
|
+
huobi_api!
|
35
|
+
rake (~> 10.0)
|
36
|
+
rspec (~> 3.0)
|
37
|
+
|
38
|
+
BUNDLED WITH
|
39
|
+
1.16.0.pre.2
|
data/LICENSE.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2018 Timothy Sherratt
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# HuboiApi
|
2
|
+
|
3
|
+
A ruby wrapper for the Huobi Pro API
|
4
|
+
|
5
|
+
So far just a few endpoints have been implemented, but adding more is
|
6
|
+
simple and welcome. See `lib/huobi_api/orders.rb` and [the huobi docs](https://github.com/huobiapi/API_Docs_en/wiki/REST_Reference) to get started.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'huboi_api'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install huboi_api
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Config
|
27
|
+
```ruby
|
28
|
+
HuobiApi.configure do |config|
|
29
|
+
# confusingly the account_id is got from the API - see below - it is different from the UID show on the website
|
30
|
+
config.account_id = 123456
|
31
|
+
# key and secret from API Management page on Huobi website
|
32
|
+
config.key = 'xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxx'
|
33
|
+
config.secret = 'yyyyyyyy-yyyyyyyy-yyyyyyyy-yyyyy'
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
### Making calls
|
38
|
+
```ruby
|
39
|
+
HuobiApi.new_order(amount: 100, side: :sell, symbol: :xrpbtc, type: :market)
|
40
|
+
# => => {"status"=>"ok", "data"=>"12345678"}
|
41
|
+
|
42
|
+
HuobiApi.order_info(order_id: 12345678)
|
43
|
+
# => {"account-id"=>"123456", "amount"=>"100", "source"=>"api", "symbol"=>"xrpbtc", "type"=>"buy-market"}
|
44
|
+
```
|
45
|
+
|
46
|
+
### Getting your account_id
|
47
|
+
```ruby
|
48
|
+
HuobiApi.configure do |config|
|
49
|
+
config.key = 'xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxx'
|
50
|
+
config.secret = 'yyyyyyyy-yyyyyyyy-yyyyyyyy-yyyyy'
|
51
|
+
end
|
52
|
+
|
53
|
+
HuobiApi.accounts
|
54
|
+
# => {"status"=>"ok", "data"=>[{"id"=>123456, "type"=>"spot", "subtype"=>"", "state"=>"working"}]}
|
55
|
+
```
|
56
|
+
Your `account_id` is 123456.
|
57
|
+
|
58
|
+
## Development
|
59
|
+
|
60
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests (though you may need to write some tests first). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
61
|
+
|
62
|
+
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).
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/huboi_api.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "huobi_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/huobi_api.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "huobi_api/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "huobi_api"
|
8
|
+
spec.version = HuobiApi::VERSION
|
9
|
+
spec.authors = ["Timothy Sherratt"]
|
10
|
+
spec.email = ["tim@wrkbr.com"]
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.summary = %q{Ruby wrapper for Huobi Pro API}
|
14
|
+
spec.description = %q{A ruby wrapper for the Huobi Pro API}
|
15
|
+
spec.homepage = "https://github.com/tpsherratt/huobi_api"
|
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 "rack", "~> 1.5"
|
25
|
+
spec.add_dependency "openssl", "~> 2.0"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
end
|
data/lib/huobi_api.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'huobi_api/version'
|
2
|
+
require 'huobi_api/client'
|
3
|
+
|
4
|
+
module HuobiApi
|
5
|
+
@key = nil
|
6
|
+
@secret = nil
|
7
|
+
@accout_id = nil
|
8
|
+
|
9
|
+
def self.key=(key)
|
10
|
+
@key = key
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.key
|
14
|
+
@key
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.secret=(secret)
|
18
|
+
@secret = secret
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.secret
|
22
|
+
@secret
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.account_id=(account_id)
|
26
|
+
@account_id = account_id
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.account_id
|
30
|
+
@account_id
|
31
|
+
end
|
32
|
+
|
33
|
+
# configure like
|
34
|
+
# HuobiApi.configure do |config|
|
35
|
+
# config.key = 'my key'
|
36
|
+
# // also secret and account_id
|
37
|
+
# end
|
38
|
+
def self.configure
|
39
|
+
yield self
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.client
|
43
|
+
@client ||= Client.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.method_missing(method, *args, &block)
|
47
|
+
return super unless client.respond_to?(method)
|
48
|
+
client.send method, *args, &block
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.respond_to?(method, include_private = false)
|
52
|
+
client.respond_to?(method, include_private) || super(method, include_private)
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'rack'
|
3
|
+
require 'openssl'
|
4
|
+
require 'net/http'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module HuobiApi
|
8
|
+
module Network
|
9
|
+
|
10
|
+
BASE_URL='https://api.huobi.pro'.freeze
|
11
|
+
SIGNATURE_VERSION=2
|
12
|
+
HEADERS={
|
13
|
+
'Content-Type'=> 'application/json',
|
14
|
+
'Accept' => 'application/json',
|
15
|
+
'Accept-Language' => 'en-GB',
|
16
|
+
}.freeze
|
17
|
+
|
18
|
+
def post(endpoint, data)
|
19
|
+
request(endpoint, :POST, data)
|
20
|
+
end
|
21
|
+
|
22
|
+
def get(endpoint, data)
|
23
|
+
request(endpoint, :GET, data)
|
24
|
+
end
|
25
|
+
|
26
|
+
def request(endpoint, method, data)
|
27
|
+
uri = URI.parse(BASE_URL)
|
28
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
29
|
+
http.use_ssl = true
|
30
|
+
|
31
|
+
params = build_params(endpoint, method, data)
|
32
|
+
url = "#{BASE_URL}#{endpoint}?#{Rack::Utils.build_query(params)}"
|
33
|
+
|
34
|
+
begin
|
35
|
+
JSON.parse http.send_request(method, url, JSON.dump(data), HEADERS).body
|
36
|
+
rescue Net::HTTPExceptions => e
|
37
|
+
{"message" => 'error' ,"request_error" => e.message}
|
38
|
+
rescue JSON::ParserError => e
|
39
|
+
{"message" => 'error' ,"request_error" => e.message}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_params(endpoint, method, data)
|
44
|
+
# things huobi want you to send (and sign)
|
45
|
+
params = {
|
46
|
+
'AccessKeyId' => HuobiApi.key,
|
47
|
+
'SignatureMethod' => 'HmacSHA256',
|
48
|
+
'SignatureVersion' => SIGNATURE_VERSION,
|
49
|
+
'Timestamp' => Time.now.getutc.strftime("%Y-%m-%dT%H:%M:%S")
|
50
|
+
}
|
51
|
+
|
52
|
+
# add in what we're sending, if it's a get request
|
53
|
+
params.merge!(data) if method.to_s.upcase == "GET"
|
54
|
+
# alphabetical order, as that's what huobi likes
|
55
|
+
sorted_params = hash_sort(params)
|
56
|
+
# now mash into a query string
|
57
|
+
query_string = Rack::Utils.build_query(sorted_params)
|
58
|
+
# now add some other random shit
|
59
|
+
to_sign = "#{method.to_s.upcase}\napi.huobi.pro\n#{endpoint}\n#{query_string}"
|
60
|
+
# now sign in
|
61
|
+
sig = sign(to_sign)
|
62
|
+
# and mash it into the params
|
63
|
+
params['Signature'] = sig
|
64
|
+
|
65
|
+
params
|
66
|
+
end
|
67
|
+
|
68
|
+
def sign(data)
|
69
|
+
Base64.encode64(OpenSSL::HMAC.digest('sha256', HuobiApi.secret, data)).gsub("\n","")
|
70
|
+
end
|
71
|
+
|
72
|
+
def hash_sort(ha)
|
73
|
+
puts ha
|
74
|
+
Hash[ha.sort_by{ |k, _| k }]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module HuobiApi
|
2
|
+
module Orders
|
3
|
+
def new_order(amount:, side:, symbol:, type:, price: nil)
|
4
|
+
options ={
|
5
|
+
amount: amount.to_s,
|
6
|
+
symbol: symbol,
|
7
|
+
type: "#{side}-#{type}",
|
8
|
+
source: 'api',
|
9
|
+
'account-id': HuobiApi.account_id
|
10
|
+
}
|
11
|
+
options[:price] = price unless type.to_s == 'market'
|
12
|
+
|
13
|
+
endpoint = '/v1/order/orders/place'
|
14
|
+
post(endpoint, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def order_info(order_id:)
|
18
|
+
endpoint = "/v1/order/orders/#{order_id}"
|
19
|
+
get(endpoint, {})
|
20
|
+
end
|
21
|
+
|
22
|
+
def order_match_results(order_id:)
|
23
|
+
endpoint = "/v1/order/orders/#{order_id}/matchresults"
|
24
|
+
get(endpoint, {})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: huobi_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Timothy Sherratt
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: openssl
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: A ruby wrapper for the Huobi Pro API
|
84
|
+
email:
|
85
|
+
- tim@wrkbr.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- Gemfile.lock
|
93
|
+
- LICENSE.md
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- huobi_api.gemspec
|
99
|
+
- lib/huobi_api.rb
|
100
|
+
- lib/huobi_api/accounts.rb
|
101
|
+
- lib/huobi_api/client.rb
|
102
|
+
- lib/huobi_api/network.rb
|
103
|
+
- lib/huobi_api/orders.rb
|
104
|
+
- lib/huobi_api/version.rb
|
105
|
+
homepage: https://github.com/tpsherratt/huobi_api
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.5.1
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Ruby wrapper for Huobi Pro API
|
129
|
+
test_files: []
|