-
Notifications
You must be signed in to change notification settings - Fork 16
/
mtgox.js
193 lines (160 loc) · 5.7 KB
/
mtgox.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var querystring = require("querystring"),
crypto = require("crypto"),
request = require("request"),
JSONStream = require("JSONStream");
function MtGoxClient(key, secret, currency) {
var self = this;
self.key = key;
self.secret = secret;
self._currency = currency || "BTCUSD";
var SATOSHI_FACTOR = Math.pow(10,8);
function makePublicRequest(path, args, callback) {
var params = querystring.stringify(args);
if (params) path = path + "?" + params;
return executeRequest(basicOptions(path), callback);
}
function makeRequest(path, args, callback) {
if (!self.key || !self.secret) {
throw new Error("Must provide key and secret to make this API request.");
}
// generate a nonce
args.nonce = (new Date()).getTime() * 1000;
// compute the post data
var postData = querystring.stringify(args);
// append the path to the post data
var message = path + "\0" + postData;
// compute the sha512 signature of the message
var hmac = crypto.createHmac("sha512", new Buffer(self.secret, "base64"));
hmac.update(message);
var options = basicOptions(path);
options.method = "POST";
options.body = postData;
options.headers["Rest-Key"] = self.key;
options.headers["Rest-Sign"] = hmac.digest("base64");
options.headers["Content-Length"] = postData.length;
return executeRequest(options, callback);
}
function executeRequest(options, callback) {
if (typeof callback == "function") {
request(options, function (err, res, body) {
var json;
if (err || !res || res.statusCode != 200) {
return callback(err || new Error("Request failed"));
}
// This try-catch handles cases where Mt.Gox returns 200 but responds with HTML,
// causing the JSON.parse to throw
try {
json = JSON.parse(body);
} catch(err) {
if (body.indexOf("<") != -1) {
return callback(new Error("MtGox responded with html:\n" + body));
} else {
return callback(new Error("JSON parse error: " + err));
}
}
callback(null, json);
});
} else {
var parser = JSONStream.parse(["data", true]);
request.get(options).pipe(parser);
return parser;
}
}
function basicOptions(path) {
return {
uri: "https://data.mtgox.com/api/2/" + path,
agent: false,
headers: {
"User-Agent": "Mozilla/4.0 (compatible; MtGox node.js client)",
"Content-type": "application/x-www-form-urlencoded"
}
};
}
self.setCurrency = function(currency) {
self._currency = currency;
};
self.info = function(callback) {
makeRequest(self._currency + "/money/info", {}, callback);
};
self.idKey = function(callback) {
makeRequest(self._currency + "/money/idkey", {}, callback);
};
self.orders = function(callback) {
makeRequest(self._currency + "/money/orders", {}, callback);
};
self.currency = function(callback) {
makePublicRequest(self._currency + "/money/currency", {}, callback);
};
self.ticker = function(callback) {
makePublicRequest(self._currency + "/money/ticker", {}, callback);
};
self.tickerFast = function(callback) {
makePublicRequest(self._currency + "/money/ticker_fast", {}, callback);
};
self.quote = function(type, amount, callback) {
makePublicRequest(self._currency + "/money/order/quote", {
"type": type,
"amount": amount
}, callback);
};
// price is an optional argument, if not used it must be set to null
self.add = function(type, amount_int, price_int, callback) {
var args = {
"type": type,
"amount_int": amount_int
};
if (price_int) args.price_int = price_int;
makeRequest(self._currency + "/money/order/add", args, callback);
};
self.cancel = function(id, callback) {
makeRequest(self._currency + "/money/order/cancel", {
"oid": id
}, callback);
};
// not currently implemented
self.result = function(type, order, callback) {
makeRequest(self._currency + "/money/order/result", {
"type": type,
"order": order
}, callback);
};
self.lag = function(callback) {
makePublicRequest(self._currency + "/money/order/lag", {}, callback);
};
self.fetchTrades = function(since, callback) {
var args = {};
if (typeof since != undefined) args.since = since;
return makePublicRequest(self._currency + "/money/trades/fetch", args, callback);
};
self.fetchDepth = function(callback) {
makePublicRequest(self._currency + "/money/depth/fetch", {}, callback);
};
self.fullDepth = function(callback) {
makePublicRequest(self._currency + "/money/depth/full", {}, callback);
};
// page is an optional argument, if not used it must be set to null
self.history = function(currency, page, callback) {
var args = { "currency": currency };
if (page) args.page = page;
makeRequest("money/wallet/history", args, callback);
};
self.sendBitcoin = function(address, amount, fee, callback) {
var amountInt = amount * SATOSHI_FACTOR;
var feeInt = fee * SATOSHI_FACTOR;
var args = { address: address, amount_int: amountInt, fee_int: feeInt };
makeRequest("money/bitcoin/send_simple", args, callback);
};
self.depositAddress = function(callback) {
self.info(function(err, json) {
if (err) return callback(err);
if (json.result !== "success") {
var error = new Error("Unexpected response while retrieving account number: " + json.result);
return callback(error);
}
var args = { "account": json.data.Link };
makeRequest(self._currency + "/money/bitcoin/get_address", args, callback);
});
}
// More to come!
}
module.exports = MtGoxClient;