Day 10 (12 Days of Dune): Calculating price impact and total liquidity for USDC/WETH in Uniswap v2
And we've finally gotten to simulating swaps - the lower the price impact, the better!
We’ll be answering questions and covering more beginner-focused concepts during office hours at 2pm EST, so join us in the #12-days-of-dune discord channel. You’ll need to earn the course NFT and connect to the Dune guild.
Web3 Data Degens is a reader-supported publication. Posts are free, but I greatly appreciate your paid support as well :)
Welcome to day 10!
You can find the full explanation below (premiering with a live watch party at 9 am EST today).
Here are the main concepts:
If you take the reserves of token 1 and divide it by the reserves of token 0, you get the price of token 1 (with respect to token 0). Assuming it’s WETH/USDC, you get the price of WETH in dollar terms. However this swap price doesn’t capture the liquidity depth of the pair. When you make a swap, the actual price moves along the x*y=k curve such that the actual amount of tokens you get out of a swap will always be lower than what the “quoted” swap price is (all other things constant). Taking the difference between actual and quoted gives you the price impact.
Price impact is commonly confused with slippage (as even we made this mistake). Slippage is the further decrease (or increase) in price due to a MEV sandwich. You can read more about the difference and how protocols control for this here.
We use the formula found from various forums to calculate price impact given you some input amount and the current reserves, accounting for the pool fees.
Price impact will change based on how much you set as the input amount. We choose custom amounts to simulate in parameters of 2000 USDC and 2 ETH. There are many other ways to choose amounts to simulate more programmatically, which we will revisit on day 12.
Altogether you should get the charts below. The query is on Dune here.
Remember, if you found this question hard make sure to come to our office hours (2 PM EST) in our discord! Details are at the top of the email.
Tomorrow, we’ll be covering the question:
What is the volatility of liquidity and prices for USDC/WETH? How can the two be compared to understand LP behaviors?
As always, feel free to reach out to me here or on Twitter with any questions you might have.