Commit e1c2c54e authored by tom's avatar tom

fix block views

parent 1bedf712
...@@ -2,10 +2,16 @@ import { createPublicClient, http } from 'viem'; ...@@ -2,10 +2,16 @@ import { createPublicClient, http } from 'viem';
import currentChain from './currentChain'; import currentChain from './currentChain';
export const publicClient = createPublicClient({ export const publicClient = (() => {
if (currentChain.rpcUrls.public.http.filter(Boolean).length === 0) {
return;
}
return createPublicClient({
chain: currentChain, chain: currentChain,
transport: http(), transport: http(),
batch: { batch: {
multicall: true, multicall: true,
}, },
}); });
})();
...@@ -50,6 +50,10 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery { ...@@ -50,6 +50,10 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery {
const rpcQuery = useQuery<RpcResponseType, unknown, Block | null>({ const rpcQuery = useQuery<RpcResponseType, unknown, Block | null>({
queryKey: [ 'RPC', 'block', { heightOrHash } ], queryKey: [ 'RPC', 'block', { heightOrHash } ],
queryFn: async() => { queryFn: async() => {
if (!publicClient) {
return null;
}
const blockParams = heightOrHash.startsWith('0x') ? { blockHash: heightOrHash as `0x${ string }` } : { blockNumber: BigInt(heightOrHash) }; const blockParams = heightOrHash.startsWith('0x') ? { blockHash: heightOrHash as `0x${ string }` } : { blockNumber: BigInt(heightOrHash) };
return publicClient.getBlock(blockParams).catch(() => null); return publicClient.getBlock(blockParams).catch(() => null);
}, },
...@@ -86,13 +90,13 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery { ...@@ -86,13 +90,13 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery {
}; };
}, },
placeholderData: GET_BLOCK, placeholderData: GET_BLOCK,
enabled: apiQuery.isError || apiQuery.errorUpdateCount > 0, enabled: publicClient !== undefined && (apiQuery.isError || apiQuery.errorUpdateCount > 0),
retry: false, retry: false,
refetchOnMount: false, refetchOnMount: false,
}); });
React.useEffect(() => { React.useEffect(() => {
if (apiQuery.isPlaceholderData) { if (apiQuery.isPlaceholderData || !publicClient) {
return; return;
} }
...@@ -109,7 +113,7 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery { ...@@ -109,7 +113,7 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery {
} }
}, [ rpcQuery.data, rpcQuery.isPlaceholderData ]); }, [ rpcQuery.data, rpcQuery.isPlaceholderData ]);
const isRpcQuery = Boolean((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0 && rpcQuery.data); const isRpcQuery = Boolean(publicClient && (apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0 && rpcQuery.data);
const query = isRpcQuery ? rpcQuery as UseQueryResult<Block, ResourceError<{ status: number }>> : apiQuery; const query = isRpcQuery ? rpcQuery as UseQueryResult<Block, ResourceError<{ status: number }>> : apiQuery;
return { return {
......
...@@ -63,6 +63,10 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param ...@@ -63,6 +63,10 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param
const rpcQuery = useQuery<RpcResponseType, unknown, BlockTransactionsResponse | null>({ const rpcQuery = useQuery<RpcResponseType, unknown, BlockTransactionsResponse | null>({
queryKey: [ 'RPC', 'block_txs', { heightOrHash } ], queryKey: [ 'RPC', 'block_txs', { heightOrHash } ],
queryFn: async() => { queryFn: async() => {
if (!publicClient) {
return null;
}
const blockParams = heightOrHash.startsWith('0x') ? const blockParams = heightOrHash.startsWith('0x') ?
{ blockHash: heightOrHash as `0x${ string }`, includeTransactions: true } : { blockHash: heightOrHash as `0x${ string }`, includeTransactions: true } :
{ blockNumber: BigInt(heightOrHash), includeTransactions: true }; { blockNumber: BigInt(heightOrHash), includeTransactions: true };
...@@ -125,13 +129,13 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param ...@@ -125,13 +129,13 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param
}; };
}, },
placeholderData: GET_BLOCK_WITH_TRANSACTIONS, placeholderData: GET_BLOCK_WITH_TRANSACTIONS,
enabled: tab === 'txs' && (blockQuery.isDegradedData || apiQuery.isError || apiQuery.errorUpdateCount > 0), enabled: publicClient !== undefined && tab === 'txs' && (blockQuery.isDegradedData || apiQuery.isError || apiQuery.errorUpdateCount > 0),
retry: false, retry: false,
refetchOnMount: false, refetchOnMount: false,
}); });
React.useEffect(() => { React.useEffect(() => {
if (apiQuery.isPlaceholderData) { if (apiQuery.isPlaceholderData || !publicClient) {
return; return;
} }
...@@ -151,7 +155,7 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param ...@@ -151,7 +155,7 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param
const isRpcQuery = Boolean(( const isRpcQuery = Boolean((
blockQuery.isDegradedData || blockQuery.isDegradedData ||
((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0) ((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0)
) && rpcQuery.data); ) && rpcQuery.data && publicClient);
const rpcQueryWithPages: QueryWithPagesResult<'block_txs'> = React.useMemo(() => { const rpcQueryWithPages: QueryWithPagesResult<'block_txs'> = React.useMemo(() => {
return { return {
......
...@@ -65,6 +65,10 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab ...@@ -65,6 +65,10 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
const rpcQuery = useQuery<RpcResponseType, unknown, BlockWithdrawalsResponse | null>({ const rpcQuery = useQuery<RpcResponseType, unknown, BlockWithdrawalsResponse | null>({
queryKey: [ 'RPC', 'block', { heightOrHash } ], queryKey: [ 'RPC', 'block', { heightOrHash } ],
queryFn: async() => { queryFn: async() => {
if (!publicClient) {
return null;
}
const blockParams = heightOrHash.startsWith('0x') ? { blockHash: heightOrHash as `0x${ string }` } : { blockNumber: BigInt(heightOrHash) }; const blockParams = heightOrHash.startsWith('0x') ? { blockHash: heightOrHash as `0x${ string }` } : { blockNumber: BigInt(heightOrHash) };
return publicClient.getBlock(blockParams).catch(() => null); return publicClient.getBlock(blockParams).catch(() => null);
}, },
...@@ -89,6 +93,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab ...@@ -89,6 +93,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
}, },
placeholderData: GET_BLOCK, placeholderData: GET_BLOCK,
enabled: enabled:
publicClient !== undefined &&
tab === 'withdrawals' && tab === 'withdrawals' &&
config.features.beaconChain.isEnabled && config.features.beaconChain.isEnabled &&
(blockQuery.isDegradedData || apiQuery.isError || apiQuery.errorUpdateCount > 0), (blockQuery.isDegradedData || apiQuery.isError || apiQuery.errorUpdateCount > 0),
...@@ -97,7 +102,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab ...@@ -97,7 +102,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
}); });
React.useEffect(() => { React.useEffect(() => {
if (apiQuery.isPlaceholderData) { if (apiQuery.isPlaceholderData || !publicClient) {
return; return;
} }
...@@ -117,7 +122,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab ...@@ -117,7 +122,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
const isRpcQuery = Boolean(( const isRpcQuery = Boolean((
blockQuery.isDegradedData || blockQuery.isDegradedData ||
((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0) ((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0)
) && rpcQuery.data); ) && rpcQuery.data && publicClient);
const rpcQueryWithPages: QueryWithPagesResult<'block_withdrawals'> = React.useMemo(() => { const rpcQueryWithPages: QueryWithPagesResult<'block_withdrawals'> = React.useMemo(() => {
return { return {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment