ChainlinkOracle

ChainlinkOracle: EUR/USD Price Infrastructure

πŸ“‹ Overview

The ChainlinkOracle is the contract that manages integration with Chainlink price feeds to obtain real-time EUR/USD and USDC/USD prices. It includes robust security mechanisms like circuit breakers, data freshness validation, and timestamp manipulation protection.


πŸ—οΈ Contract Architecture

Inheritance

contract ChainlinkOracle is 
    IChainlinkOracle,
    Initializable,
    AccessControlUpgradeable,
    PausableUpgradeable,
    UUPSUpgradeable

Chainlink Price Feeds

Feed
Variable
Description

EUR/USD

eurUsdPriceFeed

Euro price in dollars

USDC/USD

usdcUsdPriceFeed

USDC price (validation)


πŸ” Roles & Permissions

Role
Responsibilities

DEFAULT_ADMIN_ROLE

General administration

ORACLE_MANAGER_ROLE

Feed configuration, bounds, tolerances

EMERGENCY_ROLE

Circuit breaker, pause

UPGRADER_ROLE

Contract upgrades


βš™οΈ Security Constants


πŸ“Š Configuration Variables

Price Bounds

Variable
Type
Description
Default Value

minEurUsdPrice

uint256

Min EUR/USD price (18 dec)

0.80e18

maxEurUsdPrice

uint256

Max EUR/USD price (18 dec)

1.40e18

usdcToleranceBps

uint256

USDC tolerance (BPS)

200 (2%)

State Variables

Variable
Type
Description

lastValidEurUsdPrice

uint256

Last valid EUR/USD price

lastPriceUpdateTime

uint256

Last update timestamp

lastPriceUpdateBlock

uint256

Last update block

circuitBreakerTriggered

bool

Circuit breaker state

devModeEnabled

bool

Development mode active


πŸ”„ Price Retrieval

getEurUsdPrice Function

Validation Flow

Circuit Breaker

If validation fails:


⏱️ Timestamp Validation

Manipulation Protection

Miners can slightly manipulate block.timestamp. The contract implements two protections:

1. Timestamp Drift Check

2. Block-Based Staleness

Why Both?

Method
Advantage
Limitation

Timestamp

Temporal precision

Manipulable by miners

Block number

Not manipulable

Varies by chain

Combining both provides robust protection.


πŸ§ͺ Dev Mode

What is Dev Mode?

devModeEnabled disables certain validations to facilitate testing and development.

Dev Mode Impact

Validation
Normal Mode
Dev Mode

Timestamp staleness

βœ… Active

βœ… Active

Price bounds

βœ… Active

βœ… Active

Price deviation (5%)

βœ… Active

❌ Disabled

When to Use?

  • Local tests (Anvil/Hardhat)

  • Testnets with simulated prices

  • Edge case debugging

⚠️ WARNING: Dev Mode should NEVER be enabled in production on mainnet.


🚨 Circuit Breaker

Triggers

The circuit breaker is automatically triggered when:

  1. Price out of bounds: price < minEurUsdPrice or price > maxEurUsdPrice

  2. Excessive deviation: |price - lastPrice| > 5% (if devMode off)

  3. Stale data: Oracle hasn't been updated for 1h

  4. Invalid timestamp: Data in the future

Manual Trigger

Reset

Reset Conditions:

  • Oracle must provide fresh data

  • Price must be within bounds

  • Must be called manually after investigation


πŸ“Š USDC Validation

The contract also validates that USDC stays close to $1.00:

Why This Matters?

  • USDC is the protocol's collateral

  • A USDC depeg directly affects collateral value

  • The protocol must react immediately in case of depeg


πŸ“Š View Functions

Oracle Status

Health Check

Price Info


βš™οΈ Configuration

Update Price Bounds

Validations:

  • _minPrice > 0

  • _maxPrice > _minPrice

  • No radical changes (guardrails)

Update USDC Tolerance

Update Price Feeds

⚠️ Changing price feeds is a critical operation that should go through a timelock.


πŸ›‘οΈ Security

Implemented Protections

Protection
Description

Staleness Check

Price > 1h = stale

Timestamp Drift

No future prices

Block Staleness

Double-check based on blocks

Price Bounds

EUR/USD between 0.80 and 1.40

Deviation Limit

Max 5% between updates

USDC Validation

USDC must stay ~$1.00

Circuit Breaker

Automatic fallback

Role-Based Access

Permission separation

Recovery Functions


πŸ“‹ Events


πŸ“ Example Scenarios

Scenario 1: Normal Price

Scenario 2: Stale Price

Scenario 3: Flash Crash

Scenario 4: USDC Depeg


πŸ”— Protocol Integration


Summary: The ChainlinkOracle provides EUR/USD and USDC/USD prices to the protocol with robust validations. Security mechanisms (staleness, bounds, deviation, circuit breaker) protect against manipulation and corrupted data. Dev Mode enables testing but should never be enabled in production.

Last updated