diff --git a/lib/apis/coingecko.js b/lib/apis/coingecko.js index 9cb01f4..22539ea 100644 --- a/lib/apis/coingecko.js +++ b/lib/apis/coingecko.js @@ -29,6 +29,19 @@ function get_simple_price(id, currency, market_array, api_key, cb) { let last_price = 0; let last_usd_price = 0; let counter = 0; + let api_market = null; + + // check if the currency variable is set + if (currency != null && currency != '') { + // find the market currency in the market_array + const base_index = market_array.findIndex(p => p.currency.toLowerCase() == currency.toLowerCase()); + + // check if the currency is found in the market_array + if (base_index > -1) { + // find the api market data row + api_market = body[market_array[base_index].coingecko_id.toLowerCase()]; + } + } // loop through all api object keys Object.keys(body).forEach(function(key, index, map) { @@ -36,9 +49,24 @@ function get_simple_price(id, currency, market_array, api_key, cb) { // 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()])); + // determine if the api already returend the market price + if (currency != null && currency != '' && body[key][currency.toLowerCase()] != null) { + // calculate the market price + last_price += (market_array[market_index].last_price * (currency == null || currency == '' ? 0 : body[key][currency.toLowerCase()])); + } else { + // check if the market currency exists in the api data + if (api_market != null) { + // calculate the currency ratio + const ratio = (api_market['usd'] / body[key]['usd']); + + // calculate the market price + last_price += (market_array[market_index].last_price / ratio); + } + } + + // calculate the usd price last_usd_price += (market_array[market_index].last_price * body[key]['usd']); + counter++; } });