# freesparrowrob/dex-arbitrage-bot architecture

> Architecture diagram of the GitHub repository freesparrowrob/dex-arbitrage-bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/freesparrowrob/dex-arbitrage-bot
- Repository: https://github.com/freesparrowrob/dex-arbitrage-bot

## Overview

##### Project Type and Purpose
- The project is a blockchain-based arbitrage bot that monitors multiple decentralized exchanges (DEXs) (e.g., Uniswap V3, Trader Joe, Sushiswap, Pancakeswap, with dYdX coming soon).
- Its purpose is to scan for arbitrage opportunities – buying low on one exchange and selling high on another – and then execute trades automatically.
- The README and files indicate that the bot leverages smart contracts (see src/contracts) and uses Hardhat (src/hardhat.config.js) for deployment and testing.

##### File Structure and Its Implications
- Top-Level Directories:
- bin: Contains a packaged release (the zip file) indicating that there is a build or distribution artifact.
- src: This is the heart of the code, including configurations, contracts, data used, and scripts for performing various tasks.
- Under src:
- config: Includes network-specific configuration files (aurora.json, fantom.json) which likely detail connection parameters, API keys, or addresses.
- contracts: Houses smart contract code, suggesting that some operations occur on-chain.
- data: Stores runtime or static JSON data files such as token lists and route logs; useful for logging arbitrage paths, available tokens, and possibly caching market data.
- scripts: Holds several JavaScript files (e.g., trade.js, deploy.js, instadeploy.js, instatrade.js, recover.js, etc.) that serve different functionalities like managing trades, deploying contracts, managing balances, and testing.
- hardhat.config.js and package.json files denote the use of Node.js environment with Hardhat for smart contract development and deployment.

##### Components and Their Interactions
a. Main Components to Represent:
– Configuration Module: Represent the network configurations from files like aurora.json and fantom.json. This defines connection parameters to the blockchain networks.
– Smart Contract Module: Located in the “contracts” folder; this component represents on-chain logic and the interface for executing trade transactions.
– Data/Logging Module: Represented by the “data” directory files (e.g., uniqueRoutes.json, tokenList.json, fantomRouteLog.txt). These hold static data and historical logs of arbitrage trades.
– Bot Engine / Scripts: Located in “scripts”. These encapsulate various functionalities:
- Trade execution (trade.js, instatrade.js)
- Contract deployment/upgrades (deploy.js, instadeploy.js)
- Ancillary functions like checking balances, managing gas (balances.js, gas.js), and recovering funds (recover.js)
- Testing and experimental runs (test.js)
– External DEX Interfaces: Although not coded directly in separate directories, it is critical to denote external interactions with various DEX platforms (Uniswap V3, Trader Joe, etc.). Their APIs or smart contract interfaces should be highlighted.
– Hardhat Environment/Deployment Module: Through files like hardhat.config.js, this environment is responsible for compiling, testing, and deploying smart contracts.

b. Relationships and Data Flow:
– The Bot Engine interfaces with the Configuration Module to get active network details and credentials.
– It also interacts with the Smart Contract Module (through Hardhat / deployed contracts) to execute trades.
– Data flows from external DEXs must be ingested (market price feeds) and processed by the Bot Engine.
– Logging and persistent data storage happen via the Data/Logging Module (the JSON files storing routes, tokens, and logs).
– Deployment and maintenance are facilitated by scripts that use Hardhat to compile/deploy or test contracts, making these interactions central to the deployment workflow.

c. Architectural Patterns:
– Separation of Concerns: Configuration (network settings), Core Logic (arbitrage detection/trade execution), Deployment, and Data Logging are separated into distinct modules.
– Modular Script-based Architecture: Each discrete function (trade, deploy, recover, etc.) is encapsulated in its own script, making the system highly modular.
– Integration with External Blockchain Networks: The design should clearly show the two-way communication between the bot’s backend and external blockchain/Dex endpoints.

– All components need clear, descriptive labels.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External DEX Services
    subgraph "External DEX Services"
        dex1["Uniswap V3"]:::external
        dex2["Trader Joe"]:::external
        dex3["Sushiswap"]:::external
        dex4["Pancakeswap"]:::external
        dex5["dYdX (coming soon)"]:::external
    end

    %% Core Modules
    subgraph "Core Modules"
        config["Configuration Module"]:::config

        subgraph "Bot Engine / Script Modules"
            subgraph "Trade Execution"
                trade1["trade.js"]:::script
                trade2["instatrade.js"]:::script
            end
            subgraph "Contract Deployment"
                deploy1["deploy.js"]:::script
                deploy2["instadeploy.js"]:::script
            end
            subgraph "Ancillary Functions"
                anc1["balances.js"]:::script
                anc2["gas.js"]:::script
                anc3["fund.js"]:::script
                anc4["recover.js"]:::script
                anc5["test.js"]:::script
            end
        end

        contracts["Smart Contracts Module"]:::contracts
        data["Data/Logging Module"]:::data
        hardhat["Hardhat/Node.js Environment"]:::env
        binArtifact["Distribution Artifact"]:::artifact
    end

    %% External DEX Services to Bot Engine (Trade Execution)
    dex1 -->|"sendsMarketData"| trade1
    dex2 -->|"sendsMarketData"| trade1
    dex3 -->|"sendsMarketData"| trade1
    dex4 -->|"sendsMarketData"| trade1
    dex5 -->|"sendsMarketData"| trade1

    %% Configuration feeds Bot Engine
    config -->|"providesSettings"| trade1
    config -->|"providesSettings"| deploy1

    %% Internal Script Relationships within Bot Engine
    trade1 --- trade2
    deploy1 --- deploy2
    anc1 --- anc2
    anc3 --- anc4
    anc4 --- anc5

    %% Bot Engine interactions with other Modules
    trade1 -->|"executesTrade"| contracts
    deploy1 -->|"deploysSC"| contracts
    trade1 -->|"logsData"| data
    anc1 -->|"checksBalanceFor"| contracts
    trade1 -->|"triggersDeploy"| hardhat
    deploy1 -->|"triggersDeploy"| hardhat

    %% Hardhat/Node.js interactions
    hardhat -->|"compilesDeploys"| contracts
    hardhat -->|"runsTests"| anc5

    %% Distribution Artifact connection (packaging output)
    trade1 -.->|"packagedIn"| binArtifact
    deploy1 -.->|"packagedIn"| binArtifact

    %% Click Events
    click config "https://github.com/freesparrowrob/dex-arbitrage-bot/tree/main/src/config"
    click contracts "https://github.com/freesparrowrob/dex-arbitrage-bot/tree/main/src/contracts"
    click trade1 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/trade.js"
    click trade2 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/instatrade.js"
    click deploy1 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/deploy.js"
    click deploy2 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/instadeploy.js"
    click anc1 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/balances.js"
    click anc2 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/gas.js"
    click anc3 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/fund.js"
    click anc4 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/recover.js"
    click anc5 "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/scripts/test.js"
    click hardhat "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/hardhat.config.js"
    click hardhat "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/package.json"
    click hardhat "https://github.com/freesparrowrob/dex-arbitrage-bot/blob/main/src/package-lock.json"
    click data "https://github.com/freesparrowrob/dex-arbitrage-bot/tree/main/src/data"
    click binArtifact "https://github.com/freesparrowrob/dex-arbitrage-bot/tree/main/bin"

    %% Styling
    classDef external fill:#f9d6a5,stroke:#d98c3a,stroke-width:2px;
    classDef config fill:#cce5ff,stroke:#339af0,stroke-width:2px;
    classDef script fill:#e6ffcc,stroke:#82c91e,stroke-width:2px;
    classDef contracts fill:#ffe6e6,stroke:#ff8787,stroke-width:2px;
    classDef data fill:#e6f7ff,stroke:#74c0fc,stroke-width:2px;
    classDef env fill:#fce5cd,stroke:#fab005,stroke-width:2px;
    classDef artifact fill:#f5f5f5,stroke:#adb5bd,stroke-width:2px;

    class dex1,dex2,dex3,dex4,dex5 external;
    class config config;
    class trade1,trade2,deploy1,deploy2,anc1,anc2,anc3,anc4,anc5 script;
    class contracts contracts;
    class data data;
    class hardhat env;
    class binArtifact artifact;
```
