diff --git a/app.js b/app.js index 41091e0..2c87d17 100644 --- a/app.js +++ b/app.js @@ -14,6 +14,11 @@ let blocks = []; let provider; let signer; +function shortenAddress(address, chars = 4) { + const start = address.slice(0, chars + 2); // Keep the '0x' prefix and the first few characters + const end = address.slice(-chars); // Keep the last few characters + return `${start}...${end}`; +} document.getElementById('connect-button').addEventListener('click', async () => { if (typeof window.ethereum !== 'undefined') { @@ -34,7 +39,7 @@ document.getElementById('connect-button').addEventListener('click', async () => // Hide the connect button after successful connection document.getElementById('connect-alert').style.display = 'none'; - appendAlert(`Succefully connected to Metamask account ${signer.getAddress()}!`, 'success'); + appendAlert(`Succefully connected to Metamask account ${await shortenAddress(signer.getAddress())}!`, 'success'); } catch (error) { console.error('Error connecting to MetaMask:', error); appendAlert('An internal error ocurred...', 'danger'); @@ -65,17 +70,17 @@ async function updateBlocks(depth, startFrom=0, clear=true) { let block = await provider.getBlock(latestBlockNumber - i); let blockN = blocks.length; blocks.push(block.number); - + + let date = new Date(block.timestamp * 1000); + // Display block blockDetailsDiv.innerHTML += `
${block.transactions.length} txns. Hash: ${block.hash}.
-By: ${block.miner}. At ${new Date(block.timestamp * 1000).toLocaleString()} +
${block.transactions.length} txns. Hash: ${shortenAddress(block.hash)}.
+By: ${shortenAddress(block.miner)}. At ${formatDistanceToNow(date)}
${ethers.utils.formatEther(tx.value)} ETH. Contract..
+${ethers.utils.formatEther(tx.value)} ETH. Contract.
From: ${shortenAddress(tx.from)}. To: ${shortenAddress(tx.to)}
`; } blockTxns.innerHTML += ` -