Fix market sync crash for invalid default pair
-The market sync could crash when calculating the USD price if the base pair of the markets_page.default_exchange.trading_pair value was not found in coingecko's vs_currencies list from https://api.coingecko.com/api/v3/simple/supported_vs_currencies (for example, using USDT as the default base pair). A different calculation is now used in this case which does not require any additional api credits to be used.
This commit is contained in:
+30
-2
@@ -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++;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user