Tutorial of Flash Swaps of Uniswap V3
Let’s execute Flash Swaps on hardhat forking network!
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
2. Clone This Repo
Run the following command.
git clone https://github.com/yuichiroaoki/flash-swap-example.gitQuickstart
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
- 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 install3. Compile Smart Contracts
Run the following command.
yarn compile4. Execute Flash Swaps on Mainnet Fork 🔥
Run the following command, and it will run scripts/flashswaps.ts script.
yarn flashswapsExpected 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.

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

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.

- 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 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.


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.

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

Hope this helps. Thank you for reading😁
References
Join Coinmonks Telegram Channel and learn about crypto trading and investing




