Fix pricechart if no price/historical data available.

This commit is contained in:
gerlofvanek
2025-10-10 12:26:36 +02:00
parent 73d486d6f0
commit eb46a4fcc5
2 changed files with 108 additions and 50 deletions

View File

@@ -12592,6 +12592,7 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
return return_data
if rate_source == "coingecko.com":
try:
coin_ids: str = ""
for coin_id in coins_list:
if len(coin_ids) > 0:
@@ -12620,9 +12621,13 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
price_change_24h = v.get("usd_24h_change")
# Convert to float if value exists, otherwise keep as None
volume_value = float(volume_24h) if volume_24h is not None else None
volume_value = (
float(volume_24h) if volume_24h is not None else None
)
price_change_value = (
float(price_change_24h) if price_change_24h is not None else 0.0
float(price_change_24h)
if price_change_24h is not None
else 0.0
)
return_data[coin_id] = {
@@ -12633,6 +12638,13 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
"volume_24h": volume_value,
"price_change_24h": price_change_value,
}
except Exception as e:
self.log.warning(f"Could not fetch volume data: {e}")
for coin_id in need_coins:
return_data[coin_id] = {
"volume_24h": None,
"price_change_24h": 0.0,
}
else:
raise ValueError(f"Unknown rate source {rate_source}")
@@ -12723,6 +12735,7 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
)
for coin_id in need_coins:
try:
exchange_name: str = self.getExchangeName(coin_id, rate_source)
url: str = (
f"https://api.coingecko.com/api/v3/coins/{exchange_name}/market_chart?vs_currency=usd&days={days}"
@@ -12735,6 +12748,11 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
if "prices" in js:
return_data[coin_id] = js["prices"]
new_values[coin_id] = js["prices"]
except Exception as e:
self.log.warning(
f"Could not fetch historical data for {Coins(coin_id).name}: {e}"
)
return_data[coin_id] = []
else:
raise ValueError(f"Unknown rate source {rate_source}")

View File

@@ -866,8 +866,11 @@ destroyChart: function() {
const allData = await api.fetchHistoricalDataXHR([coinSymbol]);
data = allData[coinSymbol];
if (!data || Object.keys(data).length === 0) {
throw new Error(`No data returned for ${coinSymbol}`);
if (!data || (Array.isArray(data) && data.length === 0) || Object.keys(data).length === 0) {
console.warn(`No price data available for ${coinSymbol}`);
chartModule.hideChartLoader();
chartModule.showNoDataMessage(coinSymbol);
return;
}
CacheManager.set(cacheKey, data, 'chart');
@@ -897,6 +900,8 @@ destroyChart: function() {
chartModule.initChart();
}
chartModule.hideNoDataMessage();
const chartData = chartModule.prepareChartData(coinSymbol, data);
if (chartData.length > 0 && chartModule.chart) {
chartModule.chart.data.datasets[0].data = chartData;
@@ -951,6 +956,41 @@ destroyChart: function() {
chart.classList.remove('hidden');
},
showNoDataMessage: function(coinSymbol) {
const chartCanvas = document.getElementById('coin-chart');
if (!chartCanvas) {
return;
}
if (this.chart) {
this.chart.data.datasets[0].data = [];
this.chart.update('none');
}
let messageDiv = document.getElementById('chart-no-data-message');
if (!messageDiv) {
messageDiv = document.createElement('div');
messageDiv.id = 'chart-no-data-message';
messageDiv.style.cssText = 'position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center; color: #888; font-size: 14px; z-index: 10;';
chartCanvas.parentElement.style.position = 'relative';
chartCanvas.parentElement.appendChild(messageDiv);
}
messageDiv.innerHTML = `
<div style="padding: 20px; background: rgba(0,0,0,0.05); border-radius: 8px;">
<div style="font-size: 16px; margin-bottom: 8px;">No Price Data Available</div>
</div>
`;
messageDiv.classList.remove('hidden');
},
hideNoDataMessage: function() {
const messageDiv = document.getElementById('chart-no-data-message');
if (messageDiv) {
messageDiv.classList.add('hidden');
}
},
cleanup: function() {
if (this.pendingAnimationFrame) {
cancelAnimationFrame(this.pendingAnimationFrame);