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)}

-
`; @@ -86,17 +91,12 @@ async function updateBlocks(depth, startFrom=0, clear=true) { } } -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}`; -} - async function showTransactions(blockN) { - let blockTxns = document.getElementById(`block-${blockN}-txns`); + let blockTxns = document.getElementById('txn-details'); let blockId = blocks[blockN]; let updBlock = await provider.getBlockWithTransactions(blockId); + // Clear the transactions blockTxns.innerHTML = ''; for(let i = 0; i < updBlock.transactions.length; i++) { @@ -110,17 +110,20 @@ async function showTransactions(blockN) { To: ${shortenAddress(tx.to)}

`; } else { + // If there's data or address is 0x0, it's a contract interaction/creation content = ` -

${ethers.utils.formatEther(tx.value)} ETH. Contract..

+

${ethers.utils.formatEther(tx.value)} ETH. Contract.

From: ${shortenAddress(tx.from)}. To: ${shortenAddress(tx.to)}

`; } blockTxns.innerHTML += ` -
  • - ${content} -
  • +
    +
    + ${content} +
    +
    `; } } \ No newline at end of file diff --git a/index.html b/index.html index 07a064a..b0bda7d 100644 --- a/index.html +++ b/index.html @@ -4,8 +4,6 @@ Ethereum Block Explorer - @@ -15,15 +13,22 @@
    You must to be able to continue!
    -
    -

    No blocks here yet...

    +
    +
    +

    No blocks here yet...

    +
    +
    +

    Transactions will show here...

    +
    - + + + \ No newline at end of file