okcoin-ruby 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/README.md +5 -2
- data/lib/okcoin/rest.rb +94 -92
- data/lib/okcoin/version.rb +1 -1
- data/lib/okcoin/ws.rb +62 -60
- data/okcoin-ruby.gemspec +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fde5fbf15d7d1456c2e71b7c082d50027e61b2d
|
4
|
+
data.tar.gz: e3df5c33d806fe76fe3bb2342160e14f28d83dfb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: acf2b40dbf2faf83ebabd4aaf3a67735e19087dff98323373288b5fb0248b8dc2dd0c5dcb0443b256a87863c1f30fcf972f42f2ef4c9f3a6949de8225d724155
|
7
|
+
data.tar.gz: e1439a1322dbc48dbc6395a779186aa6f6f55c28525f76c5156a90a62de929d7894230f422ba7e0cf722ea837dd0ebb865c9174ca6329ab591e436182937f365
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -19,7 +19,8 @@ And then execute:
|
|
19
19
|
### 1. REST Example
|
20
20
|
```
|
21
21
|
okcoin = Okcoin::Rest.new api_key: ENV['OKCOIN_APIKEY'], secret_key: ENV['OKCOIN_SECRET']
|
22
|
-
puts okcoin.
|
22
|
+
puts okcoin.userinfo
|
23
|
+
puts okcoin.orderbook(pair: "btcusd", items_no: 50)
|
23
24
|
```
|
24
25
|
|
25
26
|
### 2. WebSocket Example
|
@@ -27,7 +28,9 @@ puts okcoin.user_info
|
|
27
28
|
```
|
28
29
|
okcoin = Okcoin::WS.new api_key: ENV['OKCOIN_APIKEY'], secret_key: ENV['OKCOIN_SECRET']
|
29
30
|
okcoin.userinfo
|
30
|
-
|
31
|
+
okcoin.pingpong
|
32
|
+
okcoin.price_api("{'event':'addChannel','channel':'ok_btcusd_ticker'}")
|
33
|
+
while true; sleep 1; end
|
31
34
|
okcoin.close
|
32
35
|
```
|
33
36
|
|
data/lib/okcoin/rest.rb
CHANGED
@@ -1,118 +1,120 @@
|
|
1
|
-
class Okcoin
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
class Okcoin
|
2
|
+
class Rest
|
3
|
+
BASE_URI = "https://www.okcoin.com/api"
|
4
|
+
TIMEOUT = 0.5
|
5
|
+
|
6
|
+
def initialize(api_key: nil, secret_key: nil)
|
7
|
+
@api_key = api_key
|
8
|
+
@secret_key = secret_key
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
+
public
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
def orderbook(pair:, items_no: 50)
|
14
|
+
query = { "ok" => 1, "symbol" => pair, "size" => items_no }
|
15
|
+
get_request(url: "/depth.do", query: query)
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def futures_userinfo
|
19
|
+
post_data = initial_post_data
|
20
|
+
post_request post_data: post_data, action: "/v1/future_userinfo.do"
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
def equity
|
24
|
+
post_data = initial_post_data
|
25
|
+
post_request(post_data: post_data, action: "/v1/future_userinfo.do")["info"]["btc"]["account_rights"]
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
def futures_orderbook(pair:, contract:, items_no: 50)
|
29
|
+
query = { "symbol" => pair, "contractType" => contract, "size" => items_no }
|
30
|
+
get_request(url: "/future_depth.do", query: query)
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
33
|
+
def trade_futures(pair:, amount:, type:, contract_type:, match_price:, price: nil, lever_rate: 10)
|
34
|
+
post_data = initial_post_data
|
35
|
+
|
36
|
+
post_data["symbol"] = pair
|
37
|
+
post_data["contract_type"] = contract_type
|
38
|
+
post_data["amount"] = amount
|
39
|
+
post_data["type"] = type
|
40
|
+
post_data["match_price"] = match_price
|
41
|
+
post_data["lever_rate"] = lever_rate
|
41
42
|
|
42
|
-
|
43
|
+
post_data["price"] = price if price
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
post_request post_data: post_data, action: "/v1/future_trade.do"
|
46
|
+
end
|
46
47
|
|
47
|
-
|
48
|
-
|
48
|
+
def future_cancel(pair:, contract_type:, order_id:)
|
49
|
+
post_data = initial_post_data
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
post_data["symbol"] = pair
|
52
|
+
post_data["contract_type"] = contract_type
|
53
|
+
post_data["order_id"] = order_id
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
post_request post_data: post_data, action: "/v1/future_cancel.do"
|
56
|
+
end
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
def futures_orders_info(order_id:, symbol:, contract_type:)
|
59
|
+
post_data = initial_post_data
|
60
|
+
post_data["symbol"] = symbol
|
61
|
+
post_data["contract_type"] = contract_type
|
62
|
+
post_data["order_id"] = order_id
|
62
63
|
|
63
|
-
|
64
|
-
|
64
|
+
post_request post_data: post_data, action: "/v1/future_orders_info.do"
|
65
|
+
end
|
65
66
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
67
|
+
def futures_position(pair:, contract_type:)
|
68
|
+
post_data = initial_post_data
|
69
|
+
post_data["symbol"] = pair
|
70
|
+
post_data["contract_type"] = contract_type
|
71
|
+
post_request post_data: post_data, action: "/v1/future_position.do"
|
72
|
+
end
|
72
73
|
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
def userinfo
|
76
|
+
post_data = initial_post_data
|
77
|
+
post_request post_data: post_data, action: "/v1/userinfo.do"
|
78
|
+
end
|
78
79
|
|
79
|
-
|
80
|
+
private
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
82
|
+
def handle_timeouts
|
83
|
+
begin
|
84
|
+
yield
|
85
|
+
rescue => ex
|
86
|
+
Rails.logger.info "Okcoin: An error of type #{ex.class} happened, message is #{ex.message}. Retrying..."
|
87
|
+
sleep TIMEOUT
|
88
|
+
retry
|
89
|
+
end
|
88
90
|
end
|
89
|
-
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
def initial_post_data
|
93
|
+
post_data = {}
|
94
|
+
post_data["api_key"] = @api_key
|
95
|
+
post_data
|
96
|
+
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
98
|
+
def post_request(post_data:, action:)
|
99
|
+
params_string = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
|
100
|
+
hashed_string = params_string + "&secret_key=#{@secret_key}"
|
101
|
+
signature = OpenSSL::Digest.new("md5", hashed_string).to_s.upcase
|
102
|
+
post_data["sign"] = signature
|
103
|
+
|
104
|
+
payload = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
|
105
|
+
|
106
|
+
handle_timeouts do
|
107
|
+
request = Curl.post(BASE_URI + action, payload) do |request|
|
108
|
+
request.headers['Content-type'] = 'application/x-www-form-urlencoded'
|
109
|
+
end
|
110
|
+
JSON.parse(request.body_str)
|
108
111
|
end
|
109
|
-
JSON.parse(request.body_str)
|
110
112
|
end
|
111
|
-
end
|
112
113
|
|
113
|
-
|
114
|
-
|
115
|
-
|
114
|
+
def get_request(url:, query: nil)
|
115
|
+
handle_timeouts do
|
116
|
+
query ? JSON.parse(Curl.get(BASE_URI + url, query).body_str) : JSON.parse(Curl.get(BASE_URI + url).body_str)
|
117
|
+
end
|
116
118
|
end
|
117
|
-
|
119
|
+
end
|
118
120
|
end
|
data/lib/okcoin/version.rb
CHANGED
data/lib/okcoin/ws.rb
CHANGED
@@ -1,74 +1,76 @@
|
|
1
|
-
class Okcoin
|
2
|
-
|
3
|
-
|
1
|
+
class Okcoin
|
2
|
+
class WS
|
3
|
+
include Celluloid
|
4
|
+
BASE_URI = 'wss://real.okcoin.com:10440/websocket/okcoinapi'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
public
|
12
|
-
# When WebSocket is opened, register callbacks
|
13
|
-
def on_open
|
14
|
-
puts "Websocket connection to #{BASE_URI} established succesfully"
|
6
|
+
def initialize(api_key: nil, secret_key: nil)
|
7
|
+
@driver = Celluloid::WebSocket::Client.new(BASE_URI, Celluloid::Actor.current)
|
8
|
+
@api_key = api_key
|
9
|
+
@secret_key = secret_key
|
15
10
|
end
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
public
|
13
|
+
# When WebSocket is opened, register callbacks
|
14
|
+
def on_open
|
15
|
+
puts "Websocket connection to #{BASE_URI} established succesfully"
|
16
|
+
end
|
21
17
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
terminate
|
27
|
-
end
|
18
|
+
# When raw WebSocket message is received
|
19
|
+
def on_message(msg)
|
20
|
+
puts "Message received: #{msg}"
|
21
|
+
end
|
28
22
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
# When WebSocket is closed
|
24
|
+
def on_close(code, reason)
|
25
|
+
puts "WebSocket connection closed: #{code.inspect}, #{reason.inspect}"
|
26
|
+
@driver.terminate
|
27
|
+
terminate
|
28
|
+
end
|
33
29
|
|
34
|
-
|
35
|
-
|
36
|
-
|
30
|
+
# close WebSocket connection
|
31
|
+
def close
|
32
|
+
@driver.close
|
33
|
+
end
|
37
34
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
35
|
+
def pingpong
|
36
|
+
every(5){ @driver.text "{'event':'ping'}" }
|
37
|
+
end
|
42
38
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
def userinfo
|
40
|
+
post_data = initial_post_data
|
41
|
+
emit(event: 'addChannel', channel: 'ok_spotusd_userinfo', post_data: post_data)
|
42
|
+
end
|
47
43
|
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
def futures_userinfo
|
45
|
+
post_data = initial_post_data
|
46
|
+
emit(event: 'addChannel', channel: 'ok_futureusd_userinfo', post_data: post_data)
|
47
|
+
end
|
51
48
|
|
52
|
-
|
49
|
+
def price_api(data)
|
50
|
+
@driver.text data
|
51
|
+
end
|
53
52
|
|
54
|
-
|
55
|
-
post_data = {}
|
56
|
-
post_data['api_key'] = @api_key
|
57
|
-
post_data
|
58
|
-
end
|
53
|
+
private
|
59
54
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
post_data = post_data.sort
|
66
|
-
post_data.collect! { |item| "'#{item[0]}':'#{item[1]}'" }
|
67
|
-
post_data = post_data.join(",")
|
68
|
-
end
|
55
|
+
def initial_post_data
|
56
|
+
post_data = {}
|
57
|
+
post_data['api_key'] = @api_key
|
58
|
+
post_data
|
59
|
+
end
|
69
60
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
61
|
+
def sign(post_data:)
|
62
|
+
params_string = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
|
63
|
+
hashed_string = params_string + "&secret_key=#{@secret_key}"
|
64
|
+
signature = OpenSSL::Digest.new('md5', hashed_string).to_s.upcase
|
65
|
+
post_data['sign'] = signature
|
66
|
+
post_data = post_data.sort
|
67
|
+
post_data.collect! { |item| "'#{item[0]}':'#{item[1]}'" }
|
68
|
+
post_data = post_data.join(",")
|
69
|
+
end
|
70
|
+
|
71
|
+
def emit(event:, channel:, post_data: nil)
|
72
|
+
post_data = sign(post_data: post_data)
|
73
|
+
@driver.text "{'event':'#{event}', 'channel':'#{channel}', 'parameters': {#{post_data}}}"
|
74
|
+
end
|
75
|
+
end
|
74
76
|
end
|
data/okcoin-ruby.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: okcoin-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Cherevkov
|
@@ -93,9 +93,9 @@ require_paths:
|
|
93
93
|
- lib
|
94
94
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
95
|
requirements:
|
96
|
-
- - "
|
96
|
+
- - "~>"
|
97
97
|
- !ruby/object:Gem::Version
|
98
|
-
version: '0'
|
98
|
+
version: '2.0'
|
99
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|