From 49fe1c6307dd484da9b2f73a388eee2253e291f0 Mon Sep 17 00:00:00 2001 From: Tom Meagher Date: Sun, 30 Oct 2022 17:45:06 -0400 Subject: [PATCH 1/2] refactor: remove addressOrName --- docs/components/web3/Account.tsx | 2 +- docs/pages/docs/hooks/useBalance.en-US.mdx | 32 +++---- docs/pages/docs/hooks/useEnsAvatar.en-US.mdx | 24 ++--- docs/pages/docs/migration-guide.en-US.mdx | 48 +++++++++- docs/pages/examples/connect-wallet.en-US.mdx | 6 +- examples/_dev/src/components/Account.tsx | 2 +- examples/_dev/src/components/Balance.tsx | 8 +- .../src/actions/accounts/fetchBalance.test.ts | 74 +++------------- .../core/src/actions/accounts/fetchBalance.ts | 37 ++------ .../src/actions/ens/fetchEnsAvatar.test.ts | 16 ++-- .../core/src/actions/ens/fetchEnsAvatar.ts | 8 +- .../src/hooks/accounts/useBalance.test.ts | 88 +++++++------------ .../react/src/hooks/accounts/useBalance.ts | 19 ++-- .../react/src/hooks/ens/useEnsAvatar.test.ts | 18 ++-- packages/react/src/hooks/ens/useEnsAvatar.ts | 18 ++-- 15 files changed, 175 insertions(+), 225 deletions(-) diff --git a/docs/components/web3/Account.tsx b/docs/components/web3/Account.tsx index c5fabc03ac..ea1f39a3fb 100644 --- a/docs/components/web3/Account.tsx +++ b/docs/components/web3/Account.tsx @@ -13,7 +13,7 @@ export function Account() { chainId: 1, }) const { data: ensAvatarData } = useEnsAvatar({ - addressOrName: address, + address, chainId: 1, }) const { disconnect } = useDisconnect() diff --git a/docs/pages/docs/hooks/useBalance.en-US.mdx b/docs/pages/docs/hooks/useBalance.en-US.mdx index 1f64bd7ef8..b950ec99d6 100644 --- a/docs/pages/docs/hooks/useBalance.en-US.mdx +++ b/docs/pages/docs/hooks/useBalance.en-US.mdx @@ -18,7 +18,7 @@ import { useBalance } from 'wagmi' function App() { const { data, isError, isLoading } = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', }) if (isLoading) return
Fetching balance…
@@ -65,16 +65,16 @@ function App() { ## Configuration -### addressOrName (optional) +### address (optional) -Address or ENS name to fetch balance for. If `addressOrName` is not defined, hook will not run. +Address to fetch balance for. If `address` is not defined, hook will not run. ```tsx {5} import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', }) } ``` @@ -88,7 +88,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, }) } @@ -103,7 +103,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', formatUnits: 'gwei', }) } @@ -111,14 +111,14 @@ function App() { ### token (optional) -Address for ERC-20 token. If `token` is provided, hook fetches token balance instead of Ethereum balance. For example, we can fetch `awkweb.eth`'s current [$UNI](https://etherscan.io/address/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984) balance. +Address for ERC-20 token. If `token` is provided, hook fetches token balance instead of Ethereum balance. For example, we can fetch `0xA0Cf798816D4b9b9866b5330EEa46a18382f251e`'s current [$UNI](https://etherscan.io/address/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984) balance. ```tsx {6} import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', token: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', }) } @@ -133,7 +133,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', watch: true, }) } @@ -148,7 +148,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', cacheTime: 2_000, }) } @@ -163,7 +163,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', enabled: false, }) } @@ -178,7 +178,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', staleTime: 2_000, }) } @@ -193,7 +193,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', suspense: true, }) } @@ -208,7 +208,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', onError(error) { console.log('Error', error) }, @@ -225,7 +225,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', onSettled(data, error) { console.log('Settled', { data, error }) }, @@ -242,7 +242,7 @@ import { useBalance } from 'wagmi' function App() { const balance = useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', onSuccess(data) { console.log('Success', data) }, diff --git a/docs/pages/docs/hooks/useEnsAvatar.en-US.mdx b/docs/pages/docs/hooks/useEnsAvatar.en-US.mdx index 104570d9af..da2ea06614 100644 --- a/docs/pages/docs/hooks/useEnsAvatar.en-US.mdx +++ b/docs/pages/docs/hooks/useEnsAvatar.en-US.mdx @@ -18,7 +18,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const { data, isError, isLoading } = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', }) if (isLoading) return
Fetching avatar…
@@ -51,16 +51,16 @@ function App() { ## Configuration -### addressOrName (optional) +### address (optional) -Address or ENS name to fetch avatar for. If `addressOrName` is not defined, hook will not run. +Address or ENS name to fetch avatar for. If `address` is not defined, hook will not run. ```tsx {5} import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'moxey.eth', + address: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', }) } ``` @@ -74,7 +74,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, }) } @@ -89,7 +89,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', cacheTime: 2_000, }) } @@ -104,7 +104,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', enabled: false, }) } @@ -119,7 +119,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', staleTime: 2_000, }) } @@ -134,7 +134,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', suspense: true, }) } @@ -149,7 +149,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', onSuccess(data) { console.log('Success', data) }, @@ -166,7 +166,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', onError(error) { console.log('Error', error) }, @@ -183,7 +183,7 @@ import { useEnsAvatar } from 'wagmi' function App() { const ensAvatar = useEnsAvatar({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', onSettled(data, error) { console.log('Settled', { data, error }) }, diff --git a/docs/pages/docs/migration-guide.en-US.mdx b/docs/pages/docs/migration-guide.en-US.mdx index 57a45692f4..91f14e0a32 100644 --- a/docs/pages/docs/migration-guide.en-US.mdx +++ b/docs/pages/docs/migration-guide.en-US.mdx @@ -20,6 +20,52 @@ If you are coming from an earlier version of `wagmi`, you will need to make sure wagmi no longer supports CommonJS and only supports [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). If you are using modern tooling, like [Next.js](https://nextjs.org), [Vite](https://vitejs.dev), or [Vitest](https://vitest.dev), you likely don't need to do anything! [Remix](https://remix.run/docs/en/v1/pages/gotchas#importing-esm-packages) and [Jest](https://jestjs.io/docs/ecmascript-modules) require some additional configuration. Check out [this guide](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) for more info on ESM support and Frequently Asked Questions across various tools and setups. +### `useBalance` + +#### Configuration changes + +`addressOrName` renamed to `address`. + +```diff +const result = useBalance({ +- addressOrName: '0x…', ++ address: '0x…', +}) +``` + +If you were using an ENS name instead of an address, you can resolve the name to an address before passing it to the action. + +```diff ++ const { data: address } = useEnsAddress({ name: 'example.eth' }) +const result = useBalance({ +- addressOrName: 'example.eth', ++ address, +}) +``` + +### `useEnsAvatar` + +#### Configuration changes + +`addressOrName` renamed to `address`. + +```diff +const result = useEnsAvatar({ +- addressOrName: '0x…', ++ address: '0x…', +}) +``` + +If you were using an ENS name instead of an address, you can resolve the name to an address before passing it to the action. + +```diff ++ const { data: address } = useEnsAddress({ name: 'example.eth' }) +const result = useEnsAvatar({ +- addressOrName: 'example.eth', ++ address, +}) +``` + ## 0.7.x Breaking changes @@ -70,7 +116,7 @@ If you were using an ENS name instead of an address, you can resolve the name to ```diff - import { useContractRead } from 'wagmi' + import { useContractRead, useEnsAddress } from 'wagmi' -+ const { data: address} = useEnsAddress({ name: 'example.eth'}) ++ const { data: address } = useEnsAddress({ name: 'example.eth' }) const result = useContractRead({ - addressOrName: 'example.eth', + address, diff --git a/docs/pages/examples/connect-wallet.en-US.mdx b/docs/pages/examples/connect-wallet.en-US.mdx index 5080906215..a7127f86d2 100644 --- a/docs/pages/examples/connect-wallet.en-US.mdx +++ b/docs/pages/examples/connect-wallet.en-US.mdx @@ -15,7 +15,9 @@ The example below uses [`useConnect`](/docs/hooks/useConnect), [`useAccount`](/d - Check out [ConnectKit](https://docs.family.co/connectkit?utm_source=wagmi-dev) to get started with pre-built interface on top of wagmi for managing wallet connections. + Check out [ConnectKit](https://docs.family.co/connectkit?utm_source=wagmi-dev) + to get started with pre-built interface on top of wagmi for managing wallet + connections. ## Step 1: Configuring Connectors @@ -132,7 +134,7 @@ import { export function Profile() { const { address, connector, isConnected } = useAccount() - const { data: ensAvatar } = useEnsAvatar({ addressOrName: address }) + const { data: ensAvatar } = useEnsAvatar({ address }) const { data: ensName } = useEnsName({ address }) const { connect, connectors, error, isLoading, pendingConnector } = useConnect() diff --git a/examples/_dev/src/components/Account.tsx b/examples/_dev/src/components/Account.tsx index dfedf773c9..c519e2de7d 100644 --- a/examples/_dev/src/components/Account.tsx +++ b/examples/_dev/src/components/Account.tsx @@ -21,7 +21,7 @@ export const Account = () => { onDisconnect: () => console.log('disconnected'), }) const ensAvatar = useEnsAvatar({ - addressOrName: account?.address, + address: account?.address, chainId: 1, }) const ensName = useEnsName({ address: account?.address, chainId: 1 }) diff --git a/examples/_dev/src/components/Balance.tsx b/examples/_dev/src/components/Balance.tsx index 9d05df8ba8..a7e9f3105b 100644 --- a/examples/_dev/src/components/Balance.tsx +++ b/examples/_dev/src/components/Balance.tsx @@ -1,5 +1,5 @@ import { useState } from 'react' -import { useAccount, useBalance } from 'wagmi' +import { Address, useAccount, useBalance } from 'wagmi' export const Balance = () => { return ( @@ -17,7 +17,7 @@ export const Balance = () => { const AccountBalance = () => { const { address } = useAccount() const { data, refetch } = useBalance({ - addressOrName: address, + address, watch: true, }) @@ -31,7 +31,9 @@ const AccountBalance = () => { const FindBalance = () => { const [address, setAddress] = useState('') - const { data, isLoading, refetch } = useBalance({ addressOrName: address }) + const { data, isLoading, refetch } = useBalance({ + address: address as Address, + }) const [value, setValue] = useState('') diff --git a/packages/core/src/actions/accounts/fetchBalance.test.ts b/packages/core/src/actions/accounts/fetchBalance.test.ts index 79d89c1921..4b725b730c 100644 --- a/packages/core/src/actions/accounts/fetchBalance.test.ts +++ b/packages/core/src/actions/accounts/fetchBalance.test.ts @@ -10,31 +10,12 @@ describe('fetchBalance', () => { setupClient() }) - describe('addressOrName', () => { - it('address', async () => { - expect( - await fetchBalance({ - addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', - }), - ).toMatchInlineSnapshot(` - { - "decimals": 18, - "formatted": "0.40742480512617271", - "symbol": "ETH", - "value": { - "hex": "0x05a776b39e3a7026", - "type": "BigNumber", - }, - } - `) - }) - - it('name', async () => { - expect( - await fetchBalance({ - addressOrName: 'awkweb.eth', - }), - ).toMatchInlineSnapshot(` + it('address', async () => { + expect( + await fetchBalance({ + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + }), + ).toMatchInlineSnapshot(` { "decimals": 18, "formatted": "0.40742480512617271", @@ -45,13 +26,12 @@ describe('fetchBalance', () => { }, } `) - }) }) it('chainId', async () => { expect( await fetchBalance({ - addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, }), ).toMatchInlineSnapshot(` @@ -70,7 +50,7 @@ describe('fetchBalance', () => { it('formatUnits', async () => { expect( await fetchBalance({ - addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', formatUnits: 'gwei', }), ).toMatchInlineSnapshot(` @@ -90,7 +70,7 @@ describe('fetchBalance', () => { it('address', async () => { expect( await fetchBalance({ - addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', token: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', }), ).toMatchInlineSnapshot(` @@ -109,7 +89,7 @@ describe('fetchBalance', () => { it('bytes32 contract', async () => { expect( await fetchBalance({ - addressOrName: '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2', + address: '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2', token: '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2', }), ).toMatchInlineSnapshot(` @@ -124,38 +104,6 @@ describe('fetchBalance', () => { } `) }) - - describe('name', () => { - it('valid', async () => { - expect( - await fetchBalance({ - addressOrName: 'awkweb.eth', - token: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', - }), - ).toMatchInlineSnapshot(` - { - "decimals": 18, - "formatted": "18.0553", - "symbol": "UNI", - "value": { - "hex": "0xfa914fb05d1c4000", - "type": "BigNumber", - }, - } - `) - }) - - it('not configured', async () => { - await expect( - fetchBalance({ - addressOrName: 'thisnamedoesnotexist.eth', - token: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', - }), - ).rejects.toThrowErrorMatchingInlineSnapshot( - '"ENS name not configured (operation=\\"resolveName(\\\\\\"thisnamedoesnotexist.eth\\\\\\")\\", code=UNSUPPORTED_OPERATION, version=ethers/5.7.1)"', - ) - }) - }) }) }) @@ -163,7 +111,7 @@ describe('fetchBalance', () => { it('token with less than 18 decimals formats units correctly', async () => { expect( await fetchBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', token: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', }), ).toMatchInlineSnapshot(` diff --git a/packages/core/src/actions/accounts/fetchBalance.ts b/packages/core/src/actions/accounts/fetchBalance.ts index cc111af724..33e240648a 100644 --- a/packages/core/src/actions/accounts/fetchBalance.ts +++ b/packages/core/src/actions/accounts/fetchBalance.ts @@ -1,11 +1,5 @@ import { Address, ResolvedConfig } from 'abitype' -import { logger } from 'ethers' -import { - Logger, - formatUnits, - isAddress, - parseBytes32String, -} from 'ethers/lib/utils.js' +import { formatUnits, parseBytes32String } from 'ethers/lib/utils.js' import { getClient } from '../../client' import { erc20ABI, erc20ABI_bytes32 } from '../../constants' @@ -15,8 +9,8 @@ import { readContracts } from '../contracts' import { getProvider } from '../providers' export type FetchBalanceArgs = { - /** Address or ENS name */ - addressOrName: string + /** Address of balance to check */ + address: Address /** Chain id to use for provider */ chainId?: number /** Units for formatting output */ @@ -33,7 +27,7 @@ export type FetchBalanceResult = { } export async function fetchBalance({ - addressOrName, + address, chainId, formatUnits: unit, token, @@ -42,25 +36,6 @@ export async function fetchBalance({ const provider = getProvider({ chainId }) if (token) { - // Convert ENS name to address if required - let resolvedAddress: Address - if (isAddress(addressOrName)) resolvedAddress = addressOrName - else { - const address = (await provider.resolveName( - addressOrName, - )) as Address | null - // Same error `provider.getBalance` throws for invalid ENS name - if (!address) - logger.throwError( - 'ENS name not configured', - Logger.errors.UNSUPPORTED_OPERATION, - { - operation: `resolveName(${JSON.stringify(addressOrName)})`, - }, - ) - resolvedAddress = address - } - type FetchContractBalance = { abi: typeof erc20ABI | typeof erc20ABI_bytes32 } @@ -72,7 +47,7 @@ export async function fetchBalance({ { ...erc20Config, functionName: 'balanceOf', - args: [resolvedAddress], + args: [address], }, { ...erc20Config, functionName: 'decimals' }, { ...erc20Config, functionName: 'symbol' }, @@ -106,7 +81,7 @@ export async function fetchBalance({ } const chains = [...(client.provider.chains || []), ...(client.chains ?? [])] - const value = await provider.getBalance(addressOrName) + const value = await provider.getBalance(address) const chain = chains.find((x) => x.id === provider.network.chainId) return { decimals: chain?.nativeCurrency?.decimals ?? 18, diff --git a/packages/core/src/actions/ens/fetchEnsAvatar.test.ts b/packages/core/src/actions/ens/fetchEnsAvatar.test.ts index 5ba385d92d..2c78698167 100644 --- a/packages/core/src/actions/ens/fetchEnsAvatar.test.ts +++ b/packages/core/src/actions/ens/fetchEnsAvatar.test.ts @@ -16,6 +16,7 @@ import { fetchEnsAvatar } from './fetchEnsAvatar' const handlers = [ // brantly.eth + // 0x983110309620d911731ac0932219af06091b6744 rest.get( 'https://wrappedpunks.com:3000/api/punks/metadata/2430', (_req, res, ctx) => @@ -32,6 +33,7 @@ const handlers = [ ), ), // nick.eth + // 0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5 rest.get( 'https://api.opensea.io/api/v1/metadata/0x495f947276749Ce646f68AC8c248420045cb7b5e/0x11ef687cfeb2e353670479f2dcc76af2bc6b3935000000000002c40000000001', (_req, res, ctx) => @@ -72,10 +74,12 @@ describe('fetchEnsAvatar', () => { afterAll(() => server.close()) describe('args', () => { - describe('addressOrName', () => { + describe('address', () => { it('no result', async () => { expect( - await fetchEnsAvatar({ addressOrName: 'awkweb.eth' }), + await fetchEnsAvatar({ + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + }), ).toMatchInlineSnapshot(`null`) }) @@ -83,7 +87,7 @@ describe('fetchEnsAvatar', () => { it('erc1155', async () => { expect( await fetchEnsAvatar({ - addressOrName: 'nick.eth', + address: '0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5', }), ).toMatchInlineSnapshot( `"https://lh3.googleusercontent.com/hKHZTZSTmcznonu8I6xcVZio1IF76fq0XmcxnvUykC-FGuVJ75UPdLDlKJsfgVXH9wOSmkyHw0C39VAYtsGyxT7WNybjQ6s3fM3macE"`, @@ -93,7 +97,7 @@ describe('fetchEnsAvatar', () => { it('erc721', async () => { expect( await fetchEnsAvatar({ - addressOrName: 'brantly.eth', + address: '0x983110309620d911731ac0932219af06091b6744', }), ).toMatchInlineSnapshot( `"https://api.wrappedpunks.com/images/punks/2430.png"`, @@ -103,7 +107,7 @@ describe('fetchEnsAvatar', () => { it('custom', async () => { expect( await fetchEnsAvatar({ - addressOrName: 'tanrikulu.eth', + address: '0x0d59d0f7dcc0fbf0a3305ce0261863aaf7ab685c', }), ).toMatchInlineSnapshot( `"https://ipfs.io/ipfs/QmUShgfoZQSHK3TQyuTfUpsc8UfeNfD8KwPUvDBUdZ4nmR"`, @@ -115,7 +119,7 @@ describe('fetchEnsAvatar', () => { it('chainId', async () => { expect( await fetchEnsAvatar({ - addressOrName: 'nick.eth', + address: '0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5', chainId: 1, }), ).toMatchInlineSnapshot( diff --git a/packages/core/src/actions/ens/fetchEnsAvatar.ts b/packages/core/src/actions/ens/fetchEnsAvatar.ts index 1f9b710d59..ff407ad380 100644 --- a/packages/core/src/actions/ens/fetchEnsAvatar.ts +++ b/packages/core/src/actions/ens/fetchEnsAvatar.ts @@ -1,8 +1,10 @@ +import { Address } from 'abitype' + import { getProvider } from '../providers' export type FetchEnsAvatarArgs = { /** Address or ENS name */ - addressOrName: string + address: Address /** Chain id to use for provider */ chainId?: number } @@ -10,12 +12,12 @@ export type FetchEnsAvatarArgs = { export type FetchEnsAvatarResult = string | null export async function fetchEnsAvatar({ - addressOrName, + address, chainId, }: FetchEnsAvatarArgs): Promise { const provider = getProvider({ chainId }) // TODO: Update with more advanced logic // https://github.com/ensdomains/ens-avatar - const avatar = await provider.getAvatar(addressOrName) + const avatar = await provider.getAvatar(address) return avatar } diff --git a/packages/react/src/hooks/accounts/useBalance.test.ts b/packages/react/src/hooks/accounts/useBalance.test.ts index f8225c325f..f411ffe77a 100644 --- a/packages/react/src/hooks/accounts/useBalance.test.ts +++ b/packages/react/src/hooks/accounts/useBalance.test.ts @@ -6,7 +6,7 @@ import { useBalance } from './useBalance' describe('useBalance', () => { it('mounts', async () => { const { result, waitFor } = renderHook(() => - useBalance({ addressOrName: 'awkweb.eth' }), + useBalance({ address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' }), ) await waitFor(() => expect(result.current.isSuccess).toBeTruthy(), { @@ -43,19 +43,18 @@ describe('useBalance', () => { }) describe('configuration', () => { - describe('addressOrName', () => { - it('address', async () => { - const { result, waitFor } = renderHook(() => - useBalance({ - addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', - }), - ) + it('address', async () => { + const { result, waitFor } = renderHook(() => + useBalance({ + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + }), + ) - await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) + await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { internal, ...res } = result.current - expect(res).toMatchInlineSnapshot(` + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { internal, ...res } = result.current + expect(res).toMatchInlineSnapshot(` { "data": { "decimals": 18, @@ -80,50 +79,14 @@ describe('useBalance', () => { "status": "success", } `) - }) - - it('name', async () => { - const { result, waitFor } = renderHook(() => - useBalance({ - addressOrName: 'medha.eth', - }), - ) - - await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) - - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { internal, ...res } = result.current - expect(res).toMatchInlineSnapshot(` - { - "data": { - "decimals": 18, - "formatted": "0.048320181048190068", - "symbol": "ETH", - "value": { - "hex": "0xabaaf2dadc2474", - "type": "BigNumber", - }, - }, - "error": null, - "fetchStatus": "idle", - "isError": false, - "isFetched": true, - "isFetchedAfterMount": true, - "isFetching": false, - "isIdle": false, - "isLoading": false, - "isRefetching": false, - "isSuccess": true, - "refetch": [Function], - "status": "success", - } - `) - }) }) it('chainId', async () => { const { result, waitFor } = renderHook(() => - useBalance({ chainId: 1, addressOrName: 'awkweb.eth' }), + useBalance({ + chainId: 1, + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + }), ) await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) @@ -160,7 +123,7 @@ describe('useBalance', () => { it('enabled', async () => { const { result, waitFor } = renderHook(() => useBalance({ - addressOrName: 'moxey.eth', + address: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', enabled: false, }), ) @@ -190,7 +153,10 @@ describe('useBalance', () => { it('formatUnits', async () => { const { result, waitFor } = renderHook(() => - useBalance({ addressOrName: 'awkweb.eth', formatUnits: 'gwei' }), + useBalance({ + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + formatUnits: 'gwei', + }), ) await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) @@ -227,7 +193,10 @@ describe('useBalance', () => { it('token', async () => { const ensTokenAddress = '0xc18360217d8f7ab5e7c516566761ea12ce7f9d72' const { result, waitFor } = renderHook(() => - useBalance({ addressOrName: 'awkweb.eth', token: ensTokenAddress }), + useBalance({ + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', + token: ensTokenAddress, + }), ) await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) @@ -265,7 +234,10 @@ describe('useBalance', () => { describe('return value', () => { it('refetch', async () => { const { result } = renderHook(() => - useBalance({ enabled: false, addressOrName: 'worm.eth' }), + useBalance({ + enabled: false, + address: '0xfb843f8c4992efdb6b42349c35f025ca55742d33', + }), ) await act(async () => { @@ -286,7 +258,7 @@ describe('useBalance', () => { }) describe('behavior', () => { - it('does nothing when `addressOrName` is missing', async () => { + it('does nothing when `address` is missing', async () => { const { result, waitFor } = renderHook(() => useBalance()) await waitFor(() => expect(result.current.isIdle).toBeTruthy()) @@ -315,7 +287,7 @@ describe('useBalance', () => { it('token', async () => { const { result, waitFor } = renderHook(() => useBalance({ - addressOrName: 'awkweb.eth', + address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', token: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', }), ) diff --git a/packages/react/src/hooks/accounts/useBalance.ts b/packages/react/src/hooks/accounts/useBalance.ts index 3fb612704f..74187c63db 100644 --- a/packages/react/src/hooks/accounts/useBalance.ts +++ b/packages/react/src/hooks/accounts/useBalance.ts @@ -13,24 +13,23 @@ export type UseBalanceArgs = Partial & { export type UseBalanceConfig = QueryConfig export const queryKey = ({ - addressOrName, + address, chainId, formatUnits, token, }: Partial & { chainId?: number -}) => - [{ entity: 'balance', addressOrName, chainId, formatUnits, token }] as const +}) => [{ entity: 'balance', address, chainId, formatUnits, token }] as const const queryFn = ({ - queryKey: [{ addressOrName, chainId, formatUnits, token }], + queryKey: [{ address, chainId, formatUnits, token }], }: QueryFunctionArgs) => { - if (!addressOrName) throw new Error('address is required') - return fetchBalance({ addressOrName, chainId, formatUnits, token }) + if (!address) throw new Error('address is required') + return fetchBalance({ address, chainId, formatUnits, token }) } export function useBalance({ - addressOrName, + address, cacheTime, chainId: chainId_, enabled = true, @@ -45,11 +44,11 @@ export function useBalance({ }: UseBalanceArgs & UseBalanceConfig = {}) { const chainId = useChainId({ chainId: chainId_ }) const balanceQuery = useQuery( - queryKey({ addressOrName, chainId, formatUnits, token }), + queryKey({ address, chainId, formatUnits, token }), queryFn, { cacheTime, - enabled: Boolean(enabled && addressOrName), + enabled: Boolean(enabled && address), staleTime, suspense, onError, @@ -63,7 +62,7 @@ export function useBalance({ if (!enabled) return if (!watch) return if (!blockNumber) return - if (!addressOrName) return + if (!address) return balanceQuery.refetch() // eslint-disable-next-line react-hooks/exhaustive-deps }, [blockNumber]) diff --git a/packages/react/src/hooks/ens/useEnsAvatar.test.ts b/packages/react/src/hooks/ens/useEnsAvatar.test.ts index 2ba55a7521..d0f130258c 100644 --- a/packages/react/src/hooks/ens/useEnsAvatar.test.ts +++ b/packages/react/src/hooks/ens/useEnsAvatar.test.ts @@ -23,7 +23,7 @@ const handlers = [ }), ), ), - // nick.eth + // 0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5 rest.get( 'https://api.opensea.io/api/v1/metadata/0x495f947276749Ce646f68AC8c248420045cb7b5e/0x11ef687cfeb2e353670479f2dcc76af2bc6b3935000000000002c40000000001', (_req, res, ctx) => @@ -63,7 +63,7 @@ describe('useEnsAvatar', () => { it('mounts', async () => { const { result, waitFor } = renderHook(() => - useEnsAvatar({ addressOrName: 'nick.eth' }), + useEnsAvatar({ address: '0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5' }), ) await waitFor(() => expect(result.current.isSuccess).toBeTruthy(), { @@ -92,11 +92,11 @@ describe('useEnsAvatar', () => { }) describe('configuration', () => { - describe('addressOrName', () => { + describe('address', () => { it('has avatar', async () => { const { result, waitFor } = renderHook(() => useEnsAvatar({ - addressOrName: 'nick.eth', + address: '0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5', }), ) @@ -128,7 +128,7 @@ describe('useEnsAvatar', () => { it('does not have avatar', async () => { const { result, waitFor } = renderHook(() => useEnsAvatar({ - addressOrName: '0x5FE6C3F8d12D5Ad1480F6DC01D8c7864Aa58C523', + address: '0x5FE6C3F8d12D5Ad1480F6DC01D8c7864Aa58C523', }), ) @@ -159,7 +159,7 @@ describe('useEnsAvatar', () => { it('chainId', async () => { const { result, waitFor } = renderHook(() => useEnsAvatar({ - addressOrName: 'nick.eth', + address: '0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5', chainId: 1, }), ) @@ -190,7 +190,7 @@ describe('useEnsAvatar', () => { it('enabled', async () => { const { result, waitFor } = renderHook(() => useEnsAvatar({ - addressOrName: 'brantly.eth', + address: '0x983110309620d911731ac0932219af06091b6744', enabled: false, }), ) @@ -223,7 +223,7 @@ describe('useEnsAvatar', () => { it('refetch', async () => { const { result } = renderHook(() => useEnsAvatar({ - addressOrName: 'nick.eth', + address: '0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5', enabled: false, }), ) @@ -238,7 +238,7 @@ describe('useEnsAvatar', () => { }) describe('behavior', () => { - it('does nothing when `addressOrName` is missing', async () => { + it('does nothing when `address` is missing', async () => { const { result, waitFor } = renderHook(() => useEnsAvatar()) await waitFor(() => expect(result.current.isIdle).toBeTruthy()) diff --git a/packages/react/src/hooks/ens/useEnsAvatar.ts b/packages/react/src/hooks/ens/useEnsAvatar.ts index dffbb95411..9f6fbd855b 100644 --- a/packages/react/src/hooks/ens/useEnsAvatar.ts +++ b/packages/react/src/hooks/ens/useEnsAvatar.ts @@ -12,22 +12,22 @@ export type UseEnsAvatarArgs = Partial export type UseEnsLookupConfig = QueryConfig export const queryKey = ({ - addressOrName, + address, chainId, }: { - addressOrName?: UseEnsAvatarArgs['addressOrName'] + address?: UseEnsAvatarArgs['address'] chainId?: number -}) => [{ entity: 'ensAvatar', addressOrName, chainId }] as const +}) => [{ entity: 'ensAvatar', address, chainId }] as const const queryFn = ({ - queryKey: [{ addressOrName, chainId }], + queryKey: [{ address, chainId }], }: QueryFunctionArgs) => { - if (!addressOrName) throw new Error('addressOrName is required') - return fetchEnsAvatar({ addressOrName, chainId }) + if (!address) throw new Error('address is required') + return fetchEnsAvatar({ address, chainId }) } export function useEnsAvatar({ - addressOrName, + address, cacheTime, chainId: chainId_, enabled = true, @@ -39,9 +39,9 @@ export function useEnsAvatar({ }: UseEnsAvatarArgs & UseEnsLookupConfig = {}) { const chainId = useChainId({ chainId: chainId_ }) - return useQuery(queryKey({ addressOrName, chainId }), queryFn, { + return useQuery(queryKey({ address, chainId }), queryFn, { cacheTime, - enabled: Boolean(enabled && addressOrName && chainId), + enabled: Boolean(enabled && address && chainId), staleTime, suspense, onError, From 423f573c643a2fe2886b18a0458931553705b8c9 Mon Sep 17 00:00:00 2001 From: Tom Meagher Date: Sun, 30 Oct 2022 17:51:27 -0400 Subject: [PATCH 2/2] chore: add changesets --- .changeset/chatty-horses-admire.md | 22 ++++++++++++++++++++++ .changeset/spotty-timers-prove.md | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 .changeset/chatty-horses-admire.md create mode 100644 .changeset/spotty-timers-prove.md diff --git a/.changeset/chatty-horses-admire.md b/.changeset/chatty-horses-admire.md new file mode 100644 index 0000000000..abf1fc0c07 --- /dev/null +++ b/.changeset/chatty-horses-admire.md @@ -0,0 +1,22 @@ +--- +'@wagmi/core': minor +--- + +**Breaking**: `addressOrName` renamed to `address` for `fetchBalance` and `fetchEnsAvatar`. + +```diff +const result = await fetchBalance({ +- addressOrName: '0x…', ++ address: '0x…', +}) +``` + +If you were using an ENS name instead of an address, you can resolve the name to an address before passing it to the action. + +```diff ++ const { data: address } = await fetchEnsAddress({ name: 'example.eth' }) +const result = await fetchBalance({ +- addressOrName: 'example.eth', ++ address, +}) +``` diff --git a/.changeset/spotty-timers-prove.md b/.changeset/spotty-timers-prove.md new file mode 100644 index 0000000000..647cf8b278 --- /dev/null +++ b/.changeset/spotty-timers-prove.md @@ -0,0 +1,22 @@ +--- +'wagmi': minor +--- + +**Breaking**: `addressOrName` renamed to `address` for `useBalance` and `useEnsAvatar`. + +```diff +const result = useBalance({ +- addressOrName: '0x…', ++ address: '0x…', +}) +``` + +If you were using an ENS name instead of an address, you can resolve the name to an address before passing it to the action. + +```diff ++ const { data: address } = useEnsAddress({ name: 'example.eth' }) +const result = useBalance({ +- addressOrName: 'example.eth', ++ address, +}) +```