Skip to content

Commit 494ccbc

Browse files
committed
feat(cli): add withdraw for Aave aTokens and ERC-4626 vaults
- withdraw: POOL/UNDERLYING + Pool.withdraw; ERC-4626 redeem (max or convertToShares) - docs: README, skills, polygon-defi; CHANGELOG Unreleased - fix: strip trailing slash in getExplorerUrl
1 parent 0a4f50c commit 494ccbc

12 files changed

Lines changed: 486 additions & 10 deletions

File tree

packages/connector-ui/public/SKILL.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: Polygon Agent
3-
description: "Complete Polygon agent toolkit for on-chain operations on Polygon. Use this skill whenever helping an agent set up a wallet, check balances, send or swap tokens, bridge assets, deposit to earn yield, register on-chain identity, submit or query reputation/feedback, or make x402 micropayments. Covers the full lifecycle: Sequence smart contract wallets, Trails DeFi actions, ERC-8004 identity + reputation, x402 payments. Single CLI entry point (`polygon-agent`), AES-256-GCM encrypted storage."
3+
description: "Complete Polygon agent toolkit for on-chain operations on Polygon. Use this skill whenever helping an agent set up a wallet, check balances, send or swap tokens, bridge assets, deposit or withdraw from yield (Aave aTokens, ERC-4626 vaults), register on-chain identity, submit or query reputation/feedback, or make x402 micropayments. Covers the full lifecycle: Sequence smart contract wallets, Trails DeFi actions, ERC-8004 identity + reputation, x402 payments. Single CLI entry point (`polygon-agent`), AES-256-GCM encrypted storage."
44
---
55

66
# Polygon Agentic CLI
@@ -105,6 +105,7 @@ polygon-agent send-native --to <addr> --amount <num> [--broadcast] [--direct]
105105
polygon-agent send-token --symbol <SYM> --to <addr> --amount <num> [--token <addr>] [--decimals <n>] [--broadcast]
106106
polygon-agent swap --from <SYM> --to <SYM> --amount <num> [--to-chain <chain>] [--slippage <num>] [--broadcast]
107107
polygon-agent deposit --asset <SYM> --amount <num> [--protocol aave|morpho] [--broadcast]
108+
polygon-agent withdraw --position <addr> --amount <num|max> [--chain <chain>] [--broadcast]
108109
polygon-agent fund [--wallet <n>] [--token <addr>]
109110
polygon-agent x402-pay --url <url> --wallet <n> [--method GET] [--body <str>] [--header Key:Value]
110111
```
@@ -131,6 +132,7 @@ polygon-agent agent feedback --agent-id <id> --value <score> [--tag1 <t>] [--tag
131132
- **Fee preference** — auto-selects USDC over native POL when both available
132133
- **`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.
133134
- **`deposit`** — picks highest-TVL pool via Trails `getEarnPools`. If session rejects (contract not whitelisted), re-create wallet with `--contract <depositAddress>`
135+
- **`withdraw`**`--position` = aToken or ERC-4626 vault; `--amount` = `max` or underlying units (Aave / vault). Dry-run JSON includes `poolAddress` / `vault`. Broadcast needs session on the **same chain** as `--chain`, with pool/vault + underlying token whitelisted where the relayer touches them
134136
- **`x402-pay`** — probes endpoint for 402, smart wallet funds builder EOA with exact token amount, EOA signs EIP-3009 payment. Chain auto-detected from 402 response
135137
- **`send-native --direct`** — bypasses ValueForwarder contract for direct EOA transfer
136138
- **Session permissions** — without `--usdc-limit` etc., session gets bare-bones defaults and may not transact
@@ -163,6 +165,7 @@ CLI commands output JSON (non-TTY). After running a command, always render the r
163165
| `send` / `send-token` / `send-native` | One-liner summary: amount, symbol, recipient. If broadcast, show tx hash as a code span and explorer URL as a link. |
164166
| `swap` | Summary: `X FROM → Y TO` with chain. If broadcast, show deposit tx hash + explorer link. |
165167
| `deposit` | Summary: amount, asset, protocol, pool address. If broadcast, show tx hash + explorer link. |
168+
| `withdraw` | Summary: `kind` (aave / erc4626), position, amount, pool or vault. If broadcast, show tx hash + explorer link. |
166169
| `fund` | Show the `fundingUrl` as a clickable link with a brief instruction to open it. |
167170
| `wallet create` / `wallet list` | Wallet name, truncated address, chain in a small table or bullet list. |
168171
| `agent register` | Show agent name and tx hash as a code span with Polygonscan link. Remind user to retrieve `agentId` from the Registered event on the Logs tab. |
@@ -183,7 +186,7 @@ For specific workflows, fetch and load the relevant sub-skill:
183186
| Use Case | Skill URL |
184187
|----------|-----------|
185188
| Polymarket prediction market trading | https://agentconnect.polygon.technology/polygon-polymarket/SKILL.md |
186-
| DeFi — swap, deposit, yield | https://agentconnect.polygon.technology/polygon-defi/SKILL.md |
189+
| DeFi — swap, deposit, withdraw, yield | https://agentconnect.polygon.technology/polygon-defi/SKILL.md |
187190
| x402 discovery & pay-per-call APIs | https://agentconnect.polygon.technology/polygon-discovery/SKILL.md |
188191

189192
---
@@ -201,6 +204,8 @@ For specific workflows, fetch and load the relevant sub-skill:
201204
| `Invalid code: hash mismatch` | Wrong 6-digit code entered — retry (3 attempts allowed) |
202205
| `Relay request not found` | Session expired or already used — re-run `wallet create` (or `wallet create --print-url`) |
203206
| Deposit session rejected | Re-create wallet with `--contract <depositAddress>` |
207+
| `withdraw` / broadcast: wrong chain or session rejects | Use `wallet create --chain <same as --chain>` and `--contract` for pool/vault + underlying ERC-20 on that chain; omit tight `--usdc-limit` if it blocks fee transfers |
208+
| `Stored explicit session is missing pk` | Re-link: `wallet import --code …` after `wallet create` |
204209
| Wrong recipient in Trails widget | Run `polygon-agent fund` (do not construct the URL manually) |
205210
| `x402-pay`: no 402 response | Endpoint doesn't require x402 payment, or URL is wrong |
206211
| `x402-pay`: payment token mismatch | Chain/token in the 402 response differs from wallet — check `--wallet` points to the right chain |

packages/connector-ui/public/skills/SKILL.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: Polygon Agent
3-
description: "Complete Polygon agent toolkit for on-chain operations on Polygon. Use this skill whenever helping an agent set up a wallet, check balances, send or swap tokens, bridge assets, deposit to earn yield, register on-chain identity, submit or query reputation/feedback, or make x402 micropayments. Covers the full lifecycle: Sequence smart contract wallets, Trails DeFi actions, ERC-8004 identity + reputation, x402 payments. Single CLI entry point (`polygon-agent`), AES-256-GCM encrypted storage."
3+
description: "Complete Polygon agent toolkit for on-chain operations on Polygon. Use this skill whenever helping an agent set up a wallet, check balances, send or swap tokens, bridge assets, deposit or withdraw from yield (Aave aTokens, ERC-4626 vaults), register on-chain identity, submit or query reputation/feedback, or make x402 micropayments. Covers the full lifecycle: Sequence smart contract wallets, Trails DeFi actions, ERC-8004 identity + reputation, x402 payments. Single CLI entry point (`polygon-agent`), AES-256-GCM encrypted storage."
44
---
55

66
# Polygon Agentic CLI
@@ -105,6 +105,7 @@ polygon-agent send-native --to <addr> --amount <num> [--broadcast] [--direct]
105105
polygon-agent send-token --symbol <SYM> --to <addr> --amount <num> [--token <addr>] [--decimals <n>] [--broadcast]
106106
polygon-agent swap --from <SYM> --to <SYM> --amount <num> [--to-chain <chain>] [--slippage <num>] [--broadcast]
107107
polygon-agent deposit --asset <SYM> --amount <num> [--protocol aave|morpho] [--broadcast]
108+
polygon-agent withdraw --position <addr> --amount <num|max> [--chain <chain>] [--broadcast]
108109
polygon-agent fund [--wallet <n>] [--token <addr>]
109110
polygon-agent x402-pay --url <url> --wallet <n> [--method GET] [--body <str>] [--header Key:Value]
110111
```
@@ -131,6 +132,7 @@ polygon-agent agent feedback --agent-id <id> --value <score> [--tag1 <t>] [--tag
131132
- **Fee preference** — auto-selects USDC over native POL when both available
132133
- **`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.
133134
- **`deposit`** — picks highest-TVL pool via Trails `getEarnPools`. If session rejects (contract not whitelisted), re-create wallet with `--contract <depositAddress>`
135+
- **`withdraw`**`--position` = aToken or ERC-4626 vault; `--amount` = `max` or underlying units (Aave / vault). Dry-run JSON includes `poolAddress` / `vault`. Broadcast needs session on the **same chain** as `--chain`, with pool/vault + underlying token whitelisted where the relayer touches them
134136
- **`x402-pay`** — probes endpoint for 402, smart wallet funds builder EOA with exact token amount, EOA signs EIP-3009 payment. Chain auto-detected from 402 response
135137
- **`send-native --direct`** — bypasses ValueForwarder contract for direct EOA transfer
136138
- **Session permissions** — without `--usdc-limit` etc., session gets bare-bones defaults and may not transact
@@ -163,6 +165,7 @@ CLI commands output JSON (non-TTY). After running a command, always render the r
163165
| `send` / `send-token` / `send-native` | One-liner summary: amount, symbol, recipient. If broadcast, show tx hash as a code span and explorer URL as a link. |
164166
| `swap` | Summary: `X FROM → Y TO` with chain. If broadcast, show deposit tx hash + explorer link. |
165167
| `deposit` | Summary: amount, asset, protocol, pool address. If broadcast, show tx hash + explorer link. |
168+
| `withdraw` | Summary: `kind` (aave / erc4626), position, amount, pool or vault. If broadcast, show tx hash + explorer link. |
166169
| `fund` | Show the `fundingUrl` as a clickable link with a brief instruction to open it. |
167170
| `wallet create` / `wallet list` | Wallet name, truncated address, chain in a small table or bullet list. |
168171
| `agent register` | Show agent name and tx hash as a code span with Polygonscan link. Remind user to retrieve `agentId` from the Registered event on the Logs tab. |
@@ -183,7 +186,7 @@ For specific workflows, fetch and load the relevant sub-skill:
183186
| Use Case | Skill URL |
184187
|----------|-----------|
185188
| Polymarket prediction market trading | https://agentconnect.polygon.technology/polygon-polymarket/SKILL.md |
186-
| DeFi — swap, deposit, yield | https://agentconnect.polygon.technology/polygon-defi/SKILL.md |
189+
| DeFi — swap, deposit, withdraw, yield | https://agentconnect.polygon.technology/polygon-defi/SKILL.md |
187190
| x402 discovery & pay-per-call APIs | https://agentconnect.polygon.technology/polygon-discovery/SKILL.md |
188191

189192
---
@@ -201,6 +204,8 @@ For specific workflows, fetch and load the relevant sub-skill:
201204
| `Invalid code: hash mismatch` | Wrong 6-digit code entered — retry (3 attempts allowed) |
202205
| `Relay request not found` | Session expired or already used — re-run `wallet create` (or `wallet create --print-url`) |
203206
| Deposit session rejected | Re-create wallet with `--contract <depositAddress>` |
207+
| `withdraw` / broadcast: wrong chain or session rejects | Use `wallet create --chain <same as --chain>` and `--contract` for pool/vault + underlying ERC-20 on that chain; omit tight `--usdc-limit` if it blocks fee transfers |
208+
| `Stored explicit session is missing pk` | Re-link: `wallet import --code …` after `wallet create` |
204209
| Wrong recipient in Trails widget | Run `polygon-agent fund` (do not construct the URL manually) |
205210
| `x402-pay`: no 402 response | Endpoint doesn't require x402 payment, or URL is wrong |
206211
| `x402-pay`: payment token mismatch | Chain/token in the 402 response differs from wallet — check `--wallet` points to the right chain |

packages/connector-ui/public/skills/polygon-defi/SKILL.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ polygon-agent deposit --asset USDC --amount 0.3 --protocol morpho --broadcast
129129

130130
Vault/pool addresses are resolved dynamically from Trails — they are not hardcoded. The dry-run output includes `depositAddress` so you can inspect the exact contract before broadcasting.
131131

132+
## Withdraw (Aave aToken or ERC-4626 vault)
133+
134+
Pass the **position token** you hold: an **Aave aToken** address, or a **Morpho / ERC-4626 vault** (share) address. The CLI resolves the Aave **Pool** via `POOL()` on the aToken, or uses `redeem` on the vault. Dry-run by default.
135+
136+
```bash
137+
# Full exit from an Aave position (aToken from balances output)
138+
polygon-agent withdraw --position 0x68215b6533c47ff9f7125ac95adf00fe4a62f79e --amount max --chain mainnet
139+
140+
# Partial Aave withdraw (underlying units, e.g. USDC)
141+
polygon-agent withdraw --position <aToken> --amount 0.5 --chain mainnet --broadcast
142+
143+
# ERC-4626: max redeems all shares; partial amount is underlying units (convertToShares)
144+
polygon-agent withdraw --position <vault> --amount max --chain polygon --broadcast
145+
```
146+
147+
Whitelist the **pool** (Aave) or **vault** contract on the session if the wallet rejects the call (`polygon-agent wallet create --contract <poolOrVault>`).
148+
149+
**Same chain as the transaction:** if you use `withdraw --chain mainnet`, create or refresh the session with **`wallet create --chain mainnet`** (not only Polygon defaults). Include **`--contract`** for the **pool** and for the **underlying ERC-20** on that chain (e.g. mainnet USDC `0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48`) so fee / helper transfers are allowed. Tight **`--usdc-limit`** can block those — omit or relax for yield exits.
150+
132151
### Session Whitelisting
133152

134153
If the deposit is rejected with a session permission error, the pool's contract address needs to be whitelisted when creating the wallet session:

packages/polygon-agent-cli/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [Unreleased]
7+
8+
### Features
9+
10+
* **cli:** `withdraw` command — exit Aave v3 via aToken (`Pool.withdraw`) or ERC-4626 vaults (`redeem`); dry-run shows pool/vault and calldata ([docs](README.md#cli-reference))
11+
12+
### Bug Fixes
13+
14+
* **cli:** avoid double slash in transaction explorer URLs when `blockExplorer.rootUrl` ends with `/`
15+
616
## [0.7.2](https://github.com/0xPolygon/polygon-agent-cli/compare/@polygonlabs/agent-cli@0.7.1...@polygonlabs/agent-cli@0.7.2) (2026-04-14)
717

818

packages/polygon-agent-cli/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Wallet sessions are created through a secure handshake between the CLI, the Conn
124124
| ------------ | -------------------------------------------------------------------------------------------- | ------------------------------- |
125125
| **Bridging** | Move assets cross-chain into your Polygon wallet and fund the initial flows to your wallet | `fund` |
126126
| **Swapping** | Token swaps with configurable slippage seamlessly built in | `swap` |
127-
| **Actions** | Composable onchain operations (deposit into a DeFi vault, stake with your favorite protocol) | `send`, `deposit`, `send-token` |
127+
| **Actions** | Composable onchain operations (deposit / withdraw from yield, send tokens) | `send`, `deposit`, `withdraw`, `send-token` |
128128

129129
### Onchain Agentic Identity
130130

@@ -179,8 +179,14 @@ polygon-agent balances --chains polygon,base,arbitrum # Same wallet, multiple c
179179
polygon-agent send --to 0x... --amount 1.0 # Send POL (dry-run)
180180
polygon-agent send --symbol USDC --to 0x... --amount 10 --broadcast
181181
polygon-agent swap --from USDC --to USDT --amount 5 --broadcast
182+
polygon-agent withdraw --position <aToken-or-vault> --amount max [--chain <chain>] # dry-run; add --broadcast
183+
polygon-agent withdraw --position <aToken> --amount 0.5 --chain mainnet --broadcast # partial (underlying units)
182184
```
183185

186+
**`withdraw`** exits **Aave v3** positions using your **aToken** address (`POOL()` + `UNDERLYING_ASSET_ADDRESS()``Pool.withdraw`), or **ERC-4626** vaults (e.g. Morpho) via `redeem`. Dry-run prints `poolAddress` / `vault` and calldata.
187+
188+
For **`--broadcast`**, the session must allow the **pool or vault** and (on that chain) **fee / underlying ERC-20** touches — use `wallet create --chain <same-as-tx> --contract <pool> --contract <underlying-USDC-or-asset>`. If your default session is Polygon but you transact on **mainnet**, create or extend a **mainnet** session for that chain.
189+
184190
### Agent Registry (ERC-8004)
185191

186192
```bash

0 commit comments

Comments
 (0)