A Gentle Introduction to Blockchain Oracles & Chainlink

#100DaysOfWeb3 Week 1 | This week I've learned about Decentralized Oracles and how they're useful for our smart contracts.

A Gentle Introduction to Blockchain Oracles & Chainlink

Hey, fren! gm.

Previously, in week 0, I had a gentle introduction to Solidity, the preferred programming language of the Ethereum ecosystem. In the second week, I continued with the same trajectory and learned some new concepts, such as:

🔹Arrays & Structs
🔹Loops and Libraries
🔹Receive & Fallback
🔹Const and Immutable

I also created a simple smart contract called Fund Me and while doing so, I came to know about an interesting problem called the Oracle problem.

So, what is the Oracle problem?

As we already know, a blockchain is just a public network that stores transactions in groups of blocks. In simpler times, we had blockchains like Bitcoin with a distributed public ledger that allowed people to send value from one wallet to another in a completely decentralized way.

Today, there are some advanced blockchains like Ethereum that allows us to create complex "Dapps" with the use of smart contracts that are simply digital contracts that get executed when certain conditions are met.

So, let's say we want to write a smart contract to pay John whenever he places a bet on Manchester United and Manchester United ends up winning the match.

Should be pretty simple, right? All we gotta do is get the final match score and let the smart contract do its thing.

But there is a problem.

How would our smart contract know that Manchester United won the match?

As it turns out, smart contracts are actually not that smart. They are simply a piece of code that lives inside the blockchain, and blockchains are deterministic systems by design with no access to real-world information on their own.

Image Credit: Chainlink

Since blockchains do not have access to any real-world data or events, they don't know what's the price of ETH is, who won the football world cup, or what's the weather like today.

As a result, our smart contracts cannot interact with "off-chain" data, which is something outside their blockchain environment.

So, we cannot ask our smart contract to perform calls to external APIs to check info like match scores, they are just a piece of code that lives on the blockchain and execute what they're told to do by the code.

That's the Oracle problem!

With the help of Oracles, we can get access to that off-chain information, and write our smart contracts in such a way that they can rely on outside information fed by an Oracle, and make decisions based on that information.

On a high level, Oracles are external services that feed real-world, off-chain information into the blockchain so that our smart contract can get access to that information and execute its logical conditions based on that data.

So the main job of an Oracle is to provide a secure feed of external data to the blockchain and provide a validation mechanism.

Think of them as a trusted third party that provides a reliable feed of information to the smart contracts.

Back to the betting example.

Let's say we want to access the score of today's Manchester United game. We know there are different sources and public APIs to gather that information.

So, we can ask an Oracle to do the following for us:

  1. gather info from different trusted sources, like API-Football,
  2. compare the result of each source,
  3. through its validation mechanism, figure out the correct score,
  4. supply it to the blockchain.
So in a nutshell, an Oracle acts as a middleman between the real world and blockchain and makes sure that the smart contracts have the correct data they need to make accurate decisions.

But there is a problem with this model. We can't trust a single centralized Oracle with the information to feed our smart contract.

What if the information provided by a single Oracle is outdated, or worse, manipulated?

Credit: Chainlink

The effectiveness of our smart contract depends on the quality of off-chain data it's being fed by the Oracles. So, we need to make sure that these Oracles should be resistant to manipulation and provide accurate & updated information.

Decentralized Oracle Network

To ensure data accuracy without relying on a single source of truth, a Decentralized Oracle Network combines multiple independent oracle node operators and multiple reliable data sources to establish end-to-end decentralization.

Chainlink is a modular decentralized oracle network that can be utilized to deliver accurate data and perform external computation.

In the Chainlink network, each independent node retrieves data from an off-chain source and brings it on-chain which is then aggregated & deterministically validated by the network.

Credit: Chainlink

1️⃣ Chainlink Data Feeds: provide a quick & easy way to connect smart contracts with real-world data such as the latest asset prices, weather information, etc.

2️⃣ Chainlink Verifiable Random Functions: Since blockchains are deterministic systems, they can't have randomness.

So, Chainlink VRFs provide a way to get provably random numbers, enabling smart contracts to guarantee fairness & randomness without compromising security or usability in applications that demand randomness like lotteries, randomized NFTs, gaming, etc.

3️⃣ Chainlink Keepers: provides decentralized event-driven execution of smart contract functions after specific times or when certain events are triggered.

Chainlink Keepers will listen to different events, and once a trigger returns true, Chainlink nodes will perform whatever action we want them to perform.

For example, let's say I want to write a smart contract to withdraw funds from a fundraiser smart contract to a wallet every time the balance of my wallet goes below 1 ETH. 

To read more about Chainlink & Decentralized Oracle Networks, feel free to go through Chainlink Docs.

So, that was a gentle introduction to Blockchain Oracles. There are still plenty of things to wrap my head around about Oracles though.

Last week was insane in terms of the amount of information I had to retain, but learning new things consistently over a long period is what takes us far.

Here's a piece of advice to stick with while digging deeper through the Web3 rabbit hole:

Follow me on Twitter.

That's it for now, fren!

Thanks for reading till the end. If you found this post helpful or if I made a mistake somewhere, please let me know by commenting below. I'm a newbie so all the corrections are greatly appreciated.

Also, please consider subscribing here so that you get a notification in your mail whenever I post new stuff. Subscribing is free, and it gives me a ton of motivation to keep going.