Skip to content

Commit

Permalink
Don't cache things when doing ENS name network lookups, let wagmi/vie…
Browse files Browse the repository at this point in the history
…m handle caching
  • Loading branch information
adamgall committed May 1, 2024
1 parent e300a1f commit 46b0c42
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/hooks/utils/cache/cacheDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export enum CacheExpiry {
*/
export enum CacheKeys {
FAVORITES = 'favorites',
ENS_RESOLVE_PREFIX = 'ens_resolve_', // name.eth -> 0x0 caching
DECODED_TRANSACTION_PREFIX = 'decode_trans_',
MULTISIG_METADATA_PREFIX = 'm_m_',
MASTER_COPY_PREFIX = 'master_copy_of_',
Expand Down
32 changes: 2 additions & 30 deletions src/hooks/utils/useAddress.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { useEffect, useState } from 'react';
import { getAddress, isAddress } from 'viem';
import { useEthersProvider } from '../../providers/Ethers/hooks/useEthersProvider';
import { useNetworkConfig } from '../../providers/NetworkConfig/NetworkConfigProvider';
import { couldBeENS } from '../../utils/url';
import { CacheKeys, CacheExpiry } from './cache/cacheDefaults';
import { useLocalStorage } from './cache/useLocalStorage';

const useAddress = (addressInput: string | undefined) => {
const provider = useEthersProvider();

const [address, setAddress] = useState<string>();
const [isValidAddress, setIsValidAddress] = useState<boolean>();
const [isAddressLoading, setIsAddressLoading] = useState<boolean>(false);
const { setValue, getValue } = useLocalStorage();
const { chain } = useNetworkConfig();

useEffect(() => {
setIsAddressLoading(true);
Expand Down Expand Up @@ -49,21 +44,6 @@ const useAddress = (addressInput: string | undefined) => {
return;
}

// check our cache for a potential resolved address (name.eth -> 0x0)
const cachedResolvedAddress = getValue(addressInput);
if (cachedResolvedAddress) {
setAddress(cachedResolvedAddress);
setIsValidAddress(true);
setIsAddressLoading(false);
return;
} else if (cachedResolvedAddress === undefined) {
// a previous lookup did not resolve
setAddress(addressInput);
setIsValidAddress(false);
setIsAddressLoading(false);
return;
}

if (!provider) {
setAddress(addressInput);
setIsValidAddress(undefined);
Expand All @@ -73,19 +53,11 @@ const useAddress = (addressInput: string | undefined) => {

provider
.resolveName(addressInput)
.then((resolvedAddress: any) => {
.then(resolvedAddress => {
if (!resolvedAddress) {
// cache an unresolved address as 'undefined' for 20 minutes
setValue(CacheKeys.ENS_RESOLVE_PREFIX + addressInput, undefined, 20);
setAddress(addressInput);
setIsValidAddress(false);
} else {
// cache a resolved address for a week
setValue(
CacheKeys.ENS_RESOLVE_PREFIX + addressInput,
resolvedAddress,
CacheExpiry.ONE_WEEK,
);
setAddress(resolvedAddress);
setIsValidAddress(true);
}
Expand All @@ -97,7 +69,7 @@ const useAddress = (addressInput: string | undefined) => {
.finally(() => {
setIsAddressLoading(false);
});
}, [provider, addressInput, getValue, setValue, chain]);
}, [addressInput, provider]);

return { address, isValidAddress, isAddressLoading };
};
Expand Down

0 comments on commit 46b0c42

Please sign in to comment.