uniswapv3 如何根据一个代币查兑换另一个代币的数量?

用的之前看到的合约代码,uniswapv3 在测试网上添加流动性后,前端页面和在合约里面查到的价格差了10倍,是哪里不对吗?

function getPrice(address tokenIn, address tokenOut)
external
view
returns (uint256 price)
{
IUniswapV3Pool pool = IUniswapV3Pool(factory.getPool(tokenIn, tokenOut, fee));
(uint160 sqrtPriceX96,,,,,,) =  pool.slot0();
return uint(sqrtPriceX96).mul(uint(sqrtPriceX96)).mul(1e18) >> (96 * 2);
}

function getQuote(address tokenA, uint amountA, address tokenB) public view override returns (uint256 amountB) {
int24 tick = OracleLibrary.consult(factory.getPool(tokenIn, tokenOut, fee), 60);
return OracleLibrary.getQuoteAtTick(tick, uint128(amountA), tokenA, tokenB);
}
请先 登录 后评论

最佳答案 2022-01-06 09:50

你这两个方法应该都是可以的。

一个是直接计算出数量,一个是价格, 不一样的呀

请先 登录 后评论

其它 3 个回答

小溪
请先 登录 后评论
路远 - 开发
请先 登录 后评论
volar
请先 登录 后评论