What is an oracle in smart contracts?

An oracle is the source a contract trusts for data it can't work out on its own, most often the price of an asset. Lending markets use it to value collateral, perpetual exchanges to settle positions and vaults to price what they hold, so a wrong answer from the oracle becomes a wrong loan, liquidation or share price.

Last updated . 2 minute read. By the QuillAudits Team.

What kinds of price oracles do contracts use?

Source How it works Typical failure
Push feed An off-chain network writes the price on chain when it moves past a threshold or a set time passes Stale answers, wrong decimals, or a feed for the wrong asset
Pool spot price The contract divides one pool reserve by the other One large swap or a flash loan moves it within a transaction
TWAP An average of the price in a pool over a time window A short window over a thin pool moves within a few blocks
Your own contracts The share price of an ERC-4626 vault, or the value of an LP token Donations and rounding while the vault or pool is young

Chainlink describes its feeds as updating when the off-chain price deviates from the last on-chain price, or after a heartbeat period passes (Chainlink). Its latestRoundData returns the answer with startedAt and updatedAt timestamps, and marks answeredInRound as deprecated (Chainlink).

What should a contract check when it reads a price?

  • The answer is positive and scaled by the decimals() of that feed, not an assumed 8 or 18.
  • updatedAt falls within the heartbeat of the feed, and older answers revert.
  • The feed prices the asset you mean, not a related token or a ratio between two.
  • On a rollup, the L2 sequencer uptime feed says the sequencer is up, as the Chainlink example checks (Chainlink).
  • Moving the price costs more than the borrowing it would allow.
  • There is a plan for when the oracle stops: pause, fall back or reject.

The oracle price manipulation page has vulnerable and fixed code for a lender that reads a price, and the TWAP entry covers averaged prices.

A stale price is a typical oracle finding: latestRoundData() is called but updatedAt is never compared with the heartbeat, so if the feed stops updating, the market keeps lending against the last price it saw. The fix reverts on a stale answer and stops new borrowing until the feed recovers.

FAQ

What is the oracle problem?

A blockchain can only check data that is already on chain, so any outside fact, such as a price, has to be brought in by someone the contract trusts. The design question is how to make abusing that trust cost more than it pays.

Sources

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts