UniswapOracle

An ETH/USDC Uniswap TWAP snapshot oracle

Contract

UniswapOracle.sol implements IUniswapOracle, CoreRef

Description

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.

Updates should:

  • 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.

Events

Update
DurationUpdate
Update

new reported peg

type

param

description

uint256

_peg

new peg value

DurationUpdate

New TWAP duration

type

param

description

uint256

_duration

new value of the TWAP duration

Read-Only Functions

read

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.

This method is pausable. If paused, it won't revert but it will return valid as false

isOutdated

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.

priorTimestamp

function priorTimestamp() external returns (uint32);

Returns the prior timestamp used in the time-weighted average price calculation from the Uniswap pair.

priorCumulative

function priorCumulative() external returns (uint256);

Returns the prior cumulative price used in the time-weighted average price calculation from the Uniswap pair.

duration

function duration() external returns (uint256);

Returns the duration of the time-weighted average price.

pair

function pair() external returns (IUniswapV2Pair);

Returns the referenced Uniswap pair for the oracle.

Public State-Changing Functions

update

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.

emits Update

This method is pausable

Governor-Only⚖️ State-Changing Functions

setDuration

function setDuration(uint256 _duration) external;

Changes the time-weighted average price to _duration second snapshots

emits DurationUpdate

ABIs