# Blockchain IQ x Cloudflare Agents: a market-brief agent that pays per call

A [Cloudflare Agents SDK](https://developers.cloudflare.com/agents/) example. `MarketBriefAgent` is a Durable Object agent that buys [Blockchain IQ](https://www.blockchainiq.co/agents)'s crypto market brief ($0.01 USDC on Base) over [x402](https://www.x402.org) and keeps the latest one, with its settlement proof, in agent state. There's no account and no API key: a wallet with a few cents of USDC is the only credential.

Made by **[ScrapeMole](https://scrapemole.pages.dev)**. MIT licensed. Not affiliated with Blockchain IQ.

## What it shows

- `Agent` with typed, persisted state (`lastBrief`, `lastPayment`, `paidCalls`, today's spend).
- `@callable()` methods `buyBrief()` and `getBrief()`, reachable over RPC/WebSocket at `/agents/market-brief-agent/<name>` or from the Worker via `getAgentByName`.
- Paying with Blockchain IQ's recommended stack: `@x402/fetch` + `@x402/evm` + `viem`, running inside workerd.
- Optional `scheduleEvery` refresh (`REFRESH_MINUTES`, off by default).
- Spend guards: a free 402 quote check, a per-call cap (`MAX_USDC_PER_CALL`, default $0.01, also enforced as an x402 policy and spend control), a daily budget (`DAILY_BUDGET_USDC`, default $0.10) and a bearer token for paid triggers once deployed.

## Run locally and make one paid call ($0.01)

Node 18+.

```bash
git clone https://scrapemole.pages.dev/code/blockchainiq-cloudflare-agent.git
cd blockchainiq-cloudflare-agent
npm install
echo "AGENT_PRIVATE_KEY=0x..." > .dev.vars   # dedicated Base wallet, >= $0.01 USDC, no ETH needed
npm run paid-call                             # starts wrangler dev, POST /brief, prints the proof, stops
```

Output (real run, 26 Oct 2026):

```
 BTC  $80276  24h -0.28885%
 ETH  $2328.48  24h -0.52341%
 SOL  $101.4  24h 0.16896%
 XRP  $1.29  24h -0.72857%
flags: [ '1 high-importance event in the next 48h: FOMC rate decision (2026-10-28 14:00 ET)' ]
payer: 0xCb535d13C7fC5Fe6C2B88da3f3601B840CCaf8Bc
amount (atomic USDC): 10000
transaction: 0xdf23be05e48991f340998b84206e3c0a944b22ba8a54945831faa330fabfb6cf
https://basescan.org/tx/0xdf23be05e48991f340998b84206e3c0a944b22ba8a54945831faa330fabfb6cf
```

Or run it by hand:

```bash
npm run dev
curl -X POST http://127.0.0.1:8787/brief   # pays $0.01, returns the brief + payment proof
curl http://127.0.0.1:8787/brief           # free: the stored state
```

Without `TRIGGER_TOKEN`, paid triggers are only accepted on localhost.

## Deploy

```bash
npx wrangler secret put AGENT_PRIVATE_KEY
npx wrangler secret put TRIGGER_TOKEN
npm run deploy
curl -X POST -H "Authorization: Bearer $TRIGGER_TOKEN" https://<your-worker>.workers.dev/brief
```

## How payment works

1. `quoteAtomic()` reads the price from the unpaid **HTTP 402** `PAYMENT-REQUIRED` header (free) and refuses anything above the cap.
2. `wrapFetchWithPayment` repeats the request. On the 402 it signs an EIP-3009 USDC authorization on Base (`eip155:8453`) for exactly that amount and retries. Settlement is gasless for the payer.
3. **HTTP 200** JSON comes back, plus a `PAYMENT-RESPONSE` header. `decodePaymentResponseHeader` gives the transaction hash, which the agent stores in `lastPayment`.

A 404 or an error is never charged.

## Files

- `src/index.ts`: the agent, the spend guards and the Worker routes
- `wrangler.jsonc`: the Durable Object binding, SQLite migration and vars
- `scripts/paid-call.mjs`: the one-command paid test

## Credits

Built by ScrapeMole (https://scrapemole.pages.dev), which also sells pay-per-call scraping APIs for agents over x402. Data and API by Blockchain IQ (https://www.blockchainiq.co). Market data, not investment advice.
