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.