Quiz Entry - updated: 2026.07.30
How do you read the ETH/USD price with Chainlink's AggregatorV3Interface, and why must you handle decimals?
You point an AggregatorV3Interface at the feed's address and call latestRoundData(); the price comes back as an int scaled by the feed's decimals (typically 8), so you multiply to match wei's 18 decimals.
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x...); // feed address
(, int256 price, , , ) = priceFeed.latestRoundData();
// price of ETH/USD, but with 8 decimals, e.g. 300000000000 for 3000.00000000
return uint256(price) * 1e10; // scale 8 -> 18 decimals
latestRoundData() returns several values; the one you want is price. Two subtleties:
- Decimals — Solidity has no fractional numbers, so the feed encodes the price as an integer with a fixed number of decimal places (ETH/USD uses 8).
msg.valueuses 18. To compare or multiply them you scale the price up by1e10so both use 18 decimals. The feed'sdecimals()function tells you the exact count. - Signed type —
priceisint256(feeds could in principle be negative), so you typecast it touint256withuint256(price).
Go deeper:
Chainlink docs: using data feeds — reading
latestRoundData()and handling the feed's decimals.