7
7
} from 'viem' ;
8
8
import { getPublicClient } from './clients.js' ;
9
9
import { readContract } from './contracts.js' ;
10
+ import { resolveAddress } from './ens.js' ;
10
11
11
12
// Standard ERC20 ABI (minimal for reading)
12
13
const erc20Abi = [
@@ -66,27 +67,37 @@ const erc1155Abi = [
66
67
] as const ;
67
68
68
69
/**
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
70
74
*/
71
75
export async function getETHBalance (
72
- address : Address ,
76
+ addressOrEns : string ,
73
77
network = 'ethereum'
74
78
) : Promise < { wei : bigint ; ether : string } > {
79
+ // Resolve ENS name to address if needed
80
+ const address = await resolveAddress ( addressOrEns , network ) ;
81
+
75
82
const client = getPublicClient ( network ) ;
76
- const balanceWei = await client . getBalance ( { address } ) ;
83
+ const balance = await client . getBalance ( { address } ) ;
77
84
78
85
return {
79
- wei : balanceWei ,
80
- ether : formatEther ( balanceWei )
86
+ wei : balance ,
87
+ ether : formatEther ( balance )
81
88
} ;
82
89
}
83
90
84
91
/**
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
86
97
*/
87
98
export async function getERC20Balance (
88
- tokenAddress : Address ,
89
- ownerAddress : Address ,
99
+ tokenAddressOrEns : string ,
100
+ ownerAddressOrEns : string ,
90
101
network = 'ethereum'
91
102
) : Promise < {
92
103
raw : bigint ;
@@ -96,6 +107,10 @@ export async function getERC20Balance(
96
107
decimals : number ;
97
108
}
98
109
} > {
110
+ // Resolve ENS names to addresses if needed
111
+ const tokenAddress = await resolveAddress ( tokenAddressOrEns , network ) ;
112
+ const ownerAddress = await resolveAddress ( ownerAddressOrEns , network ) ;
113
+
99
114
const publicClient = getPublicClient ( network ) ;
100
115
101
116
const contract = getContract ( {
@@ -122,65 +137,83 @@ export async function getERC20Balance(
122
137
123
138
/**
124
139
* 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
125
145
*/
126
146
export async function isNFTOwner (
127
- tokenAddress : Address ,
128
- ownerAddress : Address ,
147
+ tokenAddressOrEns : string ,
148
+ ownerAddressOrEns : string ,
129
149
tokenId : bigint ,
130
150
network = 'ethereum'
131
151
) : 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
+
140
156
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 } ` ) ;
145
167
return false ;
146
168
}
147
169
}
148
170
149
171
/**
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
151
177
*/
152
178
export async function getERC721Balance (
153
- tokenAddress : Address ,
154
- ownerAddress : Address ,
179
+ tokenAddressOrEns : string ,
180
+ ownerAddressOrEns : string ,
155
181
network = 'ethereum'
156
182
) : 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 ( {
160
188
address : tokenAddress ,
161
189
abi : erc721Abi ,
162
- client : publicClient ,
163
- } ) ;
164
-
165
- return contract . read . balanceOf ( [ ownerAddress ] ) ;
190
+ functionName : 'balanceOf' ,
191
+ args : [ ownerAddress ]
192
+ } , network ) as Promise < bigint > ;
166
193
}
167
194
168
195
/**
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
170
202
*/
171
203
export async function getERC1155Balance (
172
- tokenAddress : Address ,
173
- ownerAddress : Address ,
204
+ tokenAddressOrEns : string ,
205
+ ownerAddressOrEns : string ,
174
206
tokenId : bigint ,
175
207
network = 'ethereum'
176
208
) : 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 ( {
180
214
address : tokenAddress ,
181
215
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 > ;
186
219
}
0 commit comments