From 43fbf39b89ff1622de28f0b4f8e14757ca9f205a Mon Sep 17 00:00:00 2001 From: Wizzard Date: Thu, 14 Mar 2024 01:37:43 -0400 Subject: [PATCH] Price change fix up --- main.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index a4266ce..84570a9 100644 --- a/main.js +++ b/main.js @@ -106,17 +106,22 @@ function createStatsEmbed(tokenData = {}) { const priceChange24hrFormatted = tokenData.priceChange24hr ? formatPercentage(tokenData.priceChange24hr) : 'N/A'; const calculateDollarChange = (price, percentageChange) => { - if (price && percentageChange) { - const dollarChange = (parseFloat(percentageChange) / 100) * parseFloat(price); - return `$${parseFloat(dollarChange).toFixed(8)}`; - } - return 'N/A'; + if (price && percentageChange) { + let dollarChange = (parseFloat(percentageChange) / 100) * parseFloat(price); + if (dollarChange < 0) { + return `-$${Math.abs(parseFloat(dollarChange)).toFixed(8)}`; + } else { + return `$${parseFloat(dollarChange).toFixed(8)}`; + } + } + return 'N/A'; }; const dollarChange1hr = calculateDollarChange(tokenData.price, tokenData.priceChange1hr); const dollarChange24hr = calculateDollarChange(tokenData.price, tokenData.priceChange24hr); + const dollarChange5m = calculateDollarChange(tokenData.price, tokenData.priceChange5m); - const randomColor = Math.floor(Math.random() * 16777215).toString(16); + const randomColor = Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0'); const embed = new EmbedBuilder() .setTitle(`${tokenData.name} $${tokenData.symbol} Token Stats`) @@ -128,7 +133,7 @@ function createStatsEmbed(tokenData = {}) { { name: 'Price (USD)', value: priceFormatted, inline: true }, { name: 'Liquidity (USD)', value: liquidityFormatted, inline: true }, { name: 'Status', value: tokenData.profitStatus ? 'Making Money 📈💰' : 'Losing Money 📉😡', inline: false }, - { name: 'Price Change (5 M)', value: `${priceChange5mFormatted} (${calculateDollarChange(tokenData.price, tokenData.priceChange)})`, inline: true }, + { name: 'Price Change (5 M)', value: `${priceChange5mFormatted} (${dollarChange5m})`, inline: true }, { name: 'Price Change (1 H)', value: `${priceChange1hrFormatted} (${dollarChange1hr})`, inline: true }, { name: 'Price Change (24 H)', value: `${priceChange24hrFormatted} (${dollarChange24hr})`, inline: true } ); @@ -280,7 +285,7 @@ const checkStatusAndUpdateOBS = async () => { price: pair.priceUsd, liquidity: pair.liquidity.usd, profitStatus: pair.priceChange.m5 > 0, - priceChange: pair.priceChange.m5, + priceChange5m: pair.priceChange.m5, priceChange1hr: pair.priceChange.h1, priceChange24hr: pair.priceChange.h24 };