import React, { useEffect, useState, useMemo } from "react"; import { useParams, NavLink } from "react-router-dom"; import { ethers, BigNumber } from "ethers"; import { provider } from "./ethersconfig"; import StandardFrame from "./StandardFrame"; import StandardSubtitle from "./StandardSubtitle"; import ContentFrame from "./ContentFrame"; import Timestamp from "./components/Timestamp"; import GasValue from "./components/GasValue"; import BlockLink from "./components/BlockLink"; import AddressLink from "./components/AddressLink"; type BlockParams = { blockNumberOrHash: string; }; interface ExtendedBlock extends ethers.providers.Block { size: number; sha3Uncles: string; stateRoot: string; totalDifficulty: BigNumber; } const Block: React.FC = () => { const params = useParams(); const [block, setBlock] = useState(); useEffect(() => { const readBlock = async () => { let _rawBlock: any; if (ethers.utils.isHexString(params.blockNumberOrHash, 32)) { _rawBlock = await provider.send("eth_getBlockByHash", [ params.blockNumberOrHash, false, ]); } else { _rawBlock = await provider.send("eth_getBlockByNumber", [ params.blockNumberOrHash, false, ]); } const _block = provider.formatter.block(_rawBlock); const extBlock: ExtendedBlock = { size: provider.formatter.number(_rawBlock.size), sha3Uncles: _rawBlock.sha3Uncles, stateRoot: _rawBlock.stateRoot, totalDifficulty: provider.formatter.bigNumber( _rawBlock.totalDifficulty ), ..._block, }; setBlock(extBlock); }; readBlock(); }, [params.blockNumberOrHash]); useEffect(() => { if (block) { document.title = `Block #${block.number} | Otterscan`; } }, [block]); const extraStr = useMemo(() => { try { return block && ethers.utils.toUtf8String(block.extraData); } catch (err) { console.error(err); } }, [block]); return ( Block{" "} #{params.blockNumberOrHash} {block && ( {ethers.utils.commify(block.number)} {block.transactions.length} transactions {" "} in this block
N/A N/A {ethers.utils.commify(block.difficulty)} {ethers.utils.commify(block.totalDifficulty.toString())} {ethers.utils.commify(block.size)} bytes {extraStr} (Hex:{" "} {block.extraData}) N/A {block.hash} {block.sha3Uncles} {block.stateRoot} {block.nonce}
)}
); }; type InfoRowProps = { title: string; }; const InfoRow: React.FC = ({ title, children }) => (
{title}:
{children}
); export default React.memo(Block);