@@ -115,14 +115,24 @@ const erc1155TransferAbi = [
115115
116116/**
117117 * Transfer ETH to an address
118+ * @param privateKey The private key of the sender
119+ * @param to The recipient address
120+ * @param amount The amount to transfer as a human-readable string in ETH (e.g. "0.1" for 0.1 ETH)
121+ * @param network The network to use
122+ * @returns Transaction hash
118123 */
119124export async function transferETH (
120- privateKey : Hex ,
125+ privateKey : string | Hex ,
121126 to : Address ,
122127 amount : string , // in ether
123128 network = 'ethereum'
124129) : Promise < Hash > {
125- const client = getWalletClient ( privateKey , network ) ;
130+ // Ensure the private key has 0x prefix
131+ const formattedKey = typeof privateKey === 'string' && ! privateKey . startsWith ( '0x' )
132+ ? `0x${ privateKey } ` as Hex
133+ : privateKey as Hex ;
134+
135+ const client = getWalletClient ( formattedKey , network ) ;
126136
127137 // Convert amount from ether to wei
128138 const value = parseEther ( amount ) ;
@@ -138,12 +148,18 @@ export async function transferETH(
138148
139149/**
140150 * Transfer ERC20 tokens from one address to another
151+ * @param tokenAddress The address of the ERC20 token contract
152+ * @param toAddress The recipient address
153+ * @param amount The amount to transfer as a human-readable string (e.g. "1.5" for 1.5 tokens)
154+ * @param privateKey The private key of the sender
155+ * @param network The network to use
156+ * @returns Transaction details
141157 */
142158export async function transferERC20 (
143159 tokenAddress : Address ,
144160 toAddress : Address ,
145161 amount : string ,
146- privateKey : `0x${string } `,
162+ privateKey : string | `0x${string } `,
147163 network : string = 'ethereum'
148164) : Promise < {
149165 txHash : Hash ;
@@ -156,8 +172,13 @@ export async function transferERC20(
156172 decimals : number ;
157173 } ;
158174} > {
175+ // Ensure the private key has 0x prefix
176+ const formattedKey = typeof privateKey === 'string' && ! privateKey . startsWith ( '0x' )
177+ ? `0x${ privateKey } ` as Hex
178+ : privateKey as Hex ;
179+
159180 const publicClient = getPublicClient ( network ) ;
160- const walletClient = getWalletClient ( privateKey , network ) ;
181+ const walletClient = getWalletClient ( formattedKey , network ) ;
161182 const account = walletClient . account ! ;
162183 const chain = getChain ( network ) ;
163184
@@ -201,12 +222,18 @@ export async function transferERC20(
201222
202223/**
203224 * Approve another address to spend a specific amount of tokens
225+ * @param tokenAddress The address of the ERC20 token contract
226+ * @param spenderAddress The address to approve for spending
227+ * @param amount The amount to approve as a human-readable string (e.g. "1.5" for 1.5 tokens)
228+ * @param privateKey The private key of the token owner
229+ * @param network The network to use
230+ * @returns Transaction details
204231 */
205232export async function approveERC20 (
206233 tokenAddress : Address ,
207234 spenderAddress : Address ,
208235 amount : string ,
209- privateKey : `0x${string } `,
236+ privateKey : string | `0x${string } `,
210237 network : string = 'ethereum'
211238) : Promise < {
212239 txHash : Hash ;
@@ -219,8 +246,13 @@ export async function approveERC20(
219246 decimals : number ;
220247 } ;
221248} > {
249+ // Ensure the private key has 0x prefix
250+ const formattedKey = typeof privateKey === 'string' && ! privateKey . startsWith ( '0x' )
251+ ? `0x${ privateKey } ` as Hex
252+ : privateKey as Hex ;
253+
222254 const publicClient = getPublicClient ( network ) ;
223- const walletClient = getWalletClient ( privateKey , network ) ;
255+ const walletClient = getWalletClient ( formattedKey , network ) ;
224256 const account = walletClient . account ! ;
225257 const chain = getChain ( network ) ;
226258
@@ -269,7 +301,7 @@ export async function transferERC721(
269301 tokenAddress : Address ,
270302 toAddress : Address ,
271303 tokenId : bigint ,
272- privateKey : `0x${string } `,
304+ privateKey : string | `0x${string } `,
273305 network : string = 'ethereum'
274306) : Promise < {
275307 txHash : Hash ;
@@ -279,8 +311,13 @@ export async function transferERC721(
279311 symbol : string ;
280312 } ;
281313} > {
314+ // Ensure the private key has 0x prefix
315+ const formattedKey = typeof privateKey === 'string' && ! privateKey . startsWith ( '0x' )
316+ ? `0x${ privateKey } ` as Hex
317+ : privateKey as Hex ;
318+
282319 const publicClient = getPublicClient ( network ) ;
283- const walletClient = getWalletClient ( privateKey , network ) ;
320+ const walletClient = getWalletClient ( formattedKey , network ) ;
284321 const account = walletClient . account ! ;
285322 const fromAddress = account . address ;
286323 const chain = getChain ( network ) ;
@@ -325,25 +362,40 @@ export async function transferERC721(
325362
326363/**
327364 * Transfer an ERC1155 token
365+ * @param tokenAddress The address of the ERC1155 token contract
366+ * @param toAddress The recipient address
367+ * @param tokenId The ID of the token to transfer
368+ * @param amount The amount to transfer as a string (ERC1155 tokens don't typically have decimals, but we use string for consistency)
369+ * @param privateKey The private key of the sender
370+ * @param network The network to use
371+ * @returns Transaction details
328372 */
329373export async function transferERC1155 (
330374 tokenAddress : Address ,
331375 toAddress : Address ,
332376 tokenId : bigint ,
333- amount : bigint ,
334- privateKey : `0x${string } `,
377+ amount : string ,
378+ privateKey : string | `0x${string } `,
335379 network : string = 'ethereum'
336380) : Promise < {
337381 txHash : Hash ;
338382 tokenId : string ;
339383 amount : string ;
340384} > {
385+ // Ensure the private key has 0x prefix
386+ const formattedKey = typeof privateKey === 'string' && ! privateKey . startsWith ( '0x' )
387+ ? `0x${ privateKey } ` as Hex
388+ : privateKey as Hex ;
389+
341390 const publicClient = getPublicClient ( network ) ;
342- const walletClient = getWalletClient ( privateKey , network ) ;
391+ const walletClient = getWalletClient ( formattedKey , network ) ;
343392 const account = walletClient . account ! ;
344393 const fromAddress = account . address ;
345394 const chain = getChain ( network ) ;
346395
396+ // Convert amount to bigint (ERC1155 tokens typically don't have decimals)
397+ const amountBigInt = BigInt ( amount ) ;
398+
347399 // Verify balance before transfer
348400 const contract = getContract ( {
349401 address : tokenAddress ,
@@ -352,23 +404,23 @@ export async function transferERC1155(
352404 } ) ;
353405
354406 const balance = await contract . read . balanceOf ( [ fromAddress , tokenId ] ) ;
355- if ( balance < amount ) {
356- throw new Error ( `Insufficient balance (${ balance . toString ( ) } ) for transfer of ${ amount . toString ( ) } tokens with ID #${ tokenId } ` ) ;
407+ if ( balance < amountBigInt ) {
408+ throw new Error ( `Insufficient balance (${ balance . toString ( ) } ) for transfer of ${ amount } tokens with ID #${ tokenId } ` ) ;
357409 }
358410
359411 // Send the token
360412 const hash = await walletClient . writeContract ( {
361413 address : tokenAddress ,
362414 abi : erc1155TransferAbi ,
363415 functionName : 'safeTransferFrom' ,
364- args : [ fromAddress , toAddress , tokenId , amount , '0x' as `0x${string } `] , // empty bytes for data
416+ args : [ fromAddress , toAddress , tokenId , amountBigInt , '0x' as `0x${string } `] , // empty bytes for data
365417 account,
366418 chain
367419 } ) ;
368420
369421 return {
370422 txHash : hash ,
371423 tokenId : tokenId . toString ( ) ,
372- amount : amount . toString ( )
424+ amount : amount
373425 } ;
374426}
0 commit comments