diff --git a/lib/apis/coingecko.js b/lib/apis/coingecko.js index 5666170..9cb01f4 100644 --- a/lib/apis/coingecko.js +++ b/lib/apis/coingecko.js @@ -1,23 +1,27 @@ 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) { +function get_coin_list(api_key, cb) { + 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) return cb(error, []); 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 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) { +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}`}` + (api_key == null || api_key == '' ? '' : '&x_cg_demo_api_key=' + api_key), 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 if (body['status'] != null && body['status']['error_message'] != null && body['status']['error_message'] != '') + return cb(body['status']['error_message'], 0, 0); else { try { if (market_array != null) { @@ -59,21 +63,21 @@ function get_simple_price(id, currency, market_array, cb) { } module.exports = { - get_coin_data: function (cb) { - get_coin_list(function (err, coin_list) { + get_coin_data: function (api_key, cb) { + get_coin_list(api_key, 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) { + get_market_prices: function (id, currency, api_key, cb) { + get_simple_price(id, currency, null, api_key, 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) { + get_avg_market_prices: function (id, currency, market_array, api_key, cb) { + get_simple_price(id, currency, market_array, api_key, function (err, last_price, last_usd) { if (last_price == null) console.log(`Error: "${currency}" is not a valid coingecko api currency`); diff --git a/lib/settings.js b/lib/settings.js index 031db2b..5f3f404 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -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. // 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_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 // 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 diff --git a/scripts/sync.js b/scripts/sync.js index 293e64d..a668417 100644 --- a/scripts/sync.js +++ b/scripts/sync.js @@ -1026,7 +1026,7 @@ function get_market_price(market_array) { console.log('Calculating market price.. Please wait..'); // 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 if (err == null) { // get current stats @@ -1050,7 +1050,7 @@ function get_market_price(market_array) { }); } else { // coingecko api returned an error - console.log(err); + console.log(`Error: ${err}`); exit(1); } }); @@ -1091,7 +1091,7 @@ function get_market_price(market_array) { const currency = lib.get_market_currency_code(); // 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 if (mkt_err == null) { // update the last usd price @@ -1108,7 +1108,7 @@ function get_market_price(market_array) { }); } else { // coingecko api returned an error - console.log(mkt_err); + console.log(`Error: ${mkt_err}`); exit(1); } }); @@ -1118,7 +1118,7 @@ function get_market_price(market_array) { } } else { // coingecko api returned an error - console.log(coin_err); + console.log(`Error: ${coin_err}`); exit(1); } }); @@ -1173,7 +1173,7 @@ function coingecko_coin_list_api(market_symbols, cb) { const coingecko = require('../lib/apis/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 if (err == null) { // 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 { // failed to get the coingecko api list - console.log(err); + console.log(`Error: ${err}`); return cb(''); } }); diff --git a/settings.json.template b/settings.json.template index 0997a25..bc5309a 100644 --- a/settings.json.template +++ b/settings.json.template @@ -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. // 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_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 // 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