Oracle integration (Chainlink) in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Chainlink oracles, we want to know how the time to get data changes as we ask for more information.
We ask: How does the number of oracle requests affect the time our smart contract takes?
Analyze the time complexity of the following code snippet.
contract PriceConsumer {
address[] public oracles;
function requestPrices() public {
for (uint i = 0; i < oracles.length; i++) {
Chainlink.Request memory req = buildChainlinkRequest(...);
sendChainlinkRequestTo(oracles[i], req, fee);
}
}
}
This code sends a price request to each oracle in a list one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over the list of oracles to send requests.
- How many times: Once for each oracle in the array.
As the number of oracles grows, the contract sends more requests, so the time grows directly with the number of oracles.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 requests sent |
| 100 | 100 requests sent |
| 1000 | 1000 requests sent |
Pattern observation: The time increases steadily as more oracles are added.
Time Complexity: O(n)
This means the time to send requests grows in direct proportion to the number of oracles.
[X] Wrong: "Sending requests to multiple oracles happens instantly all at once."
[OK] Correct: Each request is sent one by one in a loop, so more oracles mean more time spent sending requests.
Understanding how your contract's time grows with oracle calls shows you can write efficient blockchain code that scales well.
"What if we batch multiple oracle requests into one call? How would the time complexity change?"
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
