UniswapV4CMXOracle

Git Source

Oracle for CMX/ETH exchange rates using Uniswap V4 pools

Provides real-time and time-weighted average prices for CMX token

State Variables

PROTOCOL_ADMIN

Address of the protocol admin

address public immutable PROTOCOL_ADMIN;

CMX_TOKEN

CMX token address

address public immutable CMX_TOKEN;

WETH_TOKEN

WETH token address

address public immutable WETH_TOKEN;

poolManager

Uniswap V4 Pool Manager

address public poolManager;

cmxPool

CMX/ETH pool configuration

PoolConfig public cmxPool;

maxPriceAge

Maximum age for price data (in seconds)

uint256 public maxPriceAge = 300;

authorizedUpdaters

Authorized price updaters

mapping(address => bool) public authorizedUpdaters;

priceObservations

Price observations for TWAP

PriceObservation[] public priceObservations;

MAX_OBSERVATIONS

Maximum number of observations to store

uint256 public constant MAX_OBSERVATIONS = 100;

MIN_TWAP_PERIOD

Minimum TWAP period (in seconds)

uint32 public constant MIN_TWAP_PERIOD = 60;

MAX_TWAP_PERIOD

Maximum TWAP period (in seconds)

uint32 public constant MAX_TWAP_PERIOD = 3600;

Functions

constructor

Initialize the CMX oracle

constructor(address protocolAdmin, address cmxToken, address wethToken, address initialPoolManager);

Parameters

Name
Type
Description

protocolAdmin

address

Address of the protocol admin

cmxToken

address

CMX token address

wethToken

address

WETH token address

initialPoolManager

address

Initial pool manager address

onlyProtocolAdmin

modifier onlyProtocolAdmin();

onlyAuthorizedUpdater

modifier onlyAuthorizedUpdater();

getCurrentPrice

Get current CMX/ETH exchange rate from Uniswap V4 pool

function getCurrentPrice() external view returns (uint256 price, uint256 timestamp);

Returns

Name
Type
Description

price

uint256

Current price (CMX per ETH, 18 decimals)

timestamp

uint256

Block timestamp of price

getTWAP

Get time-weighted average price for specified period

function getTWAP(uint32 period) external view returns (uint256 twapPrice, uint256 observationCount);

Parameters

Name
Type
Description

period

uint32

TWAP period in seconds

Returns

Name
Type
Description

twapPrice

uint256

Time-weighted average price

observationCount

uint256

Number of observations used

getPriceWithFallback

Get price with fallback to TWAP if spot price is stale

function getPriceWithFallback(uint32 twapPeriod) external view returns (uint256 price, string memory source);

Parameters

Name
Type
Description

twapPeriod

uint32

TWAP period to use as fallback

Returns

Name
Type
Description

price

uint256

Best available price

source

string

Price source used ("spot" or "twap")

updatePoolManager

Update pool manager address

function updatePoolManager(address newManager) external onlyProtocolAdmin;

Parameters

Name
Type
Description

newManager

address

New pool manager address

configureCMXPool

Configure CMX/ETH pool

function configureCMXPool(bytes32 poolKey, uint24 fee, int24 tickSpacing) external onlyProtocolAdmin;

Parameters

Name
Type
Description

poolKey

bytes32

Pool key identifier

fee

uint24

Pool fee tier

tickSpacing

int24

Tick spacing

updatePriceObservation

Update price observation (called by authorized updaters)

function updatePriceObservation(uint256 price) external onlyAuthorizedUpdater;

Parameters

Name
Type
Description

price

uint256

New price observation

setMaxPriceAge

Set maximum price age

function setMaxPriceAge(uint256 newMaxAge) external onlyProtocolAdmin;

Parameters

Name
Type
Description

newMaxAge

uint256

New maximum age in seconds

authorizeUpdater

Authorize a price updater

function authorizeUpdater(address updater) external onlyProtocolAdmin;

Parameters

Name
Type
Description

updater

address

Address to authorize

revokeUpdaterAuthorization

Revoke updater authorization

function revokeUpdaterAuthorization(address updater) external onlyProtocolAdmin;

Parameters

Name
Type
Description

updater

address

Address to revoke

getConfiguration

Get oracle configuration

function getConfiguration()
    external
    view
    returns (
        address cmxToken,
        address wethToken,
        address poolManagerAddr,
        PoolConfig memory poolConfig,
        uint256 maxAge,
        uint256 observationCount
    );

Returns

Name
Type
Description

cmxToken

address

CMX token address

wethToken

address

WETH token address

poolManagerAddr

address

Pool manager address

poolConfig

PoolConfig

Pool configuration

maxAge

uint256

Maximum price age

observationCount

uint256

Number of observations

getRecentObservations

Get recent price observations

function getRecentObservations(uint256 count) external view returns (PriceObservation[] memory observations);

Parameters

Name
Type
Description

count

uint256

Number of recent observations to return

Returns

Name
Type
Description

observations

PriceObservation[]

Array of recent price observations

_fetchSpotPrice

Fetch spot price from Uniswap V4 pool

function _fetchSpotPrice() internal view returns (uint256 price);

Returns

Name
Type
Description

price

uint256

Current spot price

Events

PoolManagerUpdated

Emitted when pool manager is updated

event PoolManagerUpdated(address indexed oldManager, address indexed newManager);

Parameters

Name
Type
Description

oldManager

address

Previous pool manager

newManager

address

New pool manager

CMXPoolUpdated

Emitted when CMX pool is updated

event CMXPoolUpdated(bytes32 indexed oldPool, bytes32 indexed newPool);

Parameters

Name
Type
Description

oldPool

bytes32

Previous pool key

newPool

bytes32

New pool key

PriceFetched

Emitted when price is fetched

event PriceFetched(uint256 indexed price, uint256 timestamp, string source);

Parameters

Name
Type
Description

price

uint256

Current price (CMX per ETH)

timestamp

uint256

Block timestamp

source

string

Price source (spot or TWAP)

Errors

InvalidPoolManager

error InvalidPoolManager(address manager);

InvalidCMXToken

error InvalidCMXToken(address token);

InvalidPoolKey

error InvalidPoolKey(bytes32 poolKey);

PriceNotAvailable

error PriceNotAvailable(string reason);

InvalidTWAPPeriod

error InvalidTWAPPeriod(uint32 period);

UnauthorizedCaller

error UnauthorizedCaller(address caller);

StalePrice

error StalePrice(uint256 age, uint256 maxAge);

Structs

PoolConfig

Pool configuration for CMX/ETH pair

struct PoolConfig {
    bytes32 poolKey;
    uint24 fee;
    int24 tickSpacing;
    bool isActive;
}

Properties

Name
Type
Description

poolKey

bytes32

Unique identifier for the pool

fee

uint24

Pool fee tier

tickSpacing

int24

Tick spacing for the pool

isActive

bool

Whether the pool is active

PriceObservation

Price observation for TWAP calculations

struct PriceObservation {
    uint256 price;
    uint256 timestamp;
    uint256 blockNumber;
}

Properties

Name
Type
Description

price

uint256

Price at observation time

timestamp

uint256

Block timestamp of observation

blockNumber

uint256

Block number of observation

Last updated

Was this helpful?