Skip to main content
Glama

estimate_gas

Calculate gas fees for transactions on supported blockchain networks like BSC, Ethereum, and more using recipient address, value, and data inputs. Defaults to BSC mainnet.

Instructions

Estimate the gas cost for a transaction

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dataNoThe transaction data as a hex string
networkNoNetwork name (e.g. 'bsc', 'opbnb', 'ethereum', 'base', etc.) or chain ID. Supports others main popular networks. Defaults to BSC mainnet.bsc
toYesThe recipient address
valueNoThe amount of ETH to send in ether (e.g., '0.1')

Implementation Reference

  • The main handler function for the 'estimate_gas' MCP tool. It processes input parameters, constructs the transaction params, calls the services.estimateGas helper, and returns the estimated gas in a standardized MCP response format.
    async ({ to, value, data, network }) => { 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 mcpToolRes.success({ network, estimatedGas: gas.toString() }) } catch (error) { return mcpToolRes.error(error, "estimating gas") } }
  • Zod schema defining the input parameters for the 'estimate_gas' tool: 'to' (required address), 'value' (optional ETH amount), 'data' (optional hex data), 'network' (chain identifier).
    { 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: defaultNetworkParam },
  • The registration of the 'estimate_gas' tool on the MCP server within registerTransactionTools, including name, description, input schema, and handler function.
    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: defaultNetworkParam }, async ({ to, value, data, network }) => { 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 mcpToolRes.success({ network, estimatedGas: gas.toString() }) } catch (error) { return mcpToolRes.error(error, "estimating gas") } } )
  • Supporting helper function that performs the actual gas estimation using the viem public client for the specified network and transaction 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/bnb-chain/bnbchain-mcp'

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