Oracles: Why DeFi Depends on External Prices

Push and pull oracles, TWAP, price manipulation: understand where the prices used by DeFi come from and what you should check.

DeFi · Intermédiaire · 8 min read · Publié le

The essentials

The oracle problem

A smart contract is a program that runs on the blockchain deterministically: all the network's nodes must get exactly the same result. For this reason, a contract can't go fetch information from the internet itself, such as the price of ETH on an exchange. Two nodes querying a website at two different moments wouldn't get the same answer.

Yet most DeFi applications need prices:

  • A lending protocol has to know whether a borrower's collateral still covers their debt, and liquidate them if not.
  • A crypto-backed stablecoin has to value its collateral.
  • A perpetual contracts platform has to calculate gains, losses and liquidations.

An oracle is the mechanism that publishes data from the outside world onto the blockchain, most often a price. This is what's called the "oracle problem": a protocol's security can't be better than that of the data it receives. You can have an audited contract with no flaw at all; if you feed it a false price, it will act correctly on false information.

The main families of oracles

Push oracles: the price is published continuously

In the push model, a network of operators regularly publishes the price on the blockchain, whether or not anyone needs it. Contracts only have to read the last recorded value.

Chainlink Data Feeds are the best-known illustration. According to its documentation:

  • several independent node operators supply data, which is aggregated and then published onchain;
  • an update is triggered when the price moves beyond a defined threshold (deviation threshold);
  • or when a maximum delay has elapsed without an update (heartbeat), even if the price has barely moved.

Consequence: between two updates, the price read by the contract may differ slightly from the market. This is a design choice, because each publication costs gas fees.

Pull oracles: the price is brought on demand

In the pull model, the price is signed offchain at high frequency, and is only written to the blockchain when an application needs it. It's generally the user or the application that includes the update in its transaction and pays for it.

Pyth Network's documentation explains that pull oracles only update the price onchain when it's requested, that anyone can submit an update (the contract checks that it is authentic), and that this model allows a much higher frequency: Pyth cites updates every 400 milliseconds offchain.

ModelWho publishes the pricePoint of attention
PushThe oracle network, continuouslyPrice potentially lagging between two updates
PullThe user or the app, on demandThe application must actually require a recent price

Neither model is superior in itself. What matters is how the protocol integrates it.

Onchain oracles: the TWAP

A third approach is to read the price directly from a DEX pool. Reading a pool's instantaneous price is dangerous: an actor can move it sharply with a large swap, sometimes funded by a flash loan (a loan borrowed and repaid within the same transaction).

Hence the TWAP (time-weighted average price). Uniswap's documentation explains that v3 pools record a history of observations, and that by comparing two observations you get an average price over the chosen period. To distort a TWAP over a long window, you have to hold an artificial price for a long time, which costs far more than a single swap.

Limits: a TWAP lags by construction (it reflects the past), and it remains manipulable if the pool is illiquid or the window is short.

How an oracle can fail

The risks fall into a few categories:

  • Stale price: the oracle hasn't been updated for a long time. Chainlink's documentation recommends that developers check the timestamp (updatedAt) to make sure the value is recent enough.
  • Extreme volatility: the price goes outside the reasonable bounds provided for. Chainlink also advises monitoring minimum and maximum values.
  • Infrastructure outage: delays or unavailability of nodes, or of the sequencer on a layer 2. For the latter case, Chainlink offers feeds indicating whether the sequencer is running, to allow a grace period and avoid mass liquidations.
  • Source manipulation: if the oracle aggregates illiquid markets, it's enough to manipulate those markets to fool the oracle, even if the oracle itself works perfectly.

This last case is the most instructive.

A concrete example: Mango Markets, October 2022

Mango Markets was a decentralized exchange on Solana offering, among other things, perpetual contracts. According to the CFTC, the US derivatives markets regulator, here is what happened on October 11, 2022:

  1. A trader opened, through two accounts, leveraged positions on MNGO/USDC contracts, MNGO being the platform's governance token.
  2. He then quickly bought large quantities of MNGO on three exchanges that fed the price oracle used by Mango Markets.
  3. According to the oracle, the price of MNGO rose more than 13-fold within 30 minutes.
  4. With the value of his positions artificially inflated, he used them as collateral to borrow and withdraw more than $110 million of assets from the platform.

The CFTC states that about $67 million was subsequently returned. In its January 2023 press release, it presents this case as its first action involving a scheme described as "oracle manipulation." These are the authority's allegations, made as part of its proceedings.

The technical takeaway: the oracle did faithfully relay the price from the markets it observed. The problem was that those markets were illiquid enough to be moved by a single actor, and that the protocol accepted this token as collateral to borrow other assets.

What you can check as a user

You don't write the contracts, but you choose where to deposit your funds. Before using a lending protocol, a perps platform or a stablecoin:

  • Which price source? The documentation should say which oracle is used for each asset (oracle network, pull, TWAP, internal price).
  • What liquidity is behind it? A thinly traded asset is easier to manipulate. Beware of markets where a small token serves as collateral with large borrowing caps.
  • Are there safeguards? Deposit or borrow caps per asset, price bounds, freshness checks, a grace period in case of a sequencer outage.
  • Which price for liquidations? On perps, the mark price (often derived from an oracle) can differ from the last traded price. Know which one triggers your liquidation.
  • Incident history: has the protocol already suffered an oracle manipulation, and what did it change afterward?
  • Audits: do the reports mention the oracle integration?

Mistakes to avoid

  • Assuming the displayed price is the contract's price. The interface may show a market price while the contract uses a slightly different oracle price.
  • Borrowing right at the liquidation threshold. A few seconds' lag or a one-off spike is enough to trigger a liquidation. Keep a buffer.
  • Depositing in an exotic market for yield. Isolated markets on illiquid tokens concentrate manipulation risk.
  • Ignoring layer 2 outages. During a sequencer interruption, you sometimes can't adjust your position while prices move.
  • Believing an audit covers everything. A bug-free contract can be drained if its oracle reads a manipulable market.

This guide is general information and does not constitute investment, legal or tax advice.

Key takeaways

  • A blockchain can't read an external price on its own: oracles supply it, and DeFi's security depends on them.
  • Push oracles publish continuously based on a deviation threshold or a maximum delay; pull oracles bring the price on demand, inside the transaction.
  • The TWAP smooths out one-off manipulations by averaging the price over a period, at the cost of some lag.
  • Mango Markets (October 2022) shows that a faithful oracle can relay a manipulated price if its source markets are illiquid.
  • Before depositing, check the price source, the liquidity of the accepted assets, the safeguards and your margin before liquidation.

Sources

Compare services related to this topic