How freesparrowrob/dex-arbitrage-bot works
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.