ChainlinkPrices

📄 View Source Code

Overview

ChainlinkPrices integrates Chainlink's decentralized oracle network to provide reliable USD pricing for protocol assets. It manages multiple price feeds with built-in staleness checks and decimal normalization for consistent valuations.

Core Functions:

  • Feed Management: Maps assets to their Chainlink aggregators with metadata tracking

  • Staleness Protection: Enforces heartbeat intervals to reject outdated prices

  • Decimal Normalization: Converts all prices to 18-decimal USD values

  • Cross-Rate Support: Handles ETH/BTC denominated feeds with automatic USD conversion

The module implements automatic validation of price feed data, configurable staleness thresholds per asset, time-locked governance for feed updates, and special handling for core assets (ETH, WETH, BTC) that cannot be disabled. It integrates with PriceDesk for aggregation and uses MissionControl for configuration management.

Architecture & Dependencies

ChainlinkPrices is built using a modular architecture with direct oracle integration:

Core Module Dependencies

  • LocalGov: Provides governance functionality with access control

  • Addys: Address resolution for protocol contracts

  • PriceSourceData: Asset registration and pause state management

  • TimeLock: Time-locked changes for feed management

External Integrations

  • Chainlink Oracles: Direct integration with Chainlink aggregator contracts

  • MissionControl: Retrieves global price staleness configuration

Module Initialization

Immutable Assets

Data Structures

ChainlinkRound Struct

Raw data returned from Chainlink oracles:

ChainlinkConfig Struct

Configuration for each asset's price feed:

PendingChainlinkConfig Struct

Tracks pending changes during timelock:

State Variables

Feed Configuration

  • feedConfig: HashMap[address, ChainlinkConfig] - Active Chainlink configurations

  • pendingUpdates: HashMap[address, PendingChainlinkConfig] - Pending changes

Constants

  • NORMALIZED_DECIMALS: constant(uint256) = 18 - Standard decimal format

Inherited State

From modules:

  • Governance state (LocalGov)

  • Pause state and asset registry (PriceSourceData)

  • TimeLock configuration

System Architecture Diagram

Constructor

__init__

Initializes ChainlinkPrices with core asset feeds and governance settings.

Parameters

Name
Type
Description

_ripeHq

address

RipeHq contract for protocol integration

_tempGov

address

Initial temporary governance address

_minPriceChangeTimeLock

uint256

Minimum timelock for feed changes

_maxPriceChangeTimeLock

uint256

Maximum timelock for feed changes

_wethAddr

address

Wrapped ETH token address

_ethAddr

address

ETH representation address

_btcAddr

address

BTC representation address

_ethUsdFeed

address

Chainlink ETH/USD feed address

_btcUsdFeed

address

Chainlink BTC/USD feed address

Deployment Behavior

  • Sets ETH/USD feed for both ETH and WETH if provided

  • Sets BTC/USD feed for BTC if provided

  • Validates feeds during deployment

  • Registers default assets in PriceSourceData

Example Usage

Core Price Functions

getPrice

Retrieves the current price for an asset from Chainlink oracles.

Parameters

Name
Type
Description

_asset

address

Asset to get price for

_staleTime

uint256

Maximum age for price data in seconds. The caller is responsible for providing this value (often sourced from MissionControl). A value of 0 disables the staleness check for this call.

_priceDesk

address

Not used in Chainlink implementation

Returns

Type
Description

uint256

Normalized price in 18 decimals (0 if invalid)

Process Flow

  1. Config Load: Retrieves ChainlinkConfig for asset

  2. Oracle Call: Queries Chainlink aggregator

  3. Validation: Checks answer validity and staleness

  4. Normalization: Converts to 18 decimals

  5. Cross-Rate: Applies ETH/USD or BTC/USD if needed

Example Usage

getPriceAndHasFeed

Returns price and feed existence status.

Returns

Type
Description

uint256

Current price (0 if no feed)

bool

True if feed exists

getChainlinkData

Direct access to Chainlink oracle data with normalization.

Useful for debugging or accessing feeds not registered in the contract.

Feed Management Functions

addNewPriceFeed

Initiates addition of a new Chainlink price feed.

Parameters

Name
Type
Description

_asset

address

Asset to add price feed for

_newFeed

address

Chainlink aggregator address

_staleTime

uint256

Maximum age for price data in seconds (0 uses MissionControl default)

_needsEthToUsd

bool

Convert ETH price to USD

_needsBtcToUsd

bool

Convert BTC price to USD

Access

Only callable by governance

Validation

  • Feed must return valid price data

  • Cannot set both ETH and BTC conversion

  • Asset must not already have a feed

Events Emitted

  • NewChainlinkFeedPending - Contains feed details and confirmation block

Example Usage

confirmNewPriceFeed

Confirms a pending feed addition after timelock.

Process Flow

  1. Re-validation: Ensures feed still returns valid data

  2. Timelock Check: Verifies sufficient time passed

  3. Configuration Save: Stores feed config

  4. Asset Registration: Adds to PriceSourceData

Events Emitted

  • NewChainlinkFeedAdded - Confirms feed is active

cancelNewPendingPriceFeed

Cancels a pending new price feed addition.

Parameters

Name
Type
Description

_asset

address

Asset with pending feed to cancel

Access

Only callable by governance

Events Emitted

  • NewChainlinkFeedCancelled - Pending feed cancelled

updatePriceFeed

Updates existing feed configuration.

Used when Chainlink migrates to new aggregator addresses.

confirmPriceFeedUpdate

Confirms a pending feed update after timelock.

Access

Only callable by governance

Events Emitted

  • ChainlinkFeedUpdated - Feed update confirmed

cancelPriceFeedUpdate

Cancels a pending price feed update.

Parameters

Name
Type
Description

_asset

address

Asset with pending update to cancel

Access

Only callable by governance

Events Emitted

  • ChainlinkFeedUpdateCancelled - Pending update cancelled

disablePriceFeed

Removes a price feed (except core assets).

Restrictions

  • Cannot disable ETH, WETH, or BTC feeds

  • Requires time-locked confirmation

confirmDisablePriceFeed

Confirms a pending feed removal after timelock.

Access

Only callable by governance

Events Emitted

  • ChainlinkFeedDisabled - Feed removed

cancelDisablePriceFeed

Cancels a pending price feed removal.

Parameters

Name
Type
Description

_asset

address

Asset with pending disable to cancel

Access

Only callable by governance

Events Emitted

  • DisableChainlinkFeedCancelled - Pending disable cancelled

Oracle Interaction

Internal Price Retrieval

Cross-Rate Conversion

Validation Functions

isValidNewFeed

Validates new feed configuration.

Validation Checks

  1. No Existing Feed: Asset must not have active feed

  2. Valid Addresses: Neither asset nor feed can be empty

  3. Single Conversion: Cannot use both ETH and BTC conversion

  4. Working Feed: Must return valid price with staleness check

isValidUpdateFeed

Validates feed updates.

Additional checks:

  • New feed must differ from current

  • Asset must have existing feed

isValidDisablePriceFeed

Validates feed removal.

Checks:

  • Feed must exist

  • Cannot disable core assets (ETH, WETH, BTC)

Staleness Management

Default Staleness

Retrieved from MissionControl:

Per-Call Staleness

Can override default:

Staleness in Validation

Feed validation uses MissionControl staleness to ensure feeds work with protocol settings.

Security Considerations

Oracle Security

  • Round ID Validation: Ensures answers are from current round

  • Timestamp Checks: Prevents future-dated prices

  • Positive Price: Rejects zero or negative prices

  • Answer Freshness: Uses answeredInRound for staleness

Access Control

  • Governance Only: All feed management restricted

  • Time-locked Changes: Prevents rushed modifications

  • Core Asset Protection: ETH/WETH/BTC cannot be disabled

Cross-Rate Security

  • Single Conversion: Prevents compound conversion errors

  • Dependency Validation: Ensures ETH/BTC feeds exist

  • Atomic Calculation: No partial conversions

Integration Safety

  • Decimal Handling: Safe normalization to 18 decimals

  • Overflow Protection: Vyper's built-in checks

  • Zero Returns: Invalid data returns 0, not reverts

Common Integration Patterns

Multi-Asset Pricing

Cross-Rate Setup

Feed Migration

Events

Feed Management Events

  • NewChainlinkFeedPending - New feed initiated

  • NewChainlinkFeedAdded - Feed confirmed and active

  • NewChainlinkFeedCancelled - Pending feed cancelled

  • ChainlinkFeedUpdatePending - Update initiated

  • ChainlinkFeedUpdated - Update confirmed

  • ChainlinkFeedUpdateCancelled - Update cancelled

  • DisableChainlinkFeedPending - Removal initiated

  • ChainlinkFeedDisabled - Feed removed

  • DisableChainlinkFeedCancelled - Removal cancelled

All events include relevant addresses, configuration flags, and timelock details.

Last updated