๐ŸŽ‰ 75% of content is free forever โ€” Unlock Premium from $10/mo โ†’
CW
Search coursesโ€ฆ
๐Ÿ’ผ Servicesโ„น๏ธ Aboutโœ‰๏ธ ContactView Pricing Plansfrom $10

Blockchain Oracles

Oracle Networks๐ŸŸข Free Lesson

Advertisement

Blockchain Oracles

Price feeds, data oracles, Chainlink integration, and oracle design patterns.

Overview

Oracles connect blockchains to external data.

Oracle Types

TypeUse Case
Price FeedAsset prices
RandomnessVRF for NFTs/gaming
DataExternal APIs
ComputeOff-chain computation

Chainlink Price Feed

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract PriceConsumer {
    AggregatorV3Interface internal priceFeed;
    
    constructor(address _priceFeed) {
        priceFeed = AggregatorV3Interface(_priceFeed);
    }
    
    function getLatestPrice() public view returns (int) {
        (
            ,
            int price,
            ,
            uint256 updatedAt,
            
        ) = priceFeed.latestRoundData();
        
        require(block.timestamp - updatedAt < 3600, "Stale price");
        return price;
    }
}

Chainlink VRF

import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";

contract RandomNFT is VRFConsumerBaseV2 {
    uint256 public lastRandom;
    
    constructor() VRFConsumerBaseV2(vrfCoordinator) {
        vrfCoordinator = 0x...');
        keyHash = 0x...';
        subscriptionId = 1;
    }
    
    function requestRandomNumber() public {
        s_vrfCoordinator.requestRandomWords(
            keyHash,
            subscriptionId,
            3,
            200000,
            1
        );
    }
    
    function fulfillRandomWords(uint256 requestId, uint256[] memory randomWords) internal override {
        lastRandom = randomWords[0];
    }
}

Oracle Design Patterns

๐Ÿ“œ

Smart Contract

On-chain request

๐Ÿ”ฎ

Oracle Network

Decentralized nodes

๐Ÿ“ก

External Data

APIs, databases

Best Practices

  1. Multiple sources โ€” Avoid single point
  2. Staleness checks โ€” Fresh data
  3. Fallback mechanisms โ€” Graceful degradation
  4. Cost optimization โ€” Batch requests

Practice

Integrate Chainlink price feeds into a DeFi protocol.

โญ

Premium Content

Blockchain Oracles

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

๐ŸŽฏEnd-to-end Projects
๐Ÿ’ผInterview Prep
๐Ÿ“œCertificates
๐ŸคCommunity Access

Already a member? Log in

Need Expert Blockchain Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement