Suppose that you and a friend want to bet on the outcome of a sports match using a smart contract. You bet $10 on Team A and your friend bets $10 on Team B, resulting in $20 held in escrow by the smart contract. But, how does the smart contract know the winner? By design, smart contracts don’t interact with off-chain data.
Oracles provide access to off-chain data and deliver it securely and reliably to the blockchain. Using Decentralized Oracle Networks (DONs) like Chainlink, you can decentralize off-chain data and establish multiple data sources to prevent stale data or downtime. The result is a robust solution to provide real-world data to smart contracts.
Let’s take a closer look at smart contract limitations, how oracles overcome them, different types of oracles, and common use cases.
Oracles help connect deterministic smart contracts with off-chain data sources in a secure and reliable manner.
Smart Contract Limitations
Smart contracts power everything from decentralized finance (DeFi) and decentralized autonomous organizations (DAOs) to Web3 ecosystems and P2E games. They enable two parties to securely automate and decentralize nearly any kind of deal or transaction. And since they run on the blockchain, they provide security, reliability, and accessibility.
The problem is that smart contract operations must be fully deterministic since they exist on a blockchain. In other words, they must produce the same result every time they run so that nodes can validate them. As a result, they cannot use off-chain data because it’s impossible to ensure that the data remains consistent and available over time.
For example, suppose that a smart contract executing trades needs a real-time asset price. If it fetches the price from a third-party server, there’s no guarantee that the server will always be available and error-free when sending responses. As a result, other nodes may not be able to validate transactions generated by the smart contract.
How Oracles Work
Oracles overcome smart contract limitations by operating both on-chain and off-chain. The off-chain server waits for requests from a smart contract by reading through new blocks. Each request contains a data request, gas payment limits, and a smart contract that will receive the response and ultimately parse the data on-chain.
For example, the sports betting smart contract might work like this:
- An off-chain server reads through blocks until it finds a request.
- The request seeks the winner of a game and specifies a smart contract where the winning team should be sent.
- The server fetches the winning team and sends the data to the smart contract address defined in the request.
- The smart contract identifies the winner and pays out the bet by releasing the escrow.
The problem is that centralized oracles present a major security risk. If attackers compromise the off-chain server, they could inject arbitrary data to steal cryptocurrency or cause other problems. Fortunately, Chainlink provides a DON that aggregates multiple individual nodes into a single response, providing decentralization and availability.
Chainlink is one of the most popular oracles, with nearly $40 billion in total value secured as of June 2022. The decentralized platform makes it easy to build hybrid smart contracts that bring in external market data, weather data, sports data, randomness functions, or any API. As a result, developers can build reliable dapps without limitations.
Chainlink works using a `ChainlinkClient` parent contract, enabling smart contracts to consume data from oracles. The client makes requests to a known Chainlink oracle using LINK tokens. By default, Chainlink provides easy access to a range of popular data, including price and weather data, but you can access data from any API using the platform.
Types of Oracles
Smart contracts may use off-chain data in all kinds of ways. As a result, there are various types of oracles designed to accomplish different tasks. For example, a smart contract handling payments may need to send a request out to a bank to process a payment, whereas a smart contract facilitating trades may need to receive asset prices in real-time.
The four most popular types of oracles are:
- Input Oracles: The most common are input oracles that fetch off-chain data and deliver it to an on-chain smart contract. For instance, a smart contract may need coin price data.
- Output Oracles: Output oracles enable smart contracts to send data to an off-chain system to take action. For example, a DeFi protocol may instruct an off-chain bank to make a payment.
- Cross-Chain Oracles: Cross-chain oracles read and write data between different blockchains. A common use case is bridging assets across different blockchains.
- Computing Oracles: Compute-enabled oracles secure off-chain computation. For instance, a smart contract may want a verifiably random number to use in a function.
Oracle Use Cases
Smart contracts enable two parties to securely automate and decentralize nearly any kind of deal or transaction. Since many real-world transactions involve off-chain data, there are countless instances where oracles are necessary. Oracle use cases range from blockchain-based games to complex financial ecosystems to industrial internet-of-things.
Some common oracle use cases include:
- P2E & NFTs: A P2E game may use oracles to generate randomness to assign NFT traits, distribute loot boxes, or match players in tournaments or other gameplay.
- DeFi Protocols: DeFi protocols make extensive use of oracles to access financial data and peg certain crypto assets to real-world assets, such as forex baskets.
- Insurance: Insurance smart contracts may use oracles to verify events when processing claims or to make payouts to other blockchains or traditional banks.
- Enterprises: Many enterprises use cross-chain oracles as blockchain middleware to connect their backend systems to different blockchain networks.
The Bottom Line
Smart contracts cannot directly receive and process off-chain data and remain deterministic. Fortunately, oracles make it easy to send and receive data from off-chain servers. Chainlink helps ensure that these oracles remain decentralized on their own, preserving the primary benefits of blockchain technology.
If you trade cryptocurrencies, ZenLedger can help you accurately prepare your taxes each year. The platform automatically aggregates your transactions across wallets and exchanges, computes your capital gains or losses, and pre-fills the IRS forms you need.