# aegisfinance/aegis architecture

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

- Interactive diagram: https://gitdiagram.com/aegisfinance/aegis
- Repository: https://github.com/aegisfinance/aegis

## Overview

##### Project Type & Purpose
- This is a backend canisters project built for the Internet Computer (IC) blockchain. It implements the core logic for Aegis Finance’s decentralized exchange platform.
- The system supports several key use cases: managing user tokens (ICP, ckBTC, ckETH), options trading, insurance contracts, and staking of AEGIS tokens.
- That the frontend is maintained in another repo (Aegis-Dapp), so the focus here is solely on backend canisters and supporting libraries.

##### Main Components
- Canisters: The heart of the system is its multiple independent canisters:
– Accounts Canister: Manages user tokens including deposit, withdrawal, and inter-chain conversions.
– Main Canister: Handles staking functions for $AEGIS tokens.
– Insurance Canister: Implements buying/selling of insurance contracts.
– Options Canister: Implements trading options contracts.
- Ledgers & Minters: There are dedicated components and associated WASM modules for various ledgers (ckbtc, cketh, ckusdt, icp) plus minter utilities. These interact with the accounts canister and support token conversions.
- Libraries: Several shared libraries such as “blackscholes-rust” (for pricing models and options calculations), ic_ledger_utils, management_canister, minter_utils, serializer, and stable_memory provide core functionalities and are reused across the system.
- Declarations & Interfaces: The “declarations” directory and corresponding TypeScript declarations indicate a strong typed interface for canister interactions (e.g., the candid interface files for each canister).

##### Relationships and Interactions
- Data flow should show API interactions between these canisters where necessary (for instance, the accounts canister will interact with the ledgers/minter modules for token management).
- External services include:
– Bitcoin Node: A separate external component (managed via scripts such as bitcoin.sh) that supports BTC node operations and integration.
– External blockchains’ assets (BTC, ETH, etc.) are interfaced via the ledger canisters.
- The libraries are shared resources used by one or more canisters. Group these as supporting components that interact with the core canisters.

##### Architectural Patterns & Design Principles
- The design leverages a microservices-like, canister-based architecture where each major function (tokens, insurance, options, staking) is encapsulated in its own module.
- There is a clear separation of concerns: core business logic exists in canisters, while shared utilities are abstracted away in libraries.
- The use of candid interface definitions and declarations implies a strong API contract between components.
- Deployment and testing scripts suggest automated builds, deployment pipelines, and thorough unit/integration tests.

##### Final Details for the Engineer
- Canister-based component is clearly separated to emphasize independence.
- Be as detailed as possible—don’t shy away from breaking down internal modules (e.g., within the accounts canister, show the subdivision into API routes, lifecycle events, wallet management, etc.) if space allows.
- Interplay between blockchain integrations (Bitcoin node, multi-chain assets) and the decentralized canister deployments.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Backend Canisters
    subgraph "Backend Canisters"
        ACC["Accounts Canister"]:::canister
        MAIN["Main Canister"]:::canister
        INS["Insurance Canister"]:::canister
        OPT["Options Canister"]:::canister
    end

    %% Token Ledgers and Minters
    subgraph "Token Ledgers and Minters"
        subgraph "Ledger Components"
            CKBTC_LEDGER["ckBTC Ledger"]:::ledger
            CKETH_LEDGER["ckETH Ledger"]:::ledger
            CKUSDT_LEDGER["ckUSDT Ledger"]:::ledger
            ICP_LEDGER["ICP Ledger"]:::ledger
        end
        subgraph "Minter Components"
            CKBTC_MINTER["ckBTC Minter"]:::minter
            CKETH_MINTER["ckETH Minter"]:::minter
            KYT["Kyt Utility"]:::minter
        end
    end

    %% Shared Libraries
    subgraph "Shared Libraries"
        BS["blackscholes-rust"]:::lib
        ICLU["ic_ledger_utils"]:::lib
        MGMT["management_canister"]:::lib
        MINTR_UTIL["minter_utils"]:::lib
        SER["serializer"]:::lib
        STABLE["stable_memory"]:::lib
    end

    %% External Services
    subgraph "External Services"
        BTC_NODE["Bitcoin Node"]:::external
    end

    %% API Client & Deployment
    subgraph "API & CI/CD"
        DAPP["Aegis-Dapp (API Client)"]:::external
        PIPELINE["CI/CD Pipeline & Tests"]:::pipeline
    end

    %% Relationships

    %% Accounts Canister interacts with ledger components and minters
    ACC -->|"calls"| CKBTC_LEDGER
    ACC -->|"calls"| CKETH_LEDGER
    ACC -->|"calls"| CKUSDT_LEDGER
    ACC -->|"calls"| ICP_LEDGER
    ACC -->|"triggers"| CKBTC_MINTER
    ACC -->|"triggers"| CKETH_MINTER
    ACC -->|"triggers"| KYT

    %% Canisters using Shared Libraries
    ACC -->|"uses"| BS
    ACC -->|"uses"| ICLU
    ACC -->|"uses"| MGMT
    ACC -->|"uses"| MINTR_UTIL
    ACC -->|"uses"| SER
    ACC -->|"uses"| STABLE

    MAIN -->|"uses"| BS
    MAIN -->|"uses"| ICLU
    MAIN -->|"uses"| MGMT
    MAIN -->|"uses"| MINTR_UTIL
    MAIN -->|"uses"| SER
    MAIN -->|"uses"| STABLE

    INS -->|"uses"| BS
    OPT -->|"uses"| BS

    %% Inter-canister interactions
    MAIN -->|"updates staking"| ACC

    %% External integration with Bitcoin Node
    BTC_NODE -->|"feedsData"| CKBTC_LEDGER
    BTC_NODE -->|"feedsData"| CKETH_LEDGER

    %% External API client interactions
    DAPP -->|"callsAPIs"| ACC
    DAPP -->|"callsAPIs"| MAIN
    DAPP -->|"callsAPIs"| INS
    DAPP -->|"callsAPIs"| OPT

    %% CI/CD Pipeline connecting to core components
    PIPELINE --> ACC
    PIPELINE --> MAIN
    PIPELINE --> INS
    PIPELINE --> OPT
    PIPELINE --> BS

    %% Click Events
    click ACC "https://github.com/aegisfinance/aegis/tree/main/src/canisters/accounts"
    click MAIN "https://github.com/aegisfinance/aegis/tree/main/src/canisters/main"
    click INS "https://github.com/aegisfinance/aegis/tree/main/src/canisters/insurance"
    click OPT "https://github.com/aegisfinance/aegis/tree/main/src/canisters/options"

    click CKBTC_LEDGER "https://github.com/aegisfinance/aegis/blob/main/src/ledgers/ckbtc_ledger.did"
    click CKETH_LEDGER "https://github.com/aegisfinance/aegis/blob/main/src/ledgers/cketh_ledger.did"
    click CKUSDT_LEDGER "https://github.com/aegisfinance/aegis/blob/main/src/ledgers/ckusdt_ledger.did"
    click ICP_LEDGER "https://github.com/aegisfinance/aegis/blob/main/src/ledgers/icp_ledger.did"

    click CKBTC_MINTER "https://github.com/aegisfinance/aegis/blob/main/src/minter/ckbtc_minter.did"
    click CKETH_MINTER "https://github.com/aegisfinance/aegis/blob/main/src/minter/cketh_minter.did"
    click KYT "https://github.com/aegisfinance/aegis/tree/main/src/minter/kyt"

    click BS "https://github.com/aegisfinance/aegis/tree/main/src/libraries/blackscholes-rust"
    click ICLU "https://github.com/aegisfinance/aegis/tree/main/src/libraries/ic_ledger_utils"
    click MGMT "https://github.com/aegisfinance/aegis/tree/main/src/libraries/management_canister"
    click MINTR_UTIL "https://github.com/aegisfinance/aegis/tree/main/src/libraries/minter_utils"
    click SER "https://github.com/aegisfinance/aegis/tree/main/src/libraries/serializer"
    click STABLE "https://github.com/aegisfinance/aegis/tree/main/src/libraries/stable_memory"

    click BTC_NODE "https://github.com/aegisfinance/aegis/blob/main/scripts/bitcoin.sh"

    %% Styles
    classDef canister fill:#e0f7fa,stroke:#006064,stroke-width:2px;
    classDef ledger fill:#ffe0b2,stroke:#e65100,stroke-width:2px;
    classDef minter fill:#d1c4e9,stroke:#311b92,stroke-width:2px;
    classDef lib fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef external fill:#ffcdd2,stroke:#b71c1c,stroke-width:2px;
    classDef pipeline fill:#fff9c4,stroke:#f57f17,stroke-width:2px;
```
