UNPKG

4.43 kBJavaScriptView Raw
1'use strict';
2var request = require('request'),
3 crypto = require('crypto'),
4 querystring = require('querystring');
5
6var BTCE = function(apiKey, secret, options) {
7 this.url = 'https://btc-e.com/tapi';
8 this.publicApiUrl = 'https://btc-e.com/api/2/';
9 this.timeout = 5000;
10 this.apiKey = apiKey;
11 this.secret = secret;
12 this._strictSSL = true;
13
14 if (typeof options === 'function') {
15 this.nonce = options;
16 } else if (options) {
17 this.nonce = options.nonce;
18 this.agent = options.agent;
19
20 if (typeof options.timeout !== 'undefined') {
21 this.timeout = options.timeout;
22 }
23 if (typeof options.tapi_url !== 'undefined') {
24 this.url = options.tapi_url;
25 }
26 if (typeof options.public_url !== 'undefined') {
27 this.publicApiUrl = options.public_url;
28 }
29 if (typeof options.strict_ssl !== 'undefined') {
30 this._strictSSL = !!options.strict_ssl;
31 }
32 }
33};
34
35BTCE.prototype._sendRequest = function (options, callback) {
36 var self = this;
37 var requestOptions = {
38 timeout: self.timeout,
39 agent: self.agent,
40 strictSSL: self._strictSSL
41 };
42
43 for (var key in options) {
44 requestOptions[key] = options[key];
45 }
46
47 request(requestOptions, function(err, response, body) {
48 if(err || response.statusCode !== 200) {
49 return callback(new Error(err || response.statusCode));
50 }
51
52 var result;
53 try {
54 result = JSON.parse(body);
55 } catch(error) {
56 return callback(error);
57 }
58
59 if(result.error) {
60 return callback(new Error(result.error));
61 }
62
63 callback(null, result);
64 });
65};
66
67BTCE.prototype.makeRequest = function(method, params, callback) {
68 var self = this;
69
70 if(!self.apiKey || !self.secret) {
71 return callback(new Error('Must provide API key and secret to use the trade API.'));
72 }
73
74 // If the user provided a function for generating the nonce, then use it.
75 if(self.nonce) {
76 params.nonce = self.nonce();
77 } else {
78 params.nonce = Math.round((new Date()).getTime() / 1000);
79 }
80
81 var formData = {};
82 for (var key in params) {
83 formData[key] = params[key];
84 }
85 formData.method = method;
86
87 var form = querystring.stringify(formData);
88 var sign = crypto.createHmac('sha512', self.secret).update(new Buffer(form)).digest('hex').toString();
89
90 self._sendRequest({
91 url: self.url,
92 method: 'POST',
93 form: form,
94 headers: {
95 Sign: sign,
96 Key: self.apiKey
97 }
98 }, callback);
99};
100
101BTCE.prototype.makePublicApiRequest = function(pair, method, callback) {
102 this._sendRequest({
103 url: this.publicApiUrl + pair + '/' + method
104 }, callback);
105};
106
107BTCE.prototype.getInfo = function(callback) {
108 this.makeRequest('getInfo', {}, callback);
109};
110
111BTCE.prototype.transHistory = function(params, callback) {
112 this.makeRequest('TransHistory', params, callback);
113};
114
115BTCE.prototype.tradeHistory = function(params, callback) {
116 this.makeRequest('TradeHistory', params, callback);
117};
118
119BTCE.prototype.orderInfo = function(paramsOrOrderId, callback) {
120 var inputType = typeof paramsOrOrderId;
121 var input = (inputType === 'string' || inputType === 'number') ?
122 {order_id: paramsOrOrderId} : paramsOrOrderId;
123 this.makeRequest('OrderInfo', input, callback);
124};
125
126BTCE.prototype.orderList = function(params, callback) {
127 this.makeRequest('OrderList', params, callback);
128};
129
130BTCE.prototype.activeOrders = function(pair, callback) {
131 if (!callback) {
132 callback = pair;
133 pair = null;
134 }
135
136 this.makeRequest('ActiveOrders', {pair: pair}, callback);
137};
138
139BTCE.prototype.trade = function(pair, type, rate, amount, callback) {
140 this.makeRequest('Trade', {
141 'pair': pair,
142 'type': type,
143 'rate': rate,
144 'amount': amount
145 }, callback);
146};
147
148BTCE.prototype.cancelOrder = function(paramsOrOrderId, callback) {
149 var inputType = typeof paramsOrOrderId;
150 var input = (inputType === 'string' || inputType === 'number') ?
151 {order_id: paramsOrOrderId} : paramsOrOrderId;
152 this.makeRequest('CancelOrder', input, callback);
153};
154
155BTCE.prototype.ticker = function(pair, callback) {
156 this.makePublicApiRequest(pair, 'ticker', callback);
157};
158
159BTCE.prototype.trades = function(pair, callback) {
160 this.makePublicApiRequest(pair, 'trades', callback);
161};
162
163BTCE.prototype.depth = function(pair, callback) {
164 this.makePublicApiRequest(pair, 'depth', callback);
165};
166
167BTCE.prototype.fee = function(pair, callback) {
168 this.makePublicApiRequest(pair, 'fee', callback);
169};
170
171module.exports = BTCE;