Oracles

This document is better viewed on the docs page.

Oracle hooks that enable Uniswap v4 pools to function as price oracles by recording price observations and exposing oracle interfaces.

  • BaseOracleHook: Enables a Uniswap V4 pool to record price observations and expose an oracle interface for TWAP queries.

  • OracleHookWithV3Adapters: Extends oracle functionality with V3-compatible adapter contracts for integration with existing V3 oracle consumers.

Panoptic Oracles

BaseOracleHook

import "uniswap-hooks/src/oracles/panoptic/BaseOracleHook.sol";

A hook that enables a Uniswap V4 pool to record price observations and expose an oracle interface

constructor(int24 _maxAbsTickDelta) internal

Initializes a Uniswap V4 pool with this hook, stores baseline observation state, and optionally performs a cardinality increase.

getHookPermissions() → struct Hooks.Permissions public

Get the hook permissions to signal which hook functions are to be implemented.

Used at deployment to validate the address correctly represents the expected permissions.

_afterInitialize(address, struct PoolKey key, uint160, int24 tick) → bytes4 internal

The hook called after the state of a pool is initialized

_beforeSwap(address, struct PoolKey key, struct SwapParams, bytes) → bytes4, BeforeSwapDelta, uint24 internal

The hook called before a swap.

Note that this hook does not return either a BeforeSwapDelta or lp fee override — this call is used exclusively for recording price observations.

observe(uint32[] secondsAgos, PoolId underlyingPoolId) → int56[], int56[] external

Returns the cumulative tick as of each timestamp secondsAgo from the current block timestamp on underlyingPoolId.

To get a time weighted average tick, you must call this with two values, one representing the beginning of the period and another for the end of the period. E.g., to get the last hour time-weighted average tick, you must call it with secondsAgos = [3600, 0].
The time weighted average tick represents the geometric time weighted average price of the pool, in log base sqrt(1.0001) of currency1 / currency0. The TickMath library can be used to go from a tick value to a ratio.

increaseObservationCardinalityNext(uint16 observationCardinalityNext, PoolId underlyingPoolId) public

Increase the maximum number of price and liquidity observations that the oracle of underlyingPoolId.

MAX_ABS_TICK_DELTA() → int24 public

The maximum absolute tick delta that can be observed for the truncated oracle

observationsById() → mapping(PoolId => struct Oracle.Observation[65535]) public

The list of observations for a given pool ID

stateById() → mapping(PoolId => struct BaseOracleHook.ObservationState) public

The current observation array state for the given pool ID

IncreaseObservationCardinalityNext(PoolId indexed underlyingPoolId, uint16 observationCardinalityNextOld, uint16 observationCardinalityNextNew) event

Emitted by the hook for increases to the number of observations that can be stored.

observationCardinalityNext is not the observation cardinality until an observation is written at the index just before a mint/swap/burn.

PoolNotInitialized() error

Observation cardinality cannot be increased if the pool is not initialized

OracleHookWithV3Adapters

import "uniswap-hooks/src/oracles/panoptic/OracleHookWithV3Adapters.sol";

A hook that enables a Uniswap V4 pool to record price observations and expose an oracle interface with Uniswap V3-compatible adapters

constructor(int24 _maxAbsTickDelta) internal

Initializes a Uniswap V4 pool with this hook, stores baseline observation state, and optionally performs a cardinality increase.

_afterInitialize(address, struct PoolKey key, uint160, int24 tick) → bytes4 internal

The hook called after the state of a pool is initialized

standardAdapter() → mapping(PoolId => address) public

Maps pool IDs to their standard V3 oracle adapters

truncatedAdapter() → mapping(PoolId => address) public

Maps pool IDs to their truncated V3 oracle adapters

AdaptersDeployed(PoolId indexed poolId, address standardAdapter, address truncatedAdapter) event

Emitted when adapter contracts are deployed for a pool.