Reentrancy and Oracle ManipulationTWAP
Two common smart-contract exploits. A reentrancy attack calls back into a contract before its first execution has finished, for example to withdraw funds twice. Oracle manipulation feeds a protocol a false price — often by briefly moving a thin market — so it lends or liquidates on wrong data.
Reentrancy and oracle manipulation are two of the most common ways DeFi protocols lose money. Reentrancy abuses the order in which a contract updates its records. Oracle manipulation feeds a contract a distorted price. Traders who deposit into DeFi carry these risks even if they never write code.
- Category
- Cryptocurrency and Blockchain
- Reading time
- 4 min
- Also called
- Smart contract exploits, DeFi hacks, Reentrancy attack
- Updated
The two exploits in plain words
In a reentrancy attack, a hostile contract calls back into a vulnerable contract while that contract's first call is still running.1
Oracle manipulation targets the price feed instead — for example, using a flash loan to pump an asset's spot price just before using a lending protocol.1
Both tricks make a contract act on information that is wrong at the moment it matters.
How each attack works
Reentrancy: request a withdrawal
The attacker's contract asks a vulnerable contract to send funds.
Reentrancy: call back early
Before the balance is updated, the attacker's contract calls withdraw again.
Reentrancy: repeat
Reentrancy lets the attacker re-enter a function again and again before the first call completes.2
Oracle: borrow big
The attacker takes a flash loan to get a large, temporary pile of tokens.
Oracle: move the price
They trade in a thin pool so a spot-price oracle reports a distorted value.
Oracle: exploit and repay
Flash loan attacks on weak price oracles have cost DeFi protocols millions.1
Worked examples
Example 1 — Reentrancy drain
Vault loses 4 ETHA hypothetical vault holds 100 ETH. An attacker deposits 1 ETH and re-enters withdraw 5 times before the balance updates.
| Withdrawn | 5 × 1 ETH = 5 ETH |
| Attacker gain | 5 − 1 = 4 ETH |
| Vault loss | 4 ETH |
The attacker's contract kept calling back before the balance updated, so the vulnerable contract paid out again and again.1
Example 2 — Inflated collateral
$10,000 bad debtA hypothetical lender values a token at $1 using one pool. An attacker pushes that pool's price to $3 and posts 10,000 tokens.
| Real collateral value | 10,000 × $1 = $10,000 |
| Reported value | 10,000 × $3 = $30,000 |
| Over-borrowing at 50% LTV | $15,000 − $5,000 = $10,000 |
A flash loan briefly inflated the spot price, and the lending protocol trusted it.1
Example 3 — TWAP dampens the spike
Oracle reads about $1.07A hypothetical spike lifts the spot price from $1 to $3 for 1 minute; the oracle uses a 30-minute average.
| Spike weight | 1 ÷ 30 ≈ 3.3% |
| Average price | $1 + ($2 × 3.3%) ≈ $1.07 |
A time-weighted average price (TWAP) oracle is much harder to push around with a single big trade.1
Reentrancy vs oracle manipulation
| Aspect | Reentrancy | Oracle manipulation |
|---|---|---|
| What is abused | Order of state updates | The price feed |
| Typical tool | A malicious contract | A flash loan |
| Main defence | Checks-effects-interactions | Decentralized or time-averaged oracles |
| Typical target | Vaults and withdraw functions | Lending and derivatives protocols |
Why traders should care
Choosing protocols
Audit history adds to Counterparty Risk checks.
Sizing deposits
Treat DeFi deposits as Exposure that can go to zero.
Liquidation risk
Bad oracle prices can trigger unfair Liquidation.
Spreading risk
Diversification across protocols limits one exploit's damage.
Advantages and disadvantages
Advantages
- Known patterns make audits more effective.
- Defences such as TWAPs are well documented.
- Understanding exploits improves protocol choice.
Disadvantages
- Losses can be large and instant.
- Audits cannot rule out every bug.
- Users may have no way to recover funds.
- Complex protocols combine many attack surfaces.
Going deeper
Read as far as you need. Each level builds on the one before it.
Why old bugs keep returning
Beginner
Reentrancy is still a serious risk, because contracts call each other constantly and one missed check is enough.1
The standard reentrancy fix
Intermediate
The standard defence is the checks-effects-interactions pattern: check conditions, update balances, and only then send funds.1
Common mistakes
Equating audit with safety
Audited code has still been exploited.
Ignoring the oracle
A protocol is only as sound as its price feed.
Chasing yield blindly
Very high yields can hide fragile designs.
Putting everything in one protocol
A single exploit then hits the whole stake.
Check your understanding
Test yourself before moving on. Each answer comes with an explanation.
Key points to remember
- Reentrancy re-enters a function before balances update.
- Oracle manipulation feeds a contract a false price.
- Flash loans make price attacks cheap to fund.
- Checks-effects-interactions and TWAPs are key defences.
- DeFi deposits carry smart contract risk.
Frequently asked questions
Can I protect myself from DeFi exploits?
Is a flash loan an attack?
Who pays when a protocol is exploited?
Further learning
- Counterparty Risk
Assessing protocol and code risk.
- Liquidation
How bad prices trigger liquidations.
- Diversification
Spreading deposits across protocols.
- Liquidity
Why thin pools are easy to move.
Sources
This article is written in our own words. The sources below support the specific facts, rules and figures marked with a number in the text. Links open the original publisher’s page.
- ethereum.org. Smart contract security (opens in a new tab) Checked .
- European Supervisory Authorities (EBA and ESMA). Joint Report on recent developments in crypto-assets (Article 142 MiCAR, 2025) (opens in a new tab) Checked .
- ethereum.org. Oracles (opens in a new tab) Checked .
Educational content, not financial advice. Examples use hypothetical prices and simplified costs; actual fees, margin rules and regulations vary by broker, market and country. We are not affiliated with any publisher linked above. Last reviewed . Report an error or request a source credit.