-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcoinListGenerator.ts
81 lines (76 loc) · 2.98 KB
/
coinListGenerator.ts
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
import axios from 'axios';
import fs from "fs";
import Exchanges from "./exchanges";
import Tickers from './tickers';
import { IExchangeAsset } from './types';
const createEndpointAndTimeout = (tickerEndpoint: string, baseAsset: string, exchangeName: string) => {
var endpoint = tickerEndpoint + baseAsset;
var timeout=50;
switch (exchangeName) {
case "Binance":
endpoint += "USDT"
break;
case "Coinbase Exchange":
endpoint += "-USD/ticker"
timeout=150;
break;
case "FTX":
endpoint += "/USD"
break;
case "Kucoin":
endpoint += "-USDT"
break;
case "Gate.io":
endpoint += "_USDT"
break;
case "Bybit":
endpoint += "USDT&limit=1"
break;
case "Huobi":
endpoint = tickerEndpoint + baseAsset.toLowerCase() + "usdt&size=1"
break;
default:
break;
}
return {endpoint,timeout};
}
const generateCoinList = async () => {
var exchangeTasks = Exchanges.map(async (exchange) => {
console.time(`${exchange.name}, Done in`)
var tickerTasks = Tickers.map(async (ticker, index) => {
var {endpoint,timeout} = createEndpointAndTimeout(exchange.tickerEndpoint!, ticker.baseAsset, exchange.name);
var exchangeAsset: IExchangeAsset = { exchange: { name: exchange.name, tickerEndpoint: endpoint } };
await new Promise((resolve, reject) => {
setTimeout(async () => {
try {
await axios(endpoint).then((data) => {
if (data.data.status !== "error" && data.data.data !== null && data.data.result !== null) {
ticker.exchanges!.push(exchangeAsset)
}
}).catch((error) => {
if (error.response.status === 429) {
console.count(`${exchange.name},${error.response.status} Error Count`);
} else if (error.response.status !== 404) {
console.log(ticker.baseAsset, exchange.name, error.response.status)
}
})
} catch (error) {
console.log(ticker.baseAsset, exchange.name, error)
} finally {
resolve("");
}
}, timeout * index);
}).then(() => {
return new Promise((res, rej) => { res("") })
})
})
await Promise.all(tickerTasks).then(() => {
console.timeEnd(`${exchange.name}, Done in`)
return new Promise((res, rej) => { res("") })
})
})
await Promise.all(exchangeTasks).then(() => {
fs.writeFileSync('./data.json', JSON.stringify(Tickers, null, 2), 'utf-8');
})
}
generateCoinList()