Skip to content

Commit

Permalink
Populate RPC responses as they are received
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Jul 26, 2024
1 parent 5d22525 commit 119df42
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/components/RpcTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,49 @@ import { Web3Context } from "../context/Web3Context";
import { JsonRpcProvider } from "ethers";
import { ChainData } from "../types/chain";

async function checkRpc(chainId: number, rpc: string) {
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(rpc, chainId, { staticNetwork: true });
const provider = new JsonRpcProvider(rpcUrl, chainId, {
staticNetwork: true,
});
const blockNumber = await provider.getBlockNumber();
return { blockNumber, latency: Date.now() - now };
return { rpcUrl, blockNumber, latency: Date.now() - now };
} catch (error) {
return { error };
return { rpcUrl, error };
}
}

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

useEffect(() => {
Promise.all(rpcs.map((rpc) => checkRpc(chainId, rpc))).then(setRpcResults);
rpcs.forEach((rpc) =>
checkRpc(chainId, rpc).then((result) => {
setRpcResults((state) => [...(state ?? []), result]);
})
);
}, []);

const mergedRpcs = rpcs
.map((rpcUrl, index) => {
const rpcResult = rpcResults?.[index] ?? {};
return { rpcUrl, ...rpcResult };
.map((rpcUrl) => {
const rpcResult = rpcResults?.find((result) => result.rpcUrl === rpcUrl);
return rpcResult ?? { rpcUrl };
})
.sort((a, b) => {
if (!a.latency) {
Expand Down

0 comments on commit 119df42

Please sign in to comment.