Skip to main content
Glama

estimate-gas

Calculate transaction gas requirements without sending to the network. Input recipient address, contract call data, value, and fee details to estimate costs accurately.

Instructions

Estimate the gas necessary to complete a transaction without submitting it to the network.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chainIdNoChain ID to validate against before sending transaction.
dataNoA contract hashed method call with encoded args.
maxFeePerGasNoTotal fee per gas in wei, inclusive of maxPriorityFeePerGas.
maxPriorityFeePerGasNoMax priority fee per gas in wei.
toYesThe transaction recipient or contract address.
valueNoValue in wei sent with this transaction.

Implementation Reference

  • The execute handler for the 'estimate-gas' tool. It calls wagmi's estimateGas function with the provided arguments, returns the result as JSON stringified text content, and handles TransactionExecutionError or other errors by returning error messages.
    execute: async (args) => { try { const result = await estimateGas(wagmiConfig, args); return { content: [ { type: "text", text: JSONStringify({ hash: result, }), }, ], }; } catch (error) { if (error instanceof TransactionExecutionError) { return { content: [ { type: "text", text: error.cause.message, }, ], }; } return { content: [ { type: "text", text: (error as Error).message, }, ], }; } },
  • Zod schema defining the input parameters for the 'estimate-gas' tool.
    parameters: z.object({ to: Address.describe("The transaction recipient or contract address."), data: Address.optional().describe("A contract hashed method call with encoded args."), value: z.coerce.bigint().optional().describe("Value in wei sent with this transaction."), maxFeePerGas: z.coerce.bigint().optional().describe("Total fee per gas in wei, inclusive of maxPriorityFeePerGas."), maxPriorityFeePerGas: z.coerce.bigint().optional().describe("Max priority fee per gas in wei."), chainId: z.coerce.number().optional().describe("Chain ID to validate against before sending transaction."), }),
  • The server.addTool call that registers the 'estimate-gas' tool, including name, description, parameters schema, and execute handler.
    server.addTool({ name: "estimate-gas", description: "Estimate the gas necessary to complete a transaction without submitting it to the network.", parameters: z.object({ to: Address.describe("The transaction recipient or contract address."), data: Address.optional().describe("A contract hashed method call with encoded args."), value: z.coerce.bigint().optional().describe("Value in wei sent with this transaction."), maxFeePerGas: z.coerce.bigint().optional().describe("Total fee per gas in wei, inclusive of maxPriorityFeePerGas."), maxPriorityFeePerGas: z.coerce.bigint().optional().describe("Max priority fee per gas in wei."), chainId: z.coerce.number().optional().describe("Chain ID to validate against before sending transaction."), }), execute: async (args) => { try { const result = await estimateGas(wagmiConfig, args); return { content: [ { type: "text", text: JSONStringify({ hash: result, }), }, ], }; } catch (error) { if (error instanceof TransactionExecutionError) { return { content: [ { type: "text", text: error.cause.message, }, ], }; } return { content: [ { type: "text", text: (error as Error).message, }, ], }; } }, });
  • Helper function that registers the 'estimate-gas' tool on the FastMCP server.
    export function registerEstimateGasTools(server: FastMCP, wagmiConfig: Config): void { server.addTool({ name: "estimate-gas", description: "Estimate the gas necessary to complete a transaction without submitting it to the network.", parameters: z.object({ to: Address.describe("The transaction recipient or contract address."), data: Address.optional().describe("A contract hashed method call with encoded args."), value: z.coerce.bigint().optional().describe("Value in wei sent with this transaction."), maxFeePerGas: z.coerce.bigint().optional().describe("Total fee per gas in wei, inclusive of maxPriorityFeePerGas."), maxPriorityFeePerGas: z.coerce.bigint().optional().describe("Max priority fee per gas in wei."), chainId: z.coerce.number().optional().describe("Chain ID to validate against before sending transaction."), }), execute: async (args) => { try { const result = await estimateGas(wagmiConfig, args); return { content: [ { type: "text", text: JSONStringify({ hash: result, }), }, ], }; } catch (error) { if (error instanceof TransactionExecutionError) { return { content: [ { type: "text", text: error.cause.message, }, ], }; } return { content: [ { type: "text", text: (error as Error).message, }, ], }; } }, }); };
  • Invocation of the registerEstimateGasTools helper as part of the overall registerTools function.
    registerEstimateGasTools(server, wagmiConfig);

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/Xiawpohr/metamask-mcp'

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