Repository navigation
Expand file tree
/
Copy pathidex.js
More file actions
637 lines (605 loc) · 21.4 KB
/
Copy pathidex.js
File metadata and controls
637 lines (605 loc) · 21.4 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
(async () => {
const fs = require('fs');
const request = require('request');
const axios = require('axios');
const BigNumber = require('bignumber.js');
const readFilePromise = async file => await fs.promises.readFile(file);
const { soliditySha3 } = require('web3-utils');
const {
hashPersonalMessage,
bufferToHex,
toBuffer,
ecsign,
privateToAddress
} = require('ethereumjs-util');
const { mapValues } = require('lodash');
const idexContractAddress = '0x2a0c0dbecc7e4d658f48e01e3fa353f44050c208';
let apiKey = "", privateKey = "", headers = { "API-Key": apiKey }, contracts = {};
// Get IDEX contracts
const currencies = async () => {
return new Promise((resolve, reject) => {
axios.post('https://api.idex.market/returnCurrencies', {}, { headers })
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
throw error;
});
});
};
// Account nonce
const get_nonce = async (address) => {
return new Promise((resolve, reject) => {
axios.post('https://api.idex.market/returnNextNonce', { address }, { headers })
.then(function (response) {
resolve(response.data.nonce);
})
.catch(function (error) {
throw error;
});
});
};
// Convert from human amount to BigNumber
function expand(amount, decimals = 18) {
const bn = new BigNumber(amount);
return bn.times(10 ** decimals).toFixed(0);
}
// Return bid and ask
async function spread(market) {
if (market.indexOf("_") == -1) market = `ETH_${market}`;
try {
let book = await orderBook(market, 1);
return { ask: book.asks[0].price, bid: book.bids[0].price };
} catch (error) {
console.warn(error);
return false;
}
}
// Fill an orderHash using human numbers for amount
async function trade(orderHash, amount, decimals = 18) {
let bigamount = expand(amount, decimals);
return await rawtrade(orderHash, bigamount);
}
// Fill an orderHash using BigNumber
async function rawtrade(orderHash, amount) {
const nonce = await get_nonce(address);
const args = {
orderHash,
amount,
nonce,
address
};
const raw = soliditySha3(
{
t: 'uint256',
v: args.orderHash,
},
{
t: 'uint256',
v: args.amount,
},
{
t: 'address',
v: args.address,
},
{
t: 'uint256',
v: args.nonce,
},
);
const salted = hashPersonalMessage(toBuffer(raw));
const vrs = mapValues(
ecsign(salted, toBuffer(privateKey)),
(value, key) => key === 'v' ? value : bufferToHex(value),
);
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'https://api.idex.market/trade',
timeout: 10000,
headers: {
'Accept': 'application/json',
'API-Key': apiKey,
'User-agent': 'Node IDEX API'
},
json: {
address: args.address,
orderHash: args.orderHash,
amount: args.amount,
nonce: args.nonce,
v: vrs.v,
r: vrs.r,
s: vrs.s
}
}, function (err, resp, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
// Required to call on startup
async function init() {
return contracts = await currencies();
}
// Limit Buy
async function buy(tokenBuy, amountBuy, price) {
let total = price * amountBuy;
//let buyTokens = total / price;
return limit(tokenBuy, amountBuy, 'ETH', total);
}
// Limit Sell
async function sell(tokenSell, amountSell, price) {
let total = price * amountSell;
return limit('ETH', total, tokenSell, amountSell);
}
// Get decimals for symbol
function get_decimals(symbol) {
if (typeof contracts[symbol] == "undefined") throw `get_decimals undefined: ${symbol}`;
return contracts[symbol].decimals;
}
// Get BigNumber for human amount, and decimals for symbol
function get_amount(symbol, amount) {
return expand(amount, get_decimals(symbol));
}
// Limit order
async function limit(tokenBuy, amountBuy, tokenSell, amountSell, buyDecimals = 18, sellDecimals = 18) {
if (tokenBuy.length !== 42) {
if (typeof contracts[tokenBuy] == "undefined") throw `tokenBuy undefined: ${tokenBuy}`;
buyDecimals = contracts[tokenBuy].decimals;
tokenBuy = contracts[tokenBuy].address;
}
if (tokenSell.length !== 42) {
if (typeof contracts[tokenSell] == "undefined") throw `tokenSell undefined: ${tokenBuy}`;
sellDecimals = contracts[tokenSell].decimals;
tokenSell = contracts[tokenSell].address;
}
const nonce = await get_nonce(address);
const bigBuy = new BigNumber(amountBuy), bigSell = new BigNumber(amountSell);
amountBuy = bigBuy.times(10 ** buyDecimals).toFixed(0);
amountSell = bigSell.times(10 ** sellDecimals).toFixed(0);
return new Promise((resolve, reject) => {
const args = {
tokenBuy,
amountBuy,
tokenSell,
amountSell,
address,
nonce,
expires: 100000,
};
const raw = soliditySha3(
{
t: 'address',
v: idexContractAddress,
},
{
t: 'address',
v: args.tokenBuy,
},
{
t: 'uint256',
v: args.amountBuy,
},
{
t: 'address',
v: args.tokenSell,
},
{
t: 'uint256',
v: args.amountSell,
},
{
t: 'uint256',
v: args.expires,
},
{
t: 'uint256',
v: args.nonce,
},
{
t: 'address',
v: args.address,
},
);
const salted = hashPersonalMessage(toBuffer(raw));
const vrs = mapValues(
ecsign(salted, toBuffer(privateKey)),
(value, key) => key === 'v' ? value : bufferToHex(value),
);
request({
method: 'POST',
url: 'https://api.idex.market/order',
timeout: 10000,
headers: {
'Accept': 'application/json',
'API-Key': apiKey,
'User-agent': 'Node IDEX API'
},
json: {
tokenBuy: args.tokenBuy,
amountBuy: args.amountBuy,
tokenSell: args.tokenSell,
amountSell: args.amountSell,
address: args.address,
nonce: args.nonce,
expires: args.expires,
v: vrs.v,
r: vrs.r,
s: vrs.s
}
}, function (err, resp, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
// Cancel All
async function cancelAll(side = false) {
let orders = await get_open_orders(false, address, 100);
for (let order of orders) {
if (side && order.type != side) continue;
console.info(`..${order.type} ${Number(order.total).toFixed(2)} ${order.params.sellSymbol} worth of ${order.params.buySymbol} @ ${Number(order.price).toFixed(8)}`);
await cancel(order.orderHash);
}
}
// Cancel individual orderHash
async function cancel(orderHash) {
const nonce = await get_nonce(address);
const args = {
orderHash,
nonce,
address,
};
const raw = soliditySha3(
{
t: 'uint256',
v: orderHash,
},
{
t: 'uint256',
v: nonce,
},
);
const salted = hashPersonalMessage(toBuffer(raw));
const vrs = mapValues(
ecsign(salted, toBuffer(privateKey)),
(value, key) => key === 'v' ? value : bufferToHex(value),
);
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'https://api.idex.market/cancel',
timeout: 10000,
headers: {
'Accept': 'application/json',
'API-Key': apiKey,
'User-agent': 'Node IDEX API'
},
json: {
orderHash: args.orderHash,
nonce: args.nonce,
address,
v: vrs.v,
r: vrs.r,
s: vrs.s
}
}, function (err, resp, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
// Get IDEX trade history for a symbol
const get_trade_history = async (address = false, market = false, start = 0, count = 0, sort = "desc") => {
//if (!start) start = new Date().getTime() - (60 * 60 * 24 * 90); // 90 days default
return new Promise((resolve, reject) => {
let params = { sort };
if (address) params.address = address;
if (market) params.market = market;
if (start) params.start = parseInt(start / 1000);
if (count) params.count = count;
axios.post('https://api.idex.market/returnTradeHistory', params, { headers })
.then(function (response) {
resolve(response);
})
.catch(function (error) {
throw error;
});
});
}
// Get IDEX order book for a market
const orderBook = (market, count = 20) => {
if (market.indexOf("_") == -1) market = `ETH_${market}`;
//if (!start) start = new Date().getTime() - (60 * 60 * 24 * 90); // 90 days default
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'https://api.idex.market/returnOrderBook',
timeout: 10000,
headers: {
'Accept': 'application/json',
'API-Key': apiKey,
'User-agent': 'Node IDEX API'
},
json: { market, count }
}, function (err, resp, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
// Get IDEX balances for an address
const get_balances = (address = false) => {
if (!address) address = get_address();
return new Promise((resolve, reject) => {
axios.post('https://api.idex.market/returnBalances', { address }, { headers })
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
reject(error);
});
});
};
// Get IDEX ticker
const ticker = (market = false) => {
return new Promise((resolve, reject) => {
let params = market ? { market } : {};
axios.post('https://api.idex.market/returnTicker', params, { headers })
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
reject(error);
});
});
};
// Get 24h Volume
const return24Volume = (region = false) => {
return new Promise((resolve, reject) => {
let params = region ? { region } : {};
axios.post('https://api.idex.market/return24Volume', params, { headers })
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
reject(error);
});
});
};
// Get IDEX open orders
const get_open_orders = (market = false, address = false, count = 10) => {
return new Promise((resolve, reject) => {
let params = { count };
if (market) params.market = market;
if (address) params.address = address;
axios.post('https://api.idex.market/returnOpenOrders', params, { headers })
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
throw error;
});
});
};
// Get Contract Event Logs
const get_contract_eventlog = (address, contract) => {
return new Promise((resolve, reject) => {
axios.post(`https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=0&toBlock=latest&address=${contract}&topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef&topic2=0x000000000000000000000000${address.substr(2)}`, {})
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
throw error;
});
});
};
// Random number within range
const get_random = (min, max) => {
return Math.random() * (max - min) + min;
};
// Withdraw
async function withdraw(symbol, amount) {
if (typeof contracts[symbol] == "undefined") throw `withdraw: invalid symbol: ${symbol}`;
let decimals = contracts[symbol].decimals;
let token = contracts[symbol].address;
let bigamount = expand(amount, decimals);
const nonce = await get_nonce(address);
const raw = soliditySha3(
{
t: 'address',
v: idexContractAddress,
},
{
t: 'address',
v: token,
},
{
t: 'uint256',
v: bigamount,
},
{
t: 'address',
v: address,
},
{
t: 'uint256',
v: nonce
}
);
const salted = hashPersonalMessage(toBuffer(raw));
const vrs = mapValues(
ecsign(salted, toBuffer(privateKey)),
(value, key) => key === 'v' ? value : bufferToHex(value),
);
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'https://api.idex.market/withdraw',
timeout: 10000,
headers: {
'Accept': 'application/json',
'API-Key': apiKey,
'User-agent': 'Node IDEX API'
},
json: {
address,
token,
nonce,
amount: bigamount,
v: vrs.v,
r: vrs.r,
s: vrs.s
}
}, function (err, resp, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
// todo: marketBuy, marketSell: like below but continues until all is sold
// Buy amount from cheapest sell order
async function chomp_buy(market, maxAmount = Infinity) {
let book = await orderBook(market, 1), obj = book.asks[0];
let price = obj.price, orderHash = obj.orderHash, amount = obj.amount;
console.info(`${amount} @ ${price} ${orderHash}`);
let rawAmount = obj.params.amountBuy;
if (rawAmount > maxAmount) rawAmount = maxAmount;
let order = await rawtrade(orderHash, rawAmount);
console.info(order);
}
// Sell amount into best buy order
async function chomp_sell(symbol, maxAmount = Infinity) {
let market = `ETH_${symbol}`, book = await orderBook(market, 1), obj = book.bids[0];
let price = obj.price, orderHash = obj.orderHash, amount = obj.amount;
console.info(`${amount} @ ${price} ${orderHash}`);
let rawAmount = obj.params.amountBuy;
if (Number(amount) > Number(maxAmount)) {
let order = await trade(orderHash, maxAmount, get_decimals(symbol));
return console.info(order);
}
let order = await rawtrade(orderHash, rawAmount);
console.info(order);
}
// Set API Key
const set_apikey = (key) => {
apiKey = key;
headers = { "API-Key": apiKey };
};
// Convert private key to public ethereum address
const get_address = () => {
return '0x' + privateToAddress(privateKey).toString('hex');
};
// Pass apiKey and privateKey as parameters or from json.
const auth = (_apiKey, _privateKey = false) => {
if (_apiKey.endsWith(".json")) {
let json = JSON.parse(fs.readFileSync(_apiKey, "utf8"));
_apiKey = json.apiKey;
privateKey = json.privateKey;
} else {
privateKey = _privateKey;
}
if (!privateKey) throw "Invalid privateKey";
if (!_apiKey) console.warn("WARN: apiKey is not set");
set_apikey(_apiKey);
return get_address();
};
async function api(action, json = {}) {
const userAgent = 'Mozilla/4.0 (compatible; Node IDEX API)';
const contentType = 'application/json';
let headers = {
'User-Agent': userAgent,
'Content-type': contentType
};
try {
const response = await axios.request({
url: action,
headers: headers,
method: 'POST',
baseURL: 'https://api.idex.market/',
data: json
});
if (response && response.status !== 200) return new Error(JSON.stringify(response.data));
return response.data;
} catch (error) {
return new Error(JSON.stringify(error.response.data));
}
}
// So long, and thanks for all the fish!
module.exports = {
init,
buy,
sell,
trade,
rawtrade,
limit,
cancel,
cancelAll,
expand,
get_nonce,
get_decimals,
currencies,
ticker,
get_amount,
chomp_sell,
chomp_buy,
orderBook,
spread,
get_balances,
get_random,
set_apikey,
withdraw,
auth,
balances: get_balances,
get_address,
return24Volume,
returnTicker: async function returnTicker(ticker) {
const json = { market: `${ticker}` }
return await api(`returnTicker`, json)
},
returnOpenOrders: async function returnOpenOrders(market, address = null) {
console.log({ market })
return await api(`returnOpenOrders`, { market, address })
},
returnOrderBook: async function returnOrderBook(market, count = 100) {
return await api(`returnOrderBook`, { market, count })
},
returnOrderStatus: async function returnOrderStatus(orderHash) {
return await api(`returnOrderStatus`, { orderHash })
},
returnTradeHistory: async function returnTradeHistory() {
return await api(`returnTradeHistory`)
},
returnCurrencies: async function returnCurrencies() {
return await api(`returnCurrencies`)
},
returnBalances: async function returnBalances() {
return await api(`returnBalances`)
},
returnCompleteBalances: async function returnCompleteBalances() {
return await api(`returnCompleteBalances`)
},
returnDepositsWithdrawals: async function returnDepositsWithdrawals() {
return await api(`returnDepositsWithdrawals`)
},
returnOrderTrades: async function returnOrderTrades() {
return await api(`returnOrderTrades`)
},
returnNextNonce: async function returnNextNonce(address) {
return await api(`returnNextNonce`, { address })
},
returnContractAddress: async function returnContractAddress() {
return await api(`returnContractAddress`)
},
// Convert to sortable array. {"ETHBTC":{}} to [{symbol:"ETHBTC"}]
obj_to_array: json => {
let output = [];
for (let key in json) {
let obj = json[key];
obj.symbol = key;
output.push(obj);
}
return output;
}
};
})();