Use Data Feed On-chain

Reading data feeds on-chain

To consume price data, your smart contract should reference IAggregator.sol, which defines the external functions implemented by Data Feeds.

IAggregator.sol

// SPDX-License-Identifier: LGPL-3.0
pragma solidity 0.8.20;

interface IAggregator {
  /**
   * @notice median from the most recent report
   */
  function latestAnswer() external view returns (int256);

  /**
   * @notice timestamp of block in which last report was transmitted
   */
  function latestTimestamp() external view returns (uint256);

  /**
   * @notice Aggregator round (NOT OCR round) in which last report was transmitted
   */
  function latestRound() external view returns (uint256);

  /**
   * @notice median of report from given aggregator round (NOT OCR round)
   * @param _roundId the aggregator round of the target report
   */
  function getAnswer(uint256 _roundId) external view returns (int256);

  /**
   * @notice timestamp of block in which report from given aggregator round was transmitted
   * @param _roundId aggregator round (NOT OCR round) of target report
   */
  function getTimestamp(uint256 _roundId) external view returns (uint256);

  /**
   * @return answers are stored in fixed-point format, with this many digits of precision
   */
  function decimals() external view returns (uint8);

  /**
   * @notice human-readable description of observable this contract is reporting on
   */
  function description() external view returns (string memory);

  /**
   * @notice aggregator contract version
   */
  function version() external view returns (uint256);

  /**
   * @notice details for the given aggregator round
   * @param _roundId target aggregator round (NOT OCR round). Must fit in uint32
   * @return roundId _roundId
   * @return answer median of report from given _roundId
   * @return startedAt timestamp of block in which report from given _roundId was transmitted
   * @return updatedAt timestamp of block in which report from given _roundId was transmitted
   * @return answeredInRound _roundId
   */
  function getRoundData(
    uint80 _roundId
  ) external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);

  /**
   * @notice aggregator details for the most recently transmitted report
   * @return roundId aggregator round of latest report (NOT OCR round)
   * @return answer median of latest report
   * @return startedAt timestamp of block containing latest report
   * @return updatedAt timestamp of block containing latest report
   * @return answeredInRound aggregator round of latest report
   */
  function latestRoundData()
    external
    view
    returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}

Example

Last updated