Chainlink Integration (Oracles)
Chainlink — de facto standard for price oracles in DeFi. $100B+ secured by Chainlink price feeds in Aave, Compound, Synthetix and hundreds of other protocols. Chainlink integration — mandatory skill for any DeFi developer.
Price Feeds: Basic Integration
Chainlink Data Feeds — aggregated prices from network of independent node operators.
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceConsumer {
AggregatorV3Interface internal priceFeed;
constructor() {
// ETH/USD on Ethereum mainnet
priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
}
function getLatestPrice() public view returns (int256) {
(
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) = priceFeed.latestRoundData();
// Check staleness
require(updatedAt >= block.timestamp - 3600, "Stale price");
require(answer > 0, "Invalid price");
return answer; // 8 decimals for USD feeds
}
}
Staleness check — mandatory: if updatedAt too old — oracle not updated. Better to revert on stale price than use outdated data.
Decimals and Normalization
Different feeds have different decimals:
- ETH/USD: 8 decimals (price × 10^8)
- ETH/BTC: 8 decimals
- USDC/ETH: 18 decimals
Always read priceFeed.decimals() and normalize.
Chainlink VRF (Verifiable Random Function)
For applications requiring verifiable randomness: NFT mint, lotteries, games.
Chainlink Automation
Automate on-chain tasks without centralized service: decentralized network of keepers calls performUpkeep when condition met.
Chainlink Automation — replacement for cron-bots. Decentralized keeper network calls function when condition executed.
Integration of basic price feeds — several hours. VRF and Automation — 1-3 days for production-ready implementation.







