binance_ruby 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/binance_ruby.rb +184 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 549053c379a5e7fc68ff8c867a681b57876a4e6b
|
4
|
+
data.tar.gz: 2831f448da9bb5aca96ecbacb12ce447957d0333
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 15ad3e9016144d38c49636b0181ed0864245571c6c6524463158dcd68b745fce9ac23655a2a775c0bdc6f30b901b7e44e243250f76069a5c3ff857e854f2bc63
|
7
|
+
data.tar.gz: b584082a5fe4d7d392dd16f80880fa20fcb4f617794adb5de453c331cf215bdb21a7ffc2df005a9e18162be6d93bddeeacbaeced15f7a48a7c5140e544f37929
|
data/lib/binance_ruby.rb
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'json'
|
3
|
+
require 'date'
|
4
|
+
require "base64"
|
5
|
+
require 'openssl'
|
6
|
+
|
7
|
+
class Binance
|
8
|
+
include HTTParty
|
9
|
+
base_uri 'https://www.binance.com/api'
|
10
|
+
|
11
|
+
def initialize(api_key=nil, api_secret=nil, options={})
|
12
|
+
@api_key = api_key
|
13
|
+
@api_secret = api_secret
|
14
|
+
@base_uri = options[:base_uri] ||= 'https://www.binance.com/api'
|
15
|
+
end
|
16
|
+
|
17
|
+
###########################
|
18
|
+
###### Public Data ########
|
19
|
+
###########################
|
20
|
+
|
21
|
+
def server_time
|
22
|
+
response = get_public('time')
|
23
|
+
response["serverTime"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def order_book(pair)
|
27
|
+
opts = {'symbol' => pair}
|
28
|
+
response = get_public 'depth', opts
|
29
|
+
end
|
30
|
+
|
31
|
+
def aggregate_trade_list(pair)
|
32
|
+
opts = {'symbol' => pair}
|
33
|
+
response = get_public 'aggTrades', opts
|
34
|
+
end
|
35
|
+
|
36
|
+
def candles(pair, interval)
|
37
|
+
opts = {'symbol' => pair, 'interval' => interval}
|
38
|
+
response = get_public 'klines', opts
|
39
|
+
end
|
40
|
+
|
41
|
+
#interval list : 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
|
42
|
+
|
43
|
+
def ticker_day(pair)
|
44
|
+
opts = {'symbol' => pair}
|
45
|
+
response = get_public 'ticker/24hr', opts
|
46
|
+
end
|
47
|
+
|
48
|
+
def ticker_all
|
49
|
+
response = get_public 'ticker/allPrices'
|
50
|
+
end
|
51
|
+
|
52
|
+
def ticker_all_order_book
|
53
|
+
response = get_public '/ticker/allBookTickers'
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_public(method, opts={})
|
57
|
+
url = @base_uri + '/v1/' + method
|
58
|
+
r = self.class.get(url, query: opts)
|
59
|
+
resp = JSON.parse(r.body)
|
60
|
+
end
|
61
|
+
|
62
|
+
###########################
|
63
|
+
###### Private Data #######
|
64
|
+
###########################
|
65
|
+
|
66
|
+
def get_account(opts={})
|
67
|
+
response = get_private '/account?', opts
|
68
|
+
end
|
69
|
+
|
70
|
+
# Mandatory parameters for order : symbol side type timeInForce quantity price
|
71
|
+
|
72
|
+
def test_order(opts={})
|
73
|
+
opts[:type] == "MARKET" ? required_opts = %w{ symbol side type timeInForce quantity price } : required_opts = %w{ symbol side type quantity }
|
74
|
+
leftover = required_opts - opts.keys.map(&:to_s)
|
75
|
+
if leftover.length > 0
|
76
|
+
raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
|
77
|
+
end
|
78
|
+
response = post_private '/order/test?', opts
|
79
|
+
end
|
80
|
+
|
81
|
+
def set_order(opts={})
|
82
|
+
opts[:type] == "MARKET" ? required_opts = %w{ symbol side type timeInForce quantity price } : required_opts = %w{ symbol side type quantity }
|
83
|
+
leftover = required_opts - opts.keys.map(&:to_s)
|
84
|
+
if leftover.length > 0
|
85
|
+
raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
|
86
|
+
end
|
87
|
+
response = post_private '/order?', opts
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_order(opts={})
|
91
|
+
required_opts = %w{ symbol }
|
92
|
+
leftover = required_opts - opts.keys.map(&:to_s)
|
93
|
+
if leftover.length > 0 || (opts["orderId"].nil? && opts["origClientOrderId"].nil?)
|
94
|
+
raise ArgumentError.new("Required options not given : symbol and either orderId or origClientOrderId}")
|
95
|
+
end
|
96
|
+
response = get_private '/order?', opts
|
97
|
+
end
|
98
|
+
|
99
|
+
def get_open_orders(opts={})
|
100
|
+
required_opts = %w{ symbol }
|
101
|
+
leftover = required_opts - opts.keys.map(&:to_s)
|
102
|
+
if leftover.length > 0
|
103
|
+
raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
|
104
|
+
end
|
105
|
+
response = get_private '/openOrders?', opts
|
106
|
+
end
|
107
|
+
|
108
|
+
def get_all_orders(opts={})
|
109
|
+
required_opts = %w{ symbol }
|
110
|
+
leftover = required_opts - opts.keys.map(&:to_s)
|
111
|
+
if leftover.length > 0
|
112
|
+
raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
|
113
|
+
end
|
114
|
+
response = get_private '/allOrders?', opts
|
115
|
+
end
|
116
|
+
|
117
|
+
def get_trades(opts={})
|
118
|
+
required_opts = %w{ symbol }
|
119
|
+
leftover = required_opts - opts.keys.map(&:to_s)
|
120
|
+
if leftover.length > 0
|
121
|
+
raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
|
122
|
+
end
|
123
|
+
response = get_private '/myTrades?', opts
|
124
|
+
end
|
125
|
+
|
126
|
+
def cancel_order(opts={})
|
127
|
+
required_opts = %w{ symbol }
|
128
|
+
leftover = required_opts - opts.keys.map(&:to_s)
|
129
|
+
if leftover.length > 0 || (opts["orderId"].nil? && opts["origClientOrderId"].nil?)
|
130
|
+
raise ArgumentError.new("Required options not given : symbol and either orderId or origClientOrderId}")
|
131
|
+
end
|
132
|
+
response = delete_private '/order?', opts
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
###########################
|
138
|
+
###### Create request #####
|
139
|
+
###########################
|
140
|
+
|
141
|
+
def get_private(method, opts={})
|
142
|
+
header = {"X-MBX-APIKEY":@api_key }
|
143
|
+
r = self.class.get(set_request(method, opts), headers: header)
|
144
|
+
resp = JSON.parse(r.body)
|
145
|
+
end
|
146
|
+
|
147
|
+
def post_private(method, opts={})
|
148
|
+
header = {"X-MBX-APIKEY":@api_key }
|
149
|
+
r = self.class.post(set_request(method, opts), headers: header)
|
150
|
+
resp = JSON.parse(r.body)
|
151
|
+
end
|
152
|
+
|
153
|
+
def delete_private(method, opts={})
|
154
|
+
header = {"X-MBX-APIKEY":@api_key }
|
155
|
+
r = self.class.delete(set_request(method, opts), headers: header)
|
156
|
+
resp = JSON.parse(r.body)
|
157
|
+
end
|
158
|
+
|
159
|
+
def set_request(method, opts={})
|
160
|
+
options = Array.new
|
161
|
+
unless opts.empty?
|
162
|
+
opts.each do |key, value|
|
163
|
+
options << key + "=" + value
|
164
|
+
end
|
165
|
+
data = options.join('&') + '×tamp=' + DateTime.now.strftime('%Q').to_s
|
166
|
+
else
|
167
|
+
data = '×tamp=' + DateTime.now.strftime('%Q').to_s
|
168
|
+
end
|
169
|
+
|
170
|
+
digest = OpenSSL::Digest.new('sha256')
|
171
|
+
signature = OpenSSL::HMAC.hexdigest(digest, @api_secret, data)
|
172
|
+
|
173
|
+
url = @base_uri + '/v3' + method + data + "&signature=" + signature
|
174
|
+
return url
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: binance_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brieuc Lambert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: a simple wrapper for binance api in ruby
|
14
|
+
email: lambert.brieuc@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/binance_ruby.rb
|
20
|
+
homepage: http://rubygems.org/gems/binance_ruby
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.6.14
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Binance!
|
44
|
+
test_files: []
|