# NEAR Intents payment rail (ScrapeMole API)

Pay for any paid `/v1/*` endpoint with any token that NEAR Intents (1Click API) can route. ScrapeMole receives USDC.
This runs next to x402 (Base/Solana USDC). It does not replace it.

Status (2026-11-01): live on NEAR **mainnet** at `https://scrapemole-api-mainnet.scrapemole.workers.dev` (settles Base USDC inside NEAR Intents to the merchant's implicit-Eth account `0xcb53…f8bc`, `recipientType=INTENTS`, no withdraw fee; buyer pays about $0.00202 for a $0.002 call). Also on NEAR **testnet** through the Tidepool sandbox (a community-built, 1Click-compatible API that uses test tokens) at
`https://scrapemole-api.scrapemole.workers.dev`. 

## Do this
1. To see prices, tokens and steps, `GET /v1/intents`.
2. To get a deposit address tied to one exact call, `POST /v1/intents/quote` with `{"resource":"/v1/youtube/search?q=lofi","originAsset":"<1Click assetId>","refundTo":"<your account>"}`. This step is free.
   - The quote is EXACT_OUTPUT: the price in USDC goes to the merchant, and the deadline is 10 minutes.
3. To pay, fund `depositAddress` with `amountIn` of `originAsset`. You have two ways to do it:
   - from an intents balance, using `/v0/generate-intent`, then a NEP-413 signature, then `/v0/submit-intent`;
   - with a normal transfer on the origin chain.
4. To get the data, `GET <resource>` with the header `X-Intents-Deposit: <depositAddress>`. You can also send `X-Intents-Tx: <hash>` to speed up deposit detection.
   - `402 INTENTS_PENDING` means the payment hasn't settled yet. Retry after the `Retry-After` header (about 5 s).
   - `200` means you got the data. Proof of settlement is in the `x-intents-settlement` header (base64 JSON).
   - `409 INTENTS_DEPOSIT_USED` means that deposit was already used. Each deposit pays for one successful call.
5. For a full buyer demo, see `tools/intents-pay.mjs`. Run `node tools/intents-pay.mjs --api <url> --resource <path> [--faucet]`.

## Guarantees (see src/intents.js)
- Quotes are created by the server and stored in a Durable Object (`IntentsLedger`, SQLite). Each one is bound to an endpoint and a canonical input.
- A deposit is accepted only when 1Click `/v0/status` is `SUCCESS`, the recipient and destination asset match, and `amountOut` ≥ price.
- The deposit is claimed atomically with a 2-minute lock. It is consumed only when the call returns 200, so a failed call is never charged and can be retried with the same deposit.
- No private keys are held by the Worker. The merchant is only a receive address.

## Config (wrangler vars)
- `INTENTS_API_BASE`: the 1Click base URL.
- `INTENTS_RECIPIENT`: the receive address.
- `INTENTS_RECIPIENT_TYPE`: `INTENTS` or `DESTINATION_CHAIN`.
- `INTENTS_DEST_ASSET` and `INTENTS_DEST_DECIMALS`: the asset ScrapeMole receives.
- `INTENTS_NETWORK`: the network name.
- `ONECLICK_JWT` (optional secret): skips the 0.25% 1Click fee.

## MCP (added 2026-11-02, hackathon build day 2)
- Free tool `near_intents_quote` on POST /mcp: `{tool, arguments, originAsset, refundTo[, depositType, refundType]}` returns the same quote as POST /v1/intents/quote, bound to that exact tool call.
- Pay: `tools/call` the paid tool with the same arguments and `params._meta["near-intents/deposit"] = "<depositAddress>"` (or `{depositAddress, txHash}`). Pending swap returns isError with INTENTS_PENDING; success returns data plus `result._meta["near-intents/settlement"]`; replay returns INTENTS_DEPOSIT_USED.
- The unpaid tools/call PaymentRequired now carries `nearIntents` (with the MCP hint). Server card and llms.txt advertise it. Only tools served by this Worker accept Intents; remote tools stay x402-only.
