uniswapRouter.getAmountsOut 返回的代币 B 值小于反向中的值

问题描述 投票:0回答:1

我有下一个代码来熟悉uniswap机制:

  • 创建配对合约
  • 获取储备
  • 调用
    getAmountsOut
    并提供所有UNI代币储备
  const pairAddress = await _V2Factory.getPair(uniToken, wethToken);
  const pairContract = new ethers.Contract(
    pairAddress,
    IUniswapV2Pair.abi,
    _provider
  );
  const tmpReserves = await pairContract.getReserves();
  const reserves = [reserves.reserve0, reserves.reserve1];
  console.log("reserves", reserves);

  const output = await router.getAmountsOut(reserves[0], [
    uniToken.address,
    wethToken.address,
  ]);
  console.log("output", output);

输出是:

  • 储备:
    [ 36851089197799104779745113323n, 281312509765248795074n ]
  • 输出:
    [ 36851089197799104779745113323n, 140444953548298972803n ]

问题是为什么我没有收到

output
数组的第二个元素等于
281312509765248795074n
- 全部
WETH
流动性成对但只是部分流动性等于
140444953548298972803n
值?

我正在运行本地安全帽节点,第 3 方操作不应影响结果。我也在主网上尝试过,显然结果是一样的。

也尝试从

getAmountsOut
切换到
getAmountOut
,但其行为方式相同:

const output2 = await router.getAmountOut(
    reserves[0],
    reserves[0],
    reserves[1]
  );
  console.log("output2", output2);

输出:

143271499822164119007n

ethereum web3js hardhat uniswap
1个回答
0
投票

Uniswap 和其他恒定产品自动化做市商 (AMM) 对每个池使用以下不变量:

其中

x
y
是池中代币的数量,
k
是常数。

函数

getAmountsOut
模拟池中的交易。任何交易后,
x
y
都会改变,但不变量仍然成立:

如果您以

getAmountsOut
作为参数来调用
x
,您实际上会问“如果在池中将
y
加倍,我会得到多少
x
?”那么
x' = 2x
不变量是:

求解

y'
,您将得到
y' = y / 2
,它与您的输出相匹配。顺便说一句,这笔交易也会将池子中的价格推高 4 倍,因为价格定义为
y/x

© www.soinside.com 2019 - 2024. All rights reserved.