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

  1. Reentrancy: request a withdrawal

    The attacker's contract asks a vulnerable contract to send funds.

  2. Reentrancy: call back early

    Before the balance is updated, the attacker's contract calls withdraw again.

  3. Reentrancy: repeat

    Reentrancy lets the attacker re-enter a function again and again before the first call completes.2

  4. Oracle: borrow big

    The attacker takes a flash loan to get a large, temporary pile of tokens.

  5. Oracle: move the price

    They trade in a thin pool so a spot-price oracle reports a distorted value.

  6. 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 ETH

A hypothetical vault holds 100 ETH. An attacker deposits 1 ETH and re-enters withdraw 5 times before the balance updates.

Example 1 — Reentrancy drain — calculation
Withdrawn5 × 1 ETH = 5 ETH
Attacker gain5 − 1 = 4 ETH
Vault loss4 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 debt

A hypothetical lender values a token at $1 using one pool. An attacker pushes that pool's price to $3 and posts 10,000 tokens.

Example 2 — Inflated collateral — calculation
Real collateral value10,000 × $1 = $10,000
Reported value10,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.07

A hypothetical spike lifts the spot price from $1 to $3 for 1 minute; the oracle uses a 30-minute average.

Example 3 — TWAP dampens the spike — calculation
Spike weight1 ÷ 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

AspectReentrancyOracle manipulation
What is abusedOrder of state updatesThe price feed
Typical toolA malicious contractA flash loan
Main defenceChecks-effects-interactionsDecentralized or time-averaged oracles
Typical targetVaults and withdraw functionsLending 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

The oracle problem

Advanced

The oracle problem is trusting outside data: how can a contract know the price came from the right source and wasn't altered?3 A common answer is a decentralised oracle network that pulls from many sources.1

Common mistakes

  1. Equating audit with safety

    Audited code has still been exploited.

  2. Ignoring the oracle

    A protocol is only as sound as its price feed.

  3. Chasing yield blindly

    Very high yields can hide fragile designs.

  4. 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.

Q1What does the checks-effects-interactions pattern defend against?
Q2An attacker re-enters withdraw 3 times on a 2 ETH deposit. How much is taken in total?

Key points to remember

  1. Reentrancy re-enters a function before balances update.
  2. Oracle manipulation feeds a contract a false price.
  3. Flash loans make price attacks cheap to fund.
  4. Checks-effects-interactions and TWAPs are key defences.
  5. DeFi deposits carry smart contract risk.

Frequently asked questions

Can I protect myself from DeFi exploits?

You cannot remove the risk, but you can favour audited, time-tested protocols and limit how much you deposit.

Is a flash loan an attack?

No. A flash loan is a tool; it becomes part of an attack when used to distort prices.

Who pays when a protocol is exploited?

Usually depositors, unless the protocol has insurance or chooses to compensate users.

Further learning

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.

  1. ethereum.org. Smart contract security (opens in a new tab) Checked .
  2. European Supervisory Authorities (EBA and ESMA). Joint Report on recent developments in crypto-assets (Article 142 MiCAR, 2025) (opens in a new tab) Checked .
  3. 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.