Introduction to Oracles

Introduction to Oracles

Hybrid Smart Contracts With Oracle

Introduction

In this quest, we’ll go through what oracles are, when to use them, and why we should use them. We’ll also cover the different types of oracles and their use cases. Lastly, we’ll cover a quick example of one of the most common use cases of oracles - getting price feeds.

Learning Objective

By the end of this quest, you will be able to:

  • Describe what oracles are and why they are used

  • Describe the situations where oracles are used

  • Explain the differences between the different types of oracles

  • Explain the use cases of oracles

  • Get the price feed of ETH/USD on the Goerli Testnet

Prerequisite

To run this quest, you should have the following:

  • A Remix IDE

  • Metamask Wallet Extension installed

What are Oracles?

Blockchain oracles are a crucial component of many decentralized applications (dApps) and smart contracts on blockchain platforms. These oracles provide a connection between the blockchain network and the outside world, allowing smart contracts to access external data and trigger real-world events.
In simple terms, a blockchain oracle is a bridge between a blockchain and the outside world. It is a piece of software that retrieves information from external sources, such as the internet or another database, and brings it onto the blockchain. This enables smart contracts to access data that is not stored on the blockchain, allowing them to make decisions and execute their functions based on real-world data.

A blockchain oracle is a bridge between a blockchain and the outside world.

Benefit of Oracles

An advantage of blockchain oracles is that they can greatly expand the capabilities of smart contracts. Without oracles, a smart contract can only access data that is stored on the blockchain. But with the help of an oracle, a smart contract can access a wide range of external data, such as real-time prices, weather information, or even events in the physical world. This allows developers to build more complex and powerful dApps and smart contracts that can interact with the outside world in meaningful ways.

An oracle can help to retrieve information from the real world onto the blockchain.

Types of Oracles

Given the extensive range of off-chain resources, blockchain oracles come in many shapes and sizes. Not only do hybrid smart contracts need various types of external data and computation, but they require various mechanisms for delivery and different levels of security. Generally, each type of oracle involves some combination of fetching, validating, computing upon, and delivering data to a destination.
There are several different types of blockchain oracles, each with its own unique characteristics and uses. The following are the various types of oracles as defined by ChainLink.

Input Oracles

Today, the most well-known type of oracle is an "input oracle," which collects data from the real world (off-chain) and delivers it to a blockchain network for smart contract consumption. This type of oracle is used to power Chainlink Price Feeds, which provide on-chain access to financial market data to DeFi smart contracts. For example, oracles that get the price information of cryptocurrencies or assets from exchanges are input oracles.

A/an input oracle fetches data from the real-world (off-chain) and delivers it onto a blockchain network.

Output Oracles

The inverse of input oracles are "output oracles," which allow smart contracts to send commands to off-chain systems that cause them to perform specific actions. An oracle that transmits the circulating supply of a cryptocurrency to a website or a smart lock that unlocks when the payment has been made in the blockchain are examples of output oracles.

Cross-Chain Oracles

Oracles that can read and write data between various blockchains are called cross-chain oracles. These oracles allow for interoperability between different blockchains, such as using data from one blockchain to initiate an action on another or allowing assets to be transferred between chains and used outside of their original blockchain.

Compute-Enabled Oracles

Compute-enabled oracles use secure off-chain computation to provide decentralized services that are impractical to do on-chain due to technical, legal, or financial constraints. This is a new type of oracle that is becoming more widely used by smart contract applications.

Blockchain Oracle Use Cases

Oracles are used by smart contract developers to create more sophisticated dApps for various blockchain applications. While there are countless potential use cases, the following are the most common ones:

Decentralized Finance (DeFi)

Oracles are commonly utilized within DeFi to access financial information about assets and markets. For example, synthetic asset platforms utilize price oracles to link token values to real-world assets, while automated market makers use them to improve capital efficiency by concentrating liquidity at the current market price. Oracles are also used by decentralized money markets to calculate users' borrowing abilities and to determine if a user's position is undercollateralized and potentially subject to liquidation.

Dynamic NFTs & Gaming

One use case of NFTs and gaming smart contracts could be in the creation of a virtual world where players can buy, sell, and trade unique in-game items using NFTs. These items could range from virtual clothing and accessories for avatars, to special weapons or rare items that give players special abilities or bonuses in the game.

The use of smart contracts would allow for secure and transparent transactions within the game, ensuring that players are fairly compensated for their in-game items and that the value of those items is accurately reflected in the market. Smart contracts could also be used to automate the distribution of in-game rewards or to enforce rules and regulations within the game, such as preventing players from cheating or engaging in other unfair practices.

Insurance

Insurance smart contracts are digital contracts that use blockchain technology to automate the process of buying and selling insurance. They can be used to create self-executing contracts that automatically pay out claims when certain conditions are met, without the need for manual intervention. For example, a smart contract could be used to automatically pay out a claim for medical expenses incurred by a policyholder. The contract could be triggered by data from a hospital or electronic medical record system, eliminating the need for the manual claim processing.

Enterprise

One potential use case for enterprise smart contracts is in the supply chain management industry. For example, a company that manufactures and sells products may use smart contracts to automate the ordering, payment, and delivery process with its suppliers.

The company could create a smart contract that specifies the terms of the agreement with its suppliers, including the amount of product to be ordered, the price, and the delivery date. When the company places an order with a supplier, the smart contract automatically executes the payment and delivery process, eliminating the need for manual input and reducing the risk of errors or disputes.

Sustainability

An example of sustainability smart contracts is in the agricultural industry. Farmers can use smart contracts to track the sustainability practices they are implementing on their farms, such as using renewable energy sources, reducing water usage, and implementing sustainable crop rotations. These practices can be verified by third parties, such as environmental organizations or government agencies, and the data can be recorded on the blockchain for transparency and accountability.

There are many oracle projects in the blockchain space, such as Chainlink, Band Protocol, Nest Protocol, Universal Market Access (UMA) and many more. For this campaign, we will be focusing on Chainlink since it is the most widely used oracle for projects in the blockchain space.
Chainlink is a decentralized oracle network that connects smart contracts on the blockchain with external data sources, off-chain payments, and other off-chain resources. This enables smart contracts to securely access off-chain data and services, making them more useful and versatile. By providing a secure and reliable link between the blockchain and the real world, Chainlink allows smart contracts to be used in a wider range of applications and industries.

The Chainlink network is made up of multiple nodes that are run by independent operators and are connected to each other through a decentralized network. This allows for a high degree of reliability and security, as no single node or operator can control or manipulate the data that is sent to or from the smart contracts.

Visit this link to find out more about Chainlink.

STEPS:

  1. Go ahead to create your metamask wallet

  2. Claim Goerli Testnet Tokens. To continue with the remainder of the steps, testnet tokens are needed as gas fees. As we are deploying our smart contract to the Goerli Testnet, we will need to have some GoerliETH in our wallet to pay for the gas fees associated with these transactions.
    Click on this link, sign up or log in with Alchemy. After logging in, copy your wallet account address, paste it into the space provided and click on “Send Me ETH”. To check if you have received the GoerliETH in your wallet, change the network on Metamask to Goerli Test Network. You should receive your testnet GoerliETH in your wallet after a few minutes.

Chainlink Price Feeds are decentralized oracle networks that allow smart contracts to securely access off-chain data from a variety of data sources. These price feeds provide real-time, reliable data for a wide range of assets, including cryptocurrencies, commodities, and fiat currencies.
For this step, let’s see first-hand just how easy it is to implement the Chainlink Price Feed functionality into our smart contract! To start, click on this link. Scroll down to the section on Solidity and click on ‘Open In Remix’. It will open up a new tab with a file called PriceConsumerV3.sol. Refer to the image below for a reference on where you can find the ‘Open in Remix’ button.

On the left side of the screen, click on the third icon (Solidity compiler). The code written using Solidity is compiled using a Solidity compiler, which outputs byte code and other artifacts needed for the deployment of smart contracts. Click on ‘Compile PriceConsumerV3’ to compile the code. Once the code is compiled, you will see a tick beside the Solidity compiler icon.

Before continuing with the next steps, make sure that you have switched the network to Goerli Test Network in your MetaMask wallet. Next, click on the 4th icon on the left (Deploy and run transactions). On the top of the left panel, change the ENVIRONMENT to Injected Provider - Metamask to connect to your wallet. To deploy the contract, click on ‘Deploy’. Since we’re using our MetaMask wallet to deploy this contract, your MetaMask extension will prompt you to approve the transaction, press on ‘Confirm’.

The ‘PriceConsumerV3.sol’ file is basically looking to retrieve the price of ETH/USD on the Goerli Test Network. The tab below will show a green tick when the contract has been successfully deployed. To check the price of ETH/USD, you can click on the getLatestPrice() function under ‘Deployed Contracts’. And just as simple as that, our smart contract can retrieve the price of ETH/USD!

Conclusion

We have learned about how we can use oracles to make our smart contracts hybrid! By hybrid, we’re referring to the ability to connect our smart contracts with the external world - the capability to push and pull data from outside to inside the blockchain and vice versa.
We also tried out a common use case of oracles - getting price feeds.