I'm building a simple lending dApp. I need to know the price of ETH to calculate collateral. If the blockchain is a closed system, how can it "know" the external market price securely? If the price feed is manipulated, could my users' funds be drained by a flash loan attack?
3 answers
The "Oracle Problem" refers to the fact that blockchains cannot natively pull data from the internet. To solve this, you use a Decentralized Oracle Network (DON) like Chainlink. Instead of one single source, a DON aggregates price data from hundreds of different exchanges and nodes. They use a consensus mechanism to agree on the "true" price before sending it to your smart contract. This prevents a single point of failure or manipulation. If one exchange has a price spike due to low liquidity, the Oracle filters it out as an outlier, protecting your lending protocol from liquidating users unfairly.
Even with multiple sources, couldn't an attacker use a "Flash Loan" to temporarily pump the price on those exchanges right before the Oracle updates? How do we defend against that?
Using a "Pull" oracle where the user pays the gas to update the price right when they need it can also help keep the data as fresh as possible.
I agree, Ralph. "On-demand" updates are becoming the standard for high-frequency DeFi to ensure that the collateral calculations are always based on the most recent market data.
To defend against Flash Loan manipulation, you shouldn't use "spot prices" from a single DEX. Instead, use a "Time-Weighted Average Price" (TWAP) or a decentralized Oracle that pulls from off-chain high-liquidity exchanges. These sources are much harder to manipulate because the attacker would need enough capital to move the entire global market, not just one pool. Most modern DeFi hacks happen because developers used a "naive" oracle that only looked at one local price source.