Skip to main content
Try out the RareSkills ZK AI Tutor!
Uniswap V2 Book by RareSkills

Checklist for building a Uniswap V2 clone

Last updated on Nov 12, 2026

It’s very educational to rebuild Uniswap v2 from scratch using modern Solidity (or Huff if you really want to do it in hard mode). Here are some pointers and suggestions for doing so.

  • Use an updated version of Solidity. Be aware that this will lead to syntactic changes.
  • Replace the fixed point numbers with custom data types.
  • Use the Solady ERC20 for the ERC20 to save gas.
  • Don’t use Uniswap V2’s current reentrancy protection, it isn’t gas efficient anymore, use OpenZeppelin’s or some other alternative.
  • Be careful to add unchecked in the price oracle, it expects to overflow.
  • Don’t use safeMath with Solidity 0.8.0 or higher.
  • If you are not implementing the router separately, safety checks for slippage need to be built into the contract. EOAs cannot send tokens as part of the transaction.
  • Be sure to put the reentrancy lock in the right places. Is Uniswap V2 subject to read-only reentrancy? Why or why not?
  • The factory contract can be simplified without assembly because of Solidity updates.
  • Watch out for fee-on-transfer tokens or re-basing tokens.
  • The square root function can be done more efficiently with the Solady library, but be sure you are rounding in the correct direction.
  • Uniswap hardcodes the fee with magic numbers, this isn’t an ideal way to write code.
  • Don’t forget to follow the professional Solidity style guide (which Uniswap V2 does not follow).
  • The way the factory tracks pairs isn’t gas efficient, try to improve it.
  • Some storage variables in the original implementation could be made to be immutable (immutable variables were not available at the time Uniswap V2 was launched).
  • Custom errors will lead to a cheaper deployment cost than require statements (generally)
  • When burning the initial supply, make sure that the totalSupply does not go down to zero. Otherwise, the defense against the first deposit attack will not work. Some implementations of burn reduce the total supply rather than locking up the funds as Uniswap intends.
  • Be careful to burn, mint, and update reserves in the correct order when computing shares.
  • Uniswap V2’s _safeTransfer is subject to the memory expansion attack (very unlikely, but still should be guarded against). Since only one bool will be read, it’s best to returndatacopy only one word. Try to avoid getting into the habit of reading the entire return data from other contracts.
  • A good invariant test is liquidity should never go down if burn is not called.
  • Don’t use the balance variables or pool balance as an oracle, as these are vulnerable to flash loan attacks.
  • Remember to always round in favor of the pool when doing trades or minting or burning shares.
  • Don’t forget to write unit tests.
  • Give our book on Solidity gas optimization a read for inspiration about how to improve performance (after you finish your second or third draft of the Uniswap clone and have written tests). If you want to do this in hard mode, there are several opportunities to improve the gas using assembly without substantial sacrifices in readability.
  • Try out Damn Vulnerable Defi Puppet V2. It should be easy for you to solve at this point.

Learn more with RareSkills

This material is part of our advanced Solidity Bootcamp. Please see the program to learn more.

Originally Published November 1, 2023

Related posts

8 min read

Breaking Down the Uniswap V2 Swap Function

Uniswap V2’s swap function is cleverly designed, but many devs find its logic counterintuitive the first time they encounter it.

Last updated onNov 12, 2026
Read Article

6 min read

How Uniswap V2 computes the mintFee

Uniswap V2 was designed to collect 1/6th of the swap fees to the protocol.

Last updated onNov 12, 2026
Read Article

9 min read

Checklist for Technical Writing

Are fluff transitions removed? (”It is important to note, ”Why did they do this?”, “Here’s how we can solve this problem.”) Sometimes, they are necessary…

Last updated onNov 12, 2026
Read Article

8 min read

Uniswap V2 Mint and Burn Functions Explained

The lifecycle of Uniswap V2 is someone mints LP tokens (supplies liquidity, i.e. tokens to the pool) for the first time, then a second depositor mints…

Last updated onNov 12, 2026
Read Article