For monitoring prices and detecting potential arbitrage opportunities, you do not need to deploy the contract.
In you .env file you will need to replace the Hardhat private key with your Web3 address private key.
PRIVATE_KEY="" // Insert your Web3 address private key between the quotation marks
Inside the Config file:
- Set
isLocal
to false.
If you set isLocal
to false, and then run the bot, this will allow the bot to monitor swap events on the actual mainnet, instead of locally.
- Set
isDeployed
to false.
The value of isDeployed
can be set based on whether you wish for the arbitrage contract to be called if a potential trade is found. By default, isDeployed
is set to true for local testing. Ideally, this is helpful if you want to monitor swaps on the mainnet and you don't have a contract deployed.
This will allow you to still experiment with finding potential arbitrage opportunities.
If you are looking to test on an EVM compatible chain, you can follow these steps:
- Update your .env file.
Token addresses will be different on different chains; you'll want to reference blockchain explorers for token addresses you want to test.
- Ethereum Sepolia Testnet
- Arbitrum Sepolia Testnet
- Optimism Sepolia Testnet
- Polygon Mumbai Testnet
- Avalanche Fuji Testnet
After finding the token addresses, place them between the quotation marks inside your .env file:
ARB_FOR=""
ARB_AGAINST=""
Depending on the strategy you want to implement, you will probably need to modify some components in your .env file. Replace the current values with those that will best fit your strategy.
PRICE_DIFFERENCE=0.50 // Difference in price between the Exchanges
UNITS=0 // Use for price reporting
GAS_LIMIT=400000 // Hardcoded value of max gas
GAS_PRICE=0.00000006 // Hardcoded value of gas price in this case 60 gwei
- Update the config.json file.
Update the router and factory addresses inside the Config file. Based on the Exchanges you want to use. Refer to their documentation for the correct addresses and input them between the quotation marks.
V2_ROUTER_02_ADDRESS=""
FACTORY_ADDRESS=""
- Update the initialization.js script.
The initialization.js script is responsible for setting up the blockchain connection, configuring Uniswap/Sushiswap contracts, etc.
- Update the WebSocket RPC URL inside the Initialization script. Example for Polygon:
provider = new hre.ethers.providers.WebSocketProvider(`wss://polygon-mainnet.infura.io/v3/{process.env.INFURA_API_KEY}`)
- Update the forking URL inside Hardhat Config file. Example for Polygon:
const POLYGON_MAINNET_RPC_URL = `https://polygon-mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`;
url: POLYGON_MAINNET_RPC_URL
- Change Arbitrage.sol.
Depending on the chain you pick, you may also need to change the Flash Loan Provider.
If you are using the same liquidity provider that we used in our project, which is Balancer, Balancer currently supports the following chains:
- Ethereum Mainnet
- Arbitrum
- Optimism
- Polygon
- Gnosis
- Avalanche
Be sure to check their documentation for the latest updates regarding their contracts and deployment addresses:
- Double-Check the scripts.
Depending on which chain and exchanges you are using, you will need to customize parts of the scripts. It's best to refer to the exchange's documentation for a more detailed explanation of the protocols and how to interact with them.
If you are using Uniswap, we recommend looking into how Uniswap V2 reserves work, in addition to how getAmountsIn
and getAmountsOut
work:
As you customize parts of the scripts, you might want to refer to the Uniswap Documentation.
You can test the arbitage in your local console by using the Test script. Make sure to code the trade you want to execute; otherwise, it won't work. To launch the test, use the command:
npx hardhat test
To run the trading bot, use the command:
node bot.js
Note: Keep in mind that once the bot is running, you cannot manipulate the trading pair tokens. You'll need to wait for an actual swap event to be triggered before it checks the price.
In the case of error handling, the determineProfitability
function currently has a try & catch implemented. Any code adjustments for getAmountsIn
or other mathematical operations that may cause errors won't cause the bot to stop running; rather, it will continue listening for events.