Advanced DeFi & market structure

price oracle feed

A blockchain cannot see the outside world: a smart contract has no way to know what ETH costs on a global market, only what is written on-chain. A price oracle feed is the bridge that brings off-chain prices on-chain so that lending protocols can value collateral, perpetuals can compute funding, and liquidations can know when to fire. Because so much money depends on these numbers, the feed itself is one of the most security-critical and most attacked components in all of DeFi.

There are two broad architectures. A decentralized oracle network like Chainlink has many independent node operators each fetch a price from several exchanges, then aggregates their reports on-chain (taking a median to resist a few bad actors) and pushes the result into a contract that other protocols read. The opposing approach is an on-chain oracle such as a time-weighted average price (TWAP) derived from a DEX like Uniswap: instead of trusting external reporters, it computes an average of a pool's own price over a window of time, which is cheap and fully on-chain but lags the real price. There is also a pull model (used by Pyth) where signed price updates are fetched and posted by the user at the moment of use.

Every feed faces the same two failure modes, and good design trades them off. Manipulation: a spot price read from a single thin pool can be shoved by a flash loan in one block, which is why protocols prefer aggregated feeds or time-averaged prices that cannot be moved instantaneously. Staleness and latency: a feed that updates only when the price moves a threshold (a deviation trigger) or every fixed interval (a heartbeat) can lag a fast crash, so contracts must check the timestamp and reject prices that are too old. The recurring lesson behind countless DeFi exploits is blunt: never use a raw, single-source spot price for any decision that controls funds.

A TWAP resists manipulation by averaging over time, but that same averaging makes it lag — so it is safer against flash-loan attacks yet more dangerous during a genuine fast crash, when it may value collateral far above its real, plummeting price. There is no single 'best' oracle, only the right trade-off for the risk.