-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (111 loc) · 3.63 KB
/
index.js
File metadata and controls
130 lines (111 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var request = require('request')
, crypto = require('crypto')
, querystring = require('querystring')
, MTGOX_HTTP_ENDPOINT = 'https://data.mtgox.com/api/2/BTCUSD/money/'
;
module.exports = new HttpGoxClient(options);
// options will contain at least the key and the secret
function HttpGoxClient (options) {
this.options = options || {};
this.key = this.options.key;
this.b64secret = new Buffer(this.options.secret, 'base64');
}
HttpGoxClient.prototype._executeRequest = function (options, cb) {
if ('function' === typeof cb) {
request(options, function(err, res, body) {
if (err) {
cb(err);
} else {
cb(null, JSON.parse(body));
}
});
} else {
return request(options);
}
};
HttpGoxClient.prototype._makePublicRequest = function (path, args, cb) {
// deal with optional args argument
if ('function' === typeof args) {
cb = args;
args = {};
}
args ? args : args = {};
var qs = querystring.stringify(args);
if (qs) path = path + '?' + querystring;
var headers = {
'User-Agent': 'czzarr-mtgox-http-client',
};
var url = MTGOX_HTTP_ENDPOINT + path;
var options = { url: url, headers: headers };
return this._executeRequest(options, cb);
};
HttpGoxClient.prototype._makePrivateRequest = function (path, args, cb) {
// deal with optional args argument
if ('function' === typeof args) {
cb = args;
args = {};
}
args ? args : args = {};
var url = MTGOX_HTTP_ENDPOINT + path;
// use tonces instead of nonces to be able to send multiple requests at once
args.tonce = Date.now() * 1000; // + Math.ceil(Math.random() * 1000);
var data = querystring.stringify(args);
var hash_data = 'BTCUSD/money/' + path + '\0' + data;
var hmac = crypto.createHmac('sha512', this.b64secret);
hmac.write(hash_data);
hmac.end();
var headers = {
'User-Agent': 'czzarr-mtgox-http-client',
'Rest-Key': this.key,
'Rest-Sign': hmac.read().toString('base64'),
'Content-Length': data.length,
'Content-Type': 'application/x-www-form-urlencoded'
};
var options = { url: url, method: 'POST', body: data, headers: headers };
return this._executeRequest(options, cb);
};
HttpGoxClient.prototype.add = function (type, amount_int, price_int, cb) {
// PREVENT MARKET ORDERS
if (!typeof price_int === 'number') {
console.log('trying to place a market order, exiting');
return;
}
var path = 'order/add';
var args = { type: type, amount_int: amount_int, price_int: price_int };
return this._makePrivateRequest(path, args, cb);
};
HttpGoxClient.prototype.cancel = function (orderId, cb) {
var path = 'order/cancel';
var args = { oid: orderId };
return this._makePrivateRequest(path, args, cb);
};
HttpGoxClient.prototype.idKey = function (cb) {
var path = 'idkey';
return this._makePrivateRequest(path, cb);
};
HttpGoxClient.prototype.info = function (cb) {
var path = 'info';
return this._makePrivateRequest(path, cb);
};
HttpGoxClient.prototype.lag = function (cb) {
var path = 'order/lag';
return this._makePublicRequest(path, cb);
};
HttpGoxClient.prototype.orders = function (cb) {
var path = 'orders';
return this._makePrivateRequest(path, cb);
};
HttpGoxClient.prototype.result = function (type, orderId, cb) {
var path = 'order/result';
var args = { type: type, order: orderId };
return this._makePrivateRequest(path, args, cb);
};
HttpGoxClient.prototype.tickerFast = function (cb) {
var path = 'ticker_fast';
return this._makePublicRequest(path, cb);
};
HttpGoxClient.prototype.trades = function (since, cb) {
var path = 'trades/fetch';
var args = { since: since }
return this._makePublicRequest(path, args, cb);
};