Skip to content

Commit

Permalink
add block explorer url
Browse files Browse the repository at this point in the history
  • Loading branch information
karooolis committed Sep 27, 2024
1 parent 41f4ad4 commit 4f396bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Abi } from "viem";
import { ExternalLinkIcon } from "lucide-react";
import { Hex } from "viem";
import { Row, flexRender } from "@tanstack/react-table";
import { Separator } from "../../../../../../components/ui/Separator";
import { TableCell, TableRow } from "../../../../../../components/ui/Table";
import { TruncatedHex } from "../../../../../../components/ui/TruncatedHex";
import { useChain } from "../../../../hooks/useChain";
import { explorerForChainId } from "../../../../utils/explorerForChainId";
import { WatchedTransaction } from "./TransactionsTable";
import { columns } from "./columns";

Expand All @@ -15,9 +18,19 @@ function TranctionTableRowDataCell({ label, children }: { label: string; childre
);
}

export function TransactionTableRow({ row }: { row: Row<WatchedTransaction>; abi: Abi }) {
const data = row.original;
function BlockExplorerLink({ hash, chainId }: { hash?: Hex; chainId: number }) {
const explorerUrl = explorerForChainId({ hash, chainId });
if (!explorerUrl) return null;

return (
<a href={`${explorerUrl}/tx/${hash}`} target="_blank" rel="noopener noreferrer" className="flex hover:underline">
<ExternalLinkIcon className="mr-2 h-3 w-3" /> Link
</a>
);
}
export function TransactionTableRow({ row }: { row: Row<WatchedTransaction> }) {
const { id: chainId } = useChain();
const data = row?.original;
return (
<>
<TableRow className="cursor-pointer" onClick={() => row.toggleExpanded()}>
Expand All @@ -41,6 +54,9 @@ export function TransactionTableRow({ row }: { row: Row<WatchedTransaction>; abi
<TranctionTableRowDataCell label="From">
{data?.transaction?.from && <TruncatedHex hex={data.transaction.from} />}
</TranctionTableRowDataCell>
<TranctionTableRowDataCell label="Explorer URL">
<BlockExplorerLink hash={data.transaction?.hash} chainId={chainId} />
</TranctionTableRowDataCell>
<TranctionTableRowDataCell label="Tx value">
{data.transaction?.value?.toString()}
</TranctionTableRowDataCell>
Expand All @@ -56,7 +72,6 @@ export function TransactionTableRow({ row }: { row: Row<WatchedTransaction>; abi
<TranctionTableRowDataCell label="Gas price">
{data.receipt?.effectiveGasPrice.toString()}
</TranctionTableRowDataCell>
<TranctionTableRowDataCell label="Total fee">TODO:</TranctionTableRowDataCell>
</div>

<Separator className="mt-6" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { columns } from "./columns";

export type WatchedTransaction = {
hash: Hex;
timestamp: bigint;
transaction?: Transaction;
functionData?: DecodeFunctionDataReturnType;
receipt?: TransactionReceipt;
Expand Down
13 changes: 13 additions & 0 deletions packages/explorer/src/app/(explorer)/utils/explorerForChainId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Hex } from "viem";
import { chainIdToName, supportedChains, validateChainId } from "../../../common";

export function explorerForChainId({ hash, chainId }: { hash: Hex | undefined; chainId: number }): string | undefined {
if (!hash) return undefined;
validateChainId(chainId);

const chainName = chainIdToName[chainId];
const chain = supportedChains[chainName];
const explorerUrl = chain.blockExplorers?.default.url;
if (!explorerUrl) return undefined;
return explorerUrl;
}

0 comments on commit 4f396bc

Please sign in to comment.