DEV Community

MY ZT
MY ZT

Posted on β€’ Edited on

Uniswap V2 notes

uniswap-v2.md
https://youtu.be/t0NZq8SmywU

contract overview

Factory

pair

add remove liquidity and swap tokens

router

is an intermediate contract between the user and the pair contracts the main job of the router contract is to safely add remove liquidity from the pair contracts and also safely swap tokens with the pair contracts.

the router contract is a useful contract if we have to do multiple hop swaps

Code repos:

v2-core

v2-periphery

swap

Image description

swapExactTokensForTokens() -> getAmountsOut()

swapTokensForExactTokens() -> getAmountsIn()

How to directly call the swap in pair contract:

//@ztmy before directly call the pair contract instead of calling the router contract:
// 1. transfer token to pair
// 2. excute UniswapV2Library.getAmountsOut to calculate the amount0Out, amount1Out
uint256 amountOut = params.isZeroForOne
    ? getAmountOut(params.amountOut, reserve1, reserve0)
    : getAmountOut(params.amountOut, reserve0, reserve1);

// transfer eth borrow form loan swap to pair1
IERC20(tokenOut).transfer(params.pair1, params.amountOut);
// git DAI form pair1
IUniswapV2Pair(params.pair1).swap({
    amount0Out: params.isZeroForOne ? amountOut : 0, // 10682631633961643998417
    amount1Out: params.isZeroForOne ? 0 : amountOut, // 0
    to: address(this),
    data: ""
});
Enter fullscreen mode Exit fullscreen mode

spot price (mid price) = current price = X0 / Y0

execution price

Cause of slippage: Market movement

createPair

1 byte=8 bits=2 hex characters

Ethereum address length = 20 bytes = 40 hexadecimal characters = a large decimal integer of more than 48 bits

to make sure that the UniswapV2Pair contract is only dependent on token0, token1 that it can be calculated from token0 and token1, for the UniswapV2Pair contract Constructor argument it passes nothing, it will call a function called initialize to set token0, token1.

add liquidity

Image description

the amount of tokens inside this AMM is described by this point.

when we're adding liquidity is that the resulting amount of token X and token y most must be along this line.

three way to define the pool value:

Image description

get pool shares after add liquidity:

Image description

Flash swap (Flash loan)

Flash swap fee derives from swap fee.
Image description

// Uniswap V2 callback
if (data.length > 0) IUniswapV2Callee(to).uniswapV2Call(msg.sender, amount0Out, amount1Out, data);
Enter fullscreen mode Exit fullscreen mode

the danger of amm's spot price as a price oricle was Spot price manipulation.

TWAP-(time weighted average price)

Image description

Cumulative price

using cumulative price to calculate the TWAP

Image description

uint256(uint224(y) * 2**112 / uint224(x)) = (y / x) * 2^112
Enter fullscreen mode Exit fullscreen mode

Fixed-Point Number is a method of representing decimal numbers using integers.used to simulate decimal arithmetic in systems (such as Solidity) that do not support floating-point arithmetic.

Arbitrage

Image description

UniswapV2Arb1

  1. execute Arbitrage between three UniswapV2Pair contracts
  2. model_1 in uniswap_v2_arb.png(borrow DAI and return DAI)
  3. The swap fee is calculated at the end.
  4. in this exercise we'll calling the router contract(swapExactTokensForTokens)

UniswapV2Arb2

  1. execute Arbitrage between two UniswapV2Pair contracts
  2. model_1 in uniswap_v2_arb.png(borrow ETH and return DAI)
  3. The swap fee calculation is included in the borrowing process(getAmountOut).
  4. in this exercise we'll need to directly call the pair contract instead of calling the router contract.

Arbitrage Optimal Amount In
Image description

Top comments (0)