Python UNISWAP子图 - 恒定产品公式
我正在尝试计算对交易的价格影响,并取得奇怪的结果。
我正在使用uniswap v2子图来获取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
}
}
}
"""
然后,我将此查询的结果保存到单个变量中,并为“恒定产品公式”进行相同的数学,该数学与Uniswap表示它用于池,
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)
因为我的金额仅为1美元,即每电代币支付的价格应与Token0price相匹配。但是我的结果看起来像是:
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
我要么缺少某些东西,要么以某种方式错了?任何建议/想法将不胜感激。
这是我找到常数产品公式< /a>
另外,我唯一的进口是“请求”和“熊猫” 在Google合作笔记本中运行它。
如果很难阅读,我对此表示歉意,我完全是新手。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不包括影响回报量的0.3%费用。最初,我们有“ x”和“ y”量,因此恒定的乘积
是x和y之间存在的反相关关系,因为一个增加会导致另一个
方程的图表
i.sstatic.net/izlf7.png“ rel =“ nofollow noreferrer 图表上的“ k”是恒定的
您添加了一些“ x”令牌,“ y”令牌的数量将减少,但是如果您乘以右侧paranthesse,则
:
xy
取消我正在寻找我正在寻找的。
dy
所以请留下
dy
到目前为止,我们一直在交换“ x”代币以接收“ y”令牌,我们正在计算多少“ y”令牌将从水池。当我们交换“ X”令牌时,我们还必须支付0.3%的费用,即0.3%的“ X”令牌。我们要发送交换的“ DX”金额,因为将占用此金额的0.3%,我们实际上将交换
这是我们实际上交换的“ x”令牌的总数。我们的最终公式将是
您应计算WETH购买金额的方式。
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
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
If you add some "x" token, the amount of "y" token will decrease, but "k" is constant in everwhere on the graph
If you multiply right side parantheses:
xy
cancels outI am looking for
dy
soleave
dy
aloneSo 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
this is total amount of "x" token that we are actually swapping. Our final formula will be
this is how you should calculate the weth purchase amount.
在您的计算中,您忘记了与每次交换相关的0.3%费用。使用费用时,恒定产品公式的常数实际上会改变,因此在计算输出储备时不会准确。混乱不是吗?我解释了为什么k-value在答案中会更改在这里。
相反,我找到了恒定产品公式的有用。本质上,要获得USDC输入金额的Weth的输出量,公式将是:
” ,和
wethoutputreserve = wethreserve -wethoutput
。借助这些输出储备,您可以计算新的代币价格,从而对上一笔交易的价格产生影响。让我知道这是否回答您的问题。
In your calculations you're forgetting to account for the 0.3% fee associated with each swap. When using a fee, the constant in the Constant Product Formula will actually change and so won't be accurate when calculating output reserves. Confusing isn't it? I explained why the K-value changes in my answer here.
Instead, I've found this formalization of the Constant Product Formula to be useful. Essentially to get an output amount in WETH given an input amount in USDC, the formula would be:
Then,
usdcOutputReserve = usdcReserve - usdcInput
, andwethOutputReserve = wethReserve - wethOutput
. With these output reserves you can calculate the new token prices and therefore price impact of the previous trade.Let me know if this answers your question.
您可以:
请参阅链接以获取更长的,完整的源代码。
如果您使用的是jupyter笔记本,您可能还会感兴趣,您可能会在此处感兴趣。 a>。
You can find example price impact calculations in Python here:
See the link for longer, full, source code.
If you are using Jupyter notebooks, you might be also interested the example notebooks here.