Note: This repo is not under maintenning. If you encounter problems when running. Please figure it out by yourself.
This repo is written only for POC. The bot is implemented simply for demostration. It's not robust enough to be competitve with other arb bots that are running in the network. If you want a production-ready arb bot, then you might need to implement a bot by yourself.
If you have any question, please read the issues first. You may find your answer there.
A flashbot contract can be used to argitrage between Uniswap V2 like AMMs.
There are a lot of AMMs on Ethereum and other blockchains that support EVM. Many of these AMMs are UniswapV2 fork project or have same interface with Uniswap V2. A list of these AMMs:
We can do arbitrage between these AMMs once the prices of same token pair diverges on different AMMs. We can encapsulate arbitrage transactions in one EVM transaction so that we won't lose any money even the price moves before we send transaction.
Suppose we'd like to do arbitrage transactions on token pair TokenX/WETH. The TokenX/WETH pair must exists on multiple AMMs on Ethereum(or other EVM compatible blockchains such as BSC).
The arbitrage can be done by using
flashswap of Uniswap V2:
FlashBotcontract to start arbitrage
The point of the process is to calculate how much of the amount x so we can get as much profit as possible.
Supoose the initial state of Pair0 and Pair1 are as follows:
|Base Token amount||a1||a2|
|Quote Token amount||b1||b2|
So we get：
The amount borrowed Quote Token are some, so
Delta b1 =
Delta b2, let
x = \Delta b, then the profit as a function of x is:
We wanna calculate the x value when the function get a maximum value. First we need to get the derivative of function:
When the derivative function is 0, the function has a maximum/minimum value, and we can set some conditions to ignore the solution at the minimum. It is possible to solve
The previous equation is reduced to a general quadratic equation:
We can get the solution:
The solution x is the amount we need to borrow from Pair0.
Edit network config in
hardhat.config.ts.(Currently it is BSC in the repo, you alse can use Ethereum mainnet)
Copy the secret sample config：
$ cp .secret.ts.sample .secret.ts
Edit the private and address field in above config.
Then run the script to deploy. By default, it deploys to BSC. If you wanna dpeloy to other network, you may need to change the network settings in
hardhat.config.ts. You also need to change the WETH or other token address in the
deploy.ts, it's WBNB address by default.
$ hardhart --network XXX run scripts/deploy.ts
$ npx hardhat --network bscTestnet run scripts/deploy.ts
The contract has a function
getProfit(address pool1, address pool2), which can be used to calculate the maximum profit between two pairs(denominated in base token).
The bot need to call
getProfit() to get the possible profit between token pairs. Once it is profitable, bot calls
flashArbitrage(pool1, pool2) to do the arbitrage. The profit will leaves in the contract address.
Contract owner can call
withdraw() to withdraw the profit.
There already implemented a bot in typescript, to run it:
$ yarn run bot
$ hardhat test
To be simple, it moves the prices between different AMMs to the same level. You'll get profit by doing that. This contract helps you to get the maximum profit. And it uses flashswap so you only need little money(just some gas fees) to run it.
By default, the tests use a forking network of the BSC mainnet(thanks to the powerful hardhat tool). The tests in
SwapTest.ts demonstrate that the contract works correctly for arbitrage. You can check it by yourself.
Well, there are too many bots running in the wild, especially in ETH and BSC. The bot code in this repo is too simple to be competitive. You can't expect just running my code and earning a bunch of money. You need to find out some strategies to make your own money.
At the first time, the bot uses
tokens.ts to automatically get all possible token pairs and saves them into
bsc-pairs.json. So it can reuse the json file next time the bot launches.
If you want some other pairs. You can delete the
bsc-pairs.json and edit the three variables above. Rerun the bot so it uses the new pairs. You can check the new pairs in
If you are running hardhat behind a proxy, maybe you will encounter the error like
HH502: Couldn't download compiler versions list. Please check your connection when running
npx hardhat compile. In order to go through this error, you need to set
HTTPS_PROXY in your terminal. According to this issue, the hardhat version 2.4.0 and the later version has supported
HTTPS_PROXY. So you need to change the hardhat version from
2.4.0 or later in package.json.
The detailed error is
TSError: x Unable to compile TypeScript. bot/index.ts:63:13 - error TS2571: Object is of type 'unknown'. Please make run your TypeScript version is ^4.2.4. You may meet this error if your TypeScipt version is above 4.4.x.
If you encounter this error, please don't be surprised. First of all, this bot hasn't tested well in bscTestnet. So if you want to run it well you would like better deploy it in bscMainnet. Secondly, since there are a lot of token pairs are deprecated so you need to modify the