# ScrapeMole x NEAR Intents

NEAR HACKATHON entry. Track: Autonomous Business. Tags: agents.

Source: https://scrapemole.pages.dev/code/scrapemole-near-intents/

```bash
git clone https://scrapemole.pages.dev/code/scrapemole-near-intents.git
```

## What it is

ScrapeMole is a data API business with zero human staff. It is run by an AI and sells scraped public data to AI agents.
This repo is its NEAR Intents payment rail. With it, any agent can pay per call with any of the ~210 tokens that 1Click routes.
The merchant receives Base USDC inside NEAR Intents (`recipientType: INTENTS`, so there is no withdraw fee).

This repo is an excerpt of the production Cloudflare Worker. It contains only the NEAR Intents rail files. The rest of the API (`src/index.js`, scrapers) is closed for now. `index-intents-lines.txt` shows how `src/index.js` wires the rail in.

| File | Purpose |
|---|---|
| `src/intents.js` | 1Click quote, `IntentsLedger` Durable Object, `X-Intents-Deposit` gate |
| `src/mcp.js` | MCP server: free `near_intents_quote` tool, `_meta["near-intents/deposit"]` payment |
| `tools/intents-pay.mjs` | Buyer demo: quote, generate-intent, NEP-413 sign, submit-intent, poll, 200 |
| `docs/near-intents.md` | Rail docs |
| `index-intents-lines.txt` | Wiring lines from the closed `src/index.js` |

## Try it in 60 seconds (free, no wallet)

Base URL (mainnet): `https://scrapemole-api-mainnet.scrapemole.workers.dev`

1. To see the rail info, prices, and tokens:
   ```bash
   curl https://scrapemole-api-mainnet.scrapemole.workers.dev/v1/intents
   ```
   You get `rail`, `network`, `settlementAsset`, `recipient`, `how`, `prices`, `payWith`.

2. To see an unpaid call:
   ```bash
   curl -i "https://scrapemole-api-mainnet.scrapemole.workers.dev/v1/youtube/search?q=lofi"
   ```
   You get `402`. The body holds the x402 offer plus a `nearIntents` object (`network`, `quote`, `docs`, `header: "X-Intents-Deposit"`).

3. To get a quote (free, creates nothing on-chain):
   ```bash
   curl -X POST https://scrapemole-api-mainnet.scrapemole.workers.dev/v1/intents/quote \
     -H 'content-type: application/json' \
     -d '{"resource":"/v1/youtube/search?q=lofi","originAsset":"nep141:wrap.near","refundTo":"<your.near>"}'
   ```
   You get `201` with this shape:
   ```json
   {
     "depositAddress": "…",
     "originAsset": "nep141:wrap.near",
     "amountIn": "…", "amountInFormatted": "…", "amountInUsd": "…",
     "pays": { "resource": "/v1/youtube/search?q=lofi", "priceUsd": "0.002", "amountOut": "2000", "recipient": "…" },
     "deadline": "<ISO time, +10 min>",
     "next": "Fund the depositAddress, then GET … with header X-Intents-Deposit: …"
   }
   ```
   Observed Nov 1 2026: a $0.002 call costs about 0.000462 wNEAR (~$0.00202).

4. To use MCP, list the tools, then call the free quote tool:
   ```bash
   curl -X POST https://scrapemole-api-mainnet.scrapemole.workers.dev/mcp \
     -H 'content-type: application/json' \
     -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'

   curl -X POST https://scrapemole-api-mainnet.scrapemole.workers.dev/mcp \
     -H 'content-type: application/json' \
     -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"near_intents_quote","arguments":{"tool":"youtube_search","arguments":{"q":"lofi"},"originAsset":"nep141:wrap.near","refundTo":"<your.near>"}}}'
   ```
   You get `result.structuredContent` with the same quote fields (`depositAddress`, `amountIn`, `deadline`, `next`).

## Pay flow

**HTTP**
1. `POST /v1/intents/quote` returns `depositAddress` and `amountIn`.
2. Fund `depositAddress` with `amountIn` of `originAsset`. Use 1Click `/v0/generate-intent`, then a NEP-413 signature, then `/v0/submit-intent`. Or send a plain transfer on the origin chain.
3. `GET <resource>` with header `X-Intents-Deposit: <depositAddress>`. Optionally add `X-Intents-Tx: <hash>` to speed up detection.

**MCP**
1. `tools/call near_intents_quote` with `{tool, arguments, originAsset, refundTo}`.
2. Fund the deposit, as in HTTP step 2.
3. `tools/call <tool>` with the same arguments and `params._meta["near-intents/deposit"] = "<depositAddress>"` (or `{depositAddress, txHash}`).

**Responses**

| HTTP | Code | Meaning | MCP equivalent |
|---|---|---|---|
| 402 | `INTENTS_PENDING` + `Retry-After: 5` | Swap not settled. Retry. | `isError`, `INTENTS_PENDING` |
| 200 | `x-intents-settlement` header (base64 JSON) | Data served. Deposit consumed. | `result._meta["near-intents/settlement"]` |
| 409 | `INTENTS_DEPOSIT_USED` | Deposit already paid for a call. Get a new quote. | `isError`, `INTENTS_DEPOSIT_USED` |
| 402 | `INTENTS_UNKNOWN_DEPOSIT`, `INTENTS_WRONG_RESOURCE`, `INTENTS_MISMATCH` | Not issued here, wrong call, or wrong payout. | same codes |
| 429 | `INTENTS_BUSY` | Another call holds the 2-min lock. | same code |

The settlement JSON holds `rail`, `network`, `depositAddress`, `asset`, `amountOut`, `recipient`, `originAsset`, `nearTxHashes`.

## Guarantees / design

- The server creates quotes and stores them in a SQLite Durable Object (`IntentsLedger`). Each quote is bound to one endpoint and one canonical input. A buyer cannot reuse someone else's deposit or pay for a different call.
- Quotes are `EXACT_OUTPUT`: the merchant receives exactly the endpoint price. The deadline is 10 minutes.
- A deposit is accepted only when 1Click `/v0/status` is `SUCCESS`, the recipient and destination asset match, and `amountOut` ≥ price.
- The claim is atomic with a 2-minute lock.
- The deposit is consumed only on a 200 response. Failed calls (4xx/5xx) are never charged, and the same deposit can be retried.
- The Worker holds no private keys. The merchant is only a receive address.

## Autonomous business angle

An AI operator agent runs the whole business on daily routines: code, deploys, listings, marketing, monitoring, and revenue tracking. The human owner handles only payouts and legal.

Revenue can be checked on-chain, with no trust in us:
- Merchant wallet on Base (x402): `0xCb535d13C7fC5Fe6C2B88da3f3601B840CCaf8Bc` ([USDC transfers on Basescan](https://basescan.org/token/0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913?a=0xCb535d13C7fC5Fe6C2B88da3f3601B840CCaf8Bc))
- Its NEAR Intents implicit account: `0xcb535d13c7fc5fe6c2b88da3f3601b840ccaf8bc` (USDC token `nep141:base-0x833589fcd6edb6e08f4c7c32d4f71b54bda02913.omft.near` in `intents.near`)
- Solana USDC account (x402): `3KeAbVBMJzF9H2XEg82G6uXrRtTcsAxpg34VRkdbCNfu`

### Treasury view

Do: `node tools/treasury.mjs` (or `--json`). Node 18+, no install, no keys, read-only.
See: current USDC on all three rails, read from public RPCs (Base `balanceOf`, `intents.near` `mt_balance_of`, Solana `getTokenAccountBalance`), plus explorer links.

The operator agent runs the same read every night and logs it to the owner's private dashboard. Moving funds (for example batching the Intents balance out to Base) always needs the human owner's explicit OK; the agent never holds that decision.

Numbers as of Nov 4 2026: about $0.56 received from 4 paying wallets over 154 x402 payments; current balance 0.044 USDC on Base (0.45 USDC was spent on Oct 26 to fund the Solana rail), 0 on NEAR Intents (no Intents payments yet), 0 on Solana. The market is small. This is real, not simulated.

## Before vs during the hackathon (Nov 1-14 2026)

**Before**
- ScrapeMole x402 API (live since Oct 11 2026).
- Apify actors.
- Oct 28: TESTNET prototype of the Intents rail via Tidepool. Payment tx: https://testnet.nearblocks.io/txns/GtLhNMsisgzviqLaHL7TVqjbWiKSTNJqrZvyASzK8s2R. Settlement tx: https://testnet.nearblocks.io/txns/HZB3N8Z46prNyfVB12Dsj7peZBKLxcXFdjmTzDDKZSjC.

**During**
- Nov 1: mainnet rail. Quoting uses a partner key. The recipient is `INTENTS`, which avoids the 0.0024 USDC withdraw fee.
- Nov 2: MCP pay tool (`near_intents_quote` + `_meta["near-intents/deposit"]`).
- Nov 3: this public repo and README.
- Nov 4: treasury view (`tools/treasury.mjs`).

**Planned by Nov 14**
- Demo video.

## Run the buyer demo

```bash
npm i bs58
node tools/intents-pay.mjs --api <url> --resource <path> [--faucet]
```

- Testnet API: `https://scrapemole-api.scrapemole.workers.dev`. Use `--faucet` here; it is testnet only and calls the Tidepool faucet.
- Example: `node tools/intents-pay.mjs --api https://scrapemole-api.scrapemole.workers.dev --resource "/v1/youtube/search?q=lofi&limit=3" --faucet`
- Key: an ed25519 NEAR implicit account in `BUYER_KEY_FILE` (default `~/.scrapemole-buyer/near-testnet-buyer.json`). The script refuses non-testnet keys unless you pass `--mainnet`. The key stays local and is never sent.
- Output: quote, intent hash, `pending` lines, `200 OK`, settlement JSON, explorer links, and a replay check (`409 INTENTS_DEPOSIT_USED`).

## Config (wrangler vars)

| Var | Meaning |
|---|---|
| `INTENTS_API_BASE` | 1Click base URL (`https://1click.chaindefuser.com` mainnet, `https://www.usetidepool.tech` testnet) |
| `INTENTS_RECIPIENT` | Receive address |
| `INTENTS_RECIPIENT_TYPE` | `INTENTS` or `DESTINATION_CHAIN` |
| `INTENTS_DEST_ASSET` | 1Click asset id ScrapeMole receives |
| `INTENTS_DEST_DECIMALS` | Decimals of that asset (default 6) |
| `INTENTS_NETWORK` | `near:mainnet` or `near:testnet` |
| `ONECLICK_JWT` (secret, optional) | Skips the 0.25% 1Click fee |

The rail is off unless `INTENTS_API_BASE` and `INTENTS_RECIPIENT` are set.

## License

MIT
