实时价格那就只能去查询链上合约的实时状态了,用来计算价格,使用 UniswapV2Library库(注意修改initcodehash为pancake的initcodehash)来计算价格,tokenIn是你要查询价格的Token,tokenOut是稳币USDX,amountIn传入1个单位的tokenIn,amountOut既是你要查询的tokenIn价格
(uint256 reserveIn, uint256 reserveOut) = UniswapV2Library.getReserves(
factory,
address(tokenIn),
address(tokenOut)
);
uint256 amountOut = UniswapV2Library.getAmountOut(amountIn,reserveIn,reserveOut);
如果没有和稳币直接建立交易对,那就用getAmountsOut接口
function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts)