Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show RPC block number on chain page #187

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 88 additions & 16 deletions src/components/RpcTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext } from "react";
import React, { useContext, useEffect, useState } from "react";
import {
Button,
Skeleton,
Table,
TableContainer,
Tbody,
Expand All @@ -10,34 +11,105 @@ import {
Tr,
} from "@chakra-ui/react";
import { Web3Context } from "../context/Web3Context";
import { JsonRpcProvider } from "ethers";
import { ChainData } from "../types/chain";

export const RpcTable = ({ rpcs, handleRpcClick }) => {
interface RpcResult {
rpcUrl: string;
blockNumber?: number;
latency?: number;
error?: unknown;
}

async function checkRpc(chainId: number, rpcUrl: string): Promise<RpcResult> {
try {
const now = Date.now();
const provider = new JsonRpcProvider(rpcUrl, chainId, {
staticNetwork: true,
});
const blockNumber = await provider.getBlockNumber();
return { rpcUrl, blockNumber, latency: Date.now() - now };
} catch (error) {
return { rpcUrl, error };
}
}

export const RpcTable = ({
chainId,
rpcs,
handleRpcClick,
}: Pick<ChainData, "chainId"> & {
rpcs: ChainData["rpc"];
handleRpcClick: (rpc: string) => void;
}) => {
const { isConnected, handleConnect } = useContext(Web3Context);
const [rpcResults, setRpcResults] = useState<RpcResult[] | null>(null);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metamask

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oliver.2023.avalos.cb.id
0x2cb8170c91fDd19FDB3dC8f1e687baa2B6DAC337
CoinBase.
#187 (comment)

const { isConnected, handleConnect } = useContext(Web3Context); + const [rpcResults, setRpcResults] = useState<RpcResult[] | null>(null); + + useEffect(() => {0x2cb8170c91fDd19FDB3dC8f1e687baa2B6DAC337}


useEffect(() => {
rpcs.forEach((rpc) =>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chainlist

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x2cb8170c91fDd19FDB3dC8f1e687baa2B6DAC337
CoinBase
Oliver.2023.avalos.cb.id
Metamask.0xf931a800B8B96453674d78A75D83593432Ae26d4.wallet
#187 (comment)

const { isConnected, handleConnect } = useContext(Web3Context);

  • const [rpcResults, setRpcResults] = useState<RpcResult[] | null>(null);
  • useEffect(() => {0x2cb8170c91fDd19FDB3dC8f1e687baa2B6DAC337}
    Oliver Joaquín Madera Avalos
    Oliver23Avalos

checkRpc(chainId, rpc).then((result) => {
setRpcResults((state) => [...(state ?? []), result]);
})
);
}, []);

const mergedRpcs = rpcs
.map((rpcUrl) => {
const rpcResult = rpcResults?.find((result) => result.rpcUrl === rpcUrl);
return rpcResult ?? { rpcUrl };
})
.sort((a, b) => {
if (!a.latency) {
return 1;
}
if (!b.latency) {
return -1;
}
return a.latency < b.latency ? -1 : 1;
});

return (
<TableContainer>
<Table>
<Thead>
<Tr>
<Th pl="0">RPC URL</Th>
<Th>Block Number</Th>
<Th>Latency</Th>
<Th></Th>
</Tr>
</Thead>
<Tbody>
{rpcs.map((rpcUrl) => (
<Tr>
<Td pl="0">{rpcUrl}</Td>
<Td pr="0" textAlign="end">
{!isConnected ? (
<Button onClick={handleConnect}>Connect</Button>
) : (
<Button onClick={() => handleRpcClick(rpcUrl)}>
Add Chain
</Button>
)}
</Td>
</Tr>
))}
{mergedRpcs.map(({ rpcUrl, blockNumber, latency, error }) => {
return (
<Tr key={rpcUrl}>
<Td pl="0">{rpcUrl}</Td>
<Td>
{blockNumber || error ? (
blockNumber ?? "Unavailable"
) : (
<Skeleton height="24px" />
)}
</Td>
<Td>
{latency || error ? (
<>{latency ?? "?"} ms</>
) : (
<Skeleton height="24px" />
)}
</Td>
<Td pr="0" textAlign="end">
{!isConnected ? (
<Button onClick={handleConnect}>Connect</Button>
) : (
<Button onClick={() => handleRpcClick(rpcUrl)}>
Add Chain
</Button>
)}
</Td>
</Tr>
);
})}
</Tbody>
</Table>
</TableContainer>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/chain/{Chain.chainId}.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const ChainPage = ({ data }: { data: { chain: ChainData } }) => {
)}
</StatGroup>
<Divider my="8" />
<RpcTable rpcs={rpc} handleRpcClick={handleRpcClick} />
<RpcTable chainId={chainId} rpcs={rpc} handleRpcClick={handleRpcClick} />
</Flex>
</Layout>
</>
Expand Down
Loading