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

We only really need to search for ENS names on mainnet #2636

Merged
merged 1 commit into from
Dec 16, 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
18 changes: 1 addition & 17 deletions src/components/ui/page/Global/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as amplitude from '@amplitude/analytics-browser';
import * as Sentry from '@sentry/react';
import { useEffect, useState } from 'react';
import { createPublicClient, http, PublicClient } from 'viem';
import { useAccount } from 'wagmi';
import { useAccountFavorites } from '../../../../hooks/DAO/loaders/useFavorites';
import {
Expand Down Expand Up @@ -36,8 +35,6 @@ const useUpdateFavoritesCache = (onFavoritesUpdated: () => void) => {

useEffect(() => {
(async () => {
const publicClientsByChain = new Map<number, PublicClient>();

const favoriteNames = await Promise.all(
favoritesList.map(async favorite => {
const favoriteChain = wagmiConfig.chains.find(
Expand All @@ -54,22 +51,9 @@ const useUpdateFavoritesCache = (onFavoritesUpdated: () => void) => {
return;
}

let favoritePublicClient = publicClientsByChain.get(favoriteChain.id);

if (!favoritePublicClient) {
favoritePublicClient = createPublicClient({
chain: favoriteChain,
transport: http(favoriteNetwork.rpcEndpoint),
});
publicClientsByChain.set(favoriteChain.id, favoritePublicClient);
}

const networkConfig = getNetworkConfig(favoriteChain.id);

return Promise.all([
favorite,
getSafeName(favoritePublicClient, networkConfig.subgraph, favorite.address),
]);
return Promise.all([favorite, getSafeName(networkConfig.subgraph, favorite.address)]);
}),
);

Expand Down
34 changes: 16 additions & 18 deletions src/hooks/utils/useGetSafeName.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { useCallback } from 'react';
import { Address, ChainDoesNotSupportContract, PublicClient } from 'viem';
import { usePublicClient } from 'wagmi';
import { Address, http, createPublicClient } from 'viem';
import { DAOQueryDocument } from '../../../.graphclient';
import graphQLClient from '../../graphql';
import { useNetworkConfigStore } from '../../providers/NetworkConfig/useNetworkConfigStore';
import {
supportedNetworks,
useNetworkConfigStore,
} from '../../providers/NetworkConfig/useNetworkConfigStore';
import { createAccountSubstring } from './useGetAccountName';

export const getSafeName = async (
publicClient: PublicClient,
subgraph: { space: number; slug: string; version: string },
address: Address,
) => {
const ensName = await publicClient.getEnsName({ address }).catch((error: Error) => {
if (error.name === ChainDoesNotSupportContract.name) {
// Sliently fail, this is fine.
// https://github.com/wevm/viem/discussions/781
} else {
throw error;
}
});
const mainnet = supportedNetworks.find(network => network.chain.id === 1);
if (!mainnet) {
throw new Error('Mainnet not found');
}

const mainnetPublicClient = createPublicClient({
chain: mainnet.chain,
transport: http(mainnet.rpcEndpoint),
});
const ensName = await mainnetPublicClient.getEnsName({ address });
if (ensName) {
return ensName;
}
Expand All @@ -45,17 +47,13 @@ export const getSafeName = async (

export const useGetSafeName = (chainId?: number) => {
const { getConfigByChainId } = useNetworkConfigStore();
const publicClient = usePublicClient({ chainId });

return {
getSafeName: useCallback(
(address: Address) => {
if (!publicClient) {
throw new Error('Public client not available');
}
return getSafeName(publicClient, getConfigByChainId(chainId).subgraph, address);
return getSafeName(getConfigByChainId(chainId).subgraph, address);
},
[publicClient, getConfigByChainId, chainId],
[chainId, getConfigByChainId],
),
};
};
Loading