Added a coingecko api key option

-As of Feb 2024 the free "keyless" coingecko api will be deprecated and will likely stop working. There is a free demo api key that can be used which can now be plugged into the explorer settings to allow the coingecko api to continue working. Read more here: https://support.coingecko.com/hc/en-us/articles/21880397454233
This commit is contained in:
Joe Uhren
2024-01-05 14:55:34 -07:00
parent 65c48ea829
commit 9851e2ce9d
4 changed files with 28 additions and 18 deletions
+15 -11
View File
@@ -1,23 +1,27 @@
const request = require('postman-request'); const request = require('postman-request');
const base_url = 'https://api.coingecko.com/api/v3/'; const base_url = 'https://api.coingecko.com/api/v3/';
function get_coin_list(cb) { function get_coin_list(api_key, cb) {
request({ uri: base_url + 'coins/list?include_platform=false', json: true}, function (error, response, body) { request({ uri: base_url + 'coins/list?include_platform=false' + (api_key == null || api_key == '' ? '' : '&x_cg_demo_api_key=' + api_key), json: true}, function (error, response, body) {
if (error) if (error)
return cb(error, []); return cb(error, []);
else if (body == null || body == '' || typeof body !== 'object') else if (body == null || body == '' || typeof body !== 'object')
return cb('No data returned', []); return cb('No data returned', []);
else if (body['status'] != null && body['status']['error_message'] != null && body['status']['error_message'] != '')
return cb(body['status']['error_message'], []);
else else
return cb(null, body); return cb(null, body);
}); });
} }
function get_simple_price(id, currency, market_array, cb) { function get_simple_price(id, currency, market_array, api_key, cb) {
request({ uri: base_url + `simple/price?ids=${id.toLowerCase()}&vs_currencies=usd${currency == null || currency == '' ? '' : `,${currency}`}`, json: true}, function (error, response, body) { request({ uri: base_url + `simple/price?ids=${id.toLowerCase()}&vs_currencies=usd${currency == null || currency == '' ? '' : `,${currency}`}` + (api_key == null || api_key == '' ? '' : '&x_cg_demo_api_key=' + api_key), json: true}, function (error, response, body) {
if (error) if (error)
return cb(error, 0, 0); return cb(error, 0, 0);
else if (body == null || body == '' || typeof body !== 'object') else if (body == null || body == '' || typeof body !== 'object')
return cb('No data returned', 0, 0); return cb('No data returned', 0, 0);
else if (body['status'] != null && body['status']['error_message'] != null && body['status']['error_message'] != '')
return cb(body['status']['error_message'], 0, 0);
else { else {
try { try {
if (market_array != null) { if (market_array != null) {
@@ -59,21 +63,21 @@ function get_simple_price(id, currency, market_array, cb) {
} }
module.exports = { module.exports = {
get_coin_data: function (cb) { get_coin_data: function (api_key, cb) {
get_coin_list(function (err, coin_list) { get_coin_list(api_key, function (err, coin_list) {
return cb(err, coin_list); return cb(err, coin_list);
}); });
}, },
get_market_prices: function (id, currency, cb) { get_market_prices: function (id, currency, api_key, cb) {
get_simple_price(id, currency, null, function (err, last_price, last_usd) { get_simple_price(id, currency, null, api_key, function (err, last_price, last_usd) {
if (last_price == null) if (last_price == null)
console.log(`Error: "${currency}" is not a valid coingecko api currency`); console.log(`Error: "${currency}" is not a valid coingecko api currency`);
return cb(err, last_price, last_usd); return cb(err, last_price, last_usd);
}); });
}, },
get_avg_market_prices: function (id, currency, market_array, cb) { get_avg_market_prices: function (id, currency, market_array, api_key, cb) {
get_simple_price(id, currency, market_array, function (err, last_price, last_usd) { get_simple_price(id, currency, market_array, api_key, function (err, last_price, last_usd) {
if (last_price == null) if (last_price == null)
console.log(`Error: "${currency}" is not a valid coingecko api currency`); console.log(`Error: "${currency}" is not a valid coingecko api currency`);
+3
View File
@@ -930,6 +930,9 @@ exports.markets_page = {
// The USD fiat currency is also built-in and automatically returned from the coingecko api so there is no need to specify USD here. // The USD fiat currency is also built-in and automatically returned from the coingecko api so there is no need to specify USD here.
// For more information about which currency values are supported, you can review the vs_currencies parameter of the /simple/price api from here: https://www.coingecko.com/api/documentation // For more information about which currency values are supported, you can review the vs_currencies parameter of the /simple/price api from here: https://www.coingecko.com/api/documentation
"coingecko_currency": "BTC", "coingecko_currency": "BTC",
// "coingecko_api_key": Supply a free or paid coingecko api key for fetching USD price and/or market price via the market_price = "COINGECKO" option
// NOTE: As of Feb 2024, the free "keyless" coingecko api will be deprecated and require an api key to continue use
"coingecko_api_key": "",
// default_exchange: a collection of settings that pertain to the default exchange // default_exchange: a collection of settings that pertain to the default exchange
// When the "show_market_dropdown_menu" setting is disabled, the market header menu will navigate directly to the default exchange page // When the "show_market_dropdown_menu" setting is disabled, the market header menu will navigate directly to the default exchange page
// The default_exchange.trading_pair is used to determine the last market price when the market_price value is set to AVERAGE // The default_exchange.trading_pair is used to determine the last market price when the market_price value is set to AVERAGE
+7 -7
View File
@@ -1026,7 +1026,7 @@ function get_market_price(market_array) {
console.log('Calculating market price.. Please wait..'); console.log('Calculating market price.. Please wait..');
// get the market price from coingecko api // get the market price from coingecko api
coingecko.get_market_prices(coingecko_id, currency, function (err, last_price, last_usd_price) { coingecko.get_market_prices(coingecko_id, currency, settings.markets_page.coingecko_api_key, function (err, last_price, last_usd_price) {
// check for errors // check for errors
if (err == null) { if (err == null) {
// get current stats // get current stats
@@ -1050,7 +1050,7 @@ function get_market_price(market_array) {
}); });
} else { } else {
// coingecko api returned an error // coingecko api returned an error
console.log(err); console.log(`Error: ${err}`);
exit(1); exit(1);
} }
}); });
@@ -1091,7 +1091,7 @@ function get_market_price(market_array) {
const currency = lib.get_market_currency_code(); const currency = lib.get_market_currency_code();
// get the market price from coingecko api // get the market price from coingecko api
coingecko.get_avg_market_prices(api_ids, currency, market_array, function (mkt_err, last_price, last_usd) { coingecko.get_avg_market_prices(api_ids, currency, market_array, settings.markets_page.coingecko_api_key, function (mkt_err, last_price, last_usd) {
// check for errors // check for errors
if (mkt_err == null) { if (mkt_err == null) {
// update the last usd price // update the last usd price
@@ -1108,7 +1108,7 @@ function get_market_price(market_array) {
}); });
} else { } else {
// coingecko api returned an error // coingecko api returned an error
console.log(mkt_err); console.log(`Error: ${mkt_err}`);
exit(1); exit(1);
} }
}); });
@@ -1118,7 +1118,7 @@ function get_market_price(market_array) {
} }
} else { } else {
// coingecko api returned an error // coingecko api returned an error
console.log(coin_err); console.log(`Error: ${coin_err}`);
exit(1); exit(1);
} }
}); });
@@ -1173,7 +1173,7 @@ function coingecko_coin_list_api(market_symbols, cb) {
const coingecko = require('../lib/apis/coingecko'); const coingecko = require('../lib/apis/coingecko');
// get the list of coins from coingecko // get the list of coins from coingecko
coingecko.get_coin_data(function (err, coin_list) { coingecko.get_coin_data(settings.markets_page.coingecko_api_key, function (err, coin_list) {
// check if there was an error // check if there was an error
if (err == null) { if (err == null) {
// initialize the rate limiter to wait 2 seconds between requests to prevent abusing external apis // initialize the rate limiter to wait 2 seconds between requests to prevent abusing external apis
@@ -1211,7 +1211,7 @@ function find_coingecko_id(symbol, cb) {
} }
} else { } else {
// failed to get the coingecko api list // failed to get the coingecko api list
console.log(err); console.log(`Error: ${err}`);
return cb(''); return cb('');
} }
}); });
+3
View File
@@ -1014,6 +1014,9 @@
// The USD fiat currency is also built-in and automatically returned from the coingecko api so there is no need to specify USD here. // The USD fiat currency is also built-in and automatically returned from the coingecko api so there is no need to specify USD here.
// For more information about which currency values are supported, you can review the vs_currencies parameter of the /simple/price api from here: https://www.coingecko.com/api/documentation // For more information about which currency values are supported, you can review the vs_currencies parameter of the /simple/price api from here: https://www.coingecko.com/api/documentation
"coingecko_currency": "BTC", "coingecko_currency": "BTC",
// "coingecko_api_key": Supply a free or paid coingecko api key for fetching USD price and/or market price via the market_price = "COINGECKO" option
// NOTE: As of Feb 2024, the free "keyless" coingecko api will be deprecated and require an api key to continue use
"coingecko_api_key": "",
// default_exchange: a collection of settings that pertain to the default exchange // default_exchange: a collection of settings that pertain to the default exchange
// When the "show_market_dropdown_menu" setting is disabled, the market header menu will navigate directly to the default exchange page // When the "show_market_dropdown_menu" setting is disabled, the market header menu will navigate directly to the default exchange page
// The default_exchange.trading_pair is used to determine the last market price when the market_price value is set to AVERAGE // The default_exchange.trading_pair is used to determine the last market price when the market_price value is set to AVERAGE