Verification checklist
- The accumulator adds tick times seconds. The price ratio is 1.0001 raised to the mean tick, not the average of the endpoint prices.
- When the cumulative delta is negative and does not divide evenly, consult truncates toward zero and then subtracts 1. -201 divided by 2 is written as -101, not -100.
- When secondsAgo is 0, observe extends the latest observation to the current time. consult reverts with BP. A window older than the oldest observation reverts with OLD.
- Liquidity is a harmonic mean. One hundred seconds at 1000 and one hundred at 3000 write 1500. A stretch at liquidity 0 uses 1 as the divisor.
1. The accumulator adds ticks, not prices
A pool page often prints a TWAP as one price. Oracle v1.0.0 has no price field on Observation. The comment on tickCumulative says it is the tick times the time elapsed since the pool was initialized. transform adds the active tick times the timestamp delta to the previous cumulative. Two equal stretches, tick 0 and tick 10000, add 0×100 + 10000×100 = 1000000. Over 200 seconds, 1000000 / 200 = 5000. The average is a tick.
The price ratio uses the truncation from the square-root article on this site: 1.0001 raised to the tick, cut off at 12 decimal places. Tick 10000 is 2.718145926825, the same figure as that article. Tick 0 is 1. Tick 5000 is 1.648680055931. Adding the two already truncated prices and dividing by 2 gives 1.8590729634125, which cuts off at 12 decimals as 1.859072963412. That is 0.210392907481 above 1.648680055931.
The gap is not rounding error. A power of 1.0001 is convex. Averaging the ticks and then raising the power is not the same number as raising each power and then averaging the prices. Reading 1.859072963412 back as the greatest tick whose ratio does not exceed it: tick 6201 ratios to 1.859056306853, and tick 6202 ratios to 1.859242212484. The arithmetic average sits between those two ticks, so the lower tick is 6201. The distance from 5000 to 6201 is 1201 ticks. A note that writes the time-weighted price as the average of the endpoint prices overstates the tick by 1201 and the ratio by 0.210392907481.
This is not the question in the square-root article, and it is not the question in the spot-versus-fill article. The square-root article asks whether one division of the sqrt price is already the quote. The spot article asks whether reserve divided by reserve can be multiplied out into this swap's output. This article assumes the single-tick ratio is already computed that way, and asks whether the mean tick over an interval is the average of the prices at the ends. OracleLibrary v1.3.0 getQuoteAtTick quotes from the square root at the mean tick, not from the arithmetic average price. Feeding 1.859072963412 into the quote moves the tick to 6201.
| Reading | Tick | Price ratio, truncated to 12 decimals |
|---|---|---|
| Cumulative delta divided by seconds | 5000 | 1.648680055931 |
| Endpoint prices added, then divided by 2 | Lower tick 6201 | 1.859072963412 |
| Gap between the two readings | 1201 | 0.210392907481 |
2. A negative window steps one tick toward negative infinity
Division in consult truncates toward zero. The comment says the result always rounds toward negative infinity. A zero remainder is left alone. A negative cumulative delta that does not divide evenly is then reduced by 1. A positive window does not show that extra step.
Take a teaching delta of 201 over 2 seconds. 201 / 2 = 100, remainder 1. The delta is positive, so the contract writes 100. Truncation toward zero is also 100. This pair cannot prove that the negative pair behaves the same way.
Change the delta to -201. Truncation toward zero is -100. The remainder is not zero, so the contract subtracts 1 and writes -101. The ratio at tick -100 is 0.990050328741. The ratio at tick -101 is 0.989951333607. The missing tick is a ratio gap of 0.000098995134. Copying the toward-zero result writes the higher ratio at -100, not the contract's -101.
| Cumulative delta | Seconds | Toward zero | consult |
|---|---|---|---|
| 201 | 2 | 100 | 100 |
| -201 | 2 | -100 | -101 |
The two signs are not mirrors. 201 leaves 100, and -201 leaves -101. Both drop a remainder of 1. Only the negative delta moves one more tick. If the window divides evenly, a delta of -200 over 2 seconds has remainder 0, and the contract writes -100 without the extra subtraction. The extra step depends on the remainder, not on the sign by itself.
3. The open interval still accumulates, and an early window reverts
write stores at most one observation per block. Between writes, the latest tickCumulative in the array stays at the last stored value. When secondsAgo is 0 and the stored timestamp is not the current time, observe runs transform and adds the current tick times that open interval. The open interval is the ordinary case, not an edge case.
Teaching case: the only observation is at time 0 with cumulative 0. The active tick stays at 200, and nothing writes a new observation. At time 200, observe with secondsAgo 0 transforms the cumulative to 0 + 200×200 = 40000. Reading the stored 0 writes the mean tick of those 200 seconds as 0. Dividing after the transform, the mean tick is 200. The missing piece is the whole 40000, not a rounding dust.
If the window is older than the oldest observation, getSurroundingObservations requires the target to be at or after that observation and reverts with OLD. initialize sets cardinality to 1, so the array holds that single observation. A secondsAgo longer than the age of that observation makes observe revert. It does not return slot0.tick, and it does not return 0. Reading the revert as "the TWAP is 0" or "the TWAP equals the spot tick" writes a number after the call has already failed.
consult has a separate gate. secondsAgo of 0 fails the require and reverts with BP. observe allows 0, and that 0 means "extend to now." consult does not. Putting the spot tick in as the answer to a zero-second consult does not make the function return that tick. Check whether the seconds are 0, then whether the window is older than the oldest observation, and only then divide the cumulative delta.
4. Between two observations, divide first and multiply second
When the target time falls between two observations, observeSingle does not multiply the cumulative delta by the time fraction and then divide. It divides the cumulative delta by the observation interval first, then multiplies by the seconds from the left observation to the target. The remainder is dropped before the multiplication.
Teaching case: cumulative 0 on the left, 5 on the right, an interval of 3 seconds, and a target 2 seconds from the left. 5 / 3 = 1, times 2, is 2. Multiplying first gives 5×2 / 3 = 3. The contract is short by 1. That 1 is the interpolated cumulative, not the mean tick itself. On a short window, the missing 1 can move the mean tick by a full step. Writing the formula as multiply-then-divide puts the step back, and that result is not what the contract stores.
5. Liquidity is a harmonic mean, and 0 is divided as 1
The same oracle also stores secondsPerLiquidityCumulativeX128. transform adds the time delta shifted left by 128, divided by liquidity. When liquidity is 0, the divisor is 1. consult turns that delta into a harmonic mean: seconds times 2 to the 160, minus 1, divided by the cumulative delta shifted left by 32. That is not the two liquidities added and divided by 2.
Teaching case: 100 seconds at liquidity 1000, then 100 seconds at liquidity 3000. After transform's integer division and consult's division, the harmonic mean is 1500. The arithmetic mean (1000+3000)/2 is 2000. Using 2000 as the depth of this window overstates it by 500, which is one third of 1500. The harmonic mean sits closer to the thinner stretch. The 1000 weighs more in the denominator, so 1500 is nearer to 1000 than to 3000.
One hundred seconds at liquidity 0, then one hundred at 3000, use 1 as the divisor, and the contract's harmonic mean is 1. Averaging 0 and 3000 writes 1500, which overstates the contract by 1499. The real weighted harmonic mean that uses divisor 1 is 6000/3001, truncated to 12 decimals as 1.999333555481. The contract's integer division writes 1, short of that real value by less than 1. The shortfall is the division, not a decision to treat liquidity 0 as 1500.
In the fee article, liquidity 0 means the growth accumulator does not add the remainder. This oracle still accumulates seconds with divisor 1. The two ledgers move in opposite directions when liquidity is 0: fee growth stops, and the time-weighted liquidity divisor becomes 1. Copying a 0 from one ledger into the other misstates both the fee and the depth.
6. Divide the accumulator, then raise 1.0001 to that tick
The failing sentences are "the time-weighted price is the average of the two prices" and "an OLD revert means the price is 0." The sentence that can be checked is "the cumulative delta; the seconds; whether a negative remainder subtracted 1; whether seconds of 0 went to observe or to consult; whether the window is older than the oldest observation; whether the interpolation divided first; whether liquidity 0 used divisor 1." In the teaching window, mean tick 5000 ratios to 1.648680055931, and the average of the endpoint prices is 1.859072963412. -201 divided by 2 is written as -101. Liquidity 1000 and 3000 for 100 seconds each harmonic-mean to 1500, not 2000.
This is an analytical frame. The accumulator, transform, the OLD revert, and the divide-then-multiply interpolation come from Oracle at v1.0.0. The round toward negative infinity, the BP revert, the harmonic mean, and the quote at the mean tick come from OracleLibrary at v1.3.0. 1.648680055931, 1.859072963412, 0.210392907481, 6201, -101, 1500, and 1 are teaching conversions. They were not measured on a live pool, and they are not investment advice.
7. Misreadings that keep coming back
Is the time-weighted price the two endpoint prices added and divided by 2?
No. The v1.0.0 accumulator adds tick times seconds. One hundred seconds at tick 0 and one hundred at tick 10000 average to tick 5000. Raised as a power of 1.0001 and truncated to 12 decimals, the ratio is 1.648680055931. The two prices added and divided by 2 are 1.859072963412.
If the cumulative delta is -201 over 2 seconds, is the mean tick -100?
No. consult first truncates toward zero and gets -100. When the delta is negative and does not divide evenly, it subtracts 1 and writes -101. The ratio at tick -100 is 0.990050328741. The ratio at tick -101 is 0.989951333607.
If the window is older than the oldest observation, does observe return the spot tick?
No. observe reverts with OLD when the target is older than the oldest observation. It does not return slot0.tick, and it does not return 0. consult reverts with BP when secondsAgo is 0, which is a different call from observe using 0 to mean the current cumulative.