estimate_gas
Calculate the gas cost for EVM-based blockchain transactions by providing recipient address, value, and optional data or network details.
Instructions
Estimate the gas cost for a transaction
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | No | The transaction data as a hex string | |
| network | No | Network name or chain ID. Defaults to Ethereum mainnet. | |
| to | Yes | The recipient address | |
| value | No | The amount of ETH to send in ether (e.g., '0.1') |
Implementation Reference
- src/core/tools.ts:516-575 (registration)MCP server.tool registration for the 'estimate_gas' tool, including Zod input schema and the complete async handler function that constructs the transaction parameters and delegates to the core services.estimateGas helper.server.tool( 'estimate_gas', 'Estimate the gas cost for a transaction', { to: z.string().describe('The recipient address'), value: z .string() .optional() .describe("The amount of ETH to send in ether (e.g., '0.1')"), data: z .string() .optional() .describe('The transaction data as a hex string'), network: z .string() .optional() .describe('Network name or chain ID. Defaults to Ethereum mainnet.') }, async ({ to, value, data, network = 'ethereum' }) => { try { const params: any = { to: to as Address }; if (value) { params.value = services.helpers.parseEther(value); } if (data) { params.data = data as `0x${string}`; } const gas = await services.estimateGas(params, network); return { content: [ { type: 'text', text: JSON.stringify( { network, estimatedGas: gas.toString() }, null, 2 ) } ] }; } catch (error) { return { content: [ { type: 'text', text: `Error estimating gas: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } );
- src/core/tools.ts:534-574 (handler)The async handler function implementing the core logic of the 'estimate_gas' tool: parameter validation, transaction param construction, delegation to services.estimateGas, and formatted response.async ({ to, value, data, network = 'ethereum' }) => { try { const params: any = { to: to as Address }; if (value) { params.value = services.helpers.parseEther(value); } if (data) { params.data = data as `0x${string}`; } const gas = await services.estimateGas(params, network); return { content: [ { type: 'text', text: JSON.stringify( { network, estimatedGas: gas.toString() }, null, 2 ) } ] }; } catch (error) { return { content: [ { type: 'text', text: `Error estimating gas: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/core/tools.ts:519-533 (schema)Zod schema for 'estimate_gas' tool inputs: to (required address), optional value (ether string), data (hex), network.{ to: z.string().describe('The recipient address'), value: z .string() .optional() .describe("The amount of ETH to send in ether (e.g., '0.1')"), data: z .string() .optional() .describe('The transaction data as a hex string'), network: z .string() .optional() .describe('Network name or chain ID. Defaults to Ethereum mainnet.') },
- Underlying helper function estimateGas that retrieves the viem public client for the network and calls client.estimateGas with the provided parameters.export async function estimateGas(params: EstimateGasParameters, network = 'ethereum'): Promise<bigint> { const client = getPublicClient(network); return await client.estimateGas(params); }