okex 0.1.0 → 0.2.1

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: 0e1959ae12e438525f9e0c2dcac836c61e07ebc1f07938448db0e65887309aa7
4
- data.tar.gz: 98e6f5d24a98bd43e79721d94efc0a90fa0c7291c97e2290916c0d6f4483ae4b
3
+ metadata.gz: 7a607b5cc12603ee0523d5c79d24ca3777e17b5809f0a2d1f46835bcf855fadb
4
+ data.tar.gz: 1443cf42904c1567069f4aefa505b3470cb996d9c4586c10a0aacb49b2f7c82d
5
5
  SHA512:
6
- metadata.gz: c8d61280766b8251a64b9b10c071451c03fe04faf77b3ac22d7f3b064420dcd084aa05d783beb4440dc499efd231e7b851906d922559d6ed3b2f608a129b6ee7
7
- data.tar.gz: d872d29e31bde33229905cf66aae17f2b98e5f3a426fa3a1250d2cc874acbbfa4865e13f276f94d4acda6cac0b7c241c76ab49e15404055d0a70a6de22a44021
6
+ metadata.gz: 6f1ea6535eeffa3f2dbe6e35b7b44ccd753820f51478f3ba882a854efe9e73bbf4424586d1b881679cbd8db84cd5c8f1a87d7f906d7f6fdfe9d6c78d24152a2b
7
+ data.tar.gz: 9b9dc428f69094870b41cf4fe9b6a77cffdfbf979dbf04e0b72ea471e000d078d3999d06d2b1ed4a573c6485d75c7c7441911434e0a9a191e61f22703e8dcad1
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,12 +1,31 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- okex (0.1.0)
4
+ okex (0.2.1)
5
+ faraday (~> 1.5)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
10
  diff-lcs (1.4.4)
11
+ faraday (1.5.0)
12
+ faraday-em_http (~> 1.0)
13
+ faraday-em_synchrony (~> 1.0)
14
+ faraday-excon (~> 1.1)
15
+ faraday-httpclient (~> 1.0.1)
16
+ faraday-net_http (~> 1.0)
17
+ faraday-net_http_persistent (~> 1.1)
18
+ faraday-patron (~> 1.0)
19
+ multipart-post (>= 1.2, < 3)
20
+ ruby2_keywords (>= 0.0.4)
21
+ faraday-em_http (1.0.0)
22
+ faraday-em_synchrony (1.0.0)
23
+ faraday-excon (1.1.0)
24
+ faraday-httpclient (1.0.1)
25
+ faraday-net_http (1.0.1)
26
+ faraday-net_http_persistent (1.1.0)
27
+ faraday-patron (1.0.0)
28
+ multipart-post (2.1.1)
10
29
  rake (10.5.0)
11
30
  rspec (3.10.0)
12
31
  rspec-core (~> 3.10.0)
@@ -21,6 +40,7 @@ GEM
21
40
  diff-lcs (>= 1.2.0, < 2.0)
22
41
  rspec-support (~> 3.10.0)
23
42
  rspec-support (3.10.2)
43
+ ruby2_keywords (0.0.4)
24
44
 
25
45
  PLATFORMS
26
46
  ruby
data/lib/okex.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  require "okex/version"
2
+ require "okex/api_v3"
3
+ require "okex/api_v5"
4
+ require "okex/client"
5
+ require "okex/coin"
6
+ require "openssl"
7
+ require "faraday"
2
8
 
3
- module Okex
9
+ module OKEX
4
10
  class Error < StandardError; end
5
11
  end
@@ -0,0 +1,46 @@
1
+ class OKEX::ApiV3
2
+ def initialize(client)
3
+ @client = client
4
+ end
5
+
6
+ def balance(coin)
7
+ client.get("/api/account/v3/wallet/#{coin.code}")
8
+ end
9
+
10
+ def instruments
11
+ client.get("/api/swap/v3/instruments")
12
+ end
13
+
14
+ # 市价做空永续合约
15
+ # amount: 仓位开多少张
16
+ def short_swap(instrument_id, amount)
17
+ param = {
18
+ instrument_id: instrument_id.to_s,
19
+ size: amount.to_s,
20
+ type: "2",
21
+ order_type: "4",
22
+ }
23
+
24
+ client.post("/api/swap/v3/order", param)
25
+ end
26
+
27
+ # 市价平多
28
+ def close_long(instrument_id)
29
+ close_position(instrument_id, "long")
30
+ end
31
+
32
+ # 市价平空
33
+ def close_short(instrument_id)
34
+ close_position(instrument_id, "short")
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :client
40
+
41
+ def close_position(instrument_id, direction)
42
+ param = {"instrument_id": instrument_id, "direction": direction}
43
+
44
+ client.post("/api/swap/v3/close_position", param)
45
+ end
46
+ end
@@ -0,0 +1,48 @@
1
+ class OKEX::ApiV5
2
+ def initialize(client)
3
+ @client = client
4
+ end
5
+
6
+ def instruments
7
+ client.get("/api/v5/public/instruments")
8
+ end
9
+
10
+ def orders
11
+ client.get("/api/v5/account/positions")
12
+ end
13
+
14
+ def short_swap(instid, amount)
15
+ params = {
16
+ "instId": instid,
17
+ "tdMode": "cross",
18
+ "side": "sell",
19
+ "posSide": "short",
20
+ "ordType": "market",
21
+ "sz": "1"
22
+ }
23
+
24
+ client.post("/api/v5/trade/order", params)
25
+ end
26
+
27
+ def close_long(instid)
28
+ close_position(instid, "long")
29
+ end
30
+
31
+ def close_short(instid)
32
+ close_position(instid, "short")
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :client
38
+
39
+ def close_position(instid, direction)
40
+ params = {
41
+ "instId": instid,
42
+ "mgnMode": "cross",
43
+ "posSide": direction
44
+ }
45
+
46
+ client.post("/api/v5/trade/close-position", params)
47
+ end
48
+ end
@@ -0,0 +1,71 @@
1
+ require 'time'
2
+
3
+ module OKEX
4
+ class Client
5
+ def initialize(key, secret, passphrase)
6
+ @api_key = key
7
+ @api_secret = secret
8
+ @passphrase = passphrase
9
+ end
10
+
11
+ def get(path)
12
+ ts = timestamp
13
+ sig = sign(ts, "GET", path)
14
+
15
+ _resp(::Faraday.get(HOST + path, nil, headers(ts, sig)))
16
+ end
17
+
18
+ def post(path, payload)
19
+ payload_json = gen_payload(payload)
20
+
21
+ ts = timestamp
22
+ sig = sign(ts, "POST", path + payload_json)
23
+
24
+ begin
25
+ conn = ::Faraday.new(HOST + path, :ssl => {:verify => false})
26
+ result = conn.post(HOST + path, payload_json, headers(ts, sig))
27
+ _resp(result)
28
+ rescue => e
29
+ puts "[error] #{e.inspect}"
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :api_key, :api_secret, :passphrase
36
+
37
+ HOST = "https://www.okex.com"
38
+
39
+ def gen_payload(payload)
40
+ m = payload.map { |k, v| JSON.generate({k => v}, {space: ' '})}
41
+ m2 = m.join(", ").gsub("{", "").gsub("}", "")
42
+
43
+ "{#{m2}}"
44
+ end
45
+
46
+ def sign(timestamp, method, path)
47
+ msg = "#{timestamp}#{method}#{path}"
48
+ digest = OpenSSL::HMAC.digest("sha256", api_secret, msg)
49
+
50
+ Base64.strict_encode64(digest)
51
+ end
52
+
53
+ def timestamp
54
+ Time.now.utc.iso8601(3)
55
+ end
56
+
57
+ def headers(timestamp, sign)
58
+ {
59
+ 'OK-ACCESS-KEY': api_key,
60
+ 'OK-ACCESS-SIGN': sign,
61
+ 'OK-ACCESS-TIMESTAMP': timestamp,
62
+ 'OK-ACCESS-PASSPHRASE': passphrase,
63
+ 'Content-Type': 'application/json'
64
+ }
65
+ end
66
+
67
+ def _resp(result)
68
+ JSON.parse(result.body)
69
+ end
70
+ end
71
+ end
data/lib/okex/coin.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "okex/coin/bitcoin"
2
+
3
+ module OKEX
4
+ module Coin
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ module OKEX
2
+ module Coin
3
+ class Bitcoin
4
+ CODE = "BTC".freeze
5
+
6
+ def self.code
7
+ CODE
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/okex/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Okex
2
- VERSION = "0.1.0"
1
+ module OKEX
2
+ VERSION = "0.2.1"
3
3
  end
data/okex.gemspec CHANGED
@@ -4,11 +4,11 @@ require "okex/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "okex"
7
- spec.version = Okex::VERSION
7
+ spec.version = OKEX::VERSION
8
8
  spec.authors = ["David Zhang"]
9
9
  spec.email = ["v8os.com@gmail.com"]
10
10
 
11
- spec.summary = %q{OKEX API}
11
+ spec.summary = %q{OKEX Ruby API}
12
12
  spec.description = %q{RESTful API for okex.com}
13
13
  spec.homepage = "https://github.com/daqing/okex"
14
14
  spec.license = "MIT"
@@ -38,4 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.add_development_dependency "bundler", "~> 1.17"
39
39
  spec.add_development_dependency "rake", "~> 10.0"
40
40
  spec.add_development_dependency "rspec", "~> 3.0"
41
+
42
+ spec.add_runtime_dependency "faraday", "~> 1.5"
41
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Zhang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-04 00:00:00.000000000 Z
11
+ date: 2021-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
55
69
  description: RESTful API for okex.com
56
70
  email:
57
71
  - v8os.com@gmail.com
@@ -71,6 +85,11 @@ files:
71
85
  - bin/console
72
86
  - bin/setup
73
87
  - lib/okex.rb
88
+ - lib/okex/api_v3.rb
89
+ - lib/okex/api_v5.rb
90
+ - lib/okex/client.rb
91
+ - lib/okex/coin.rb
92
+ - lib/okex/coin/bitcoin.rb
74
93
  - lib/okex/version.rb
75
94
  - okex.gemspec
76
95
  homepage: https://github.com/daqing/okex
@@ -99,5 +118,5 @@ rubyforge_project:
99
118
  rubygems_version: 2.7.6
100
119
  signing_key:
101
120
  specification_version: 4
102
- summary: OKEX API
121
+ summary: OKEX Ruby API
103
122
  test_files: []