Blockchain Oracles

Blockchain Oracles

APIs for Blockchain

What are Blockchain Oracles?

In simple terms, Blockchain Oracles are APIs that can be integrated within the blockchain smart contract.

Blockchain oracles are third-party services that connect deterministic blockchain with the outside world for accessing information like Weather conditions, Ether prices in USD, News, Stock market, etc. Oracle can be centralized or decentralized and operate on various [blockchain] networks.

The oracles add the data to the blockchain through an external transaction. This is done so that the blockchain has all the data it needs to verify and validate itself through a consensus mechanism.

Oracle Problem

As some might have already guessed, there is a big catch with using the Oracles. While using Oracles, our blockchain network gets dependent on some single external data source or a centralized API; which essentially counters the purpose of blockchain i.e. Decentrality. Additionally depending on a single node essentially means leaving the entire control of execution of that node and, getting vulnerable to many security problems.

So the Oracle Problems can be defined as the combination of:

  1. Blockchain networks by themselves can't use external data.

  2. Using Oracles invalidates the concept of Decentrality leaving loopholes concerned with multiple Security risks.

Decentralized Oracles

As the name suggests Decentralized Oracles are nodes that do not depend upon single data source. Decentralized Oracles are one of the solutions proposed to counter the Oracle problem. Even if any of API nodes are deleted, hacked, or modified, it won't be affecting the decentrality of the network, keep our smart contract reliable and decentralized!

There are quite a few Decentralized Oracles in the market today such as ChainLink, UMA, API3, etc.

Chainlink is by far one of the most popular Oracle network that aims to provide accurate and reliable data to the blockchain enabling applications to reach their maximum potential. Additionally Chainlink is also Open-Source backed by a large community of developers.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract PriceConsumerV3 {
    AggregatorV3Interface internal priceFeed;

    /**
     * Network: Goerli
     * Aggregator: ETH/USD
     * Address: 0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
     */
    constructor() {
        priceFeed = AggregatorV3Interface(
            0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
        );
    }

    function getLatestPrice() public view returns (int) {
        (
            ,
            int price,
            ,
            ,
        ) = priceFeed.latestRoundData();
        return price;
    }
}

The above contract fetches the price of ETH in USD, and it works on Goerli test Network. The implementation provides a basic example of using Chainlink API but can also be modified or made more complex to suit one's needs.

Conclusion

Using an oracle network can bring a lot of benefits to smart contracts by making them accessible to external data sources. One of the key advantages of using a decentralized oracle network is the increased security and trust in the data provided. With decentralized oracles, smart contracts can access data from various sources in a secure and reliable way, bringing out their maximum potential.

Now that you understand what Oracle is and how to use it into your code, hopefully you'll find the one you need. If you have a favourite, do shart which one!