Module 3: How Blockchains and Transactions Work

Oracles, How Blockchains Learn About the World

Chapter 12 of 13 · about 6 minutes

12.1 The football bet

Back in Module 1, Chapter 4, Mint and Node made a football bet using a smart contract. The contract would pay the winner automatically when the result was published. Mint had never thought about the last part of that sentence.

"Wait," said Mint. "How does the smart contract actually know who won the match?"

"It doesn't," said Hash. "Blockchains can't see anything outside themselves. Not match results, not the weather, not even the price of ETH on an exchange. Something has to bring that information in. That something is called an oracle, and a surprising number of crypto disasters started with one."

12.2 The oracle problem

A blockchain is a closed system. Every node must be able to verify every transaction and get exactly the same result. If a smart contract could look up a website, different nodes might see different answers at different moments, and they'd never agree. So smart contracts can only use data that's already on the blockchain.

An oracle is a service that brings outside data onto the blockchain, so smart contracts can use it. Oracles supply match results, weather data, interest rates, and above all, prices.

This creates the oracle problem: a smart contract can be perfectly written and completely decentralised, but if it relies on an oracle, it's only as trustworthy as that oracle. Feed it a wrong price, and it will execute flawlessly on the wrong information.

12.3 Why prices matter so much

Many of the most important applications in crypto depend on knowing the current price of assets.

ApplicationWhat it uses prices for
Lending protocolsDeciding how much you can borrow against your collateral, and when to liquidate you
Stablecoins backed by cryptoChecking that collateral is worth enough to back the stablecoins issued
Perpetual futuresCalculating funding, margin, and liquidations (the Perpetual Futures module)
Options and structured productsSettling contracts at expiry
Prediction marketsSettling outcomes of real-world events

If a lending protocol believes your collateral is worth more than it really is, you can borrow too much. If it believes your collateral is worth less, you can be liquidated unfairly.

12.4 How oracles work

There are two main approaches.

Decentralised oracle networks. Many independent operators each gather prices from multiple sources, such as major exchanges. Their answers are combined, typically by taking a median, and published on-chain. Updates usually happen when the price moves beyond a set threshold, or after a set time has passed, called a heartbeat. Relying on many operators and many sources makes manipulation much harder.

On-chain prices from decentralised exchanges. A smart contract can read prices directly from a DEX pool. To make this harder to manipulate, protocols often use a time-weighted average price (TWAP), the average price over a period like 30 minutes, rather than the price at a single moment.

ApproachStrengthsWeaknesses
Decentralised oracle networkDraws on many sources and operators; hard to manipulateRelies on the network's operators; updates aren't continuous
DEX spot priceFully on-chain, always availableVery easy to manipulate within a single transaction
DEX time-weighted averageHarder to manipulate than spot pricesLags fast moves; thin pools can still be manipulated over time

12.5 Flash loans and manipulation

To understand the biggest oracle risk, you need to know about flash loans. A flash loan lets anyone borrow a huge amount of tokens with no collateral, as long as the loan is repaid within the same transaction. If it isn't repaid, the entire transaction reverses, as if it never happened.

Flash loans have legitimate uses, like arbitrage and refinancing debt. But they also mean that anyone, even without capital, can temporarily control enormous sums for the length of one transaction.

Now combine that with a protocol that uses a DEX spot price from a small pool as its oracle.

A simplified attack:

StepActionEffect
1Attacker deposits 100,000 TOKEN into a lending protocol as collateral. Fair price: $1, so $100,000Normal deposit
2In one transaction, attacker takes a flash loan and buys TOKEN aggressively in a thin DEX poolThe pool's price jumps to $5
3The lending protocol reads its price from that poolCollateral now appears to be worth $500,000
4Attacker borrows $375,000 of stablecoins against it, at a 75% loan-to-value limitBorrowed against inflated collateral
5Attacker sells TOKEN back, repays the flash loanThe pool price returns to about $1
6Attacker walks away with the stablecoinsThe protocol is left holding $100,000 of collateral against $375,000 of debt

Chapter 12 · Flash-loan oracle manipulation

Six steps. One: the attacker deposits 100,000 TOKEN, fairly worth $1 each or $100,000, as collateral. Two to five happen in one transaction: take a flash loan and buy TOKEN in a thin DEX pool, pushing its price from $1 to $5; the protocol reads that spot price, so the collateral appears worth $500,000; the attacker borrows $375,000 of stablecoins at 75% loan-to-value; then sells TOKEN back and repays the flash loan as the price returns to about $1. Six: the attacker keeps the $375,000, leaving the protocol with $100,000 of collateral against $375,000 of debt, about $275,000 of bad debt. Beside it, a safeguard panel: a 30-minute TWAP barely moves from a one-transaction spike and an oracle network takes the median of many sources, so the collateral stays valued near $100,000 and the borrow is refused; a caveat notes TWAPs lag fast moves and thin pools can still be pushed over time.

Every contract in the attack works exactly as coded; the flaw is that the lending protocol trusts a price one transaction can move. Averaging over time or across many sources makes that spike nearly invisible, though not impossible to fake.

The protocol now has about $275,000 of bad debt, losses that ultimately fall on its lenders. The smart contract worked exactly as coded. The oracle told it the wrong thing.

"So the bug wasn't in the code," said Node. "It was in what the code trusted."

"That's the oracle problem in one sentence," said Hash.

12.6 A real example

Oracle manipulation has caused many large losses. One well-known case came in October 2022, when a trader manipulated the price of the MNGO token on Mango Markets, a lending and trading platform. By pushing up the price that the platform used to value collateral, the trader borrowed and withdrew over $100 million. The trader later argued it was a legitimate trading strategy. A US court convicted him of fraud, but the conviction was later overturned. The case remains a notable example of oracle and market manipulation risk in DeFi.

12.7 Other oracle risks

Manipulation isn't the only risk.

Stale prices. If an oracle stops updating, or updates too slowly during a crash, protocols may act on outdated prices.

Bad source data. If many sources report a wrong price, for example during a flash crash on a major exchange, oracles can pass that error on.

Centralised control. Some oracles are run by a single party. If that party is compromised or makes a mistake, every protocol relying on it is affected.

Real-world event disputes. For outcomes like elections or sports, there can be genuine ambiguity about the "correct" answer, which prediction markets must resolve through dispute processes.

12.8 What this means for you

When you use a lending protocol, stablecoin, or derivatives platform, part of your risk is its oracle design. Useful questions include:

  1. Which oracle does the protocol use for each asset?
  2. Does it rely on many sources, or a single pool or party?
  3. How often does the price update, and what happens if it stops?
  4. Is the asset you're using thinly traded, and therefore easier to manipulate?

Thinly traded tokens accepted as collateral are a recurring red flag. Many exploits start there.

12.9 Try it yourself

Pick a well-known lending protocol and find its documentation on price oracles. Which oracle does it use for ETH? For a smaller token? Check how often prices update and what the protocol does if an oracle fails.

Then find a public price feed from a decentralised oracle network on a block explorer or the oracle provider's website. Note its update threshold and heartbeat.

Key takeaways from this chapter

  1. Blockchains can't access outside data directly. Oracles bring prices and other information on-chain.
  2. A smart contract is only as reliable as the data it trusts. That's the oracle problem.
  3. Lending, stablecoins, derivatives, and prediction markets all depend heavily on price oracles.
  4. Decentralised oracle networks aggregate many sources. DEX spot prices are easy to manipulate; time-weighted averages help but aren't perfect.
  5. Flash loans let anyone temporarily borrow huge sums within one transaction, which can be used to manipulate weak oracles.
  6. Oracle risks also include stale prices, bad source data, and centralised control.
  7. Thinly traded tokens used as collateral are a recurring red flag for oracle manipulation.