Design Oracle Security
Use TWAP prices and multiple oracle sources with staleness checks and circuit breakers — preventing flash loan-based oracle manipulation that has drained over $1B from DeFi protocols.
Why This Is Best Practice
Adopted by: OWASP Smart Contract Top 10 SC02 (Oracle Manipulation). Uniswap V3's TWAP oracle is the on-chain reference implementation used by dozens of protocols. Chainlink Data Feeds (used by Aave, Compound, Synthetix) is the leading off-chain oracle with cryptographic attestation. The DeFi Security Alliance's oracle best practices mandate multiple sources for any price used in liquidations or collateral valuation.
Impact: Oracle manipulation is the leading DeFi attack vector by total value lost. Mango Markets lost $117M (2022) via MANGO token price manipulation. Euler Finance lost $197M (2023) partially via oracle manipulation. Cream Finance's $130M loss (2021) and the Harvest Finance $34M loss (2020) were both price oracle attacks. CoinGecko's "DeFi Security Incident Review" (2023) attributes $1.3B+ in losses to oracle vulnerabilities. Spot price oracles (single AMM pool, single block) are manipulable via flash loans with zero capital risk.
Why best: Spot price oracles read from a single block — flash loans allow an attacker to borrow millions, manipulate the price in that block, exploit the oracle, and repay — all in one transaction with no capital. TWAP (Time-Weighted Average Price) averages over many blocks, making manipulation expensive and visible. Multiple oracle sources require an attacker to simultaneously manipulate independent systems.
Sources: OWASP Smart Contract Top 10 SC02; SWC-116; Uniswap V3 TWAP oracle documentation; Chainlink security overview; Euler Finance post-mortem (2023)
Steps
Use Uniswap V3 TWAP instead of spot price for on-chain oracles:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import "@uniswap/v3-periphery/contracts/libraries/OracleLibrary.sol";
contract TWAPOracle {
IUniswapV3Pool public immutable pool;
uint32 public constant TWAP_PERIOD = 1800; // 30-minute TWAP
constructor(address _pool) {
pool = IUniswapV3Pool(_pool);
}
function getTWAPPrice() public view returns (uint256 price) {
uint32[] memory secondsAgos = new uint32[](2);
secondsAgos[0] = TWAP_PERIOD;
secondsAgos[1] = 0;
(int56[] memory tickCumulatives, ) = pool.observe(secondsAgos);
int56 tickCumulativeDiff = tickCumulatives[1] - tickCumulatives[0];
int24 arithmeticMeanTick = int24(tickCumulativeDiff / int56(int32(TWAP_PERIOD)));
uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(arithmeticMeanTick);
price = FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96);
}
}
Use Chainlink Data Feeds with staleness checks:
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract ChainlinkPriceConsumer {
AggregatorV3Interface public immutable priceFeed;
uint256 public constant MAX_STALENESS = 3600; // 1 hour max age
constructor(address _priceFeed) {
priceFeed = AggregatorV3Interface(_priceFeed);
}
function getPrice() public view returns (int256) {
(
uint80 roundId,
int256 price,
,
uint256 updatedAt,
uint80 answeredInRound
) = priceFeed.latestRoundData();
// Staleness check — stale prices are as dangerous as manipulated ones
require(block.timestamp - updatedAt <= MAX_STALENESS, "Price feed stale");
// Completeness check
require(answeredInRound >= roundId, "Incomplete round");
require(price > 0, "Invalid price");
return price;
}
}
Use multiple oracle sources — median of 3 prevents single-source manipulation:
contract MultiOracleConsumer {
AggregatorV3Interface[3] public priceFeeds;
constructor(address[3] memory feeds) {
for (uint i = 0; i < 3; i++) {
priceFeeds[i] = AggregatorV3Interface(feeds[i]);
}
}
function getMedianPrice() public view returns (int256) {
int256[3] memory prices;
for (uint i = 0; i < 3; i++) {
(, prices[i], , uint256 updatedAt, ) = priceFeeds[i].latestRoundData();
require(block.timestamp - updatedAt <= 3600, "Feed stale");
}
return _median3(prices[0], prices[1], prices[2]);
}
function _median3(int256 a, int256 b, int256 c) internal pure returns (int256) {
if ((a >= b && a <= c) || (a >= c && a <= b)) return a;
if ((b >= a && b <= c) || (b >= c && b <= a)) return b;
return c;
}
}
Implement circuit breakers — reject prices that deviate too far from TWAP:
contract OracleWithCircuitBreaker {
int256 public constant MAX_PRICE_DEVIATION_BPS = 500; // 5% max deviation
function validatePrice(int256 spotPrice, int256 twapPrice) internal pure {
int256 deviation = abs(spotPrice - twapPrice) * 10000 / twapPrice;
require(deviation <= MAX_PRICE_DEVIATION_BPS,
"Price deviates >5% from TWAP — possible manipulation");
}
function abs(int256 x) internal pure returns (int256) {
return x >= 0 ? x : -x;
}
}
Use price averaging over multiple blocks for liquidation prices:
contract SafeLiquidation {
mapping(address => uint256[]) private priceHistory;
uint256 private constant PRICE_SAMPLES = 5;
function recordPrice(address asset, uint256 price) external {
priceHistory[asset].push(price);
if (priceHistory[asset].length > PRICE_SAMPLES) {
// Remove oldest
for (uint i = 0; i < priceHistory[asset].length - 1; i++) {
priceHistory[asset][i] = priceHistory[asset][i + 1];
}
priceHistory[asset].pop();
}
}
function getLiquidationPrice(address asset) public view returns (uint256) {
uint256[] memory history = priceHistory[asset];
require(history.length == PRICE_SAMPLES, "Insufficient price history");
uint256 sum;
for (uint i = 0; i < history.length; i++) sum += history[i];
return sum / PRICE_SAMPLES;
}
}
Rules
- Never use
pool.slot0().sqrtPriceX96 (spot price from a single AMM pool) for any financial calculation — it's manipulable with a flash loan.
- TWAP period must be long enough that manipulation is economically unviable: 30 minutes minimum, 1 hour for protocols with large TVL.
- Always check Chainlink
updatedAt timestamp — a stale feed returning old prices is as dangerous as a manipulated feed.
- Oracle failures (reverts, stale data) should pause protocol operations, not fall back to an unchecked default price.
Common Mistakes
block.timestamp as a price oracle input — block timestamps are manipulable by miners/validators within ~15 seconds; use cumulative price accumulators.
- Single-source oracle for collateral pricing — if the single source is compromised or goes down, the entire protocol's liquidation system fails.
- Not testing oracle manipulation in fork tests — Foundry's
vm.createFork() allows testing against mainnet state; simulate flash loan price manipulation to verify TWAP resistance.
- Chainlink circuit breaker not accounting for decimals — Chainlink returns prices with 8 decimals; failing to normalize before comparison causes incorrect deviation calculations.
1---2name: design-oracle-security3description: Use when writing DeFi smart contracts that rely on price feeds, randomness, or external data — preventing oracle manipulation via TWAP, multiple oracle sources, and circuit breakers.4source: 'OWASP Smart Contract Top 10 SC02 (owasp.org/www-project-smart-contract-top-10/); SWC-116; Uniswap V3 TWAP documentation; Chainlink Data Feeds documentation'5tags: [security, owasp, solidity, smart-contracts, oracle, defi, price-manipulation, ethereum]6metadata:7 source: https://skillmd.com/skills/jeffreytse/design-oracle-security8---910# Design Oracle Security1112Use TWAP prices and multiple oracle sources with staleness checks and circuit breakers — preventing flash loan-based oracle manipulation that has drained over $1B from DeFi protocols.1314## Why This Is Best Practice1516**Adopted by:** OWASP Smart Contract Top 10 SC02 (Oracle Manipulation). Uniswap V3's TWAP oracle is the on-chain reference implementation used by dozens of protocols. Chainlink Data Feeds (used by Aave, Compound, Synthetix) is the leading off-chain oracle with cryptographic attestation. The DeFi Security Alliance's oracle best practices mandate multiple sources for any price used in liquidations or collateral valuation.17**Impact:** Oracle manipulation is the leading DeFi attack vector by total value lost. Mango Markets lost $117M (2022) via MANGO token price manipulation. Euler Finance lost $197M (2023) partially via oracle manipulation. Cream Finance's $130M loss (2021) and the Harvest Finance $34M loss (2020) were both price oracle attacks. CoinGecko's "DeFi Security Incident Review" (2023) attributes $1.3B+ in losses to oracle vulnerabilities. Spot price oracles (single AMM pool, single block) are manipulable via flash loans with zero capital risk.18**Why best:** Spot price oracles read from a single block — flash loans allow an attacker to borrow millions, manipulate the price in that block, exploit the oracle, and repay — all in one transaction with no capital. TWAP (Time-Weighted Average Price) averages over many blocks, making manipulation expensive and visible. Multiple oracle sources require an attacker to simultaneously manipulate independent systems.1920Sources: OWASP Smart Contract Top 10 SC02; SWC-116; Uniswap V3 TWAP oracle documentation; Chainlink security overview; Euler Finance post-mortem (2023)2122## Steps23241. **Use Uniswap V3 TWAP instead of spot price for on-chain oracles**:2526 ```solidity27 // SPDX-License-Identifier: MIT28 pragma solidity ^0.8.19;2930 import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";31 import "@uniswap/v3-periphery/contracts/libraries/OracleLibrary.sol";3233 contract TWAPOracle {34 IUniswapV3Pool public immutable pool;35 uint32 public constant TWAP_PERIOD = 1800; // 30-minute TWAP3637 constructor(address _pool) {38 pool = IUniswapV3Pool(_pool);39 }4041 function getTWAPPrice() public view returns (uint256 price) {42 uint32[] memory secondsAgos = new uint32[](2);43 secondsAgos[0] = TWAP_PERIOD;44 secondsAgos[1] = 0;4546 (int56[] memory tickCumulatives, ) = pool.observe(secondsAgos);4748 int56 tickCumulativeDiff = tickCumulatives[1] - tickCumulatives[0];49 int24 arithmeticMeanTick = int24(tickCumulativeDiff / int56(int32(TWAP_PERIOD)));5051 uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(arithmeticMeanTick);52 price = FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96);53 }54 }55 ```56572. **Use Chainlink Data Feeds with staleness checks**:5859 ```solidity60 import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";6162 contract ChainlinkPriceConsumer {63 AggregatorV3Interface public immutable priceFeed;64 uint256 public constant MAX_STALENESS = 3600; // 1 hour max age6566 constructor(address _priceFeed) {67 priceFeed = AggregatorV3Interface(_priceFeed);68 }6970 function getPrice() public view returns (int256) {71 (72 uint80 roundId,73 int256 price,74 ,75 uint256 updatedAt,76 uint80 answeredInRound77 ) = priceFeed.latestRoundData();7879 // Staleness check — stale prices are as dangerous as manipulated ones80 require(block.timestamp - updatedAt <= MAX_STALENESS, "Price feed stale");81 // Completeness check82 require(answeredInRound >= roundId, "Incomplete round");83 require(price > 0, "Invalid price");8485 return price;86 }87 }88 ```89903. **Use multiple oracle sources — median of 3 prevents single-source manipulation**:9192 ```solidity93 contract MultiOracleConsumer {94 AggregatorV3Interface[3] public priceFeeds;9596 constructor(address[3] memory feeds) {97 for (uint i = 0; i < 3; i++) {98 priceFeeds[i] = AggregatorV3Interface(feeds[i]);99 }100 }101102 function getMedianPrice() public view returns (int256) {103 int256[3] memory prices;104 for (uint i = 0; i < 3; i++) {105 (, prices[i], , uint256 updatedAt, ) = priceFeeds[i].latestRoundData();106 require(block.timestamp - updatedAt <= 3600, "Feed stale");107 }108 return _median3(prices[0], prices[1], prices[2]);109 }110111 function _median3(int256 a, int256 b, int256 c) internal pure returns (int256) {112 if ((a >= b && a <= c) || (a >= c && a <= b)) return a;113 if ((b >= a && b <= c) || (b >= c && b <= a)) return b;114 return c;115 }116 }117 ```1181194. **Implement circuit breakers — reject prices that deviate too far from TWAP**:120121 ```solidity122 contract OracleWithCircuitBreaker {123 int256 public constant MAX_PRICE_DEVIATION_BPS = 500; // 5% max deviation124125 function validatePrice(int256 spotPrice, int256 twapPrice) internal pure {126 int256 deviation = abs(spotPrice - twapPrice) * 10000 / twapPrice;127 require(deviation <= MAX_PRICE_DEVIATION_BPS,128 "Price deviates >5% from TWAP — possible manipulation");129 }130131 function abs(int256 x) internal pure returns (int256) {132 return x >= 0 ? x : -x;133 }134 }135 ```1361375. **Use price averaging over multiple blocks for liquidation prices**:138139 ```solidity140 contract SafeLiquidation {141 mapping(address => uint256[]) private priceHistory;142 uint256 private constant PRICE_SAMPLES = 5;143144 function recordPrice(address asset, uint256 price) external {145 priceHistory[asset].push(price);146 if (priceHistory[asset].length > PRICE_SAMPLES) {147 // Remove oldest148 for (uint i = 0; i < priceHistory[asset].length - 1; i++) {149 priceHistory[asset][i] = priceHistory[asset][i + 1];150 }151 priceHistory[asset].pop();152 }153 }154155 function getLiquidationPrice(address asset) public view returns (uint256) {156 uint256[] memory history = priceHistory[asset];157 require(history.length == PRICE_SAMPLES, "Insufficient price history");158 uint256 sum;159 for (uint i = 0; i < history.length; i++) sum += history[i];160 return sum / PRICE_SAMPLES;161 }162 }163 ```164165## Rules166167- Never use `pool.slot0().sqrtPriceX96` (spot price from a single AMM pool) for any financial calculation — it's manipulable with a flash loan.168- TWAP period must be long enough that manipulation is economically unviable: 30 minutes minimum, 1 hour for protocols with large TVL.169- Always check Chainlink `updatedAt` timestamp — a stale feed returning old prices is as dangerous as a manipulated feed.170- Oracle failures (reverts, stale data) should pause protocol operations, not fall back to an unchecked default price.171172## Common Mistakes173174- **`block.timestamp` as a price oracle input** — block timestamps are manipulable by miners/validators within ~15 seconds; use cumulative price accumulators.175- **Single-source oracle for collateral pricing** — if the single source is compromised or goes down, the entire protocol's liquidation system fails.176- **Not testing oracle manipulation in fork tests** — Foundry's `vm.createFork()` allows testing against mainnet state; simulate flash loan price manipulation to verify TWAP resistance.177- **Chainlink circuit breaker not accounting for decimals** — Chainlink returns prices with 8 decimals; failing to normalize before comparison causes incorrect deviation calculations.