approveERC20
Authorize a specified Ethereum address to spend a defined amount of ERC20 tokens from your wallet on supported networks.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | The amount of tokens to transfer (can be decimal, e.g. '1.5') | |
| 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 | ||
| 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. | |
| spenderAddress | Yes | The Ethereum address to approve for spending tokens | |
| tokenAddress | Yes | The address of the ERC20 token contract |
Implementation Reference
- src/tools/handlers/erc20.ts:228-283 (handler)The primary handler function implementing the approveERC20 tool logic. It validates inputs using Zod, handles deprecated parameters, executes the approval transaction via ethersService, and formats the response with transaction details.approveERC20: async (args: unknown) => { const schema = z.object({ contractAddress: contractAddressSchema.optional(), tokenAddress: tokenAddressSchema, // Deprecated spenderAddress: CommonSchemas.ethereumAddress.describe('Address to approve for spending tokens'), amount: amountSchema, provider: providerSchema, chainId: chainIdSchema, gasLimit: CommonSchemas.gasLimit, gasPrice: CommonSchemas.gasPrice, }); try { // First validate with friendly errors const validatedParams = validateWithFriendlyErrors( schema, args, 'Approve ERC20 Spending' ); // Then map deprecated parameters for backward compatibility const mapped = mapParameters(validatedParams); // Ensure we have a contract address (from either new or old parameter name) const contractAddr = mapped.contractAddress || validatedParams.tokenAddress; if (!contractAddr) { throw new Error('Contract address is required. Please provide either contractAddress or tokenAddress.'); } // Create options object for transaction parameters const options: TokenOperationOptions = {}; if (validatedParams.gasLimit) options.gasLimit = validatedParams.gasLimit; if (validatedParams.gasPrice) options.gasPrice = validatedParams.gasPrice; const tx = await ethersService.approveERC20( contractAddr, validatedParams.spenderAddress, validatedParams.amount, mapped.provider, mapped.chainId, options ); // Get token info to format the response const tokenInfo = await ethersService.getERC20TokenInfo(contractAddr, mapped.provider, mapped.chainId); return { content: [{ type: "text", text: `Successfully approved ${validatedParams.spenderAddress} to spend ${validatedParams.amount} ${tokenInfo.symbol}.\nTransaction Hash: ${tx.hash}` }] }; } catch (error) { return createErrorResponse(error, 'approving token spending'); } },
- The schema definition for the approveERC20 tool, including name, description, and detailed inputSchema used for MCP tool registration and validation.{ name: "approveERC20", description: "Approve a spender to use a certain amount of your ERC20 tokens", inputSchema: { type: "object", properties: { tokenAddress: { type: "string", description: "The address of the ERC20 token contract" }, spenderAddress: { type: "string", description: "The Ethereum address to approve for spending tokens" }, amount: { type: "string", description: "The amount of tokens to approve (can be decimal, e.g. '1.5')" }, provider: { type: "string", description: "Optional. Either a network name or custom RPC URL. Use getSupportedNetworks to get a list of supported networks." }, chainId: { type: "number", description: "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: { type: "string", description: "Optional. The gas limit for the transaction" }, gasPrice: { type: "string", description: "Optional. The gas price for the transaction in gwei" } }, required: ["tokenAddress", "spenderAddress", "amount"] } },