Skip to content

āš”šŸ¦„Arbitrage between USDC/ETH 0.3% fee pool and USDC/ETH 0.05% fee pool

Notifications You must be signed in to change notification settings

Aboudoc/UniswapV3-FlashSwap-Arbitrage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Ā 

History

24 Commits
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

Arbitrage Profit for Constant Product AMM

Uniswap V3
Explore the docs Ā»

View Demo Ā· Report Bug Ā· Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Simple Arbitrage Contract
  5. Arbitrage Profit For CPAMM
  6. Test Arbitrage
  7. Forking mainnet
  8. Note
  9. Roadmap
  10. Contributing
  11. License
  12. Contact
  13. Acknowledgments

About The Project

Arbitrage between USDC/ETH 0.3% fee pool and USDC/ETH 0.05% fee pool

(back to top)

Built With

  • Hardhat
  • Ethers

(back to top)

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

  • npm

    npm init -y
  • hardhat

    npm install --save-dev hardhat

    run:

    npx hardhat

    verify:

    npx hardhat verify --network goerli "contract address" "pair address"

Installation

  1. Clone the repo

    git clone https://github.com/Aboudoc/Uniswap-V3-Arbitrage.git
  2. Install NPM packages

    npm install
  3. Dependencies

    npm add @uniswap/v3-periphery @uniswap/v3-core
    npm add --save-dev dotenv

(back to top)

Usage

If you need testnet funds, use the Alchemy testnet faucet.

This project shows a simple arbitrage strategy

You can find a deep overview of Uniswap v3 in this repo

(back to top)

Simple Arbitrage Contract

Test

In this arbitrage example we will:

  1. Borrow USDC from one pool

  2. Swap USDC back to WETH in another pool

  3. Repay first pool with WETH

    If the amount of WETH bought back in step 2 is greater than the amount repaid in step 3, then there is profit from the the arbitrage. Otherwise there was a loss.

Function flashSwap

This function will start the arbitrage by borrowing USDC from pool0. pool0 will send USDC to this contract and then call uniswapV3SwapCallback

Inside uniswapV3Callback, we will need to send wethAmountIn amount of WETH to pool0.

  1. Encode data to be later decoded inside uniswapV3SwapCallback. The data to encode are msg.sender, pool0 and fee1.

  2. Initiate the arbitrage by calling IUniswapV3Pool.swap on pool0. Below are the inputs to pass.

    recipient: Address to receive output token

    zeroForOne: Direction of the swap, true for token0 to token1

    amountSpecified: Amount to swap

    sqrtPriceLimitX96: Limit for the change in price

    data: Data to be passed to uniswapV3SwapCallback

Function _flash

Swap tokenIn for tokenOut by calling router.exactInputSingle

  1. Approve amountIn
  2. Prepare params
  3. Call exactInputSingle on router and store amountOut

This function returns amountOut

Function uniswapV3SwapCallback

This function is called by pool0 immediately after we call IUniswapV3Pool.swap.

amount0 is amount of USDC we borrowed. This variable will be a negative number since USDC is taken out of the pool.

amount1 is amount of WETH we owe to pool0.

data is the data we've encoded inside the function flashSwap.

  1. Decode data
  2. msg.sendermust be pool0
  3. Store amount0 and amount1 as usdcAmountOut and wethAmountIn
  4. Call _swap to swap USDC for WETH and store wethAmountOut
  5. Repay WETH back to pool0. If there is profit, transfer to caller. Otherwise transfer the loss from caller.

(back to top)

Arbitrage Profit For CPAMM

let's see some equations related to the arbitrage profit for a constant product AMM

Test

First, assume that a CEX has infinite liquidity

The AMM has token X and token Y, and the price P = Y / X

On the CEX the price is lower, p - dp

An arbitrageur can make a profit: The difference in the token Y that he sold on the CEX and then got back from the AMM is the profit A

Test

The green line represents the current price of the AMM. There are X0 amount of token X and Y0 amount of token Y => P = Y0 / X0

Test

On the CEX (red line), the price is quoted as P - dp

Test

The trade in purple will lower the price for P to P - dp. But before the arbitrageur can execute this trade, he first has to get dx amount of token X.

Test

To do that, the arbitrageur will go to the CEX and then sell dy0 amount of token Y to get dx amount of token X. Next he will go to AMM and sell this exact amount of token X to get back in return dy1 amount of token Y.

=> On the AMM the price has shifted from P to P - dp.

Test

We can also write this equation in terms of price change and the amount of token X that was sold (dx).

To do that we'll need to get two prices, the price of the CEX and the price of the AMM

Take the difference, multiply it by dx and this is the arbitrage profit

Let's start with the AMM price

Test

Let's find the blue slope:

Test

Let's also rewrite dy0 in terms of price and dx

Test

We are now ready to rewrite the arbitrage profit

Test

In some cases, we can further sumplify this equation:

Test

Finally:

Test

(back to top)

Test Arbitrage

Test

(back to top)

Forking mainnet

hardhat.config.js

  networks: {
        hardhat: {
          forking: {
            url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
       },
     },
  }

Note: Replace the ${} component of the URL with your personal Alchemy API key.

npx hardhat test test/flashSwap.test.js

(back to top)

Note

Further reading

You can find Uniswap pools referenced below. Select a pool with the highest TVL

Uniswap V3 Pool Infos

You can find official Uniswap documentation below:

Single Hop Swap

Multi Hop Swap

Sources

Smart Contract Engineer

(back to top)

Roadmap

  • [-] Maths
  • [-] Test on mainnet fork
  • Deploy on mainnet?
  • Further reading

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Reda Aboutika - @twitter - reda.aboutika@gmail.com

Project Link: https://github.com/Aboudoc/Uniswap-V3-Arbitrage.git

(back to top)

Acknowledgments

(back to top)

About

āš”šŸ¦„Arbitrage between USDC/ETH 0.3% fee pool and USDC/ETH 0.05% fee pool

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published