Integration
Pricing and graduation
Deriving price, market cap and progress correctly.
Price from the pool
const [sqrtPriceX96] = await pool.read.slot0()
const sq = Number(sqrtPriceX96) / 2 ** 96
const p1p0 = sq * sq // token1 per token0
const tokenIsToken0 = token.toLowerCase() < WETH.toLowerCase()
const price = tokenIsToken0 ? p1p0 : (p1p0 ? 1 / p1p0 : 0) // ETH per token
Derive the ordering, never assume it. Uniswap sorts a pair by address, so
whether a coin is token0 varies per coin. Getting it backwards yields the
reciprocal, which is a plausible-looking number that is completely wrong.
Market cap
price × 1e9, in ETH. Multiply by an ETH price for a fiat figure; there is no
on-chain oracle for that here.
Liquidity and graduation progress
const wethInPool = await weth.read.balanceOf([pool])
const progress = Number(formatEther(wethInPool)) / 4.2
The 4.2 threshold is a frontend constant. It is not in any contract, so do not expect on-chain behaviour to change at it. See Graduation.