Web3 - Oracle Fundamentals: The Bridge Between Blockchain and the Real World
1. What is an Oracle
In the world of blockchain, smart contracts are "automated execution programs," but they have a natural limitation: they cannot directly access off-chain data. This means that if a smart contract needs weather data, financial data, logistics status, or even random numbers, it cannot obtain them directly.
An Oracle is used to solve this problem. It is an "information intermediary" responsible for bringing off-chain data onto the chain, or conversely, sending on-chain information off-chain.
For example
Suppose you are developing a decentralized insurance contract where users can purchase "rain insurance." If the rainfall exceeds 50mm on a certain day, the contract will automatically pay out.
But the problem arises: how does the smart contract know the weather on that day? It cannot access the weather API itself—this is where the Oracle comes in. The Oracle can collect data from multiple weather APIs and submit the final result to the blockchain, allowing the contract to make decisions based on this data.
2. Why Smart Contracts Cannot Directly Access Off-Chain Data
The decentralized and deterministic design of blockchain presents several core issues when dealing with external data:
Decentralization Disruption
📡 The core of blockchain is "decentralization," but if a smart contract relies on a single data source (like a specific API), that data source becomes a single point of failure and may even be manipulated.
📡 For example, if a weather API is hacked and returns erroneous data, the smart contract may make incorrect decisions.
Consistency Issues
📡 Blockchain requires all nodes to arrive at the same computational result, but external API data may vary by time and location.
📡 For instance, one blockchain node is in China and another in the United States; they may access the same API but receive different exchange rate data, leading to consensus failure.
Data Availability Issues
📡 If an API suddenly goes down, all smart contracts relying on it may fail to operate normally.
Data Immutability
📡 Data on the blockchain is immutable, but off-chain API data can be modified.
📡 For example, a decentralized exchange relies on an API to provide ETH/USD prices; if the API provider intentionally alters the price, it could lead to manipulated trades.
3. How Oracles Work The core function of an Oracle is to allow on-chain contracts to securely and reliably obtain off-chain data. Its typical workflow is as follows:
- On-Chain Data Request
A smart contract or user initiates a request, telling the Oracle, "I want to obtain certain data." This request will be stored on the blockchain and notify all Oracle nodes.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract WeatherOracleRequester {
event WeatherRequest(uint256 requestId, string city, address callbackAddress);
uint256 public nextRequestId;
function requestWeather(string memory city) external {
uint256 requestId = nextRequestId++;
emit WeatherRequest(requestId, city, address(this));
}
}
In this example, the contract broadcasts the request through the emit event, and off-chain Oracle nodes will listen for this request.
- Oracle Listens for Requests
Off-chain Oracle nodes will listen for requests on the blockchain and extract tasks. For example, it may see a request:
Request ID: 101
Data Type: Weather Data
City: Shanghai
API Data Source: https://weatherapi.com/shanghai
3. Oracle Obtains Off-Chain Data
Each Oracle node will obtain data from multiple data sources according to the request, such as:
WeatherAPI.com: Rainfall 52mm
OpenWeather.com: Rainfall 51mm
LocalWeather.com: Rainfall 53mm
4. Data Verification and Aggregation
Different data sources may return different results, and the Oracle will use aggregation algorithms to ensure data accuracy:
Taking the median: 52mm
Weighted average (considering the weights of different APIs)
Removing outliers (if a certain API's result deviates too much, it will be excluded)
5. Oracle Submits Data On-Chain
The Oracle submits the processed data to the blockchain, for example:
contract WeatherOracle {
event WeatherResponse(uint256 requestId, uint256 rainfall);
function submitWeatherData(uint256 requestId, uint256 rainfall) external {
emit WeatherResponse(requestId, rainfall);
}
}
6. Smart Contract Uses Data
The smart contract listens for the WeatherResponse event and triggers logic accordingly:
contract WeatherInsurance {
mapping(address => uint256) public balances;
function claimInsurance(uint256 rainfall) public {
require(rainfall > 50, "No payout, rainfall too low");
payable(msg.sender).transfer(balances[msg.sender]);
}
}
If the rainfall exceeds 50mm, the contract will automatically pay the insurance amount to the user.
4. Application Scenarios of Oracles
1. DeFi Oracles (Decentralized Finance)
DeFi protocols require external price data, such as ETH/USD prices. Common oracles include Chainlink, Pyth, and Band Protocol, which provide reliable market data.
2. Insurance and Weather Oracles
Decentralized insurance (like Arbol, Etherisc) uses weather oracles to determine whether to pay claims.
3. Supply Chain and Logistics
Smart contracts can obtain supply chain status through oracles, such as whether goods have been delivered.
4. Random Number Oracles
Generating random numbers on-chain is difficult, so lotteries, NFT minting, etc., often rely on random number oracles like Chainlink VRF.
5. How to Ensure Oracle Data Consistency Across All Miner Nodes?
1. Blockchain State Model
After the Oracle updates the data, it will be stored in the smart contract.
All miners will read the same state when executing transactions.
2. Data Synchronization Mechanism
After the Oracle submits data, all nodes will synchronize updates.
For example, in block 99, the Oracle updates the ETH/USD price to $3000.
In block 100, whether it's miner A or miner B, they will read $3000 without any discrepancies.
6. Conclusion
Oracles are the bridge connecting blockchain with the real world, enabling smart contracts to access off-chain data.
Since smart contracts cannot directly access external APIs, oracles solve this problem by listening for requests, obtaining data, verifying data, and submitting data.
The core challenges of oracles are data authenticity, decentralization, and consistency, with common solutions including decentralized oracles, data aggregation, and consensus mechanisms.
Oracles have been widely applied in DeFi, insurance, supply chains, NFTs, random number generation, and more.
In the future, as oracle technology continues to evolve, Web3 applications will become smarter, and the interaction between blockchain and the real world will become even closer!
Introduction to The Web3 Community
The Web3 is a community focused on the design and development of Web3 technology solutions, dedicated to providing professional enhancement tutorials, research and development, and training services for individuals and enterprises. In addition, The Web3 also offers comprehensive support such as project security audits, investment research analysis, and project incubation.
- The Web3 consists of three core components:
- "The Web3 Community": Focused on education and training, successfully cultivating 130 excellent developers and over 30 outstanding product managers.
- "The Web3 Security Lab": Provides professional security audit services for well-known projects such as DappLink, FishCake, Parapack, and RootHash.
- "The Web3 Capital": Has invested over $2 million, actively promoting the incubation and growth of Web3 innovative projects.











