currentPrice
Contract: JBChainlinkV3PriceFeed
Interface: IJBPriceFeed
- Step by step
- Code
- Errors
- Bug bounty
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
-
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:
-
Make sure the reported price is not from a previous round.
// Make sure the price isn't stale.
if (answeredInRound < roundId) revert STALE_PRICE(); -
Make sure the round has finished.
// Make sure the round is finished.
if (updatedAt == 0) revert INCOMPLETE_ROUND(); -
Make sure the price isn't negative.
// Make sure the price is positive.
if (_price < 0) revert NEGATIVE_PRICE(); -
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:
-
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:
JBFixedPointNumber
.adjustDecimals(...)
/**
@notice
Gets the current price from the feed, normalized to the specified number of decimals.
@param _decimals The number of decimals the returned fixed point price should include.
@return The current price of the feed, as a fixed point number with the specified number of decimals.
*/
function currentPrice(uint256 _decimals) external view override returns (uint256) {
// Get the latest round information.
(uint80 roundId, int256 _price, , uint256 updatedAt, uint80 answeredInRound) = feed
.latestRoundData();
// Make sure the price isn't stale.
if (answeredInRound < roundId) revert STALE_PRICE();
// Make sure the round is finished.
if (updatedAt == 0) revert INCOMPLETE_ROUND();
// Make sure the price is positive.
if (_price < 0) revert NEGATIVE_PRICE();
// Get a reference to the number of decimals the feed uses.
uint256 _feedDecimals = feed.decimals();
// Return the price, adjusted to the target decimals.
return uint256(_price).adjustDecimals(_feedDecimals, _decimals);
}
String | Description |
---|---|
STALE_PRICE | Thrown if the price was reported in a previous round. |
INCOMPLETE_ROUND | Thrown if the price was reported during a round that hasn't finished yet. |
NEGATIVE_PRICE | Thrown if the reported price is negative. |
Category | Description | Reward |
---|---|---|
Optimization | Help make this operation more efficient. | 0.5ETH |
Low severity | Identify a vulnerability in this operation that could lead to an inconvenience for a user of the protocol or for a protocol developer. | 1ETH |
High severity | Identify a vulnerability in this operation that could lead to data corruption or loss of funds. | 5+ETH |