otterscan/src/Block.tsx

193 lines
7.0 KiB
TypeScript
Raw Normal View History

2021-07-26 22:59:07 +00:00
import React, { useEffect, useMemo, useContext } from "react";
2021-07-01 18:21:40 +00:00
import { useParams, NavLink } from "react-router-dom";
import { BigNumber } from "@ethersproject/bignumber";
import { commify } from "@ethersproject/units";
import { toUtf8String } from "@ethersproject/strings";
2021-07-29 00:59:21 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBurn } from "@fortawesome/free-solid-svg-icons/faBurn";
2021-07-01 18:21:40 +00:00
import StandardFrame from "./StandardFrame";
import StandardSubtitle from "./StandardSubtitle";
import NavBlock from "./block/NavBlock";
2021-07-01 18:21:40 +00:00
import ContentFrame from "./ContentFrame";
import InfoRow from "./components/InfoRow";
2021-07-01 18:21:40 +00:00
import Timestamp from "./components/Timestamp";
import GasValue from "./components/GasValue";
import PercentageBar from "./components/PercentageBar";
2021-07-01 18:21:40 +00:00
import BlockLink from "./components/BlockLink";
import DecoratedAddressLink from "./components/DecoratedAddressLink";
2021-07-03 21:51:57 +00:00
import TransactionValue from "./components/TransactionValue";
import FormattedBalance from "./components/FormattedBalance";
import ETH2USDValue from "./components/ETH2USDValue";
import USDValue from "./components/USDValue";
import HexValue from "./components/HexValue";
2021-07-09 05:07:20 +00:00
import { RuntimeContext } from "./useRuntime";
import { useLatestBlockNumber } from "./useLatestBlock";
import { blockTxsURL } from "./url";
2021-07-26 22:59:07 +00:00
import { useBlockData } from "./useErigonHooks";
import { useETHUSDOracle } from "./usePriceOracle";
2021-07-01 18:21:40 +00:00
const Block: React.FC = () => {
2021-07-09 05:07:20 +00:00
const { provider } = useContext(RuntimeContext);
2021-11-25 09:28:45 +00:00
const { blockNumberOrHash } = useParams();
if (blockNumberOrHash === undefined) {
throw new Error("blockNumberOrHash couldn't be undefined here");
}
2021-07-01 18:21:40 +00:00
2021-11-25 09:28:45 +00:00
const block = useBlockData(provider, blockNumberOrHash);
2021-07-01 18:21:40 +00:00
useEffect(() => {
if (block) {
document.title = `Block #${block.number} | Otterscan`;
}
}, [block]);
const extraStr = useMemo(() => {
try {
return block && toUtf8String(block.extraData);
2021-07-01 18:21:40 +00:00
} catch (err) {
2021-10-18 20:01:32 +00:00
console.info("Error while converting block extra data to string");
console.info(err);
2021-07-01 18:21:40 +00:00
}
}, [block]);
2021-07-29 00:59:21 +00:00
const burntFees =
block?.baseFeePerGas && block.baseFeePerGas.mul(block.gasUsed);
const netFeeReward = block?.feeReward ?? BigNumber.from(0);
const gasUsedPerc =
block && block.gasUsed.mul(10000).div(block.gasLimit).toNumber() / 100;
2021-07-01 18:21:40 +00:00
2021-07-08 19:02:42 +00:00
const latestBlockNumber = useLatestBlockNumber(provider);
const blockETHUSDPrice = useETHUSDOracle(provider, block?.number);
2021-07-01 18:21:40 +00:00
return (
<StandardFrame>
<StandardSubtitle>
2021-07-23 22:20:49 +00:00
<div className="flex space-x-1 items-baseline">
<span>Block</span>
2021-11-25 09:28:45 +00:00
<span className="text-base text-gray-500">#{blockNumberOrHash}</span>
2021-07-23 22:20:49 +00:00
{block && (
<NavBlock
blockNumber={block.number}
latestBlockNumber={latestBlockNumber}
/>
2021-07-23 22:20:49 +00:00
)}
</div>
</StandardSubtitle>
{block && (
<ContentFrame>
<InfoRow title="Block Height">
<span className="font-bold">{commify(block.number)}</span>
2021-07-01 18:21:40 +00:00
</InfoRow>
<InfoRow title="Timestamp">
<Timestamp value={block.timestamp} />
</InfoRow>
<InfoRow title="Transactions">
<NavLink
className="bg-link-blue bg-opacity-10 text-link-blue hover:bg-opacity-100 hover:text-white rounded-lg px-2 py-1 text-xs"
to={blockTxsURL(block.number)}
2021-07-01 18:21:40 +00:00
>
{block.transactionCount} transactions
2021-07-01 18:21:40 +00:00
</NavLink>{" "}
in this block
</InfoRow>
<InfoRow title="Mined by">
<DecoratedAddressLink address={block.miner} miner />
2021-07-01 18:21:40 +00:00
</InfoRow>
2021-07-03 21:51:57 +00:00
<InfoRow title="Block Reward">
<TransactionValue value={block.blockReward.add(netFeeReward)} />
{!netFeeReward.isZero() && (
2021-07-03 22:14:03 +00:00
<>
{" "}
(<TransactionValue value={block.blockReward} hideUnit /> +{" "}
<TransactionValue value={netFeeReward} hideUnit />)
2021-07-03 22:14:03 +00:00
</>
)}
{blockETHUSDPrice && (
<>
{" "}
<span className="px-2 border-yellow-200 border rounded-lg bg-yellow-100 text-yellow-600">
<ETH2USDValue
ethAmount={block.blockReward.add(netFeeReward)}
eth2USDValue={blockETHUSDPrice}
/>
</span>
</>
)}
2021-07-03 21:51:57 +00:00
</InfoRow>
<InfoRow title="Uncles Reward">
<TransactionValue value={block.unclesReward} />
</InfoRow>
<InfoRow title="Size">{commify(block.size)} bytes</InfoRow>
{block.baseFeePerGas && (
<InfoRow title="Base Fee">
<span>
<FormattedBalance value={block.baseFeePerGas} decimals={9} />{" "}
Gwei (
<FormattedBalance
value={block.baseFeePerGas}
decimals={0}
/>{" "}
wei)
</span>
</InfoRow>
)}
2021-07-29 00:59:21 +00:00
{burntFees && (
<InfoRow title="Burnt Fees">
<div className="flex items-baseline space-x-1">
<span className="flex space-x-1 text-orange-500">
<span title="Burnt fees">
<FontAwesomeIcon icon={faBurn} size="1x" />
</span>
<span>
<span className="line-through">
<FormattedBalance value={burntFees} />
</span>{" "}
Ether
</span>
</span>
</div>
</InfoRow>
)}
<InfoRow title="Gas Used/Limit">
<div className="flex space-x-3 items-baseline">
<div>
<GasValue value={block.gasUsed} /> /{" "}
<GasValue value={block.gasLimit} />
</div>
<PercentageBar perc={gasUsedPerc!} />
</div>
2021-07-01 18:21:40 +00:00
</InfoRow>
<InfoRow title="Extra Data">
{extraStr} (Hex:{" "}
2021-10-18 20:00:04 +00:00
<span className="font-data break-all">{block.extraData}</span>)
2021-07-01 18:21:40 +00:00
</InfoRow>
<InfoRow title="Ether Price">
<USDValue value={blockETHUSDPrice} />
</InfoRow>
<InfoRow title="Difficult">
{commify(block._difficulty.toString())}
</InfoRow>
<InfoRow title="Total Difficult">
{commify(block.totalDifficulty.toString())}
</InfoRow>
2021-07-01 18:21:40 +00:00
<InfoRow title="Hash">
2021-07-04 01:33:34 +00:00
<HexValue value={block.hash} />
2021-07-01 18:21:40 +00:00
</InfoRow>
<InfoRow title="Parent Hash">
<BlockLink blockTag={block.parentHash} />
</InfoRow>
<InfoRow title="Sha3Uncles">
<HexValue value={block.sha3Uncles} />
2021-07-01 18:21:40 +00:00
</InfoRow>
<InfoRow title="StateRoot">
<HexValue value={block.stateRoot} />
2021-07-01 18:21:40 +00:00
</InfoRow>
<InfoRow title="Nonce">
<span className="font-data">{block.nonce}</span>
</InfoRow>
</ContentFrame>
)}
</StandardFrame>
);
};
2021-11-25 09:28:45 +00:00
export default Block;