0
0
Blockchain / Solidityprogramming~7 mins

Oracle integration (Chainlink) in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Oracles help smart contracts get information from outside the blockchain. Chainlink is a popular way to connect smart contracts with real-world data safely.

You want your smart contract to know the current price of a cryptocurrency.
You need weather data inside your smart contract for insurance purposes.
You want to trigger actions in your contract based on sports scores.
You want to get random numbers securely for a game on the blockchain.
Syntax
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.

Examples
This sets up the Chainlink token, oracle address, job ID, and fee for a price feed request.
Blockchain / Solidity
setPublicChainlinkToken();
oracle = 0x7AFe30cb3E53dba6801aa0ea647A0Ecea7cbe18d;
jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
fee = 0.1 * 10 ** 18;
This function asks the oracle to get Bitcoin price from a web API and return the USD rate.
Blockchain / Solidity
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);
}
This function receives the price from the oracle and saves it in the contract.
Blockchain / Solidity
function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) {
    latestPrice = _price;
}
Sample Program

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.

Blockchain / Solidity
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;
    }
}
OutputSuccess
Important Notes

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.

Summary

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.