77} from 'viem' ;
88import { getPublicClient } from './clients.js' ;
99import { readContract } from './contracts.js' ;
10+ import { resolveAddress } from './ens.js' ;
1011
1112// Standard ERC20 ABI (minimal for reading)
1213const erc20Abi = [
@@ -66,27 +67,37 @@ const erc1155Abi = [
6667] as const ;
6768
6869/**
69- * Get the ETH balance of an address for a specific network
70+ * Get the ETH balance for an address
71+ * @param addressOrEns Ethereum address or ENS name
72+ * @param network Network name or chain ID
73+ * @returns Balance in wei and ether
7074 */
7175export async function getETHBalance (
72- address : Address ,
76+ addressOrEns : string ,
7377 network = 'ethereum'
7478) : Promise < { wei : bigint ; ether : string } > {
79+ // Resolve ENS name to address if needed
80+ const address = await resolveAddress ( addressOrEns , network ) ;
81+
7582 const client = getPublicClient ( network ) ;
76- const balanceWei = await client . getBalance ( { address } ) ;
83+ const balance = await client . getBalance ( { address } ) ;
7784
7885 return {
79- wei : balanceWei ,
80- ether : formatEther ( balanceWei )
86+ wei : balance ,
87+ ether : formatEther ( balance )
8188 } ;
8289}
8390
8491/**
85- * Get the ERC20 token balance of an address for a specific network
92+ * Get the balance of an ERC20 token for an address
93+ * @param tokenAddressOrEns Token contract address or ENS name
94+ * @param ownerAddressOrEns Owner address or ENS name
95+ * @param network Network name or chain ID
96+ * @returns Token balance with formatting information
8697 */
8798export async function getERC20Balance (
88- tokenAddress : Address ,
89- ownerAddress : Address ,
99+ tokenAddressOrEns : string ,
100+ ownerAddressOrEns : string ,
90101 network = 'ethereum'
91102) : Promise < {
92103 raw : bigint ;
@@ -96,6 +107,10 @@ export async function getERC20Balance(
96107 decimals : number ;
97108 }
98109} > {
110+ // Resolve ENS names to addresses if needed
111+ const tokenAddress = await resolveAddress ( tokenAddressOrEns , network ) ;
112+ const ownerAddress = await resolveAddress ( ownerAddressOrEns , network ) ;
113+
99114 const publicClient = getPublicClient ( network ) ;
100115
101116 const contract = getContract ( {
@@ -122,65 +137,83 @@ export async function getERC20Balance(
122137
123138/**
124139 * Check if an address owns a specific NFT
140+ * @param tokenAddressOrEns NFT contract address or ENS name
141+ * @param ownerAddressOrEns Owner address or ENS name
142+ * @param tokenId Token ID to check
143+ * @param network Network name or chain ID
144+ * @returns True if the address owns the NFT
125145 */
126146export async function isNFTOwner (
127- tokenAddress : Address ,
128- ownerAddress : Address ,
147+ tokenAddressOrEns : string ,
148+ ownerAddressOrEns : string ,
129149 tokenId : bigint ,
130150 network = 'ethereum'
131151) : Promise < boolean > {
132- const publicClient = getPublicClient ( network ) ;
133-
134- const contract = getContract ( {
135- address : tokenAddress ,
136- abi : erc721Abi ,
137- client : publicClient ,
138- } ) ;
139-
152+ // Resolve ENS names to addresses if needed
153+ const tokenAddress = await resolveAddress ( tokenAddressOrEns , network ) ;
154+ const ownerAddress = await resolveAddress ( ownerAddressOrEns , network ) ;
155+
140156 try {
141- const owner = await contract . read . ownerOf ( [ tokenId ] ) ;
142- return owner . toLowerCase ( ) === ownerAddress . toLowerCase ( ) ;
143- } catch ( error ) {
144- // If the token doesn't exist or there's an error, return false
157+ const actualOwner = await readContract ( {
158+ address : tokenAddress ,
159+ abi : erc721Abi ,
160+ functionName : 'ownerOf' ,
161+ args : [ tokenId ]
162+ } , network ) as Address ;
163+
164+ return actualOwner . toLowerCase ( ) === ownerAddress . toLowerCase ( ) ;
165+ } catch ( error : any ) {
166+ console . error ( `Error checking NFT ownership: ${ error . message } ` ) ;
145167 return false ;
146168 }
147169}
148170
149171/**
150- * Get ERC721 NFT balance for an address (number of NFTs owned)
172+ * Get the number of NFTs owned by an address for a specific collection
173+ * @param tokenAddressOrEns NFT contract address or ENS name
174+ * @param ownerAddressOrEns Owner address or ENS name
175+ * @param network Network name or chain ID
176+ * @returns Number of NFTs owned
151177 */
152178export async function getERC721Balance (
153- tokenAddress : Address ,
154- ownerAddress : Address ,
179+ tokenAddressOrEns : string ,
180+ ownerAddressOrEns : string ,
155181 network = 'ethereum'
156182) : Promise < bigint > {
157- const publicClient = getPublicClient ( network ) ;
158-
159- const contract = getContract ( {
183+ // Resolve ENS names to addresses if needed
184+ const tokenAddress = await resolveAddress ( tokenAddressOrEns , network ) ;
185+ const ownerAddress = await resolveAddress ( ownerAddressOrEns , network ) ;
186+
187+ return readContract ( {
160188 address : tokenAddress ,
161189 abi : erc721Abi ,
162- client : publicClient ,
163- } ) ;
164-
165- return contract . read . balanceOf ( [ ownerAddress ] ) ;
190+ functionName : 'balanceOf' ,
191+ args : [ ownerAddress ]
192+ } , network ) as Promise < bigint > ;
166193}
167194
168195/**
169- * Get ERC1155 token balance
196+ * Get the balance of an ERC1155 token for an address
197+ * @param tokenAddressOrEns ERC1155 contract address or ENS name
198+ * @param ownerAddressOrEns Owner address or ENS name
199+ * @param tokenId Token ID to check
200+ * @param network Network name or chain ID
201+ * @returns Token balance
170202 */
171203export async function getERC1155Balance (
172- tokenAddress : Address ,
173- ownerAddress : Address ,
204+ tokenAddressOrEns : string ,
205+ ownerAddressOrEns : string ,
174206 tokenId : bigint ,
175207 network = 'ethereum'
176208) : Promise < bigint > {
177- const publicClient = getPublicClient ( network ) ;
178-
179- const contract = getContract ( {
209+ // Resolve ENS names to addresses if needed
210+ const tokenAddress = await resolveAddress ( tokenAddressOrEns , network ) ;
211+ const ownerAddress = await resolveAddress ( ownerAddressOrEns , network ) ;
212+
213+ return readContract ( {
180214 address : tokenAddress ,
181215 abi : erc1155Abi ,
182- client : publicClient ,
183- } ) ;
184-
185- return contract . read . balanceOf ( [ ownerAddress , tokenId ] ) ;
216+ functionName : 'balanceOf' ,
217+ args : [ ownerAddress , tokenId ]
218+ } , network ) as Promise < bigint > ;
186219}
0 commit comments