# jacobcreech/anchor-escrow-2024 architecture

> Architecture diagram of the GitHub repository jacobcreech/anchor-escrow-2024, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jacobcreech/anchor-escrow-2024
- Repository: https://github.com/jacobcreech/anchor-escrow-2024

## Overview

##### Project type and purpose
- This is a Solana smart-contract (on-chain program) built with the Anchor framework (“anchor-escrow”).
- It implements an escrow service: makers lock Token A in a PDA vault and takers deposit Token B; once a threshold is met, tokens swap or maker can refund.

##### Main components
a. Off-chain clients
- Maker wallet (Signer)
- Taker wallet (Signer)
- Anchor CLI / JS test harness (tests/anchor-escrow.ts)
- Migration script (migrations/deploy.ts)
b. On-chain program & accounts
- Anchor Escrow Program (programs/anchor-escrow)
- Escrow state account (PDA)
- Vault token account (PDA)
- Maker ATA_A (associated token account for Token A)
- Maker ATA_B (for Token B)
- Taker ATA_A, Taker ATA_B
c. External Solana system programs
- System Program (account creation, funding)
- SPL Token Program (transfer_checked, close_account)
- Associated Token Program (init_if_needed, ATA creation)

##### Relationships & data flows
- Deployment: Migration script → Anchor Program → Solana Cluster
- Initialization (“Make”): Maker wallet → Anchor Program →
- create escrow PDA (Escrow state)
- create vault PDA (holds Token A)
- transfer Token A from Maker ATA_A → Vault PDA
- Refund (“Refund”): Maker wallet → Anchor Program →
- transfer all Token A from Vault PDA → Maker ATA_A
- close Vault PDA (lamports returned to maker)
- close Escrow state account
- Take (“Take”): Taker wallet → Anchor Program →
- transfer “receive” amount of Token B from Taker ATA_B → Maker ATA_B
- transfer all Token A from Vault PDA → Taker ATA_A
- close Vault PDA & Escrow state account

##### Architectural patterns & design principles
- PDA-based accounts for Escrow state & Vault (deterministic addresses via seeds: “escrow” + maker pubkey + seed)
- Anchor’s Account Contexts (Make, Refund, Take) separating concerns per instruction
- CPI calls to Token Program & AssociatedToken Program for token transfers and account closures
- Strong typing of accounts via Anchor’s InterfaceAccount and Account<…> generics

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Off-chain Clients
    subgraph "Off-chain Clients"
        Maker["Maker Wallet (Signer)"]:::offchain
        Taker["Taker Wallet (Signer)"]:::offchain
        TestHarness["Anchor CLI / JS Test Harness"]:::offchain
        Migration["Migration Script"]:::offchain
    end

    %% On-chain Program
    subgraph "Anchor Escrow Program (On-chain)"
        AEProgram["Anchor Escrow Program"]:::program
        Cargo["Cargo.toml"]:::program
        Lib["src/lib.rs"]:::program
        Contexts["src/contexts/mod.rs"]:::program
        MakeCtx["src/contexts/make.rs"]:::program
        RefundCtx["src/contexts/refund.rs"]:::program
        TakeCtx["src/contexts/take.rs"]:::program
        StateDef["src/state.rs"]:::program
    end

    %% On-chain Accounts
    subgraph "On-chain Accounts (PDAs & ATAs)"
        EscrowPDA["Escrow State Account PDA\nseeds: ['escrow', maker, seed]"]:::accounts
        VaultPDA["Vault Token Account PDA\nseeds: ['vault', maker, seed]"]:::accounts
        ATA_MA["Maker ATA_A"]:::accounts
        ATA_MB["Maker ATA_B"]:::accounts
        ATA_TA["Taker ATA_A"]:::accounts
        ATA_TB["Taker ATA_B"]:::accounts
    end

    %% Solana System Programs
    subgraph "Solana Native Programs"
        System["System Program"]:::solana
        SPL["SPL Token Program"]:::solana
        ATAProg["Associated Token Program"]:::solana
    end

    %% Deployment flow
    Migration -->|"deploy"| AEProgram

    %% Make flow
    Maker -->|"make"| AEProgram
    AEProgram -->|"create Escrow PDA\n(System Program)"| System
    AEProgram -->|"create Vault PDA\n(ATA Program)"| ATAProg
    AEProgram -->|"transfer A to Vault\n(transfer_checked)"| SPL
    SPL --> VaultPDA

    %% Refund flow
    Maker -->|"refund"| AEProgram
    AEProgram -->|"transfer A to Maker\n(transfer_checked)"| SPL
    SPL --> ATA_MA
    AEProgram -->|"close Vault PDA\n(close_account)"| SPL
    AEProgram -->|"close Escrow PDA\n(close_account)"| System

    %% Take flow
    Taker -->|"take"| AEProgram
    AEProgram -->|"transfer B to Maker\n(transfer_checked)"| SPL
    SPL --> ATA_MB
    AEProgram -->|"transfer A to Taker\n(transfer_checked)"| SPL
    SPL --> ATA_TA
    AEProgram -->|"close Vault PDA\n(close_account)"| SPL
    AEProgram -->|"close Escrow PDA\n(close_account)"| System

    %% Click Events
    click TestHarness "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/tests/anchor-escrow.ts"
    click Migration "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/migrations/deploy.ts"
    click AEProgram "https://github.com/jacobcreech/anchor-escrow-2024/tree/master/programs/anchor-escrow/"
    click Cargo "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/programs/anchor-escrow/Cargo.toml"
    click Lib "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/programs/anchor-escrow/src/lib.rs"
    click StateDef "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/programs/anchor-escrow/src/state.rs"
    click MakeCtx "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/programs/anchor-escrow/src/contexts/make.rs"
    click RefundCtx "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/programs/anchor-escrow/src/contexts/refund.rs"
    click TakeCtx "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/programs/anchor-escrow/src/contexts/take.rs"
    click Contexts "https://github.com/jacobcreech/anchor-escrow-2024/blob/master/programs/anchor-escrow/src/contexts/mod.rs"

    %% Styles
    classDef offchain fill:#f9f,stroke:#333,stroke-width:1px
    classDef program fill:#bbf,stroke:#333,stroke-width:1px
    classDef accounts fill:#bfb,stroke:#333,stroke-width:1px
    classDef solana fill:#ffd,stroke:#333,stroke-width:1px
```
