Section 13 of 18
Oracle: The Observation Ring Buffer
Key takeaway: V3's oracle stores its own history. A ring buffer of up to 65535
Observationstructs lives inside the pool, each recordingtickCumulative(tick-seconds since initialization) andsecondsPerLiquidityCumulativeX128. Because tick islog1.0001(price), the arithmetic mean of tick over a window is the geometric mean of price — a TWAP that is symmetric between token0 and token1 and cheap to read: two accumulator values and one subtraction. Anyone who wants a longer history callsincreaseObservationCardinalityNextand pays the SSTOREs themselves;growpre-warms the new slots withblockTimestamp = 1so future swappers pay a cheap nonzero-to-nonzero store instead of a 20k fresh one. All the timestamp math wraps at 2^32 by design, which in Solidity 0.8 means it must live insideuncheckedblocks.
What You Are Building
The Oracle library: transform, initialize, write, grow, getSurroundingObservations, observeSingle, and observe — plus the two pool-side entry points, observe and increaseObservationCardinalityNext. The comparator lte and binarySearch are provided; you implement everything that reads and writes the ring buffer around them.
Your Code
Requirements
Sign up free — keep reading + earn 15 Lynx
Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.
Sign up free to continueAlready have an account? Log in