Flash loans are a legitimate DeFi primitive that let users borrow any amount of assets without collateral, provided the loan is repaid within the same transaction. Attackers abuse them to amplify the economic impact of other vulnerabilities — particularly price oracle manipulation.
In a single atomic transaction, an attacker:
If any step fails, the entire transaction reverts — making flash loans risk-free for the attacker.
// Contract uses a single DEX spot price as an oracle — VULNERABLE
function getPrice() public view returns (uint) {
return dex.getReserveA() / dex.getReserveB(); // easily manipulated
}
function borrow(uint amount) public {
require(getPrice() >= MIN_COLLATERAL_RATIO);
// Attacker manipulates dex price before calling this
_mint(msg.sender, amount);
}
// Use a time-weighted average price (TWAP) oracle
import "@uniswap/v3-periphery/contracts/libraries/OracleLibrary.sol";
function getPrice() public view returns (uint) {
(int24 tick,) = OracleLibrary.consult(pool, 1800); // 30-min TWAP
return OracleLibrary.getQuoteAtTick(tick, 1e18, tokenA, tokenB);
}
block.number check to prevent same-block oracle reads after large swaps.