Search code examples
pythonblockchainweb3pyuniswapthegraph

Python Uniswap Subgraph - Constant product formula


I'm trying to calculate the price impact on trades and am getting strange results.

I am using uniswap v2 subgraph to get current data for WETH/USDC.

def loadUni2():
    query = """

{
    pairs (
    first: 10
    orderBy: volumeUSD
    orderDirection:desc
  
  ){
    id
    reserve0
    token0Price
    token0 {
      id
      symbol
      decimals
    }
    token1Price
    reserve1
    token1{
      id
      symbol
      decimals
    } 
  }
}

"""

I then save the results of this query into individual variables and do the same math for the "constant product formula" that uniswap says it uses for its pools

pair = pairs[0]


#sort dataframe by lowest price 
low = pair.sort_values(by='token0Price', ascending=True)

quoteReserve = low['reserve0'].values[0]   #USDC Tokens in pair verified by checking info.uniswap.org
baseReserve = low['reserve1'].values[0]    #WETH tokens in pair verified by checking info.uniswap.org
token0Price = low['token0Price'].values[0] 
token1Price = low['token1Price'].values[0]

#Buy Low
amount = 1   # purchase amount in USD
constant = quoteReserve * baseReserve

newReserve = (quoteReserve + amount)

output = constant / newReserve

wethPurchaseAmount = baseReserve - output

pricePerToken = amount / wethPurchaseAmount

if (newReserve * output) == constant:
  check = True

print(f'Token0Price before trade : {token0Price}')
print(f'Token1Price before trade: {token1Price}')
print(f'Quote Reserves: {quoteReserve}')
print(f'Base Reserves: {baseReserve}')
print(f'Constant: {constant}')
print(f'New Reserve: {newReserve}')
print(f'Output: {output}')
print(f'WETH Purchased Amount: {wethPurchaseAmount}')
print(f'Price paid Per Token : {pricePerToken}')
print(check)

Since my amount is only $1 the price paid per token should match the token0Price. But my results look like:


Token0Price : 1942.4506384054528
Token1Price: 0.0005148135969215
Quote Reserves: 121784650.548786
Base Reserves: 105869.64875708237
Constant: 12893298177603.992
New Reserve: 121784651.548786
Output: 105869.64788776389
WETH Purchased Amount: 0.0008693184790899977
Price Per Token: 1150.3264040203076
True

I'm either missing something or somehow my math is wrong? Any suggestions/ideas would be greatly appreciated.

Here's the link for where I found an example of the Constant Product Formula

Also, the only imports I have are 'requests' and 'pandas' Running it in a google collab notebook.

I apologize in advance if this is hard to read, I'm completely new to this.


Solution

  • you are not including the 0.3% percent fee which affects the amount of return value. initially we have "x" and "y" amount so constant product is

     k = x * y
    

    There is an inverse relationship between x and y because increase in one will lead to decrease on other

    this is our graph for this equation

    enter image description here

    If you add some "x" token, the amount of "y" token will decrease, but "k" is constant in everwhere on the graph

      k  = (x+dx) * (y-dy)
      xy = (x+dx) * (y-dy)
    

    If you multiply right side parantheses:

      xy = xy - xdy + ydx -dydx
    

    xy cancels out

     0=ydx - xdy - dydx
    

    I am looking for dy so

     xdy + dydx = ydx
     dy(x+dx)=ydx
    

    leave dy alone

     dy = ydx / (x+dx)
    

    So far we have been swapping "x" token to receive "y" token and we are calculating how much "y" tokens will be decreased from the pool. When we swap "x" token we also have to pay 0.3% fee which is 0.3% "x" token. "dx" here amount that we are sending to swap, since 0.3% of this amount will be taken, we will be actually swapping

    (1 - 0.3%) dx
    (1 - (0.3/100)) dx
    (1 - (0.003))dx
    0.997 dx
    

    this is total amount of "x" token that we are actually swapping. Our final formula will be

     dy = y *(0.997dx) / (x + 0.997dx)
    

    this is how you should calculate the weth purchase amount.