const FACTORY_ABI = [
"function getPool(address, address, uint24) view returns (address)"
];
const POOL_ABI = [
"function slot0() view returns (uint160, int24, uint16, uint16, uint16, uint8, bool)",
"function token0() view returns (address)",
"function token1() view returns (address)",
"function fee() view returns (uint24)",
"function liquidity() view returns (uint128)"
];
const factory = new ethers.Contract(V3_FACTORY, FACTORY_ABI, provider);
const poolAddress = await factory.getPool(tokenA, tokenB, 3000); // 0.3% 费率
if (poolAddress === ethers.ZeroAddress) {
console.log("Pool does not exist for this fee tier");
} else {
const pool = new ethers.Contract(poolAddress, POOL_ABI, provider);
const [sqrtPriceX96, tick] = await pool.slot0();
// 将 sqrtPriceX96 转换为价格
const price = (Number(sqrtPriceX96) / 2 ** 96) ** 2;
console.log(`Current price (token1/token0): ${price}`);
console.log(`Current tick: ${tick}`);
}