# 0xtan1319/token2022-tax-fee-distribution-contract architecture

> Architecture diagram of the GitHub repository 0xtan1319/token2022-tax-fee-distribution-contract, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0xtan1319/token2022-tax-fee-distribution-contract
- Repository: https://github.com/0xtan1319/token2022-tax-fee-distribution-contract

## Overview

##### Main Components
- CLI Layer: This is responsible for interacting with the smart contract. It includes scripts for commands (command.ts, scripts.ts), configuration files (config.ts), utility functions (utils.ts), and type definitions (types.ts). Additionally, web3 bindings and IDL files (distribution.json and distribution.ts) are used to facilitate communication with the blockchain.
- On-Chain Program (Smart Contract): Found under programs/Distribution, this Rust code (with its own Cargo.toml and Xargo.toml) implements the distribution logic, account management (accounts, constants, errors, lib logic), and adheres to Solana’s programming model.
- Migrations & Deployment Scripts: The migrations folder includes deploy scripts (both JavaScript and TypeScript) to assist in program deployment on the blockchain.
- Build & Target Artifacts: The target directory shows deployment outcomes and generated IDL files which are used by the CLI to create type-safe bindings.
- Configuration & Dependency Management: Files like Cargo.toml, package.json, and tsconfig.json indicate that the project integrates both Rust (via Cargo) and Node.js (with yarn) for different parts of the system.

##### Relationships and Interactions
- The CLI (client) sends transactions and script commands (using web3 libraries) to the deployed on-chain smart contract.
- There is a directional relationship where the CLI acts as the entry point for different roles: Smart Contract Owner, Caller, and Guest. Each of these roles triggers different functions (init_global, update_caller, reward distribution commands, and status queries).
- The Rust smart contract handles validation, reward distribution logic, and state management (e.g., initializing the Global PDA and managing reward modifications).
- The migration scripts are used for deployment, ensuring the proper setup of on-chain accounts and PDAs before any CLI commands are executed.
- The CLI uses IDL files (and their generated TypeScript counterparts) to simplify interaction with the Solana web3 library and ensure that commands correctly reference the on-chain program.

##### Architectural Patterns and Key Technologies
- It uses a layered architecture with a clear separation between the CLI (off-chain) and the on-chain smart contract (backend logic).
- The project leverages the Anchor framework (as indicated by the Anchor.toml file) which implies the usage of best practices for Solana smart contract development.
- It also demonstrates role-based command functionality, differentiating between admin functions (like transferring Mint Authority, updating caller address) and caller functions (reward distribution), hinting at security and access control as core design elements.
- Both TypeScript (Node.js) and Rust are key components, each playing their role in the overall system. The migrations also emphasize the use of deployment scripts to maintain consistent blockchain state.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Global Project Configuration
    ProjConfig["Project Configuration"]:::configColor

    %% Migrations & Deployment Scripts
    subgraph "Migrations & Deployment Scripts"
        migrationDeployJs["deploy.js"]:::migrationColor
        migrationDeployTs["deploy.ts"]:::migrationColor
    end

    %% Build & Target Artifacts
    subgraph "Build & Target Artifacts"
        targetDeploy["Deploy Artifacts (target/deploy)"]:::buildColor
        targetIDL["Generated IDL (target/idl/distribution.json)"]:::buildColor
        targetTypes["Generated Types (target/types/distribution.ts)"]:::buildColor
    end

    %% On-Chain Program (Smart Contract)
    subgraph "On-Chain Program"
        subgraph "Program Core Files"
            rustLib["lib.rs"]:::onchainColor
            rustAccount["account.rs"]:::onchainColor
            rustConstants["constants.rs"]:::onchainColor
            rustError["error.rs"]:::onchainColor
        end
        subgraph "Build Files"
            cargoToml["Cargo.toml"]:::onchainColor
            xargoToml["Xargo.toml"]:::onchainColor
        end
    end

    %% CLI / Client
    subgraph "CLI/Client"
        subgraph "Command & Script Handlers"
            cliCommand["command.ts"]:::cliColor
            cliScripts["scripts.ts"]:::cliColor
        end
        subgraph "Configuration & Utilities"
            cliConfig["config.ts"]:::cliColor
            cliUtils["utils.ts"]:::cliColor
            cliTypes["types.ts"]:::cliColor
        end
        subgraph "Web3 & IDL Integration"
            cliDistributionJson["distribution.json"]:::cliColor
            cliDistributionTs["distribution.ts"]:::cliColor
            cliLib["lib.ts"]:::cliColor
        end
    end

    %% Connections from Project Configuration
    ProjConfig ---|"dependency_management"| migrationDeployJs
    ProjConfig ---|"dependency_management"| targetDeploy
    ProjConfig ---|"dependency_management"| cargoToml
    ProjConfig ---|"dependency_management"| cliConfig

    %% Deployment flow: Migrations deploy to On-Chain Program
    migrationDeployJs ---|"deploy_setup"| cargoToml
    migrationDeployTs ---|"deploy_setup"| xargoToml

    %% CLI communicates with On-Chain Program
    cliCommand ---|"transaction_submission"| rustLib
    rustLib ---|"status_response"| cliCommand

    %% Build artifacts assist CLI integration via IDL binding
    targetIDL ---|"IDL_binding"| cliDistributionTs

    %% Class Definitions
    classDef cliColor fill:#d4e157,stroke:#33691e,stroke-width:2px;
    classDef onchainColor fill:#81d4fa,stroke:#01579b,stroke-width:2px;
    classDef migrationColor fill:#ffab91,stroke:#bf360c,stroke-width:2px;
    classDef buildColor fill:#ce93d8,stroke:#6a1b9a,stroke-width:2px;
    classDef configColor fill:#a5d6a7,stroke:#2e7d32,stroke-width:2px;

    %% Click Events for CLI Layer
    click cliCommand "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/command.ts"
    click cliScripts "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/scripts.ts"
    click cliConfig "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/config.ts"
    click cliUtils "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/utils.ts"
    click cliTypes "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/types.ts"
    click cliDistributionJson "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/distribution.json"
    click cliDistributionTs "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/distribution.ts"
    click cliLib "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/cli/lib.ts"

    %% Click Events for On-Chain Program
    click rustLib "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/programs/Distribution/src/lib.rs"
    click rustAccount "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/programs/Distribution/src/account.rs"
    click rustConstants "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/programs/Distribution/src/constants.rs"
    click rustError "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/programs/Distribution/src/error.rs"
    click cargoToml "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/programs/Distribution/Cargo.toml"
    click xargoToml "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/programs/Distribution/Xargo.toml"

    %% Click Events for Migrations
    click migrationDeployJs "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/migrations/deploy.js"
    click migrationDeployTs "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/migrations/deploy.ts"

    %% Click Events for Build & Target Artifacts
    click targetDeploy "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/tree/main/target/deploy"
    click targetIDL "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/target/idl/distribution.json"
    click targetTypes "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/target/types/distribution.ts"

    %% Click Events for Project Configuration & Dependency Management
    click ProjConfig "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/Anchor.toml"
    click ProjConfig "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/Cargo.toml"
    click ProjConfig "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/Cargo.lock"
    click ProjConfig "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/package.json"
    click ProjConfig "https://github.com/0xtan1319/token2022-tax-fee-distribution-contract/blob/main/tsconfig.json"
```
