# emmanuelist/decentralized-bitcoin-backed-microlending architecture

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

- Interactive diagram: https://gitdiagram.com/emmanuelist/decentralized-bitcoin-backed-microlending
- Repository: https://github.com/emmanuelist/decentralized-bitcoin-backed-microlending

## Overview

##### Main Components
- Frontend: Built using React (with TypeScript), Vite, and Tailwind CSS. This layer covers all user-facing aspects such as the dashboard, loan management interfaces, collateral handling, and admin panels.
- Blockchain & Smart Contract: The smart contract (microlending.clar) is written in Clarity and deployed on the Stacks blockchain. This component handles core financial logic such as loan creation, liquidation, collateral price updates, and emergency controls.
- Integration Layer: This consists of client-side libraries and utility functions (found in frontend/src/lib/stacks and frontend/src/lib) that facilitate communication between the React app and the deployed blockchain contracts. It also includes integration with external wallets (Hiro Wallet) and other blockchain services.
- Testing & Configuration: The tests directory (tests/microlending_test.ts) ensures functionality and proper smart contract interaction. Additionally, configuration files such as Clarinet.toml (for smart contract tools) and settings/Devnet.toml (network configuration for development) highlight the importance of deployment and environment management.

##### Relationships and Interactions
- External service integration with Hiro Wallet as a component connected to both the frontend (for user authentication) and the blockchain (for transaction signing).

##### Architectural Patterns and Design Principles
- Separation of concerns: The frontend is entirely decoupled from blockchain logic via dedicated libraries.
- That the project follows a modular design: distinct directories for contracts, frontend UI components, and tests.
- Use of modern frontend tooling (Vite, React) together with blockchain integration patterns common in decentralized applications.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface Layer"
        UI["Frontend UI (React)"]:::frontend
        Dashboard["Dashboard"]:::frontend
        LoanForm["Loan Form"]:::frontend
        AdminPanel["Admin Panel"]:::frontend
        LoanTable["Loan Table"]:::frontend
    end

    %% Integration & Service Layer
    subgraph "Integration & Service Layer"
        Integration["Integration Layer (Stacks API & Utilities)"]:::integration
    end

    %% Blockchain Layer
    subgraph "Blockchain Layer"
        Contract["Smart Contract (Clarity)\n(create-loan-request,liquidate-loan,update-asset-price,toggle-emergency-stop)"]:::contract
        Blockchain["Blockchain Network (Stacks)"]:::blockchain
    end

    %% External Wallet Integration
    Wallet["Wallet Integration (Hiro Wallet)"]:::wallet

    %% Testing & Environment Configuration
    subgraph "Testing & Environment"
        Tests["Tests (microlending_test.ts)"]:::testing
        Clarinet["Clarinet.toml"]:::testing
        Devnet["Devnet.toml"]:::testing
    end

    %% Connections from UI Components to Integration Layer
    Dashboard -->|"triggers_API"| Integration
    LoanForm -->|"triggers_API"| Integration
    AdminPanel -->|"triggers_API"| Integration
    LoanTable -->|"triggers_API"| Integration

    %% Connection from Frontend Container to Wallet for user authentication
    UI -->|"auth"| Wallet

    %% Integration calling Smart Contract functions
    Integration -->|"calls_SC"| Contract

    %% Smart Contract deployed on Blockchain
    Contract -->|"deployed_on"| Blockchain

    %% Wallet signs transactions on Blockchain network
    Wallet -->|"txnsign"| Blockchain

    %% Testing validates Smart Contract interactions
    Tests -->|"validate_SC"| Contract

    %% Testing uses configuration files
    Tests -->|"usesConfig"| Clarinet
    Tests -->|"usesConfig"| Devnet


    %% Click Events
    click UI "https://github.com/emmanuelist/decentralized-bitcoin-backed-microlending/tree/main/frontend/"
    click Contract "https://github.com/emmanuelist/decentralized-bitcoin-backed-microlending/blob/main/contracts/microlending.clar"
    click Integration "https://github.com/emmanuelist/decentralized-bitcoin-backed-microlending/tree/main/frontend/src/lib/"
    click Tests "https://github.com/emmanuelist/decentralized-bitcoin-backed-microlending/blob/main/tests/microlending_test.ts"
    click Clarinet "https://github.com/emmanuelist/decentralized-bitcoin-backed-microlending/blob/main/Clarinet.toml"
    click Devnet "https://github.com/emmanuelist/decentralized-bitcoin-backed-microlending/blob/main/settings/Devnet.toml"

    %% Styles
    classDef frontend fill:#aaddff,stroke:#333,stroke-width:2px;
    classDef integration fill:#ddffaa,stroke:#333,stroke-width:2px;
    classDef contract fill:#ffddaa,stroke:#333,stroke-width:2px;
    classDef blockchain fill:#ffaaff,stroke:#333,stroke-width:2px;
    classDef wallet fill:#ffffaa,stroke:#333,stroke-width:2px;
    classDef testing fill:#aaffaa,stroke:#333,stroke-width:2px;
```
