viabtc 0.2.0 → 0.3.0

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: 6f6faea21869b0bb5cf1c68e7ce2c55d5ee769872d747d6a62e5dbb3d8d22ba6
4
- data.tar.gz: bb10abb7ad810ec80c4bd4161fedef55f6609e279af16196d627bed1062c3771
3
+ metadata.gz: bed0b4acacb8c5a6c55f73a06b8c70a06daec112400b0e185ad01bb6bd605900
4
+ data.tar.gz: 236ed2ed08255f325891d18dace3dd4807b7e845a014d5a3cb1c067b2cf76833
5
5
  SHA512:
6
- metadata.gz: 10466297750f595fa88d76daad7971608b4933cf0004352499c3ad34011b408dedb8d8bd5588e4274eb83de23e53fa0f294f2759632e002281caacc6cef79407
7
- data.tar.gz: 975385083e97437dd00f7b55438c87ec0650e8ee083e4c55791dd4874b8c1cb9cd71e97e77308762631bd510f899d387eb3265277d0a37969fb6938e2963362c
6
+ metadata.gz: 879fe516995d8a737092b59a54a8dc549c02a70fedb3ec2fe689b28a57064e5588097f27d17b4fdb899a198cfc54bc3e197fa5fc91867f6daaf6c642c349a5c7
7
+ data.tar.gz: 1dcc68642ebcd0b4a55992fa1a3b5cb61561f961e00c8aaec4f988b3c837ffe6c050b6385264c79d5782f404d36a3679e0bfccf305124f255c1e1480cc3dfd9f
@@ -0,0 +1,17 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ indent_style = space
7
+ insert_final_newline = true
8
+ trim_trailing_whitespace = true
9
+
10
+ [*.gemspec]
11
+ indent_size = 2
12
+
13
+ [*.rb]
14
+ indent_size = 2
15
+
16
+ [*.yml]
17
+ indent_size = 2
@@ -1 +1 @@
1
- ruby-2.5.3
1
+ ruby-2.6.0
@@ -3,5 +3,8 @@ sudo: false
3
3
  language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
+ - 2.6.0
6
7
  - 2.5.3
8
+ - 2.4.5
9
+ - 2.3.8
7
10
  before_install: gem install bundler -v 1.17.1
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- viabtc (0.2.0)
5
- faraday (~> 0.15.3)
4
+ viabtc (0.3.0)
5
+ faraday (~> 0.15.4)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  diff-lcs (1.3)
11
- faraday (0.15.3)
11
+ faraday (0.15.4)
12
12
  multipart-post (>= 1.2, < 3)
13
13
  multipart-post (2.0.0)
14
14
  rake (10.5.0)
@@ -36,4 +36,4 @@ DEPENDENCIES
36
36
  viabtc!
37
37
 
38
38
  BUNDLED WITH
39
- 1.17.1
39
+ 1.17.2
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # ViaBTC
1
+ # ViaBTC 📈
2
2
 
3
- [![Build Status](https://travis-ci.org/krmbzds/viabtc.svg?branch=develop)](https://travis-ci.org/krmbzds/viabtc) [![Downloaded](https://img.shields.io/gem/dt/viabtc.svg)](https://rubygems.org/gems/viabtc) [![Gem Version](https://img.shields.io/gem/v/viabtc.svg)](https://rubygems.org/gems/viabtc)
3
+ [![Build Status](https://travis-ci.org/krmbzds/viabtc.svg?branch=develop)](https://travis-ci.org/krmbzds/viabtc) [![Downloaded](https://img.shields.io/gem/dt/viabtc.svg)](https://rubygems.org/gems/viabtc) [![Gem Version](https://img.shields.io/gem/v/viabtc.svg)](https://rubygems.org/gems/viabtc) [![RubyDoc](https://img.shields.io/badge/rubydoc-info-blue.svg)](https://www.rubydoc.info/gems/viabtc/)
4
4
 
5
- An HTTP client to interface with the open-source [ViaBTC Exchange Server][ViaBTC Exchange Server Repo] API.
5
+ An HTTP & WebSocket client to interface with the open-source [ViaBTC Exchange Server][ViaBTC Exchange Server Repo].
6
6
 
7
7
  ## Installation
8
8
 
@@ -14,64 +14,41 @@ If using **Rails**, create a file named `viabtc.rb` under `config/initializers`
14
14
 
15
15
  ```rb
16
16
  ViaBTC.configure do |config|
17
- config.base_url = 'http://localhost:18080'
18
- config.faraday_response = :logger
19
- config.faraday_adapter = :net_http
17
+ config.http_base_url = 'http://localhost:18080'
20
18
  end
21
19
  ```
22
20
 
23
- If not, add it anywhere in your code that runs before a new client is initialized.
21
+ If not, add it anywhere in your code that runs before a new client is initialized. Read more at: 📖[Configuration Wiki](https://github.com/krmbzds/viabtc/wiki/Configuration)
24
22
 
25
23
  ## Usage
26
24
 
27
25
  Create a new instance:
28
26
 
29
27
  ```rb
30
- viabtc_client = ViaBTC::Client.new
28
+ viabtc_http_client = ViaBTC::HTTP::Client.new
31
29
  ```
32
30
 
33
31
  Make an API request:
34
32
 
35
33
  ```rb
36
- viabtc_client.market_status(market: 'ETHBTC')
37
- ```
34
+ viabtc_http_client.market_status(market: 'ETHBTC')
38
35
 
39
- Market status example output:
40
- ```rb
41
- {"error"=>nil, "result"=>{"low"=>"0", "period"=>86400, "last"=>"0", "high"=>"0", "open"=>"0", "volume"=>"0", "close"=>"0", "deal"=>"0"}, "id"=>0}
36
+ #=> {"error"=>nil, "result"=>{"low"=>"0", "period"=>86400, "last"=>"0", "high"=>"0", "open"=>"0", "volume"=>"0", "close"=>"0", "deal"=>"0"}, "id"=>0}
42
37
  ```
43
38
 
44
39
  ## Support
45
40
 
46
- ### API Actions
47
-
48
- #### HTTP Protocol
49
-
50
- | API Method | Corresponding Methods |
51
- |---|---|
52
- | `balance.query` | `#balance` |
53
- | `balance.update` | `#withdraw` `#deposit` |
54
- | `balance.history` | `#balance_history` |
55
- | `asset.list` | `#asset_list` |
56
- | `asset.summary` | `#asset_summary` |
57
- | `order.put_limit` | `#limit_sell` `#limit_buy` |
58
- | `order.put_market` | `#market_sell` `#market_buy` |
59
- | `order.cancel` | `#cancel_order` |
60
- | `order.deals` | `#order_deals` |
61
- | `order.book` | `#sell_orders` `#buy_orders` |
62
- | `order.depth` | `#order_depth` |
63
- | `order.pending` | `#pending_orders` |
64
- | `order.pending_detail` | `#pending_order_details` |
65
- | `order.finished` | `#finished_orders` `#finished_sell_orders` `#finished_buy_orders` |
66
- | `order.finished_detail` | `#finished_order_detail` |
67
- | `market.last` | `#market_last` |
68
- | `market.deals` | `#market_deals` |
69
- | `market.user_deals` | `#user_executed_orders` |
70
- | `market.kline` | `#market_kline` |
71
- | `market.status` | `#market_status` |
72
- | `market.status_today` | `#market_status_today` |
73
- | `market.list` | `#market_list` |
74
- | `market.summary` | `#market_summary` |
41
+ #### Ruby Versions Tested Against
42
+
43
+ - `2.6.0` (stable)
44
+ - ✅ `2.5.3` (stable)
45
+ - `2.4.5` (stable)
46
+ - ⏳ `2.3.8` (security maintenance phase)
47
+
48
+ #### ViaBTC Exchange Server API Support
49
+
50
+ - 📖 [HTTP Protocol](https://github.com/krmbzds/viabtc/wiki/API-Support#http-protocol)
51
+ - 📖 [WebSocket Protocol](https://github.com/krmbzds/viabtc/wiki/API-Support#websocket-protocol)
75
52
 
76
53
  ## Development
77
54
 
@@ -88,7 +65,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
88
65
  5. Push to the branch (`git push origin my-new-feature`)
89
66
  6. Create new Pull Request
90
67
 
91
- ### Donations
68
+ ### Donations ❤️
92
69
 
93
70
  You can donate me at [Librepay][Donation]. Thanks! ☕️
94
71
 
@@ -1,7 +1,8 @@
1
1
  require 'viabtc/version'
2
2
  require 'viabtc/error'
3
3
  require 'viabtc/config'
4
- require 'viabtc/client'
4
+ require 'viabtc/helpers'
5
+ require 'viabtc/http'
5
6
 
6
7
  require 'faraday'
7
8
  require 'json'
@@ -9,10 +9,10 @@ module ViaBTC
9
9
  end
10
10
 
11
11
  class Configuration
12
- attr_accessor :base_url, :faraday_response, :faraday_adapter
12
+ attr_accessor :http_base_url, :http_response, :http_adapter
13
13
 
14
14
  def initialize
15
- @base_url = nil
15
+ @http_base_url = nil
16
16
  end
17
17
  end
18
18
  end
@@ -16,11 +16,12 @@ module ViaBTC
16
16
  end
17
17
 
18
18
  # ViaBTC Exchange Server General Error Codes
19
- InvalidArgument = Class.new(ExchangeError) # 1: Invalid Argument
20
- InternalError = Class.new(ExchangeError) # 2: Internal Error
21
- ServiceUnavailable = Class.new(ExchangeError) # 3: Service Unavailable
22
- MethodNotFound = Class.new(ExchangeError) # 4: Method Not Found
23
- ServiceTimeout = Class.new(ExchangeError) # 5: Service Timeout
19
+ InvalidArgument = Class.new(ExchangeError) # 1: Invalid Argument
20
+ InternalError = Class.new(ExchangeError) # 2: Internal Error
21
+ ServiceUnavailable = Class.new(ExchangeError) # 3: Service Unavailable
22
+ MethodNotFound = Class.new(ExchangeError) # 4: Method Not Found
23
+ ServiceTimeout = Class.new(ExchangeError) # 5: Service Timeout
24
+ RequireAuthentication = Class.new(ExchangeError) # 6: Require Authentication
24
25
 
25
26
  # ViaBTC Exchange Server Undocumented Error Codes with Gotchas
26
27
 
@@ -0,0 +1,47 @@
1
+ module ViaBTC
2
+ module Helpers
3
+
4
+ def response_invalid?(response)
5
+ !response['error'].nil?
6
+ end
7
+
8
+ def amount_valid?(amount)
9
+ amount.is_a?(Numeric) && amount.positive? && (amount.is_a?(Integer) || amount.is_a?(Float))
10
+ end
11
+
12
+ def fee_rate_valid?(fee_rate)
13
+ (0...1).cover?(fee_rate)
14
+ end
15
+
16
+ def now
17
+ Time.now.to_i
18
+ end
19
+
20
+ def raise_exchange_error(response)
21
+ error_code = response['error']['code']
22
+ error_message = response['error']['message']
23
+
24
+ case error_code
25
+ when 1 then raise ViaBTC::Error::InvalidArgument.new(response)
26
+ when 2 then raise ViaBTC::Error::InternalError.new(response)
27
+ when 3 then raise ViaBTC::Error::ServiceUnavailable.new(response)
28
+ when 4 then raise ViaBTC::Error::MethodNotFound.new(response)
29
+ when 5 then raise ViaBTC::Error::ServiceTimeout.new(response)
30
+ when 6 then raise ViaBTC::Error::RequireAuthentication.new(response)
31
+ when 10
32
+ case error_message
33
+ when 'balance not enough' then raise ViaBTC::Error::LimitOrderBalanceNotEnough.new(response)
34
+ when 'repeat update' then raise ViaBTC::Error::RepeatBalanceUpdate.new(response)
35
+ else raise ViaBTC::Error::ExchangeError.new(response)
36
+ end
37
+ when 11
38
+ case error_message
39
+ when 'amount too small' then raise ViaBTC::Error::LimitOrderAmountTooSmall.new(response)
40
+ when 'balance not enough' then raise ViaBTC::Error::BalanceNotEnough.new(response)
41
+ end
42
+ when 12 then raise ViaBTC::Error::LimitOrderNoEnoughTrader.new(response)
43
+ else raise ViaBTC::Error::ExchangeError.new(response)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,201 @@
1
+ module ViaBTC
2
+ module HTTP
3
+ class Client
4
+ include ViaBTC::Helpers
5
+
6
+ def initialize(url: nil, http_response: nil, http_adapter: nil)
7
+ if ViaBTC.configuration
8
+ url ||= ViaBTC.configuration.http_base_url
9
+ http_response ||= ViaBTC.configuration.http_response
10
+ http_adapter ||= ViaBTC.configuration.http_adapter
11
+ end
12
+
13
+ @connection ||= connection(url, http_response, http_adapter)
14
+ end
15
+
16
+ # Asset API
17
+
18
+ def balance(user_id:, id: 0)
19
+ request(id: id, method: 'balance.query', params: [user_id])
20
+ end
21
+
22
+ def withdraw(user_id:, asset:, withdraw_id:, amount:, source: 'web', id: 0)
23
+ raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
24
+ request(
25
+ id: id,
26
+ method: 'balance.update',
27
+ params: [user_id, asset, 'withdraw', withdraw_id, amount, {source: source}]
28
+ )
29
+ end
30
+
31
+ def deposit(user_id:, asset:, deposit_id:, amount:, source: 'web', id: 0)
32
+ raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
33
+ request(
34
+ id: id,
35
+ method: 'balance.update',
36
+ params: [user_id, asset, 'deposit', deposit_id, amount, {source: source}]
37
+ )
38
+ end
39
+
40
+ def balance_history(user_id:, asset:, business: '', start_time: 0, end_time: 0, offset: 0, limit: 100, id: 0)
41
+ request(id: id, method: 'balance.history', params: [user_id, asset, business, start_time, end_time, offset, limit])
42
+ end
43
+
44
+ def asset_list
45
+ request(method: 'asset.list', params: [])
46
+ end
47
+
48
+ def asset_summary
49
+ request(method: 'asset.summary', params: [])
50
+ end
51
+
52
+ # Trade API
53
+
54
+ def limit_sell(user_id:, market:, amount:, price:, taker_fee_rate:, maker_fee_rate:, source: 'web', id: 0)
55
+ raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
56
+ raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
57
+ raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{maker_fee_rate}" unless fee_rate_valid?(maker_fee_rate)
58
+ request(
59
+ id: id,
60
+ method: 'order.put_limit',
61
+ params: [user_id, market, 1, amount, price, taker_fee_rate, maker_fee_rate, source]
62
+ )
63
+ end
64
+
65
+ def limit_buy(user_id:, market:, amount:, price:, taker_fee_rate:, maker_fee_rate:, source: 'web', id: 0)
66
+ raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
67
+ raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
68
+ raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{maker_fee_rate}" unless fee_rate_valid?(maker_fee_rate)
69
+ request(
70
+ id: id,
71
+ method: 'order.put_limit',
72
+ params: [user_id, market, 2, amount, price, taker_fee_rate, maker_fee_rate, source]
73
+ )
74
+ end
75
+
76
+ def market_sell(user_id:, market:, amount:, taker_fee_rate:, source: 'web', id: 0)
77
+ raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
78
+ raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
79
+ request(
80
+ id: id,
81
+ method: 'order.put_market',
82
+ params: [user_id, market, 1, amount, taker_fee_rate.to_s, source]
83
+ )
84
+ end
85
+
86
+ def market_buy(user_id:, market:, amount:, taker_fee_rate:, source: 'web', id: 0)
87
+ raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
88
+ raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
89
+ request(
90
+ id: id,
91
+ method: 'order.put_market',
92
+ params: [user_id, market, 2, amount, taker_fee_rate.to_s, source]
93
+ )
94
+ end
95
+
96
+ def cancel_order(user_id:, market:, order_id:, id: 0)
97
+ request(id: id, method: 'order.cancel', params: [user_id, market, order_id])
98
+ end
99
+
100
+ def order_deals(order_id:, offset: 0, limit: 100, id:0)
101
+ request(id: id, method: 'order.deals', params: [order_id, offset, limit])
102
+ end
103
+
104
+ def sell_orders(market:, offset: 0, limit: 100, id: 0)
105
+ request(id: id, method: 'order.book', params: [market, 1, offset, limit])
106
+ end
107
+
108
+ def buy_orders(market:, offset: 0, limit: 100, id: 0)
109
+ request(id: id, method: 'order.book', params: [market, 2, offset, limit])
110
+ end
111
+
112
+ def order_depth(market:, limit: 100, interval: '0', id: 0)
113
+ request(id: id, method: 'order.depth', params: [market, limit, interval])
114
+ end
115
+
116
+ def pending_orders(user_id:, market:, offset: 0, limit: 100, id: 0)
117
+ request(id: id, method: 'order.pending', params: [user_id, market, offset, limit])
118
+ end
119
+
120
+ def pending_order_details(order_id:, market:, id: 0)
121
+ request(id: id, method: 'order.pending_detail', params: [market, order_id])
122
+ end
123
+
124
+ def finished_orders(user_id:, market:, start_time: 0, end_time: 0, offset: 0, limit: 100, side:0, id: 0)
125
+ request(id: id, method: 'order.finished', params: [user_id, market, start_time, end_time, offset, limit, side])
126
+ end
127
+
128
+ def finished_sell_orders(user_id:, market:, start_time: 0, end_time: 0, offset: 0, limit: 100, side:1, id: 0)
129
+ request(id: id, method: 'order.finished', params: [user_id, market, start_time, end_time, offset, limit, side])
130
+ end
131
+
132
+ def finished_buy_orders(user_id:, market:, start_time: 0, end_time: 0, offset: 0, limit: 100, side:2, id: 0)
133
+ request(id: id, method: 'order.finished', params: [user_id, market, start_time, end_time, offset, limit, side])
134
+ end
135
+
136
+ def finished_order_detail(order_id:, id: 0)
137
+ request(id: id, method: 'order.finished_detail', params: [order_id])
138
+ end
139
+
140
+ # Market API
141
+
142
+ def market_last(market:, id: 0)
143
+ request(id: id, method: 'market.last', params: [market])
144
+ end
145
+
146
+ def market_deals(market:, limit: 10000, last_id: 0, id: 0)
147
+ request(id: id, method: 'market.deals', params: [market, limit, last_id])
148
+ end
149
+
150
+ def user_executed_orders(user_id:, market:, offset: 0, limit: 100, id: 0)
151
+ request(id: id, method: 'market.user_deals', params: [user_id, market, offset, limit])
152
+ end
153
+
154
+ def market_kline(market:, start_time: now - 86400, end_time: now, interval: 3600, id: 0)
155
+ request(id: id, method: 'market.kline', params: [market, start_time, end_time, interval])
156
+ end
157
+
158
+ def market_status(market:, period: 86400, id: 0)
159
+ request(id: id, method: 'market.status', params: [market, period])
160
+ end
161
+
162
+ def market_status_today(market:, id: 0)
163
+ market_status(market: market, period: 86400, id: id)
164
+ end
165
+
166
+ def market_list(id: 0)
167
+ request(id: id, method: 'market.list', params: [])
168
+ end
169
+
170
+ def market_summary(id: 0)
171
+ request(id: id, method: 'market.summary', params: [])
172
+ end
173
+
174
+ private
175
+
176
+ def connection(url, http_response, http_adapter)
177
+ raise ViaBTC::Error::Configuration, 'required: url' unless url
178
+ http_response ||= :logger
179
+ http_adapter ||= :net_http
180
+ Faraday.new(url) do |conn|
181
+ conn.response http_response
182
+ conn.adapter http_adapter
183
+ conn.headers['Content-Type'] = ['application/json']
184
+ end
185
+ end
186
+
187
+ def request(method:, params:, id: 0)
188
+ response = @connection.post do |req|
189
+ req.body = {
190
+ 'id' => id,
191
+ 'method' => method,
192
+ 'params' => params
193
+ }.to_json
194
+ end
195
+ response = JSON.parse(response.body)
196
+ raise_exchange_error(response) if response_invalid?(response)
197
+ response
198
+ end
199
+ end
200
+ end
201
+ end
@@ -1,3 +1,3 @@
1
1
  module ViaBTC
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.required_ruby_version = '>= 2.1.0'
29
29
 
30
30
  # Runtime dependencies
31
- spec.add_dependency 'faraday', '~> 0.15.3'
31
+ spec.add_dependency 'faraday', '~> 0.15.4'
32
32
 
33
33
  # Development dependencies
34
34
  spec.add_development_dependency 'bundler', '~> 1.17'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viabtc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kerem Bozdas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-19 00:00:00.000000000 Z
11
+ date: 2018-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.15.3
19
+ version: 0.15.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.15.3
26
+ version: 0.15.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -73,6 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".editorconfig"
76
77
  - ".gitignore"
77
78
  - ".rspec"
78
79
  - ".ruby-gemset"
@@ -86,9 +87,10 @@ files:
86
87
  - bin/console
87
88
  - bin/setup
88
89
  - lib/viabtc.rb
89
- - lib/viabtc/client.rb
90
90
  - lib/viabtc/config.rb
91
91
  - lib/viabtc/error.rb
92
+ - lib/viabtc/helpers.rb
93
+ - lib/viabtc/http.rb
92
94
  - lib/viabtc/version.rb
93
95
  - viabtc.gemspec
94
96
  homepage: https://github.com/krmbzds/viabtc/
@@ -110,8 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
112
  - !ruby/object:Gem::Version
111
113
  version: '0'
112
114
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 2.7.6
115
+ rubygems_version: 3.0.1
115
116
  signing_key:
116
117
  specification_version: 4
117
118
  summary: ViaBTC Exchange Server API Wrapper
@@ -1,240 +0,0 @@
1
- module ViaBTC
2
- class Client
3
- def initialize(base_url: nil, faraday_response: nil, faraday_adapter: nil)
4
- if ViaBTC.configuration
5
- base_url ||= ViaBTC.configuration.base_url
6
- faraday_response ||= ViaBTC.configuration.faraday_response
7
- faraday_adapter ||= ViaBTC.configuration.faraday_adapter
8
- end
9
-
10
- @connection ||= connection(base_url, faraday_response, faraday_adapter)
11
- end
12
-
13
- # Asset API
14
-
15
- def balance(user_id:, id: 0)
16
- request(id: id, method: 'balance.query', params: [user_id])
17
- end
18
-
19
- def withdraw(user_id:, asset:, withdraw_id:, amount:, source: 'web', id: 0)
20
- raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
21
- request(
22
- id: id,
23
- method: 'balance.update',
24
- params: [user_id, asset, 'withdraw', withdraw_id, amount, {source: source}]
25
- )
26
- end
27
-
28
- def deposit(user_id:, asset:, deposit_id:, amount:, source: 'web', id: 0)
29
- raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
30
- request(
31
- id: id,
32
- method: 'balance.update',
33
- params: [user_id, asset, 'deposit', deposit_id, amount, {source: source}]
34
- )
35
- end
36
-
37
- def balance_history(user_id:, asset:, business: '', start_time: 0, end_time: 0, offset: 0, limit: 100, id: 0)
38
- request(id: id, method: 'balance.history', params: [user_id, asset, business, start_time, end_time, offset, limit])
39
- end
40
-
41
- def asset_list
42
- request(method: 'asset.list', params: [])
43
- end
44
-
45
- def asset_summary
46
- request(method: 'asset.summary', params: [])
47
- end
48
-
49
- # Trade API
50
-
51
- def limit_sell(user_id:, market:, amount:, price:, taker_fee_rate:, maker_fee_rate:, source: 'web', id: 0)
52
- raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
53
- raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
54
- raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{maker_fee_rate}" unless fee_rate_valid?(maker_fee_rate)
55
- request(
56
- id: id,
57
- method: 'order.put_limit',
58
- params: [user_id, market, 1, amount, price, taker_fee_rate, maker_fee_rate, source]
59
- )
60
- end
61
-
62
- def limit_buy(user_id:, market:, amount:, price:, taker_fee_rate:, maker_fee_rate:, source: 'web', id: 0)
63
- raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
64
- raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
65
- raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{maker_fee_rate}" unless fee_rate_valid?(maker_fee_rate)
66
- request(
67
- id: id,
68
- method: 'order.put_limit',
69
- params: [user_id, market, 2, amount, price, taker_fee_rate, maker_fee_rate, source]
70
- )
71
- end
72
-
73
- def market_sell(user_id:, market:, amount:, taker_fee_rate:, source: 'web', id: 0)
74
- raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
75
- raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
76
- request(
77
- id: id,
78
- method: 'order.put_market',
79
- params: [user_id, market, 1, amount, taker_fee_rate.to_s, source]
80
- )
81
- end
82
-
83
- def market_buy(user_id:, market:, amount:, taker_fee_rate:, source: 'web', id: 0)
84
- raise ViaBTC::Error::InvalidParameter, "invalid amount: #{amount}" unless amount_valid?(amount)
85
- raise ViaBTC::Error::InvalidParameter, "invalid fee rate #{taker_fee_rate}" unless fee_rate_valid?(taker_fee_rate)
86
- request(
87
- id: id,
88
- method: 'order.put_market',
89
- params: [user_id, market, 2, amount, taker_fee_rate.to_s, source]
90
- )
91
- end
92
-
93
- def cancel_order(user_id:, market:, order_id:, id: 0)
94
- request(id: id, method: 'order.cancel', params: [user_id, market, order_id])
95
- end
96
-
97
- def order_deals(order_id:, offset: 0, limit: 100, id:0)
98
- request(id: id, method: 'order.deals', params: [order_id, offset, limit])
99
- end
100
-
101
- def sell_orders(market:, offset: 0, limit: 100, id: 0)
102
- request(id: id, method: 'order.book', params: [market, 1, offset, limit])
103
- end
104
-
105
- def buy_orders(market:, offset: 0, limit: 100, id: 0)
106
- request(id: id, method: 'order.book', params: [market, 2, offset, limit])
107
- end
108
-
109
- def order_depth(market:, limit: 100, interval: '0', id: 0)
110
- request(id: id, method: 'order.depth', params: [market, limit, interval])
111
- end
112
-
113
- def pending_orders(user_id:, market:, offset: 0, limit: 100, id: 0)
114
- request(id: id, method: 'order.pending', params: [user_id, market, offset, limit])
115
- end
116
-
117
- def pending_order_details(order_id:, market:, id: 0)
118
- request(id: id, method: 'order.pending_detail', params: [market, order_id])
119
- end
120
-
121
- def finished_orders(user_id:, market:, start_time: 0, end_time: 0, offset: 0, limit: 100, side:0, id: 0)
122
- request(id: id, method: 'order.finished', params: [user_id, market, start_time, end_time, offset, limit, side])
123
- end
124
-
125
- def finished_sell_orders(user_id:, market:, start_time: 0, end_time: 0, offset: 0, limit: 100, side:1, id: 0)
126
- request(id: id, method: 'order.finished', params: [user_id, market, start_time, end_time, offset, limit, side])
127
- end
128
-
129
- def finished_buy_orders(user_id:, market:, start_time: 0, end_time: 0, offset: 0, limit: 100, side:2, id: 0)
130
- request(id: id, method: 'order.finished', params: [user_id, market, start_time, end_time, offset, limit, side])
131
- end
132
-
133
- def finished_order_detail(order_id:, id: 0)
134
- request(id: id, method: 'order.finished_detail', params: [order_id])
135
- end
136
-
137
- # Market API
138
-
139
- def market_last(market:, id: 0)
140
- request(id: id, method: 'market.last', params: [market])
141
- end
142
-
143
- def market_deals(market:, limit: 10000, last_id: 0, id: 0)
144
- request(id: id, method: 'market.deals', params: [market, limit, last_id])
145
- end
146
-
147
- def user_executed_orders(user_id:, market:, offset: 0, limit: 100, id: 0)
148
- request(id: id, method: 'market.user_deals', params: [user_id, market, offset, limit])
149
- end
150
-
151
- def market_kline(market:, start_time: now - 86400, end_time: now, interval: 3600, id: 0)
152
- request(id: id, method: 'market.kline', params: [market, start_time, end_time, interval])
153
- end
154
-
155
- def market_status(market:, period: 86400, id: 0)
156
- request(id: id, method: 'market.status', params: [market, period])
157
- end
158
-
159
- def market_status_today(market:, id: 0)
160
- market_status(market: market, period: 86400, id: id)
161
- end
162
-
163
- def market_list(id: 0)
164
- request(id: id, method: 'market.list', params: [])
165
- end
166
-
167
- def market_summary(id: 0)
168
- request(id: id, method: 'market.summary', params: [])
169
- end
170
-
171
- private
172
-
173
- def connection(base_url, faraday_response, faraday_adapter)
174
- raise ViaBTC::Error::Configuration, 'required: base_url' unless base_url
175
- faraday_response ||= :logger
176
- faraday_adapter ||= :net_http
177
- Faraday.new(base_url) do |conn|
178
- conn.response faraday_response
179
- conn.adapter faraday_adapter
180
- conn.headers['Content-Type'] = ['application/json']
181
- end
182
- end
183
-
184
- def request(method:, params:, id: 0)
185
- response = @connection.post do |req|
186
- req.body = {
187
- 'id' => id,
188
- 'method' => method,
189
- 'params' => params
190
- }.to_json
191
- end
192
- response = JSON.parse(response.body)
193
- raise_exchange_error(response) if response_invalid?(response)
194
- response
195
- end
196
-
197
- def response_invalid?(response)
198
- !response['error'].nil?
199
- end
200
-
201
- def amount_valid?(amount)
202
- amount.is_a?(Numeric) && amount.positive? && (amount.is_a?(Integer) || amount.is_a?(Float))
203
- end
204
-
205
- def fee_rate_valid?(fee_rate)
206
- (0...1).cover?(fee_rate)
207
- end
208
-
209
- def now
210
- Time.now.to_i
211
- end
212
-
213
- def raise_exchange_error(response)
214
- error_code = response['error']['code']
215
- error_message = response['error']['message']
216
-
217
- case error_code
218
- when 1 then raise ViaBTC::Error::InvalidArgument.new(response)
219
- when 2 then raise ViaBTC::Error::InternalError.new(response)
220
- when 3 then raise ViaBTC::Error::ServiceUnavailable.new(response)
221
- when 4 then raise ViaBTC::Error::MethodNotFound.new(response)
222
- when 5 then raise ViaBTC::Error::ServiceTimeout.new(response)
223
- when 10
224
- case error_message
225
- when 'balance not enough' then raise ViaBTC::Error::LimitOrderBalanceNotEnough.new(response)
226
- when 'repeat update' then raise ViaBTC::Error::RepeatBalanceUpdate.new(response)
227
- else raise ViaBTC::Error::ExchangeError.new(response)
228
- end
229
- when 11
230
- case error_message
231
- when 'amount too small' then raise ViaBTC::Error::LimitOrderAmountTooSmall.new(response)
232
- when 'balance not enough' then raise ViaBTC::Error::BalanceNotEnough.new(response)
233
- end
234
- when 12 then raise ViaBTC::Error::LimitOrderNoEnoughTrader.new(response)
235
- else raise ViaBTC::Error::ExchangeError.new(response)
236
- end
237
- end
238
-
239
- end
240
- end