ui: Fix BCH rate/market calculation and filter bids/offers.

This commit is contained in:
gerlofvanek 2024-11-15 17:52:00 +01:00 committed by tecnovert
parent 3b86985ae3
commit 2983238ef5

View file

@ -6,7 +6,7 @@ const coinNameToSymbol = {
'Particl Blind': 'particl', 'Particl Blind': 'particl',
'Particl Anon': 'particl', 'Particl Anon': 'particl',
'Monero': 'monero', 'Monero': 'monero',
'Wownero': 'wownero', 'Wownero': 'wownero',
'Litecoin': 'litecoin', 'Litecoin': 'litecoin',
'Firo': 'firo', 'Firo': 'firo',
'Zcoin': 'firo', 'Zcoin': 'firo',
@ -15,7 +15,7 @@ const coinNameToSymbol = {
'Decred': 'decred', 'Decred': 'decred',
'Zano': 'zano', 'Zano': 'zano',
'Dogecoin': 'dogecoin', 'Dogecoin': 'dogecoin',
'Bitcoin Cash': 'bitcoincash' 'Bitcoin Cash': 'bitcoin-cash'
}; };
function makePostRequest(url, headers = {}) { function makePostRequest(url, headers = {}) {
@ -97,7 +97,7 @@ async function fetchLatestPrices() {
} }
console.log('Fetching latest prices...'); console.log('Fetching latest prices...');
const url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,dash,dogecoin,decred,litecoin,particl,pivx,monero,zano,wownero,zcoin&vs_currencies=USD,BTC'; const url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,bitcoin-cash,dash,dogecoin,decred,litecoin,particl,pivx,monero,zano,wownero,zcoin&vs_currencies=USD,BTC';
try { try {
const data = await makePostRequest(url); const data = await makePostRequest(url);
@ -325,11 +325,20 @@ function getNoOffersMessage() {
window.tableRateModule = { window.tableRateModule = {
coinNameToSymbol: { coinNameToSymbol: {
'Bitcoin': 'BTC', 'Particl': 'PART', 'Particl Blind': 'PART', 'Bitcoin': 'BTC',
'Particl Anon': 'PART', 'Monero': 'XMR', 'Wownero': 'WOW', 'Particl': 'PART',
'Litecoin': 'LTC', 'Firo': 'FIRO', 'Dash': 'DASH', 'Particl Blind': 'PART',
'PIVX': 'PIVX', 'Decred': 'DCR', 'Zano': 'ZANO', 'Particl Anon': 'PART',
'Bitcoin Cash': 'BCH' 'Monero': 'XMR',
'Wownero': 'WOW',
'Litecoin': 'LTC',
'Firo': 'FIRO',
'Dash': 'DASH',
'PIVX': 'PIVX',
'Decred': 'DCR',
'Zano': 'ZANO',
'Bitcoin Cash': 'BCH',
'Dogecoin': 'DOGE'
}, },
cache: {}, cache: {},
@ -667,21 +676,23 @@ function filterAndSortData() {
const isFiroOrZcoin = (coin) => ['firo', 'zcoin'].includes(coin.toLowerCase()); const isFiroOrZcoin = (coin) => ['firo', 'zcoin'].includes(coin.toLowerCase());
const isParticlVariant = (coin) => ['particl', 'particl anon', 'particl blind'].includes(coin.toLowerCase()); const isParticlVariant = (coin) => ['particl', 'particl anon', 'particl blind'].includes(coin.toLowerCase());
const coinMatches = (offerCoin, filterCoin) => { const coinMatches = (offerCoin, filterCoin) => {
offerCoin = offerCoin.toLowerCase(); offerCoin = offerCoin.toLowerCase();
filterCoin = filterCoin.toLowerCase(); filterCoin = filterCoin.toLowerCase();
if (offerCoin === filterCoin) return true; if (offerCoin === filterCoin) return true;
if (isFiroOrZcoin(offerCoin) && isFiroOrZcoin(filterCoin)) return true; if (isFiroOrZcoin(offerCoin) && isFiroOrZcoin(filterCoin)) return true;
if (filterCoin === 'bitcoincash' && offerCoin === 'bitcoin cash') return true;
if (isParticlVariant(filterCoin)) { if (filterCoin === 'bitcoin cash' && offerCoin === 'bitcoincash') return true;
return offerCoin === filterCoin;
} if (isParticlVariant(filterCoin)) {
return offerCoin === filterCoin;
if (filterCoin === 'particl' && isParticlVariant(offerCoin)) return true; }
return false; if (filterCoin === 'particl' && isParticlVariant(offerCoin)) return true;
};
return false;
};
originalJsonData.forEach(offer => { originalJsonData.forEach(offer => {
const coinFrom = (offer.coin_from || ''); const coinFrom = (offer.coin_from || '');
@ -1026,6 +1037,9 @@ async function calculateProfitLoss(fromCoin, toCoin, fromAmount, toAmount, isOwn
if (lowerCoin === 'firo' || lowerCoin === 'zcoin') { if (lowerCoin === 'firo' || lowerCoin === 'zcoin') {
return 'zcoin'; return 'zcoin';
} }
if (lowerCoin === 'bitcoin cash') {
return 'bitcoin-cash';
}
return coinNameToSymbol[coin] || lowerCoin; return coinNameToSymbol[coin] || lowerCoin;
}; };
@ -1045,17 +1059,15 @@ async function calculateProfitLoss(fromCoin, toCoin, fromAmount, toAmount, isOwn
let percentDiff; let percentDiff;
if (isOwnOffer) { if (isOwnOffer) {
// For sent offers or own offers, always calculate as if selling
percentDiff = ((toValueUSD / fromValueUSD) - 1) * 100; percentDiff = ((toValueUSD / fromValueUSD) - 1) * 100;
} else { } else {
// For received offers, calculate savings percentage
percentDiff = ((fromValueUSD / toValueUSD) - 1) * 100; percentDiff = ((fromValueUSD / toValueUSD) - 1) * 100;
} }
console.log(`Percent difference: ${percentDiff.toFixed(2)}%`); console.log(`Percent difference: ${percentDiff.toFixed(2)}%`);
return percentDiff; return percentDiff;
} }
function getProfitColorClass(percentage) { function getProfitColorClass(percentage) {
const numericPercentage = parseFloat(percentage); const numericPercentage = parseFloat(percentage);
if (numericPercentage > 0) return 'text-green-500'; if (numericPercentage > 0) return 'text-green-500';
@ -1065,36 +1077,39 @@ function getProfitColorClass(percentage) {
} }
function getMarketRate(fromCoin, toCoin) { function getMarketRate(fromCoin, toCoin) {
return new Promise((resolve) => { return new Promise((resolve) => {
console.log(`Attempting to get market rate for ${fromCoin} to ${toCoin}`); console.log(`Attempting to get market rate for ${fromCoin} to ${toCoin}`);
if (!latestPrices) { if (!latestPrices) {
console.warn('Latest prices object is not available'); console.warn('Latest prices object is not available');
resolve(null); resolve(null);
return; return;
} }
const getPriceKey = (coin) => { const getPriceKey = (coin) => {
const lowerCoin = coin.toLowerCase(); const lowerCoin = coin.toLowerCase();
if (lowerCoin === 'firo' || lowerCoin === 'zcoin') { if (lowerCoin === 'firo' || lowerCoin === 'zcoin') {
return 'zcoin'; return 'zcoin';
} }
return coinNameToSymbol[coin] || lowerCoin; if (lowerCoin === 'bitcoin cash') {
}; return 'bitcoin-cash';
}
return coinNameToSymbol[coin] || lowerCoin;
};
const fromSymbol = getPriceKey(fromCoin); const fromSymbol = getPriceKey(fromCoin);
const toSymbol = getPriceKey(toCoin); const toSymbol = getPriceKey(toCoin);
const fromPrice = latestPrices[fromSymbol]?.usd; const fromPrice = latestPrices[fromSymbol]?.usd;
const toPrice = latestPrices[toSymbol]?.usd; const toPrice = latestPrices[toSymbol]?.usd;
if (!fromPrice || !toPrice) { if (!fromPrice || !toPrice) {
console.warn(`Missing price data for ${!fromPrice ? fromCoin : toCoin}`); console.warn(`Missing price data for ${!fromPrice ? fromCoin : toCoin}`);
resolve(null); resolve(null);
return; return;
} }
const rate = toPrice / fromPrice; const rate = toPrice / fromPrice;
console.log(`Market rate calculated: ${rate} ${toCoin}/${fromCoin}`); console.log(`Market rate calculated: ${rate} ${toCoin}/${fromCoin}`);
resolve(rate); resolve(rate);
}); });
} }
function getTimerColor(offer) { function getTimerColor(offer) {
@ -1218,7 +1233,13 @@ function createRateColumn(offer, coinFrom, coinTo) {
const getPriceKey = (coin) => { const getPriceKey = (coin) => {
const lowerCoin = coin.toLowerCase(); const lowerCoin = coin.toLowerCase();
return lowerCoin === 'firo' || lowerCoin === 'zcoin' ? 'zcoin' : coinNameToSymbol[coin] || lowerCoin; if (lowerCoin === 'firo' || lowerCoin === 'zcoin') {
return 'zcoin';
}
if (lowerCoin === 'bitcoin cash') {
return 'bitcoin-cash';
}
return coinNameToSymbol[coin] || lowerCoin;
}; };
const fromPriceUSD = latestPrices[getPriceKey(coinFrom)]?.usd || 0; const fromPriceUSD = latestPrices[getPriceKey(coinFrom)]?.usd || 0;
@ -1226,11 +1247,11 @@ function createRateColumn(offer, coinFrom, coinTo) {
const rateInUSD = rate * toPriceUSD; const rateInUSD = rate * toPriceUSD;
// Add logging for debugging
console.log(`Rate calculation for ${fromSymbol} to ${toSymbol}:`); console.log(`Rate calculation for ${fromSymbol} to ${toSymbol}:`);
console.log(`Using price keys: ${getPriceKey(coinFrom)}, ${getPriceKey(coinTo)}`);
console.log(`Got prices: $${fromPriceUSD}, $${toPriceUSD}`);
console.log(`Rate: ${rate} ${toSymbol}/${fromSymbol}`); console.log(`Rate: ${rate} ${toSymbol}/${fromSymbol}`);
console.log(`Inverse Rate: ${inverseRate} ${fromSymbol}/${toSymbol}`);
console.log(`${fromSymbol} price: $${fromPriceUSD}`);
console.log(`${toSymbol} price: $${toPriceUSD}`);
console.log(`Rate in USD: $${rateInUSD.toFixed(2)}`); console.log(`Rate in USD: $${rateInUSD.toFixed(2)}`);
return ` return `
@ -1383,6 +1404,9 @@ function updateTooltipTargets(row, uniqueId) {
function getCoinSymbolLowercase(coin) { function getCoinSymbolLowercase(coin) {
if (typeof coin === 'string') { if (typeof coin === 'string') {
if (coin.toLowerCase() === 'bitcoin cash') {
return 'bitcoin-cash';
}
return (coinNameToSymbol[coin] || coin).toLowerCase(); return (coinNameToSymbol[coin] || coin).toLowerCase();
} else if (coin && typeof coin === 'object' && coin.symbol) { } else if (coin && typeof coin === 'object' && coin.symbol) {
return coin.symbol.toLowerCase(); return coin.symbol.toLowerCase();
@ -1471,7 +1495,13 @@ function createCombinedRateTooltip(offer, coinFrom, coinTo, isSentOffers, treatA
const getPriceKey = (coin) => { const getPriceKey = (coin) => {
const lowerCoin = coin.toLowerCase(); const lowerCoin = coin.toLowerCase();
return lowerCoin === 'firo' || lowerCoin === 'zcoin' ? 'zcoin' : getCoinSymbolLowercase(coin); if (lowerCoin === 'firo' || lowerCoin === 'zcoin') {
return 'zcoin';
}
if (lowerCoin === 'bitcoin cash') {
return 'bitcoin-cash';
}
return coinNameToSymbol[coin] || lowerCoin;
}; };
const fromSymbol = getPriceKey(coinFrom); const fromSymbol = getPriceKey(coinFrom);
@ -1486,7 +1516,7 @@ function createCombinedRateTooltip(offer, coinFrom, coinTo, isSentOffers, treatA
const percentDiff = ((rate - marketRate) / marketRate) * 100; const percentDiff = ((rate - marketRate) / marketRate) * 100;
const formattedPercentDiff = percentDiff.toFixed(2); const formattedPercentDiff = percentDiff.toFixed(2);
const percentDiffDisplay = formattedPercentDiff === "0.00" ? "0.00" : const percentDiffDisplay = formattedPercentDiff === "0.00" ? "0.00" :
(percentDiff > 0 ? `+${formattedPercentDiff}` : formattedPercentDiff); (percentDiff > 0 ? `+${formattedPercentDiff}` : formattedPercentDiff);
const aboveOrBelow = percentDiff > 0 ? "above" : percentDiff < 0 ? "below" : "at"; const aboveOrBelow = percentDiff > 0 ? "above" : percentDiff < 0 ? "below" : "at";
const action = isSentOffers || treatAsSentOffer ? "selling" : "buying"; const action = isSentOffers || treatAsSentOffer ? "selling" : "buying";