bitfinex-rb 0.0.3

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
+ SHA1:
3
+ metadata.gz: 30f018e3da530a1424941a50719b8aa64b719a7a
4
+ data.tar.gz: 0cebfb76c4489d260f9177345dc6bada2fd972e7
5
+ SHA512:
6
+ metadata.gz: 9bb9f05e543f01f8b69e1269af331d020e6a5408545c72926b0dc1550a350ddd06736315f8ca1aa7f267e48dfa80ab1d29a4d53fbe51dab3d46a307905586c33
7
+ data.tar.gz: db36a7a68b56eb1c8af63f85d1165a03013daa729f75ac99bf7393f5eb478440c32f056009d79a13dabdab3188257a4414232ac394e30b7e00ca2d385b0b6df7
data/lib/bitfinex.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'uri'
2
+ require 'base64'
3
+ require 'openssl'
4
+ require 'faraday'
5
+ require 'json'
6
+ require 'faraday_middleware'
7
+ require 'bitfinex/configurable'
8
+ require 'bitfinex/errors'
9
+ require 'bitfinex/connection'
10
+ require 'bitfinex/authenticated_rest'
11
+ require 'bitfinex/ticker'
12
+ require 'bitfinex/trades'
13
+ require 'bitfinex/funding_book'
14
+ require 'bitfinex/orderbook'
15
+ require 'bitfinex/lends'
16
+ require 'bitfinex/symbols'
17
+ require 'bitfinex/stats'
18
+ require 'bitfinex/account_info'
19
+ require 'bitfinex/deposit'
20
+ require 'bitfinex/orders'
21
+ require 'bitfinex/wallet'
22
+ require 'bitfinex/positions'
23
+ require 'bitfinex/historical_data'
24
+ require 'bitfinex/margin_funding'
25
+ require 'bitfinex/client'
26
+
@@ -0,0 +1,16 @@
1
+ module Bitfinex
2
+
3
+ module AccountInfoClient
4
+
5
+ # Get account information
6
+ #
7
+ # @return [Hash] your account information
8
+ # @example:
9
+ # client.account_info
10
+ def account_info
11
+ resp = authenticated_post("account_infos")
12
+ resp.body
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,35 @@
1
+ module Bitfinex
2
+ module AuthenticatedConnection
3
+
4
+ private
5
+ def authenticated_post(url, options = {})
6
+ raise Bitfinex::InvalidAuthKeyError unless valid_key?
7
+ complete_url = build_url(url)
8
+ payload = build_payload("/v1/#{url}", options[:params])
9
+ response = rest_connection.post do |req|
10
+ req.url complete_url
11
+ req.headers['Content-Type'] = 'application/json'
12
+ req.headers['Accept'] = 'application/json'
13
+ req.headers['X-BFX-PAYLOAD'] = payload
14
+ req.headers['X-BFX-SIGNATURE'] = sign(payload)
15
+ req.headers['X-BFX-APIKEY'] = config.api_key
16
+ end
17
+ end
18
+
19
+ def build_payload(url, params = {})
20
+ payload = {}
21
+ payload['nonce'] = (Time.now.to_f * 10_000).to_i.to_s
22
+ payload['request'] = url
23
+ payload.merge!(params) if params
24
+ Base64.strict_encode64(payload.to_json)
25
+ end
26
+
27
+ def sign(payload)
28
+ OpenSSL::HMAC.hexdigest('sha384', config.secret, payload)
29
+ end
30
+
31
+ def valid_key?
32
+ !! (config.api_key && config.secret)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ module Bitfinex
2
+ class Client
3
+ include Bitfinex::RestConnection
4
+ include Bitfinex::AuthenticatedConnection
5
+ include Bitfinex::TickerClient
6
+ include Bitfinex::TradesClient
7
+ include Bitfinex::FundingBookClient
8
+ include Bitfinex::OrderbookClient
9
+ include Bitfinex::StatsClient
10
+ include Bitfinex::LendsClient
11
+ include Bitfinex::SymbolsClient
12
+ include Bitfinex::AccountInfoClient
13
+ include Bitfinex::DepositClient
14
+ include Bitfinex::OrdersClient
15
+ include Bitfinex::PositionsClient
16
+ include Bitfinex::HistoricalDataClient
17
+ include Bitfinex::MarginFundingClient
18
+ include Bitfinex::WalletClient
19
+ include Bitfinex::Configurable
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ module Bitfinex
2
+ module Configurable
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ def config
8
+ self.class.config
9
+ end
10
+
11
+ module ClassMethods
12
+ def configure
13
+ yield config
14
+ end
15
+
16
+ def config
17
+ @configuration ||= Configuration.new
18
+ end
19
+ end
20
+ end
21
+
22
+ class Configuration
23
+ attr_accessor :api_endpoint, :debug, :debug_connection, :secret, :api_key
24
+ def initialize
25
+ self.api_endpoint = "https://api.bitfinex.com/v1/"
26
+ self.debug = false
27
+ self.debug_connection = false
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,48 @@
1
+ require 'logger'
2
+ module Bitfinex
3
+ # Network Layer for API Rest client
4
+ module RestConnection
5
+ private
6
+ # Make an HTTP GET request
7
+ def get(url, options={})
8
+ rest_connection.get do |req|
9
+ req.url build_url(url)
10
+ req.headers['Content-Type'] = 'application/json'
11
+ req.headers['Accept'] = 'application/json'
12
+ req.params = options[:params] if options.has_key?(:params) && !options[:params].empty?
13
+ end
14
+ end
15
+
16
+ # Make sure parameters are allowed for the HTTP call
17
+ def check_params(params, allowed_params)
18
+ if (params.keys - allowed_params).empty?
19
+ return params
20
+ else
21
+ raise Bitfinex::ParamsError
22
+ end
23
+ end
24
+
25
+ def rest_connection
26
+ @conn ||= new_rest_connection
27
+ end
28
+
29
+ def build_url(url)
30
+ URI.join(config.api_endpoint, url).path
31
+ end
32
+
33
+ def new_rest_connection
34
+ Faraday.new(url: base_api_endpoint) do |conn|
35
+ conn.use Faraday::Response::RaiseError
36
+ conn.response :logger, Logger.new(STDOUT) , bodies: true if config.debug_connection
37
+ conn.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
38
+ conn.adapter :net_http
39
+ end
40
+ end
41
+
42
+ def base_api_endpoint
43
+ url = URI.parse config.api_endpoint
44
+ "#{url.scheme}://#{url.host}"
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ module Bitfinex
2
+ module DepositClient
3
+ # Return your deposit address to make a new deposit.
4
+ #
5
+ # @param method [string] Method of deposit (methods accepted: “bitcoin”, “litecoin”, “darkcoin”, “mastercoin” (tethers)).
6
+ # @param wallet_name [string] Wallet to deposit in (accepted: “trading”, “exchange”, “deposit”). Your wallet needs to already exist
7
+ # @params renew [integer] (optional) Default is 0. If set to 1, will return a new unused deposit address
8
+ #
9
+ # @return [Hash] confirmation of your deposit
10
+ # @example:
11
+ # client.deposit("bitcoin", "exchange")
12
+ def deposit method, wallet_name, renew=0
13
+ params = {
14
+ method: method,
15
+ wallet_name: wallet_name,
16
+ renew: renew
17
+ }
18
+
19
+ authenticated_post("deposit/new", params).body
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module Bitfinex
2
+ class ClientError < Exception; end
3
+ class ParamsError < ClientError; end
4
+ class InvalidAuthKeyError < ClientError; end
5
+ end
@@ -0,0 +1,18 @@
1
+ module Bitfinex
2
+ module FundingBookClient
3
+
4
+ # Get the full margin funding book
5
+
6
+ # @param currency [string] (optional) Speficy the currency, default "USD"
7
+ # @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
+ # @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.
9
+ # @return [Hash] of :bids and :asks arrays
10
+ # @example:
11
+ # client.funding_book
12
+ def funding_book(currency="usd", params = {})
13
+ check_params(params, %i{limit_bids limit_asks})
14
+ get("lendbook/#{currency}", params).body
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,53 @@
1
+ module Bitfinex
2
+ module HistoricalDataClient
3
+
4
+ # View all of your balance ledger entries.
5
+ #
6
+ # @param currency [string] (optional) Specify the currency, default "USD"
7
+ # @param params :since [time] (optional) Return only the history after this timestamp.
8
+ # @param params :until [time] (optional) Return only the history before this timestamp.
9
+ # @param params :limit [int] (optional) Limit the number of entries to return. Default is 500.
10
+ # @param params :wallet [string] (optional) Return only entries that took place in this wallet. Accepted inputs are: “trading”, “exchange”, “deposit”
11
+ # @return [Array]
12
+ # @example:
13
+ # client.history
14
+ def history(currency="usd", params = {})
15
+ check_params(params, %i{since until limit wallet})
16
+ params.merge!({currency: currency})
17
+ authenticated_post("history", params).body
18
+ end
19
+
20
+ # View your past deposits/withdrawals.
21
+ #
22
+ # @param currency [string] (optional) Specify the currency, default "USD"
23
+ # @param params :method (optional) The method of the deposit/withdrawal (can be “bitcoin”, “litecoin”, “darkcoin”, “wire”)
24
+ # @param params :since (optional) Return only the history after this timestamp
25
+ # @param params :until [time] (optional) Return only the history before this timestamp.
26
+ # @param params :limit [int] (optional) Limit the number of entries to return. Default is 500.
27
+ # @return [Array]
28
+ # @example:
29
+ # client.movements
30
+ def movements(currency="usd", params = {})
31
+ check_params(params, %i{method since until limit})
32
+ params.merge!({currency: currency})
33
+ authenticated_post("history/movements", params).body
34
+ end
35
+
36
+ # View your past trades.
37
+ #
38
+ # @param symbol The pair traded (BTCUSD, LTCUSD, LTCBTC)
39
+ # @param params :until [time] (optional) Return only the history before this timestamp.
40
+ # @param params :timestamp [time] (optional) Trades made before this timestamp won’t be returned
41
+ # @param params :until [time] (optional) Trades made after this timestamp won’t be returned
42
+ # @param params :limit_trades [int] Limit the number of trades returned. Default is 50.
43
+ # @param params :reverse [int] Return trades in reverse order (the oldest comes first). Default is returning newest trades first.
44
+ # @return [Array]
45
+ # @example:
46
+ # client.mytrades
47
+ def mytrades(symbol, params = {})
48
+ check_params(params, %i{until limit_trades reverse timestamp})
49
+ params.merge!({symbol: symbol})
50
+ authenticated_post("mytrades", params).body
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ module Bitfinex
2
+ module LendsClient
3
+
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
+ #
6
+ # @param currency [string] (optional) Specify the currency, default "USD"
7
+ # @param params :timestamp [time] (optional) Only show data at or after this timestamp
8
+ # @param params :limit_lends [int] (optional) Limit the amount of funding data returned. Must be > 1, default 50
9
+ # @return [Array]
10
+ # @example:
11
+ # client.lends
12
+ def lends(currency = "usd", params = {})
13
+ check_params(params, %i{timestamp limit_lends})
14
+ get("lends/#{currency}", params).body
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,101 @@
1
+ module Bitfinex
2
+ module MarginFundingClient
3
+
4
+ # Submit a new offer
5
+ #
6
+ # @param currency [string] The name of the currency, es: 'USD'
7
+ # @param amount [decimal] Offer size: how much to lend or borrow
8
+ # @param rate [decimal] Rate to lend or borrow at. In percentage per 365 days. Set to 0 for FRR).
9
+ # @param period [integer] Number of days of the funding contract (in days)
10
+ # @param direction [string] Either “lend” or “loan”
11
+ # @return [Hash]
12
+ # @example:
13
+ # client.new_offer("btc", 10.0, 20, 365, "lend")
14
+ def new_offer(currency, amount, rate, period, direction)
15
+ params = {
16
+ currency: currency,
17
+ amount: amount,
18
+ rate: rate,
19
+ period: period,
20
+ direction: direction
21
+ }
22
+
23
+ authenticated_post("offer/new", params).body
24
+ end
25
+
26
+ # Cancel an offer
27
+ #
28
+ # @param offer_id [int] The offer ID given by `#new_offer`
29
+ # @return [Hash]
30
+ # @example:
31
+ # client.cancel_offer(1000)
32
+ def cancel_offer(offer_id)
33
+ authenticated_post("offer/cancel", {offer_id: offer_id}).body
34
+ end
35
+
36
+ # Get the status of an offer. Is it active? Was it cancelled? To what extent has it been executed? etc.
37
+ #
38
+ # @param offer_id [int] The offer ID give by `#new_offer`
39
+ # @return [Hash]
40
+ # @example:
41
+ # client.offer_status(1000)
42
+ def offer_status(offer_id)
43
+ authenticated_post("offer/status", {offer_id: offer_id}).body
44
+ end
45
+
46
+ # View your funds currently taken (active credits)
47
+ #
48
+ # @return [Array]
49
+ # @example:
50
+ # client.credits
51
+ def credits
52
+ authenticated_post("credits").body
53
+ end
54
+
55
+ # View your active offers
56
+ #
57
+ # @return [Array] An array of the results of /offer/status for all your live offers (lending or borrowing
58
+ # @example:
59
+ # client.offers
60
+ def offers
61
+ authenticated_post("offers").body
62
+ end
63
+
64
+ # View your funding currently borrowed and used in a margin position
65
+ #
66
+ # @return [Array] An array of your active margin funds
67
+ # @example:
68
+ # client.taken_funds
69
+ def taken_funds
70
+ authenticated_post("taken_funds").body
71
+ end
72
+
73
+ # View your funding currently borrowed and not used (available for a new margin position).
74
+ #
75
+ # @return [Array] An array of your active unused margin funds
76
+ # @example:
77
+ # client.unused_taken_funds
78
+ def unused_taken_funds
79
+ authenticated_post("unused_taken_funds").body
80
+ end
81
+
82
+ # View the total of your active funding used in your position(s).
83
+ #
84
+ # @return [Array] An array of your active funding
85
+ # @example:
86
+ # client.total_taken_funds
87
+ def total_taken_funds
88
+ authenticated_post("total_taken_funds").body
89
+ end
90
+
91
+ # Allow you to close an unused or used taken fund
92
+ #
93
+ # @param swap_id [int] The ID given by `#taken_funds` or `#unused_taken_funds
94
+ # @return [Hash]
95
+ # @example:
96
+ # client.close_funding(1000)
97
+ def close_funding(swap_id)
98
+ authenticated_post("funding/close", {swap_id: swap_id}).body
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,18 @@
1
+ module Bitfinex
2
+ module OrderbookClient
3
+
4
+ # Get the full order book
5
+ #
6
+ # @param symbol [string]
7
+ # @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.
8
+ # @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.
9
+ # @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
10
+ # @return [Hash] :bids [Array], :asks [Array]
11
+ # @example:
12
+ # client.orderbook("btcusd")
13
+ def orderbook(symbol="btcusd", params = {})
14
+ check_params(params, %i{limit_bids limit_asks group})
15
+ get("book/#{symbol}",params).body
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,117 @@
1
+ module Bitfinex
2
+
3
+ module OrdersClient
4
+
5
+ # Submit a new order
6
+ # @param symbol [string] The name of the symbol (see `#symbols`)
7
+ # @param amount [decimal] Order size: how much to buy or sell
8
+ # @param type [string] Either “market” / “limit” / “stop” / “trailing-stop” / “fill-or-kill” / “exchange market” / “exchange limit” / “exchange stop” / “exchange trailing-stop” / “exchange fill-or-kill”. (type starting by “exchange ” are exchange orders, others are margin trading orders)
9
+ # @param side [string] Either “buy” or “sell”
10
+ # @param price [decimal] Price to buy or sell at. Must be positive. Use random number for market orders.
11
+ # @param params :is_hidden [bool] (optional) true if the order should be hidden. Default is false
12
+ # @param params :is_postonly [bool] (optional) true if the order should be post only. Default is false. Only relevant for limit orders
13
+ # @param params :ocoorder [bool] Set an additional STOP OCO order that will be linked with the current order
14
+ # @param params :buy_price_oco [decimal] If ocoorder is true, this field represent the price of the OCO stop order to place
15
+ # @return [Hash]
16
+ # @example:
17
+ # client.new_order("usdbtc", 100, "market", "sell", 0)
18
+ def new_order(symbol, amount, type, side, price = nil, params = {})
19
+ check_params(params, %i{is_hidden is_postonly ocoorder buy_price_oco})
20
+
21
+ params.merge!({
22
+ symbol: symbol,
23
+ amount: amount,
24
+ type: type,
25
+ side: side,
26
+ exchange: 'bitfinex',
27
+ price: price})
28
+
29
+ authenticated_post("order/new", params: params).body
30
+ end
31
+
32
+ # Submit several new orders at once
33
+ #
34
+ # @param orders [Array] Array of Hash with the following elements
35
+ # @param orders :symbol [string] The name of the symbol
36
+ # @param orders :amount [decimal] Order size: how much to buy or sell
37
+ # @param orders :price [decimal] Price to buy or sell at. May omit if a market order
38
+ # @param orders :exchange [string] "bitfinex"
39
+ # @param orders :side [string] Either “buy” or “sell”
40
+ # @param orders :type [string] Either “market” / “limit” / “stop” / “trailing-stop” / “fill-or-kill”
41
+ # @return [Hash] with a `object_id` that is an `Array`
42
+ # @example:
43
+ # client.multiple_orders([{symbol: "usdbtc", amount: 10, price: 0, exchange: "bitfinex", side: "buy", type: "market"}])
44
+ def multiple_orders(orders)
45
+ authenticated_post("order/new/multi", params: orders).body
46
+ end
47
+
48
+ # Cancel an order
49
+ #
50
+ # @param ids [Array] or [integer] or nil
51
+ # if it's Array it's supposed to specify a list of IDS
52
+ # if it's an integer it's supposed to be a single ID
53
+ # if it's not specified it deletes all the orders placed
54
+ # @return [Hash]
55
+ # @example
56
+ # client.cancel_orders([100,231,400])
57
+ def cancel_orders(ids=nil)
58
+ case ids
59
+ when Array
60
+ authenticated_post("order/cancel/multi", {order_ids: ids}).body
61
+ when Numeric
62
+ authenticated_post("order/cancel", {order_id: ids}).body
63
+ when NilClass
64
+ authenticated_post("order/cancel/all").body
65
+ else
66
+ raise ParamsError
67
+ end
68
+ end
69
+
70
+ # Replace an orders with a new one
71
+ #
72
+ # @param id [int] the ID of the order to replace
73
+ # @param symbol [string] the name of the symbol
74
+ # @param amount [decimal] Order size: how much to buy or sell
75
+ # @param type [string] Either “market” / “limit” / “stop” / “trailing-stop” / “fill-or-kill” / “exchange market” / “exchange limit” / “exchange stop” / “exchange trailing-stop” / “exchange fill-or-kill”. (type starting by “exchange ” are exchange orders, others are margin trading orders)
76
+ # @param side [string] Either “buy” or “sell”
77
+ # @param price [decimal] Price to buy or sell at. May omit if a market order
78
+ # @param is_hidden [bool] (optional) true if the order should be hidden. Default is false
79
+ # @return [Hash] the order
80
+ # @example:
81
+ # client.replace_order(100,"usdbtc", 10, "market", "buy", 0)
82
+ def replace_order(id, symbol, amount, type, side, price, is_hidden=false)
83
+ params = {
84
+ order_id: id,
85
+ symbol: symbol,
86
+ amount: amount,
87
+ type: type,
88
+ side: side,
89
+ exchange: 'bitfinex',
90
+ is_hidden: is_hidden,
91
+ price: price
92
+ }
93
+ authenticated_post("order/cancel/replace", params).body
94
+ end
95
+
96
+ # Get the status of an order. Is it active? Was it cancelled? To what extent has it been executed? etc.
97
+ #
98
+ # @param id
99
+ # @return [Hash]
100
+ # @exmaple:
101
+ # client.order_status(100)
102
+ def order_status(id)
103
+ authenticated_post("order/status", order_id: id).body
104
+ end
105
+
106
+
107
+ # View your active orders.
108
+ #
109
+ # @return [Hash]
110
+ # @example:
111
+ # client.orders
112
+ def orders
113
+ authenticated_post("orders").body
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,27 @@
1
+ module Bitfinex
2
+ module PositionsClient
3
+
4
+ # View your active positions.
5
+ #
6
+ # @return [Array]
7
+ # @example:
8
+ # client.positions
9
+ def positions
10
+ authenticated_post("positions").body
11
+ end
12
+
13
+ # A position can be claimed if:
14
+ #
15
+ # It is a long position: The amount in the last unit of the position pair that you have in your trading wallet AND/OR the realized profit of the position is greater or equal to the purchase amount of the position (base price * position amount) and the funds which need to be returned. For example, for a long BTCUSD position, you can claim the position if the amount of USD you have in the trading wallet is greater than the base price * the position amount and the funds used.
16
+ #
17
+ # It is a short position: The amount in the first unit of the position pair that you have in your trading wallet is greater or equal to the amount of the position and the margin funding used.
18
+ # @param position_id [int] The position ID given by `/positions`
19
+ # @param amount [decimal] The partial amount you wish to claim
20
+ # @return [Hash]
21
+ # @example:
22
+ # client.claim_position(100,10)
23
+ def claim_position(position_id, amount)
24
+ authenticated_post("position/claim", {position_id: position_id, amount: amount}).body
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module Bitfinex
2
+ module StatsClient
3
+
4
+ # Various statistics about the requested pair.
5
+ #
6
+ # @param symbol [string] Symbol of the pair you want info about. Default 'btcusd'
7
+ # @return [Array]
8
+ # @example:
9
+ # client.stats('btcusd')
10
+ def stats(symbol = "btcusd")
11
+ get("stats/#{symbol}").body
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ module Bitfinex
2
+ module SymbolsClient
3
+
4
+ # Get a list of valid symbol IDs.
5
+ #
6
+ # @return [Array]
7
+ # @example:
8
+ # client.symbols
9
+ def symbols
10
+ get("symbols").body
11
+ end
12
+
13
+ # Get detailed list of symbols
14
+ #
15
+ # @return [Array]
16
+ # @example:
17
+ # client.symbols_details
18
+ def symbols_details
19
+ get("symbols_details").body
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module Bitfinex
2
+ module 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
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Bitfinex
2
+ module TradesClient
3
+
4
+ # Get a list of the most recent trades for the given symbol.
5
+ #
6
+ # @param symbol [string] the name of the symbol
7
+ # @param params :timestamp [time] Only show trades at or after this timestamp.
8
+ # @param params :limit_trades [int] Limit the number of trades returned. Must be >= 1.
9
+ # @return [Array]
10
+ # @example:
11
+ # client.trades
12
+ def trades(symbol="btcusd", params={})
13
+ check_params(params, %i{timestamp limit_trades})
14
+ get("trades/#{symbol}", params: params).body
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Bitfinex
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,89 @@
1
+ module Bitfinex
2
+ module WalletClient
3
+
4
+ # See your balances.
5
+ #
6
+ # @param params :type [string] “trading”, “deposit” or “exchange”.
7
+ # @param params :currency [string] currency
8
+ # @param params :amount [decimal] How much balance of this currency in this wallet
9
+ # @param params :available [decimal] How much X there is in this wallet that is available to trade
10
+ # @return [Array]
11
+ # @example:
12
+ # client.balances
13
+ def balances(params = {})
14
+ check_params(params, %i{type currency amount available})
15
+ authenticated_post("balances", params).body
16
+ end
17
+
18
+ # See your trading wallet information for margin trading.
19
+ #
20
+ # @return [Array]
21
+ # @example:
22
+ # client.margin_infos
23
+ def margin_infos
24
+ authenticated_post("margin_infos").body
25
+ end
26
+
27
+ # Allow you to move available balances between your wallets.
28
+ #
29
+ # @param amount [decimal] Amount to transfer
30
+ # @param currency [string] Currency of funds to transfer
31
+ # @param wallet_from [string] Wallet to transfer from
32
+ # @param wallet_to [string] Wallet to transfer to
33
+ # @return [Array]
34
+ # @example:
35
+ # client.transfer(10, 'btc', "exchange", "deposit")
36
+ def transfer(amount, currency, wallet_from, wallet_to)
37
+ params = {
38
+ amount: amount,
39
+ currency: currency,
40
+ wallet_from: wallet_from,
41
+ wallet_to: wallet_to
42
+ }
43
+ authenticated_post("transfer", params).body
44
+ end
45
+
46
+ # Allow you to request a withdrawal from one of your wallet.
47
+ #
48
+ # @param withdraw_type [string] can be “bitcoin”, “litecoin” or “darkcoin” or “tether” or “wire”
49
+ # @param walletselected [string] The wallet to withdraw from, can be “trading”, “exchange”, or “deposit”.
50
+ # @param amount [decimal] Amount to withdraw
51
+ # For Cryptocurrencies (including tether):
52
+ # @param params :address [string] Destination address for withdrawal
53
+ # For wire withdrawals
54
+ # @param params :account_name [string] account name
55
+ # @param params :account_number [string] account number
56
+ # @param params :bank_name [string] bank name
57
+ # @param params :bank_address [string] bank address
58
+ # @param params :bank_city [string] bank city
59
+ # @param params :bank_country [string] bank country
60
+ # @param params :detail_payment [string] (optional) message to beneficiary
61
+ # @param params :intermediary_bank_name [string] (optional) intermediary bank name
62
+ # @param params :intermediary_bank_address [string] (optional) intermediary bank address
63
+ # @param params :intermediary_bank_city [string] (optional) intermediary bank city
64
+ # @param params :intermediary_bank_country [string] (optional) intermediary bank country
65
+ # @param params :intermediary_bank_account [string] (optional) intermediary bank account
66
+ # @param params :intermediary_bank_swift [string] (optional) intemediary bank swift
67
+ # @param params :expressWire [int] (optional). “1” to submit an express wire withdrawal, “0” or omit for a normal withdrawal
68
+ # @return [Array]
69
+ # @example:
70
+ # client.withdraw("bitcoin","deposit",1000, address: "1DKwqRhDmVyHJDL4FUYpDmQMYA3Rsxtvur")
71
+ def withdraw(withdraw_type, walletselected, amount, params={})
72
+ params.merge!({
73
+ withdraw_type: withdraw_type,
74
+ walletselected: walletselected,
75
+ amount: amount})
76
+
77
+ authenticated_post("withdraw", params).body
78
+ end
79
+
80
+ # Check the permissions of the key being used to generate this request
81
+ #
82
+ # @return [Hash]
83
+ # @example:
84
+ # client.key_info
85
+ def key_info
86
+ authenticated_post("key_info").body
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,64 @@
1
+ require 'eventmachine'
2
+ require 'faye/websocket'
3
+ require 'json'
4
+
5
+ module Bitfinex
6
+ module Websocket
7
+
8
+ class EMClient
9
+ def initialize(options = {})
10
+ # set some defaults
11
+ @url = options[:url] || 'wss://api2.bitfinex.com:3000/ws'
12
+ @reconnect = options[:reconenct] || false
13
+ end
14
+
15
+ def on(msg, &blk)
16
+ ivar = "@#{msg}_cb"
17
+ instance_variable_set(ivar.to_sym, blk)
18
+ end
19
+
20
+ def run!
21
+ if EventMachine.reactor_running?
22
+ connect!
23
+ else
24
+ EM.run { connect! }
25
+ end
26
+ end
27
+
28
+ def stop!
29
+ @ws.close
30
+ end
31
+
32
+ def connect!
33
+ @ws = Faye::WebSocket::Client.new(@url)
34
+ @ws.onopen = method(:ws_opened)
35
+ @ws.onmessage = method(:ws_receive)
36
+ @ws.onclose = method(:ws_closed)
37
+ @ws.onerror = method(:ws_error)
38
+ end
39
+
40
+ def send(msg)
41
+ msg = msg.is_a?(Hash) ? msg.to_json : msg
42
+ @ws.send(msg)
43
+ end
44
+
45
+ private
46
+
47
+ def ws_opened(event)
48
+ @open_cb.call(event) if @open_cb
49
+ end
50
+
51
+ def ws_receive(event)
52
+ @message_cb.call(event.data) if @message_cb
53
+ end
54
+
55
+ def ws_closed(_event)
56
+ EM.stop
57
+ end
58
+
59
+ def ws_error(event)
60
+ fail event
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,60 @@
1
+ module Bitfinex
2
+ module Websocket
3
+
4
+ class EMClient
5
+ def initialize(options = {})
6
+ # set some defaults
7
+ @url = options[:url] || 'wss://api2.bitfinex.com:3000/ws'
8
+ @reconnect = options[:reconenct] || false
9
+ end
10
+
11
+ def on(msg, &blk)
12
+ ivar = "@#{msg}_cb"
13
+ instance_variable_set(ivar.to_sym, blk)
14
+ end
15
+
16
+ def run!
17
+ if EventMachine.reactor_running?
18
+ connect!
19
+ else
20
+ EM.run { connect! }
21
+ end
22
+ end
23
+
24
+ def stop!
25
+ @ws.close
26
+ end
27
+
28
+ def connect!
29
+ @ws = Faye::WebSocket::Client.new(@url)
30
+ @ws.onopen = method(:ws_opened)
31
+ @ws.onmessage = method(:ws_receive)
32
+ @ws.onclose = method(:ws_closed)
33
+ @ws.onerror = method(:ws_error)
34
+ end
35
+
36
+ def send(msg)
37
+ msg = msg.is_a?(Hash) ? msg.to_json : msg
38
+ @ws.send(msg)
39
+ end
40
+
41
+ private
42
+
43
+ def ws_opened(event)
44
+ @open_cb.call(event) if @open_cb
45
+ end
46
+
47
+ def ws_receive(event)
48
+ @message_cb.call(event.data) if @message_cb
49
+ end
50
+
51
+ def ws_closed(_event)
52
+ EM.stop
53
+ end
54
+
55
+ def ws_error(event)
56
+ fail event
57
+ end
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitfinex-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Bitfinex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday-detailed_logger
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.0.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 1.0.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: json
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 1.8.3
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.8.3
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.3
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.8.3
73
+ - !ruby/object:Gem::Dependency
74
+ name: faraday_middleware
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '0.10'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.10.0
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.10.0
93
+ - !ruby/object:Gem::Dependency
94
+ name: bundler
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 1.9.2
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 1.8.0
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 1.9.2
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 1.8.0
113
+ description: Simple Bitfinex API ruby wrapper
114
+ email:
115
+ - developers@bitfinex.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - lib/bitfinex.rb
121
+ - lib/bitfinex/account_info.rb
122
+ - lib/bitfinex/authenticated_rest.rb
123
+ - lib/bitfinex/client.rb
124
+ - lib/bitfinex/configurable.rb
125
+ - lib/bitfinex/connection.rb
126
+ - lib/bitfinex/deposit.rb
127
+ - lib/bitfinex/errors.rb
128
+ - lib/bitfinex/funding_book.rb
129
+ - lib/bitfinex/historical_data.rb
130
+ - lib/bitfinex/lends.rb
131
+ - lib/bitfinex/margin_funding.rb
132
+ - lib/bitfinex/orderbook.rb
133
+ - lib/bitfinex/orders.rb
134
+ - lib/bitfinex/positions.rb
135
+ - lib/bitfinex/stats.rb
136
+ - lib/bitfinex/symbols.rb
137
+ - lib/bitfinex/ticker.rb
138
+ - lib/bitfinex/trades.rb
139
+ - lib/bitfinex/version.rb
140
+ - lib/bitfinex/wallet.rb
141
+ - lib/bitfinex/websocket.rb
142
+ - lib/bitfinex/websocket/em_client.rb
143
+ homepage: https://www.bitfinex.com/
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.4.5
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Bitfinex API Wrapper
167
+ test_files: []
168
+ has_rdoc: