The UniswapOracle contract maintains a uniswap TWAP.
It maintains a pair contract to reference and a flag for whether the target price is token0 or token1 of the pair. Has a timestamp duration that must be exceeded between oracle updates. The duration is set to 10 minutes at launch.
take the difference between the current and prior timestamp and make sure it exceeds the duration
get the cumulative price difference between Eth and USDC and normalize by 10**12 (the decimal difference between them)
divide the ratio between the cumulative price and timestamp to get a peg price, then divide by 2^112 to resolve an integer from the stored fixed point 112x112 that Uniswap uses.
update the peg and prior cumulative and timestamp
The Governor ⚖️can change the duration.
new reported peg
new peg value
New TWAP duration
new value of the TWAP duration
function read() external view returns (Decimal.D256 memory, bool);
Reads the oracle value and reports the peg as FEI per underlying. The boolean value returned informs whether the reported value is valid. Invalid means the oracle is uninitialized or the contract is paused.
function isOutdated() external view returns (bool);
Returns true, if the oracle is still within the
duration window. If false, then most read functions relying on the oracle would be inaccurate.
function priorTimestamp() external returns (uint32);
Returns the prior timestamp used in the time-weighted average price calculation from the Uniswap pair.
function priorCumulative() external returns (uint256);
Returns the prior cumulative price used in the time-weighted average price calculation from the Uniswap pair.
function duration() external returns (uint256);
Returns the duration of the time-weighted average price.
function pair() external returns (IUniswapV2Pair);
Returns the referenced Uniswap pair for the oracle.
function update() external returns (bool);
Updates the oracle with new time-weighted average price data from Uniswap if the
duration window has passed since the last update. Returns true if updated, and false otherwise.
function setDuration(uint256 _duration) external;
Changes the time-weighted average price to
_duration second snapshots