anton-karlovskiy / amm-arbitrageur

An arbitrage bot between Uniswap AMMs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Note: This repo is not under maintaining. 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 demonstration. It's not robust enough to be competitive 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.

AMM Arbitrageur

An arbitrageur contract can be used to arbitrage between Uniswap V2 like AMMs. For Chinese: 中文说明

The rationale

There are a lot of AMMs on Ethereum and other blockchains that support EVM. Many of these AMMs are UniswapV2 fork projects or have the same interface with Uniswap V2. A list of these AMMs:

  • Uniswap V2 (Ethereum)
  • SushiSwap (Ethereum)
  • PancakeSwap (BSC)
  • MDEX (BSC/heco) ...

We can do arbitrage between these AMMs once the prices of the 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 when 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 exist on multiple AMMs on Ethereum (or other EVM compatible blockchains such as BSC).

  • We call WETH as Base token. It can be any token with actual value such as USDT/USDC/DAI/BUSD...
  • We call TokenX as Quote token. It can be any token even if it's a fake token without value. Quote tokens won't be reserved after arbitrage's been done.
  • After arbitrage, only the base tokens are reserved. So our profit is denominate in base token.
  • If two tokens in a pair can both be considered as base token. Either one can be reserved after arbitrage.

The arbitrage can be done by using flashswap of Uniswap V2:

  • Suppose pair0 and pair1 are two pairs of the same two tokens on different AMMs. Once the price diverges, we can do arbitrage.

  • We call the FlashBot contract to start arbitrage.

  • The contract calculates the price denominated in quote token. Suppose the price of quote token in Pair0 is lower:

    1. By using flashswap, the contract first borrows some quote tokens from Pair0, the amount is x. The contract needs to repay the debt to Pair0. The debt can be denominated in base token. This is a functionality of Uniswap V2.
    2. Sell all the borrowed quote tokens on Pair1. The contract gets base tokens of amount y2.
    3. Repay the debt to Pair0 in base token of amount y1.
    4. The contract gets profit of y2 - y1.

The point of the process is to calculate how much of the amount x so we can get as much profit as possible.

Suppose the initial states of Pair0 and Pair1 are as the following:

Pair0 Pair1
Base Token amount a1 a2
Quote Token amount b1 b2

So we get:

The borrowed Quote Token amount is the same, 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 gets a maximum value. First we need to get the derivative of the function:

When the derivative of the 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:

Let:

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.

Deploy the contract

  1. Edit the network config in hardhat.config.ts. (Currently it is BSC in the repo, you can also use Ethereum mainnet)

  2. Copy the secret sample config:

$ cp .secret.ts.sample .secret.ts
  1. Edit the private and address fields in the above config.

  2. Then run the script to deploy. By default, it deploys to BSC. If you wanna deploy 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

For example,

$ npx hardhat --network bscTestnet run scripts/deploy.ts

Bot implementation

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 needs 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 be left in the contract address.

The contract owner can call withdraw() to withdraw the profit.

There already implemented a bot in typescript, to run it:

$ yarn run bot

Available AMMs on BSC

Run unit tests

$ hardhat test

FAQ

Too much math, what the hell is this contract for?

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.

How do I know the correctness of the contract?

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.

But I didn't make any profit by running your bot.

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.

How can I change the token pairs the bot is monitoring?

First, the bot uses bscBaseTokens, bscQuoteTokens, and bscDexes in tokens.ts to automatically get all possible token pairs and saves them into pairs-bsc.json. So it can reuse the json file next time the bot launches.

If you want some other pairs. You can delete the pairs-bsc.json and edit the three variables above. Rerun the bot so it uses the new pairs. You can check the new pairs in pairs-bsc.json.

Any suggestions to be competitive?

  • Lower the network latency, you can use your own node.
  • Higher the gas price, make sure your transaction gets handled quickly enough to take the profit. This is just like a competition between bots if they find a profitable chance at the same time.
  • Monitoring lesser tokens, the more you're monitoring, the lesser frequently the bot is looping. You can run multiple bots to monitor separate token pairs.
  • Go to some other networks such as FTM/Matic/..., there may be lesser bots running on them.
  • Do some other works such as liquidation, arbitrage in balancer/curve/0x... You need to implement these features by yourself.

Why terminal stuck when running "npx hardhat compile"?

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 HTTP_PROXY or HTTPS_PROXY in your terminal. According to this issue, the hardhat version 2.4.0 and the later versions support HTTP_PROXY or HTTPS_PROXY. So you need to change the hardhat version from 2.1.2 to 2.4.0 or later in package.json.

Error occurs when running "npx hardhat run --network xxx bot/index.ts"

The detailed error is TSError: x Unable to compile TypeScript. bot/index.ts:63:13 - error TS2571: Object is of type 'unknown'. Please make sure your TypeScript version is ^4.2.4. You may meet this error if your TypeScript version is above 4.4.x.

Error "cannot estimate gas; transaction may fail or may require manual gas limit"

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 that are deprecated so you need to modify the pairs-bsc.json.

About

An arbitrage bot between Uniswap AMMs

License:Do What The F*ck You Want To Public License


Languages

Language:Solidity 53.9%Language:TypeScript 46.1%