TWAPOracleLib
Title: TWAPOracleLib
Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Uniswap V2 (https://github.com/Uniswap/v2-periphery/blob/master/contracts/examples/ExampleSlidingWindowOracle.sol)
Library implementing a Uniswap V2-style time-weighted average price
oracle. Any address may call recordObservation to push a cumulative
price snapshot; consult computes the TWAP over a requested window.
Prices are returned as Uniswap V2 UQ112x112 fixed-point values
(cumulative delta / time delta). The precision is the same as the
underlying pair's price0CumulativeLast / price1CumulativeLast.
Functions
twapOracleStorage
Returns the ERC-7201 storage struct for TWAPOracle.
function twapOracleStorage() internal pure returns (TWAPOracleStorage storage $);Returns
| Name | Type | Description |
|---|---|---|
| $ | TWAPOracleStorage |
__TWAPOracle_init
Registers the ITWAPOracle ERC-165 interface.
Must be called between preInitializer / postInitializer.
function __TWAPOracle_init() internal;registerInterface
Writes true to the ERC-165 map slot for ITWAPOracle.
function registerInterface() internal;getPair
Returns the pair address registered under key.
function getPair(bytes32 key) internal view returns (address pair);Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 |
Returns
| Name | Type | Description |
|---|---|---|
| pair | address |
getLatestObservation
Returns the most recently recorded observation for a pair.
Reverts TWAPPairNotRegistered if the key has no pair.
Reverts TWAPInsufficientHistory if no observations exist.
function getLatestObservation(bytes32 key) internal view returns (ITWAPOracle.Observation memory);Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | ITWAPOracle.Observation |
consult
Computes the TWAP over the last windowSeconds for a pair.
Selects the oldest observation that is at least windowSeconds old
and computes (cumulativeNow - cumulativeOld) / (timestampNow - timestampOld).
The search walks backward from the second-to-last observation to find the most recent one that is old enough to satisfy the window, ensuring we return a TWAP that covers at least the requested period.
function consult(bytes32 key, uint32 windowSeconds) internal view returns (uint256 price0Twap, uint256 price1Twap);Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The pair identifier. |
| windowSeconds | uint32 | Desired TWAP window in seconds. |
Returns
| Name | Type | Description |
|---|---|---|
| price0Twap | uint256 | TWAP of token0 in UQ112x112 fixed-point per second units. |
| price1Twap | uint256 | TWAP of token1 in UQ112x112 fixed-point per second units. |
registerPair
Registers a Uniswap V2 pair and records the initial observation.
Caller must hold DEFAULT_ADMIN_ROLE. Re-registering an existing key discards its stored
observations, so consult never mixes cumulatives from two different pairs.
function registerPair(bytes32 key, address pair) internal;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | Arbitrary identifier for this pair. |
| pair | address | Address of the IUniswapV2Pair contract. |
unregisterPair
Removes a registered pair and all its stored observations.
Caller must hold DEFAULT_ADMIN_ROLE.
function unregisterPair(bytes32 key) internal;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The pair identifier to remove. |
recordObservation
Records a new cumulative price observation for the pair.
Open to any caller; reverts TWAPPairNotRegistered if the pair is
not registered.
function recordObservation(bytes32 key) internal;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The pair identifier. |
_appendObservation
Reads the current cumulatives from the pair and appends an observation.
Skips the push if an observation with the same timestamp was already recorded (same-block deduplication). This prevents unbounded array growth from permissionless callers spamming recordObservation within a single block and avoids wasted gas on redundant SSTOREs.
function _appendObservation(TWAPOracleStorage storage $, bytes32 key, address pair) internal;Parameters
| Name | Type | Description |
|---|---|---|
| $ | TWAPOracleStorage | |
| key | bytes32 | |
| pair | address |