// Calculator functionality for Portfolio page let calculatorPrices = {}; // Popular cryptocurrencies to track const cryptoList = [ { id: 'bitcoin', name: 'Bitcoin', symbol: 'BTC' }, { id: 'ethereum', name: 'Ethereum', symbol: 'ETH' }, { id: 'binancecoin', name: 'BNB', symbol: 'BNB' }, { id: 'cardano', name: 'Cardano', symbol: 'ADA' }, { id: 'solana', name: 'Solana', symbol: 'SOL' }, { id: 'ripple', name: 'XRP', symbol: 'XRP' } ]; // Currency symbols and formatting const currencyConfig = { usd: { symbol: '$', name: 'USD', decimals: 2 }, eur: { symbol: '€', name: 'EUR', decimals: 2 }, gbp: { symbol: '£', name: 'GBP', decimals: 2 }, jpy: { symbol: '¥', name: 'JPY', decimals: 0 } }; // Tab switching functionality function switchCalculatorTab(tabName) { // Update tab buttons const tabButtons = document.querySelectorAll('.calc-tab-btn'); tabButtons.forEach(btn => { if (btn.getAttribute('data-calc') === tabName) { btn.classList.add('active'); } else { btn.classList.remove('active'); } }); // Update panels const panels = document.querySelectorAll('.calculator-panel'); panels.forEach(panel => { if (panel.id === tabName + '-calc') { panel.classList.add('active'); } else { panel.classList.remove('active'); } }); } function handleTabClick(event) { const button = event.target.closest('.calc-tab-btn'); if (button) { const tabName = button.getAttribute('data-calc'); switchCalculatorTab(tabName); } } // Crypto Converter Calculator async function fetchConverterPrices() { try { const response = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,binancecoin,cardano,solana,ripple&vs_currencies=usd,eur,gbp,jpy'); const data = await response.json(); calculatorPrices = data; return data; } catch (error) { console.error('Error fetching converter prices:', error); return {}; } } function convertCrypto() { const fromCrypto = document.getElementById('from-crypto'); const toCurrency = document.getElementById('to-currency'); const fromAmount = document.getElementById('from-amount'); const resultElement = document.getElementById('converted-amount'); if (!fromCrypto || !toCurrency || !fromAmount || !resultElement) { return; } const fromValue = fromCrypto.value; const toValue = toCurrency.value; const amount = parseFloat(fromAmount.value) || 0; if (!calculatorPrices[fromValue] || amount <= 0) { resultElement.textContent = '$0.00'; return; } let result = 0; if (['usd', 'eur', 'gbp', 'jpy'].includes(toValue)) { // Convert to fiat result = amount * calculatorPrices[fromValue][toValue]; const config = currencyConfig[toValue]; resultElement.textContent = `${config.symbol}${result.toLocaleString(undefined, { minimumFractionDigits: config.decimals, maximumFractionDigits: config.decimals })}`; } else { // Convert to another crypto const fromPrice = calculatorPrices[fromValue].usd; const toPrice = calculatorPrices[toValue].usd; result = (amount * fromPrice) / toPrice; const cryptoSymbol = cryptoList.find(c => c.id === toValue)?.symbol || toValue.toUpperCase(); resultElement.textContent = `${result.toLocaleString(undefined, { minimumFractionDigits: 6, maximumFractionDigits: 6 })} ${cryptoSymbol}`; } } function handleConvertClick() { convertCrypto(); } function handleConverterInputChange() { convertCrypto(); } // Profit Calculator function calculateProfit() { const entryPrice = parseFloat(document.getElementById('entry-price')?.value) || 0; const exitPrice = parseFloat(document.getElementById('exit-price')?.value) || 0; const investment = parseFloat(document.getElementById('investment-amount')?.value) || 0; const feePercentage = parseFloat(document.getElementById('trading-fees')?.value) || 0; if (entryPrice <= 0 || investment <= 0) { return; } // Calculate quantities and fees const quantity = investment / entryPrice; const buyFee = investment * (feePercentage / 100); const sellValue = quantity * exitPrice; const sellFee = sellValue * (feePercentage / 100); const totalFees = buyFee + sellFee; // Calculate profit/loss const grossProfit = sellValue - investment; const netProfit = grossProfit - totalFees; const profitPercentage = (netProfit / investment) * 100; // Update display const profitAmountEl = document.getElementById('profit-amount'); const profitPercentageEl = document.getElementById('profit-percentage'); const totalFeesEl = document.getElementById('total-fees'); const netProfitEl = document.getElementById('net-profit'); if (profitAmountEl) profitAmountEl.textContent = `$${grossProfit.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; if (profitPercentageEl) profitPercentageEl.textContent = `${profitPercentage.toFixed(2)}%`; if (totalFeesEl) totalFeesEl.textContent = `$${totalFees.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; if (netProfitEl) netProfitEl.textContent = `$${netProfit.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; // Color coding for profit/loss if (netProfit >= 0) { if (profitAmountEl) profitAmountEl.className = 'font-bold text-green-400'; if (netProfitEl) netProfitEl.className = 'font-bold text-xl text-green-400'; if (profitPercentageEl) profitPercentageEl.className = 'font-bold text-green-400'; } else { if (profitAmountEl) profitAmountEl.className = 'font-bold text-red-400'; if (netProfitEl) netProfitEl.className = 'font-bold text-xl text-red-400'; if (profitPercentageEl) profitPercentageEl.className = 'font-bold text-red-400'; } } function handleProfitCalculateClick() { calculateProfit(); } // Mining Calculator function calculateMining() { const hashRate = parseFloat(document.getElementById('hash-rate')?.value) || 0; // TH/s const powerConsumption = parseFloat(document.getElementById('power-consumption')?.value) || 0; // Watts const electricityCost = parseFloat(document.getElementById('electricity-cost')?.value) || 0; // $/kWh const poolFee = parseFloat(document.getElementById('pool-fee')?.value) || 0; // % if (hashRate <= 0 || powerConsumption <= 0) { return; } // Bitcoin network difficulty and block reward (simplified calculation) const btcPrice = calculatorPrices.bitcoin?.usd || 45000; // Fallback BTC price const networkHashrate = 450000000; // TH/s (approximate) const blockReward = 6.25; // BTC per block const blocksPerDay = 144; // Blocks per day // Daily mining calculations const dailyBtcReward = (hashRate / networkHashrate) * blockReward * blocksPerDay; const dailyRevenueGross = dailyBtcReward * btcPrice; const dailyRevenue = dailyRevenueGross * (1 - poolFee / 100); // Electricity cost calculation const dailyPowerConsumption = (powerConsumption * 24) / 1000; // kWh per day const dailyElectricityCost = dailyPowerConsumption * electricityCost; // Profit calculations const dailyProfit = dailyRevenue - dailyElectricityCost; const monthlyProfit = dailyProfit * 30; // Update display const dailyRevenueEl = document.getElementById('daily-revenue'); const dailyElectricityEl = document.getElementById('daily-electricity'); const dailyProfitEl = document.getElementById('daily-profit'); const monthlyProfitEl = document.getElementById('monthly-profit'); if (dailyRevenueEl) dailyRevenueEl.textContent = `$${dailyRevenue.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; if (dailyElectricityEl) dailyElectricityEl.textContent = `$${dailyElectricityCost.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; if (dailyProfitEl) dailyProfitEl.textContent = `$${dailyProfit.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; if (monthlyProfitEl) monthlyProfitEl.textContent = `$${monthlyProfit.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; // Color coding for profit/loss if (dailyProfit >= 0) { if (dailyProfitEl) dailyProfitEl.className = 'font-bold text-green-400'; if (monthlyProfitEl) monthlyProfitEl.className = 'font-bold text-xl text-green-400'; } else { if (dailyProfitEl) dailyProfitEl.className = 'font-bold text-red-400'; if (monthlyProfitEl) monthlyProfitEl.className = 'font-bold text-xl text-red-400'; } } function handleMiningCalculateClick() { calculateMining(); } // Tax Calculator function calculateTax() { const purchasePrice = parseFloat(document.getElementById('purchase-price')?.value) || 0; const salePrice = parseFloat(document.getElementById('sale-price')?.value) || 0; const holdingPeriod = parseInt(document.getElementById('holding-period')?.value) || 0; const taxBracket = parseFloat(document.getElementById('tax-bracket')?.value) || 22; if (purchasePrice <= 0 || salePrice <= 0) { return; } // Calculate capital gain/loss const capitalGain = salePrice - purchasePrice; // Determine tax rate based on holding period const isLongTerm = holdingPeriod >= 12; let taxRate; if (isLongTerm) { // Long-term capital gains rates (simplified) if (taxBracket <= 10) taxRate = 0; else if (taxBracket <= 22) taxRate = 15; else taxRate = 20; } else { // Short-term capital gains = ordinary income tax rate taxRate = taxBracket; } // Calculate tax owed const taxOwed = Math.max(0, capitalGain * (taxRate / 100)); const afterTaxProfit = capitalGain - taxOwed; // Update display const capitalGainEl = document.getElementById('capital-gain'); const appliedTaxRateEl = document.getElementById('applied-tax-rate'); const taxTypeEl = document.getElementById('tax-type'); const taxOwedEl = document.getElementById('tax-owed'); const afterTaxProfitEl = document.getElementById('after-tax-profit'); if (capitalGainEl) capitalGainEl.textContent = `$${capitalGain.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; if (appliedTaxRateEl) appliedTaxRateEl.textContent = `${taxRate}%`; if (taxTypeEl) taxTypeEl.textContent = isLongTerm ? 'Long-term' : 'Short-term'; if (taxOwedEl) taxOwedEl.textContent = `$${taxOwed.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; if (afterTaxProfitEl) afterTaxProfitEl.textContent = `$${afterTaxProfit.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; // Color coding if (capitalGain >= 0) { if (capitalGainEl) capitalGainEl.className = 'font-bold text-green-400'; } else { if (capitalGainEl) capitalGainEl.className = 'font-bold text-red-400'; } if (afterTaxProfit >= 0) { if (afterTaxProfitEl) afterTaxProfitEl.className = 'font-bold text-xl text-green-400'; } else { if (afterTaxProfitEl) afterTaxProfitEl.className = 'font-bold text-xl text-red-400'; } } function handleTaxCalculateClick() { calculateTax(); } function init() { // Calculator functionality - use specific container for calculators const calculatorTabContainer = document.querySelector('#seffgz .backdrop-blur-xl'); if (calculatorTabContainer) { calculatorTabContainer.addEventListener('click', handleTabClick); } // Crypto converter const convertBtn = document.getElementById('convert-btn'); if (convertBtn) { convertBtn.addEventListener('click', handleConvertClick); } const fromAmountInput = document.getElementById('from-amount'); const fromCryptoSelect = document.getElementById('from-crypto'); const toCurrencySelect = document.getElementById('to-currency'); if (fromAmountInput) { fromAmountInput.addEventListener('input', handleConverterInputChange); } if (fromCryptoSelect) { fromCryptoSelect.addEventListener('change', handleConverterInputChange); } if (toCurrencySelect) { toCurrencySelect.addEventListener('change', handleConverterInputChange); } // Profit calculator const profitCalculateBtn = document.getElementById('calculate-profit-btn'); if (profitCalculateBtn) { profitCalculateBtn.addEventListener('click', handleProfitCalculateClick); } // Mining calculator const miningCalculateBtn = document.getElementById('calculate-mining-btn'); if (miningCalculateBtn) { miningCalculateBtn.addEventListener('click', handleMiningCalculateClick); } // Tax calculator const taxCalculateBtn = document.getElementById('calculate-tax-btn'); if (taxCalculateBtn) { taxCalculateBtn.addEventListener('click', handleTaxCalculateClick); } // Initial setup fetchConverterPrices(); // Set up auto-refresh for converter prices setInterval(() => { fetchConverterPrices(); }, 60000); // Refresh every minute } function teardown() { const calculatorTabContainer = document.querySelector('#seffgz .backdrop-blur-xl'); if (calculatorTabContainer) { calculatorTabContainer.removeEventListener('click', handleTabClick); } const convertBtn = document.getElementById('convert-btn'); if (convertBtn) { convertBtn.removeEventListener('click', handleConvertClick); } const fromAmountInput = document.getElementById('from-amount'); const fromCryptoSelect = document.getElementById('from-crypto'); const toCurrencySelect = document.getElementById('to-currency'); if (fromAmountInput) { fromAmountInput.removeEventListener('input', handleConverterInputChange); } if (fromCryptoSelect) { fromCryptoSelect.removeEventListener('change', handleConverterInputChange); } if (toCurrencySelect) { toCurrencySelect.removeEventListener('change', handleConverterInputChange); } const profitCalculateBtn = document.getElementById('calculate-profit-btn'); if (profitCalculateBtn) { profitCalculateBtn.removeEventListener('click', handleProfitCalculateClick); } const miningCalculateBtn = document.getElementById('calculate-mining-btn'); if (miningCalculateBtn) { miningCalculateBtn.removeEventListener('click', handleMiningCalculateClick); } const taxCalculateBtn = document.getElementById('calculate-tax-btn'); if (taxCalculateBtn) { taxCalculateBtn.removeEventListener('click', handleTaxCalculateClick); } } // Export functions for the website system export { init, teardown };