Skip to main content
Glama

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
NameRequiredDescriptionDefault
contractAddressYesThe smart contract address to interact with
dataYesThe contract interaction data (encoded function call) as hex string
valueNoOptional. The amount of ETH to send with the transaction (default: '0')0
fromAddressYesThe Ethereum address sending the transaction
providerNoOptional. 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.
chainIdNoOptional. The chain ID to use.
gasLimitNoOptional. The gas limit for the transaction
gasPriceNoOptional. The gas price (in gwei) for legacy transactions
maxFeePerGasNoOptional. The maximum fee per gas (in gwei) for EIP-1559 transactions
maxPriorityFeePerGasNoOptional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions

Implementation Reference

  • 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)}` }] }; } } );
  • 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)}` }] }; } }
  • 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" ) },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/crazyrabbitLTC/mcp-ethers-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server