Skip to main content
Glama

getGasPrice

Retrieve current gas prices in slow, normal, and fast tiers with Gwei and estimated USD costs for EVM chains.

Instructions

현재 가스비를 slow/normal/fast 3단계로 조회합니다 (Gwei + USD 예상 비용)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chainNoEVM 체인ethereum

Implementation Reference

  • The core handler function that fetches gas estimates from the RPC client and calculates gas costs.
    async function handler(args: z.infer<typeof inputSchema>): Promise<ToolResult<GasPriceData>> {
      const { chain } = args;
    
      const cacheKey = `gas:${chain}`;
      const cached = cache.get<GasPriceData>(cacheKey);
      if (cached.hit) return makeSuccess(chain, cached.data, true);
    
      try {
        const client = getClient(chain);
    
        const [block, maxPriorityFee] = await Promise.all([
          client.getBlock({ blockTag: "latest" }),
          client.estimateMaxPriorityFeePerGas(),
        ]);
    
        const baseFee = block.baseFeePerGas ?? 0n;
    
        const prioritySlow = maxPriorityFee * 80n / 100n;
        const priorityNormal = maxPriorityFee;
        const priorityFast = maxPriorityFee * 150n / 100n;
    
        const maxFeeSlow = baseFee + prioritySlow;
        const maxFeeNormal = baseFee + priorityNormal;
        const maxFeeFast = baseFee + priorityFast;
    
        let ethPriceUsd = 0;
        try {
          const nativeCoingeckoId = getNativeCoingeckoId(chain);
          if (nativeCoingeckoId) {
            const priceData = await getPrice(nativeCoingeckoId);
            ethPriceUsd = priceData.priceUsd;
          }
        } catch {
          // 가격 조회 실패해도 가스비 자체는 반환
        }
    
        function calcCostUsd(maxFeeGwei: bigint): number {
          if (ethPriceUsd === 0) return 0;
          const costWei = maxFeeGwei * STANDARD_GAS;
          const costEth = Number(costWei) / 1e18;
          return Math.round(costEth * ethPriceUsd * 10000) / 10000;
        }
    
        const data: GasPriceData = {
          slow: {
            maxFeePerGas: formatGwei(maxFeeSlow),
            maxPriorityFeePerGas: formatGwei(prioritySlow),
            estimatedCostUsd: calcCostUsd(maxFeeSlow),
          },
          normal: {
            maxFeePerGas: formatGwei(maxFeeNormal),
            maxPriorityFeePerGas: formatGwei(priorityNormal),
            estimatedCostUsd: calcCostUsd(maxFeeNormal),
          },
          fast: {
            maxFeePerGas: formatGwei(maxFeeFast),
            maxPriorityFeePerGas: formatGwei(priorityFast),
            estimatedCostUsd: calcCostUsd(maxFeeFast),
          },
          baseFee: formatGwei(baseFee),
          lastBlock: Number(block.number),
        };
    
        cache.set(cacheKey, data, GAS_CACHE_TTL);
        return makeSuccess(chain, data, false);
      } catch (err) {
        const message = sanitizeError(err);
        return makeError(`Failed to fetch gas price: ${message}`, "RPC_ERROR");
      }
    }
  • Input validation schema for the getGasPrice tool.
    const inputSchema = z.object({
      chain: z.enum(SUPPORTED_CHAINS).default("ethereum").describe("EVM 체인"),
    });
  • Tool registration for getGasPrice in the MCP server.
    export function register(server: McpServer) {
      server.tool(
        "getGasPrice",
        "현재 가스비를 slow/normal/fast 3단계로 조회합니다 (Gwei + USD 예상 비용)",
        inputSchema.shape,
        async (args) => {
          const result = await handler(args as z.infer<typeof inputSchema>);
          return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
        },
      );
    }

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/calintzy/evmscope'

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