wazirx_official 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '01769330492f824af56041c147a239d9acb15045a1fe9105531d098b216801bd'
4
+ data.tar.gz: d26a08b771a3d002d50401c394d676ebfa42150aba244beba1f9f2f670b0d0fe
5
+ SHA512:
6
+ metadata.gz: 4b31a587cd48c24bfa55f71d32c6f123850b55f06fff75bc6b08fa710a8399008beb3c707faa864cd32ac5a31ddba97e5a810da01ca2ada28726ce2fadb1fa49
7
+ data.tar.gz: 1e8f6336d30edbf11f68ca5a3e400f7792ee3d89075031870235090ba1a9835faa67eb3618e7b28c5801f95bbdc7a694a2dffae4dfec1d9ef5970e532cb0759e
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'wazirx_official'
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
@@ -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,28 @@
1
+ require 'faraday_middleware'
2
+
3
+ module Wazirx
4
+ module Client
5
+ class REST
6
+ def public_client(adapter)
7
+ Faraday.new(url: "#{BASE_URL}/sapi") do |conn|
8
+ conn.request :multipart
9
+ conn.request :url_encoded
10
+ conn.response :json, content_type: /\bjson$/
11
+ conn.adapter adapter
12
+ end
13
+ end
14
+
15
+ def signed_client(api_key, secret_key, adapter)
16
+ Faraday.new(url: "#{BASE_URL}/sapi") do |conn|
17
+ conn.request :multipart
18
+ conn.request :url_encoded
19
+ conn.response :json, content_type: /\bjson$/
20
+ conn.headers['X-API-KEY'] = api_key
21
+ conn.use TimestampRequestMiddleware
22
+ conn.use SignRequestMiddleware, secret_key
23
+ conn.adapter :net_http
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ module Wazirx
2
+ module Client
3
+ class REST
4
+ ENDPOINTS = {
5
+ # Public API Endpoints
6
+ ping: 'v1/ping',
7
+ time: 'v1/time',
8
+ system_status: 'v1/systemStatus',
9
+ exchange_info: 'v1/exchangeInfo',
10
+ tickers: 'v1/tickers/24hr',
11
+ ticker: 'v1/ticker/24hr',
12
+ depth: 'v1/depth',
13
+ trades: 'v1/trades',
14
+ historical_trades: 'v1/historicalTrades',
15
+
16
+ # Account API Endpoints
17
+ order: 'v1/order',
18
+ test_order: 'v1/order/test',
19
+ open_orders: 'v1/openOrders',
20
+ all_orders: 'v1/allOrders',
21
+ account: 'v1/account',
22
+ funds: 'v1/funds',
23
+
24
+ # Websocket Auth Token
25
+ ws_auth_token: 'v1/create_auth_token'
26
+ }.freeze
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,70 @@
1
+ module Wazirx
2
+ module Client
3
+ class REST
4
+ METHODS = [
5
+ # Public API Methods
6
+ # #ping
7
+ { name: :ping, client: :public,
8
+ action: :get, endpoint: :ping },
9
+ # #time
10
+ { name: :time, client: :public,
11
+ action: :get, endpoint: :time },
12
+ # #system_status
13
+ { name: :system_status, client: :public,
14
+ action: :get, endpoint: :system_status },
15
+ # #exchange_info
16
+ { name: :exchange_info, client: :public,
17
+ action: :get, endpoint: :exchange_info },
18
+ # #tickers
19
+ { name: :tickers, client: :public,
20
+ action: :get, endpoint: :tickers },
21
+ # #ticker
22
+ { name: :ticker, client: :public,
23
+ action: :get, endpoint: :ticker },
24
+ # #depth
25
+ { name: :depth, client: :public,
26
+ action: :get, endpoint: :depth },
27
+ # #trades
28
+ { name: :trades, client: :public,
29
+ action: :get, endpoint: :trades },
30
+ # #historical_trades
31
+ { name: :historical_trades, client: :signed,
32
+ action: :get, endpoint: :historical_trades },
33
+
34
+ # Account API Methods
35
+ # #create_order!
36
+ { name: :create_order!, client: :signed,
37
+ action: :post, endpoint: :order },
38
+ # #create_test_order
39
+ { name: :create_test_order, client: :signed,
40
+ action: :post, endpoint: :test_order },
41
+ # #query_order
42
+ { name: :query_order, client: :signed,
43
+ action: :get, endpoint: :order },
44
+ # #cancel_order!
45
+ { name: :cancel_order!, client: :signed,
46
+ action: :delete, endpoint: :order },
47
+ # #open_orders
48
+ { name: :open_orders, client: :signed,
49
+ action: :get, endpoint: :open_orders },
50
+ # #cancel_open_orders!
51
+ { name: :cancel_open_orders, client: :signed,
52
+ action: :delete, endpoint: :open_orders },
53
+ # #all_orders
54
+ { name: :all_orders, client: :signed,
55
+ action: :get, endpoint: :all_orders },
56
+ # #account_info
57
+ { name: :account_info, client: :signed,
58
+ action: :get, endpoint: :account },
59
+ # #funds
60
+ { name: :funds_info, client: :signed,
61
+ action: :get, endpoint: :funds },
62
+
63
+ # Websocket Auth Token
64
+ # #create_auth_token!
65
+ { name: :create_auth_token!, client: :signed,
66
+ action: :post, endpoint: :ws_auth_token }
67
+ ].freeze
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,23 @@
1
+ module Wazirx
2
+ module Client
3
+ class REST
4
+ # Sign the query string using HMAC(sha-256) and appends to query string
5
+ SignRequestMiddleware = Struct.new(:app, :secret_key) do
6
+ def call(env)
7
+ key = 'signature'
8
+ hash = OpenSSL::HMAC.hexdigest(
9
+ OpenSSL::Digest.new('sha256'), secret_key, env.method == :get ? env.url.query : URI.encode_www_form(JSON.parse(env.body))
10
+ )
11
+ if env.method == :get
12
+ env.url.query = REST.add_query_param(env.url.query, 'signature', hash)
13
+ else
14
+ body = REST.sort_body env.body, key, hash
15
+ env.body = URI.encode_www_form(body)
16
+ end
17
+
18
+ app.call env
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ require 'date'
2
+
3
+ module Wazirx
4
+ module Client
5
+ class REST
6
+ # Generate a timestamp in milliseconds and append to query string
7
+ TimestampRequestMiddleware = Struct.new(:app) do
8
+ def call(env)
9
+ key = 'timestamp'
10
+ value = DateTime.now.strftime('%Q')
11
+
12
+ if env.method == :get
13
+ env.url.query = REST.add_query_param(
14
+ env.url.query, key, value
15
+ )
16
+ else
17
+ body = REST.sort_body env.body, key, value
18
+ env.body = JSON.generate(body)
19
+ end
20
+
21
+ app.call env
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,60 @@
1
+ require 'faraday'
2
+
3
+ require_relative 'rest/sign_request_middleware'
4
+ require_relative 'rest/timestamp_request_middleware'
5
+ require_relative 'rest/clients'
6
+ require_relative 'rest/endpoints'
7
+ require_relative 'rest/methods'
8
+
9
+ module Wazirx
10
+ module Client
11
+ class REST
12
+ BASE_URL = 'https://api.wazirx.com'.freeze
13
+
14
+ def initialize(api_key: '', secret_key: '',
15
+ adapter: Faraday.default_adapter)
16
+ @clients = {}
17
+ @clients[:public] = public_client adapter
18
+ @clients[:signed] = signed_client api_key, secret_key, adapter
19
+ end
20
+
21
+ def call(method, params={})
22
+ return {error: {message: "No method found named #{method}, please check if misspelled!"} }if
23
+ METHODS.select { |mthd| mthd[:name] == method.to_sym}.empty?
24
+ send(method, params)
25
+ end
26
+
27
+ METHODS.each do |method|
28
+ define_method(method[:name]) do |options = {}|
29
+ response = @clients[method[:client]].send(method[:action]) do |req|
30
+ req.url ENDPOINTS[method[:endpoint]]
31
+ puts "Params passed #{options}"
32
+ if method[:action] == :get
33
+ req.params.merge! options.map { |k, v| [k.to_s, v] }.to_h
34
+ else
35
+ req.body = JSON.generate(options.map { |k, v| [camelize(k.to_s), v] }.to_h)
36
+ end
37
+ end
38
+ response.body
39
+ end
40
+ end
41
+
42
+ def self.add_query_param(query, key, value)
43
+ query = query ? query.split('&') : []
44
+ query << "#{Faraday::Utils.escape key}=#{Faraday::Utils.escape value}"
45
+ query.sort.join('&')
46
+ end
47
+
48
+ def self.sort_body(body, key, value)
49
+ body = JSON.parse(body)
50
+ body[key] = value
51
+ body.sort.to_h
52
+ end
53
+
54
+ def camelize(str)
55
+ str.split('_')
56
+ .map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,99 @@
1
+ require 'faye/websocket'
2
+ require 'JSON'
3
+ require 'date'
4
+
5
+ module Wazirx
6
+ module Client
7
+ # Public: Client with methods mirroring the Wazirx WebSocket API
8
+ class WebSocket
9
+ # Public: String base url for WebSocket client to use
10
+ BASE_URL = 'wss://stream.wazirx.com/stream'.freeze
11
+ SUBSCRIBE = 'subscribe'
12
+
13
+ def initialize(api_key='', secret_key='')
14
+ @api_key = api_key
15
+ @secret_key = secret_key
16
+ end
17
+
18
+ def trades(symbol:, id: 0, action:SUBSCRIBE)
19
+ stream = get_mapped_streams(symbol, 'trades')
20
+ create_stream(streams: stream, id: id, action: action)
21
+ end
22
+
23
+ def all_market_ticker(id: 0,action:SUBSCRIBE)
24
+ stream = "!ticker@arr"
25
+ create_stream(streams: stream, id: id, action: action)
26
+ end
27
+
28
+ def depth(symbol:, id: 0, action:SUBSCRIBE)
29
+ stream = get_mapped_streams(symbol, 'depth')
30
+ create_stream(streams: stream, id: id, action: action)
31
+ end
32
+
33
+ def user_stream(streams:, id: 0, action: SUBSCRIBE)
34
+ create_stream(streams: streams, id: id, action: action, auth_key: get_auth_key)
35
+ end
36
+
37
+ def multi_stream(streams:, id: 0, action: SUBSCRIBE)
38
+ return "Streams should be an array!" if streams.class != Array
39
+ format_stream = []
40
+ streams.each do | stream |
41
+ format_stream += get_mapped_streams(stream[:symbol], stream[:type]) if stream[:type].to_s == 'trades'
42
+ format_stream += get_mapped_streams(stream[:symbol], stream[:type]) if stream[:type].to_s == 'depth'
43
+ format_stream << "!ticker@arr" if stream[:type].to_s == 'ticker'
44
+ end
45
+ create_stream(streams: format_stream, id: id, action: action)
46
+ end
47
+
48
+ def subscribe(streams:[], id:0, auth_key:'')
49
+ puts subscribeEvent(streams, id, auth_key)
50
+ end
51
+
52
+ def unsubscribe(streams:[], id:0, auth_key:'')
53
+ puts unsubscribeEvent(streams, id, auth_key)
54
+ end
55
+
56
+ private
57
+
58
+ def get_mapped_streams(symbols, type)
59
+ symbols.class == Array ? symbols.map { |sym| "#{sym}@#{type.to_s}" } : ["#{symbols}@#{type.to_s}"]
60
+ end
61
+
62
+ def get_auth_key
63
+ return @auth_key unless @auth_key.nil?
64
+ client = REST.new(api_key: @api_key, secret_key: @secret_key)
65
+ @auth_key = client.call('create_auth_token!', {timestamp: DateTime.now.strftime('%Q'), recvWindow: 20000})['auth_key']
66
+ end
67
+
68
+ def subscribeEvent(streams=[], id=0, auth_key='')
69
+ @ws.send(JSON.dump({'event':SUBSCRIBE, 'streams':streams.flatten,'id':id, 'auth_key':auth_key}))
70
+ end
71
+
72
+ def unsubscribeEvent(streams=[], id=0, auth_key='')
73
+ @ws.send(JSON.dump({'event':'unsubscribe', 'streams':streams.flatten,'id':id}))
74
+ end
75
+
76
+ def create_stream(streams: streams, id: id, action: action, auth_key:'')
77
+ @ws = Faye::WebSocket::Client.new(BASE_URL)
78
+ @ws.on :open do |event|
79
+ puts [:open]
80
+ if action == SUBSCRIBE
81
+ puts subscribeEvent(streams, id, auth_key)
82
+ else
83
+ puts unsubscribeEvent(streams, id, auth_key)
84
+ end
85
+ end
86
+
87
+ @ws.on :message do |event|
88
+ puts [:message, JSON.load(event.data)]
89
+ end
90
+
91
+ @ws.on :close do |event|
92
+ puts [:close, event.code, event.reason]
93
+ @ws = nil
94
+ end
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module Wazirx
2
+ VERSION = '2.0.0'.freeze
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'wazirx_official/version'
2
+ require 'wazirx_official/client/rest'
3
+ require 'wazirx_official/client/websocket'
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wazirx_official
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Som Prabh Sharma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
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: '0.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.12'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faye-websocket
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ description:
98
+ email:
99
+ - api@wazirx.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - bin/console
105
+ - bin/setup
106
+ - lib/wazirx_official.rb
107
+ - lib/wazirx_official/client/rest.rb
108
+ - lib/wazirx_official/client/rest/clients.rb
109
+ - lib/wazirx_official/client/rest/endpoints.rb
110
+ - lib/wazirx_official/client/rest/methods.rb
111
+ - lib/wazirx_official/client/rest/sign_request_middleware.rb
112
+ - lib/wazirx_official/client/rest/timestamp_request_middleware.rb
113
+ - lib/wazirx_official/client/websocket.rb
114
+ - lib/wazirx_official/version.rb
115
+ homepage: https://github.com/WazirX/wazirx-connector-ruby
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubygems_version: 3.0.3.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: API Wrapper for the Wazirx cryptocurrency exchange.
138
+ test_files: []