Using Oracles
Incorporate external data sources into your smart contracts
Blockchain Oracles
By themselves, smart contracts cannot access data outside of the blockchain or send its data to services outside of the blockchain. To do this, smart contracts use middleware called an oracles, which are third-party services that fetch and send data to and from smart contracts. Oracles are similar to traditional web-based API services.
Smart contracts can contain logic that triggers certain changes or actions based on external data from an Oracle. Without oracles, blockchains and their applications that cater to specific industries that rely on real-time changes such as finance, commodity trading, or IoT, would not have access to the data necessary to react to real-time external factors.
It's important to keep in mind that oracles are agnostic of the data they transmit - they are only responsible for fetching, verifying and relaying the data to and from the blockchain.
Inbound vs. Outbound Oracles
Inbound oracles send data to the blockchain. This is the most common type of oracle. Like traditional API services, the inbound data that the oracle provides can be anything that your smart contract needs for internal logic that it cannot access from the blockchain or the smart contract itself.
For example, you want to send 10 EWT to a friend's address when the price of a specific stock goes above $10 per share. An oracle will provide you with the data about stock price, and the blockchain logic will trigger the send when the oracle delivers data indicating that the price of a stock goes above $10.
Outbound oracles send data about events that happen on the blockchain to third party applications. For example, an account could receive a payment and trigger an outbound oracle to a mechanism that activates a pv solar panel.
Oracle Data Sources
Oracles can gather data from software and hardware. A software oracle interacts with and fetches data available from any web source that exposes its data. Some common examples are weather APIs, real-time stock information and exchange rates.
Hardware oracles interact with 'physical' data sources like sensors from IoT devices, QR codes or bar codes. Some examples could be reading the temperature from a smart thermostat or fetching data from a flood sensor..
Centralized Vs. Decentralized Oracles
Centralized oracles are developed, maintained and controlled by a single entity. One oracle is responsible for fetching data from one or many data sources, and then transmitting that data to the smart contract on the blockchain. You could liken this to using a centralized database for your smart contract's external data sources.
Decentralized oracles aggregate data from multiple sources using multiple oracle nodes, so that there is no single point of failure for data retrieval, and no single point of failure for the oracle itself. Decentralized oracles are more aligned with the wider community of public blockchain.
Fundamental Oracle Functionality
Regardless of the data source, or if it is centralized or decentralized, all oracles provide three main functionalities:
Listen to the blockchain network for incoming requests for off-chain data
Fetch data from the requested off-chain source
Transfer the data to the blockchain via a signed transaction
Insert the data into a smart contract’s local storage. Once the data is in the smart contract's storage, it can be used to trigger logic within that smart contract, or it can be available for other smart contract's to use
Oracle Design Patterns: Communicating Data To Smart Contracts
Depending on the nature of the data itself and how the smart contract uses the data, oracles can be designed to communicate with smart contracts using three main patterns:
Publish-subscribe: An oracle broadcasts data, and certain smart contracts are polling or "listening to" that oracle for changes in data. An example: "Poll the temperature of region X every 10 minutes. If the temperature is above 70 degrees celsius, turn the smart thermostat to 'on'."
Immediate-read: Oracles hold data in their storage and provide this data on request. Users or devices query the oracle for this data at the same time that it is needed. An example: "Is device X in a list of authorized devices to provide solar power to region Y."
Request-response: External accounts (via decentralized applications) interact with a smart contract and necessitate data from the oracle. The smart contract sends the data parameters to the oracle and performs the third-party data query. When the data is fetched, it sends the result back to the smart contract for the decentralized application to use. Because the decentralized application cannot always wait for the data, this occurs asynchronously. An example: A user selects from a dropdown on a decentralized application a date range to determine the average temperature of each day during the range. The start and end date are sent to the data oracle to fetch the temperatures for the days in the given date range. The data is sent back to the smart contract to perform necessary logic based on the data.
Tutorial: Using Chainlink with the Volta Test Network
Chainlink provides decentralized oracle networks. This tutorial covers:
Setting up a Chainlink node and using it from a smart contract on the Volta test network
Aggregating data from multiple Oracles, and how to use the public EWT/EUR Price Pair Oracle infrastructure on Volta
Design principles of Chainlink oracles
Scenarios where it makes sense to use Chainlink
Last updated