As with most open source arbitrage bots, this bot serves as a PoC only. AMM arbitrage is very competitive and this bot will not be able to compete with real on-chain bots.
Due to a lack of an order book, automated market makers (AMM) rely heavily on arbitrageurs to keep quote prices close to market prices.
Thanks to the innovation of flash swaps, you can perform arbitrage transactions without the need of a starting capital.
Also thanks to the atomic property of blockchain transactions, if there is any price movement after we submitted the transaction and before it is executed, we can revert the transaction and only pay the tranasction fees.
First, input your Infura API key and account private keys in ./config.ts
.
To deploy the contract to the mainnet, run
npx hardhat run --network mainnet scripts/deploy.ts
and update the contract address in ./config.ts
.
Finally, run the bot
ts-node src/index.ts
Let's look at the pricing model for arbitraging in constant product AMMs such as Uniswap V2 and Sushiswap on Ethereum and most AMMs on other blockchains.
This model does not apply to Uniswap V3 which is defined by concentrated liquidity.
Suppose a trader is swapping
Solving the equation gives
Let's consider an arbitrage opportunity between two AMM pools of the same pair
Pool | Liquidity |
Liquidity |
---|---|---|
Base | ||
Quote |
We will be swapping
From the above equations, the amount of
Then, we will be swapping all
We can see the two transactions can be considered as a single transaction through an equivalent pool (with both assets being
Thus, the total arbitrage profit is
Note that
Therefore,
Note that the positive root is sufficient as we have to satisfy the constraint