# metamask/delegation-framework architecture

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

- Interactive diagram: https://gitdiagram.com/metamask/delegation-framework
- Repository: https://github.com/metamask/delegation-framework

## Overview

- User/Client – represents the end user initiating transactions and delegations.
- DeleGator Proxy (Smart Account) – acts as an ERC-1967 proxy that forwards interactions to the chosen implementation.
- DeleGator Implementations – include multiple variants (e.g., MultiSigDeleGator, HybridDeleGator, EIP7702 Stateless) that encapsulate the business logic for handling the smart account operations.
- DeleGator Core / EIP7702 Core – contain the core delegation execution logic and ERC-4337 functionalities.
- Delegation Manager – validates and executes delegation instructions, ensuring secure off-chain delegation execution.
- Caveat Enforcers – plugins (external contracts) that apply rules and restrictions to delegated operations.
- Supporting Libraries and Interfaces – include the various libraries (e.g., SCL, FCL, OpenZeppelin contracts) and interfaces used throughout the system.
- Deployment/Verification Scripts – although not part of runtime, include the “script” directory components to show the presence of automated workflows and deployment steps.

##### Out the relationships and interactions
- The user interacts with the DeleGator Proxy.
- The Proxy delegates calls to the underlying DeleGator Implementation—this is an upgradeable contract model following the EIP-1967 proxy pattern.
- The Implementation leverages either the DeleGator Core or the EIP7702 variant to process delegations.
- The Delegation Manager is used by the DeleGator Core to coordinate and execute delegation logic.
- Caveat Enforcers are used by both the Delegation Manager and the implementation layer to ensure that any invoked delegation complies with restrictions.
- Supporting libraries and interfaces underpin and standardize interactions between these components.

##### Architectural patterns and design principles
- Use of the Proxy pattern (EIP-1967) to allow upgradeability by decoupling storage from logic.
- Modular design with clear separation between core delegation logic, implementation variations, and enforcement of security restrictions.
- Emphasis on extensibility so that developers can build custom implementations and caveat enforcers.
- Incorporation of development/testing tools (Foundry, anvil, Forge build scripts) shown in the “script” and “lib” directories.

##### Details
- Use of Foundry for building, testing, and deploying (supported by the “foundry.toml” and various scripts).
- Optionally, indicate the file structure groupings (for example, under “src” for main contracts, “lib” for imported libraries, “test” for test suites, and “script” for deployment/verification scripts).

- That each component is clearly named and has a brief description of its role.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction Layer
    subgraph "User Interaction Layer"
        U["User/Client"]:::user
        P["DeleGator Proxy (Smart Account)"]:::contract
    end

    %% Implementation Layer
    subgraph "Implementation Layer"
        subgraph "DeleGator Implementations"
            M["MultiSigDeleGator"]:::contract
            H["HybridDeleGator"]:::contract
            E["EIP7702DeleGator"]:::contract
        end
        C["DeleGator Core / EIP7702 Core"]:::contract
        D["Delegation Manager"]:::contract
        V["Caveat Enforcers"]:::contract
    end

    %% Supporting & Deployment Layer
    subgraph "Supporting & Deployment"
        L["Supporting Libraries and Interfaces"]:::library
        S["Deployment/Verification Scripts"]:::script
    end

    %% Connections
    U -->|"User_request"| P
    P -->|"Proxy_forwarding"| M
    P -->|"Proxy_forwarding"| H
    P -->|"Proxy_forwarding"| E
    M -->|"Delegation_call"| C
    H -->|"Delegation_call"| C
    E -->|"Delegation_call"| C
    C -->|"Manager_validation"| D
    D -->|"Caveat_check"| V
    M ---|"Uses"| V
    H ---|"Uses"| V
    E ---|"Uses"| V
    C ---|"Utilizes"| L
    D ---|"Utilizes"| L
    V ---|"Utilizes"| L
    S ---|"Deployment_scripts"| P

    %% Click Events
    click P "https://github.com/metamask/delegation-framework/tree/main/src"
    click M "https://github.com/metamask/delegation-framework/blob/main/documents/MultisigDeleGator.md"
    click H "https://github.com/metamask/delegation-framework/blob/main/documents/HybridDeleGator.md"
    click E "https://github.com/metamask/delegation-framework/blob/main/documents/EIP7702DeleGator.md"
    click C "https://github.com/metamask/delegation-framework/blob/main/documents/DeleGatorCore.md"
    click D "https://github.com/metamask/delegation-framework/blob/main/documents/DelegationManager.md"
    click V "https://github.com/metamask/delegation-framework/tree/main/src/enforcers"
    click L "https://github.com/metamask/delegation-framework/tree/main/src/libraries"
    click S "https://github.com/metamask/delegation-framework/tree/main/script/verification"

    %% Styles
    classDef user fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef contract fill:#CDE6D0,stroke:#333,stroke-width:2px;
    classDef library fill:#CCE5FF,stroke:#333,stroke-width:2px;
    classDef script fill:#F3D1F4,stroke:#333,stroke-width:2px;
```
