# altitude-fi/altitude-v2 architecture

> Architecture diagram of the GitHub repository altitude-fi/altitude-v2, 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/altitude-fi/altitude-v2
- Repository: https://github.com/altitude-fi/altitude-v2

## Overview

The Altitude Protocol is a blockchain-based solution implemented as a suite of smart contracts. Its core purpose is to facilitate over-collateralized loans using individual vaults that manage a supply/borrow currency pair (for example, an ETH-USDC vault). These vaults enable a range of operations—from deposit/withdraw and borrowing/repaying, to activating dormant collateral through yield-generating strategies.

##### Project Type & Purpose
- This project is focused on decentralized finance (DeFi) and consists of smart contract implementations along with extensive testing and deployment scripts.
- According to the README, it manages user vaults that incorporate multiple operational facets such as lending, yield farming, rebalancing, harvesting, and liquidations.

##### Analysis of the File Structure
- The primary directory you want to focus on is "contracts", which contains multiple subdirectories:
– "interfaces": Defines both external and internal contracts that set the standards and boundaries between components.
– "strategies": Contains specific implementations for various strategies (farming, lending, flashloan, swap) that deploy user assets.
– "vaults": Includes different versions and extensions for the vaults, ensuring modularity for features like tokenization, configurable behavior, and risk management (e.g., liquidatable, snapshotable).
– "libraries" and "misc": Provide utility functions and types that help standardize interactions and computations.
- The "scripts" (specifically, "scripts/deployer") indicate automation for contract deployment.
- The "test" directories are split between integration-tests and unit-tests, ensuring that both isolated contract functionalities and full system interactions are verified.

##### Key Components & Their Interactions
a. Vault Core:
– Represents the primary user interaction point for managing deposits, withdrawals, borrowing, and repayments.
b. Lender Strategy:
– This component is responsible for deploying user assets into external lending protocols (such as Aave) to secure competitive interest rates.
c. Farm Dispatcher:
– Handles the activation of dormant collateral by directing assets into farming strategies to generate yield.
d. Rebalancer:
– Ensures that the vault’s position remains healthy by appropriately adjusting borrow and repay transactions.
e. Harvesting & Position Update Components:
– These periodically recognize earnings, update user balances, and facilitate the distribution of gains among stakeholders.
f. Liquidation Mechanism:
– Handles cases where a user’s position becomes unhealthy, triggering liquidation for risk management.
g. Tokenization:
– Provides mechanisms to tokenize supply and debt positions, likely related to ERC20 and ETH representations.

##### Architectural Patterns & Principles
- Modular Design – Contracts are separated into multiple directories that encapsulate specific domains (e.g., farming, lending, vaults) which promotes reusability and easier maintenance.
- Interface Segregation – Clear separation between internal and external interfaces ensures that implementation details remain encapsulated.
- Extensibility – The use of extensions in the vaults (e.g., configurable, liquidatable) suggests a plug-in style architecture, making it easier to integrate new features or strategies.

##### Relevant Technologies & Libraries
- Blockchain & Smart Contracts - Likely implemented in Solidity.
- Testing Suites - Organized into unit and integration tests to simulate realistic DeFi scenarios.
- Deployment Automation - Provided via scripts under "scripts/deployer" to streamline smart contract deployment.
- Utility Libraries – Include libraries for interactions with protocols like Uniswap V3, which may play a role in swap strategies.

## Diagram (Mermaid)

```mermaid
graph TD
    userNode["User Interaction"]
    vaultCore["Vault Core (Deposit/Withdraw; Borrow/Repay)"]
    lenderStrategy["Lender Strategy (Deploys Funds to Lending Protocols)"]
    farmDispatcher["Farm Dispatcher (Activates Farming Strategies)"]
    rebalancer["Rebalancer (Adjusts Borrow/Repay)"]
    harvestingPos["Harvesting & Position Update (Updates Earnings)"]
    liquidationMechanism["Liquidation Mechanism (Manages Risk and Liquidations)"]
    tokenization["Tokenization (ERC20 Representation)"]
    
    externalLending["External Lending Protocols"]
    externalFarming["External Farming Protocols"]
    
    deployment["Contract Deployer"]
    integrationTests["Integration Tests"]
    unitTests["Unit Tests"]

    %% Main User Flow
    userNode -->|"initiates"| vaultCore

    %% Vault Core interacts with core operational components
    vaultCore -->|"manages funds"| lenderStrategy
    vaultCore -->|"manages funds"| farmDispatcher
    vaultCore -->|"triggers"| rebalancer
    vaultCore -->|"triggers"| harvestingPos
    vaultCore -->|"triggers"| liquidationMechanism
    vaultCore -->|"updates"| tokenization

    %% Strategies interacting with external protocols
    lenderStrategy -->|"deposits funds"| externalLending
    farmDispatcher -->|"activates yield"| externalFarming

    %% Feedback from external protocols to Vault Core
    externalLending -->|"returns benefits"| vaultCore
    externalFarming -->|"returns yield"| vaultCore

    %% Feedback loops from internal processes
    rebalancer -->|"adjusts positions"| vaultCore
    harvestingPos -->|"updates balance"| vaultCore
    liquidationMechanism -->|"executes liquidation"| vaultCore
    tokenization -->|"updates tokens"| vaultCore

    %% Deployment and Testing components
    deployment -->|"deploys contracts"| vaultCore
    integrationTests -->|"validates"| vaultCore
    unitTests -->|"validates"| vaultCore

    %% Styling Classes
    classDef user fill:#ffeaa7,stroke:#333,stroke-width:2px;
    classDef core fill:#feca57,stroke:#333,stroke-width:2px;
    classDef strategy fill:#55efc4,stroke:#333,stroke-width:2px;
    classDef process fill:#81ecec,stroke:#333,stroke-width:2px;
    classDef external fill:#fab1a0,stroke:#333,stroke-width:2px;
    classDef deployment fill:#dfe6e9,stroke:#333,stroke-width:2px;

    class userNode user;
    class vaultCore core;
    class lenderStrategy,farmDispatcher strategy;
    class rebalancer,harvestingPos,liquidationMechanism,tokenization process;
    class externalLending,externalFarming external;
    class deployment,integrationTests,unitTests deployment;

    %% Click Events
    click vaultCore "https://github.com/altitude-fi/altitude-v2/tree/main/contracts/vaults/v1/base"
    click lenderStrategy "https://github.com/altitude-fi/altitude-v2/tree/main/contracts/strategies/lending/aave"
    click farmDispatcher "https://github.com/altitude-fi/altitude-v2/tree/main/contracts/strategies/farming"
    click rebalancer "https://github.com/altitude-fi/altitude-v2/tree/main/contracts/misc/incentives/rebalance"
    click harvestingPos "https://github.com/altitude-fi/altitude-v2/tree/main/contracts/vaults/v1/extensions/snapshotable/harvest"
    click liquidationMechanism "https://github.com/altitude-fi/altitude-v2/tree/main/contracts/vaults/v1/extensions/liquidatable"
    click tokenization "https://github.com/altitude-fi/altitude-v2/tree/main/contracts/vaults/v1/ERC20"
```
