-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
167 lines (131 loc) · 4.41 KB
/
api.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
const fetch = require('node-fetch');
const ccxt = require('ccxt');
const {getOrderBookType} = require('./utils');
const HttpsProxyAgent = require('https-proxy-agent');
// const _ = require('lodash');
const Bottleneck = require('bottleneck');
const bittrex = new ccxt.bittrex();
const binance = new ccxt.binance();
const poloniex = new ccxt.poloniex();
const cache = require('memory-cache');
let proxies = [
'',
'http://MyWk3P:TB4SmC@185.232.169.111:9110',
'http://6WwC7T:jsw6hZ@185.232.168.169:9741',
'http://P3Mo9t:kgJ9d1@185.232.171.98:9363',
'http://WWJvXz:9jqYoH@185.233.203.31:9703',
'http://WWJvXz:9jqYoH@185.233.201.138:9398',
'http://WWJvXz:9jqYoH@185.233.200.10:9357'
];
const bittrexLimiter = new Bottleneck({
minTime: 200,
// maxConcurrent: 1
});
const binanceLimiter = new Bottleneck({
minTime: 300,
// maxConcurrent: 3
});
const poloniexLimiter = new Bottleneck({
minTime: 300,
// maxConcurrent: 3
});
const bittrexProxyIterator = ProxyIterator(proxies);
const binanceProxyIterator = ProxyIterator(proxies);
const poloniexProxyIterator = ProxyIterator(proxies);
//bittrex 500, один стакан
//binance 1000, оба стакана, передавать глубину вторым аргументом
//kucoin limit не больше 100
//bitfinex null, {limit_bids: 10000, limit_asks: 10000
//poloniex 10000
let counter = 0;
let cacheCounter = 0;
async function getOrderBook(task) {
const {exchange, currency, bookType} = task;
let data;
let proxy;
const hash = `${exchange}-${currency}-${bookType}`;
let cachedData = cache.get(hash);
if (cachedData) {
// console.log(hash, 'from cache');
cacheCounter++;
return cachedData
}
// console.log(hash);
try {
if (exchange === 'bittrex') {
proxy = bittrexProxyIterator.next();
if (proxy) exchange.agent = new HttpsProxyAgent(proxy);
data = await bittrexLimiter.schedule(() => bittrex.fetchOrderBook(`${currency}/BTC`, null, {type: getOrderBookType(bookType)}));
}
if (exchange === 'binance') {
proxy = binanceProxyIterator.next();
if (proxy) exchange.agent = new HttpsProxyAgent(proxy);
data = await binanceLimiter.schedule(() => binance.fetchOrderBook(`${currency}/BTC`, 1000))
}
if (exchange === 'poloniex') {
proxy = poloniexProxyIterator.next();
if (proxy) exchange.agent = new HttpsProxyAgent(proxy);
data = await poloniexLimiter.schedule(() => poloniex.fetchOrderBook(`${currency}/BTC`, 1000))
}
counter++;
} catch (e) {
// console.log(e.message, 'creating recursive request');
if (e instanceof ccxt.RequestTimeout) {
console.log(task.exchange, task.currency, 'request timeout');
return getOrderBook(task);
} else {
return console.log(e.message)
}
}
// console.log(hash, proxy);
let result;
if (bookType === 1) result = data.bids.length && data.bids;
if (bookType === 2) result = data.asks.length && data.asks;
cache.put(hash, result, 50000);
// console.log(result.length, new Date());
return result
}
// const exchange = new ccxt.bitfinex();
// const proxyIterator = ProxyIterator(proxies);
// let testCounter = 0;
// setInterval(async () => {
// const proxy = proxyIterator.next();
// if (proxy) exchange.agent = new HttpsProxyAgent(proxy);
//
// try {
// let data = await exchange.fetchOrderBook('LTC/BTC', null,
// {limit_asks: 1000, limit_bids: 1000}
// );
// let sum = 0;
// data.bids.forEach(item => {
// sum += item[0] * item[1]
// });
// // console.log((await exchange.fetchOrderBook('LTC/BTC', 1000)).bids.length, proxy) // polo
// console.log(data.bids.length, sum, proxy); // bittrex
//
//
// testCounter++;
// } catch (e) {
// console.log(e.message, proxy)
//
// }
// }, 1000);
//
// setInterval(() => {
// console.log('REQUESTS PER MIN', counter, cacheCounter);
// counter = 0;
// cacheCounter = 0;
// }, 60000);
function ProxyIterator(array) {
let nextIndex = 0;
let copy = array.slice();
return {
next: () => {
if (nextIndex >= copy.length) nextIndex = 0;
return copy[nextIndex++]
}
}
}
module.exports = {
getOrderBook
};