- Used 0x Api to fetch best swap rate available as it is a DEX aggregator.
- Used DODO Flashloan to get the capital required for trade.
- Used Solidity for writing smart contract and Hardhat to compile and deploy the contract on Polygon.
- Used Ethers.js to interact with the smart contract after detecting an arbitrage opportunity using the 0x api.
git clone [email protected]:proro485/zrx-arbitrage-bot.git
cd zrx-arbitrage-bot
yarn install
- Create a .env file and add these variables
POLYGON_RPC_URL = <your_rpc_url>
,PRIVATE_KEY = <your_private_key>
. Note : If you don't have an RPC url then create one using this referal link of Alchemy. Also don't forget to add "0x" infront of your private key. - Now you can open a terminal in this directory and run this command to deploy:
npx hardhat run scripts/deploy.js --network polygon
(You will get the contract address on the console, you can check the transaction here PolygonScan
npx hardhat run scripts/dualArb.js --network polygon
(this script looks for arbitrage with only two assets for eg. X -> Y -> X)npx hardhat run scripts/triArb.js --network polygon
(this script looks for triangular arbitrage for eg. X -> Y -> Z -> X)
- To change the pairs of tokens to find arbitrage in, you can go to the
constants/polygon/tokens.js
there you would find an array of namedualTokensList
andtriTokensList
you can uncomment the pair of tokens you would like to get searched for arbitrage. - If you want to add new tokens, then add them to
constants/polygon/addresses.js
within the array with nameKNOWN_TOKENS
and also to theconstants/polygon/tokens.js
in the tokens object. Now after adding the token info at these two locations you can add the order of trading in thedualTokens or triTokensList
in the same file. - If you want to change the amount used for arbitraging, then go the the
utils/utilities.js
file and change the amount in thetoBorrow
function it is currently set to 10000 USD and equivalent amounts for WMATIC, WETH, WBTC. Also after changing this you might want to update the profit percentage so change that inside thegetBPS
function (1 BPS is 0.01%, 10 BPS is 0.1%, 100BPS is 1%), you have to change the value of the amount which is passed as a parameter, you can also change the BPS value returned for that amount to any value you like. - Also you can increase/decrease the gas price by changing the gasPrice variable in the
scripts/dualArb.js
andscripts/triArb.js
. Just search for gasPrice inside the file.