UNPKG

7.59 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.7.1
2(function() {
3 var Bitfinex, crypto, qs, request;
4
5 process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
6
7 request = require('request');
8
9 crypto = require('crypto');
10
11 qs = require('querystring');
12
13 module.exports = Bitfinex = (function() {
14 function Bitfinex(key, secret) {
15 this.url = "https://api.bitfinex.com";
16 this.key = key;
17 this.secret = secret;
18 this.nonce = Math.round((new Date()).getTime() / 1000);
19 }
20
21 Bitfinex.prototype._nonce = function() {
22 return this.nonce++;
23 };
24
25 Bitfinex.prototype.make_request = function(sub_path, params, cb) {
26 var headers, key, nonce, path, payload, signature, url, value;
27 if (!this.key || !this.secret) {
28 return cb(new Error("missing api key or secret"));
29 }
30 path = '/v1/' + sub_path;
31 url = this.url + path;
32 nonce = JSON.stringify(this._nonce());
33 payload = {
34 request: path,
35 nonce: nonce
36 };
37 for (key in params) {
38 value = params[key];
39 payload[key] = value;
40 }
41 payload = new Buffer(JSON.stringify(payload)).toString('base64');
42 signature = crypto.createHmac("sha384", this.secret).update(payload).digest('hex');
43 headers = {
44 'X-BFX-APIKEY': this.key,
45 'X-BFX-PAYLOAD': payload,
46 'X-BFX-SIGNATURE': signature
47 };
48 return request({
49 url: url,
50 method: "POST",
51 headers: headers,
52 timeout: 15000
53 }, function(err, response, body) {
54 var error, result;
55 if (err || (response.statusCode !== 200 && response.statusCode !== 400)) {
56 return cb(new Error(err != null ? err : response.statusCode));
57 }
58 try {
59 result = JSON.parse(body);
60 } catch (_error) {
61 error = _error;
62 return cb(null, {
63 messsage: body.toString()
64 });
65 }
66 if (result.message != null) {
67 return cb(new Error(result.message));
68 }
69 return cb(null, result);
70 });
71 };
72
73 Bitfinex.prototype.make_public_request = function(path, cb) {
74 var url;
75 url = this.url + '/v1/' + path;
76 return request({
77 url: url,
78 method: "GET",
79 timeout: 15000
80 }, function(err, response, body) {
81 var error, result;
82 if (err || (response.statusCode !== 200 && response.statusCode !== 400)) {
83 return cb(new Error(err != null ? err : response.statusCode));
84 }
85 try {
86 result = JSON.parse(body);
87 } catch (_error) {
88 error = _error;
89 return cb(null, {
90 messsage: body.toString()
91 });
92 }
93 if (result.message != null) {
94 return cb(new Error(result.message));
95 }
96 return cb(null, result);
97 });
98 };
99
100 Bitfinex.prototype.ticker = function(symbol, cb) {
101 return this.make_public_request('ticker/' + symbol, cb);
102 };
103
104 Bitfinex.prototype.today = function(symbol, cb) {
105 return this.make_public_request('today/' + symbol, cb);
106 };
107
108 Bitfinex.prototype.candles = function(symbol, cb) {
109 return this.make_public_request('candles/' + symbol, cb);
110 };
111
112 Bitfinex.prototype.lendbook = function(currency, cb) {
113 return this.make_public_request('lendbook/' + currency, cb);
114 };
115
116 Bitfinex.prototype.orderbook = function(symbol, cb) {
117 var maxOrders, uri;
118 maxOrders = 50;
119 uri = 'book/' + symbol + '/?limit_bids=' + maxOrders + '&limit_asks=' + maxOrders;
120 return this.make_public_request(uri, cb);
121 };
122
123 Bitfinex.prototype.trades = function(symbol, cb) {
124 return this.make_public_request('trades/' + symbol, cb);
125 };
126
127 Bitfinex.prototype.lends = function(currency, cb) {
128 return this.make_public_request('lends/' + currency, cb);
129 };
130
131 Bitfinex.prototype.get_symbols = function(cb) {
132 return this.make_public_request('symbols/', cb);
133 };
134
135 Bitfinex.prototype.new_order = function(symbol, amount, price, exchange, side, type, cb) {
136 var params;
137 params = {
138 symbol: symbol,
139 amount: amount,
140 price: price,
141 exchange: exchange,
142 side: side,
143 type: type
144 };
145 return this.make_request('order/new', params, cb);
146 };
147
148 Bitfinex.prototype.multiple_new_orders = function(symbol, amount, price, exchange, side, type, cb) {
149 var params;
150 params = {
151 symbol: symbol,
152 amount: amount,
153 price: price,
154 exchange: exchange,
155 side: side,
156 type: type
157 };
158 return this.make_request('order/new/multi', params, cb);
159 };
160
161 Bitfinex.prototype.cancel_order = function(order_id, cb) {
162 var params;
163 params = {
164 order_id: parseInt(order_id)
165 };
166 return this.make_request('order/cancel', params, cb);
167 };
168
169 Bitfinex.prototype.cancel_all_orders = function(cb) {
170 return this.make_request('order/cancel/all', {}, cb);
171 };
172
173 Bitfinex.prototype.cancel_multiple_orders = function(order_ids, cb) {
174 var params;
175 params = {
176 order_ids: order_ids.map(function(id) {
177 return parseInt(id);
178 })
179 };
180 return this.make_request('order/cancel/multi', params, cb);
181 };
182
183 Bitfinex.prototype.replace_order = function(order_id, symbol, amount, price, exchange, side, type, cb) {
184 var params;
185 params = {
186 order_id: parseInt(order_id),
187 symbol: symbol,
188 amount: amount,
189 price: price,
190 exchange: exchange,
191 side: side,
192 type: type
193 };
194 return this.make_request('order/cancel/replace', params, cb);
195 };
196
197 Bitfinex.prototype.order_status = function(order_id, cb) {
198 var params;
199 params = {
200 order_id: order_id
201 };
202 return this.make_request('order/status', params, cb);
203 };
204
205 Bitfinex.prototype.active_orders = function(cb) {
206 return this.make_request('orders', {}, cb);
207 };
208
209 Bitfinex.prototype.active_positions = function(cb) {
210 return this.make_request('positions', {}, cb);
211 };
212
213 Bitfinex.prototype.past_trades = function(symbol, timestamp, limit_trades, cb) {
214 var params;
215 params = {
216 symbol: symbol,
217 timestamp: timestamp,
218 limit_trades: limit_trades
219 };
220 return this.make_request('mytrades', params, cb);
221 };
222
223 Bitfinex.prototype.new_offer = function(currency, amount, rate, period, direction, insurance_option, cb) {
224 var params;
225 params = {
226 currency: currency,
227 amount: amount,
228 rate: rate,
229 period: period,
230 direction: direction,
231 insurance_option: insurance_option
232 };
233 return this.make_request('offer/new', params, cb);
234 };
235
236 Bitfinex.prototype.cancel_offer = function(offer_id, cb) {
237 var params;
238 params = {
239 order_id: offer_id
240 };
241 return this.make_request('offer/cancel', params, cb);
242 };
243
244 Bitfinex.prototype.offer_status = function(order_id, cb) {
245 var params;
246 params = {
247 order_id: order_id
248 };
249 return this.make_request('offer/status', params, cb);
250 };
251
252 Bitfinex.prototype.active_offers = function(cb) {
253 return this.make_request('offers', {}, cb);
254 };
255
256 Bitfinex.prototype.active_credits = function(cb) {
257 return this.make_request('credits', {}, cb);
258 };
259
260 Bitfinex.prototype.wallet_balances = function(cb) {
261 return this.make_request('balances', {}, cb);
262 };
263
264 return Bitfinex;
265
266 })();
267
268}).call(this);