Skip to content

Commit

Permalink
Merge pull request #2710 from decentdao/hotfix/v0.5.1
Browse files Browse the repository at this point in the history
Hotfix/v0.5.1
  • Loading branch information
adamgall authored Feb 4, 2025
2 parents 5171602 + 0d36269 commit 25786dd
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "decent-interface",
"version": "0.5.0",
"version": "0.5.1",
"private": true,
"dependencies": {
"@amplitude/analytics-browser": "^2.11.1",
Expand Down
27 changes: 19 additions & 8 deletions src/hooks/useNetworkEnsAddress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from 'react';
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
import { useEnsAddress } from 'wagmi';
import {
supportedEnsNetworks,
Expand All @@ -13,26 +14,36 @@ 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() {
const { chain, getConfigByChainId } = useNetworkConfigStore();

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),
Expand Down
16 changes: 12 additions & 4 deletions src/hooks/useNetworkEnsAvatar.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mainnet } from 'viem/chains';
import { useEnsAvatar } from 'wagmi';
import {
supportedEnsNetworks,
Expand All @@ -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 });
}
31 changes: 23 additions & 8 deletions src/hooks/useNetworkEnsName.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,26 +14,40 @@ 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() {
const { chain, getConfigByChainId } = useNetworkConfigStore();

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),
Expand Down

0 comments on commit 25786dd

Please sign in to comment.