DEV Community

memosr.eth
memosr.eth

Posted on

Make your agent gas-aware on Base in 5 minutes

Last week Base gas sat on its 0.006 gwei floor for 91% of 2,016 readings. One real spike hit on Nov 10 (2.3x, gone in about 15 minutes). A Uniswap swap cost about $0.0024.

So the useful question for an agent is not "when is gas cheap?" It is: "is right now a spike, and what will this transaction cost in dollars?" An RPC call gives you gwei. Your agent budgets in dollars.

This guide adds that answer to an agent in three ways, from fastest to most flexible. Each call is paid per request in USDC over x402: no API key, no signup.

What you need

  • A separate Base mainnet wallet with a dollar or two of USDC. No ETH needed: payments are EIP-3009 signatures.
  • Node.js 18+

Never use a key from a wallet that holds real funds. Never paste it into chat or commit it.

Option 1: one command, no code

See what the endpoint asks for (free, returns HTTP 402 with the price):

curl -i "https://base-gas-x402-production.up.railway.app/gas?gasLimit=180000"
Enter fullscreen mode Exit fullscreen mode

Pay and fetch with the AgentCash CLI:

npx agentcash@latest fetch "https://base-gas-x402-production.up.railway.app/gas?gasLimit=180000"
Enter fullscreen mode Exit fullscreen mode

gasLimit cheat sheet: 21000 ETH transfer, 65000 ERC-20 transfer, 85000 NFT mint, 180000 swap, 1500000 deploy.

Option 2: MCP (Claude Desktop, Claude Code, Cursor)

claude mcp add base-gas \
  --env BUYER_PRIVATE_KEY=0xYOUR_SEPARATE_WALLET_KEY \
  -- npx -y base-gas-mcp
Enter fullscreen mode Exit fullscreen mode

Then just ask:

  • "What does a Uniswap swap on Base cost right now, in dollars?"
  • "Is Base gas high right now, or should I wait?"
  • "Is Base or Arbitrum cheaper for a token transfer?"

The server only ever pays one origin and refuses any single payment above $0.05, so a prompt cannot redirect your wallet.

Option 3: in your own code

npm i @x402/fetch @x402/evm viem
Enter fullscreen mode Exit fullscreen mode
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount(process.env.BUYER_PRIVATE_KEY);

const pay = wrapFetchWithPaymentFromConfig(fetch, {
  schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(account) }],
});

const res = await pay(
  "https://base-gas-x402-production.up.railway.app/gas?gasLimit=180000"
);
const gas = await res.json();

console.log("Swap cost (USD):", gas.estimatedTransferCost.usd);
console.log("Fees to sign:", gas.suggestedFees.medium);
Enter fullscreen mode Exit fullscreen mode

suggestedFees.medium holds maxFeePerGas and maxPriorityFeePerGas in wei, ready for viem's sendTransaction:

const { maxFeePerGas, maxPriorityFeePerGas } = gas.suggestedFees.medium;
await walletClient.sendTransaction({
  to, value, data,
  maxFeePerGas: BigInt(maxFeePerGas),
  maxPriorityFeePerGas: BigInt(maxPriorityFeePerGas),
});
Enter fullscreen mode Exit fullscreen mode

maxFeePerGas is a ceiling (2x base fee + tip). You pay base fee + tip; the rest is not spent.

A simple rule for agents

  1. Call /gas before a transaction and read the USD cost.
  2. If the cost is above your budget, call /gas/history and check the verdict (cheap / normal / expensive / flat).
  3. If it says expensive, wait 15 minutes. On Base, spikes are short.

Endpoints and prices

Endpoint Price Returns
GET /gas $0.002 Base fee, priority tiers, cost in USD/ETH/gwei, ready-to-sign fees
GET /gas/compare $0.01 Same transaction on Base, OP Mainnet, Arbitrum, Ethereum
GET /gas/history $0.01 Last N hours, with a cheap/normal/expensive/flat verdict
GET /gas/cheapest-window $0.02 Cheapest hour of day from 7 days of data
GET /health free Service status

A failed call is never charged: settlement happens only on a successful response.

Docs and code: base-gas-x402.vercel.app ยท GitHub

Top comments (0)