1 |
|
2 | (function() {
|
3 | var Gemini, 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 = Gemini = (function() {
|
14 | function Gemini(key, secret, nonceGenerator) {
|
15 | this.url = "https://api.gemini.com";
|
16 | this.version = 'v1';
|
17 | this.key = key;
|
18 | this.secret = secret;
|
19 | this.nonce = new Date().getTime();
|
20 | this._nonce = typeof nonceGenerator === "function" ? nonceGenerator : function() {
|
21 | return ++this.nonce;
|
22 | };
|
23 | }
|
24 |
|
25 | Gemini.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 = '/' + this.version + '/' + 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-GEMINI-APIKEY': this.key,
|
45 | 'X-GEMINI-PAYLOAD': payload,
|
46 | 'X-GEMINI-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 (error1) {
|
61 | error = error1;
|
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 | Gemini.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 (error1) {
|
88 | error = error1;
|
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 | Gemini.prototype.orderbook = function(symbol, options, cb) {
|
101 | var err, index, option, query_string, uri, value;
|
102 | index = 0;
|
103 | uri = 'book/' + symbol;
|
104 | if (typeof options === 'function') {
|
105 | cb = options;
|
106 | } else {
|
107 | try {
|
108 | for (option in options) {
|
109 | value = options[option];
|
110 | if (index++ > 0) {
|
111 | query_string += '&' + option + '=' + value;
|
112 | } else {
|
113 | query_string = '/?' + option + '=' + value;
|
114 | }
|
115 | }
|
116 | if (index > 0) {
|
117 | uri += query_string;
|
118 | }
|
119 | } catch (error1) {
|
120 | err = error1;
|
121 | return cb(err);
|
122 | }
|
123 | }
|
124 | return this.make_public_request(uri, cb);
|
125 | };
|
126 |
|
127 | Gemini.prototype.trades = function(symbol, cb) {
|
128 | return this.make_public_request('trades/' + symbol, cb);
|
129 | };
|
130 |
|
131 | Gemini.prototype.get_symbols = function(cb) {
|
132 | return this.make_public_request('symbols', cb);
|
133 | };
|
134 |
|
135 | Gemini.prototype.symbols_details = function(cb) {
|
136 | return this.make_public_request('symbols_details', cb);
|
137 | };
|
138 |
|
139 | Gemini.prototype.new_order = function(symbol, amount, price, exchange, side, type, cb) {
|
140 | var params;
|
141 | params = {
|
142 | symbol: symbol,
|
143 | amount: amount,
|
144 | price: price,
|
145 | exchange: exchange,
|
146 | side: side,
|
147 | type: type
|
148 | };
|
149 | return this.make_request('order/new', params, cb);
|
150 | };
|
151 |
|
152 | Gemini.prototype.cancel_order = function(order_id, cb) {
|
153 | var params;
|
154 | params = {
|
155 | order_id: parseInt(order_id)
|
156 | };
|
157 | return this.make_request('order/cancel', params, cb);
|
158 | };
|
159 |
|
160 | Gemini.prototype.cancel_all_orders = function(cb) {
|
161 | return this.make_request('order/cancel/all', {}, cb);
|
162 | };
|
163 |
|
164 | Gemini.prototype.order_status = function(order_id, cb) {
|
165 | var params;
|
166 | params = {
|
167 | order_id: order_id
|
168 | };
|
169 | return this.make_request('order/status', params, cb);
|
170 | };
|
171 |
|
172 | Gemini.prototype.active_orders = function(cb) {
|
173 | return this.make_request('orders', {}, cb);
|
174 | };
|
175 |
|
176 | Gemini.prototype.account_infos = function(cb) {
|
177 | return this.make_request('account_infos', {}, cb);
|
178 | };
|
179 |
|
180 |
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | Gemini.prototype.withdraw = function(withdraw_type, walletselected, amount, address, cb) {
|
192 | var params;
|
193 | params = {
|
194 | withdraw_type: withdraw_type,
|
195 | walletselected: walletselected,
|
196 | amount: amount,
|
197 | address: address
|
198 | };
|
199 | return this.make_request('withdraw', params, cb);
|
200 | };
|
201 |
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | Gemini.prototype.transfer = function(amount, currency, walletfrom, walletto, cb) {
|
214 | var params;
|
215 | params = {
|
216 | amount: amount,
|
217 | currency: currency,
|
218 | walletfrom: walletfrom,
|
219 | walletto: walletto
|
220 | };
|
221 | return this.make_request('transfer', params, cb);
|
222 | };
|
223 |
|
224 | return Gemini;
|
225 |
|
226 | })();
|
227 |
|
228 | }).call(this);
|