From 9f743d54a4bd7e49ec0da220d372db648a907279 Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Tue, 4 Feb 2025 01:55:40 -0500 Subject: [PATCH 1/5] Enhance useNetworkEnsAvatar to validate chainId and fallback to mainnet if not supported --- src/hooks/useNetworkEnsAvatar.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/hooks/useNetworkEnsAvatar.ts b/src/hooks/useNetworkEnsAvatar.ts index 243742c87..3ff7eab84 100644 --- a/src/hooks/useNetworkEnsAvatar.ts +++ b/src/hooks/useNetworkEnsAvatar.ts @@ -1,3 +1,4 @@ +import { mainnet } from 'viem/chains'; import { useEnsAvatar } from 'wagmi'; import { supportedEnsNetworks, @@ -11,11 +12,18 @@ interface UseNetworkEnsAvatarProps { export function useNetworkEnsAvatar(props?: UseNetworkEnsAvatarProps) { const { chain } = useNetworkConfigStore(); - const propsOrFallbackChainId = props?.chainId ?? chain.id; - if (!supportedEnsNetworks.includes(propsOrFallbackChainId)) { - throw new Error(`ENS is not supported for chain ${propsOrFallbackChainId}`); + let effectiveChainId: number; + + if (props?.chainId !== undefined) { + if (!supportedEnsNetworks.includes(props.chainId)) { + throw new Error(`ENS is not supported for chain ${props.chainId}`); + } + effectiveChainId = props.chainId; + } else { + // Use the network's chain id if supported, otherwise fallback to mainnet. + effectiveChainId = supportedEnsNetworks.includes(chain.id) ? chain.id : mainnet.id; } - return useEnsAvatar({ name: props?.name, chainId: propsOrFallbackChainId }); + return useEnsAvatar({ name: props?.name, chainId: effectiveChainId }); } From cdc01384040a0ff767a3d76ab35b6b1d6fba61e3 Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Tue, 4 Feb 2025 01:55:37 -0500 Subject: [PATCH 2/5] Enhance useNetworkEnsName to validate chainId and fallback to mainnet if not supported --- src/hooks/useNetworkEnsName.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/hooks/useNetworkEnsName.ts b/src/hooks/useNetworkEnsName.ts index 5aa1d4957..288ad5a81 100644 --- a/src/hooks/useNetworkEnsName.ts +++ b/src/hooks/useNetworkEnsName.ts @@ -13,13 +13,20 @@ interface UseNetworkEnsNameProps { export function useNetworkEnsName(props?: UseNetworkEnsNameProps) { const { chain } = useNetworkConfigStore(); - const propsOrFallbackChainId = props?.chainId ?? chain.id; - if (!supportedEnsNetworks.includes(propsOrFallbackChainId)) { - throw new Error(`ENS is not supported for chain ${propsOrFallbackChainId}`); + let effectiveChainId: number; + + if (props?.chainId !== undefined) { + if (!supportedEnsNetworks.includes(props.chainId)) { + throw new Error(`ENS is not supported for chain ${props.chainId}`); + } + effectiveChainId = props.chainId; + } else { + // No explicit chainId: use network chain id if supported, otherwise fallback to mainnet. + effectiveChainId = supportedEnsNetworks.includes(chain.id) ? chain.id : mainnet.id; } - return useEnsName({ address: props?.address, chainId: propsOrFallbackChainId }); + return useEnsName({ address: props?.address, chainId: effectiveChainId }); } export function useNetworkEnsNameAsync() { From 993d817247d13a1d86fb4ab1e92b50afde4bfd76 Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Tue, 4 Feb 2025 01:57:04 -0500 Subject: [PATCH 3/5] Enhance useNetworkEnsAddress to validate chainId and fallback to mainnet if not supported --- src/hooks/useNetworkEnsAddress.ts | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/hooks/useNetworkEnsAddress.ts b/src/hooks/useNetworkEnsAddress.ts index 493b1a9b3..ebb663faa 100644 --- a/src/hooks/useNetworkEnsAddress.ts +++ b/src/hooks/useNetworkEnsAddress.ts @@ -1,5 +1,6 @@ import { useCallback } from 'react'; import { createPublicClient, http } from 'viem'; +import { mainnet } from 'viem/chains'; import { useEnsAddress } from 'wagmi'; import { supportedEnsNetworks, @@ -13,13 +14,18 @@ interface UseNetworkEnsAddressProps { export function useNetworkEnsAddress(props?: UseNetworkEnsAddressProps) { const { chain } = useNetworkConfigStore(); - const propsOrFallbackChainId = props?.chainId ?? chain.id; - if (!supportedEnsNetworks.includes(propsOrFallbackChainId)) { - throw new Error(`ENS is not supported for chain ${propsOrFallbackChainId}`); + let effectiveChainId: number; + if (props?.chainId !== undefined) { + if (!supportedEnsNetworks.includes(props.chainId)) { + throw new Error(`ENS is not supported for chain ${props.chainId}`); + } + effectiveChainId = props.chainId; + } else { + effectiveChainId = supportedEnsNetworks.includes(chain.id) ? chain.id : mainnet.id; } - return useEnsAddress({ name: props?.name, chainId: propsOrFallbackChainId }); + return useEnsAddress({ name: props?.name, chainId: effectiveChainId }); } export function useNetworkEnsAddressAsync() { @@ -27,12 +33,17 @@ export function useNetworkEnsAddressAsync() { const getEnsAddress = useCallback( (args: { name: string; chainId?: number }) => { - const propsOrFallbackChainId = args?.chainId ?? chain.id; - if (!supportedEnsNetworks.includes(propsOrFallbackChainId)) { - throw new Error(`ENS is not supported for chain ${propsOrFallbackChainId}`); + let effectiveChainId: number; + if (args.chainId !== undefined) { + if (!supportedEnsNetworks.includes(args.chainId)) { + throw new Error(`ENS is not supported for chain ${args.chainId}`); + } + effectiveChainId = args.chainId; + } else { + effectiveChainId = supportedEnsNetworks.includes(chain.id) ? chain.id : mainnet.id; } - const networkConfig = getConfigByChainId(propsOrFallbackChainId); + const networkConfig = getConfigByChainId(effectiveChainId); const publicClient = createPublicClient({ chain: networkConfig.chain, transport: http(networkConfig.rpcEndpoint), From df440ed07e5a07443b6648ae9560f712c9644363 Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Tue, 4 Feb 2025 01:49:43 -0500 Subject: [PATCH 4/5] Refactor useNetworkEnsNameAsync to handle chainId more robustly and fallback to mainnet if not provided --- src/hooks/useNetworkEnsName.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/hooks/useNetworkEnsName.ts b/src/hooks/useNetworkEnsName.ts index 288ad5a81..305f20e05 100644 --- a/src/hooks/useNetworkEnsName.ts +++ b/src/hooks/useNetworkEnsName.ts @@ -1,5 +1,6 @@ import { useCallback } from 'react'; import { Address, createPublicClient, http } from 'viem'; +import { mainnet } from 'viem/chains'; import { useEnsName } from 'wagmi'; import { supportedEnsNetworks, @@ -34,12 +35,19 @@ export function useNetworkEnsNameAsync() { const getEnsName = useCallback( (args: { address: Address; chainId?: number }) => { - const propsOrFallbackChainId = args?.chainId ?? chain.id; - if (!supportedEnsNetworks.includes(propsOrFallbackChainId)) { - throw new Error(`ENS is not supported for chain ${propsOrFallbackChainId}`); + let effectiveChainId: number; + + if (args.chainId !== undefined) { + if (!supportedEnsNetworks.includes(args.chainId)) { + throw new Error(`ENS is not supported for chain ${args.chainId}`); + } + effectiveChainId = args.chainId; + } else { + // No chain id provided: try to use network chain id, otherwise fallback to mainnet. + effectiveChainId = supportedEnsNetworks.includes(chain.id) ? chain.id : mainnet.id; } - const networkConfig = getConfigByChainId(propsOrFallbackChainId); + const networkConfig = getConfigByChainId(effectiveChainId); const publicClient = createPublicClient({ chain: networkConfig.chain, transport: http(networkConfig.rpcEndpoint), From 0d36269fca228cf0877592d0afa674a40dcc1d9f Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Tue, 4 Feb 2025 14:18:29 -0500 Subject: [PATCH 5/5] bump version v0.5.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 73b75453c..592e7dd7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "decent-interface", - "version": "0.5.0", + "version": "0.5.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "decent-interface", - "version": "0.5.0", + "version": "0.5.1", "hasInstallScript": true, "dependencies": { "@amplitude/analytics-browser": "^2.11.1", diff --git a/package.json b/package.json index 0506a5515..2fd1ac2ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "decent-interface", - "version": "0.5.0", + "version": "0.5.1", "private": true, "dependencies": { "@amplitude/analytics-browser": "^2.11.1",