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

Do not send user operations API requests, if NEXT_PUBLIC_HAS_USER_OPS is not enabled #1592

Merged
merged 4 commits into from
Feb 9, 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
22 changes: 15 additions & 7 deletions lib/web3/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import { createPublicClient, http } from 'viem';

import currentChain from './currentChain';

export const publicClient = createPublicClient({
chain: currentChain,
transport: http(),
batch: {
multicall: true,
},
});
export const publicClient = (() => {
if (currentChain.rpcUrls.public.http.filter(Boolean).length === 0) {
return;
}

try {
return createPublicClient({
chain: currentChain,
transport: http(),
batch: {
multicall: true,
},
});
} catch (error) {}
})();
10 changes: 7 additions & 3 deletions ui/block/useBlockQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery {
const rpcQuery = useQuery<RpcResponseType, unknown, Block | null>({
queryKey: [ 'RPC', 'block', { heightOrHash } ],
queryFn: async() => {
if (!publicClient) {
return null;
}

const blockParams = heightOrHash.startsWith('0x') ? { blockHash: heightOrHash as `0x${ string }` } : { blockNumber: BigInt(heightOrHash) };
return publicClient.getBlock(blockParams).catch(() => null);
},
Expand Down Expand Up @@ -86,13 +90,13 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery {
};
},
placeholderData: GET_BLOCK,
enabled: apiQuery.isError || apiQuery.errorUpdateCount > 0,
enabled: publicClient !== undefined && (apiQuery.isError || apiQuery.errorUpdateCount > 0),
retry: false,
refetchOnMount: false,
});

React.useEffect(() => {
if (apiQuery.isPlaceholderData) {
if (apiQuery.isPlaceholderData || !publicClient) {
return;
}

Expand All @@ -109,7 +113,7 @@ export default function useBlockQuery({ heightOrHash }: Params): BlockQuery {
}
}, [ 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;

return {
Expand Down
10 changes: 7 additions & 3 deletions ui/block/useBlockTxQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param
const rpcQuery = useQuery<RpcResponseType, unknown, BlockTransactionsResponse | null>({
queryKey: [ 'RPC', 'block_txs', { heightOrHash } ],
queryFn: async() => {
if (!publicClient) {
return null;
}

const blockParams = heightOrHash.startsWith('0x') ?
{ blockHash: heightOrHash as `0x${ string }`, includeTransactions: true } :
{ blockNumber: BigInt(heightOrHash), includeTransactions: true };
Expand Down Expand Up @@ -125,13 +129,13 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param
};
},
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,
refetchOnMount: false,
});

React.useEffect(() => {
if (apiQuery.isPlaceholderData) {
if (apiQuery.isPlaceholderData || !publicClient) {
return;
}

Expand All @@ -151,7 +155,7 @@ export default function useBlockTxQuery({ heightOrHash, blockQuery, tab }: Param
const isRpcQuery = Boolean((
blockQuery.isDegradedData ||
((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0)
) && rpcQuery.data);
) && rpcQuery.data && publicClient);

const rpcQueryWithPages: QueryWithPagesResult<'block_txs'> = React.useMemo(() => {
return {
Expand Down
9 changes: 7 additions & 2 deletions ui/block/useBlockWithdrawalsQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
const rpcQuery = useQuery<RpcResponseType, unknown, BlockWithdrawalsResponse | null>({
queryKey: [ 'RPC', 'block', { heightOrHash } ],
queryFn: async() => {
if (!publicClient) {
return null;
}

const blockParams = heightOrHash.startsWith('0x') ? { blockHash: heightOrHash as `0x${ string }` } : { blockNumber: BigInt(heightOrHash) };
return publicClient.getBlock(blockParams).catch(() => null);
},
Expand All @@ -89,6 +93,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
},
placeholderData: GET_BLOCK,
enabled:
publicClient !== undefined &&
tab === 'withdrawals' &&
config.features.beaconChain.isEnabled &&
(blockQuery.isDegradedData || apiQuery.isError || apiQuery.errorUpdateCount > 0),
Expand All @@ -97,7 +102,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
});

React.useEffect(() => {
if (apiQuery.isPlaceholderData) {
if (apiQuery.isPlaceholderData || !publicClient) {
return;
}

Expand All @@ -117,7 +122,7 @@ export default function useBlockWithdrawalsQuery({ heightOrHash, blockQuery, tab
const isRpcQuery = Boolean((
blockQuery.isDegradedData ||
((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0)
) && rpcQuery.data);
) && rpcQuery.data && publicClient);

const rpcQueryWithPages: QueryWithPagesResult<'block_withdrawals'> = React.useMemo(() => {
return {
Expand Down
12 changes: 6 additions & 6 deletions ui/pages/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const AddressPageContent = () => {
const userOpsAccountQuery = useApiQuery('user_ops_account', {
pathParams: { hash },
queryOptions: {
enabled: Boolean(hash),
enabled: Boolean(hash) && config.features.userOps.isEnabled,
placeholderData: USER_OPS_ACCOUNT,
},
});
Expand Down Expand Up @@ -160,16 +160,18 @@ const AddressPageContent = () => {
].filter(Boolean);
}, [ addressQuery.data, contractTabs, addressTabsCountersQuery.data, userOpsAccountQuery.data ]);

const isLoading = addressQuery.isPlaceholderData || (config.features.userOps.isEnabled && userOpsAccountQuery.isPlaceholderData);

const tags = (
<EntityTags
data={ addressQuery.data }
isLoading={ addressQuery.isPlaceholderData }
isLoading={ isLoading }
tagsBefore={ [
!addressQuery.data?.is_contract ? { label: 'eoa', display_name: 'EOA' } : undefined,
addressQuery.data?.implementation_address ? { label: 'proxy', display_name: 'Proxy' } : undefined,
addressQuery.data?.token ? { label: 'token', display_name: 'Token' } : undefined,
isSafeAddress ? { label: 'safe', display_name: 'Multisig: Safe' } : undefined,
userOpsAccountQuery.data ? { label: 'user_ops_acc', display_name: 'Smart contract wallet' } : undefined,
config.features.userOps.isEnabled && userOpsAccountQuery.data ? { label: 'user_ops_acc', display_name: 'Smart contract wallet' } : undefined,
] }
/>
);
Expand All @@ -189,8 +191,6 @@ const AddressPageContent = () => {
};
}, [ appProps.referrer ]);

const isLoading = addressQuery.isPlaceholderData;

const titleSecondRow = (
<Flex alignItems="center" w="100%" columnGap={ 2 } rowGap={ 2 } flexWrap={{ base: 'wrap', lg: 'nowrap' }}>
{ addressQuery.data?.ens_domain_name && (
Expand Down Expand Up @@ -241,7 +241,7 @@ const AddressPageContent = () => {
<AddressDetails addressQuery={ addressQuery } scrollRef={ tabsScrollRef }/>
{ /* should stay before tabs to scroll up with pagination */ }
<Box ref={ tabsScrollRef }></Box>
{ (addressQuery.isPlaceholderData || addressTabsCountersQuery.isPlaceholderData || userOpsAccountQuery.isPlaceholderData) ?
{ (isLoading || addressTabsCountersQuery.isPlaceholderData) ?
<TabsSkeleton tabs={ tabs }/> :
content
}
Expand Down
9 changes: 6 additions & 3 deletions ui/pages/Transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import config from 'configs/app';
import { useAppContext } from 'lib/contexts/app';
import throwOnResourceLoadError from 'lib/errors/throwOnResourceLoadError';
import getQueryParamString from 'lib/router/getQueryParamString';
import { publicClient } from 'lib/web3/client';
import TextAd from 'ui/shared/ad/TextAd';
import EntityTags from 'ui/shared/EntityTags';
import PageTitle from 'ui/shared/Page/PageTitle';
Expand All @@ -33,7 +34,7 @@ const TransactionPageContent = () => {
const txQuery = useTxQuery();
const { data, isPlaceholderData, isError, error, errorUpdateCount } = txQuery;

const showDegradedView = (isError || isPlaceholderData) && errorUpdateCount > 0;
const showDegradedView = publicClient && (isError || isPlaceholderData) && errorUpdateCount > 0;

const tabs: Array<RoutedTab> = (() => {
const detailsComponent = showDegradedView ?
Expand Down Expand Up @@ -97,8 +98,10 @@ const TransactionPageContent = () => {
return <RoutedTabs tabs={ tabs }/>;
})();

if (error?.status === 422) {
throwOnResourceLoadError({ resource: 'tx', error, isError: true });
if (isError && !showDegradedView) {
if (error?.status === 422 || error?.status === 404) {
throwOnResourceLoadError({ resource: 'tx', error, isError: true });
}
}

return (
Expand Down
5 changes: 5 additions & 0 deletions ui/tx/TxDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import TestnetWarning from 'ui/shared/alerts/TestnetWarning';
import DataFetchAlert from 'ui/shared/DataFetchAlert';

import TxInfo from './details/TxInfo';
import type { TxQuery } from './useTxQuery';
Expand All @@ -10,6 +11,10 @@ interface Props {
}

const TxDetails = ({ txQuery }: Props) => {
if (txQuery.isError) {
return <DataFetchAlert/>;
}

return (
<>
<TestnetWarning mb={ 6 } isLoading={ txQuery.isPlaceholderData }/>
Expand Down
4 changes: 4 additions & 0 deletions ui/tx/TxDetailsDegraded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const TxDetailsDegraded = ({ hash, txQuery }: Props) => {
const query = useQuery<RpcResponseType, unknown, Transaction | null>({
queryKey: [ 'RPC', 'tx', { hash } ],
queryFn: async() => {
if (!publicClient) {
throw new Error('No public RPC client');
}

const tx = await publicClient.getTransaction({ hash: hash as `0x${ string }` });

if (!tx) {
Expand Down
5 changes: 5 additions & 0 deletions ui/tx/TxUserOps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';

import { USER_OPS_ITEM } from 'stubs/userOps';
import { generateListStub } from 'stubs/utils';
import DataFetchAlert from 'ui/shared/DataFetchAlert';
import useQueryWithPages from 'ui/shared/pagination/useQueryWithPages';
import TxPendingAlert from 'ui/tx/TxPendingAlert';
import TxSocketAlert from 'ui/tx/TxSocketAlert';
Expand All @@ -28,6 +29,10 @@ const TxUserOps = ({ txQuery }: Props) => {
return txQuery.socketStatus ? <TxSocketAlert status={ txQuery.socketStatus }/> : <TxPendingAlert/>;
}

if (txQuery.isError) {
return <DataFetchAlert/>;
}

return <UserOpsContent query={ userOpsQuery } showTx={ false }/>;
};

Expand Down
2 changes: 1 addition & 1 deletion ui/userOps/UserOpsContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const UserOpsContent = ({ query, showTx = true, showSender = true }: Props) => {
<Hide below="lg" ssr={ false }>
<UserOpsTable
items={ query.data.items }
top={ query.pagination.isVisible ? 0 : 80 }
top={ query.pagination.isVisible ? 80 : 0 }
isLoading={ query.isPlaceholderData }
showTx={ showTx }
showSender={ showSender }
Expand Down
Loading