# Data Flow

This page explains how all five components of EcoRound work together — and specifically how **Chainlink CRE** and **Tenderly Virtual TestNets** fit into the full system flow.

***

## Full System Architecture

```mermaid
flowchart TD
    subgraph Users["👥 Users"]
        U["Browser Next.js dApp"]
    end

    subgraph DataLayer["📦 Data Layer"]
        API["EcoRound API Simulator Go/Gin — port 8080 PostgreSQL (Neon) Stores match metadata Simulates 3 data sources"]
    end

    subgraph Oracle["🔮 Chainlink CRE Oracle"]
        CRE["EcoRound Workflow Go → WASM Cron: every 30s Polls 3 sources Applies 2/3 consensus Calls lockMatch / resolveMatch"]
    end

    subgraph Tenderly["☁️ Tenderly Virtual TestNet"]
        direction TB
        RPC["Base Mainnet Fork Chain ID 84531 Persistent RPC endpoint"]
        FM["FactoryMatch"]
        VM["VaultMatch"]
        MORPHO["Morpho ERC4626"]
        USDC["USDC"]
    end

    subgraph Admin["🖥️ Admin Panel"]
        PANEL["Go TUI — panel-v2 Creates matches Simulates results"]
    end

    U -->|read match metadata| API
    U -->|read on-chain state write txns| RPC

    PANEL -->|POST /admin/matches| API
    PANEL -->|createMatch on-chain| RPC

    CRE -->|GET /admin/matches GET /pandascore /vlr /liquipedia| API
    CRE -->|lockMatch resolveMatch| RPC

    RPC --> FM
    RPC --> VM
    VM <--> MORPHO
    VM <--> USDC

    style Tenderly fill:#1a1a2a,stroke:#7B68EE,color:#ccc
    style Oracle fill:#1a2a1a,stroke:#00E6C3,color:#ccc
    style DataLayer fill:#2a1a1a,stroke:#FF4655,color:#ccc
    style Admin fill:#2a2a1a,stroke:#FFB800,color:#ccc
```

***

## How Chainlink CRE Fits In

CRE is the only entity that can trigger match state transitions on-chain. It is the bridge between real-world match data and the smart contract.

```mermaid
sequenceDiagram
    participant CRON as ⏱️ Cron (30s)
    participant CRE as Chainlink CRE Workflow
    participant API as EcoRound API
    participant PS as /pandascore
    participant VLR as /vlr
    participant LQ as /liquipedia
    participant VM as VaultMatch (Tenderly)

    CRON->>CRE: trigger

    CRE->>API: GET /admin/matches
    API-->>CRE: [{id:1, status:"open", vault:"0xabc..."}]

    par Parallel — query all 3 sources
        CRE->>PS: GET /pandascore/matches/1
        PS-->>CRE: {match_status: "started"}
    and
        CRE->>VLR: GET /vlr/matches/1
        VLR-->>CRE: {match_status: "started"}
    and
        CRE->>LQ: GET /liquipedia/matches/1
        LQ-->>CRE: {match_status: "upcoming"}
    end

    Note over CRE: 2/3 report started → consensus met

    CRE->>CRE: ABI-encode lockMatch()
    CRE->>CRE: Sign with Chainlink key management
    CRE->>VM: WriteReport → lockMatch()

    VM->>VM: status = Locked
    VM->>VM: deposit all USDC into Morpho

    VM-->>CRE: tx confirmed
    Note over CRE: [DONE] Tick complete
```

***

## How Tenderly Virtual TestNet Fits In

Tenderly provides the blockchain environment. Without it, EcoRound would need either real Base Mainnet (real funds at risk) or a testnet (no USDC, no Morpho).

```mermaid
flowchart LR
    subgraph Problem["Without Tenderly"]
        P1["Base Mainnet ❌ Real funds at risk ❌ Can't reset state ❌ Expensive to seed"]
        P2["Base Testnet ❌ No real USDC ❌ No Morpho vault ❌ Different liquidity"]
    end

    subgraph Solution["With Tenderly Virtual TestNet"]
        T1["Base Mainnet Fork✅ Real USDC contract✅ Real Morpho vault✅ No real money needed"]
        T2["tenderly_setErc20Balance✅ Instant test USDC✅ No bridging required✅ Anyone can demo"]
        T3["Persistent RPC✅ Shared state✅ All txns visible✅ Full tx traces"]
    end

    Problem --> Solution

    style Problem fill:#3a1a1a,stroke:#FF4655,color:#ccc
    style Solution fill:#1a3a1a,stroke:#00E6C3,color:#ccc
```

### The Faucet Flow (Tenderly-specific)

```mermaid
sequenceDiagram
    actor User
    participant Faucet as /faucet page
    participant Tenderly as Tenderly RPC
    participant USDC as USDC Contract

    User->>Faucet: click "Mint 10,000 USDC"
    Faucet->>Tenderly: POST tenderly_setErc20Balance[USDC_ADDRESS, userAddress, "0x2540BE400"]
    Tenderly->>USDC: override balance for user
    USDC-->>Tenderly: balance set
    Tenderly-->>Faucet: success
    Faucet-->>User: show updated balance10,000 USDC ready
```

***

## End-to-End: Deposit to Claim

The complete flow a user experiences — touching every layer of the system:

```mermaid
flowchart TD
    A(["User visits /faucet mints 10,000 USDC"]) --> B

    B["Tenderly RPC tenderly_setErc20Balance sets USDC balance"] --> C

    C(["User visits /match/1 picks Team A deposits 100 USDC"]) --> D

    D["USDC.approve(vault)VaultMatch.deposit(TeamA, 100)"] --> E

    E["VaultMatch stores deposit userDeposits[user][TeamA] = 100"] --> F

    F(["Admin panel simulates pandascore + vlr → started"]) --> G

    G["CRE oracle fires (30s) 2/3 sources started → calls lockMatch()"] --> H

    H["VaultMatch: status = Locked All USDC → Morpho vault Yield starts accruing"] --> I

    I(["Admin panel simulates pandascore + vlr → ended, TeamA"]) --> J

    J["CRE oracle fires (30s) 2/3 sources ended + agree → calls resolveMatch(TeamA)"] --> K

    K["VaultMatch: redeem from Morpho calculate totalYield status = Resolved"] --> L

    L(["User visits /match/1 clicks Claim"]) --> M

    M["VaultMatch.claim() user backed winner payout = 100 USDC + yield share"] --> N

    N(["USDC received in wallet ✅"])

    style A fill:#1a1a2a,stroke:#7B68EE,color:#ccc
    style N fill:#1a3a1a,stroke:#00E6C3,color:#00E6C3
    style G fill:#2a1a2a,stroke:#FF4655,color:#ccc
    style J fill:#2a1a2a,stroke:#FF4655,color:#ccc
    style H fill:#1a2a2a,stroke:#00E6C3,color:#ccc
    style K fill:#1a2a2a,stroke:#00E6C3,color:#ccc
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eco-round.gitbook.io/eco-round-docs/technical-details/data-flow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
