# openzeppelin/ethernaut architecture

> Architecture diagram of the GitHub repository openzeppelin/ethernaut, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openzeppelin/ethernaut
- Repository: https://github.com/openzeppelin/ethernaut

## Overview

##### Project type and purpose
- This is a full-stack Web3 dApp (a “wargame”) comprising:
- A React/Redux frontend (client/)
- A suite of Solidity smart contracts (contracts/)
- A local or public Ethereum JSON-RPC network (ganache, Hardhat network, or Sepolia)
- Players load levels (each level is a smart contract) in the browser and interact with them via Metamask or another Web3 provider.

##### File structure
- Top-level:
- client/ – React application, source code, components, actions, reducers, styles, gamedata JSON
- contracts/ – Foundry/forge Solidity project, source, tests, libraries (openzeppelin, forge-std), deployment config
- deps/ – third-party JS deps (highlight.js)
- Config/deployment/scripts – netlify.toml, GitHub workflows, client/scripts for deploy & docs
- Patterns:
- React + Redux for UI and state management
- Middleware layer in client to orchestrate Web3 calls (activateLevel, loadEthernautContract, syncPlayerProgress, etc.)
- Foundry for compiling/testing/deploying contracts
- Deployment files: contracts/remappings.txt, foundry.toml, deploy.*.json series to track deployed instances

##### Key insights from README
- Three components must run in concert:
1. Local or remote Ethereum test network
2. Contracts compiled & deployed to that network
3. React client configured to point at that network
- Standard workflows: compile:contracts, deploy:contracts, start:ethernaut

a. Main components
- User’s Browser + Metamask (Web3 wallet)
- React/Redux Frontend (client/src)
- JSON-RPC Provider (Ganache/Hardhat or Sepolia)
- Smart Contracts on EVM (contracts/src + deployed instances)
- Off-chain gamedata JSON files (client/src/gamedata)
- Deployment/CI pipelines (GitHub Actions, Netlify)
b. Relationships & interactions
1. User → Browser → React UI
2. React dispatches Redux actions → middleware invokes Web3 calls via ethers.js/web3.js → Metamask → RPC provider
3. RPC provider routes to EVM → execute smart contract functions → return transaction receipts or view data
4. React consumes contract responses and gamedata JSON → updates Redux store → re-renders UI
5. Deployment scripts (client/scripts & Foundry) → compile & deploy contracts → write addresses to deploy.*.json
c. Architectural patterns & principles
- Separation of concerns: UI vs. state management vs. blockchain interaction
- Redux middleware abstracts Web3 logic (network selection, contract loading, transaction submission, stats sync)
- Immutable on-chain state (contracts) vs. ephemeral UI state (Redux store)
- CI/CD for testing contracts (yarn test:contracts) and building/deploying frontend (Netlify, GitHub Actions)
d. Key technologies & frameworks
- Frontend: React, Redux, React-Redux, react-router (withRouter HOC), CSS modules
- Blockchain: Solidity contracts managed via Foundry/forge, OpenZeppelin libraries
- Web3: ethers.js or web3.js via middleware, Metamask as provider
- Build & deploy: yarn, Netlify, GitHub Actions

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Users and Wallet
    User["User"]:::external -->|interacts via| Browser["Browser"]:::external
    Browser -->|loads| Entry["client/src/index.js"]:::frontend
    click Entry "https://github.com/openzeppelin/ethernaut/blob/master/client/src/index.js"

    %% Presentation Layer
    subgraph "Presentation Layer" 
        direction TB
        Components["UI Components"]:::frontend
        Containers["Containers"]:::frontend
        HOC["withRouter HOC"]:::frontend
        Utils["Utils"]:::frontend
        Styles["Stylesheets"]:::frontend
        GameData["Game Data JSON"]:::data
        Entry --> Components
        Entry --> Containers
        Entry --> HOC
        Entry --> Utils
        Entry --> Styles
        Entry --> GameData
    end
    click Components "https://github.com/openzeppelin/ethernaut/tree/master/client/src/components"
    click Containers "https://github.com/openzeppelin/ethernaut/tree/master/client/src/containers"
    click HOC "https://github.com/openzeppelin/ethernaut/blob/master/client/src/hoc/withRouter.js"
    click Utils "https://github.com/openzeppelin/ethernaut/tree/master/client/src/utils"
    click Styles "https://github.com/openzeppelin/ethernaut/tree/master/client/src/styles"
    click GameData "https://github.com/openzeppelin/ethernaut/tree/master/client/src/gamedata"

    %% Application Logic Layer
    subgraph "Application Logic / State Management"
        direction TB
        Actions["Redux Actions"]:::store
        Reducers["Redux Reducers"]:::store
        Store["Redux Store"]:::store
        Middleware["Redux Middleware"]:::store
        Entry -->|dispatch action| Actions
        Actions --> Reducers
        Reducers --> Store
        Store -->|applies middleware| Middleware
    end
    click Actions "https://github.com/openzeppelin/ethernaut/blob/master/client/src/actions/index.js"
    click Reducers "https://github.com/openzeppelin/ethernaut/blob/master/client/src/reducers/index.js"
    click Store "https://github.com/openzeppelin/ethernaut/blob/master/client/src/store.js"
    click Middleware "https://github.com/openzeppelin/ethernaut/tree/master/client/src/middlewares"

    %% Web3 Interaction
    subgraph "Blockchain Interaction"
        direction TB
        Metamask["Metamask (Web3 Wallet)"]:::provider
        RPC["JSON-RPC Provider"]:::provider
        Metamask --> RPC
        Middleware -->|eth_call / eth_sendTransaction| Metamask
        RPC --> Contracts
    end

    %% Smart Contracts Layer
    subgraph "Smart Contracts (On-Chain)" 
        direction TB
        Contracts["Contracts"]:::contract
        Src["Source (contracts/src)"]:::contract
        Lib["Libraries (contracts/lib)"]:::contract
        Tests["Tests (contracts/test)"]:::contract
        Out["Build Output (contracts/out)"]:::contract
        Toml["Foundry Config"]:::contract
        Remap["Solidity Remappings"]:::contract
        Contracts --> Src
        Contracts --> Lib
        Contracts --> Tests
        Contracts --> Out
        Contracts --> Toml
        Contracts --> Remap
    end
    click Src "https://github.com/openzeppelin/ethernaut/tree/master/contracts/src"
    click Lib "https://github.com/openzeppelin/ethernaut/tree/master/contracts/lib"
    click Tests "https://github.com/openzeppelin/ethernaut/tree/master/contracts/test"
    click Out "https://github.com/openzeppelin/ethernaut/tree/master/contracts/out"
    click Toml "https://github.com/openzeppelin/ethernaut/blob/master/contracts/foundry.toml"
    click Remap "https://github.com/openzeppelin/ethernaut/blob/master/contracts/remappings.txt"

    %% Deployment & CI/CD
    subgraph "CI/CD & Deployment" 
        direction TB
        Scripts["Deployment Scripts"]:::cicd
        GHActions["GitHub Actions"]:::cicd
        Netlify["Netlify Config"]:::cicd
        Scripts -->|compile & deploy| Contracts
        GHActions -->|test & build| Scripts
        Netlify -->|deploy frontend| Entry
    end
    click Scripts "https://github.com/openzeppelin/ethernaut/tree/master/client/scripts"
    click GHActions "https://github.com/openzeppelin/ethernaut/blob/master/.github/workflows/test.yml"
    click Netlify "https://github.com/openzeppelin/ethernaut/blob/master/netlify.toml"

    %% Flows
    Components -->|renders store state| Store
    Store -->|provides props| Components

    %% Styles
    classDef frontend fill:#D6EAF8,stroke:#21618C,stroke-width:1px
    classDef store fill:#D5F5E3,stroke:#196F3D,stroke-width:1px
    classDef data fill:#FCF3CF,stroke:#B7950B,stroke-width:1px
    classDef provider fill:#EBDEF0,stroke:#7D3C98,stroke-width:1px
    classDef contract fill:#E5E7E9,stroke:#566573,stroke-width:1px
    classDef cicd fill:#F2F3F4,stroke:#A6ACAF,stroke-width:1px,stroke-dasharray: 5 5
    class User,Browser external
    classDef external fill:#FFFFFF,stroke:#000000,stroke-width:1px,stroke-dasharray: 2 2
```
