Polygon Agent Kit
Prerequisites
- - Node.js 20+
- Install globally: INLINECODE0
- Entry point: INLINECODE1
- Storage:
~/.polygon-agent/ (AES-256-GCM encrypted)
Architecture
| Wallet | Created by | Purpose | Fund? |
|---|
| EOA | INLINECODE3 | Auth with Sequence Builder | NO |
| Ecosystem Wallet |
wallet create | Primary spending wallet | YES |
Environment Variables
Required
| Variable | When |
|---|
| INLINECODE5 | Wallet creation, swaps |
| INLINECODE6 |
Balance checks |
Optional
| Variable | Default |
|---|
| INLINECODE7 | INLINECODE8 |
| INLINECODE9 |
Same as connector URL origin |
|
TRAILS_API_KEY | Falls back to
SEQUENCE_PROJECT_ACCESS_KEY |
|
TRAILS_TOKEN_MAP_JSON | Token-directory lookup |
|
POLYGON_AGENT_DEBUG_FETCH | Off — logs HTTP to
~/.polygon-agent/fetch-debug.log |
|
POLYGON_AGENT_DEBUG_FEE | Off — dumps fee options to stderr |
Complete Setup Flow
CODEBLOCK0
Commands Reference
Setup
CODEBLOCK1
Wallet
CODEBLOCK2
Operations
CODEBLOCK3
Agent (ERC-8004)
CODEBLOCK4
ERC-8004 contracts (Polygon mainnet):
- - IdentityRegistry: INLINECODE16
- ReputationRegistry: INLINECODE17
Key Behaviors
- - Dry-run by default — all write commands require
--broadcast to execute - Smart defaults —
--wallet main, --chain polygon, auto-wait on INLINECODE21 - Fee preference — auto-selects USDC over native POL when both available
fund — reads walletAddress from the wallet session and sets it as toAddress in the Trails widget URL. Always run polygon-agent fund to get the correct URL — never construct it manually or hardcode any address. The returned JSON contains fundingUrl and walletAddress so you can confirm the pre-filled recipient before sharing.deposit — picks highest-TVL pool via Trails getEarnPools. If session rejects, re-create wallet with INLINECODE30x402-pay — probes endpoint for 402, smart wallet funds builder EOA with exact token amount, EOA signs EIP-3009 payment. Chain auto-detected from 402 responsesend-native --direct — bypasses ValueForwarder contract for direct EOA transfer- Session permissions — without
--usdc-limit etc., session gets bare-bones defaults and may not transact
CRITICAL: Wallet Approval URL
When wallet create outputs a URL in the url or approvalUrl field, you MUST send the COMPLETE, UNTRUNCATED URL to the user. The URL contains cryptographic parameters (public key, callback token) that are required for session approval. If any part is cut off, the approval will fail.
- - Do NOT shorten, summarize, or add
... to the URL - Do NOT split the URL across multiple messages
- Output the raw URL exactly as returned by the CLI
Callback Modes
The wallet create command automatically starts a local HTTP server and opens a Cloudflare Quick Tunnel (*.trycloudflare.com) — no account or token required. The cloudflared binary is auto-downloaded to ~/.polygon-agent/bin/cloudflared on first use if not already installed. The connector UI POSTs the encrypted session back through the tunnel regardless of where the agent is running. The tunnel and server are torn down automatically once the session is received.
Timing: The approvalUrl is only valid while the CLI process is running. Open it immediately and complete wallet approval within the timeout window (default 300s). Never reuse a URL from a previous run — the tunnel is torn down when the CLI exits.
Manual fallback (if cloudflared is unavailable): The CLI omits callbackUrl so the connector UI displays the encrypted blob in the browser. The CLI then prompts:
After approving in the browser, the encrypted blob will be shown.
Paste it below and press Enter:
> <paste blob here>
The blob is also saved to
/tmp/polygon-session-<rid>.txt for reference. To import later:
CODEBLOCK6
Troubleshooting
| Issue | Fix |
|---|
| INLINECODE45 | Add INLINECODE46 |
| INLINECODE47 |
Run
setup first |
|
Missing wallet |
wallet list, re-run
wallet create |
|
Session expired | Re-run
wallet create (24h expiry) |
|
Fee option errors | Set
POLYGON_AGENT_DEBUG_FEE=1, ensure wallet has funds |
|
Timed out waiting for callback | Add
--timeout 600 |
|
callbackMode: manual (no tunnel) | cloudflared unavailable — paste blob from browser when prompted; blob saved to
/tmp/polygon-session-<rid>.txt |
|
404 on
*.trycloudflare.com | CLI timed out and tunnel is gone — re-run
wallet create, open the new
approvalUrl immediately |
|
"Auto-send failed" in browser | Copy the ciphertext shown below that message; run
wallet import --ciphertext '<blob>' |
| Deposit session rejected | Re-create wallet with
--contract <depositAddress> |
| Wrong recipient in Trails widget | Run
polygon-agent fund (do not construct the URL manually);
walletAddress in the output confirms the pre-filled
toAddress |
File Structure
CODEBLOCK7
Polygon Agent Kit
前置条件
- - Node.js 20+
- 全局安装:npm install -g github:0xPolygon/polygon-agent-kit
- 入口点:polygon-agent
- 存储:~/.polygon-agent/(AES-256-GCM 加密)
架构
| 钱包 | 创建方式 | 用途 | 需要资金? |
|---|
| EOA | setup | 使用 Sequence Builder 认证 | 否 |
| 生态系统钱包 |
wallet create | 主消费钱包 | 是 |
环境变量
必需变量
| 变量 | 使用场景 |
|---|
| SEQUENCEPROJECTACCESSKEY | 钱包创建、兑换 |
| SEQUENCEINDEXERACCESSKEY |
余额查询 |
可选变量
| 变量 | 默认值 |
|---|
| SEQUENCEECOSYSTEMCONNECTORURL | https://agentconnect.polygon.technology/ |
| SEQUENCEDAPP_ORIGIN |
与连接器 URL 来源相同 |
| TRAILS
APIKEY | 回退至 SEQUENCE
PROJECTACCESS_KEY |
| TRAILS
TOKENMAP_JSON | 代币目录查询 |
| POLYGON
AGENTDEBUG_FETCH | 关闭 — 将 HTTP 日志记录到 ~/.polygon-agent/fetch-debug.log |
| POLYGON
AGENTDEBUG_FEE | 关闭 — 将费用选项输出到 stderr |
完整设置流程
bash
阶段 1:设置(创建 EOA + Sequence 项目,返回访问密钥)
node cli/polygon-agent.mjs setup --name MyAgent
→ 保存 privateKey(不再显示)、eoaAddress、accessKey
阶段 2:创建生态系统钱包(自动等待浏览器批准)
export SEQUENCE
PROJECTACCESS_KEY=
node cli/polygon-agent.mjs wallet create --usdc-limit 100 --native-limit 5
阶段 3:为钱包充值
node cli/polygon-agent.mjs fund
→ 从会话中读取 walletAddress,构建 Trails 小部件 URL,其中 toAddress=
→ 始终运行此命令以获取 URL — 切勿手动构建或硬编码任何地址
→ 将返回的 fundingUrl 发送给用户;输出中的 walletAddress 确认收款方
阶段 4:验证
export SEQUENCEINDEXERACCESS_KEY=
node cli/polygon-agent.mjs balances
阶段 5:在链上注册代理(ERC-8004,Polygon 主网)
node cli/polygon-agent.mjs agent register --name MyAgent --broadcast
→ 铸造 ERC-721 NFT,在 Registered 事件中发出 agentId
→ 使用 agentId 进行声誉查询和反馈
命令参考
设置
bash
polygon-agent setup --name [--force]
钱包
bash
polygon-agent wallet create [--name ] [--chain polygon] [--timeout ] [--no-wait]
[--native-limit ] [--usdc-limit ] [--usdt-limit ]
[--token-limit ] # 可重复
[--usdc-to --usdc-amount ] # 一次性限定转账
[--contract ] # 白名单合约(可重复)
polygon-agent wallet import --ciphertext |@ [--name ] [--rid ]
polygon-agent wallet list
polygon-agent wallet address [--name ]
polygon-agent wallet remove [--name ]
操作
bash
polygon-agent balances [--wallet ] [--chain ]
polygon-agent send --to --amount [--symbol ] [--broadcast]
polygon-agent send-native --to --amount [--broadcast] [--direct]
polygon-agent send-token --symbol --to --amount [--broadcast]
polygon-agent swap --from --to --amount [--to-chain ] [--slippage ] [--broadcast]
polygon-agent deposit --asset --amount [--protocol aave|morpho] [--broadcast]
polygon-agent fund [--wallet ] [--token ]
polygon-agent x402-pay --url --wallet [--method GET] [--body ] [--header Key:Value]
代理(ERC-8004)
bash
polygon-agent agent register --name [--agent-uri ] [--metadata ] [--broadcast]
polygon-agent agent wallet --agent-id
polygon-agent agent metadata --agent-id --key
polygon-agent agent reputation --agent-id [--tag1 ]
polygon-agent agent reviews --agent-id
polygon-agent agent feedback --agent-id --value [--tag1 ] [--tag2 ] [--endpoint ] [--broadcast]
ERC-8004 合约(Polygon 主网):
- - IdentityRegistry:0x8004A169FB4a3325136EB29fA0ceB6D2e539a432
- ReputationRegistry:0x8004BAa17C55a88189AE136b182e5fdA19dE9b63
关键行为
- - 默认模拟运行 — 所有写入命令需要 --broadcast 才能执行
- 智能默认值 — --wallet main、--chain polygon、wallet create 自动等待
- 费用偏好 — 当两者都可用时,自动选择 USDC 而非原生 POL
- fund — 从钱包会话中读取 walletAddress,并将其设置为 Trails 小部件 URL 中的 toAddress。始终运行 polygon-agent fund 以获取正确的 URL — 切勿手动构建或硬编码任何地址。返回的 JSON 包含 fundingUrl 和 walletAddress,以便在分享前确认预填的收款方
- deposit — 通过 Trails getEarnPools 选择 TVL 最高的池。如果会话拒绝,使用 --contract 重新创建钱包
- x402-pay — 探测端点的 402 响应,智能钱包以精确代币数量为构建者 EOA 充值,EOA 签署 EIP-3009 支付。链从 402 响应中自动检测
- send-native --direct — 绕过 ValueForwarder 合约进行直接 EOA 转账
- 会话权限 — 如果没有 --usdc-limit 等参数,会话将获得最基本的默认值,可能无法进行交易
关键:钱包批准 URL
当 wallet create 在 url 或 approvalUrl 字段中输出 URL 时,您必须将完整、未截断的 URL 发送给用户。该 URL 包含会话批准所需的加密参数(公钥、回调令牌)。如果任何部分被截断,批准将失败。
- - 不要缩短、总结或在 URL 中添加 ...
- 不要将 URL 拆分为多条消息
- 原样输出 CLI 返回的原始 URL
回调模式
wallet create 命令会自动启动本地 HTTP 服务器并打开 Cloudflare Quick Tunnel(*.trycloudflare.com)— 无需账户或令牌。首次使用时,如果尚未安装,cloudflared 二进制文件会自动下载到 ~/.polygon-agent/bin/cloudflared。无论代理在哪里运行,连接器 UI 都会通过隧道 POST 加密的会话。一旦收到会话,隧道和服务器会自动拆除。
时机:approvalUrl 仅在 CLI 进程运行时有效。立即打开它并在超时窗口内(默认 300 秒)完成钱包批准。切勿重复使用之前运行的 URL — CLI 退出时隧道会被拆除。
手动回退(如果 cloudflared 不可用):CLI 省略 callbackUrl,因此连接器 UI 在浏览器中显示加密的 blob。