prepareERC20Approval
Generate transaction data for approving ERC20 token spending by another address, enabling secure token transfers on Ethereum networks.
Instructions
Prepare an ERC20 token approval transaction for signing. Returns transaction data that can be signed and broadcast.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contractAddress | Yes | The address of the ERC20 token contract | |
| tokenAddress | No | DEPRECATED: Use contractAddress instead. The address of the ERC20 token contract | |
| spenderAddress | Yes | The Ethereum address to approve for spending tokens | |
| amount | Yes | The amount of tokens to transfer (can be decimal, e.g. '1.5') | |
| fromAddress | Yes | The Ethereum address that owns the tokens | |
| provider | No | Optional. Either a network name or custom RPC URL. Use getAllNetworks to see available networks and their details, or getNetwork to get info about a specific network. You can use any network name returned by these tools as a provider value. | |
| chainId | No | Optional. The chain ID to use. If provided with a named network and they don't match, the RPC's chain ID will be used. | |
| gasLimit | No | ||
| gasPrice | No | ||
| maxFeePerGas | No | ||
| maxPriorityFeePerGas | No |
Implementation Reference
- src/tools/erc20.ts:417-506 (handler)MCP tool registration and handler implementation for 'prepareERC20Approval'. This includes the input schema validation (inline Zod schema), the execution logic that maps parameters, fetches token info, prepares the unsigned transaction via ethersService, and formats the response with JSON transaction data for signing."prepareERC20Approval", "Prepare an ERC20 token approval transaction for signing. Returns transaction data that can be signed and broadcast.", { contractAddress: contractAddressSchema, tokenAddress: tokenAddressSchema.optional(), // Deprecated spenderAddress: z.string().describe( "The Ethereum address to approve for spending tokens" ), amount: amountSchema, fromAddress: z.string().describe( "The Ethereum address that owns the tokens" ), provider: providerSchema, chainId: chainIdSchema, gasLimit: z.string().optional(), gasPrice: z.string().optional(), maxFeePerGas: z.string().optional(), maxPriorityFeePerGas: z.string().optional() }, async (params) => { // Map deprecated parameters const mapped = mapParameters(params); try { const contractAddr = mapped.contractAddress || params.tokenAddress; if (!contractAddr) { throw new Error('Either contractAddress or tokenAddress must be provided'); } // Get token info for display const tokenInfo = await ethersService.getERC20TokenInfo( contractAddr, mapped.provider, mapped.chainId ); // Prepare gas options const options = { gasLimit: params.gasLimit, gasPrice: params.gasPrice, maxFeePerGas: params.maxFeePerGas, maxPriorityFeePerGas: params.maxPriorityFeePerGas }; const txRequest = await ethersService.prepareERC20Approval( contractAddr, mapped.spenderAddress, mapped.amount, mapped.fromAddress, mapped.provider, mapped.chainId, options ); return { content: [{ type: "text", text: `ERC20 Approval Transaction Prepared: Token: ${tokenInfo.name} (${tokenInfo.symbol}) Owner: ${mapped.fromAddress} Spender: ${mapped.spenderAddress} Amount: ${mapped.amount} ${tokenInfo.symbol} Transaction Data: ${JSON.stringify({ to: txRequest.to, data: txRequest.data, value: txRequest.value || "0", gasLimit: txRequest.gasLimit?.toString(), gasPrice: txRequest.gasPrice?.toString(), maxFeePerGas: txRequest.maxFeePerGas?.toString(), maxPriorityFeePerGas: txRequest.maxPriorityFeePerGas?.toString(), chainId: txRequest.chainId }, null, 2)} This transaction is ready to be signed and broadcast.` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error preparing approval transaction: ${error instanceof Error ? error.message : String(error)}` }] }; } } );