-
-
Save jraicr/1855b19dc4ec920a1c267e0e67abddd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const log = require('ololog').noLocate | |
const ccxt = require('ccxt') | |
const ansi = require('ansicolor').nice // eslint-disable-line no-unused-vars | |
require('as-table') | |
const cryptocurrencies = require('cryptocurrencies') | |
var ExchangeData = {} // Keeps all the interested currencies data from exchanges | |
// var MarketcapData = {} // Keeps all data relative to the currencies marketcap | |
module.exports = { | |
/********************************************** | |
* HTTP GET: Currencies data from the exchanges | |
**********************************************/ | |
async getMarketcapData (req, res) { | |
res.send({ | |
message: ExchangeData | |
}) | |
} | |
} | |
// Prints all supported exchanges by ccxt | |
let printSupportedExchanges = function () { | |
log('Supported exchanges:', ccxt.exchanges.join(', ')) | |
} | |
async function loadMarkets (exchanges) { | |
for (let i = 0; i < exchanges.length; i++) { | |
log('[Loading]'.blue, exchanges[i].name.white, 'market.'.white) | |
try { | |
await exchanges[i].loadMarkets() | |
} catch (error) { | |
log(error) | |
} | |
} | |
} | |
let instantiateExchanges = async (id) => { | |
let exchanges = [] | |
// Checks if the exchanges are supported by ccxt | |
for (let i = 0; i < id.length; i++) { | |
let exchangeFound = (ccxt.exchanges.indexOf(id[i]) > -1) | |
if (exchangeFound) { | |
log('\nInstantiating'.white, id[i].blue, 'exchange'.white) | |
// Instantiates the exchange by id | |
let e = new ccxt[id[i]]({ enableRateLimit: false }) | |
switch (e.name) { | |
case 'Binance': | |
e.rateLimit = 13200 | |
log('Setting rate limit at', e.rateLimit, 'ms. delay between requests.') | |
break | |
case 'Bitfinex v2': | |
e.rateLimit = 3000 | |
log('Setting rate limit at', e.rateLimit, 'ms. delay between requests.') | |
break | |
case 'Bitfinex': | |
e.rateLimit = 2500 | |
log('Setting rate limit at', e.rateLimit, 'ms. delay between requests.') | |
break | |
case 'bitkk': | |
e.enableRateLimit = true | |
break | |
case 'chbtc': | |
e.enableRateLimit = true | |
break | |
} | |
// We configure some conditions here that the exchanges must meet: | |
// 1. They must be cappable of fetch all the tickers at once | |
// 2. API must be public | |
// The first point can be avoided if we collect data with websockets. | |
// | |
// Observation: Some problematic exchanges or not convenients | |
// were added to the conditions to avoid their use | |
let conditions = e.has.publicAPI && e.name !== 'coinspot' && e.name !== 'huobicny' && | |
e.name !== 'Braziliex' && e.name !== 'CoinEgg' && | |
e.name !== 'Coingi' && e.name !== 'CoinTiger' && e.name !== 'CoolCoin' && | |
e.name !== 'ICE3X' && e.name !== 'LiveCoin' && e.name !== 'YUNBI' && e.name !== 'BtcBox' && | |
e.name !== 'BTCExchange' && e.name !== 'jubi.com' && e.name !== 'Tidex' && e.name !== 'YoBit' && | |
e.name !== 'CoinMarketCap' && e.name !== 'Bitfinex' && e.name !== 'Allcoin' && e.name !== 'Novaexchange' | |
// Checks if the exchange meet the conditions and keep it in the array | |
// otherwise it shows just a skip message and the exchange will not be included | |
// in the array. | |
if (conditions) { | |
log(' ', '✓'.green, e.name.green, '[Loaded]\n'.green) | |
exchanges.push(e) | |
} else { | |
log(' ', '▲'.yellow, e.name.yellow, ' [Skipped]\n'.yellow) | |
} | |
} else { | |
log('Exchange', id[i], 'not found') | |
printSupportedExchanges() | |
} | |
} | |
await loadMarkets(exchanges) | |
log('\n ····· [Markets loading finished] ····· \n'.green) | |
// let coins = cryptocurrencies.symbols() | |
// Loop market ticker fetching and data calculations | |
// TOOD [X]: All data from currencies will be collected in ExchangeData. | |
// TODO [ ]: All data relative to marketcap will be collected in MarketcapData. Mostly calculations | |
while (true) { | |
for (let i = 0; i < exchanges.length; i++) { | |
let symbols = exchanges[i].symbols | |
let tickers = {} | |
if (exchanges[i].has.fetchTickers) { | |
try { | |
tickers = await exchanges[i].fetchTickers() | |
log('Fetching tickers from:'.white, exchanges[i].id.green, '[', exchanges[i].iso8601(exchanges[i].milliseconds()), ']') | |
} catch (error) { | |
log(error) | |
} | |
} else { | |
try { | |
for (let j = 0; j < symbols.length; j++) { | |
let ticker = await exchanges[i].fetchTicker(symbols[j]) | |
tickers[symbols[j]] = ticker | |
log('Fetching and updating symbol', symbols[j], 'ticker from:'.white, exchanges[i].id.green, '[', exchanges[i].iso8601(exchanges[i].milliseconds()), ']') | |
} | |
log('Data fetched from ', exchanges[i].name) | |
} catch (error) { | |
log(error) | |
} | |
} | |
ExchangeData[exchanges[i].id] = ccxt.sortBy(Object.values(tickers), 'symbol', false).map(ticker => ({ | |
exchange: exchanges[i].name, | |
symbol: ticker['symbol'], | |
price: ticker['last'], | |
volume: ticker['volume'] || ticker['24h_volume'] || ticker['quoteVolume'], | |
baseVolume: ticker['baseVolume'], | |
change: ticker['change'], | |
percentageChange: ticker['percentage'], | |
datetime: ticker['datetime'] | |
})) | |
} | |
} | |
} | |
var allExchanges = ccxt.exchanges | |
// var simpleExchanges = ['poloniex', 'bitfinex2'] | |
instantiateExchanges(allExchanges) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment