From f40d98ef238166a81a5622e6537c676b0d96b686 Mon Sep 17 00:00:00 2001 From: gerlofvanek Date: Thu, 16 Jan 2025 13:08:52 +0100 Subject: [PATCH] Fix API issue with Firo and various small fixes. --- basicswap/static/js/offerstable.js | 44 ++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/basicswap/static/js/offerstable.js b/basicswap/static/js/offerstable.js index 66b14f8..b0ad0b7 100644 --- a/basicswap/static/js/offerstable.js +++ b/basicswap/static/js/offerstable.js @@ -1188,7 +1188,18 @@ async function fetchPricesWithApiKey(baseUrl) { } const data = await response.json(); + console.log('Received price data:', data); + if (data && !data.Error && Object.keys(data).length > 0) { + const hasValidPrices = Object.values(data).every(coin => + coin && typeof coin.usd === 'number' && !isNaN(coin.usd) + ); + + if (!hasValidPrices) { + //console.warn('Received invalid price data structure'); + return null; + } + return data; } console.log('API key fetch returned invalid data'); @@ -1247,11 +1258,11 @@ function getEmptyPriceData() { async function fetchLatestPrices() { const PRICES_CACHE_KEY = 'prices_coingecko'; - const RETRY_DELAY = 5000; + const RETRY_DELAY = 100000; const MAX_RETRIES = 3; const cachedData = CacheManager.get(PRICES_CACHE_KEY); - if (cachedData && cachedData.remainingTime > 60000) { + if (cachedData && cachedData.remainingTime > 80000) { console.log('Using cached price data (valid for next minute)'); latestPrices = cachedData.value; return cachedData.value; @@ -1480,7 +1491,7 @@ function updateProfitLoss(row, fromCoin, toCoin, fromAmount, toAmount, isOwnOffe .then(percentDiff => { if (percentDiff === null) { profitLossElement.textContent = 'N/A'; - profitLossElement.className = 'profit-loss text-lg font-bold text-gray-400'; + profitLossElement.className = 'profit-loss text-lg font-bold text-gray-300'; return; } @@ -1619,7 +1630,7 @@ async function updateOffersTable() { const PRICES_CACHE_KEY = 'prices_coingecko'; const cachedPrices = CacheManager.get(PRICES_CACHE_KEY); - if (!cachedPrices || !cachedPrices.remainingTime || cachedPrices.remainingTime < 60000) { + if (!cachedPrices || !cachedPrices.remainingTime || cachedPrices.remainingTime < 80000) { console.log('Fetching fresh price data...'); const priceData = await fetchLatestPrices(); if (priceData) { @@ -1791,7 +1802,6 @@ function createTableRow(offer, identity = null) { const fromAmount = parseFloat(amountFrom) || 0; const toAmount = parseFloat(amountTo) || 0; - // Build row content row.innerHTML = ` ${!isPublic ? createPrivateIndicator() : ''} ${createTimeColumn(offer, postedTime, expiresIn)} @@ -1887,7 +1897,7 @@ function createDetailsColumn(offer, identity = null) { ); const identifierTextClass = identityInfo.label - ? 'text-white dark:text-white' + ? 'dark:text-white' : 'monospace'; return ` @@ -1900,7 +1910,7 @@ function createDetailsColumn(offer, identity = null) { - + ${escapeHtml(displayIdentifier)} @@ -2333,9 +2343,23 @@ function createTooltipContent(isSentOffers, coinFrom, coinTo, fromAmount, toAmou function createCombinedRateTooltip(offer, coinFrom, coinTo, treatAsSentOffer) { const rate = parseFloat(offer.rate) || 0; const inverseRate = rate ? (1 / rate) : 0; - const fromSymbol = getCoinSymbolLowercase(coinFrom); - const toSymbol = getCoinSymbolLowercase(coinTo); + const getPriceKey = (coin) => { + const lowerCoin = coin.toLowerCase(); + if (lowerCoin === 'firo' || lowerCoin === 'zcoin') { + return 'zcoin'; + } + if (lowerCoin === 'bitcoin cash') { + return 'bitcoin-cash'; + } + if (lowerCoin === 'particl anon' || lowerCoin === 'particl blind') { + return 'particl'; + } + return coinNameToSymbol[coin] || lowerCoin; + }; + + const fromSymbol = getPriceKey(coinFrom); + const toSymbol = getPriceKey(coinTo); const fromPriceUSD = latestPrices[fromSymbol]?.usd; const toPriceUSD = latestPrices[toSymbol]?.usd; @@ -2354,13 +2378,11 @@ function createCombinedRateTooltip(offer, coinFrom, coinTo, treatAsSentOffer) { const rateInUSD = rate * toPriceUSD; const marketRate = fromPriceUSD / toPriceUSD; - const percentDiff = marketRate ? ((rate - marketRate) / marketRate) * 100 : 0; const formattedPercentDiff = percentDiff.toFixed(2); const percentDiffDisplay = formattedPercentDiff === "0.00" ? "0.00" : (percentDiff > 0 ? `+${formattedPercentDiff}` : formattedPercentDiff); const aboveOrBelow = percentDiff > 0 ? "above" : percentDiff < 0 ? "below" : "at"; - const action = treatAsSentOffer ? "selling" : "buying"; return `