Use native ethereum-lists/chains json format; dont reinvent the wheel

This commit is contained in:
Willian Mitsuda 2022-04-02 14:56:36 -03:00
parent 932505fd50
commit 5086b6f856
11 changed files with 66 additions and 44 deletions

View File

@ -34,7 +34,9 @@ const Block: React.FC = () => {
if (blockNumberOrHash === undefined) { if (blockNumberOrHash === undefined) {
throw new Error("blockNumberOrHash couldn't be undefined here"); throw new Error("blockNumberOrHash couldn't be undefined here");
} }
const { nativeName, nativeSymbol } = useChainInfo(); const {
nativeCurrency: { name, symbol },
} = useChainInfo();
const block = useBlockData(provider, blockNumberOrHash); const block = useBlockData(provider, blockNumberOrHash);
useEffect(() => { useEffect(() => {
@ -146,7 +148,7 @@ const Block: React.FC = () => {
<span className="line-through"> <span className="line-through">
<FormattedBalance value={burntFees} /> <FormattedBalance value={burntFees} />
</span>{" "} </span>{" "}
{nativeSymbol} {symbol}
</span> </span>
</span> </span>
</div> </div>
@ -165,7 +167,7 @@ const Block: React.FC = () => {
{extraStr} (Hex:{" "} {extraStr} (Hex:{" "}
<span className="font-data break-all">{block.extraData}</span>) <span className="font-data break-all">{block.extraData}</span>)
</InfoRow> </InfoRow>
<InfoRow title={`${nativeName} Price`}> <InfoRow title={`${name} Price`}>
<USDValue value={blockETHUSDPrice} /> <USDValue value={blockETHUSDPrice} />
</InfoRow> </InfoRow>
<InfoRow title="Difficult"> <InfoRow title="Difficult">

View File

@ -14,7 +14,9 @@ const ETH_FEED_DECIMALS = 8;
// TODO: reduce duplication with useETHUSDOracle // TODO: reduce duplication with useETHUSDOracle
const PriceBox: React.FC = () => { const PriceBox: React.FC = () => {
const { provider } = useContext(RuntimeContext); const { provider } = useContext(RuntimeContext);
const { nativeSymbol } = useChainInfo(); const {
nativeCurrency: { symbol },
} = useChainInfo();
const latestBlock = useLatestBlock(provider); const latestBlock = useLatestBlock(provider);
const maybeOutdated: boolean = const maybeOutdated: boolean =
@ -82,9 +84,9 @@ const PriceBox: React.FC = () => {
} font-sans text-xs text-gray-800`} } font-sans text-xs text-gray-800`}
> >
<span <span
title={`${nativeSymbol}/USD last updated at: ${latestPriceTimestamp?.toString()}`} title={`${symbol}/USD last updated at: ${latestPriceTimestamp?.toString()}`}
> >
{nativeSymbol}: $<span className="font-balance">{latestPrice}</span> {symbol}: $<span className="font-balance">{latestPrice}</span>
</span> </span>
{latestGasData && ( {latestGasData && (
<> <>

View File

@ -17,7 +17,9 @@ const InternalSelfDestruct: React.FC<InternalSelfDestructProps> = ({
txData, txData,
internalOp, internalOp,
}) => { }) => {
const { nativeSymbol } = useChainInfo(); const {
nativeCurrency: { symbol },
} = useChainInfo();
const toMiner = const toMiner =
txData.confirmedData?.miner !== undefined && txData.confirmedData?.miner !== undefined &&
internalOp.to === txData.confirmedData.miner; internalOp.to === txData.confirmedData.miner;
@ -46,7 +48,7 @@ const InternalSelfDestruct: React.FC<InternalSelfDestructProps> = ({
<FontAwesomeIcon icon={faAngleRight} size="1x" /> TRANSFER <FontAwesomeIcon icon={faAngleRight} size="1x" /> TRANSFER
</span> </span>
<span> <span>
{formatEther(internalOp.value)} {nativeSymbol} {formatEther(internalOp.value)} {symbol}
</span> </span>
<div className="flex items-baseline"> <div className="flex items-baseline">
<span className="text-gray-500">To</span> <span className="text-gray-500">To</span>

View File

@ -18,7 +18,9 @@ const InternalTransfer: React.FC<InternalTransferProps> = ({
txData, txData,
internalOp, internalOp,
}) => { }) => {
const { nativeSymbol } = useChainInfo(); const {
nativeCurrency: { symbol },
} = useChainInfo();
const fromMiner = const fromMiner =
txData.confirmedData?.miner !== undefined && txData.confirmedData?.miner !== undefined &&
internalOp.from === txData.confirmedData.miner; internalOp.from === txData.confirmedData.miner;
@ -44,7 +46,7 @@ const InternalTransfer: React.FC<InternalTransferProps> = ({
<FontAwesomeIcon icon={faAngleRight} size="1x" /> TRANSFER <FontAwesomeIcon icon={faAngleRight} size="1x" /> TRANSFER
</span> </span>
<span> <span>
{formatEther(internalOp.value)} {nativeSymbol} {formatEther(internalOp.value)} {symbol}
</span> </span>
<div className="flex items-baseline"> <div className="flex items-baseline">
<span className="text-gray-500">From</span> <span className="text-gray-500">From</span>

View File

@ -22,16 +22,18 @@ const TransactionValue: React.FC<TransactionValueProps> = ({
value, value,
hideUnit, hideUnit,
}) => { }) => {
const { nativeSymbol, nativeDecimals } = useChainInfo(); const {
const formattedValue = formatValue(value, nativeDecimals); nativeCurrency: { symbol, decimals },
} = useChainInfo();
const formattedValue = formatValue(value, decimals);
return ( return (
<span <span
className={`text-sm ${value.isZero() ? "text-gray-400" : ""}`} className={`text-sm ${value.isZero() ? "text-gray-400" : ""}`}
title={`${formattedValue} ${nativeSymbol}`} title={`${formattedValue} ${symbol}`}
> >
<span className={`font-balance`}>{formattedValue}</span> <span className={`font-balance`}>{formattedValue}</span>
{!hideUnit && ` ${nativeSymbol}`} {!hideUnit && ` ${symbol}`}
</span> </span>
); );
}; };

View File

@ -10,7 +10,9 @@ type USDValueProps = {
}; };
const USDValue: React.FC<USDValueProps> = ({ value }) => { const USDValue: React.FC<USDValueProps> = ({ value }) => {
const { nativeSymbol } = useChainInfo(); const {
nativeCurrency: { symbol },
} = useChainInfo();
return ( return (
<span className="text-sm"> <span className="text-sm">
@ -24,7 +26,7 @@ const USDValue: React.FC<USDValueProps> = ({ value }) => {
.toString() .toString()
)} )}
</span>{" "} </span>{" "}
<span className="text-xs text-gray-500">/ {nativeSymbol}</span> <span className="text-xs text-gray-500">/ {symbol}</span>
</> </>
) : ( ) : (
"N/A" "N/A"

View File

@ -16,7 +16,9 @@ type BlockRowProps = {
}; };
const BlockRow: React.FC<BlockRowProps> = ({ now, block, baseFeeDelta }) => { const BlockRow: React.FC<BlockRowProps> = ({ now, block, baseFeeDelta }) => {
const { nativeSymbol } = useChainInfo(); const {
nativeCurrency: { symbol },
} = useChainInfo();
const gasTarget = block.gasLimit.div(ELASTICITY_MULTIPLIER); const gasTarget = block.gasLimit.div(ELASTICITY_MULTIPLIER);
const burntFees = const burntFees =
block?.baseFeePerGas && block.baseFeePerGas.mul(block.gasUsed); block?.baseFeePerGas && block.baseFeePerGas.mul(block.gasUsed);
@ -55,11 +57,10 @@ const BlockRow: React.FC<BlockRowProps> = ({ now, block, baseFeeDelta }) => {
</div> </div>
</div> </div>
<div className="text-right col-span-2"> <div className="text-right col-span-2">
{commify(formatEther(totalReward))} {nativeSymbol} {commify(formatEther(totalReward))} {symbol}
</div> </div>
<div className="text-right col-span-2 line-through text-orange-500"> <div className="text-right col-span-2 line-through text-orange-500">
{commify(formatEther(block.gasUsed.mul(block.baseFeePerGas!)))}{" "} {commify(formatEther(block.gasUsed.mul(block.baseFeePerGas!)))} {symbol}
{nativeSymbol}
</div> </div>
<div className="text-right text-gray-400"> <div className="text-right text-gray-400">
<TimestampAge now={now / 1000} timestamp={block.timestamp} /> <TimestampAge now={now / 1000} timestamp={block.timestamp} />

View File

@ -87,7 +87,9 @@ const Details: React.FC<DetailsProps> = ({
const devMethod = txDesc ? devDoc?.methods[txDesc.signature] : undefined; const devMethod = txDesc ? devDoc?.methods[txDesc.signature] : undefined;
const { provider } = useContext(RuntimeContext); const { provider } = useContext(RuntimeContext);
const { nativeName, nativeSymbol } = useChainInfo(); const {
nativeCurrency: { name, symbol },
} = useChainInfo();
const addresses = useMemo(() => { const addresses = useMemo(() => {
const _addresses: ChecksummedAddress[] = []; const _addresses: ChecksummedAddress[] = [];
if (txData.to) { if (txData.to) {
@ -315,7 +317,7 @@ const Details: React.FC<DetailsProps> = ({
</InfoRow> </InfoRow>
)} )}
<InfoRow title="Value"> <InfoRow title="Value">
<FormattedBalance value={txData.value} /> {nativeSymbol}{" "} <FormattedBalance value={txData.value} /> {symbol}{" "}
{!txData.value.isZero() && ethUSDPrice && ( {!txData.value.isZero() && ethUSDPrice && (
<span className="px-2 border-skin-from border rounded-lg bg-skin-from text-skin-from"> <span className="px-2 border-skin-from border rounded-lg bg-skin-from text-skin-from">
<ETH2USDValue ethAmount={txData.value} eth2USDValue={ethUSDPrice} /> <ETH2USDValue ethAmount={txData.value} eth2USDValue={ethUSDPrice} />
@ -338,8 +340,7 @@ const Details: React.FC<DetailsProps> = ({
{txData.type === 2 && ( {txData.type === 2 && (
<> <>
<InfoRow title="Max Priority Fee Per Gas"> <InfoRow title="Max Priority Fee Per Gas">
<FormattedBalance value={txData.maxPriorityFeePerGas!} />{" "} <FormattedBalance value={txData.maxPriorityFeePerGas!} /> {symbol} (
{nativeSymbol} (
<FormattedBalance <FormattedBalance
value={txData.maxPriorityFeePerGas!} value={txData.maxPriorityFeePerGas!}
decimals={9} decimals={9}
@ -347,7 +348,7 @@ const Details: React.FC<DetailsProps> = ({
Gwei) Gwei)
</InfoRow> </InfoRow>
<InfoRow title="Max Fee Per Gas"> <InfoRow title="Max Fee Per Gas">
<FormattedBalance value={txData.maxFeePerGas!} /> {nativeSymbol} ( <FormattedBalance value={txData.maxFeePerGas!} /> {symbol} (
<FormattedBalance value={txData.maxFeePerGas!} decimals={9} /> Gwei) <FormattedBalance value={txData.maxFeePerGas!} decimals={9} /> Gwei)
</InfoRow> </InfoRow>
</> </>
@ -356,7 +357,7 @@ const Details: React.FC<DetailsProps> = ({
<InfoRow title="Gas Price"> <InfoRow title="Gas Price">
<div className="flex items-baseline space-x-1"> <div className="flex items-baseline space-x-1">
<span> <span>
<FormattedBalance value={txData.gasPrice} /> {nativeSymbol} ( <FormattedBalance value={txData.gasPrice} /> {symbol} (
<FormattedBalance value={txData.gasPrice} decimals={9} /> Gwei) <FormattedBalance value={txData.gasPrice} decimals={9} /> Gwei)
</span> </span>
{sendsEthToMiner && ( {sendsEthToMiner && (
@ -407,8 +408,7 @@ const Details: React.FC<DetailsProps> = ({
<InfoRow title="Transaction Fee"> <InfoRow title="Transaction Fee">
<div className="space-y-3"> <div className="space-y-3">
<div> <div>
<FormattedBalance value={txData.confirmedData.fee} />{" "} <FormattedBalance value={txData.confirmedData.fee} /> {symbol}{" "}
{nativeSymbol}{" "}
{ethUSDPrice && ( {ethUSDPrice && (
<span className="px-2 border-skin-from border rounded-lg bg-skin-from text-skin-from"> <span className="px-2 border-skin-from border rounded-lg bg-skin-from text-skin-from">
<ETH2USDValue <ETH2USDValue
@ -421,7 +421,7 @@ const Details: React.FC<DetailsProps> = ({
{hasEIP1559 && <RewardSplit txData={txData} />} {hasEIP1559 && <RewardSplit txData={txData} />}
</div> </div>
</InfoRow> </InfoRow>
<InfoRow title={`${nativeName} Price`}> <InfoRow title={`${name} Price`}>
<USDValue value={ethUSDPrice} /> <USDValue value={ethUSDPrice} />
</InfoRow> </InfoRow>
</> </>

View File

@ -12,7 +12,9 @@ type RewardSplitProps = {
}; };
const RewardSplit: React.FC<RewardSplitProps> = ({ txData }) => { const RewardSplit: React.FC<RewardSplitProps> = ({ txData }) => {
const { nativeSymbol } = useChainInfo(); const {
nativeCurrency: { symbol },
} = useChainInfo();
const paidFees = txData.gasPrice.mul(txData.confirmedData!.gasUsed); const paidFees = txData.gasPrice.mul(txData.confirmedData!.gasUsed);
const burntFees = txData.confirmedData!.blockBaseFeePerGas!.mul( const burntFees = txData.confirmedData!.blockBaseFeePerGas!.mul(
txData.confirmedData!.gasUsed txData.confirmedData!.gasUsed
@ -41,7 +43,7 @@ const RewardSplit: React.FC<RewardSplitProps> = ({ txData }) => {
<span className="line-through"> <span className="line-through">
<FormattedBalance value={burntFees} /> <FormattedBalance value={burntFees} />
</span>{" "} </span>{" "}
{nativeSymbol} {symbol}
</span> </span>
</span> </span>
</div> </div>
@ -57,7 +59,7 @@ const RewardSplit: React.FC<RewardSplitProps> = ({ txData }) => {
<FontAwesomeIcon icon={faCoins} size="1x" /> <FontAwesomeIcon icon={faCoins} size="1x" />
</span> </span>
<span> <span>
<FormattedBalance value={minerReward} /> {nativeSymbol} <FormattedBalance value={minerReward} /> {symbol}
</span> </span>
</span> </span>
</div> </div>

View File

@ -19,7 +19,9 @@ type TraceInputProps = {
}; };
const TraceInput: React.FC<TraceInputProps> = ({ t }) => { const TraceInput: React.FC<TraceInputProps> = ({ t }) => {
const { nativeSymbol } = useChainInfo(); const {
nativeCurrency: { symbol },
} = useChainInfo();
const raw4Bytes = extract4Bytes(t.input); const raw4Bytes = extract4Bytes(t.input);
const fourBytes = use4Bytes(raw4Bytes); const fourBytes = use4Bytes(raw4Bytes);
const sigText = const sigText =
@ -57,8 +59,7 @@ const TraceInput: React.FC<TraceInputProps> = ({ t }) => {
<FunctionSignature callType={t.type} sig={sigText} /> <FunctionSignature callType={t.type} sig={sigText} />
{t.value && !t.value.isZero() && ( {t.value && !t.value.isZero() && (
<span className="text-red-700 whitespace-nowrap"> <span className="text-red-700 whitespace-nowrap">
{"{"}value: <FormattedBalance value={t.value} />{" "} {"{"}value: <FormattedBalance value={t.value} /> {symbol}
{nativeSymbol}
{"}"} {"}"}
</span> </span>
)} )}

View File

@ -3,15 +3,19 @@ import { chainInfoURL } from "./url";
import { OtterscanRuntime } from "./useRuntime"; import { OtterscanRuntime } from "./useRuntime";
export type ChainInfo = { export type ChainInfo = {
nativeName: string; nativeCurrency: {
nativeSymbol: string; name: string;
nativeDecimals: number; symbol: string;
decimals: number;
};
}; };
export const defaultChainInfo: ChainInfo = { export const defaultChainInfo: ChainInfo = {
nativeName: "Ether", nativeCurrency: {
nativeSymbol: "ETH", name: "Ether",
nativeDecimals: 18, symbol: "ETH",
decimals: 18,
},
}; };
export const ChainInfoContext = createContext<ChainInfo | undefined>(undefined); export const ChainInfoContext = createContext<ChainInfo | undefined>(undefined);
@ -40,9 +44,11 @@ export const useChainInfoFromMetadataFile = (
const info = await res.json(); const info = await res.json();
setChainInfo({ setChainInfo({
nativeName: info.nativeCurrency.name, nativeCurrency: {
nativeDecimals: info.nativeCurrency.decimals, name: info.nativeCurrency.name,
nativeSymbol: info.nativeCurrency.symbol, decimals: info.nativeCurrency.decimals,
symbol: info.nativeCurrency.symbol,
},
}); });
} catch (err) { } catch (err) {
// ignore // ignore