prepareContractTransaction
Prepare smart contract interaction transactions for signing by encoding function calls and setting transaction parameters like gas and value.
Instructions
Prepare a smart contract interaction transaction for signing. Returns transaction data that can be signed and broadcast.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contractAddress | Yes | The smart contract address to interact with | |
| data | Yes | The contract interaction data (encoded function call) as hex string | |
| value | No | Optional. The amount of ETH to send with the transaction (default: '0') | 0 |
| fromAddress | Yes | The Ethereum address sending the transaction | |
| 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. | |
| gasLimit | No | Optional. The gas limit for the transaction | |
| gasPrice | No | Optional. The gas price (in gwei) for legacy transactions | |
| maxFeePerGas | No | Optional. The maximum fee per gas (in gwei) for EIP-1559 transactions | |
| maxPriorityFeePerGas | No | Optional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions |
Implementation Reference
- src/tools/core.ts:1171-1261 (registration)Registration of the 'prepareContractTransaction' MCP tool, including input schema and the handler function that prepares the contract transaction using ethersService and returns formatted response.// Prepare Contract Transaction tool server.tool( "prepareContractTransaction", "Prepare a smart contract interaction transaction for signing. Returns transaction data that can be signed and broadcast.", { contractAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The smart contract address to interact with" ), data: z.string().regex(/^0x[a-fA-F0-9]*$/).describe( "The contract interaction data (encoded function call) as hex string" ), value: z.string().optional().default("0").describe( "Optional. The amount of ETH to send with the transaction (default: '0')" ), fromAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The Ethereum address sending the transaction" ), provider: z.string().optional().describe(PROVIDER_DESCRIPTION), chainId: z.number().optional().describe( "Optional. The chain ID to use." ), gasLimit: z.string().optional().describe( "Optional. The gas limit for the transaction" ), gasPrice: z.string().optional().describe( "Optional. The gas price (in gwei) for legacy transactions" ), maxFeePerGas: z.string().optional().describe( "Optional. The maximum fee per gas (in gwei) for EIP-1559 transactions" ), maxPriorityFeePerGas: z.string().optional().describe( "Optional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions" ) }, async ({ contractAddress, data, value, fromAddress, provider, chainId, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }) => { try { // Prepare gas options const options = { gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }; const txRequest = await ethersService.prepareContractTransaction( contractAddress, data, value || "0", fromAddress, provider, chainId, options ); return { content: [{ type: "text", text: `Contract Transaction Prepared: Contract: ${contractAddress} From: ${fromAddress} Value: ${value || "0"} ETH Data: ${data} Transaction Data: ${JSON.stringify({ to: txRequest.to, data: txRequest.data, value: txRequest.value?.toString(), from: txRequest.from, 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 contract transaction: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
- src/tools/core.ts:1205-1260 (handler)The MCP tool handler function for 'prepareContractTransaction' that validates inputs, calls ethersService helper, formats the transaction data, and returns a text response.async ({ contractAddress, data, value, fromAddress, provider, chainId, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }) => { try { // Prepare gas options const options = { gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }; const txRequest = await ethersService.prepareContractTransaction( contractAddress, data, value || "0", fromAddress, provider, chainId, options ); return { content: [{ type: "text", text: `Contract Transaction Prepared: Contract: ${contractAddress} From: ${fromAddress} Value: ${value || "0"} ETH Data: ${data} Transaction Data: ${JSON.stringify({ to: txRequest.to, data: txRequest.data, value: txRequest.value?.toString(), from: txRequest.from, 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 contract transaction: ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/tools/core.ts:1175-1204 (schema)Zod schema for input parameters of the 'prepareContractTransaction' tool.{ contractAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The smart contract address to interact with" ), data: z.string().regex(/^0x[a-fA-F0-9]*$/).describe( "The contract interaction data (encoded function call) as hex string" ), value: z.string().optional().default("0").describe( "Optional. The amount of ETH to send with the transaction (default: '0')" ), fromAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The Ethereum address sending the transaction" ), provider: z.string().optional().describe(PROVIDER_DESCRIPTION), chainId: z.number().optional().describe( "Optional. The chain ID to use." ), gasLimit: z.string().optional().describe( "Optional. The gas limit for the transaction" ), gasPrice: z.string().optional().describe( "Optional. The gas price (in gwei) for legacy transactions" ), maxFeePerGas: z.string().optional().describe( "Optional. The maximum fee per gas (in gwei) for EIP-1559 transactions" ), maxPriorityFeePerGas: z.string().optional().describe( "Optional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions" ) },