Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Oracle integration (Chainlink) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Oracle integration (Chainlink)
Smart Contract requests data
Chainlink Oracle receives request
Oracle fetches external data
Oracle sends data back to contract
Smart Contract receives and uses data
End
The smart contract asks Chainlink Oracle for data, Oracle fetches it from outside blockchain, then returns it to the contract for use.
Execution Sample
Blockchain / Solidity
contract Weather {
  int temperature;
  function requestTemperature() public {
    chainlinkRequest(...);
  }
  function fulfill(bytes32 id, int temp) public {
    temperature = temp;
  }
}
This contract requests temperature data via Chainlink and updates its state when Oracle responds.
Execution Table
StepActionSmart Contract StateOracle StateOutput/Result
1Smart contract calls requestTemperature()Request sentWaiting for requestRequest emitted
2Chainlink Oracle receives requestRequest sentRequest receivedOracle starts fetching data
3Oracle fetches external temperatureRequest sentFetching dataData retrieved from API
4Oracle sends data back to contractRequest sentData sentResponse transaction sent
5Contract fulfill() called with temptemperature updatedData sentTemperature stored
6Contract uses temperature datatemperature availableIdleContract logic proceeds
7EndFinal stateIdleProcess complete
💡 Process ends after contract receives and stores Oracle data
Variable Tracker
VariableStartAfter Step 1After Step 5Final
temperatureundefinedundefined25 (example)25
Key Moments - 3 Insights
Why does the contract wait after sending the request?
Because the Oracle fetches data asynchronously outside the blockchain, the contract must wait for the Oracle to call fulfill() later (see steps 2-5 in execution_table).
How does the contract know the data is from the Oracle?
The fulfill() function is called only by the Oracle's authorized address, ensuring data authenticity (step 5 in execution_table).
What happens if the Oracle never responds?
The contract will not update the variable and may remain waiting indefinitely or until timeout logic triggers (not shown in this simple trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the contract update its temperature variable?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Check the 'Smart Contract State' column for when 'temperature updated' occurs.
According to variable_tracker, what is the temperature value after step 5?
Aundefined
B0
C25 (example)
Dnull
💡 Hint
Look at the 'After Step 5' column for the 'temperature' variable.
If the Oracle never sends data back, which step would never happen?
AStep 2
BStep 5
CStep 4
DStep 1
💡 Hint
Refer to execution_table rows where contract state changes after Oracle response.
Concept Snapshot
Oracle integration with Chainlink:
- Contract sends request to Oracle
- Oracle fetches external data asynchronously
- Oracle calls contract's fulfill() with data
- Contract updates state with received data
- Ensures real-world data on blockchain securely
Full Transcript
This visual trace shows how a smart contract uses Chainlink Oracle to get external data. First, the contract sends a request (step 1). The Oracle receives it and fetches data from outside blockchain (steps 2-3). Then Oracle sends the data back by calling fulfill() on the contract (step 5). The contract updates its variable with the new data and continues its logic (steps 6-7). Variables like temperature start undefined and update only after Oracle response. Key points include waiting for asynchronous Oracle response and verifying data authenticity. If Oracle never responds, contract state stays unchanged. This process securely connects blockchain contracts to real-world information.

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

  1. Step 1: Understand what oracles do

    Oracles connect smart contracts to external data sources outside the blockchain.
  2. Step 2: Identify the main use of Chainlink oracles

    Chainlink oracles securely fetch real-world data like prices or weather into smart contracts.
  3. Final Answer:

    To fetch real-world data securely into the blockchain -> Option B
  4. 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

  1. Step 1: Recall Chainlink request syntax

    The correct syntax uses Chainlink.Request memory and buildChainlinkRequest function.
  2. Step 2: Match the correct parameters

    Parameters are jobId, contract address (address(this)), and fulfill function selector (this.fulfill.selector).
  3. Final Answer:

    Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); -> Option A
  4. Quick Check:

    Correct request syntax = Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); [OK]
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

  1. Step 1: Understand fulfill function behavior

    The fulfill function sets the contract's data variable to the passed _value.
  2. Step 2: Apply given input value

    Since fulfill is called with _value = 42, data becomes 42.
  3. Final Answer:

    42 -> Option D
  4. 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:
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);
medium
A. Missing LINK token approval before sending request
B. Incorrect function name: should be sendChainlinkRequestTo
C. Missing import for Chainlink library
D. Using 'add' instead of 'addString' for parameters

Solution

  1. Step 1: Review Chainlink request sending requirements

    Before sending a request, the contract must have LINK tokens approved to pay the oracle fee.
  2. Step 2: Check code for LINK approval

    The snippet does not show LINK token approval, which is required to avoid failure.
  3. Final Answer:

    Missing LINK token approval before sending request -> Option A
  4. Quick Check:

    LINK approval needed before sendChainlinkRequest [OK]
Hint: Always approve LINK tokens before sending requests [OK]
Common Mistakes:
  • Assuming sendChainlinkRequestTo is correct function
  • Confusing add and addString methods
  • Ignoring LINK token approval step
5. You want to fetch the current ETH/USD price using Chainlink in your smart contract. Which steps must you combine to do this correctly?
hard
A. Use Chainlink request but omit fulfill function to save gas
B. Directly call the price feed contract without Chainlink oracles
C. Build a Chainlink request with jobId and URL, send request with fee, implement fulfill to store price
D. Send request without specifying jobId and rely on default oracle

Solution

  1. Step 1: Build and send Chainlink request

    Create a request specifying jobId and data source URL, then send it with the required fee.
  2. Step 2: Implement fulfill function

    Define fulfill to receive the oracle response and store the ETH/USD price in your contract.
  3. Final Answer:

    Build a Chainlink request with jobId and URL, send request with fee, implement fulfill to store price -> Option C
  4. Quick Check:

    Request + fee + fulfill = correct Chainlink usage [OK]
Hint: Request data, pay fee, handle response in fulfill [OK]
Common Mistakes:
  • Skipping fulfill function implementation
  • Not specifying jobId in request
  • Assuming default oracle without jobId