Spot the Bug: the stale price trap
A contract reads latestRoundData from a Chainlink feed, takes the answer, and returns it as the price. The feed address is correct and the value is non-zero. What is still wrong?
Review and apply
Worked answers
Try the quiz above first, then open an answer to compare your reasoning. These notes are also available to read without completing an attempt.
1. A contract reads latestRoundData from a Chainlink feed, takes the answer, and returns it as the price. The feed address is correct and the value is non-zero. What is still wrong?
- A.Nothing, a correct feed address is enough
- B.It never checks how old the answer is (the updatedAt timestamp is ignored)
- C.external view should be public
- D.The uint cast wastes gas
Answer and explanation
B. It never checks how old the answer is (the updatedAt timestamp is ignored)
latestRoundData returns the answer alongside its updatedAt timestamp and round data. Reading only the answer throws away the field that tells you whether the price is fresh. A trusted feed still needs a freshness guard.
2. The feed stops updating (the aggregator stalls, or on an L2 the sequencer goes down). What does an unchecked latestRoundData call return?
- A.It reverts, so the protocol is safe
- B.It returns zero and callers can special-case that
- C.It keeps returning the last known price as if it were current
- D.It returns a price marked as stale that Solidity rejects automatically
Answer and explanation
C. It keeps returning the last known price as if it were current
A frozen feed does not revert. It keeps handing back the last answer it knew while the real market moves on. Everything priced off it, borrows, liquidations, redemptions, now runs on a number from hours ago.
3. What is the correct fix for the freshness gap?
- A.Read updatedAt and revert if now - updatedAt exceeds that feed's real heartbeat
- B.Require the answer to be greater than zero
- C.Cache the last price and average it with the new one
- D.Wrap the call in try/catch so a revert cannot propagate
Answer and explanation
A. Read updatedAt and revert if now - updatedAt exceeds that feed's real heartbeat
Consume the whole tuple. Compare updatedAt against a staleness threshold set to the actual heartbeat of that exact feed on that exact chain, and check round completeness so a partial round is not accepted. A non-zero check does not catch a stale price, because a frozen feed's last price is still non-zero.
Read alongside: See it in real audits