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
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.