Are You Using AI In Your Job?

We want to understand the real-world applications of AL and ML in business and the impact it will have on all our jobs.

Want to help? Complete the survey, your insights could make a big difference. It will just take one minute.
You'll be the first to get access to the final report.-->

What is Chainlink and Why Smart Contract Developers Should Use It

Bobby Gill | January 9, 2023

Smart contracts are pieces of software living on the blockchain, isolated from the external off-chain world – unlike Web2 applications, they cannot make HTTP requests to directly fetch data from a web service API. 

As such, to integrate data into a smart contract that originates from outside of the blockchain, developers use a concept called a blockchain oracle to call on and use data from external systems.

You can build your own oracle to feed your smart contract or use existing services, such as Chainlink, to retrieve data from multiple sources across multiple oracles in a decentralized manner.

To understand this article, you will need some exposure to smart contract development. 

For those of you new to smart contract development, it might be helpful to read our guide on How To Create A Smart Contract which will walk you through the process of deploying a smart contract to a testnet and interacting with it.

What are Blockchain Oracles?

Etymologically, an oracle is an intermediary between two worlds: Olympus and the human world. In ancient Greece, oracles were spiritual people in charge of reading the stars so as to give precious information to people who needed their services.

Places like Delphi were believed to act as conduits for oracles, who were believed to be the only reliable liaison between gods that were otherwise sequestered from all but a select few mortals. | Source: Tamara Semina on Wikimedia Commons

Blockchain oracles function similarly to how it was believed ancient Greece oracles were able to communicate with the divine. Here, blockchain oracles gather information from the external world (the gods or divine from the analogy) to place it in specific smart contracts for decentralized apps to query. 

For example, a smart contract developer that wanted to read weather data would need to use an oracle to marshal that data from the off-chain world so that it was accessible within their smart contract.

The most popular blockchain oracles are Chainlink Data Feeds as it is the simplest way of connecting smart contracts to real-world data. 

What is Chainlink and how do Chainlink Price Feeds work?

Chainlink is a blockchain company that specializes in developing services and solutions for connecting blockchain ecosystems to the real world. Driven by the ambition of making web3 as efficient as web2, Chainlink has developed various products that enable blockchain ecosystems to make outbound and inbound requests. 

One of Chainlink’s products for making inbound requests is classified as Chainlink’s Data Feeds or Price Feeds. Chainlink Data Feeds are on-chain smart contracts updated by a network of computers that requests data from various sources and aggregate it in a decentralized manner.

Source: Chainlink

Chainlink uses its own consensus mechanism called Off-Chain Reporting to reach a consensus on external data and then place the data back on-chain in a smart contract (Data Feed) for people requesting information.

In order to communicate with a Chainlink Data Feed, you will need its address and use the Data Feeds’ interface called Chainlink’s Aggregator, which consists of the following:

  • An instance of the Data Feed you want to query.
  • A constructor that instantiates the interface with the oracle’s address.
  • A function named getLatestPrice() that calls latestRoundData() to retrieve information that is then organized in Solidity tuple format.

Here is the information returned by Chainlink Price Feeds’ latestRoundData():

  • answer: The price. 
  • roundId: The round ID. 
  • startedAt: Timestamp of when the round started. 
  • updatedAt: Timestamp of when the round was updated. 
  • answeredInRound: The round ID of the round in which the answer was computed.

Note: The address of the ETH/USD oracle will be different on Ethereum mainnet from Goerli testnet, etc. For more information about data feeds oracles’ addresses, visit Price Feeds Contract Addresses.

How to Use Chainlink Price Feeds

For demonstration purposes, we will use Chainlink Aggregator interface to query a Chainlink Data Feed (ETH/USD) deployed to Goerli testnet. 

Before you begin this tutorial, make sure you’ve installed a wallet such as Metamask in your browser, your Remix IDE is integrated with your wallet, and your Ethereum account has some test coins.

Here’s how you use Chainlink Data Feeds:

1. Go to your Remix IDE and create a .sol file.

2. Copy and paste the following code into your Remix IDE’s editor:

// 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
        );
    }
 
    /**
     * Returns the latest price
     */
    function getLatestPrice() public view returns (int) {
        (
            ,
            /*uint80 roundID*/ int price /*uint startedAt*/ /*uint timeStamp*/ /*uint80 answeredInRound*/,
            ,
            ,
 
        ) = priceFeed.latestRoundData();
        return price;
    }
}

 

Note: In this tutorial, we are deploying our contract to Goerli test; therefore, we use the address of the intended Data Feed living on Goerli testnet. If you would like to deploy a contract to the Ethereum mainnet, you will need to replace the above address with the address of the intended Data Feed you would like to query. Here is a list of Price Feeds Contract Addresses for various networks.

3. Compile the code. 

4. Then, click on the “Deploy & Run Transactions” icon located in the icon bar. 

5. Click on the “Environment” drop-down menu.

6. Select the “Injected” option.

7. Click on the “Deploy” button below which will create a popup with details about the deployment transactions.

8. Click on the “Confirm” button to automatically execute the deployment transactions. Remix IDE will auto-generate a button called getLatestPrice().

9. Click on the getLatestPrice() to get the current price of Ether. You will see that only the price is returned. This is due to the fact that we hid other information in the code by using forward slashes and asterisks around them.

Conclusion

Chainlink offers developers a simple way of connecting blockchain ecosystems to real-world data by providing developers with a slew of user-friendly benefits. 

For Web3 apps to be as efficient as Web2 apps, these technologies provide much-needed functionalities that reliably drive the interconnectivity to which we’ve grown accustomed. 

Bobby Gill
Co-Founder & Chief Architect at BlueLabel | + posts

Get the latest from the Blue Label Labs’ blog in your inbox

Subscribe

* indicates required