Lesson 7 of 14
In Progress

Understanding the Role of Oracles in Blockchain

In the world of Blockchain, oracles play a vital role in enabling smart contracts to access and interact with external data sources. Without oracles, smart contracts would be limited to working with data that is already stored on the Blockchain, which would severely restrict their usefulness and potential applications.

In this chapter, we’ll explore the concept of oracles and their role in the Blockchain ecosystem. We’ll cover the types of oracles, how they work, and why they are important for Blockchain developers.

What are Oracles?

An oracle is a third-party service that provides smart contracts with access to external data. Oracles are responsible for fetching data from external sources, such as API’s, web pages, or databases, and delivering it to the smart contract in a secure and trustworthy manner.

Oracles can be thought of as a bridge between the decentralized world of Blockchain and the centralized world of external data sources. They allow smart contracts to interact with data that is not stored on the Blockchain, enabling them to perform more complex and useful functions.

Types of Oracles

There are several types of oracles, each with its own unique characteristics and use cases. Some of the most common types of oracles are:

  • Human Oracles: Human oracles are individuals who manually provide data to a smart contract. This type of oracle is typically used for tasks that require human judgment or expertise, such as legal contracts or insurance claims.
  • External Data Oracles: External data oracles retrieve data from external sources, such as API’s or web pages. They are commonly used for retrieving real-time data, such as stock prices or weather data.
  • On-Chain Oracles: On-chain oracles are smart contracts that retrieve data from other smart contracts on the same Blockchain. They are commonly used for tasks that require data from multiple sources or complex calculations.
  • Off-Chain Oracles: Off-chain oracles are smart contracts that retrieve data from external sources and store it off-chain. They are commonly used for tasks that require large amounts of data or data that is not suitable for storing on-chain.

How Oracles Work

Oracles typically work by receiving data requests from smart contracts and fetching the requested data from an external source. The process of using an oracle typically involves the following steps:

  1. A smart contract sends a data request to the oracle, specifying the data that it needs and any relevant parameters.
  2. The oracle fetches the data from the external source and verifies its authenticity and accuracy.
  3. The oracle sends the data back to the smart contract, either directly or via a secure off-chain storage mechanism.
  4. The smart contract processes the data and performs any necessary actions, such as updating its internal state or triggering other smart contracts.

Oracles can be implemented in a variety of ways, depending on the specific needs and requirements of the smart contract. Some oracles may be operated by a single entity, while others may be decentralized networks of multiple oracles working together to provide data.

Why are Oracles Important for Blockchain Developers?

Oracles are an essential component of the Blockchain ecosystem, enabling smart contracts to access and interact with external data sources. Without oracles, smart contracts would be limited to working with data that is already stored on the Blockchain, which would severely restrict their usefulness and potential applications.

For Blockchain developers, understanding the role and capabilities of oracles is crucial for building and deploying smart contracts that can interact with the real world. Oracles allow developers to build smart contracts that can perform a wide range of tasks, from retrieving real-time data to triggering events based on external conditions.

Example: Using an Oracle to Retrieve Stock Prices

Let’s say you want to build a smart contract that allows users to bet on the price of a particular stock. To do this, you’ll need to retrieve real-time stock prices from an external source.

To retrieve the stock prices, you’ll need to use an oracle. You can use an external data oracle that is able to fetch data from a stock market API and deliver it to your smart contract.

Here is an example of how you might use an oracle to retrieve stock prices in a Solidity contract:

pragma solidity ^0.5.0;

import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/ChainlinkClient.sol";

contract StockPriceOracle {
    ChainlinkClient public chainlink;
    string public stockSymbol;
    uint256 public price;

    constructor(ChainlinkClient _chainlink, string _stockSymbol) public {
        chainlink = _chainlink;
        stockSymbol = _stockSymbol;
    }

    function requestStockPrice() public {
        chainlink.requestData(chainlink.ORACLE_ADDRESS, jobId, stockSymbol);
    }

    function onData(bytes32 _jobId, uint256 _price) public {
        require(_jobId == jobId, "Invalid job ID");
        price = _price;
    }
}

In this contract, we use the “ChainlinkClient” contract to send a data request to the Chainlink Oracle contract, asking it to retrieve the current price of the specified stock symbol. The “onData” function is called when the data request is fulfilled and stores the retrieved price in the “price” variable.

Conclusion

In this chapter, we’ve explored the concept of oracles and their role in the Blockchain ecosystem. We’ve covered the types of oracles and how they work, and we’ve seen how they can be used to enable smart contracts to access and interact with external data sources.

By understanding the capabilities and limitations of oracles, Blockchain developers can build and deploy smart contracts that can perform a wide range of tasks and interact with the real world in meaningful ways.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

What is the main role of oracles in the Blockchain ecosystem?

The main role of oracles in the Blockchain ecosystem is to provide smart contracts with access to external data sources.

Name three types of oracles and briefly describe their characteristics.

  • Human oracles: Human oracles are individuals who manually provide data to a smart contract. They are typically used for tasks that require human judgment or expertise.
  • External data oracles: External data oracles retrieve data from external sources, such as API’s or web pages. They are commonly used for retrieving real-time data.
  • On-chain oracles: On-chain oracles are smart contracts that retrieve data from other smart contracts on the same Blockchain. They are commonly used for tasks that require data from multiple sources or complex calculations.

What are the steps involved in using an oracle to retrieve data for a smart contract?

The steps involved in using an oracle to retrieve data for a smart contract are:

  1. A smart contract sends a data request to the oracle, specifying the data that it needs and any relevant parameters.
  2. The oracle fetches the data from the external source and verifies its authenticity and accuracy.
  3. The oracle sends the data back to the smart contract, either directly or via a secure off-chain storage mechanism.
  4. The smart contract processes the data and performs any necessary actions, such as updating its internal state or triggering other smart contracts.

What are the benefits of using oracles for Blockchain developers?

The benefits of using oracles for Blockchain developers are:

  • Oracles enable smart contracts to access and interact with external data sources, which greatly increases their usefulness and potential applications.
  • Oracles allow developers to build smart contracts that can perform a wide range of tasks, such as retrieving real-time data or triggering events based on external conditions.
  • Oracles provide a secure and trustworthy way for smart contracts to access external data, ensuring that the data is authentic and accurate.

Write a Solidity contract that defines a function called “getWeatherData” that sends a data request to an oracle to retrieve the current temperature in a specified city. The function should take in a “bytes32” parameter called “_jobId” and a “string” parameter called “_city” and should pass these values to the “requestData” function.

pragma solidity ^0.5.0;

import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/ChainlinkClient.sol";

contract WeatherOracle {
    ChainlinkClient public chainlink;
    uint256 public temperature;

    function getWeatherData(bytes32 _jobId, string _city) public {
        chainlink.requestData(chainlink.ORACLE_ADDRESS, _jobId, _city);
    }

    function onData(bytes32 _jobId, uint256 _temperature) public {
        require(_jobId == jobId, "Invalid job ID");
        temperature = _temperature;
    }
}