avataryuichiro aoki

Summary

This text provides a tutorial for executing Flash Swaps on the Uniswap V3 protocol using the Hardhat forking network.

Abstract

The tutorial demonstrates how to use Flash Swaps on the Uniswap V3 protocol using the Hardhat forking network. It begins by explaining the installation and setup process, including installing Node.js and Yarn, cloning the tutorial repository, and creating a .env file with an environment variable. The tutorial then explains how to install dependencies, compile smart contracts, and execute Flash Swaps on the mainnet fork. The text also explains how the FlashSwaps.sol contract works and demonstrates how to swap DAI for UNI on the Kyber network protocol and then swap UNI for DAI on Uniswap. The tutorial concludes by showing how to pay back the borrowed amount and send a profit to the deployer address.

Bullet points

  • The tutorial explains how to execute Flash Swaps on the Uniswap V3 protocol using the Hardhat forking network.
  • The installation and setup process involves installing Node.js and Yarn, cloning the tutorial repository, and creating a .env file with an environment variable.
  • The tutorial demonstrates how to install dependencies, compile smart contracts, and execute Flash Swaps on the mainnet fork.
  • The tutorial explains how the FlashSwaps.sol contract works and demonstrates how to swap DAI for UNI on the Kyber network protocol and then swap UNI for DAI on Uniswap.
  • The tutorial concludes by showing how to pay back the borrowed amount and send a profit to the deployer address.

Tutorial of Flash Swaps of Uniswap V3

Let’s execute Flash Swaps on hardhat forking network!

Photo by Shahadat Rahman on Unsplash

Today I’m going to show you the basic tutorial of Flash Swaps of Uniswap V3. This might be helpful when you want to make a profit from arbitrage trading.

The tutorial repo is here.

Let’s Get Started😁

Installation and Setup

  1. Install Node.js & yarn, if you haven’t already.

2. Clone This Repo

Run the following command.

git clone https://github.com/yuichiroaoki/flash-swap-example.git

Quickstart

Right now this repo only works with hardhat mainnet fork. With hardhat, you can run your smart contracts on mainnet fork. For more: Harhdat Docs

  1. Setup Environment Variables

You’ll need an ALCHEMY_MAINNET_RPC_URL environment variable. You can get one from Alchemy website for free.

Then, you can create a .env file with the following.

ALCHEMY_MAINNET_RPC_URL='<your-own-alchemy-mainnet-rpc-url>'

2. Install Dependencies

Run the following command.

yarn install

3. Compile Smart Contracts

Run the following command.

yarn compile

4. Execute Flash Swaps on Mainnet Fork 🔥

Run the following command, and it will run scripts/flashswaps.ts script.

yarn flashswaps

Expected Outputs

$ yarn flashswaps
yarn run v1.22.5
$ npx hardhat run scripts/flashswaps.ts
No need to generate any newer typings.
deployer's initial balance 0
deployer's ending balance 4.860772792026915
Congrats! You earned 4.860772792026915 DAI !!
Done in 40.72s.

This is how you implement flash swaps and earn 4 DAI on mainnet fork.

Contract Explained

I will explain how scripts/flashswaps.ts script executes flash swaps. The script basically deploy FlashSwap contract at contracts/FlashSwaps.sol, and then call initFlash function to execute flash swaps.

First, main function of scripts/flashswaps.ts takes 4 arguments and first deploy FlashSwaps contract.

scripts/flashswaps.ts — deploy “FlashSwaps” contact

Calling initFlash Function

Then, it calls initFlash function from deployed contract, and this function kicks off the flash swap process.

scripts/flashswaps.ts — calling initFlash function

It borrows amount0 of token0 from token0/token1 pool with pool fee of fee1.

In this case, it lets your contract borrow 1,500 DAI from DAI/USDC pool with 0.05 % fee.

Note:

  • You have to pay back the borrowed amount with the pool fee you borrowed from, so you want to borrow from pools with the cheapest pool fee like the pool below.
DAI/USDC pool with 0.05% fee
  • since the script pass 0 as amount1 argument, the contract does not borrow any USDC.

You can get pool info here.

Now let’s take a look at FlashSwap.sol, the smart contract.

Swap DAI for UNI on kyber network protocol

After the contract borrowed 1,500 DAI, the contract execute uniswapV3FlashCallback function. First, it swaps 1,500 DAI for UNI on kyber protocol.

Swap 1,500 DAI on kyber protocal
contracts/FlashSwaps.sol — swapOnKyber function

Swap UNI for DAI on Uniswap

The contract gets 55.66 UNI and swap them for DAI on Uniswap DAI/ETH 0.05 % pool and UNI/ETH 0.05% pool.

Swap 55.66 UNI on Uniswap
contracts/FlashSwaps.sol — swapOnUniswap function

Pay Back Borrowed Amount & Send a Profit to Deployer Address

The contract pay back the amount you borrowed with the pool fee, so in this case, the contract will pay 1,500 DAI * (1 + 0.05%) = 1,500.75 DAI (line123). If the contract does not have enough amount to pay back the borrowed amount, the whole transaction will be reverted.

Pay Back Borrowed Amount & Send a Profit to Deployer Address

After that, the contract send a profit to deployer address (line130).

contracts/FlashSwaps.sol — payback function

Hope this helps. Thank you for reading😁

References

Join Coinmonks Telegram Channel and learn about crypto trading and investing

Also Read

Ethereum
Uniswap
Defi
Blockchain
Solidity
Recommended from ReadMedium