0
0
Blockchain / Solidityprogramming~30 mins

Oracle integration (Chainlink) in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Store the price in prices[requester] and create a getter function. Then add a function to return the price for msg.sender.