Skip to main content
Glama

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
NameRequiredDescriptionDefault
dataNoThe transaction data as a hex string
networkNoNetwork name or chain ID. Defaults to Ethereum mainnet.
toYesThe recipient address
valueNoThe amount of ETH to send in ether (e.g., '0.1')

Implementation Reference

  • 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 }; } } );
  • 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 }; } }
  • 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); }

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/chulanpro5/evm-mcp-server'

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