mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-05 18:38:09 +01:00
Fix pricechart if no price/historical data available.
This commit is contained in:
@@ -12592,6 +12592,7 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
|
|||||||
return return_data
|
return return_data
|
||||||
|
|
||||||
if rate_source == "coingecko.com":
|
if rate_source == "coingecko.com":
|
||||||
|
try:
|
||||||
coin_ids: str = ""
|
coin_ids: str = ""
|
||||||
for coin_id in coins_list:
|
for coin_id in coins_list:
|
||||||
if len(coin_ids) > 0:
|
if len(coin_ids) > 0:
|
||||||
@@ -12620,9 +12621,13 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
|
|||||||
price_change_24h = v.get("usd_24h_change")
|
price_change_24h = v.get("usd_24h_change")
|
||||||
|
|
||||||
# Convert to float if value exists, otherwise keep as None
|
# 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 = (
|
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] = {
|
return_data[coin_id] = {
|
||||||
@@ -12633,6 +12638,13 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
|
|||||||
"volume_24h": volume_value,
|
"volume_24h": volume_value,
|
||||||
"price_change_24h": price_change_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:
|
else:
|
||||||
raise ValueError(f"Unknown rate source {rate_source}")
|
raise ValueError(f"Unknown rate source {rate_source}")
|
||||||
|
|
||||||
@@ -12723,6 +12735,7 @@ class BasicSwap(BaseApp, BSXNetwork, UIApp):
|
|||||||
)
|
)
|
||||||
|
|
||||||
for coin_id in need_coins:
|
for coin_id in need_coins:
|
||||||
|
try:
|
||||||
exchange_name: str = self.getExchangeName(coin_id, rate_source)
|
exchange_name: str = self.getExchangeName(coin_id, rate_source)
|
||||||
url: str = (
|
url: str = (
|
||||||
f"https://api.coingecko.com/api/v3/coins/{exchange_name}/market_chart?vs_currency=usd&days={days}"
|
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:
|
if "prices" in js:
|
||||||
return_data[coin_id] = js["prices"]
|
return_data[coin_id] = js["prices"]
|
||||||
new_values[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:
|
else:
|
||||||
raise ValueError(f"Unknown rate source {rate_source}")
|
raise ValueError(f"Unknown rate source {rate_source}")
|
||||||
|
|
||||||
|
|||||||
@@ -866,8 +866,11 @@ destroyChart: function() {
|
|||||||
const allData = await api.fetchHistoricalDataXHR([coinSymbol]);
|
const allData = await api.fetchHistoricalDataXHR([coinSymbol]);
|
||||||
data = allData[coinSymbol];
|
data = allData[coinSymbol];
|
||||||
|
|
||||||
if (!data || Object.keys(data).length === 0) {
|
if (!data || (Array.isArray(data) && data.length === 0) || Object.keys(data).length === 0) {
|
||||||
throw new Error(`No data returned for ${coinSymbol}`);
|
console.warn(`No price data available for ${coinSymbol}`);
|
||||||
|
chartModule.hideChartLoader();
|
||||||
|
chartModule.showNoDataMessage(coinSymbol);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CacheManager.set(cacheKey, data, 'chart');
|
CacheManager.set(cacheKey, data, 'chart');
|
||||||
@@ -897,6 +900,8 @@ destroyChart: function() {
|
|||||||
chartModule.initChart();
|
chartModule.initChart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chartModule.hideNoDataMessage();
|
||||||
|
|
||||||
const chartData = chartModule.prepareChartData(coinSymbol, data);
|
const chartData = chartModule.prepareChartData(coinSymbol, data);
|
||||||
if (chartData.length > 0 && chartModule.chart) {
|
if (chartData.length > 0 && chartModule.chart) {
|
||||||
chartModule.chart.data.datasets[0].data = chartData;
|
chartModule.chart.data.datasets[0].data = chartData;
|
||||||
@@ -951,6 +956,41 @@ destroyChart: function() {
|
|||||||
chart.classList.remove('hidden');
|
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() {
|
cleanup: function() {
|
||||||
if (this.pendingAnimationFrame) {
|
if (this.pendingAnimationFrame) {
|
||||||
cancelAnimationFrame(this.pendingAnimationFrame);
|
cancelAnimationFrame(this.pendingAnimationFrame);
|
||||||
|
|||||||
Reference in New Issue
Block a user