Query editor
/* if you're coming from the video, I mixed up slippage and price impact - oops!This is calculating price impact, which is just one part of calculating slippage.Read more about it here :https://help.1inch.io/en/articles/4585109-what-is-price-impact-vs-price-slippage-in-defi#:~:text=Price%20impact%20is%20the%20influence,Automated%20Market%20Maker%20(AMM).*/--reserves--swap price (calculate from reserves)--simulate a swap, both ways--2000 USDC--2 ETH swapped--X in, and a quoted Y out. But the price moves along the curve xy=k before the swap occurs, so Y out will always be less than quoted.--this difference between actual and quoted is the slippage (as a %).--figure out a slippage formula that accounts for the 0.03% fee--used this https://stackoverflow.com/questions/72454359/python-uniswap-subgraph-constant-product-formula/72595515#72595515--ultimately we'll end up with a time series of daily slippage.withreserves as (SELECT * FROM(selectdate_trunc('day', s.evt_block_time) as day, s.contract_address as pair, pc.token0 as token_0_address, tk_0.symbol as token_0_symbol, pc.token1 as token_1_address, tk_1.symbol as token_1_symbol, {{amount 0 simulation}} as amount_0_simulated --token0 is USDC, {{amount 1 simulation}} as amount_1_simulated --token1 is WETH, cast(s.reserve0 as double) / pow(10, COALESCE(tk_0.decimals, 18)) as reserve0, cast(s.reserve1 as double) / pow(10, COALESCE(tk_1.decimals, 18)) as reserve1, row_number() OVER (partition by pair, date_trunc('day', s.evt_block_time) order by s.evt_block_time desc) as last_syncfrom uniswap_v2_ethereum.Pair_evt_Sync sleft join uniswap_v2_ethereum.Factory_evt_PairCreated pc on pc.pair = s.contract_addressleft join tokens.erc20 tk_0 on tk_0.contract_address = pc.token0left join tokens.erc20 tk_1 on tk_1.contract_address = pc.token1WHERE s.contract_address = {{Pair Address}}AND s.evt_block_time > now() - interval '10' month) bWHERE last_sync = 1), slippage_base as (SELECTday, token_0_address, token_0_symbol, token_1_address, token_1_symbol, reserve0, reserve1, amount_0_simulated, amount_1_simulated, p0.price as price_0, p1.price as price_1, amount_0_simulated*(p0.price/p1.price) as prices_supposed_0_to_1, amount_0_simulated*(reserve1/reserve0) as output_supposed_0_to_1, (0.997 * amount_0_simulated * reserve1) / (reserve0 + 0.997*amount_0_simulated) as output_simulated_0_to_1, amount_1_simulated*(p1.price/p0.price) as prices_supposed_1_to_0, amount_1_simulated*(reserve0/reserve1) as output_supposed_1_to_0, (0.997 * amount_1_simulated * reserve0) / (reserve1 + 0.997*amount_1_simulated) as output_simulated_1_to_0