# term-structure/ts-smart-contract architecture

> Architecture diagram of the GitHub repository term-structure/ts-smart-contract, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/term-structure/ts-smart-contract
- Repository: https://github.com/term-structure/ts-smart-contract

## Overview

##### Key Interactions to Highlight

a) User Interaction Flow
- Account registration
- Deposits/Withdrawals
- Loan operations
- Flash loan requests

b) Protocol Integration Flow
- How the system interacts with:
- Price feeds (indicated by price feed contracts in scripts/deploy/extension)
- External protocols (Aave, Pendle, etc.)
- Token wrappers and routers

##### Technical Components

a) Infrastructure Layer (Bottom)
- Verifier components (from contracts/zkTrueUp/verifier)
- Libraries (from contracts/zkTrueUp/libraries)
- Initialization system (from contracts/zkTrueUp/initializer)

b) Security & Control Flow
- Access control mechanisms
- Upgrade paths
- Evacuation system components (from contracts/zkTrueUp/evacuation)

##### Layer Organization

Top to Bottom:
##### Storage & State Management

Remember to include notes about the upgrade mechanisms and how the Diamond pattern enables modular functionality, as this is a key architectural feature of the system.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef external fill:#90EE90,stroke:#333,stroke-width:2px
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:2px
    classDef infrastructure fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef security fill:#FFD700,stroke:#333,stroke-width:2px
    classDef interaction fill:#FFA500,stroke:#333,stroke-width:2px
    classDef storage fill:#B8860B,stroke:#333,stroke-width:2px

    %% External Protocols Layer
    subgraph ExternalLayer["External Protocol Layer"]
        AaveV3["Aave V3"]:::external
        PendlePT["Pendle PT"]:::external
        SDai["Staked DAI"]:::external
        WstETH["Wrapped stETH"]:::external
    end

    %% Price Feeds
    subgraph PriceFeeds["Price Feed Layer"]
        PTFeed["PT Price Feed"]:::external
        SDaiFeed["sDAI Price Feed"]:::external
        WstETHFeed["wstETH Price Feed"]:::external
    end

    %% Core Diamond
    Diamond{{"Diamond Proxy"}}:::core

    %% Facets Layer
    subgraph Facets["Facet Layer"]
        Account["Account Management"]:::core
        Loan["Loan Operations"]:::core
        Flash["Flash Loan"]:::core
        Protocol["Protocol Parameters"]:::core
        Token["Token Management"]:::core
        TSB["TSB Operations"]:::core
    end

    %% Infrastructure Layer
    subgraph Infrastructure["Infrastructure Layer"]
        Verifier["Verifier"]:::infrastructure
        Libraries["Libraries"]:::infrastructure
        Init["Initializer"]:::infrastructure
        Rollup["Rollup Mechanism"]:::infrastructure
    end

    %% Security Layer
    subgraph Security["Security Layer"]
        Access["Access Control"]:::security
        Upgrade["Upgrade System"]:::security
        Evac["Evacuation System"]:::security
    end

    %% Connections
    Diamond --> Account
    Diamond --> Loan
    Diamond --> Flash
    Diamond --> Protocol
    Diamond --> Token
    Diamond --> TSB

    Account --> AaveV3
    Loan --> PendlePT
    Token --> SDai
    TSB --> WstETH

    PTFeed --> PendlePT
    SDaiFeed --> SDai
    WstETHFeed --> WstETH

    Diamond --> Infrastructure
    Diamond --> Security

    %% Click Events
    click Account "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/account"
    click Loan "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/loan"
    click Flash "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/flashLoan"
    click Protocol "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/protocolParams"
    click Token "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/token"
    click TSB "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/tsb"
    click AaveV3 "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/interfaces/aaveV3"
    click PendlePT "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/extensions/pendlePT"
    click SDai "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/extensions/sDai"
    click WstETH "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/extensions/wstETH"
    click Verifier "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/verifier"
    click Libraries "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/libraries"
    click Init "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/initializer"
    click Evac "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/evacuation"
    click Rollup "https://github.com/term-structure/TS-smart-contract/tree/main/contracts/zkTrueUp/rollup"
    click PTFeed "https://github.com/term-structure/TS-smart-contract/blob/main/scripts/deploy/extension/mainnet/PTWithRedStonePriceFeed.ts"
    click SDaiFeed "https://github.com/term-structure/TS-smart-contract/blob/main/scripts/deploy/extension/mainnet/sdaiPriceFeed.ts"
    click WstETHFeed "https://github.com/term-structure/TS-smart-contract/blob/main/scripts/deploy/extension/mainnet/wstethPriceFeed.ts"

    %% Legend
    subgraph Legend["Legend"]
        L1["External Protocols"]:::external
        L2["Core Components"]:::core
        L3["Infrastructure"]:::infrastructure
        L4["Security"]:::security
    end
```
