# AutoYield Agent
DeFi autopilot on Solana — deposit USDC into Reflect USDC+ for auto-yield, then use the yield-bearing collateral to trade perpetual futures on Drift.
## Stack
- **OpenClaw / leviathan** — agent runtime with OODA loop
- **@reflectmoney/stable.ts** — USDC+ (yield-bearing stablecoin)
- **@drift-labs/sdk** — SOL-PERP trading on Drift
- **ClawRouter** — LLM routing with wallet-auth, USDC/x402 payments
- **Telegram** — natural language trading interface
- **Express API** — paid access with 10% performance fee
## Quickstart
```bash
cp .env.example .env
# Fill in your keys
npm install
npm run dev
```
## API Server (Monetization)
Run the paid API server:
```bash
npm run api
```
### Endpoints
| Endpoint | Method | Purpose |
|---|---|---|
| `POST /api/auth/register` | Register + get API key |
| `POST /api/auth/login` | Login with wallet |
| `POST /api/trades/execute` | Execute strategy (10% fee on profit) |
| `GET /api/trades/positions` | View open positions |
| `GET /api/trades/history` | Trade history |
| `POST /api/trades/close` | Close position (triggers fee) |
| `GET /api/portfolio` | Portfolio overview |
| `POST /api/withdraw` | Withdraw (requires signature) |
| `POST /api/agent/create` | Create trading agent |
| `POST /api/agent/trade` | Execute trade via agent |
| `POST /api/agent/close` | Close position via agent |
| `GET /api/agent/list` | List user agents |
| `POST /api/agent/:id/start` | Start agent |
| `POST /api/agent/:id/stop` | Stop agent |
| `POST /api/agent/:id/strategy` | Change strategy |
### Agent Trading
Create an agent with a specific strategy:
```bash
curl -X POST http://localhost:3001/api/agent/create \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"strategy": "shark", "maxLeverage": 5}'
```
Execute trades through the agent:
```bash
curl -X POST http://localhost:3001/api/agent/trade \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"agentId": "...", "direction": "long", "size": 1.5, "leverage": 3}'
```
### Performance Fee
- **10%** of realized profit auto-deducted when positions close
- Fee tracked per-position in SQLite database
- Withdraw requires wallet signature verification
## Key Files
| File | Purpose |
|---|---|
| `scripts/dev.ts` | Main agent entry point |
| `lib/ooda-loop.ts` | OODA decision loop (Observe → Orient → Decide → Act) |
| `lib/drift.ts` | Drift SDK wrapper (positions, orders, risk) |
| `lib/reflect.ts` | Reflect SDK wrapper (USDC+, deposit, compound) |
| `api/server.ts` | Express API server for monetization |
| `api/routes/` | API route handlers (auth, trades, portfolio, withdraw) |
| `api/services/` | Business logic (auth, feeTracker, vault) |
| `api/middleware/auth.ts` | API key authentication middleware |
| `skills/reflect-yield/SKILL.md` | OpenClaw skill for yield management |
| `skills/perpsclaw/SKILL.md` | OpenClaw skill for perp trading (API-authenticated) |
| `souls/*.md` | Strategy SOULs (Shark/Wolf/Grid) |
| `configs/settings.ts` | Risk limits and loop config |
| `scripts/telegram-bot.ts` | Telegram bot interface |
## Setup
```bash
# 1. Create agent wallet
solana-keygen new -o agent-wallet.json
# 2. Fund with SOL (for tx fees) + USDC (for collateral)
# Transfer to the wallet address shown
# 3. Set environment
export AGENT_WALLET_KEY=$(cat agent-wallet.json | jq -r '.[0]')
export SOLANA_RPC_URL=https://api.mainnet-beta.solana.com
export DRIFT_PROGRAM_ID=ioPLPPRsMRg2sT5kiL5RM4QUQaRd4SkFMhcXEkeJQWp
# 4. Run
npm run dev
```
## Strategies
| Strategy | Style | Leverage | Best For |
|---|---|---|---|
| **shark** | Momentum breakout | 5x | Trending markets |
| **wolf** | Mean reversion | 3x | Range-bound, extreme RSI |
| **grid** | Grid market-making | 2x | Oscillating markets |
## Architecture
```
USDC → Reflect → USDC+ (auto-yield ~5% APY)
↓
Drift perps collateral (yield accrues while in position)
↓
Drift SOL-PERP positions
↓
Profits → USDC+ → compound
```