Skip to main content

priceFor

Contract: JBPrices​‌

Interface: IJBPrices

Gets the number of _currency units that can be converted to 1 _base unit.

Definition

function priceFor(
uint256 _currency,
uint256 _base,
uint256 _decimals
) external view override returns (uint256) { ... }
  • Arguments:
    • _currency is the currency units the feed's resulting price is in terms of.
    • _base is the base currency unit being priced by the feed.
    • _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 IJBPrices interface.
  • The function returns the price of the currency in terms of the base, as a fixed point number with the specified number of decimals.

Body

  1. Return 1 if the currency and the base are the same, since they have the same price. Normalize to the desired number of decimals.

    // If the currency is the base, return 1 since they are priced the same. Include the desired number of decimals.
    if (_currency == _base) return 10**_decimals;
  2. Get a reference to the feed.

    // Get a reference to the feed.
    IJBPriceFeed _feed = feedFor[_currency][_base];

    Internal references:

  3. If the feed exists, return the price that it's currently reporting.

    // If it exists, return the price.
    if (_feed != IJBPriceFeed(address(0))) return _feed.currentPrice(_decimals);

    External references:

  4. Get a reference to the feed using the inverse pair.

    // Get the inverse feed.
    _feed = feedFor[_base][_currency];

    Internal references:

  5. If the inverse feed exists, return the inverse of the price that it's currently reporting.

    // If it exists, return the inverse price.
    if (_feed != IJBPriceFeed(address(0)))
    return PRBMath.mulDiv(10**_decimals, 10**_decimals, _feed.currentPrice(_decimals));

    Library references:

    External references:

  6. Revert if no feed was found.

    // No price feed available, revert.
    revert PRICE_FEED_NOT_FOUND();