跳到主要内容

currentPrice

Contract: JBChainlinkV3PriceFeed​‌

Interface: IJBPriceFeed

Gets the current price from the feed, normalized to the specified number of decimals.

Definition

function currentPrice(uint256 _decimals) external view override returns (uint256)  { ... }
  • Arguments:
    • _decimals is the number of decimals the returned fixed point price should include.
  • The view function can be accessed externally by anyone.
  • The view function does not alter state on the blockchain.
  • The function overrides a function definition from the IJBPriceFeed interface.
  • The function returns the current price of the feed, as a fixed point number with the specified number of decimals.

Body

  1. Get the latest price being reported by the price feed. The latestRoundData function returns several feed parameters, but only the _price is needed.

    // Get the latest round information.
    (uint80 roundId, int256 _price, , uint256 updatedAt, uint80 answeredInRound) = feed
    .latestRoundData();

    Internal references:

    External references:

  2. Make sure the reported price is not from a previous round.

    // Make sure the price isn't stale.
    if (answeredInRound < roundId) revert STALE_PRICE();
  3. Make sure the round has finished.

    // Make sure the round is finished.
    if (updatedAt == 0) revert INCOMPLETE_ROUND();
  4. Make sure the price isn't negative.

    // Make sure the price is positive.
    if (_price < 0) revert NEGATIVE_PRICE();
  5. Get the number of decimals being reported by the price feed that the provided price is expected to have.

    // Get a reference to the number of decimals the feed uses.
    uint256 _feedDecimals = feed.decimals();

    Internal references:

    External references:

  6. Return the fixed point price after normalizing the value to the desired number of decimals.

    // Return the price, adjusted to the target decimals.
    return uint256(_price).adjustDecimals(_feedDecimals, _decimals);

    Library references: