Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import ccxt from 'ccxt';
import { sleep } from '../api/utils';
export const exchanges = ccxt.exchanges.map((exchangeId) => {
const exchange = new ccxt[exchangeId]();
exchange.loadMarkets()
// eslint-disable-next-line no-unused-vars
.then((markets) => {
// console.log(markets);
})
.catch(() => {
console.log('ERROR : loadMarkets', exchangeId);
return sleep();
});
return exchange;
});
const baseExchangeInfo = (exchange) => {
const countries = typeof exchange.countries === 'string' ? [exchange.countries] : exchange.countries;
return {
id: exchange.id,
name: exchange.name,
logo: exchange.urls ? exchange.urls.logo : null,
url,
hasOrderBook: exchange.has.fetchOrderBook,
countries: countries.map(c => (c === 'UK' ? 'GB' : c)),
fees,
currencies: exchange.currencies ? Object.keys(exchange.currencies).map(key =>
(exchange.currencies[key])) : [],
markets: exchange.markets ?
Object.keys(exchange.markets).map(key => (exchange.markets[key])) : [],
};
};
const exchanges = [];
ccxt.exchanges.forEach((exchangeId) => {
const exchange = new ccxt[exchangeId]();
exchange.loadMarkets()
// eslint-disable-next-line no-unused-vars
.then((markets) => {
exchanges.push(baseExchangeInfo(exchange));
})
.catch(() => {
console.log('ERROR : loadMarkets', exchangeId);
return sleep();
});
});
exchanges.push({ id: 'CCCAGG', name: 'CCCAGG', countries: [] });
module.exports.exchangeObj = (exchange) => {
const exch = exchanges.find(item => item.name === exchange);
if (exch) {
return new ccxt[exch.id]();
getMarketExchangeByToken() {
let exchangeNormalizedArray = [];
function marketCallback(responseData) {
return responseData;
}
ccxt.exchanges.forEach(function (exchangeName, exIdx, exchangeArr) {
try {
let currentExchange = markets.getExchangeInstance(exchangeName);
currentExchange.fetchMarkets().then(function (exchangeMarket) {
let exchangeRows = [];
let timeStamp = Date.now();
Object.keys(exchangeMarket).forEach(function (marketKey) {
exchangeRows.push({
"base": exchangeMarket[marketKey].base, "quote": exchangeMarket[marketKey].quote,
"symbol": exchangeMarket[marketKey].symbol, "market": exchangeName,
"timestamp": timeStamp
})
});
exchangeNormalizedArray = exchangeNormalizedArray.concat(MarketUtils.mergeQuotesForBaseInExchange(exchangeRows));
// console.log(exchangeNormalizedArray.length);
if (exIdx === exchangeArr.length - 1) {
marketCallback(MarketUtils.groupCoinByMarketMaps(exchangeNormalizedArray));
listExchangeMetadata: function() {
var actions = ccxt.exchanges.map(getExchangeMetadata);
var results = Promise.all(actions); // pass array of promises
return results.then(function(response){
return response;
}).catch(function(err){
return err;
});
},
async start () {
// Check exchange
if (!ccxt.exchanges.includes(this.exchange)) {
log.bright.red.error(`Exchange "${this.exchange}" is not supported YET`)
return false
}
// Load the exchange
log.cyan(`Using Exchange "${this.exchange}"`)
this.exchange = new ccxt[this.exchange](config.get(this.exchange))
// Save the markets and marketsInfo
this.marketsInfo = await this.exchange.loadMarkets()
this.markets = Object.keys(this.marketsInfo)
// Check if we selected a market/pair that is valid
if (!this.markets.includes(this.market)) {
log.bright.red.error(`Market "${this.market}" is not supported YET`)
return false
getExchangeDetailsList: function() {
var actions = ccxt.exchanges.map(getExchangeDetails);
var results = Promise.all(actions); // pass array of promises
return results.then(function(response){
return response;
});
},
.end(function(err, res) {
should.not.exist(err);
res.body.should.eql(ccxt.exchanges.map(i => '' + i));
done();
});
});
const initCCXT = async function(privateKeys) {
var stocks = _.clone(ccxt.exchanges)
for (let stock of stocks) {
if (global.CCXT[`${stock}--public`] === undefined) {
try {
global.CCXT[`${stock}--public`] = new ccxt[stock] ({
'enableRateLimit': true
})
} catch(err) {
console.log(`Can't create ${stock} instance`)
}
}
}
for (let key of privateKeys) {
global.ACCOUNTS[key.id] = {
id: key.id,
name: key.name,
}
}
};
Object.keys(UNSUPPORTED_EXCHANGES).forEach(exchangeName => {
exchangeConfigs[exchangeName] = {
override : (functionName, req, res, defaultBehaviour) => {
const details = UNSUPPORTED_EXCHANGES[exchangeName]
const message = `[${exchangeName}] is currently ${details.status} and not supported right now (${details.moreInfo}).`
throw new ccxtRestErrors.BrokenExchangeError(message)
}
}
})
module.exports = Object.assign(exchangeConfigs, {
exchanges: ccxt.exchanges.filter(exchangeName => !UNSUPPORTED_EXCHANGES.hasOwnProperty(exchangeName)),
})
router.get('/', function(req, res, next) {
res.send(CircularJSON.stringify(ccxt.exchanges));
});