From cddc77e520e9c4ad0ff38d3112066094bde569b3 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Tue, 26 Mar 2024 11:17:04 -0400 Subject: [PATCH] Fix 1 day timer & handle failure --- main.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index a5d41da..e475ba1 100644 --- a/main.js +++ b/main.js @@ -38,26 +38,29 @@ async function fetchSolanaPrice() { try { const fetch = (await import('node-fetch')).default; const response = await fetch(cryptocompareApiUrl); + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); const data = await response.json(); return parseFloat(data.USD).toFixed(2); } catch (error) { console.error('Error fetching Solana price:', error); + setTimeout(fetchSolanaPriceAndUpdateHistory, 5 * 60 * 1000); return null; } } function pruneOldData() { - const sixHoursInMilliseconds = 6 * 60 * 60 * 1000; const oneDayInMilliseconds = 24 * 60 * 60 * 1000; - const cutoffTime6Hours = Date.now() - sixHoursInMilliseconds; const cutoffTime1Day = Date.now() - oneDayInMilliseconds; priceHistory.prices = priceHistory.prices.filter(pricePoint => pricePoint.time > cutoffTime1Day); - if (priceHistory.prices.length < 360) { + if (priceHistory.prices.length < (sixHoursInMilliseconds / (60 * 1000))) { console.warn("Warning: Not enough data points for accurate 6-hour calculations."); } + if (priceHistory.prices.length < (oneDayInMilliseconds / (60 * 1000))) { + console.warn("Warning: Not enough data points for accurate 1-day calculations."); + } } async function fetchSolanaPriceAndUpdateHistory() { @@ -121,6 +124,8 @@ function immediatePriceCheckAndAnnounce() { const oneDayAgoPrice = parseFloat(findPriceAgo(1440).price); oneDayChange.percent = ((latestPrice - oneDayAgoPrice) / oneDayAgoPrice) * 100; oneDayChange.dollar = latestPrice - oneDayAgoPrice; + } else { + console.log("Insufficient data for 1-day change calculation."); } if (priceHistory.prices.length >= 360) {