System Flow

End-to-end flow of GuardAI from user deposit to emergency withdrawal.


Normal Operation Flow

┌─────────────────────────────────────────────────────────────────┐
│  USER                                                           │
│                                                                 │
│  1. Connect MetaMask to Tenderly (Chain ID 9991)                │
│  2. Navigate to Pools tab                                       │
│  3. Select asset (ETH / USDC / stETH)                          │
│  4. Enter amount → Confirm in MetaMask                          │
│     ├── ETH: single tx → depositETH()                          │
│     └── ERC20: approve() → wait → depositERC20()               │
│  5. Vault records balance: _balances[user][token] += amount     │
└─────────────────────────────────────────────────────────────────┘

                              │  Every 60 seconds

┌─────────────────────────────────────────────────────────────────┐
│  CHAINLINK CRE WORKFLOW                                         │
│                                                                 │
│  Step 1: fetch_eth_price  → Chainlink Data Feed (ETH/USD)      │
│  Step 2: fetch_lido_tvl   → DeFiLlama API                      │
│  Step 3: fetch_aave_tvl   → DeFiLlama API                      │
│  Step 4: fetch_maker_tvl  → DeFiLlama API                      │
│  Step 5: compute_risk     → Weighted score (0–100)             │
│  Step 6: write_risk_score → VaultSentinel.setRiskScore(score)  │
└─────────────────────────────────────────────────────────────────┘

                    ┌─────────┴──────────┐
                    │                    │
               score < 70          score ≥ 70
                    │                    │
                    ▼                    ▼
            ┌─────────────┐    ┌──────────────────────┐
            │   ACTIVE    │    │  _executeEmergency()  │
            │             │    │                       │
            │  Vault OK   │    │  vaultState=EMERGENCY │
            │  Gauge green│    │  _returnAllFunds()    │
            └─────────────┘    │  ├── ETH → all users  │
                               │  └── ERC20 → all users│
                               └──────────────────────┘

Emergency Flow (Detailed)


Frontend Polling Flow


ERC20 Deposit — Two-Step Flow

The two-step flow is critical. Sending depositERC20 before approve is confirmed causes nonce conflicts and dropped transactions.

Last updated