A small project to try arbitrage on dex between two pairs (same token) e.g. busd-usdt on pancake arbitrage to busd-usdt on biswap
- file
botarb.solfor smart contract on blockchain - file
calAMM.jsfor shows the logic of the bot's calculation method - file
createDataJson.jsfor fetching pairs in factory router on dex and generating json file - file
createSamePair.jsfor selecting only same pairs between 2 dex - file
tradeArbOnBsc.jsfor connects the smart contract and trade arbitrage when it detects the condition of the arbitrage
- go to file
calAMM.js
node calAMM.jsTest([94781884.284722067962798239, 94699896.246303358765962232], [0.008911144541043 , 991.9861803364])In the above example
DATA INPUT
paris busd-usdt on pancakeswap
94781884.284722067962798239 busd
94699896.246303358765962232 usdt
paris busd-usdt on twindex
991.9861803364 busd
0.008911144541043 usdt1. The bot will check the ratio to make arbitrage 1->2 or 2->1.
2. Then calculate whether to do arbitrage from liquidity pool more to less or less to more and calculate the numbers that will make the pool balanced out of the x*y = k equation.
3. Then, when get the numbers for arbitrage will be performed and the result will be displayed.
1.LP MORE -> LP LESS because busd-usdt on pancakeswap have more liquidity than busd-usdt on twindex
2.Find and get numbers for arbitrage ->In pancakeswap sell amount busd 2.976692320410171 to get usdt 2.966682040880783
ratio in the pool will change from 0.9991349819742724 to to 0.9991349192956358
3.In twindex sell amount usdt 2.966682040880783 to get busd 989.0080109808019
ratio in the pool will change from 0.000008983133754969321 to 0.999134981974273
4.The profit you will get is busd(get after arbitrage) busd 989.0080109808019 - busd(initial cost) busd 2.976692320410171 = busd 986.0313186603918
5.Then you can see that the pool ratio between busd-usdt on pancakeswap and busd-usdt on twindex is almost close together that means the arbitrage is done
1. If you want to create your own data file (if not skip this Because I also give you dataSamePair.json)
- go to file
createDataJson.js
| Parameter | Type | Description |
|---|---|---|
FileName |
string |
name file |
Length |
number |
for pairs data length from factory router on dex |
ADDRESS_ROUTER |
string |
router address |
const FileName = 'BakerySwap'
const Length = 100
const ADDRESS_ROUTER = '0xCDe540d7eAFE93aC5fE6233Bee57E1270D3E330F'In the above example. i need length paris 100 and router dex BakerySwap
you will get file dataBiswapPair.json
node createDataJson.js- go to file
createSamePair.js
| Parameter | Type | Description |
|---|---|---|
locationFile1 |
string |
name file1 for combined |
locationFile2 |
string |
name file2 for combined |
const locationFile1 = './dataApeSwapPair.json'
const locationFile2 = './dataBakerySwapPair.json'At this step, you must have data json at least two router pairs.
In the above example. I have dataApeSwapPair.json and dataBakerySwapPair.json
you will get file dataSamePair.json The resulting file is filter only same pairs from 2 dex routers for use in arbitrage
node createSamePair.jsAt this step, you must deploy smart contract botarb.sol on network Bsc.
and send token which you want arbitrage to smartcontract
let addressMain = [
'0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
'0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56',
'0xE02dF9e3e622DeBdD69fb838bB799E3F168902c5'
]In the above example. I use wbnb / busd / bake . You can edit and add address token you want if the dataSamePair.json uses that token
At this step, you must add .env file in your folder and input your information.
INFURA_ID=
PRIVATE_KEY=
ADDRESS_CONTRACT= node tradeArbOnBsc.jsIf you get something like this means bot it works and it will run loop data in file dataSamePair.json until you stop it
This picture is when the bot does arbitrage it will show information including Profit and if it have Profit it will execute tx immediately and you will get txhash back


