bitfinex-rb 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bitfinex.rb +28 -30
- data/lib/{bitfinex/errors.rb → errors.rb} +2 -2
- data/lib/models/alert.rb +27 -0
- data/lib/models/balance_info.rb +25 -0
- data/lib/models/candle.rb +29 -0
- data/lib/models/currency.rb +27 -0
- data/lib/models/funding_credit.rb +41 -0
- data/lib/models/funding_info.rb +40 -0
- data/lib/models/funding_loan.rb +40 -0
- data/lib/models/funding_offer.rb +38 -0
- data/lib/models/funding_ticker.rb +50 -0
- data/lib/models/funding_trade.rb +31 -0
- data/lib/models/ledger_entry.rb +33 -0
- data/lib/models/margin_info.rb +67 -0
- data/lib/models/model.rb +56 -0
- data/lib/models/movement.rb +33 -0
- data/lib/models/notification.rb +30 -0
- data/lib/models/order.rb +197 -0
- data/lib/models/order_book.rb +265 -0
- data/lib/models/position.rb +33 -0
- data/lib/models/public_trade.rb +27 -0
- data/lib/models/trade.rb +34 -0
- data/lib/models/trading_ticker.rb +33 -0
- data/lib/models/user_info.rb +27 -0
- data/lib/models/wallet.rb +28 -0
- data/lib/rest/rest_client.rb +103 -0
- data/lib/rest/v1.rb +63 -0
- data/lib/rest/v1/account_info.rb +23 -0
- data/lib/{bitfinex → rest}/v1/deposit.rb +2 -3
- data/lib/{bitfinex → rest}/v1/funding_book.rb +2 -4
- data/lib/{bitfinex → rest}/v1/historical_data.rb +2 -2
- data/lib/{bitfinex → rest}/v1/lends.rb +1 -2
- data/lib/{bitfinex → rest}/v1/margin_funding.rb +1 -2
- data/lib/rest/v1/order_book.rb +17 -0
- data/lib/{bitfinex → rest}/v1/orders.rb +6 -8
- data/lib/{bitfinex → rest}/v1/positions.rb +6 -2
- data/lib/{bitfinex → rest}/v1/stats.rb +1 -3
- data/lib/{bitfinex → rest}/v1/symbols.rb +1 -2
- data/lib/rest/v1/ticker.rb +13 -0
- data/lib/{bitfinex → rest}/v1/trades.rb +1 -16
- data/lib/{bitfinex → rest}/v1/wallet.rb +1 -2
- data/lib/rest/v2.rb +47 -0
- data/lib/{bitfinex → rest}/v2/margin.rb +1 -2
- data/lib/{bitfinex → rest}/v2/personal.rb +2 -18
- data/lib/{bitfinex → rest}/v2/stats.rb +1 -4
- data/lib/rest/v2/ticker.rb +20 -0
- data/lib/{bitfinex → rest}/v2/trading.rb +3 -49
- data/lib/{bitfinex → rest}/v2/utils.rb +1 -3
- data/lib/ws/ws2.rb +529 -0
- metadata +74 -56
- data/lib/bitfinex-api-rb.rb +0 -1
- data/lib/bitfinex-rb.rb +0 -1
- data/lib/bitfinex/api_versions.rb +0 -7
- data/lib/bitfinex/authenticated_rest.rb +0 -57
- data/lib/bitfinex/client.rb +0 -38
- data/lib/bitfinex/configurable.rb +0 -48
- data/lib/bitfinex/connection.rb +0 -52
- data/lib/bitfinex/v1/account_info.rb +0 -38
- data/lib/bitfinex/v1/orderbook.rb +0 -36
- data/lib/bitfinex/v1/ticker.rb +0 -27
- data/lib/bitfinex/v2/ticker.rb +0 -58
- data/lib/bitfinex/version.rb +0 -3
- data/lib/bitfinex/websocket_connection.rb +0 -231
@@ -1,38 +0,0 @@
|
|
1
|
-
module Bitfinex
|
2
|
-
module V1::AccountInfoClient
|
3
|
-
|
4
|
-
# Get account information
|
5
|
-
#
|
6
|
-
# @return [Hash] your account information
|
7
|
-
# @example:
|
8
|
-
# client.account_info
|
9
|
-
def account_info
|
10
|
-
resp = authenticated_post("account_infos")
|
11
|
-
resp.body
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
# Call block passing all account related specific messages sent via websocket
|
16
|
-
#
|
17
|
-
# @param block [Block] The code to be executed when new message is received
|
18
|
-
# @example:
|
19
|
-
# client.listen_account do |message|
|
20
|
-
# puts message.inspect
|
21
|
-
# end
|
22
|
-
def listen_account(&block)
|
23
|
-
raise BlockMissingError unless block_given?
|
24
|
-
ws_auth(&block)
|
25
|
-
end
|
26
|
-
|
27
|
-
# See the fees applied to your withdrawals
|
28
|
-
#
|
29
|
-
# @return [Hash]
|
30
|
-
# @example:
|
31
|
-
# client.fees
|
32
|
-
def fees
|
33
|
-
resp = authenticated_post("fees")
|
34
|
-
resp.body
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
module Bitfinex
|
3
|
-
module V1::OrderbookClient
|
4
|
-
|
5
|
-
# Get the full order book
|
6
|
-
#
|
7
|
-
# @param symbol [string]
|
8
|
-
# @param params :limit_bids [int] (optional) Limit the number of bids returned. May be 0 in which case the array of bids is empty. Default 50.
|
9
|
-
# @param params :limit_asks [int] (optional) Limit the number of asks returned. May be 0 in which case the array of asks is empty. Default 50.
|
10
|
-
# @param params :group [0/1] (optional) If 1, orders are grouped by price in the orderbook. If 0, orders are not grouped and sorted individually. Default 1
|
11
|
-
# @return [Hash] :bids [Array], :asks [Array]
|
12
|
-
# @example:
|
13
|
-
# client.orderbook("btcusd")
|
14
|
-
def orderbook(symbol="btcusd", params = {})
|
15
|
-
check_params(params, %i{limit_bids limit_asks group})
|
16
|
-
get("book/#{symbol}", params: params).body
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
# Get the order book changes using websocket
|
21
|
-
#
|
22
|
-
# @param pair [string]
|
23
|
-
# @param prec [string] Level of price aggregation (P0, P1, P2, P3). The default is P0.
|
24
|
-
# @param freq [string] Frequency of updates (F0, F1, F2, F3). F0=realtime / F1=2sec / F2=5sec / F3=10sec
|
25
|
-
# @param len [int] Number of price points (“25”, “100”) [default=“25”]
|
26
|
-
# @param block [Block] The code to be executed when a new order is submitted
|
27
|
-
# @example:
|
28
|
-
# client.listen_book do |order|
|
29
|
-
# puts order.inspect
|
30
|
-
# end
|
31
|
-
def listen_book(pair="BTCUSD", prec='P0', freq='F0',len=25, &block)
|
32
|
-
raise BlockMissingError unless block_given?
|
33
|
-
register_channel pair:pair, channel: 'book', prec: prec, freq: freq, len: len, &block
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
data/lib/bitfinex/v1/ticker.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Bitfinex
|
2
|
-
module V1::TickerClient
|
3
|
-
|
4
|
-
# Gives innermost bid and asks and information on the most recent trade, as well as high, low and volume of the last 24 hours.
|
5
|
-
#
|
6
|
-
# @param symbol [string] The name of hthe symbol
|
7
|
-
# @return [Hash]
|
8
|
-
# @example:
|
9
|
-
# client.ticker
|
10
|
-
def ticker(symbol = "btcusd")
|
11
|
-
get("pubticker/#{symbol}").body
|
12
|
-
end
|
13
|
-
|
14
|
-
# Call the specified block passing tickers, it uses websocket
|
15
|
-
#
|
16
|
-
# @param pair [string]
|
17
|
-
# @param block [Block] The code to be executed when a new ticker is sent by the server
|
18
|
-
# @example:
|
19
|
-
# client.listen_ticker do |tick|
|
20
|
-
# puts tick.inspect
|
21
|
-
# end
|
22
|
-
def listen_ticker(pair="BTCUSD", &block)
|
23
|
-
raise BlockMissingError unless block_given?
|
24
|
-
register_channel pair: pair, channel: "ticker", &block
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/bitfinex/v2/ticker.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Bitfinex
|
4
|
-
module V2::TickerClient
|
5
|
-
|
6
|
-
# Gives innermost bid and asks and information on
|
7
|
-
# the most recent trade, as well as high, low and
|
8
|
-
# volume of the last 24 hours.
|
9
|
-
#
|
10
|
-
# @param symbols a list of symbols
|
11
|
-
# @return [Hash]
|
12
|
-
# @example:
|
13
|
-
# client.ticker("tBTCUSD","tLTCUSD","fUSD")
|
14
|
-
def ticker(*symbols)
|
15
|
-
if symbols.size == 1
|
16
|
-
get("ticker/#{symbols.first}").body
|
17
|
-
else
|
18
|
-
get("tickers", symbols: "#{symbols.flatten.join(",")}").body
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# Call the specified block passing tickers, it uses websocket
|
23
|
-
#
|
24
|
-
# @param pair [string]
|
25
|
-
# @param block [Block] The code to be executed when a new ticker is sent by the server
|
26
|
-
#
|
27
|
-
# Documentation:
|
28
|
-
# https://docs.bitfinex.com/v2/reference#ws-public-ticker
|
29
|
-
#
|
30
|
-
# @example:
|
31
|
-
# client.listen_ticker do |tick|
|
32
|
-
# puts tick.inspect
|
33
|
-
# end
|
34
|
-
def listen_ticker(pair="tBTCUSD", &block)
|
35
|
-
raise BlockMissingError unless block_given?
|
36
|
-
register_channel pair: pair, channel: "ticker", &block
|
37
|
-
end
|
38
|
-
|
39
|
-
# Provides a way to access charting candle info
|
40
|
-
#
|
41
|
-
# @param symbol [string]
|
42
|
-
# @param time_frame [string] default '1m' - see doc for list of options
|
43
|
-
#
|
44
|
-
# Documentation:
|
45
|
-
# https://docs.bitfinex.com/v2/reference#ws-public-candle
|
46
|
-
#
|
47
|
-
# @example:
|
48
|
-
# client.listen_candles("tBTCUSD","1m") do |candle|
|
49
|
-
# puts "high #{candle[1][8]} | low #{candle[1][9]}"
|
50
|
-
# end
|
51
|
-
def listen_candles(symbol="tBTCUSD", time_frame="1m", &block)
|
52
|
-
raise BlockMissingError unless block_given?
|
53
|
-
key = "trade:#{time_frame}:#{symbol}"
|
54
|
-
register_channel key: key, channel: 'candles', &block
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
data/lib/bitfinex/version.rb
DELETED
@@ -1,231 +0,0 @@
|
|
1
|
-
require 'eventmachine'
|
2
|
-
require 'faye/websocket'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module Bitfinex
|
6
|
-
module WebsocketConnection
|
7
|
-
|
8
|
-
def listen!
|
9
|
-
subscribe_to_channels
|
10
|
-
listen
|
11
|
-
ws_client.run!
|
12
|
-
end
|
13
|
-
|
14
|
-
def ws_send(msg)
|
15
|
-
ws_client.send msg
|
16
|
-
end
|
17
|
-
|
18
|
-
def ws_close_all
|
19
|
-
ws_client.stop!
|
20
|
-
@ws_open = false
|
21
|
-
ws_reset_channels
|
22
|
-
end
|
23
|
-
|
24
|
-
def ws_auth(&block)
|
25
|
-
unless @ws_auth
|
26
|
-
nonce = (Time.now.to_f * 10_000).to_i.to_s
|
27
|
-
sub_id = add_callback(&block)
|
28
|
-
save_channel_id(sub_id,0)
|
29
|
-
if config.api_version == 1
|
30
|
-
payload = 'AUTH' + nonce
|
31
|
-
signature = sign(payload)
|
32
|
-
ws_safe_send({
|
33
|
-
apiKey: config.api_key,
|
34
|
-
authSig: sign(payload),
|
35
|
-
authPayload: payload,
|
36
|
-
subId: sub_id.to_s,
|
37
|
-
event: 'auth'
|
38
|
-
})
|
39
|
-
else
|
40
|
-
payload = 'AUTH' + nonce + nonce
|
41
|
-
signature = sign(payload)
|
42
|
-
ws_safe_send({
|
43
|
-
apiKey: config.api_key,
|
44
|
-
authSig: sign(payload),
|
45
|
-
authPayload: payload,
|
46
|
-
authNonce: nonce,
|
47
|
-
subId: sub_id.to_s,
|
48
|
-
event: 'auth'
|
49
|
-
})
|
50
|
-
end
|
51
|
-
@ws_auth = true
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def ws_unauth
|
56
|
-
ws_safe_send({event: 'unauth'})
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def ws_reset_channels
|
62
|
-
@chan_ids = []
|
63
|
-
@ws_registration_messages = []
|
64
|
-
@callbacks = {}
|
65
|
-
end
|
66
|
-
|
67
|
-
def ws_client
|
68
|
-
options = {
|
69
|
-
url: config.websocket_api_endpoint,
|
70
|
-
reconnect: config.reconnect,
|
71
|
-
reconnect_after: config.reconnect_after
|
72
|
-
}
|
73
|
-
@ws_client ||= WSClient.new(options)
|
74
|
-
end
|
75
|
-
|
76
|
-
def chan_ids
|
77
|
-
@chan_ids ||= []
|
78
|
-
end
|
79
|
-
|
80
|
-
def ws_open
|
81
|
-
@ws_open ||= false
|
82
|
-
end
|
83
|
-
|
84
|
-
def ws_registration_messages
|
85
|
-
@ws_registration_messages ||= []
|
86
|
-
end
|
87
|
-
|
88
|
-
def callbacks
|
89
|
-
@callbacks ||= []
|
90
|
-
end
|
91
|
-
|
92
|
-
def add_callback(&block)
|
93
|
-
id = 0
|
94
|
-
@mutex.synchronize do
|
95
|
-
callbacks[@c_counter] = { block: block, chan_id: nil }
|
96
|
-
id = @c_counter
|
97
|
-
@c_counter += 1
|
98
|
-
end
|
99
|
-
id
|
100
|
-
end
|
101
|
-
|
102
|
-
def register_authenticated_channel(msg, &block)
|
103
|
-
sub_id = add_callback(&block)
|
104
|
-
msg.merge!(subId: sub_id.to_s)
|
105
|
-
ws_safe_send(msg.merge(event:'subscribe'))
|
106
|
-
end
|
107
|
-
|
108
|
-
def ws_safe_send(msg)
|
109
|
-
if ws_open
|
110
|
-
ws_client.send msg
|
111
|
-
else
|
112
|
-
ws_registration_messages.push msg
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
def register_channel(msg, &block)
|
117
|
-
sub_id = add_callback(&block)
|
118
|
-
msg.merge!(subId: sub_id.to_s)
|
119
|
-
if ws_open
|
120
|
-
ws_client.send msg.merge(event: 'subscribe')
|
121
|
-
else
|
122
|
-
ws_registration_messages.push msg.merge(event: 'subscribe')
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def listen
|
127
|
-
ws_client.on(:message) do |rmsg|
|
128
|
-
msg = JSON.parse(rmsg)
|
129
|
-
if msg.kind_of?(Hash) && msg["event"] == "subscribed"
|
130
|
-
save_channel_id(msg["subId"],msg["chanId"])
|
131
|
-
elsif msg.kind_of?(Array)
|
132
|
-
exec_callback_for(msg)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def save_channel_id(sub_id,chan_id)
|
138
|
-
callbacks[sub_id.to_i][:chan_id] = chan_id
|
139
|
-
chan_ids[chan_id] = sub_id.to_i
|
140
|
-
end
|
141
|
-
|
142
|
-
def exec_callback_for(msg)
|
143
|
-
return if msg[1] == 'hb' #ignore heartbeat
|
144
|
-
id = msg[0]
|
145
|
-
callbacks[chan_ids[id.to_i]][:block].call(msg)
|
146
|
-
end
|
147
|
-
|
148
|
-
def subscribe_to_channels
|
149
|
-
ws_client.on(:open) do
|
150
|
-
@ws_open = true
|
151
|
-
ws_registration_messages.each do |msg|
|
152
|
-
ws_client.send(msg)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
class WSClient
|
158
|
-
def initialize(options = {})
|
159
|
-
# set some defaults
|
160
|
-
@url = options[:url] || 'wss://api.bitfinex.com/ws'
|
161
|
-
@reconnect = options[:reconnect] || false
|
162
|
-
@reconnect_after = options[:reconnect_after] || 30
|
163
|
-
@stop = false
|
164
|
-
end
|
165
|
-
|
166
|
-
def on(msg, &blk)
|
167
|
-
ivar = "@#{msg}_cb"
|
168
|
-
instance_variable_set(ivar.to_sym, blk)
|
169
|
-
end
|
170
|
-
|
171
|
-
def run!
|
172
|
-
if EventMachine.reactor_running?
|
173
|
-
connect!
|
174
|
-
else
|
175
|
-
EM.run { connect! }
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
def stop!
|
180
|
-
@stop = true
|
181
|
-
@ws.close
|
182
|
-
end
|
183
|
-
|
184
|
-
def connect!
|
185
|
-
@stop = false
|
186
|
-
@ws = Faye::WebSocket::Client.new(@url)
|
187
|
-
@ws.onopen = method(:ws_opened)
|
188
|
-
@ws.onmessage = method(:ws_receive)
|
189
|
-
@ws.onclose = method(:ws_closed)
|
190
|
-
@ws.onerror = method(:ws_error)
|
191
|
-
end
|
192
|
-
|
193
|
-
def send(msg)
|
194
|
-
raise ConnectionClosed if stopped?
|
195
|
-
connect! unless alive?
|
196
|
-
msg = msg.is_a?(Hash) ? msg.to_json : msg
|
197
|
-
@ws.send(msg)
|
198
|
-
end
|
199
|
-
|
200
|
-
def alive?
|
201
|
-
@ws && @ws.ready_state == Faye::WebSocket::API::OPEN
|
202
|
-
end
|
203
|
-
|
204
|
-
def stopped?
|
205
|
-
@stop
|
206
|
-
end
|
207
|
-
|
208
|
-
private
|
209
|
-
|
210
|
-
def ws_opened(event)
|
211
|
-
@open_cb.call(event) if @open_cb
|
212
|
-
end
|
213
|
-
|
214
|
-
def ws_receive(event)
|
215
|
-
@message_cb.call(event.data) if @message_cb
|
216
|
-
end
|
217
|
-
|
218
|
-
def ws_closed(event)
|
219
|
-
if @stop
|
220
|
-
EM.stop
|
221
|
-
elsif @reconnect
|
222
|
-
EM.add_timer(@reconnect_after){ connect! }
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
def ws_error(event)
|
227
|
-
raise WebsocketError, event.message
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|