bitfinex-rb 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bitfinex.rb +28 -30
  3. data/lib/{bitfinex/errors.rb → errors.rb} +2 -2
  4. data/lib/models/alert.rb +27 -0
  5. data/lib/models/balance_info.rb +25 -0
  6. data/lib/models/candle.rb +29 -0
  7. data/lib/models/currency.rb +27 -0
  8. data/lib/models/funding_credit.rb +41 -0
  9. data/lib/models/funding_info.rb +40 -0
  10. data/lib/models/funding_loan.rb +40 -0
  11. data/lib/models/funding_offer.rb +38 -0
  12. data/lib/models/funding_ticker.rb +50 -0
  13. data/lib/models/funding_trade.rb +31 -0
  14. data/lib/models/ledger_entry.rb +33 -0
  15. data/lib/models/margin_info.rb +67 -0
  16. data/lib/models/model.rb +56 -0
  17. data/lib/models/movement.rb +33 -0
  18. data/lib/models/notification.rb +30 -0
  19. data/lib/models/order.rb +197 -0
  20. data/lib/models/order_book.rb +265 -0
  21. data/lib/models/position.rb +33 -0
  22. data/lib/models/public_trade.rb +27 -0
  23. data/lib/models/trade.rb +34 -0
  24. data/lib/models/trading_ticker.rb +33 -0
  25. data/lib/models/user_info.rb +27 -0
  26. data/lib/models/wallet.rb +28 -0
  27. data/lib/rest/rest_client.rb +103 -0
  28. data/lib/rest/v1.rb +63 -0
  29. data/lib/rest/v1/account_info.rb +23 -0
  30. data/lib/{bitfinex → rest}/v1/deposit.rb +2 -3
  31. data/lib/{bitfinex → rest}/v1/funding_book.rb +2 -4
  32. data/lib/{bitfinex → rest}/v1/historical_data.rb +2 -2
  33. data/lib/{bitfinex → rest}/v1/lends.rb +1 -2
  34. data/lib/{bitfinex → rest}/v1/margin_funding.rb +1 -2
  35. data/lib/rest/v1/order_book.rb +17 -0
  36. data/lib/{bitfinex → rest}/v1/orders.rb +6 -8
  37. data/lib/{bitfinex → rest}/v1/positions.rb +6 -2
  38. data/lib/{bitfinex → rest}/v1/stats.rb +1 -3
  39. data/lib/{bitfinex → rest}/v1/symbols.rb +1 -2
  40. data/lib/rest/v1/ticker.rb +13 -0
  41. data/lib/{bitfinex → rest}/v1/trades.rb +1 -16
  42. data/lib/{bitfinex → rest}/v1/wallet.rb +1 -2
  43. data/lib/rest/v2.rb +47 -0
  44. data/lib/{bitfinex → rest}/v2/margin.rb +1 -2
  45. data/lib/{bitfinex → rest}/v2/personal.rb +2 -18
  46. data/lib/{bitfinex → rest}/v2/stats.rb +1 -4
  47. data/lib/rest/v2/ticker.rb +20 -0
  48. data/lib/{bitfinex → rest}/v2/trading.rb +3 -49
  49. data/lib/{bitfinex → rest}/v2/utils.rb +1 -3
  50. data/lib/ws/ws2.rb +529 -0
  51. metadata +74 -56
  52. data/lib/bitfinex-api-rb.rb +0 -1
  53. data/lib/bitfinex-rb.rb +0 -1
  54. data/lib/bitfinex/api_versions.rb +0 -7
  55. data/lib/bitfinex/authenticated_rest.rb +0 -57
  56. data/lib/bitfinex/client.rb +0 -38
  57. data/lib/bitfinex/configurable.rb +0 -48
  58. data/lib/bitfinex/connection.rb +0 -52
  59. data/lib/bitfinex/v1/account_info.rb +0 -38
  60. data/lib/bitfinex/v1/orderbook.rb +0 -36
  61. data/lib/bitfinex/v1/ticker.rb +0 -27
  62. data/lib/bitfinex/v2/ticker.rb +0 -58
  63. data/lib/bitfinex/version.rb +0 -3
  64. data/lib/bitfinex/websocket_connection.rb +0 -231
@@ -0,0 +1,33 @@
1
+ require_relative './model'
2
+
3
+ module Bitfinex
4
+ module Models
5
+ class Position < Model
6
+ BOOL_FIELDS = []
7
+ FIELDS = {
8
+ :symbol => 0,
9
+ :status => 1,
10
+ :amount => 2,
11
+ :base_price => 3,
12
+ :margin_funding => 4,
13
+ :margin_funding_type => 5,
14
+ :pl => 6,
15
+ :pl_perc => 7,
16
+ :liquidation_price => 8,
17
+ :leverage => 9
18
+ }
19
+
20
+ FIELDS.each do |key, index|
21
+ attr_accessor key
22
+ end
23
+
24
+ def initialize (data)
25
+ super(data, FIELDS, BOOL_FIELDS)
26
+ end
27
+
28
+ def self.unserialize (data)
29
+ return Model.unserialize(data, FIELDS, BOOL_FIELDS)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require_relative './model'
2
+
3
+ module Bitfinex
4
+ module Models
5
+ class PublicTrade < Model
6
+ BOOL_FIELDS = []
7
+ FIELDS = {
8
+ :id => 0,
9
+ :mts => 1,
10
+ :amount => 2,
11
+ :price => 3
12
+ }
13
+
14
+ FIELDS.each do |key, index|
15
+ attr_accessor key
16
+ end
17
+
18
+ def initialize (data)
19
+ super(data, FIELDS, BOOL_FIELDS)
20
+ end
21
+
22
+ def self.unserialize (data)
23
+ return Model.unserialize(data, FIELDS, BOOL_FIELDS)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ require_relative './model'
2
+
3
+ module Bitfinex
4
+ module Models
5
+ class Trade < Model
6
+ BOOL_FIELDS = []
7
+ FIELDS = {
8
+ id: 0,
9
+ symbol: 1,
10
+ mts_create: 2,
11
+ order_id: 3,
12
+ exec_amount: 4,
13
+ exec_price: 5,
14
+ order_type: 6,
15
+ order_price: 7,
16
+ maker: 8,
17
+ fee: 9,
18
+ fee_currency: 10
19
+ }
20
+
21
+ FIELDS.each do |key, index|
22
+ attr_accessor key
23
+ end
24
+
25
+ def initialize (data)
26
+ super(data, FIELDS, BOOL_FIELDS)
27
+ end
28
+
29
+ def self.unserialize (data)
30
+ return Model.unserialize(data, FIELDS, BOOL_FIELDS)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ require_relative './model'
2
+
3
+ module Bitfinex
4
+ module Models
5
+ class TradingTicker < Model
6
+ BOOL_FIELDS = []
7
+ FIELDS = {
8
+ bid: 0,
9
+ bid_size: 1,
10
+ ask: 2,
11
+ ask_size: 3,
12
+ daily_change: 4,
13
+ daily_change_perc: 5,
14
+ last_price: 6,
15
+ volume: 7,
16
+ high: 8,
17
+ low: 9
18
+ }
19
+
20
+ FIELDS.each do |key, index|
21
+ attr_accessor key
22
+ end
23
+
24
+ def initialize (data)
25
+ super(data, FIELDS, BOOL_FIELDS)
26
+ end
27
+
28
+ def self.unserialize (data)
29
+ return Model.unserialize(data, FIELDS, BOOL_FIELDS)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require_relative './model'
2
+
3
+ module Bitfinex
4
+ module Models
5
+ class UserInfo < Model
6
+ BOOL_FIELDS = []
7
+ FIELDS = {
8
+ :id => 0,
9
+ :email => 1,
10
+ :username => 2,
11
+ :timezone => 7
12
+ }
13
+
14
+ FIELDS.each do |key, index|
15
+ attr_accessor key
16
+ end
17
+
18
+ def initialize (data)
19
+ super(data, FIELDS, BOOL_FIELDS)
20
+ end
21
+
22
+ def self.unserialize (data)
23
+ return Model.unserialize(data, FIELDS, BOOL_FIELDS)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require_relative './model'
2
+
3
+ module Bitfinex
4
+ module Models
5
+ class Wallet < Model
6
+ BOOL_FIELDS = []
7
+ FIELDS = {
8
+ :type => 0,
9
+ :currency => 1,
10
+ :balance => 2,
11
+ :unsettled_interest => 3,
12
+ :balance_available => 4
13
+ }
14
+
15
+ FIELDS.each do |key, index|
16
+ attr_accessor key
17
+ end
18
+
19
+ def initialize (data)
20
+ super(data, FIELDS, BOOL_FIELDS)
21
+ end
22
+
23
+ def self.unserialize (data)
24
+ return Model.unserialize(data, FIELDS, BOOL_FIELDS)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,103 @@
1
+ require 'faraday_adapter_socks'
2
+
3
+ module Bitfinex
4
+ module RESTClient
5
+ def check_params(params, allowed_params)
6
+ if (params.keys - allowed_params).empty?
7
+ return params
8
+ else
9
+ raise Bitfinex::ParamsError
10
+ end
11
+ end
12
+
13
+ private
14
+ def get(url, params={})
15
+ rest_connection.get do |req|
16
+ req.url build_url(url)
17
+ req.headers['Content-Type'] = 'application/json'
18
+ req.headers['Accept'] = 'application/json'
19
+
20
+ params.each do |k,v|
21
+ req.params[k] = v
22
+ end
23
+
24
+ req.options.timeout = config[:rest_timeout]
25
+ req.options.open_timeout = config[:rest_open_timeout]
26
+ end
27
+ end
28
+
29
+ def rest_connection
30
+ @conn ||= new_rest_connection
31
+ end
32
+
33
+ def build_url(url)
34
+ URI.join(base_api_endpoint, url)
35
+ end
36
+
37
+ def new_rest_connection
38
+ Faraday.new(url: base_api_endpoint, :proxy => config[:proxy]) do |conn|
39
+ conn.use Bitfinex::CustomErrors
40
+ conn.response :logger, Logger.new(STDOUT), bodies: true if config[:debug_connection]
41
+ conn.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
42
+ conn.adapter :net_http_socks
43
+ end
44
+ end
45
+
46
+ def base_api_endpoint
47
+ config[:api_endpoint]
48
+ end
49
+
50
+ private
51
+ def authenticated_post(url, options = {})
52
+ raise Bitfinex::InvalidAuthKeyError unless valid_key?
53
+ complete_url = build_url(url)
54
+ body = options[:params] || {}
55
+ nonce = new_nonce
56
+
57
+ payload = if config[:api_version] == 1
58
+ build_payload("/v1/#{url}", options[:params], nonce)
59
+ else
60
+ "/api/v2/#{url}#{nonce}#{body.to_json}"
61
+ end
62
+
63
+ response = rest_connection.post do |req|
64
+ req.url complete_url
65
+ req.body = body.to_json
66
+ req.options.timeout = config[:rest_timeout]
67
+ req.options.open_timeout = config[:rest_open_timeout]
68
+ req.headers['Content-Type'] = 'application/json'
69
+ req.headers['Accept'] = 'application/json'
70
+
71
+ if config[:api_version] == 1
72
+ req.headers['X-BFX-PAYLOAD'] = payload
73
+ req.headers['X-BFX-SIGNATURE'] = sign(payload)
74
+ req.headers['X-BFX-APIKEY'] = config[:api_key]
75
+ else
76
+ req.headers['bfx-nonce'] = nonce
77
+ req.headers['bfx-signature'] = sign(payload)
78
+ req.headers['bfx-apikey'] = config[:api_key]
79
+ end
80
+ end
81
+ end
82
+
83
+ def build_payload(url, params = {}, nonce)
84
+ payload = {}
85
+ payload['nonce'] = nonce
86
+ payload['request'] = url
87
+ payload.merge!(params) if params
88
+ Base64.strict_encode64(payload.to_json)
89
+ end
90
+
91
+ def new_nonce
92
+ Time.now.to_i.to_s
93
+ end
94
+
95
+ def sign(payload)
96
+ OpenSSL::HMAC.hexdigest('sha384', config[:api_secret], payload)
97
+ end
98
+
99
+ def valid_key?
100
+ !! (config[:api_key] && config[:api_secret])
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,63 @@
1
+ require_relative './rest_client'
2
+ require_relative './v1/account_info'
3
+ require_relative './v1/deposit'
4
+ require_relative './v1/funding_book'
5
+ require_relative './v1/historical_data'
6
+ require_relative './v1/lends'
7
+ require_relative './v1/margin_funding'
8
+ require_relative './v1/order_book'
9
+ require_relative './v1/orders'
10
+ require_relative './v1/positions'
11
+ require_relative './v1/stats'
12
+ require_relative './v1/symbols'
13
+ require_relative './v1/ticker'
14
+ require_relative './v1/trades'
15
+ require_relative './v1/wallet'
16
+
17
+ module Bitfinex
18
+ class RESTv1
19
+ attr_accessor :api_endpoint, :debug, :debug_connection, :api_version
20
+ attr_accessor :rest_timeout, :rest_open_timeout, :proxy
21
+ attr_accessor :api_key, :api_secret
22
+
23
+ include Bitfinex::RESTClient
24
+ include Bitfinex::RESTv1AccountInfo
25
+ include Bitfinex::RESTv1Deposit
26
+ include Bitfinex::RESTv1FundingBook
27
+ include Bitfinex::RESTv1HistoricalData
28
+ include Bitfinex::RESTv1Lends
29
+ include Bitfinex::RESTv1MarginFunding
30
+ include Bitfinex::RESTv1OrderBook
31
+ include Bitfinex::RESTv1Orders
32
+ include Bitfinex::RESTv1Positions
33
+ include Bitfinex::RESTv1Stats
34
+ include Bitfinex::RESTv1Symbols
35
+ include Bitfinex::RESTv1Ticker
36
+ include Bitfinex::RESTv1Trades
37
+ include Bitfinex::RESTv1Wallet
38
+
39
+ def initialize(args = {})
40
+ self.api_endpoint = args[:url] ? "#{args[:url]}/v1/" : "https://api.bitfinex.com/v1/"
41
+ self.proxy = args[:proxy] || nil
42
+ self.debug_connection = false
43
+ self.api_version = 1
44
+ self.rest_timeout = 30
45
+ self.rest_open_timeout = 30
46
+ self.api_key = args[:api_key]
47
+ self.api_secret = args[:api_secret]
48
+ end
49
+
50
+ def config
51
+ {
52
+ :api_endpoint => self.api_endpoint,
53
+ :debug_connection => self.debug_connection,
54
+ :api_version => self.api_version,
55
+ :rest_timeout => self.rest_timeout,
56
+ :rest_open_timeout => self.rest_open_timeout,
57
+ :proxy => self.proxy,
58
+ :api_key => self.api_key,
59
+ :api_secret => self.api_secret
60
+ }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,23 @@
1
+ module Bitfinex
2
+ module RESTv1AccountInfo
3
+ # Get account information
4
+ #
5
+ # @return [Hash] your account information
6
+ # @example:
7
+ # client.account_info
8
+ def account_info
9
+ resp = authenticated_post("account_infos")
10
+ resp.body
11
+ end
12
+
13
+ # See the fees applied to your withdrawals
14
+ #
15
+ # @return [Hash]
16
+ # @example:
17
+ # client.fees
18
+ def fees
19
+ resp = authenticated_post("account_fees")
20
+ resp.body
21
+ end
22
+ end
23
+ end
@@ -1,6 +1,5 @@
1
1
  module Bitfinex
2
- module V1::DepositClient
3
-
2
+ module RESTv1Deposit
4
3
  # Return your deposit address to make a new deposit.
5
4
  #
6
5
  # @param method [string] Method of deposit (methods accepted: “bitcoin”, “litecoin”, “darkcoin”, “mastercoin” (tethers)).
@@ -10,7 +9,7 @@ module Bitfinex
10
9
  # @return [Hash] confirmation of your deposit
11
10
  # @example:
12
11
  # client.deposit("bitcoin", "exchange")
13
- def deposit method, wallet_name, renew=0
12
+ def deposit (method, wallet_name, renew=0)
14
13
  params = {
15
14
  method: method,
16
15
  wallet_name: wallet_name,
@@ -1,8 +1,7 @@
1
1
  module Bitfinex
2
- module V1::FundingBookClient
3
-
2
+ module RESTv1FundingBook
4
3
  # Get the full margin funding book
5
-
4
+ #
6
5
  # @param currency [string] (optional) Speficy the currency, default "USD"
7
6
  # @param params :limit_bids [int] (optional) Limit the number of funding bids returned. May be 0 in which case the array of bids is empty.
8
7
  # @param params :limit_asks [int] (optional) Limit the number of funding offers returned. May be 0 in which case the array of asks is empty.
@@ -13,6 +12,5 @@ module Bitfinex
13
12
  check_params(params, %i{limit_bids limit_asks})
14
13
  get("lendbook/#{currency}", params: params).body
15
14
  end
16
-
17
15
  end
18
16
  end
@@ -1,5 +1,5 @@
1
1
  module Bitfinex
2
- module V1::HistoricalDataClient
2
+ module RESTv1HistoricalData
3
3
 
4
4
  # View all of your balance ledger entries.
5
5
  #
@@ -28,7 +28,7 @@ module Bitfinex
28
28
  # @example:
29
29
  # client.movements
30
30
  def movements(currency="usd", params = {})
31
- check_params(params, %i{method since until limit})
31
+ check_params(params, %i{method since until limit since_movement until_movement})
32
32
  params.merge!({currency: currency})
33
33
  authenticated_post("history/movements", params: params).body
34
34
  end
@@ -1,5 +1,5 @@
1
1
  module Bitfinex
2
- module V1::LendsClient
2
+ module RESTv1Lends
3
3
 
4
4
  # Get a list of the most recent funding data for the given currency: total amount provided and Flash Return Rate (in % by 365 days) over time.
5
5
  #
@@ -13,6 +13,5 @@ module Bitfinex
13
13
  check_params(params, %i{timestamp limit_lends})
14
14
  get("lends/#{currency}", params: params).body
15
15
  end
16
-
17
16
  end
18
17
  end