# Watcher SDK: faster confirmations with your own RPC

`aipay-watch` is a small Node library and CLI that watches **your own receiving
address** and tells AIPAY the moment it sees the money. You do not poll the AIPAY
API, and the SDK cannot confirm anything by itself.

- **Distribution:** `https://aipay.im/downloads/sdk-latest.json` (version, tarball,
  sha256, file list). Tarball + `install-sdk.sh`; no npm package, no registry.
- **Also readable as source:** `sdk/aipay-watch/` in
  <https://aipay.im/downloads/pubrepo/sdk/aipay-watch/> (canonical), mirrored at
  <https://github.com/aipayim/aipay-agent>.
- **Node:** ≥ 22.18. Nothing else to install.
- **Where this doc sits:** it is indexed in [`/llms.txt`](../llms.txt) and mirrors
  the merchant API section on `POST /v1/orders/:reference/confirm`.

## Why it exists

AIPAY's own sweep reads the chain once a minute per pending order. That is fine
for one shop and expensive for ten thousand, so the work can move to the party
that already runs an RPC: you scan your own address, we verify what you found.

| | our sweep | this SDK |
| --- | --- | --- |
| discovery latency | ~10–60 s | ~2–8 s |
| whose RPC quota | ours | yours |
| who decides "paid" | the server | the server (unchanged) |

## What it can and cannot do

It can: read your pending orders once at start-up, scan your address, and hand
AIPAY a transaction hash.

It cannot: confirm a payment, hold a key, move funds, or skip verification. Every
report is re-read by the server — receipt, recipient, amount, confirmation depth —
before an order becomes `confirmed`. A wrong hash is rejected with `409`; it is
never "trusted faster".

## Install and run

```bash
# Preferred: the installer from the canonical host (verifies the sha256, then installs)
curl -fsSL -o install-sdk.sh https://aipay.im/downloads/install-sdk.sh
AIPAY_WATCH_VERSION=<version> bash install-sdk.sh

# Or by hand:
curl -fsSL https://aipay.im/downloads/aipay-watch-<version>.tgz -o sdk.tgz
sha256sum sdk.tgz   # compare with /downloads/sdk-latest.json
tar -xzf sdk.tgz

node package/bin/aipay-watch.mjs \
  --api-key "$AIPAY_API_KEY" \
  --rpc bsc=https://bsc-dataseed.binance.org \
  --state .aipay-watch-state.json
```

Useful flags: `--read-only` (discover and print, never report — no API key
needed), `--once` (single pass, for cron), `--register-watcher` (also calls
`PATCH /v1/merchants/me {"watcher": true}`), `--interval`, `--reconcile`,
`--chains`, `--tron-api-key`, `--json`.

A Tron merchant needs no JSON-RPC endpoint: `--chains tron` alone watches the
public index, or `--rpc tron=https://api.trongrid.io` to be explicit. Mixing
chains is one flag: `--rpc bsc=https://…,tron=https://api.trongrid.io`.

## What it reports, and what the server answers

```http
POST /v1/orders/ap_…/confirm   { "txHash": "0x…" }
```

| answer | meaning |
| --- | --- |
| `200` | verified and confirmed (idempotent when the hash is repeated) |
| `202` | the transaction exists but is not deep enough yet — retry later |
| `409` | that transaction does not pay this order (wrong amount/recipient, already used) |
| `429` | the confirm budget (120/min per credential) is spent — back off |

## `watcher: true` changes who is responsible

`PATCH /v1/merchants/me {"watcher": true}` makes AIPAY **stop** sweeping your
pending orders, because someone else is already watching them. Turn it on only
while this SDK (or a payer's browser) is actually running: without the flag both
paths work at the same time, which is slower but harmless; with it, the SDK is
the fast path and the cron is silent for you.

## Coverage and limits

- **EVM:** ERC-20 transfers are found with `eth_getLogs` (token + recipient +
  amount). Native-coin orders have no logs, so they still need a transaction
  hash — the CLI says so once per chain.
- **Solana:** recent signatures of your address, parsed and matched on the
  received balance delta (SPL mint or native lamports). An `ap_…` order has no
  per-order on-chain reference, so **two pending orders with the same amount to
  one address are ambiguous** — use distinct amounts.
- **Tron (TRC20 and native TRX):** TronGrid's account index —
  `GET /v1/accounts/<address>/transactions/trc20` for TRC20 and
  `GET /v1/accounts/<address>/transactions` for TRX — read from the oldest still
  pending order's time minus 60 s, filtered to incoming rows. Native TRX is
  covered here because Tron's index lists transfers, not only contract logs.
  Tron has no per-order on-chain reference either, so the same **same-amount
  ambiguity** applies. `--rpc tron=<base>` is an **HTTP index base** (TronGrid, or
  a mirror speaking the same `/v1/accounts/…` API), not a JSON-RPC endpoint; it
  may be omitted to use `https://api.trongrid.io`. The keyless index is rate
  limited under load (HTTP 429/451), so pass `--tron-api-key` (or
  `TRON_PRO_API_KEY`) if you watch more than a few orders a minute.
- **Public RPC blocked?** AIPAY exposes a read-only relay for the same five
  `eth_*` methods (`POST https://api.aipay.im/v1/rpc/<chain>`, 5,000-block
  `eth_getLogs` cap, 120 requests/min per IP, no signing). Passing
  `--rpc bsc=https://api.aipay.im/v1/rpc/bsc` keeps a watcher running when a
  public node starts rejecting browsers or your server's IP — it is a fallback,
  not a replacement for your own endpoint.
- **One tarball, one sha256:** `sdk-latest.json` lists every file and its hash, so
  a release can be diffed. `install-sdk.sh` verifies the checksum before it
  extracts anything.
- **No sandbox:** the API is mainnet-only, so a test order is a real (small)
  order. Use `--read-only` to try discovery without reporting anything.
