Skip to content

Instantly share code, notes, and snippets.

@npodonnell
Last active November 15, 2022 08:48
Show Gist options
  • Save npodonnell/dc223391821592736c7b87c6a7f9e178 to your computer and use it in GitHub Desktop.
Save npodonnell/dc223391821592736c7b87c6a7f9e178 to your computer and use it in GitHub Desktop.
UniswapV3 Notes

Uniswap v3 Notes

N. P. O'Donnell, 2022

Links

Constant Product Formula (CPF)

Uniswap v1 and v2 are based on constant product formula:

x * y = k

Where:

  • x = Number of units of asset x (a.k.a. base currency)
  • y = Number of units of asset y (a.k.a. quote currency)
  • k = A constant

Uniswap v3 Improvements over v2

Concentrated Liquidity

  • In v1 and v2, liquidity was spread across the entire price range from 0 to Inf. In v3, liquidity can be concentrated in ranges. e.g. (10.0, 1000.0). This allows buyers and sellers to get a better price (less slippage) and LPs to earn more fees.

Concentrated Liquidity

Ticks

Each possible price corresponds to a tick (implemented as 24-bit signed integer).

  • The tick index at point a is denoted ia - lower bound of position.
  • The tick index at point b is denoted ib - upper bound of position.
  • The tick index at point c is denoted ic

Glossary

Terms

  • Position: 2 points a and b on curve, between two ticks il and ih inclusive.

Acronyms

  • CPF = Constant product formula
  • LP = Liquidity pool

Variables

  • x: Number of units of asset x (a.k.a. base currency) in LP
  • y: Number of units of asset y (a.k.a. quote currency) in LP
  • k: Constant in CPF
  • L: Global liquidity
  • l: Per-position liquidity
  • a: Lower bound of a position (Asset x has lowest reserves thus highest price, asset y has highest reserves)
  • b: Upper bound of a position (Asset x has highest reserves thus lowest price, asset y has lowest reserves)
  • c: Some arbitrary point on the curve in the range (a, b)

Formulae

  • xy=k: CPF
  • L=sqrt(k): Liquidity as a function of k
  • L=sqrt(xy): Liquidity as a function of x and y
  • sqrt(P)=sqrt(y/x): SqrtPrice as a function of x and y (obviously this implies that P=y/x)
  • x=L/sqrt(P): x (base currency virtual reserves) as a function of liquidity and SqrtPrice
  • y=L*sqrt(P): y (quote currency virtual reserves) as a function of liquidity and SqrtPrice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment