# velodrome-finance/contracts architecture

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

- Interactive diagram: https://gitdiagram.com/velodrome-finance/contracts
- Repository: https://github.com/velodrome-finance/contracts

## Overview

##### Project Type and Core Purpose
- This is a blockchain protocol project that implements an automated market maker (AMM) based on a Uniswap V2-like approach, with extended features including tokenomics (voting escrow, minter, rewards distribution) and governance.
- The README clearly delineates three major areas:
- AMM contracts (e.g., Pool.sol, Router.sol, PoolFees.sol)
- Tokenomy contracts (e.g., Velo.sol, VotingEscrow.sol, Minter.sol, RewardsDistributor.sol)
- Governance and protocol mechanics (e.g., Voter.sol, Gauge.sol and a suite of rewards contracts)

##### File Structure
- Directories:
- contracts: Contains all the Solidity source files, neatly organized into subdirectories such as art, factories, gauges, governance, interfaces, libraries, and rewards. This modular structure implies a strong separation of concerns between different functionalities.
- script: Contains deployment scripts (with both Hardhat and other scripts) and constants files that are used for deployment and network-specific configuration (for example, Optimism and CI outputs).
- test: Holds testing code (including end-to-end and invariant tests) that uses Foundry.
- lib: Contains external dependencies/libraries like forge-std, ds-test, OpenZeppelin’s contracts, etc.
- Configuration Files:
- foundry.toml, hardhat.config.ts, and package.json inform us of the testing, deployment, and build environments, respectively.

##### Main Components
a. AMM Layer:
- Pool, Router, and PoolFees: Represent the core swapping functionality, liquidity management, and fee handling.
- VelodromeLibrary: Provides helper functions necessary for pricing and impact calculations.
- FactoryRegistry: Manages the allowed factories for creating additional components such as pools and rewards.
b. Token Management and Tokenomy:
- Velo (ERC20 token contract)
- VotingEscrow: Implements vote-escrow logic with NFT-like properties.
- Minter: Handles token emissions and distribution.
- RewardsDistributor: Manages the distribution of rebases and rewards.
- VeArtProxy: Provides upgradability for the vote-escrow art.
c. Governance & Protocol Mechanics:
- Voter: Central to routing voting, gauge creation, and controlling emissions.
- Gauge and associated reward contracts (Reward, VotingReward, FeesVotingReward, BribeVotingReward, ManagedReward, LockedManagedReward, FreeManagedReward): Outline how liquidity providers interact with pools to earn rewards.
- Governance contracts like VeloGovernor and EpochGovernor manage protocol-wide decisions.
d. Deployment & Interaction Infrastructure:
- Deployment scripts (script/hardhat) show the orchestration of contract deployment.
- Testing infrastructure using Foundry is an essential part of the project’s CI/CD lifecycle.
- External services and configuration (e.g., .env and funding.json) that tie into network specifics like Optimism.

##### Relationships and Data Flow
- From the FactoryRegistry to AMM and governance components, indicating how factories spawn new pools, gauges, or rewards.
- Between the Pool and Router—indicating the flow of liquidity and swap transactions.
- From the Voter contract to the Gauge and Reward contracts, showing how voting signals control reward distributions.
- From the Minter to both Voter and RewardsDistributor to delineate token emission flows.

##### Architectural Patterns and Technology Stack
- The separation into distinct directories follows a modular and layered architecture, making use of well-known patterns for DeFi protocols.
- Leverage standard design patterns used in smart contract development, such as Factory patterns (for pools, gauges, and rewards), and the separation of concerns as seen in the governance and token modules.
- Usage of standardized libraries like OpenZeppelin, forge-std, and ds-test as part of the toolchain.
- Testing and deployment are handled through Foundry and Hardhat respectively; indicate these environments as auxiliary components in the overall system design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% AMM Layer
    subgraph "AMM Layer"
        Pool["Pool"] 
        Router["Router"]
        PoolFees["PoolFees"]
        FactoryRegistry["FactoryRegistry"]
        VelodromeLibrary["VelodromeLibrary"]
    end

    %% Token Management and Tokenomy
    subgraph "Token Management & Tokenomy"
        Velo["Velo"]
        VotingEscrow["VotingEscrow"]
        VeArtProxy["VeArtProxy"]
        Minter["Minter"]
        RewardsDistributor["RewardsDistributor"]
    end

    %% Governance & Protocol Mechanics
    subgraph "Governance & Protocol Mechanics"
        Voter["Voter"]
        VeloGovernor["VeloGovernor"]
        EpochGovernor["EpochGovernor"]
        Gauge["Gauge"]
        Reward["Reward"]
        VotingReward["VotingReward"]
        FeesVotingReward["FeesVotingReward"]
        BribeVotingReward["BribeVotingReward"]
        ManagedReward["ManagedReward"]
        LockedManagedReward["LockedManagedReward"]
        FreeManagedReward["FreeManagedReward"]
    end

    %% Deployment & Interaction Infrastructure
    subgraph "Deployment & Interaction Infrastructure"
        DeployScripts["Deployment Scripts"]
        Testing["Testing (Foundry)"]
        ExternalConfig["External Config"]
    end

    %% Relationships within AMM Layer
    FactoryRegistry -->|"spawnPools"| Pool
    FactoryRegistry -->|"spawnSwaps"| Router
    FactoryRegistry -->|"spawnFees"| PoolFees
    Router -->|"swapExecution"| Pool

    %% Interaction between AMM and Governance (Factory spawns gauges)
    FactoryRegistry -->|"gaugeCreation"| Gauge

    %% Relationships within Token Management
    Minter -->|"emissionDistribution"| Voter
    Minter -->|"emissionDistribution"| RewardsDistributor
    VotingEscrow -->|"stakeVoting"| Voter

    %% Relationships within Governance & Protocol Mechanics
    Voter -->|"votingControl"| Gauge
    Voter -->|"votingControl"| Reward
    Voter -->|"votingControl"| VotingReward
    Voter -->|"votingControl"| FeesVotingReward
    Voter -->|"votingControl"| BribeVotingReward
    Voter -->|"votingControl"| ManagedReward
    Voter -->|"votingControl"| LockedManagedReward
    Voter -->|"votingControl"| FreeManagedReward

    %% Deployment & Interaction Infrastructure connections
    DeployScripts -->|"deployContracts"| FactoryRegistry
    DeployScripts -->|"deployContracts"| Minter
    Testing -->|"validateDeployment"| DeployScripts
    ExternalConfig -->|"provideConfig"| DeployScripts

    %% Styles
    classDef contract fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef deployment fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef external fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;

    class Pool,Router,PoolFees,FactoryRegistry,VelodromeLibrary,Velo,VotingEscrow,VeArtProxy,Minter,RewardsDistributor,Voter,VeloGovernor,EpochGovernor,Gauge,Reward,VotingReward,FeesVotingReward,BribeVotingReward,ManagedReward,LockedManagedReward,FreeManagedReward contract;
    class DeployScripts,Testing,ExternalConfig deployment;

    %% Click Events for AMM Layer
    click Pool "https://github.com/velodrome-finance/contracts/tree/main/contracts/factories"
    click Router "https://github.com/velodrome-finance/contracts/tree/main/contracts/factories"
    click PoolFees "https://github.com/velodrome-finance/contracts/tree/main/contracts/factories"
    click FactoryRegistry "https://github.com/velodrome-finance/contracts/tree/main/contracts/factories"
    click VelodromeLibrary "https://github.com/velodrome-finance/contracts/tree/main/contracts/libraries"

    %% Click Events for Token Management and Tokenomy
    click Velo "https://github.com/velodrome-finance/contracts/blob/main/VOTINGESCROW.md"
    click VotingEscrow "https://github.com/velodrome-finance/contracts/tree/main/contracts/governance"
    click VeArtProxy "https://github.com/velodrome-finance/contracts/tree/main/contracts/governance"
    click Minter "https://github.com/velodrome-finance/contracts/tree/main/contracts/rewards"
    click RewardsDistributor "https://github.com/velodrome-finance/contracts/tree/main/contracts/rewards"

    %% Click Events for Governance & Protocol Mechanics
    click Voter "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click VeloGovernor "https://github.com/velodrome-finance/contracts/tree/main/contracts/governance"
    click EpochGovernor "https://github.com/velodrome-finance/contracts/tree/main/contracts/governance"
    click Gauge "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click Reward "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click VotingReward "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click FeesVotingReward "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click BribeVotingReward "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click ManagedReward "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click LockedManagedReward "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"
    click FreeManagedReward "https://github.com/velodrome-finance/contracts/tree/main/contracts/gauges"

    %% Click Events for Deployment & Interaction Infrastructure
    click DeployScripts "https://github.com/velodrome-finance/contracts/tree/main/script/hardhat"
    click Testing "https://github.com/velodrome-finance/contracts/tree/main/test"
    click ExternalConfig "https://github.com/velodrome-finance/contracts/blob/main/hardhat.config.ts"
```
