-
Notifications
You must be signed in to change notification settings - Fork 174
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
Changes from all commits
e9d6561
d3f0c6d
a3e758b
0916e67
5d22525
119df42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
|
@@ -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); | ||
|
||
useEffect(() => { | ||
rpcs.forEach((rpc) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chainlist There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0x2cb8170c91fDd19FDB3dC8f1e687baa2B6DAC337
|
||
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> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metamask
There was a problem hiding this comment.
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)