Blockchain technology · · 6 min read
What is a blockchain oracle? Bringing outside data on-chain
An oracle is a system that brings information from outside the blockchain, such as an asset's price or the result of a match, onto the chain in a form smart contracts can read. Because smart contracts cannot see beyond the chain on their own, most lending, stablecoin and derivatives protocols depend on oracles; if an oracle supplies wrong data, the contract will carry out the wrong decision flawlessly.
This dependency is known as the "oracle problem". Transactions on the chain may be decentralized and verifiable, but data coming from outside is only as trustworthy as the mechanism that delivers it.
Why smart contracts can't see outside
Every node in a blockchain has to run the same transactions and reach the same result. If a contract pulled a price from a website while executing, nodes would do so at different moments, get different answers and break consensus. That is why contracts can only read data already recorded on the chain. Outside data has to be written to the chain through a transaction, and the oracle is the party that does this.
Where oracles are used
- Lending protocols: The value of collateral is calculated with the oracle price. If it falls below a certain threshold, the position becomes open to liquidation.
- Crypto-collateralized stablecoins: Whether the collateral is sufficient is monitored with oracle prices.
- On-chain derivatives: Liquidation and funding calculations for futures contracts rely on an external price.
- Prediction markets and insurance: The outcome of an event is the data that determines the payout.
- Random numbers: Games and lottery-style apps use dedicated oracle services for random numbers that cannot be predicted in advance and can be verified.
Types of oracles
| Type | How it works | Strength | Weakness |
|---|---|---|---|
| Centralized oracle | A single entity signs the data and writes it on-chain | Simple and fast | A single point of failure and trust |
| Decentralized oracle network | Independent nodes collect data from many sources, and the results are combined with a method such as the median | Resilient to one source going bad | More complex and costly |
| On-chain price | The price is read from a decentralized exchange (DEX) pool | No need to trust an outside party | Easy to manipulate in thin pools |
| First-party oracle | The entity that produces the data, such as an exchange, publishes it itself | Data closest to the source | Depends on the source's honesty |
Updates can also work in two ways. In the push model, the oracle writes the price on-chain itself under certain conditions, for example when the price moves by more than 0.5%, or once an hour even if it doesn't. In the pull model, signed prices are produced continuously off-chain, and whoever needs one brings it on-chain with their own transaction. The first pays a transaction fee for every update; the second can offer fresher data but leaves the job of fetching it to the user.
How oracle manipulation happens
The best-known type of attack targets protocols that read the price from a single, thin source at a single moment. A tool often used in such attacks is the flash loan: an uncollateralized loan that must be borrowed and repaid within the same transaction. A hypothetical example:
- A lending protocol reads the price of token X from the spot ratio in a single DEX pool. X is worth $1.
- The attacker holds 500,000 X, with a real value of $500,000.
- Within a single transaction, the attacker takes out a flash loan of a large amount of stablecoins, buys X from the pool and temporarily pushes its price to $4.
- The protocol values the 500,000 X the attacker deposits as collateral at $2,000,000 and lends 75% of that, or $1,500,000.
- The attacker sells the X bought from the pool back into it, the price returns to around $1, and the flash loan is repaid.
The protocol is left with $1,500,000 of debt that will never be repaid, backed by collateral really worth about $500,000. Before trading costs, the attacker has made about $1 million, and the protocol and its users absorb the hole. Attacks of this kind hit a number of protocols in DeFi's early years.
Beyond manipulation, the main risks are:
- Stale data: If updates stop or lag, the contract operates on an old price. In sharp moves, this can lead to late liquidations or unfair ones.
- A faulty source: A momentary price error on a single exchange flows straight into a single-source oracle.
- Centralization: If the keys that update the oracle are held by a few people, those people are the protocol's de facto operators.
- Fixed-price assumptions: A protocol that always treats a stablecoin as worth $1 stops reflecting reality when the peg breaks.
Defenses, and questions to ask as a user
- Multiple sources and the median: Taking the price from many exchanges and using the median neutralizes a single source going astray.
- Time-weighted average price (TWAP): Reading the price as an average over, say, the last 30 minutes instead of a single moment makes single-transaction manipulation very expensive. The trade-off is that the price reflects real moves with a delay.
- Deviation limits: Pausing activity, or checking against a second source, when the price changes unusually fast.
- Freshness checks: Rejecting a price that hasn't been updated for a set period.
- Low limits for thin assets: Keeping borrowing limits low when accepting illiquid tokens as collateral.
Before using a DeFi protocol, you can also ask a few questions: How many sources does the price come from? Are the assets accepted as collateral liquid enough? Who can change the oracle settings, and is there a waiting period for changes? Has the protocol had oracle-related problems before?
Key takeaways
- An oracle brings outside data onto the chain in a form smart contracts can read.
- Smart contracts can't query the outside world directly without breaking consensus, so much of DeFi depends on oracles.
- Centralized, decentralized, on-chain and first-party oracles come with different trust and cost trade-offs.
- Oracles relying on a single thin source can be manipulated with flash loans; stale data and centralization are the other main risks.
- Multi-source medians, TWAP, deviation limits and freshness checks are the main defenses.