Oracles help smart contracts get information from outside the blockchain. Chainlink is a popular way to connect smart contracts with real-world data safely.
Oracle integration (Chainlink) in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol"; contract MyContract is ChainlinkClient { using Chainlink for Chainlink.Request; address private oracle; bytes32 private jobId; uint256 private fee; constructor() { setPublicChainlinkToken(); oracle = 0x...; // Oracle address jobId = "..."; // Job ID string fee = 0.1 * 10 ** 18; // LINK token fee } function requestData() public returns (bytes32 requestId) { Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); // Add parameters to request return sendChainlinkRequestTo(oracle, req, fee); } function fulfill(bytes32 _requestId, uint256 _data) public recordChainlinkFulfillment(_requestId) { // Use the data } }
You must fund your contract with LINK tokens to pay for oracle services.
The jobId tells the oracle what kind of data to fetch.
setPublicChainlinkToken(); oracle = 0x7AFe30cb3E53dba6801aa0ea647A0Ecea7cbe18d; jobId = "29fa9aa13bf1468788b7cc4a500a45b8"; fee = 0.1 * 10 ** 18;
function requestPrice() public returns (bytes32 requestId) {
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
req.add("get", "https://api.coindesk.com/v1/bpi/currentprice/BTC.json");
req.add("path", "bpi.USD.rate_float");
return sendChainlinkRequestTo(oracle, req, fee);
}function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) {
latestPrice = _price;
}This smart contract uses Chainlink to get the current Bitcoin price in USD from an external API. It stores the price in latestPrice. You call requestPrice() to start the request. When the oracle replies, fulfill() saves the price.
pragma solidity ^0.8.7; import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol"; contract PriceConsumer is ChainlinkClient { using Chainlink for Chainlink.Request; uint256 public latestPrice; address private oracle; bytes32 private jobId; uint256 private fee; constructor() { setPublicChainlinkToken(); oracle = 0x7AFe30cb3E53dba6801aa0ea647A0Ecea7cbe18d; jobId = "29fa9aa13bf1468788b7cc4a500a45b8"; fee = 0.1 * 10 ** 18; // 0.1 LINK } function requestPrice() public returns (bytes32 requestId) { Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); req.add("get", "https://api.coindesk.com/v1/bpi/currentprice/BTC.json"); req.add("path", "bpi.USD.rate_float"); return sendChainlinkRequestTo(oracle, req, fee); } function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) { latestPrice = _price; } }
Make sure your contract has enough LINK tokens to pay the oracle fee.
Oracle addresses and job IDs differ by network (mainnet, testnet).
Chainlink requests are asynchronous; the contract waits for the oracle to call fulfill.
Chainlink oracles let smart contracts get real-world data safely.
You set oracle address, job ID, and fee to request data.
Data comes back in a callback function you define, like fulfill.
Practice
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 BQuick Check:
Oracle purpose = fetch external data [OK]
- Thinking oracles speed up blockchain transactions
- Confusing oracles with token creation
- Assuming oracles encrypt blockchain data
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 AQuick Check:
Correct request syntax = Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); [OK]
- Omitting 'memory' keyword
- Using 'new' keyword incorrectly
- Wrong function selector syntax
data after fulfillment?uint256 public data;
function fulfill(bytes32 _requestId, uint256 _value) public recordChainlinkFulfillment(_requestId) {
data = _value;
}
// Assume fulfill is called with _value = 42Solution
Step 1: Understand fulfill function behavior
The fulfill function sets the contract'sdatavariable to the passed_value.Step 2: Apply given input value
Since fulfill is called with_value = 42,databecomes 42.Final Answer:
42 -> Option DQuick Check:
fulfill sets data = _value = 42 [OK]
- Confusing _requestId with _value
- Assuming data stays zero
- Thinking function causes error
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
req.add("get", "https://api.example.com/data");
req.add("path", "price");
sendChainlinkRequest(req, fee);Solution
Step 1: Review Chainlink request sending requirements
Before sending a request, the contract must have LINK tokens approved to pay the oracle fee.Step 2: Check code for LINK approval
The snippet does not show LINK token approval, which is required to avoid failure.Final Answer:
Missing LINK token approval before sending request -> Option AQuick Check:
LINK approval needed before sendChainlinkRequest [OK]
- Assuming sendChainlinkRequestTo is correct function
- Confusing add and addString methods
- Ignoring LINK token approval step
Solution
Step 1: Build and send Chainlink request
Create a request specifying jobId and data source URL, then send it with the required fee.Step 2: Implement fulfill function
Define fulfill to receive the oracle response and store the ETH/USD price in your contract.Final Answer:
Build a Chainlink request with jobId and URL, send request with fee, implement fulfill to store price -> Option CQuick Check:
Request + fee + fulfill = correct Chainlink usage [OK]
- Skipping fulfill function implementation
- Not specifying jobId in request
- Assuming default oracle without jobId
