> ## Documentation Index
> Fetch the complete documentation index at: https://docs.polygon.technology/llms.txt
> Use this file to discover all available pages before exploring further.

# Swaps and yield

> DEX swaps, cross-chain bridging, and Trails earn pools (Aave v3, Morpho, ERC-4626), all under the smart wallet.

Three commands cover treasury movement beyond simple transfers. `swap` routes through the Trails DEX API, including cross-chain swaps via `--to-chain`. `deposit` parks idle balances in Trails earn pools (Aave v3, Morpho, ERC-4626 vaults). `withdraw` exits those positions, with auto-discovery if you know only the asset and protocol. The CLI picks the highest-TVL pool by default; pass `--protocol` to constrain.

All three are dry-run by default. Pass `--broadcast` to execute.

***

## How it works

<div style={{border:"1px solid #C8CFE1",borderRadius:"12px",overflow:"hidden",marginBottom:"24px"}}>
  <div style={{background:"linear-gradient(180deg,#EAE4F5 0%,#F6F3FB 100%)",borderBottom:"1px solid #D5C4F2",padding:"10px 16px",fontSize:"11px",fontWeight:"700",color:"#670DE5",letterSpacing:"0.06em",textTransform:"uppercase"}}>Treasury sweep flow</div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>1</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Operator</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>CLI</span>
    <span style={{fontFamily:"'Geist Mono',ui-monospace,monospace",fontSize:"12px",color:"#141635"}}>polygon-agent swap --from USDT --to USDC ...</span>
  </div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>2</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>CLI</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EAE4F5",color:"#670DE5",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"700",whiteSpace:"nowrap"}}>Trails</span>
    <span style={{fontSize:"13px",color:"#141635"}}>Routes swap quote and gas across DEX aggregators</span>
  </div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>3</span>
    <span style={{background:"#EAE4F5",color:"#670DE5",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"700",whiteSpace:"nowrap"}}>Smart wallet</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Polygon</span>
    <span style={{fontSize:"13px",color:"#141635"}}>Swap executes; output token received</span>
  </div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>4</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Operator</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>CLI</span>
    <span style={{fontFamily:"'Geist Mono',ui-monospace,monospace",fontSize:"12px",color:"#141635"}}>polygon-agent deposit --asset USDC --amount 1000</span>
  </div>

  <div style={{padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>5</span>
    <span style={{background:"#EAE4F5",color:"#670DE5",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"700",whiteSpace:"nowrap"}}>Trails</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EAE4F5",color:"#670DE5",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"700",whiteSpace:"nowrap"}}>Aave / Morpho</span>
    <span style={{fontSize:"13px",color:"#141635"}}>Deposits into highest-TVL pool by default</span>
  </div>
</div>

`--defi` at `wallet create` time gives the smart wallet permission to interact with Aave v3, Morpho, and ERC-4626 vaults. Without `--defi`, deposit and withdraw fail with a session-rejection error before any funds move.

***

## Commands

### swap

```bash theme={null}
polygon-agent swap --from USDC --to WETH --amount 100
polygon-agent swap --from USDC --to USDC --amount 100 --to-chain base --broadcast
```

DEX swap routed through Trails. Pass `--to-chain` to bridge during the swap.

| Flag          | Default  | Purpose                                 |
| ------------- | -------- | --------------------------------------- |
| `--wallet`    | `main`   | Wallet name                             |
| `--chain`     | none     | Source chain                            |
| `--from`      | required | Source token symbol                     |
| `--to`        | required | Destination token symbol                |
| `--amount`    | required | Amount to swap                          |
| `--slippage`  | none     | Tolerance 0–0.5 (e.g. `0.005` for 0.5%) |
| `--to-chain`  | none     | Destination chain for cross-chain swap  |
| `--broadcast` | `false`  | Execute the transaction                 |

### deposit

```bash theme={null}
polygon-agent deposit --asset USDC --amount 1000
polygon-agent deposit --asset USDC --amount 1000 --protocol aave --broadcast
```

Deposit into a Trails earn pool. With no `--protocol`, the CLI auto-selects the highest-TVL pool for the asset. `--protocol aave` or `--protocol morpho` constrains the choice.

| Flag          | Default  | Purpose                    |
| ------------- | -------- | -------------------------- |
| `--wallet`    | `main`   | Wallet name                |
| `--chain`     | none     | Chain                      |
| `--asset`     | `USDC`   | Asset symbol               |
| `--amount`    | required | Amount to deposit          |
| `--protocol`  | none     | Filter: `aave` or `morpho` |
| `--broadcast` | `false`  | Execute the transaction    |

### withdraw

```bash theme={null}
# By auto-discovery
polygon-agent withdraw --asset USDC --protocol aave --amount max
# By explicit position address
polygon-agent withdraw --position 0xPool... --amount 500
```

Withdraw from an Aave v3 aToken position or an ERC-4626 vault. Use `--amount max` for a full exit.

| Flag          | Default  | Purpose                                                |
| ------------- | -------- | ------------------------------------------------------ |
| `--wallet`    | `main`   | Wallet name                                            |
| `--chain`     | none     | Chain                                                  |
| `--position`  | none     | aToken or vault share address                          |
| `--asset`     | none     | Asset symbol (use with `--protocol` to auto-discover)  |
| `--protocol`  | none     | `aave` or `morpho`                                     |
| `--amount`    | required | Underlying amount (Aave) or `max` / partial (ERC-4626) |
| `--broadcast` | `false`  | Execute the transaction                                |

***

## Worked example

```bash theme={null}
# Sweep idle USDC into Aave v3
polygon-agent deposit --asset USDC --amount 5000 --protocol aave
# ⚡ Dry run, pool: 0x794a..., projected APY 4.8%

polygon-agent deposit --asset USDC --amount 5000 --protocol aave --broadcast
# Deposited 5000.00 USDC to Aave v3
# Tx: 0xabc... → https://polygonscan.com/tx/0xabc...

# Pull funds back when an initiative kicks off
polygon-agent withdraw --asset USDC --protocol aave --amount max --broadcast
# Withdrew 5,012.34 USDC from Aave v3 (principal + yield)
# Tx: 0xdef...
```

***

## Implementation

<CardGroup cols={2}>
  <Card title="Setup and wallets" icon="key" href="/payment-services/agentic-payments/cli/setup-and-wallets">
    Pass `--defi` at wallet creation to enable swaps and yield.
  </Card>

  <Card title="Transfers" icon="paper-plane" href="/payment-services/agentic-payments/cli/transfers">
    Plain token transfers; no DEX routing.
  </Card>

  <Card title="Trails" icon="route" href="/trails/index">
    The underlying DEX and earn-pool router.
  </Card>
</CardGroup>
