Oracles: Bridging the Gap Between Blockchains and the Real World

The concept of decentralization is tantamount to the fundamental nature of blockchain. But how can we access off-chain data in a decentralized way?

💡 Articles
13 February 2024
Article Image

Introduction

The concept of decentralization is tantamount to the fundamental nature of blockchain. Decentralization is precisely why blockchain ecosystems brought about an era of unparalleled security, transparency, and autonomy.

However, as is with all good things in life, there’s a catch. The decentralized nature that underpins blockchain’s prowess also presents a real challenge: isolation from real-world data. Smart contracts are undoubtedly self-executing, but they’re still merely confined to the context of the blockchain. They can’t access off-chain data.

Picture this: you’ve placed a bet on the outcome of a sports match with a friend, and both of you have deposited the betting amounts in an escrow smart contract. Your team wins, and you’re ready to walk away with your hard-earned funds. But alas, the smart contract doesn’t know that your team has won and, as such, will not transfer the funds to you. You would think that a possible solution would be to have a third friend act as an arbitrator, but then we’re back to square one: we’ve removed the ‘de’ from decentralization.

Enter oracles, the bridges between blockchains and the real world. And yes, they’re just as magical 🦄 as they sound.

➡️ An oracle is any technology or service which provides off-chain data from the real-world to a deterministic blockchain. This data can include price feeds, weather information, or sports scores as an example.

Types of Oracles

On a very basic level, oracles can be categorized based on two fundamental traits. These are:

  1. Hardware vs. software oracles: Hardware oracles comprise of external hardware devices which can collect data directly from the environment, such as temperature sensors. Software oracles, by contrast, leverage APIs to collect digital data using the internet. Their interconnectedness to the internet makes them incredibly powerful since they can provide smart contracts with the most up-to-date data.
  2. Inbound vs. outbound oracles: Inbound oracles, as the name suggests, bring external off-chain data to smart contracts. These are generally the most widely recognized type of oracle. Outbound oracles, on the other hand, allow smart contracts to send data or trigger external systems based on on-chain events.

Some additional types of oracles include:

  • Cross-chain oracles: These oracles bridge data across various blockchains. This enables interoperability between different blockchains in terms of moving data or assets from one blockchain to another. Very cool.
  • Compute-enabled oracles: These are used for computations that are impractical to perform on-chain because of block constraints or cost of computation. Computing zero-knowledge proofs to ensure data privacy is one such example.

If you’ve been paying attention so far, you may have picked up on a potential problem that we could run into when fetching data using oracles. A centralized oracle node (or server) is a recipe for disaster, because you’ll end up supplying bogus and inaccurate data to hundreds of smart contracts if the node is compromised (and defeat the very purpose of blockchain in the first point). Yikes.

In a truly decentralized utopia where everyone can have their cake and eat it too, our oracles need to be decentralized as well. This is where something like Chainlink reigns supreme.

Chainlink is a decentralized oracle network. This network comprises hundreds of oracle nodes which can fetch data from a variety of sources. Chainlink employs a number of additional measures which help ensure the reliability of the provided data:

  1. Node reputation: Nodes that consistently provide accurate and reliable data build a positive reputation over time, which makes them more likely to be chosen for data retrieval.
  2. Rewards and collateral: Nodes are required to stake Chainlink tokens (called LINK, which is ERC-20 compliant) as collateral in order to participate in the oracle network. This incentivizes node operators to provide good service; not only does Chainlink reward fulfilled requests with additional LINK tokens, but a dishonest node also risks losing its stake of LINK for poor service.
  3. Data aggregation: When a smart contract requests for data, Chainlink gathers the requested data from multiple independent sources and then calculates an aggregated result. This helps eliminate outliers, providing more accurate data to the smart contract.

A Simple Example

I’m not going to dive too deep into the more practical aspects of working with Chainlink because the documentation itself is very sound. Nonetheless, I do want to demonstrate just how easy it is to get started with it.

Take a look at this code snippet below:

This funky little smart contract uses Chainlink to fetch the latest price of Bitcoin (because I’m still not over my losses yet 😔). To be specific, we:

  • Import the AggregatorV3Interface and create an interface object by pointing it to the proxy contract which can fetch the current BTC/USD exchange rate for us. The address for this proxy contract (bitcoinPriceFeedAddress) is available in Chainlink’s documentation.
  • Fetch the latest BTC/USD exchange rate through the fetchRoundData function.

At the time of writing this article, calling the getBitcoinPrice function returns a value of 2712140805133, which suggests that one Bitcoin costs slightly more than $27,000 (after catering for Chainlink’s decimals). This also suggests that I’m still suffering massive losses, but enough about that.

Ladies and gentlemen, it really is that simple.

Concluding Thoughts

If you’ve made it this far, you should have a basic understanding of what oracles are and the role that they play in the modern blockchain ecosystem. I would highly recommend anyone looking to get started in the world of blockchain development to take a closer look at Chainlink and explore the endless development possibilities that it offers.

This article is written by Zohaib Adnan , Full-Stack Developer at Antematter.io.