18

给定一个Uniswap V3的池子地址,如何计算代币的价格呢? | 登链社区 | 技术问答

 2 years ago
source link: https://learnblockchain.cn/question/2678
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
给定一个Uniswap V3的池子地址,如何计算代币的价格呢?

10 给定一个Uniswap V3的池子地址,如何计算代币的价格呢?

回答问题即可获得 5 经验值,回答被采纳后即可获得 15 学分。

给定一个Uniswap V3的池子地址,如何使用合约提供的原始信息计算代币的价格呢?能有参数和公式就可以了,非常感谢!

Tiny熊 - 布道者 8小时前

  擅长:智能合约,以太坊

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);
  }

OracleLibrary 是 uniswap/v3 里提供的方法库。

您需要登录后才可以回答问题,登录 或者 注册

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK