Files
purple-explorer/lib/apis/coingecko.js
T
Joe Uhren 65c48ea829 Added market sync options: average and coingecko
-The previous market price calculation setting was hardcoded to only display market and USD prices for a single exchange and trading pair which was not very accurate for coins listed on multiple exchanges or with multiple trading pairs. The new default is to average the market prices for all supported exchanges and trading pairs
-The coingecko market price option was added to allow fetching the market price directly from the coingecko api instead of calculating it via supported exchanges known to the explorer
-Added a new root setting option for default_coingecko_ids which allows presetting symbols to their associated internal coingecko id to help prevent matching to the wrong currency with same symbol via coingecko api calls
-Fixed an issue where the explorer would fail to start with an enabled exchange that had no defined trading pairs
2024-01-05 00:47:22 -07:00

83 lines
3.1 KiB
JavaScript

const request = require('postman-request');
const base_url = 'https://api.coingecko.com/api/v3/';
function get_coin_list(cb) {
request({ uri: base_url + 'coins/list?include_platform=false', json: true}, function (error, response, body) {
if (error)
return cb(error, []);
else if (body == null || body == '' || typeof body !== 'object')
return cb('No data returned', []);
else
return cb(null, body);
});
}
function get_simple_price(id, currency, market_array, cb) {
request({ uri: base_url + `simple/price?ids=${id.toLowerCase()}&vs_currencies=usd${currency == null || currency == '' ? '' : `,${currency}`}`, json: true}, function (error, response, body) {
if (error)
return cb(error, 0, 0);
else if (body == null || body == '' || typeof body !== 'object')
return cb('No data returned', 0, 0);
else {
try {
if (market_array != null) {
// multiple currencies need to be combined before return
let last_price = 0;
let last_usd_price = 0;
let counter = 0;
// loop through all api object keys
Object.keys(body).forEach(function(key, index, map) {
const market_index = market_array.findIndex(p => p.coingecko_id.toLowerCase() == key.toLowerCase());
// check if the currency is found in the market_array
if (market_index > -1) {
// calculate the market and usd prices
last_price += (market_array[market_index].last_price * (currency == null || currency == '' ? 0 : body[key][currency.toLowerCase()]));
last_usd_price += (market_array[market_index].last_price * body[key]['usd']);
counter++;
}
});
// check if the counter is greater than 0
if (counter > 0) {
// average the market and usd prices
last_price = (last_price / counter);
last_usd_price = (last_usd_price / counter);
}
return cb(null, last_price, last_usd_price);
} else {
// single currency
return cb(null, (currency == null || currency == '' ? 0 : body[id.toLowerCase()][currency.toLowerCase()]), body[id.toLowerCase()]['usd']);
}
} catch(err) {
return cb('Received unexpected API data response', 0, 0);
}
}
});
}
module.exports = {
get_coin_data: function (cb) {
get_coin_list(function (err, coin_list) {
return cb(err, coin_list);
});
},
get_market_prices: function (id, currency, cb) {
get_simple_price(id, currency, null, function (err, last_price, last_usd) {
if (last_price == null)
console.log(`Error: "${currency}" is not a valid coingecko api currency`);
return cb(err, last_price, last_usd);
});
},
get_avg_market_prices: function (id, currency, market_array, cb) {
get_simple_price(id, currency, market_array, function (err, last_price, last_usd) {
if (last_price == null)
console.log(`Error: "${currency}" is not a valid coingecko api currency`);
return cb(err, last_price, last_usd);
});
}
};