Skip to content

Commit

Permalink
Use chainId instead of chain
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer committed Aug 2, 2024
1 parent 9a16b9a commit 9538ec4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/identity/hooks/useName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { getName } from '../utils/getName';
* - `{UseQueryResult}`: The rest of useQuery return values. including isLoading, isError, error, isFetching, refetch, etc.
*/
export const useName = (
{ address, chain = mainnet }: UseNameOptions,
{ address, chainId = mainnet.id }: UseNameOptions,
queryOptions?: UseNameQueryOptions,
) => {
const { enabled = true, cacheTime } = queryOptions ?? {};
const ensActionKey = `ens-name-${address}-${chain.id}`;
const ensActionKey = `ens-name-${address}-${chainId}`;
return useQuery<GetNameReturnType>({
queryKey: ['useName', ensActionKey],
queryFn: async () => {
return await getName({ address, chain });
return await getName({ address, chainId });
},
gcTime: cacheTime,
enabled,
Expand Down
4 changes: 2 additions & 2 deletions src/identity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export type GetAvatarReturnType = string | null;
*/
export type GetName = {
address: Address;
chain?: Chain;
chainId?: number; // Optional chainId for domain resolution
};

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ export type UseAttestations = {
*/
export type UseNameOptions = {
address: Address; // The Ethereum address for which the ENS name is to be fetched.
chain?: Chain; // Optional chain for domain resolution
chainId: number; // chainId for domain resolution
};

/**
Expand Down
16 changes: 8 additions & 8 deletions src/identity/utils/getName.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ describe('getName', () => {
it('should return mainnet username', async () => {
const expectedEnsName = 'leo.eth';
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chain: mainnet });
const name = await getName({ address: walletAddress, chainId: mainnet.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(mainnet);

Check failure on line 63 in src/identity/utils/getName.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/identity/utils/getName.test.tsx > getName > should return mainnet username

AssertionError: expected "spy" to be called with arguments: [ { formatters: undefined, …(8) } ] Received: 1st spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.etherscan.io/api", - "name": "Etherscan", - "url": "https://etherscan.io", - }, - }, - "contracts": Object { - "ensRegistry": Object { - "address": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", - }, - "ensUniversalResolver": Object { - "address": "0xce01f8eee7E479C928F8919abD53E553a36CeF67", - "blockCreated": 19258213, - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 14353601, - }, - }, - "fees": undefined, - "formatters": undefined, - "id": 1, - "name": "Ethereum", - "nativeCurrency": Object { - "decimals": 18, - "name": "Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://cloudflare-eth.com", - ], - }, - }, - "serializers": undefined, - }, + 1, ] 2nd spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.etherscan.io/api", - "name": "Etherscan", - "url": "https://etherscan.io", - }, - }, - "contracts": Object { - "ensRegistry": Object { - "address": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", - }, - "ensUniversalResolver": Object { - "address": "0xce01f8eee7E479C928F8919abD53E553a36CeF67", - "blockCreated": 19258213, - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 14353601, - }, - }, - "fees": undefined, - "formatters": undefined, - "id": 1, - "name": "Ethereum", - "nativeCurrency": Object { - "decimals": 18, - "name": "Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://cloudflare-eth.com", - ], - }, - }, - "serializers": undefined, - }, + 1, ] Number of calls: 2 ❯ src/identity/utils/getName.test.tsx:63:34
});

it('should return sepolia username', async () => {
const expectedEnsName = 'leo.test.eth';
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chain: sepolia });
const name = await getName({ address: walletAddress, chainId: sepolia.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(sepolia);

Check failure on line 71 in src/identity/utils/getName.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/identity/utils/getName.test.tsx > getName > should return sepolia username

AssertionError: expected "spy" to be called with arguments: [ { formatters: undefined, …(9) } ] Received: 1st spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api-sepolia.etherscan.io/api", - "name": "Etherscan", - "url": "https://sepolia.etherscan.io", - }, - }, - "contracts": Object { - "ensRegistry": Object { - "address": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", - }, - "ensUniversalResolver": Object { - "address": "0xc8Af999e38273D658BE1b921b88A9Ddf005769cC", - "blockCreated": 5317080, - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 751532, - }, - }, - "fees": undefined, - "formatters": undefined, - "id": 11155111, - "name": "Sepolia", - "nativeCurrency": Object { - "decimals": 18, - "name": "Sepolia Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://rpc.sepolia.org", - ], - }, - }, - "serializers": undefined, - "testnet": true, - }, + 11155111, ] 2nd spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api-sepolia.etherscan.io/api", - "name": "Etherscan", - "url": "https://sepolia.etherscan.io", - }, - }, - "contracts": Object { - "ensRegistry": Object { - "address": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", - }, - "ensUniversalResolver": Object { - "address": "0xc8Af999e38273D658BE1b921b88A9Ddf005769cC", - "blockCreated": 5317080, - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 751532, - }, - }, - "fees": undefined, - "formatters": undefined, - "id": 11155111, - "name": "Sepolia", - "nativeCurrency": Object { - "decimals": 18, - "name": "Sepolia Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://rpc.sepolia.org", - ], - }, - }, - "serializers": undefined, - "testnet": true, - }, + 1, ] Number of calls: 2 ❯ src/identity/utils/getName.test.tsx:71:34
});
Expand All @@ -76,7 +76,7 @@ describe('getName', () => {
mockReadContract.mockResolvedValue(expectedEnsName);
const name = await getName({
address: walletAddress,
chain: baseSepolia,
chainId: baseSepolia.id,
});
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(baseSepolia);

Check failure on line 82 in src/identity/utils/getName.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/identity/utils/getName.test.tsx > getName > should return custom testnet chain username

AssertionError: expected "spy" to be called with arguments: [ { formatters: { …(3) }, …(11) } ] Received: 1st spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api-sepolia.basescan.org/api", - "name": "Basescan", - "url": "https://sepolia.basescan.org", - }, - }, - "contracts": Object { - "disputeGameFactory": Object { - "11155111": Object { - "address": "0xd6E6dBf4F7EA0ac412fD8b65ED297e64BB7a06E1", - }, - }, - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "11155111": Object { - "address": "0xfd0Bf71F60660E2f608ed56e1659C450eB113120", - "blockCreated": 4446677, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "11155111": Object { - "address": "0x84457ca9D0163FbC4bbfe4Dfbb20ba46e48DF254", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 1059647, - }, - "portal": Object { - "11155111": Object { - "address": "0x49f53e41452c74589e85ca1677426ba426459e85", - "blockCreated": 4446677, - }, - }, - }, - "fees": undefined, - "formatters": Object { - "block": Object { - "exclude": undefined, - "format": [Function format], - "type": "block", - }, - "transaction": Object { - "exclude": undefined, - "format": [Function format], - "type": "transaction", - }, - "transactionReceipt": Object { - "exclude": undefined, - "format": [Function format], - "type": "transactionReceipt", - }, - }, - "id": 84532, - "name": "Base Sepolia", - "nativeCurrency": Object { - "decimals": 18, - "name": "Sepolia Ether", - "symbol": "ETH", - }, - "network": "base-sepolia", - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://sepolia.base.org", - ], - }, - }, - "serializers": Object { - "transaction": [Function serializeTransaction], - }, - "sourceId": 11155111, - "testnet": true, - }, + 84532, ] Number of calls: 1 ❯ src/identity/utils/getName.test.tsx:82:34
Expand All @@ -85,7 +85,7 @@ describe('getName', () => {
it('should return custom mainnet username', async () => {
const expectedEnsName = 'leo.custommainnet.eth';
mockReadContract.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chain: base });
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);

Check failure on line 90 in src/identity/utils/getName.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/identity/utils/getName.test.tsx > getName > should return custom mainnet username

AssertionError: expected "spy" to be called with arguments: [ { formatters: { …(3) }, …(9) } ] Received: 1st spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.basescan.org/api", - "name": "Basescan", - "url": "https://basescan.org", - }, - }, - "contracts": Object { - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "1": Object { - "address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35", - "blockCreated": 17482143, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "1": Object { - "address": "0x56315b90c40730925ec5485cf004d835058518A0", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 5022, - }, - "portal": Object { - "1": Object { - "address": "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e", - "blockCreated": 17482143, - }, - }, - }, - "fees": undefined, - "formatters": Object { - "block": Object { - "exclude": undefined, - "format": [Function format], - "type": "block", - }, - "transaction": Object { - "exclude": undefined, - "format": [Function format], - "type": "transaction", - }, - "transactionReceipt": Object { - "exclude": undefined, - "format": [Function format], - "type": "transactionReceipt", - }, - }, - "id": 8453, - "name": "Base", - "nativeCurrency": Object { - "decimals": 18, - "name": "Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://mainnet.base.org", - ], - }, - }, - "serializers": Object { - "transaction": [Function serializeTransaction], - }, - "sourceId": 1, - }, + 8453, ] Number of calls: 1 ❯ src/identity/utils/getName.test.tsx:90:34
});
Expand All @@ -94,7 +94,7 @@ describe('getName', () => {
const expectedEnsName = null;
mockReadContract.mockResolvedValue(expectedEnsName);
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chain: base });
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);

Check failure on line 99 in src/identity/utils/getName.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/identity/utils/getName.test.tsx > getName > should return null if user is not registered

AssertionError: expected "spy" to be called with arguments: [ { formatters: { …(3) }, …(9) } ] Received: 1st spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.basescan.org/api", - "name": "Basescan", - "url": "https://basescan.org", - }, - }, - "contracts": Object { - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "1": Object { - "address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35", - "blockCreated": 17482143, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "1": Object { - "address": "0x56315b90c40730925ec5485cf004d835058518A0", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 5022, - }, - "portal": Object { - "1": Object { - "address": "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e", - "blockCreated": 17482143, - }, - }, - }, - "fees": undefined, - "formatters": Object { - "block": Object { - "exclude": undefined, - "format": [Function format], - "type": "block", - }, - "transaction": Object { - "exclude": undefined, - "format": [Function format], - "type": "transaction", - }, - "transactionReceipt": Object { - "exclude": undefined, - "format": [Function format], - "type": "transactionReceipt", - }, - }, - "id": 8453, - "name": "Base", - "nativeCurrency": Object { - "decimals": 18, - "name": "Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://mainnet.base.org", - ], - }, - }, - "serializers": Object { - "transaction": [Function serializeTransaction], - }, - "sourceId": 1, - }, + 8453, ] 2nd spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.basescan.org/api", - "name": "Basescan", - "url": "https://basescan.org", - }, - }, - "contracts": Object { - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "1": Object { - "address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35", - "blockCreated": 17482143, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "1": Object { - "address": "0x56315b90c40730925ec5485cf004d835058518A0", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", -
});
Expand All @@ -104,14 +104,14 @@ describe('getName', () => {
const expectedEnsName = 'registered.eth';
mockReadContract.mockResolvedValue(expectedBaseName);
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chain: base });
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);

Check failure on line 109 in src/identity/utils/getName.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/identity/utils/getName.test.tsx > getName > should default to ENS name if custom chain name is not registered

AssertionError: expected "spy" to be called with arguments: [ { formatters: { …(3) }, …(9) } ] Received: 1st spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.basescan.org/api", - "name": "Basescan", - "url": "https://basescan.org", - }, - }, - "contracts": Object { - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "1": Object { - "address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35", - "blockCreated": 17482143, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "1": Object { - "address": "0x56315b90c40730925ec5485cf004d835058518A0", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 5022, - }, - "portal": Object { - "1": Object { - "address": "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e", - "blockCreated": 17482143, - }, - }, - }, - "fees": undefined, - "formatters": Object { - "block": Object { - "exclude": undefined, - "format": [Function format], - "type": "block", - }, - "transaction": Object { - "exclude": undefined, - "format": [Function format], - "type": "transaction", - }, - "transactionReceipt": Object { - "exclude": undefined, - "format": [Function format], - "type": "transactionReceipt", - }, - }, - "id": 8453, - "name": "Base", - "nativeCurrency": Object { - "decimals": 18, - "name": "Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://mainnet.base.org", - ], - }, - }, - "serializers": Object { - "transaction": [Function serializeTransaction], - }, - "sourceId": 1, - }, + 8453, ] 2nd spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.basescan.org/api", - "name": "Basescan", - "url": "https://basescan.org", - }, - }, - "contracts": Object { - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "1": Object { - "address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35", - "blockCreated": 17482143, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "1": Object { - "address": "0x56315b90c40730925ec5485cf004d835058518A0", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", -
});

it('should throw an error on unsupported chain', async () => {
await expect(
getName({ address: walletAddress, chain: optimism }),
getName({ address: walletAddress, chainId: optimism.id }),
).rejects.toBe(
'ChainId not supported, name resolution is only supported on Ethereum and Base.',
);
Expand All @@ -121,7 +121,7 @@ describe('getName', () => {
const expectedEnsName = 'zizzamia.eth';
mockReadContract.mockRejectedValue(new Error('This is an error'));
mockGetEnsName.mockResolvedValue(expectedEnsName);
const name = await getName({ address: walletAddress, chain: base });
const name = await getName({ address: walletAddress, chainId: base.id });
expect(name).toBe(expectedEnsName);
expect(getChainPublicClient).toHaveBeenCalledWith(base);

Check failure on line 126 in src/identity/utils/getName.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/identity/utils/getName.test.tsx > getName > should default to ENS when readContract throws an error

AssertionError: expected "spy" to be called with arguments: [ { formatters: { …(3) }, …(9) } ] Received: 1st spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.basescan.org/api", - "name": "Basescan", - "url": "https://basescan.org", - }, - }, - "contracts": Object { - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "1": Object { - "address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35", - "blockCreated": 17482143, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "1": Object { - "address": "0x56315b90c40730925ec5485cf004d835058518A0", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", - "blockCreated": 5022, - }, - "portal": Object { - "1": Object { - "address": "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e", - "blockCreated": 17482143, - }, - }, - }, - "fees": undefined, - "formatters": Object { - "block": Object { - "exclude": undefined, - "format": [Function format], - "type": "block", - }, - "transaction": Object { - "exclude": undefined, - "format": [Function format], - "type": "transaction", - }, - "transactionReceipt": Object { - "exclude": undefined, - "format": [Function format], - "type": "transactionReceipt", - }, - }, - "id": 8453, - "name": "Base", - "nativeCurrency": Object { - "decimals": 18, - "name": "Ether", - "symbol": "ETH", - }, - "rpcUrls": Object { - "default": Object { - "http": Array [ - "https://mainnet.base.org", - ], - }, - }, - "serializers": Object { - "transaction": [Function serializeTransaction], - }, - "sourceId": 1, - }, + 8453, ] 2nd spy call: Array [ - Object { - "blockExplorers": Object { - "default": Object { - "apiUrl": "https://api.basescan.org/api", - "name": "Basescan", - "url": "https://basescan.org", - }, - }, - "contracts": Object { - "gasPriceOracle": Object { - "address": "0x420000000000000000000000000000000000000F", - }, - "l1Block": Object { - "address": "0x4200000000000000000000000000000000000015", - }, - "l1StandardBridge": Object { - "1": Object { - "address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35", - "blockCreated": 17482143, - }, - }, - "l2CrossDomainMessenger": Object { - "address": "0x4200000000000000000000000000000000000007", - }, - "l2Erc721Bridge": Object { - "address": "0x4200000000000000000000000000000000000014", - }, - "l2OutputOracle": Object { - "1": Object { - "address": "0x56315b90c40730925ec5485cf004d835058518A0", - }, - }, - "l2StandardBridge": Object { - "address": "0x4200000000000000000000000000000000000010", - }, - "l2ToL1MessagePasser": Object { - "address": "0x4200000000000000000000000000000000000016", - }, - "multicall3": Object { - "address": "0xca11bde05977b3631167028862be2a173976ca11", -
});
Expand Down
12 changes: 6 additions & 6 deletions src/identity/utils/getName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { convertReverseNodeToBytes } from './convertReverseNodeToBytes';

export const getName = async ({
address,
chain = mainnet,
chainId = mainnet.id,
}: GetName): Promise<GetNameReturnType> => {
const chainIsBase = isBase({ chainId: chain.id });
const chainIsEthereum = isEthereum({ chainId: chain.id });
const chainIsBase = isBase({ chainId });
const chainIsEthereum = isEthereum({ chainId });
const chainSupportsUniversalResolver = chainIsEthereum || chainIsBase;

if (!chainSupportsUniversalResolver) {
Expand All @@ -27,14 +27,14 @@ export const getName = async ({
);
}

let client = getChainPublicClient(chain);
let client = getChainPublicClient(chainId);

if (chainIsBase) {
const addressReverseNode = convertReverseNodeToBytes(address, base.id);
try {
const baseName = await client.readContract({
abi: L2ResolverAbi,
address: RESOLVER_ADDRESSES_BY_CHAIN_ID[chain.id],
address: RESOLVER_ADDRESSES_BY_CHAIN_ID[chainId],
functionName: 'name',
args: [addressReverseNode],
});
Expand All @@ -47,7 +47,7 @@ export const getName = async ({
}

// Default to mainnet
client = getChainPublicClient(mainnet);
client = getChainPublicClient(mainnet.id);
// ENS username
const ensName = await client.getEnsName({
address,
Expand Down
16 changes: 14 additions & 2 deletions src/network/getChainPublicClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { http, createPublicClient } from 'viem';
import type { Chain } from 'viem/chains';
import { type Chain, base, baseSepolia, goerli, mainnet } from 'viem/chains';

const supportedChains = [mainnet, goerli, base, baseSepolia];

function getChainFromId(chainId: number): Chain | undefined {
return supportedChains.find((chain) => chain.id === chainId);
}

export function getChainPublicClient(chainId: number) {
const chain = getChainFromId(chainId);

if (!chain) {
throw new Error(`Unsupported chain ID: ${chainId}`);

Check failure on line 14 in src/network/getChainPublicClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/network/getChainPublicClient.test.ts > getChainPublicClient > should return a public client matching the given chain

Error: Unsupported chain ID: [object Object] ❯ Module.getChainPublicClient src/network/getChainPublicClient.ts:14:11 ❯ src/network/getChainPublicClient.test.ts:14:26
}

export function getChainPublicClient(chain: Chain) {
return createPublicClient({
chain: chain,
transport: http(),
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/components/WalletDropdownBaseName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export function WalletDropdownBaseName({
className,
}: WalletDropdownBaseNameReact) {
const { address } = useAccount();
const { chain } = useWalletContext();
const { chainId } = useWalletContext();

if (!address || !chain) {
if (!address || !chainId) {
return null;
}

const { data: baseName, isLoading } = useName({
address,
chain,
chainId,
});

const hasBaseUserName = !!baseName;
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserOperation } from 'permissionless';
import type { Dispatch, ReactNode, SetStateAction } from 'react';
import type { Address, Chain, PublicClient } from 'viem';
import type { Address, PublicClient } from 'viem';
import type { UseBalanceReturnType, UseReadContractReturnType } from 'wagmi';
import type { SwapError } from '../swap';

Expand Down Expand Up @@ -61,7 +61,7 @@ export type UseGetTokenBalanceResponse = {
*/
export type WalletContextType = {
address?: Address | null; // The Ethereum address to fetch the avatar and name for.
chain?: Chain; // Optional chain for domain resolution
chainId?: number; // Optional chainId for domain resolution
isOpen: boolean;
setIsOpen: Dispatch<SetStateAction<boolean>>;
};
Expand Down

0 comments on commit 9538ec4

Please sign in to comment.