Oracle price manipulation: how attackers move the price your contract reads
Oracle price manipulation is when an attacker moves the price a contract reads, usually in a thin pool, then borrows or swaps against that false price. A pool's spot price can move inside one transaction, and a short time-weighted average over a thin pool can move within a few blocks. Read prices that are expensive to move, check that they are fresh and within bounds, and size borrowing to the liquidity behind them.
How does oracle price manipulation work?
A lending market values collateral with a price. If that price comes from a pool's reserves, a large trade moves it: buy the collateral token in that pool and its price there goes up. The attacker deposits the token, borrows against the inflated value and leaves the market with bad debt once the price falls back.
Flash loans make the trade cheap, because the capital is borrowed and repaid in the same transaction. Time-weighted average prices, or TWAPs, resist a one-transaction move, but a short window over a thin pool can still be pushed over a few blocks, at a cost the attacker gets back from the loans.
What does vulnerable code look like?
This lender reads its price straight from a pool's reserves.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IPair {
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}
contract SpotPriceLender {
IPair public immutable pair; // token0 is the collateral, token1 a stablecoin
mapping(address => uint256) public collateral;
constructor(IPair pair_) {
pair = pair_;
}
// Price of 1e18 collateral in stablecoin units, read from the pool right now.
function price() public view returns (uint256) {
(uint112 r0, uint112 r1, ) = pair.getReserves();
return (uint256(r1) * 1e18) / uint256(r0); // one large swap moves this
}
function maxBorrow(address user) public view returns (uint256) {
return ((collateral[user] * price()) / 1e18) * 75 / 100;
}
}
One large swap that buys the collateral token raises price(), and maxBorrow rises with it in the same transaction.
How do you fix it?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IPriceFeed {
function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
function decimals() external view returns (uint8);
}
contract FeedPriceLender {
IPriceFeed public immutable feed;
uint256 public immutable maxAge; // seconds: the feed's heartbeat plus a margin
uint256 public immutable minPrice; // 18 decimals, bounds set for this market
uint256 public immutable maxPrice;
mapping(address => uint256) public collateral;
constructor(IPriceFeed feed_, uint256 maxAge_, uint256 minPrice_, uint256 maxPrice_) {
feed = feed_;
maxAge = maxAge_;
minPrice = minPrice_;
maxPrice = maxPrice_;
}
function price() public view returns (uint256 p) {
(, int256 answer, , uint256 updatedAt, ) = feed.latestRoundData();
require(answer > 0, "bad price");
require(block.timestamp - updatedAt <= maxAge, "stale price");
p = (uint256(answer) * 1e18) / (10 ** uint256(feed.decimals()));
require(p >= minPrice && p <= maxPrice, "price out of bounds");
}
function maxBorrow(address user) public view returns (uint256) {
return ((collateral[user] * price()) / 1e18) * 75 / 100;
}
}
- Read a price that is expensive to move. A feed that aggregates many markets, such as a Chainlink price feed, can't be moved by one pool. If you have to read a DEX, use a TWAP with a window long enough for the pool's depth.
- Check freshness and sanity. Reject a stale update, a zero or negative answer and prices outside the bounds you set for the market, and convert decimals explicitly.
- Size risk to liquidity. Set collateral factors and borrow caps from what it would cost to move the price, not from the asset's market cap.
- Watch your own prices too. Vault share prices and LP token prices can be moved by donations and by unbalancing a pool.
How has it been exploited?
On 2 April 2022 an attacker pushed up the price Inverse Finance used for its INV token and borrowed $15.6M from its Anchor money market: 1,588 ETH, 94 WBTC, 4 million DOLA and 39 YFI. Inverse's write-up says the Keep3r TWAP oracle for INV was manipulated through trades on thin SushiSwap pools, and names the cause as "an error in the TWAP oracle sampling method". INV briefly priced at $20,926.
It was not a flash loan. The attacker brought 901 ETH, withdrawn from Tornado Cash, which shows that a TWAP is only as strong as its window and the liquidity behind it.
What should a reviewer check?
- Where does each price come from: pool reserves, a TWAP, a push oracle, or your own contracts, such as a vault share price or an LP token?
- What would it cost to move each price far enough to matter, and how much could an attacker borrow if they did?
- Is each feed checked for staleness against its heartbeat, for a zero or negative answer and for its decimals?
- Is there a bound or circuit breaker for prices far outside the expected range?
- For a TWAP, is the window long enough for the pool's depth, and does the pool have enough history?
- On a rollup, does the code check the sequencer uptime feed before trusting a price?
- Are collateral factors and borrow caps set from the asset's on-chain liquidity?
How does QuillShield check for it?
Oracle bugs depend on assumptions the code can't show, such as which feeds you trust and how much liquidity you expect behind them. Before a QuillShield audit you can add context such as the whitepaper, trust assumptions, privileged roles and previous audit reports, so write those assumptions down there. The full audit then covers every contract in scope that reads a price, and each finding comes with the exact lines, the impact, a recommended fix and, on a full audit, the proof of concept result.
Pull request checks run against the audit, so a new collateral type or a switch to a different feed is reviewed against the rest of the protocol before it merges.
Full audits start at $49 and are priced from the lines of Solidity in scope, as the pricing page shows. The guide to running your first audit walks through getting a quote for your own code. Sign up free and you see the exact price before anything is charged.
Common questions
Is a TWAP safe from manipulation?
It's safer than a spot price but not safe by default. A short window over a thin pool can be moved for a few blocks, which is what happened to Inverse Finance in April 2022. Check the window length against the pool's depth.
Do flash loans cause oracle attacks?
They make them cheaper, because the attacker needs no capital of their own for a one-transaction move. The bug is in the price source, and Inverse Finance was manipulated without a flash loan.
Is a Chainlink feed enough on its own?
Use it with checks: staleness against the feed's heartbeat, a positive answer, the feed's decimals and, on a rollup, the sequencer uptime feed.
Can a vault share price be used as an oracle?
Only with care, because a donation to a young or empty vault can move its share price. That is how Resupply's exchange rate went to 0 in June 2025.