DEV Community

say1j
say1j

Posted on AI-assisted

How an AI agent pays USDC on Base to rent TRON Energy (x402 in practice)

Sending USDT on TRON has a hidden cost. A USDT transfer is a smart-contract call, so it needs Energy. If the sending wallet has none, the network burns TRX instead: about 6.5 TRX for a typical transfer, or about 13 TRX when the recipient has never held USDT.

The usual way around that is to rent Energy for an hour before you send. Today, 65,000 Energy for one hour rents for about 2.3 TRX, roughly a third of the burn. The catch is that rental sites are built for people: accounts, dashboards, top-ups.

We run TronEnergyFrog, which rents Energy through the website, the Telegram bot @tron_energy_frog_bot and an API. We also wanted an AI agent to do the whole thing on its own, with no account and no API key. That's what x402 is for. This post walks through the actual flow, which is a little unusual because the agent pays on one chain (Base) for a resource on another (TRON).

What x402 is, in one paragraph

x402 revives the HTTP 402 Payment Required status. A server answers a request with 402 and a PAYMENT-REQUIRED header describing what it accepts (network, asset, amount, recipient). The client signs a payment and retries the same request with a PAYMENT-SIGNATURE header. The server has a facilitator verify and settle it, then returns the resource with a PAYMENT-RESPONSE header. Version 2 uses CAIP-2 network IDs such as eip155:8453 for Base.

Step 1: discover

Everything an agent needs is in one public document:

curl https://api.tronenergyfrog.com/v1/x402
Enter fullscreen mode Exit fullscreen mode
{
  "registration_required": false,
  "x402_version": 2,
  "quote_endpoint": "POST /v1/x402/quotes",
  "payment_endpoint_template": "POST /v1/x402/rentals/{quote_id}",
  "status_endpoint_template": "GET /v1/x402/rentals/{quote_id}",
  "accepted": [
    { "network": "eip155:8453", "network_name": "Base", "asset": "USDC", "preferred": true },
    { "network": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", "network_name": "Solana", "asset": "USDC", "preferred": false }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Step 2: get a free quote

Energy prices move through the day, and the TRX/USD rate moves too. So the agent first asks for a quote, which locks both for 10 minutes. Creating one costs nothing.

curl -X POST https://api.tronenergyfrog.com/v1/x402/quotes \
  -H "Content-Type: application/json" \
  -d '{"target_address":"T...sender","energy_amount":65000,"duration_seconds":3600}'
Enter fullscreen mode Exit fullscreen mode

The response includes quote_id, price_trx, the exact USDC amount (payment_amount_atomic, six decimals) and valid_until. target_address must be the wallet that will send the USDT, because Energy is delegated to the sender, not the recipient.

Step 3: pay the quote

Now the agent POSTs to the quote's payment URL. The first request returns 402 with the requirements. An x402 client signs a USDC transfer authorization and retries automatically:

import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount(process.env.BASE_PRIVATE_KEY as `0x${string}`);

const payingFetch = wrapFetchWithPaymentFromConfig(fetch, {
  schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(account) }],
  // Refuse anything above $5 before signing.
  spendControls: { maxAmountPerPayment: "$5" },
});

const res = await payingFetch(
  `https://api.tronenergyfrog.com/v1/x402/rentals/${quoteId}`,
  { method: "POST", body: "{}", headers: { "Content-Type": "application/json" } },
);
console.log(res.status); // 202 once the payment is verified
Enter fullscreen mode Exit fullscreen mode

Use a dedicated wallet that holds only what the agent is allowed to spend, and keep the spend cap low. The SDK checks it before anything is signed.

Step 4: poll until the Energy lands

curl https://api.tronenergyfrog.com/v1/x402/rentals/<quote_id>
Enter fullscreen mode Exit fullscreen mode

The status moves from settling to fulfillment_pending and then to succeeded once Energy is delegated on TRON. The agent can now send its USDT without burning TRX.

How the server keeps this safe

A few design choices matter once real money moves:

  • Verify, record, then settle. The payment is verified first and recorded in our database before settlement starts. If anything fails in between, nothing is charged twice and nothing is delivered unpaid. The x402 spec recommends this order.
  • The quote is the source of truth. The amount, asset and payee in the payment must match the quote exactly, and a payment can't be reused for another quote.
  • Expired or unknown quotes are rejected before verification, so a signature for a stale price is never settled.
  • Signed attempts are rate-limited per IP. Each one costs a facilitator call, so forged signatures can't run up costs.

An MCP server, so agents don't need to write any of this

If your agent speaks MCP (Claude, Cursor, Windsurf and others), you can skip the code:

{
  "mcpServers": {
    "tronenergyfrog": {
      "command": "npx",
      "args": ["-y", "tronenergyfrog-mcp"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Without any configuration it gives the agent live prices, free quotes and rental status. Add TRONENERGYFROG_EVM_PRIVATE_KEY and pay_x402_quote appears, capped by TRONENERGYFROG_MAX_PAYMENT_USD (default $5). Tools that spend money are marked as destructive and tell the agent to confirm the address, amount and price with you first. The source is on GitHub under MIT, and it's listed in the official MCP Registry as io.github.say1j/tronenergyfrog.

Not an agent? The same rental works three other ways

  • Guest checkout on the website, with no account. No fee is added when you pay in TRX; other coins go through Heleket, which adds its own fee.
  • Telegram bot @tron_energy_frog_bot: check how much Energy a transfer needs and rent it from your phone.
  • Account API with a key from the same bot, for apps that rent from a prepaid TRX balance.

Prices are public: the price page shows the live rate, a 7-day hourly history and the cheapest hours, and the same data is available as JSON without a key at https://api.tronenergyfrog.com/v1/rates/energy/default.

What's next

The x402 Foundation has an open draft for a TRON payment binding that would let agents pay in USDT on TRON itself (via Permit2). Once it's final, the whole flow can stay on one chain. Until then, Base USDC works today.

If you're building agents that move USDT, I'd like to hear what's missing. Comments are open.

Top comments (0)