Skip to content
Verified contractExactMIT

ED1ChronicleAdapter

Address0xae8b66393d0e119e8297a40acbdf89ca62c20264
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.33

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.33+commit.64118f21
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,190,727
Creation transaction
0xf8f2e3b36d…390e989d67

ED1ChronicleAdapter.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.33;

/// @notice Oracle surface of the ED1Hub consumed by the adapters.
interface IED1HubOracle {
    function latestRoundData(address token)
        external
        view
        returns (uint80, int256, uint256, uint256, uint80);
}

/**
 * @title  ED1ChronicleAdapter — Chronicle Protocol `IChronicle` reader (immutable, fail-closed)
 * @notice Thin, immutable, read-only adapter exposing the ED1Hub price of ONE
 *         E/D1 token with Chronicle conventions (Spark / MakerDAO ecosystem):
 *
 *           · wat()             → pair identifier, e.g. bytes32("ED1USD/USD")
 *           · decimals()        → 18 (Chronicle Scribe convention)
 *           · read()            → value (18 decimals); reverts if missing/stale
 *           · readWithAge()     → (value, age); reverts if missing/stale
 *           · tryRead()         → (isValid, value); never reverts
 *           · tryReadWithAge()  → (isValid, value, age); never reverts
 *           · peek()            → (value, isValid); MakerDAO compatibility
 *
 *         `age` follows the Chronicle specification: it is the UNIX TIMESTAMP of
 *         the last update (elapsed seconds = block.timestamp - age), NOT the
 *         elapsed time. This differs from ED1Api3Adapter.readWithAge on purpose.
 *
 *         Chronicle is served by its own contract because its readers expect
 *         `decimals() == 18` and `read()` returning `uint256`, which clash with
 *         the Chainlink (8 decimals) and API3 (`read() → (int224,uint32)`)
 *         surfaces exposed by ED1OracleBridge and ED1Api3Adapter.
 *
 *         FAIL-CLOSED POLICY: the reverting functions validate round integrity
 *         (roundId != 0, answeredInRound >= roundId), positive price, sane
 *         timestamp (non-zero, not in the future), staleness against the
 *         immutable maxAge and overflow-checked scaling. The tryRead, tryReadWithAge
 *         and peek variants apply the SAME validation but report `false` instead of reverting, as
 *         Chronicle documents for deprecated/offline oracles.
 *
 * @custom:website          https://ed1.network
 * @custom:security-contact [email protected]
 */
contract ED1ChronicleAdapter {
    error InvalidAddress();
    error InvalidMaxAge();
    error InvalidLabel();
    error InvalidRound();
    error InvalidPrice();
    error InvalidTimestamp();
    error StalePrice(uint256 age, uint256 maxAge);
    error PriceOverflow();

    /// @notice Chronicle Scribe convention: values use 18 decimals.
    uint8 public constant decimals = 18;

    /// @notice Hub holding the price registry.
    IED1HubOracle public immutable hub;
    /// @notice The E/D1 core token this adapter quotes.
    address public immutable token;
    /// @notice Maximum accepted price age, enforced on EVERY read.
    uint256 public immutable maxAge;
    /// @notice Pair identifier, e.g. bytes32("ED1USD/USD").
    bytes32 public immutable wat;

    /// @dev Hub answers use 8 decimals; Chronicle values use 18 → scale by 1e10.
    uint256 private constant SCALE = 1e10;

    /// @param hub_    ED1Hub proxy address.
    /// @param token_  ED1 core token address.
    /// @param maxAge_ Staleness limit in seconds (e.g. 86400 = 24h).
    /// @param wat_    Pair identifier, e.g. bytes32("ED1USD/USD").
    constructor(address hub_, address token_, uint256 maxAge_, bytes32 wat_) {
        if (hub_ == address(0) || token_ == address(0)) revert InvalidAddress();
        if (hub_.code.length == 0) revert InvalidAddress();
        if (maxAge_ == 0) revert InvalidMaxAge();
        if (wat_ == bytes32(0)) revert InvalidLabel();
        hub = IED1HubOracle(hub_);
        token = token_;
        maxAge = maxAge_;
        wat = wat_;
    }

    /// @dev Never reverts. Returns the validated 18-decimal value and its
    ///      publication timestamp, or `valid == false`.
    function _try() private view returns (bool valid, uint256 value, uint256 updatedAt) {
        try hub.latestRoundData(token) returns (uint80 rid, int256 ans, uint256, uint256 upd, uint80 air) {
            if (rid == 0 || air < rid || ans <= 0 || upd == 0 || upd > block.timestamp) return (false, 0, 0);
            if (block.timestamp - upd > maxAge) return (false, 0, 0);
            uint256 px = uint256(ans);
            if (px > type(uint256).max / SCALE) return (false, 0, 0);
            return (true, px * SCALE, upd);
        } catch {
            return (false, 0, 0);
        }
    }

    /// @dev Fail-closed: propagates Hub errors and reverts with typed errors.
    function _read() private view returns (uint256 value, uint256 updatedAt) {
        (uint80 rid, int256 ans,, uint256 upd, uint80 air) = hub.latestRoundData(token);
        if (rid == 0 || air < rid) revert InvalidRound();
        if (ans <= 0) revert InvalidPrice();
        if (upd == 0 || upd > block.timestamp) revert InvalidTimestamp();
        uint256 elapsed = block.timestamp - upd;
        if (elapsed > maxAge) revert StalePrice(elapsed, maxAge);
        uint256 px = uint256(ans);
        if (px > type(uint256).max / SCALE) revert PriceOverflow();
        return (px * SCALE, upd);
    }

    /// @notice Current value with 18 decimals. Reverts if missing, malformed or stale.
    function read() external view returns (uint256 value) {
        (value,) = _read();
    }

    /// @notice Current value and the UNIX timestamp of its publication (Chronicle `age`).
    function readWithAge() external view returns (uint256 value, uint256 age) {
        (value, age) = _read();
    }

    /// @notice Never reverts: (false, 0) when missing, malformed or stale.
    function tryRead() external view returns (bool isValid, uint256 value) {
        (isValid, value,) = _try();
    }

    /// @notice Never reverts: (false, 0, 0) when missing, malformed or stale.
    function tryReadWithAge() external view returns (bool isValid, uint256 value, uint256 age) {
        (isValid, value, age) = _try();
    }

    /// @notice MakerDAO convention: (value with 18 decimals, isValid). Never reverts.
    function peek() external view returns (uint256 value, bool isValid) {
        (isValid, value,) = _try();
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.