Price manipulation attacks exploit contracts that rely on spot DEX prices for critical operations like liquidations, collateral valuation, or fee calculations. Cream Finance ($130M, 2021) and dozens of lending protocols have been drained by attackers manipulating token prices within a single transaction. Firepan's HOUND AI detects price manipulation vulnerabilities automatically during development and post-deployment monitoring, covering continuous analysis across the full contract lifecycle.
Price manipulation differs from oracle manipulation in that it exploits the protocol's own price mechanism rather than external feeds. The attack vector:
Price manipulation is distinct from oracle manipulation because:
Price manipulation unfolds as:
// VULNERABLE — example only
// Demonstrates: Price Manipulation
// Do NOT use in production
pragma solidity ^0.8.0;
contract VulnerableLending {
// VULNERABLE: Uses spot price for liquidation
function getCollateralPrice(address token) public view returns (uint256) {
return IUniswap(UNISWAP_V2).getPrice(token); // Spot price, flashable
}
// VULNERABLE: Liquidation using spot price
function liquidate(address borrower, address collateralToken) public {
uint256 spotPrice = getCollateralPrice(collateralToken); // Can be manipulated
uint256 collateralValue = borrowers[borrower].collateral * spotPrice;
require(collateralValue < minimumCollateral, "Not liquidatable");
// Extract collateral at manipulated price
uint256 liquidationBonus = (collateralValue * 110) / 100;
_transferCollateral(msg.sender, liquidationBonus);
}
function borrow(address token, uint256 amount) public {
uint256 collateralPrice = getCollateralPrice(token); // Spot price
uint256 collateralRequired = (amount * 120) / collateralPrice;
// Attacker can reduce collateral requirement by crashing collateral price
require(balances[msg.sender] >= collateralRequired);
// Borrow proceeds...
}
}
contract PriceManipulationAttack {
VulnerableLending public target;
address public uniswap;
address public collateralToken;
function manipulatePrice() public payable {
// STEP 1: Perform massive swap to crash collateral price
uint256 swapAmount = IERC20(collateralToken).balanceOf(uniswap) / 2;
// Swap ETH for collateralToken, crashing its price
IDex(uniswap).swap(ETH, collateralToken, address(this).balance);
// Price of collateralToken just crashed massively
// STEP 2: Liquidate at manipulated price
target.liquidate(victim, collateralToken);
// Attacker gets liquidation bonus at crashed price
// STEP 3: Sell liquidated collateral back at normal price
IDex(uniswap).swap(collateralToken, ETH, swapAmount);
// Profit = liquidation gains - swap losses
}
function attackWithFlashLoan() public {
// Can also use flash loan instead of own capital
IFlashLoanProvider(LENDER).flashLoan(
address(this),
ETH,
100000000 ether,
abi.encodeWithSignature("_executeAttack()")
);
}
function _executeAttack() internal {
// Same as above: swap → liquidate → reverse swap → repay loan
}
}
// Real example: Cream Finance exploit
contract CreamAttack {
// Cream used cTokenBalance as collateral value
// Attacker swapped massive amount of crETH, crashing its price
// Liquidated at favorable price, extracted $130M+
}
| Protocol | Date | Loss | Root Cause | |----------|------|------|-----------| | Cream Finance | 2021-10 | $130M | Flash loan exploit draining lending markets via price manipulation | | bZx (Fulcrum) | 2020-02 | $954K | sUSD price crash via DEX swap | | Harvest Finance | 2020-10 | $34M | USDC/USDT price manipulation on Curve | | Venus | 2021-05 | $150M risk | Venus token price manipulation enabled liquidations |
Manual detection focuses on price source validation:
Red flags:
Firepan's HOUND AI performs deep price dependency analysis:
Firepan's HOUND AI engine identifies price manipulation vulnerabilities across all monitored contracts.
1. Use Time-Weighted Average Price (TWAP)
Replace spot price with 30-minute TWAP from Uniswap V3:
function getTWAPPrice(address pool, uint32 timeWindow)
internal view returns (uint256)
{
(int56[] memory tickCumulatives, ) =
IUniswapV3Pool(pool).observe(new uint32[](2));
int56 tickCumulativeDelta = tickCumulatives[1] - tickCumulatives[0];
int24 timeWeightedTick = int24(tickCumulativeDelta / int56(uint56(timeWindow)));
return TickMath.getSqrtRatioAtTick(timeWeightedTick);
}
2. Use Chainlink Oracle with Circuit Breaker
Rely on external oracle + deviation bounds:
function getPrice(address token) public view returns (uint256) {
(, int256 answer, , uint256 updatedAt, ) = priceFeed.latestRoundData();
require(block.timestamp - updatedAt < 1 hours, "Stale price");
require(answer > 0, "Invalid price");
return uint256(answer);
}
3. Implement Price Deviation Bounds
Revert if price moves > threshold per block:
uint256 public lastPrice;
uint256 public maxDeviation = 10; // 10% max
function liquidateWithBounds(address borrower) public {
uint256 currentPrice = getPrice();
// Ensure price doesn't jump > 10%
require(
currentPrice >= lastPrice * 90 / 100 &&
currentPrice <= lastPrice * 110 / 100,
"Price deviation too large"
);
lastPrice = currentPrice;
// Liquidate...
}
4. Use Multiple Price Sources
Aggregate prices from multiple DEX/oracles:
function getPriceFromMultipleSources() internal view returns (uint256) {
uint256[] memory prices = new uint256[](3);
prices[0] = getTWAPPrice(UNISWAP_V3, 30 minutes);
prices[1] = IChainlink(CHAINLINK).getPrice();
prices[2] = ICurve(CURVE).getPrice();
// Return median (resistant to single-source manipulation)
return median(prices);
}
5. Whitelist Collateral Tokens
Limit liquidation to stable collateral with low manipulation risk:
mapping(address => bool) public approvedCollateral;
function liquidate(address borrower, address collateral) public {
require(approvedCollateral[collateral], "Collateral not approved");
// Proceed with liquidation...
}
Q: What is price manipulation in smart contracts?
A: Price manipulation exploits contracts relying on spot DEX prices for liquidations or collateral valuation. Attackers perform massive swaps to crash prices, trigger liquidations at favorable rates, then profit from the difference.
Q: Which protocols have been exploited via price manipulation?
A: Cream Finance ($130M, 2021), bZx ($954K, 2020), Harvest Finance ($34M, 2020), and Venus ($150M risk, 2021) suffered price manipulation attacks. Attackers used flash loans + DEX swaps to crash prices, liquidate at discount, then arbitrage.
Q: How does Firepan detect price manipulation?
A: Firepan maps price sources, simulates swap scenarios with extreme impacts, traces liquidation conditions, models flash loan attack paths, and verifies TWAP is used instead of spot prices.
Q: Can price manipulation be exploited after deployment?
A: Yes, price manipulation is immediately exploitable post-deployment. Attackers constantly scan liquidation opportunities, ready to execute flash loan + swap attacks the moment protocol goes live.
Q: How do I prevent price manipulation?
A: Use Uniswap V3 TWAP instead of spot price. Integrate Chainlink Oracle. Implement price deviation bounds (max ±10% per block). Aggregate multiple price sources (median). Whitelist safe collateral. Add circuit breaker pause mechanism.
Price manipulation has cost DeFi over $300M, particularly in lending protocols. Cream's $130M loss demonstrated the severity of spot-price dependency. Using Uniswap V3 TWAP + Chainlink Oracle + deviation bounds greatly reduces price manipulation risk. Firepan's HOUND AI detects price-dependent liquidation logic vulnerable to manipulation across all monitored contracts.
Start securing your smart contracts at https://app.firepan.com/
Firepan
12,453 contracts secured. 2,851 vulnerabilities blocked. 236 exploits prevented. Run a free surface scan — results in minutes, no credit card required.
Run Free Scan →