DeFi Protocol Dashboard

DeFi Protocol Dashboard

Overall DeFi Portfolio Summary

Your DeFi Positions

Protocol Asset Invested Amount ($) APY/APR (%) Investment Date Estimated Daily Yield ($) Estimated Annual Yield ($) Actions

${totalEstimatedAnnualYield.toLocaleString('en-US', { style: 'currency', currency: 'USD' })}

`; portfolioSummaryDiv.innerHTML = summaryHtml; } /** * Renders the table of all DeFi positions. */ function renderDeFiPositionsTable() { if (!defiPositionsTableBody) return; // Null check defiPositionsTableBody.innerHTML = ''; // Clear existing rows // Sort by protocol name for consistent display const sortedPositions = [...defiPositions].sort((a, b) => a.protocol.localeCompare(b.protocol)); sortedPositions.forEach(pos => { const dailyYield = calculateDailyYield(pos.investedAmount, pos.apyApr); const annualYield = calculateAnnualYield(pos.investedAmount, pos.apyApr); const row = defiPositionsTableBody.insertRow(); row.innerHTML = ` ${pos.protocol} ${pos.asset} ${pos.investedAmount.toLocaleString('en-US', { style: 'currency', currency: 'USD' })} ${pos.apyApr.toFixed(2)}% ${pos.investmentDate || 'N/A'} ${dailyYield.toLocaleString('en-US', { style: 'currency', currency: 'USD' })} ${annualYield.toLocaleString('en-US', { style: 'currency', currency: 'USD' })} `; }); } /** * Renders the entire dashboard content. */ function renderDashboard() { renderPortfolioSummary(); renderDeFiPositionsTable(); } // --- Data Configuration Logic --- /** * Generates a unique ID for a new position. * @returns {string} A unique ID. */ function generateUniqueId() { return 'pos' + Date.now() + Math.floor(Math.random() * 1000); } // Add/Update Position Form Submission if (addUpdatePositionForm) { addUpdatePositionForm.addEventListener('submit', function(event) { event.preventDefault(); // Prevent default form submission const id = document.getElementById('positionId').value; const protocolName = document.getElementById('protocolName').value; const asset = document.getElementById('asset').value; const investedAmount = parseFloat(document.getElementById('investedAmount').value); const apyApr = parseFloat(document.getElementById('apyApr').value); const investmentDate = document.getElementById('investmentDate').value; const newPosition = { id: id || generateUniqueId(), // Use existing ID if editing, otherwise generate new protocol: protocolName, asset: asset, investedAmount: investedAmount, apyApr: apyApr, investmentDate: investmentDate }; if (id) { // Update existing position const index = defiPositions.findIndex(pos => pos.id === id); if (index > -1) { defiPositions[index] = newPosition; } else { console.error('Position not found for update:', id); } } else { // Add new position defiPositions.push(newPosition); } renderDashboard(); // Re-render dashboard with new/updated data addUpdatePositionForm.reset(); // Clear the form positionIdInput.value = ''; // Clear hidden ID formSubmitBtn.textContent = 'Add Position'; // Reset button text switchTab('dashboard'); // Switch back to dashboard to see changes }); } /** * Populates the form for editing an existing position. * @param {string} id - The ID of the position to edit. */ window.editPosition = function(id) { const positionToEdit = defiPositions.find(pos => pos.id === id); if (!positionToEdit) { console.error('Position not found for editing:', id); return; } document.getElementById('positionId').value = positionToEdit.id; document.getElementById('protocolName').value = positionToEdit.protocol; document.getElementById('asset').value = positionToEdit.asset; document.getElementById('investedAmount').value = positionToEdit.investedAmount; document.getElementById('apyApr').value = positionToEdit.apyApr; document.getElementById('investmentDate').value = positionToEdit.investmentDate; formSubmitBtn.textContent = 'Update Position'; // Change button text switchTab('data-config'); // Switch to data config tab }; /** * Deletes a position from the array after confirmation. * @param {string} id - The ID of the position to delete. */ window.deletePosition = function(id) { showCustomModal('Confirm Deletion', 'Are you sure you want to delete this DeFi position?', () => { defiPositions = defiPositions.filter(pos => pos.id !== id); renderDashboard(); // Re-render dashboard }); }; // --- Custom Modal Logic (replaces alert/confirm) --- let currentConfirmCallback = null; /** * Shows a custom modal with a message and optional confirmation. * @param {string} title - The title of the modal. * @param {string} message - The message to display. * @param {function} [onConfirm] - Callback function to execute on confirm. If null, it's an alert. */ function showCustomModal(title, message, onConfirm = null) { if (!confirmModal || !modalTitle || !modalMessage || !confirmActionBtn) return; modalTitle.textContent = title; modalMessage.textContent = message; if (onConfirm) { confirmActionBtn.style.display = 'inline-block'; // Show confirm button confirmActionBtn.textContent = 'Confirm'; confirmActionBtn.classList.remove('btn-primary'); confirmActionBtn.classList.add('btn-danger'); currentConfirmCallback = onConfirm; } else { confirmActionBtn.style.display = 'none'; // Hide confirm button for alerts currentConfirmCallback = null; } confirmModal.style.display = 'flex'; // Show modal } /** * Closes the specified modal. * @param {string} modalId - The ID of the modal to close. */ window.closeModal = function(modalId) { const modal = document.getElementById(modalId); if (modal) { modal.style.display = 'none'; } }; // Event listener for the custom modal's confirm button if (confirmActionBtn) { confirmActionBtn.addEventListener('click', function() { if (currentConfirmCallback) { currentConfirmCallback(); } closeModal('confirmModal'); }); } // Close modal when clicking outside of it window.onclick = function(event) { if (event.target === confirmModal) { closeModal('confirmModal'); } }; // --- PDF Download Logic --- /** * Downloads the dashboard content as a PDF. */ if (downloadPdfBtn) { downloadPdfBtn.addEventListener('click', function() { const input = document.getElementById('dashboardTabContent'); // Target the dashboard content if (!input) { console.error('Dashboard content element not found for PDF generation.'); return; } // Temporarily hide elements that should not be in the PDF const elementsToHide = input.querySelectorAll('.no-print'); elementsToHide.forEach(el => el.style.display = 'none'); // Use html2canvas to capture the content as an image html2canvas(input, { scale: 2, // Increase scale for better quality useCORS: true, // Needed if external images are used (not in this case, but good practice) logging: false // Disable logging for cleaner console }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const pdf = new window.jspdf.jsPDF('p', 'mm', 'a4'); // 'p' for portrait, 'mm' for units, 'a4' for size const imgWidth = 210; // A4 width in mm const pageHeight = 297; // A4 height in mm const imgHeight = canvas.height * imgWidth / canvas.width; let heightLeft = imgHeight; let position = 0; // Add image to PDF pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight); heightLeft -= pageHeight; // If content spans multiple pages, add new pages while (heightLeft >= 0) { position = heightLeft - imgHeight; pdf.addPage(); pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight); heightLeft -= pageHeight; } pdf.save('DeFi_Protocol_Dashboard.pdf'); // Restore visibility of hidden elements elementsToHide.forEach(el => el.style.display = ''); }).catch(error => { console.error('Error generating PDF:', error); // Restore visibility of hidden elements in case of error elementsToHide.forEach(el => el.style.display = ''); }); }); } // Initial render of the dashboard when the page loads renderDashboard(); });
Scroll to Top