# cetusprotocol/cetus-amm architecture

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

- Interactive diagram: https://gitdiagram.com/cetusprotocol/cetus-amm
- Repository: https://github.com/cetusprotocol/cetus-amm

## Overview

Based on your provided file tree and the brief README, it’s clear that this project implements an Automated Market Maker (“AMM”) specifically tailored for blockchain platforms using the Move language. In this case, the project is set up to deploy on two distinct chains: Aptos and Sui. Each blockchain-specific implementation is isolated within its own directory (aptos and sui), and each contains a comparable set of Move source files to implement the core AMM functionalities.

##### Main Components
- Two primary modules representing the blockchain implementations:
- Aptos AMM Module
- Sui AMM Module
- Within each module, key components include:
- amm_config: Handles configuration parameters for the AMM.
- amm_math: Provides mathematical functions (e.g., pricing curves) needed by the AMM.
- amm_router: Manages routing of swap orders or trade flows.
- amm_script: Likely provides orchestration scripts for executing AMM transactions.
- amm_swap: Core logic for performing swap operations.
- amm_utils: Utility functions supporting other modules.
- For Aptos, note that there is a unique file called u256.move which may deal with a custom 256-bit arithmetic implementation.

##### Relationships and Interactions
- Both Aptos and Sui modules function largely in parallel—each one encapsulating its blockchain-specific AMM logic.
- Within each module, components would interact in a sequential or coordinated manner:
- The amm_config sets initial parameters that other modules refer to.
- The amm_math functions support the computational needs of the swap operations.
- The amm_router directs calls between front-end trading commands and the actual execution logic found in amm_swap.
- The amm_utils provide helper routines that might be needed by all the above functions.
- For Aptos, the specialized u256 arithmetic support is used by the math module for operations needing high precision.
- There is an overall separation between the two modules, emphasizing that even though the core AMM functionalities are parallel, the deployment context and perhaps nuances in blockchain paradigms require distinct implementations.

##### Architectural Patterns and Design Principles
- Modularization: The project is clearly organized into modules based on the target blockchain (Aptos vs. Sui), ensuring that business logic does not mix between contexts.
- Separation of Concerns: Each file (or component) handles a specific set of tasks (configuration, math operations, routing, executing swaps, etc.), which makes the code maintainable and testable.
- Abstraction: The design abstracts blockchain-specific details into their respective modules.
- Reusability: Common logic (such as core AMM functions and utility routines) is implemented in similar ways across platforms, illustrating reuse in design, allowing parallel improvements.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Aptos AMM Module"
        A_Conf["amm_config (AMM Configuration)"]
        A_Math["amm_math (Mathematical Operations)"]
        A_U256["u256 (256-bit Arithmetic Support)"]
        A_Router["amm_router (Routing Logic)"]
        A_Script["amm_script (Transaction Scripts)"]
        A_Swap["amm_swap (Swap Logic)"]
        A_Utils["amm_utils (Utility Functions)"]
    end

    subgraph "Sui AMM Module"
        S_Conf["amm_config (AMM Configuration)"]
        S_Math["amm_math (Mathematical Operations)"]
        S_Router["amm_router (Routing Logic)"]
        S_Script["amm_script (Transaction Scripts)"]
        S_Swap["amm_swap (Swap Logic)"]
        S_Utils["amm_utils (Utility Functions)"]
    end

    %% Aptos Module Connections
    A_Conf -->|"configures"| A_Math
    A_Conf -->|"configures"| A_Router
    A_Conf -->|"configures"| A_Swap
    A_Math -->|"supports"| A_Swap
    A_Math -->|"uses"| A_U256
    A_Router -->|"routes_to"| A_Script
    A_Router -->|"routes_to"| A_Swap
    A_Math -.->|"helper"| A_Utils
    A_Router -.->|"helper"| A_Utils
    A_Swap -.->|"helper"| A_Utils
    A_Script -.->|"helper"| A_Utils

    %% Sui Module Connections
    S_Conf -->|"configures"| S_Math
    S_Conf -->|"configures"| S_Router
    S_Conf -->|"configures"| S_Swap
    S_Math -->|"supports"| S_Swap
    S_Router -->|"routes_to"| S_Script
    S_Router -->|"routes_to"| S_Swap
    S_Math -.->|"helper"| S_Utils
    S_Router -.->|"helper"| S_Utils
    S_Swap -.->|"helper"| S_Utils
    S_Script -.->|"helper"| S_Utils

    %% Class Definitions for Colour Coding
    classDef configStyle fill:#ffcccc,stroke:#aa0000,stroke-width:2px;
    classDef coreStyle fill:#ccffcc,stroke:#226622,stroke-width:2px;
    classDef arithStyle fill:#ffffcc,stroke:#aaaa00,stroke-width:2px;
    classDef utilStyle fill:#ccccff,stroke:#0000aa,stroke-width:2px;

    class A_Conf,S_Conf configStyle;
    class A_Math,A_Router,A_Swap,A_Script,S_Math,S_Router,S_Swap,S_Script coreStyle;
    class A_U256 arithStyle;
    class A_Utils,S_Utils utilStyle;

    %% Click Events for Aptos Module
    click A_Conf "https://github.com/cetusprotocol/cetus-amm/blob/main/aptos/sources/amm_config.move"
    click A_Math "https://github.com/cetusprotocol/cetus-amm/blob/main/aptos/sources/amm_math.move"
    click A_U256 "https://github.com/cetusprotocol/cetus-amm/blob/main/aptos/sources/u256.move"
    click A_Router "https://github.com/cetusprotocol/cetus-amm/blob/main/aptos/sources/amm_router.move"
    click A_Script "https://github.com/cetusprotocol/cetus-amm/blob/main/aptos/sources/amm_script.move"
    click A_Swap "https://github.com/cetusprotocol/cetus-amm/blob/main/aptos/sources/amm_swap.move"
    click A_Utils "https://github.com/cetusprotocol/cetus-amm/blob/main/aptos/sources/amm_utils.move"

    %% Click Events for Sui Module
    click S_Conf "https://github.com/cetusprotocol/cetus-amm/blob/main/sui/sources/amm_config.move"
    click S_Math "https://github.com/cetusprotocol/cetus-amm/blob/main/sui/sources/amm_math.move"
    click S_Router "https://github.com/cetusprotocol/cetus-amm/blob/main/sui/sources/amm_router.move"
    click S_Script "https://github.com/cetusprotocol/cetus-amm/blob/main/sui/sources/amm_script.move"
    click S_Swap "https://github.com/cetusprotocol/cetus-amm/blob/main/sui/sources/amm_swap.move"
    click S_Utils "https://github.com/cetusprotocol/cetus-amm/blob/main/sui/sources/amm_utils.move"
```
