Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Oracle Integration with Chainlink
📖 Scenario: You are building a simple smart contract that fetches external data using Chainlink oracles. This is useful when your contract needs real-world information, like the current price of ETH in USD.
🎯 Goal: Create a smart contract that requests the latest ETH price from a Chainlink oracle and stores it on the blockchain.
📋 What You'll Learn
Create a mapping to store the latest price
Add a Chainlink oracle address and job ID as configuration variables
Write a function to request the price from the oracle
Write a callback function to receive and store the price
Print the stored price
💡 Why This Matters
🌍 Real World
Smart contracts often need real-world data like prices, weather, or sports scores. Chainlink oracles provide this data securely.
💼 Career
Understanding oracle integration is key for blockchain developers building decentralized finance (DeFi) apps or other smart contracts that rely on external data.
Progress0 / 4 steps
1
Set up the price storage mapping
Create a public mapping called prices that maps address to uint256 to store the latest price for each requester.
Blockchain / Solidity
Hint
Use mapping(address => uint256) public prices; inside the contract.
2
Add Chainlink oracle configuration
Add two public variables: oracle of type address and jobId of type bytes32. Set oracle to 0x1234567890abcdef1234567890abcdef12345678 and jobId to "0x4c7b7ffb66e34d408b2f264769c6d0b2".
Blockchain / Solidity
Hint
Declare oracle as address public and jobId as bytes32 public with the given values.
3
Write the requestPrice function
Write a public function called requestPrice that takes no arguments. Inside, emit an event RequestPrice(address requester) with msg.sender as the requester. (This simulates sending a request to the oracle.)
Blockchain / Solidity
Hint
Define an event RequestPrice and emit it inside requestPrice with msg.sender.
4
Store and print the price
Write a public function called fulfillPrice that takes address requester and uint256 price as arguments. Inside, store price in prices[requester]. Then, write a public view function called getPrice that takes address requester and returns the stored price. Finally, add a line to print the price for msg.sender by calling getPrice(msg.sender).
Blockchain / Solidity
Hint
Store the price in prices[requester] and create a getter function. Then add a function to return the price for msg.sender.
Practice
(1/5)
1. What is the main purpose of using Chainlink oracles in smart contracts?
easy
A. To encrypt data stored on the blockchain
B. To fetch real-world data securely into the blockchain
C. To create new tokens automatically
D. To speed up transaction processing on the blockchain
Solution
Step 1: Understand what oracles do
Oracles connect smart contracts to external data sources outside the blockchain.
Step 2: Identify the main use of Chainlink oracles
Chainlink oracles securely fetch real-world data like prices or weather into smart contracts.
Final Answer:
To fetch real-world data securely into the blockchain -> Option B
Quick Check:
Oracle purpose = fetch external data [OK]
Hint: Oracles bring outside data inside smart contracts [OK]
Common Mistakes:
Thinking oracles speed up blockchain transactions
Confusing oracles with token creation
Assuming oracles encrypt blockchain data
2. Which of the following is the correct way to declare a Chainlink request in Solidity?
easy
A. Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
B. Chainlink.Request req = new Chainlink.Request(jobId, address(this), fulfill);
C. Request memory req = Chainlink.buildRequest(jobId, this, fulfill);
D. ChainlinkRequest req = buildRequest(jobId, this, fulfill.selector);
Solution
Step 1: Recall Chainlink request syntax
The correct syntax uses Chainlink.Request memory and buildChainlinkRequest function.
Step 2: Match the correct parameters
Parameters are jobId, contract address (address(this)), and fulfill function selector (this.fulfill.selector).
Final Answer:
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); -> Option A
Hint: Use buildChainlinkRequest with memory and fulfill.selector [OK]
Common Mistakes:
Omitting 'memory' keyword
Using 'new' keyword incorrectly
Wrong function selector syntax
3. Given this Solidity snippet, what will be the value of data after fulfillment?
uint256 public data;
function fulfill(bytes32 _requestId, uint256 _value) public recordChainlinkFulfillment(_requestId) {
data = _value;
}
// Assume fulfill is called with _value = 42
medium
A. 0
B. RequestId bytes32 value
C. Compilation error
D. 42
Solution
Step 1: Understand fulfill function behavior
The fulfill function sets the contract's data variable to the passed _value.
Step 2: Apply given input value
Since fulfill is called with _value = 42, data becomes 42.
Final Answer:
42 -> Option D
Quick Check:
fulfill sets data = _value = 42 [OK]
Hint: fulfill sets data to passed _value parameter [OK]
Common Mistakes:
Confusing _requestId with _value
Assuming data stays zero
Thinking function causes error
4. Identify the error in this Chainlink oracle request code snippet: