eth_gasPrice
Get the current Ethereum gas price in wei to estimate transaction costs before sending blockchain operations.
Instructions
Retrieves the current gas price in wei
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.js:140-161 (handler)The handler function for the 'eth_gasPrice' tool. It calls makeRpcCall('eth_gasPrice'), parses the hex result to wei and gwei, and returns formatted text output or error.async () => { try { console.error('Getting current gas price'); const gasPrice = await makeRpcCall('eth_gasPrice'); // Convert hex gas price to decimal and then to Gwei for readability const gasPriceWei = parseInt(gasPrice, 16); const gasPriceGwei = gasPriceWei / 1e9; return { content: [{ type: "text", text: `Current Gas Price:\n${gasPriceWei} Wei\n${gasPriceGwei.toFixed(2)} Gwei` }] }; } catch (error) { return { content: [{ type: "text", text: `Error: Failed to get gas price. ${error.message}` }], isError: true }; } }
- index.js:135-162 (registration)Registers the 'eth_gasPrice' tool using server.tool method, specifying the tool name, description, empty input schema (no parameters), and inline handler function.// Tool 2: eth_gasPrice - Gets the current gas price server.tool( 'eth_gasPrice', 'Retrieves the current gas price in wei', {}, async () => { try { console.error('Getting current gas price'); const gasPrice = await makeRpcCall('eth_gasPrice'); // Convert hex gas price to decimal and then to Gwei for readability const gasPriceWei = parseInt(gasPrice, 16); const gasPriceGwei = gasPriceWei / 1e9; return { content: [{ type: "text", text: `Current Gas Price:\n${gasPriceWei} Wei\n${gasPriceGwei.toFixed(2)} Gwei` }] }; } catch (error) { return { content: [{ type: "text", text: `Error: Failed to get gas price. ${error.message}` }], isError: true }; } } );
- index.js:84-102 (helper)Helper utility function 'makeRpcCall' used by the eth_gasPrice handler to send JSON-RPC requests to the Ethereum RPC endpoint.async function makeRpcCall(method, params = []) { try { const response = await axios.post(ETH_RPC_URL, { jsonrpc: '2.0', id: 1, method, params }); if (response.data.error) { throw new Error(`RPC Error: ${response.data.error.message}`); } return response.data.result; } catch (error) { console.error(`Error making RPC call to ${method}:`, error.message); throw error; } }