The protocol computes and stores some data which can be consumed by external contracts to create TWAP price oracles for pools. The oracle related data of the pool is updated with any operation which may change the price. It is updated at most once in a block. The first operation of the pool in that block updates the oracle data before any changes are made to the pool ratio.
Pools have 3 oracle related fields in their local state:
- 1.asset_1_cumulative_price (bytes)
- 2.asset_2_cumulative_price (bytes)
- 3.cumulative_price_update_timestamp (uint)
Cumulative price fields are scaled by 2^64 to provide more precision. In the bootstrap operation `asset_1_cumulative_price` and `asset_2_cumulative_price` are set to zero and `cumulative_price_update_timestamp` is set to the latest block timestamp.
time_delta = (latest block timestamp) - (cumulative price update timestamp)
asset_1_cumulative_price = asset_1_cumulative_price + ((asset_2_reserves * 2^64) * time_delta) / (asset_1_reserves)
asset_2_cumulative_price =asset_2_cumulative_price + ((asset_1_reserves * 2^64) * time_delta) / (asset_2_reserves)
cumulative_price_update_timestamp = latest block timestamp
The cumulative price fields accumulate uint128 values represented as bytes. Even if these values accumulate until the final possible block (max uint64 value) then the total cumulative price will never exceed a uint192 or 24 bytes. For this reason, these values do not overflow or wrap around.