Skip to content
Lattice

TWAPOracle

Git Source

Inherits: ITWAPOracle

Title: TWAPOracle

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)

Diamond facet for a Uniswap V2-style time-weighted average price oracle.

Stateless delegator — all logic and storage live in TWAPOracleLib. Consumers inherit this contract and add AccessControl + an initializer.

Notes:

  • lattice-version: 0.1.0
  • lattice-source: Uniswap V2

Functions

getPair

Returns the address of the Uniswap V2 pair registered under key.

function getPair(bytes32 key) external view virtual override returns (address pair);

Parameters

NameTypeDescription
keybytes32The pair identifier.

Returns

NameTypeDescription
pairaddress

getLatestObservation

Returns the most recently recorded observation for a pair.

function getLatestObservation(bytes32 key) external view virtual override returns (Observation memory);

Parameters

NameTypeDescription
keybytes32The pair identifier.

Returns

NameTypeDescription
<none>Observation

consult

Computes the TWAP over the last windowSeconds for a registered pair.

Selects the oldest observation that is at least windowSeconds old and computes (cumulativeNow - cumulativeOld) / (timestampNow - timestampOld).

function consult(bytes32 key, uint32 windowSeconds)
        external
        view
        virtual
        override
        returns (uint256 price0Twap, uint256 price1Twap);

Parameters

NameTypeDescription
keybytes32The pair identifier.
windowSecondsuint32The desired TWAP window in seconds.

Returns

NameTypeDescription
price0Twapuint256TWAP of token0 over the window (UQ112x112 fixed-point).
price1Twapuint256TWAP of token1 over the window (UQ112x112 fixed-point).

registerPair

Registers a Uniswap V2 pair under the given key and records an initial observation.

Caller must hold DEFAULT_ADMIN_ROLE. Re-registering an existing key discards its stored observations and starts a fresh history.

function registerPair(bytes32 key, address pair) external virtual override;

Parameters

NameTypeDescription
keybytes32Arbitrary identifier for this pair.
pairaddressAddress of the IUniswapV2Pair contract.

unregisterPair

Removes a registered pair and all its stored observations.

Caller must hold DEFAULT_ADMIN_ROLE.

function unregisterPair(bytes32 key) external virtual override;

Parameters

NameTypeDescription
keybytes32The pair identifier to remove.

recordObservation

Records a new cumulative price observation for the pair.

Open to any caller to allow permissionless oracle maintenance.

function recordObservation(bytes32 key) external virtual override;

Parameters

NameTypeDescription
keybytes32The pair identifier.

exportSelectors

ERC-8153 selector export: this facet's cuttable selectors, tightly packed (4 bytes each).

Excludes exportSelectors() itself (0x0ef22643) - it is never cut into a diamond. Order matches forge inspect TWAPOracle methodIdentifiers (alphabetical by signature); kept in exact parity by ExportSelectorsParityTest. Chunks: consult(bytes32,uint32) 0x47473b00 getLatestObservation(bytes32) 0x43542636 getPair(bytes32) 0xb8e5303d recordObservation(bytes32) 0x06bd6d1e registerPair(bytes32,address) 0x64b7e622 unregisterPair(bytes32) 0x0f464dd7

function exportSelectors() external pure virtual returns (bytes memory selectors);

Returns

NameTypeDescription
selectorsbytes