Reading the pool
How depth is reconstructed from the contract, and the mistakes that are easy to make doing it.
Finding the pool
The pool address is obtained by asking the factory contract, not by computing it from a construction hash. That hash can differ between deployments, and a wrongly computed address does not announce itself — it returns plausible emptiness. One question to the factory removes an entire class of silent error.
Reading current state
Current price, active liquidity, step size, fee tier and both token addresses are fetched together in one batched call. Token ordering comes from here rather than from the index, because the ordering that matters is the one the contracts use, and getting it backwards mirrors every depth chart in the product.
Walking the steps
Occupied price steps are stored in a bitmap, read in words. For a window around the current price, the relevant words are fetched in one batch, then liquidity is rebuilt outward from the active step in both directions by accumulating the change recorded at each occupied step.
step = current tick / tick spacing (floor, not truncate)
word = step >> 8
fetch words [word - n .. word + n] in one batched call
accumulate outward from the active step, both directions
convert each step into token amounts at its price boundsTwo details in there are easy to get wrong and expensive when you do:
- Floor, not truncate. Prices below parity produce negative step numbers, and the two operations disagree for negatives. Truncating puts the current price in the wrong band.
- Uninitialised steps still exist. A step with no recorded change still carries whatever liquidity the previous step had. Rendering only the steps the contract explicitly returns leaves gaps that are not really there.
Checking the result
Token amounts across the whole window were summed and compared against the pool contract's own token balances. They agreed to within a fraction of a percent, and that figure matched the independent index to within a hundredth of a percent.
Three sources landing on the same number is a much stronger check than comparing two charts by eye, which is why it was done this way rather than by looking.