What is an Oracle? Providing Data to A Blockchain
Smart contracts are programs that self-execute when certain events or conditions are met. These events can take place on the blockchain or off the blockchain. For example, in an on-chain event, a sender could send funds to a smart contract as collateral for a loan, which would then trigger a loan amount to send. In an off-chain event, a smart contract might need a way to retrieve accurate data on these events to know when to execute. Oracles connect the off-chain world and on-chain smart contracts by acting as a data source to the smart contract.
How developers use oracles
The advantage of using a smart contract is that we don’t need a third party to act as a middleman. The code determines the terms of the smart contract, and the code is law. These terms can rely on external events for a condition to be met before a transaction executes. For example, we can use a smart contract to issue an exchange of tokens based on the value of the US Dollar. A smart contract would query an oracle to get the value of the US Dollar from some external source in order to know when to perform the exchange.
Decentralized Finance (DeFi) is one of the biggest use-cases for oracles. Price oracles provide information on the values of tokens and liquidity, which is used to manage a user's financial position. NFTs (Non-fungible tokens) can use oracles to get information on external events in the news like election results so that the appearance of the NFT dynamically changes based on those results.
We can also use oracles to encourage good behaviors toward sustainability and citizenship by rewarding users with tokens after completing tasks or meeting certain goals. For example, we could offer residents of a city tokens for carbon offsetting.
Types of Oracles
Oracles can be both software, like a server or database, or hardware like IoT devices and sensors. They can be external services that provide data to multiple applications and blockchains that connect to them.
Projects can also create internal oracles for their specialized needs. An example of this is Augur, a decentralized betting platform, which has an oracle that provides the results of the events being bet on.
Find out how Augur uses Alchemy to scale their decentralized oracle and peer to peer protocol and create better user experiences.
Oracles are bi-directional, which means they can both fetch data from the real world as well as serve data to the outside world about on-chain events. Smart locks that are unlocked based on completed transactions are a popular example of when on-chain event data is needed off-chain.
In addition to communicating to the outside world, oracles can enable cross-chain communications amongst different blockchains. Developers can use this to bridge assets or trigger actions based on the transactions and events from one chain to another. Oracles can also save on valuable compute costs by performing computations like random number generation off-chain.
The oracle problem
Usually, the “oracle problem” comes up anytime the value of oracles is discussed. The uniqueness of building on a blockchain is that a consensus on the state of the blockchain between the nodes must be reached. This means each node must be able to take the same transactions and events and get the same result as all other nodes.
An attacker could change the off-chain data that an oracle retrieves or the data could change overtime.
For example, the value of the US Dollar can change frequently in just a day. If a smart contract executes based on this value, the price could change by the time another node performs the computation. This would break the consensus between the nodes, as the expected result is not the actual result anymore.
Not only are there potential issues of whether the data provided by the oracle is accurate, but a centralized source can also be hacked or experience downtime. In these situations, the smart contract will either be operating with corrupted data or no data at all. Since smart contracts cannot be changed and are self-executing, this can lead to irreversible damage for the parties involved. Oracles must address these issues if they are to be secure and trusted providers of data to smart contracts.
The oracle solution
A popular method of solving the oracle problem is by using a blockchain itself. This approach is called a Decentralized Oracle Network (DON).
In a DON, instead of nodes coming to a consensus on a ledger of transactions, each node is an oracle. The nodes are then tasked with retrieving independent data from separate off-chain sources. The data from the nodes are then aggregated together and a value of truth is decided on.
This also solves the issue of the uptime of a data source as the smart contract will still be able to function since there are multiple sources of data. You can see a DON in action here.
The most common way developers can use oracles is through a third-party service. The first and most popular service for this is Chainlink. Through Chainlink, developers and API providers can become participating node operators by connecting their API to the Chainlink network.
Chainlink gives providers operating these oracles, on-chain identifiers, which they can use to manage and reward the reputation of the oracles. Using an ERC677 token incentivizes providers to give well-formatted and accurate data as well as any off-chain computation performed.
Another network that is growing in popularity is the Band Protocol. This network also aims to provide reliable and secure sources for external data like Chainlink. Similarly, there is also a BAND token to reward data providers for providing good data. The key difference is that Band Protocol operates on Cosmos, as opposed to Ethereum, as a cost-saving measure.
How to use an oracle
We can retrieve data from an oracle either through an API request by using frameworks like the one provided by Chainlink or by referencing data that has already been sent by a provider and now exists on-chain. We can retrieve this data by referencing the smart contract that holds this information and address. A common use of this method is in the world of DeFi. Smart contracts hold exchange rates that are updated regularly, and this data can be used by other smart contracts to perform token swaps.
For smart contracts, to have any use in the real world, they need to have access to what is going on in the world. Oracles are the windows of data that smart contracts need to react towards off-chain events. Using a centralized source of data in a decentralized environment like a blockchain is not ideal. DONs solve many of these issues that come with centralization, providing security and stability. As these oracle services expand and onboard more providers, oracles will play a large part in the growth of blockchain and smart contract technology.