Uniswap V2: Calculating the Settlement Price of an AMM Swap

Uniswap V2: Calculating the Settlement Price of an AMM Swap This article explains how to determine the price settlement of a trading pair in an Automated Market Maker (AMM). It answers the question of “How many token X can be swapped for token Y from the AMM?”. The swap() function on Uniswap V2 requires you […]

Uniswap V2 Architecture: An Introduction to Automated Market Makers

Uniswap V2 Architecture: An Introduction to Automated Market Makers Uniswap is a DeFi app that enables traders to swap one token for another in a trustless manner. It was one of the early automated market makers for trading (though not the first). Automated market makers are an alternative to an order book, which the reader […]

How Uniswap V2 computes the mintFee

How Uniswap V2 computes the mintFee Uniswap V2 was designed to collect 1/6th of the swap fees to the protocol. Since a swap fee is 0.3%, 1/6th of that is 0.05%, so 0.05% of every trade would go to the protocol. Although this feature was never actually activated, we discuss this feature anyway since some […]

Uniswap v2 router code walkthrough

Uniswap v2 router code walkthrough The Router contracts provide a user-facing smart contract for safely minting and burning LP tokens (adding and removing liquidity) safely swapping pair tokens They add the ability to swap Ether by integrating with the wrapped Ether (WETH) ERC20 contract. They add the slippage related safety checks omitted from the core […]

UniswapV2Library Code Walkthrough

UniswapV2Library Code Walkthrough UniswapV2Library The Uniswap V2 Library simplifies some interactions with pair contracts and is used heavily by the Router contracts. It contains eight functions that are not state-changing,. They are also handy for integrating Uniswap V2 from a smart contract. getAmountOut() and getAmountIn() If we want to predict the amount of token y […]

How the TWAP Oracle in Uniswap v2 Works

How the TWAP Oracle in Uniswap v2 Works What exactly is “price” in Uniswap? Suppose we have 1 Ether and 2,000 USDC in a pool. This implies that the price of Ether is 2,000 USDC. Specifically, the price of Ether is 2,000 USDC / 1 Ether (ignoring decimals). More generally, the price of an asset, […]

Checklist for building a Uniswap V2 clone

Checklist for building a Uniswap V2 clone 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. […]

Uniswap V2 Mint and Burn Functions Explained

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 liquidity, swaps happen, then eventually the liquidity providers burn their LP tokens to redeem pool tokens. It turns out to be easier […]

Breaking Down the Uniswap V2 Swap Function

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. This article explains how it works in depth. Here is the code reproduced below: Admittedly, this is a wall of code, but let’s break it down. On line […]