DEV Community

loading...
Cover image for Off-chain Data in Ethereum

Off-chain Data in Ethereum

patrickalphac profile image Patrick Collins Updated on ・4 min read

Introduction

For my first article on Dev.to, I figured I'd speak a little about something I'm familiar with, data & blockchain! And in particular, accessing off-chain data in ethereum and all blockchains.

Getting off-chain data has been an issue for ethereum engineers for a long time. Smart contracts weren't able to do anything more interesting than token transfers. This is great, but it doesn't allow for ethereum to do what it was ultimately designed to do - create smart contracts.

Why?

Because off-chain data in ethereum doesn't work in a deterministic system!!

If you clicked that link above, you'll see them talk about API calls in blockchain. Blockchains work because all nodes in the system can come to a consensus. Consensus is how all nodes agree upon a value. In a system such as blockchain, were all information is contained, this is really easy.

Verifying off-chain data and on-chain

5 + 5 = 10 
Enter fullscreen mode Exit fullscreen mode

This simple math problem is something that's easy for all nodes on the network to understand. All nodes can add 5 together, and if a node returns a result other than 10, they can easily exclude that node's blocks.

However if we have something like:

API call result from API Y + API call result from API Z = 10
Enter fullscreen mode Exit fullscreen mode

Then no node on the platform will ever come to a consensus! APIs are updated, changed, depreciated, and hacked, so depending on when a node goes to verify this sequence, each node will get wildly different results. This breaks the determinism of the blockchain and means that as part of the native system getting off-chain data doesn't work.

But.... but we want smart contracts to empower the world right? How can we do that if a smart contract can't even tell me who is president right now?

Enter oracles

Blockchain Oracle

A blockchain oracle is any entity that brings off-chain data on-chain, or sends on-chain data to the real world. They are the middleware between the real world and the blockchain.

They make a transaction on-chain with the off-chain data, and boom! We now have a way to validate the data on-chain, all other nodes just validate the data of the transaction the same way they validate every other transaction.

Cool! So we are all set right?

Nope. You're forgetting about a big issue.

Decentralism

Decentralized oracle

Smart contracts are meant to be decentralized. This means, they have no central point of failure, now if we pull our data from one node, guess what we are doing? We are adding a centralized point of failure.

Decentralized Oracle

So how do we address this?

We pull from a lot of nodes!

The same principle that makes blockchain work with many nodes securing the system is the principle that powers the idea of decentralized oracles. We also want to pull from multiple off-chain data sources so our ethereum contracts can stay decentralized.

What this looks like

Chainlink is the standard for decentralized oracles on ethereum and blockchains in general. As a chain agnostic technology chainlink is compatible with any chain, but let's look at an ethereum example.

Make any API call

For this, you'll need the following:

  1. Metamask/a eth wallet
  2. Testnet LINK
  3. Testnet ETH

You can follow the guide here to get all this setup.

This link also has a guide on how to do the rest as well!

Step 1. Decide what data you want

This can be as easy as picking an out an API. For this, let's say we want to get the volume data of ETH/USD from Cryptocompare. Here is the API we will be using.

Note, that the return type is important, we will be writing this tutorial in solidity so we know we want to get the volume as a uint256.

https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD
Enter fullscreen mode Exit fullscreen mode

Step 2. Pick your oracle

First, let's pick an independent chainlink oracle/node to pull our data from. For this, we will just be showing you how to pull from a single node. This is just a centralized example! To decentralize it you just replicate this across multiple independent nodes and then aggregate the responses on-chain.

You can choose your nodes on node listing services like market.link. You'll want to switch to a testnet like kovan since we will be testing this on kovan!

Chainlink Node Listing service

You can look up the data that you want to get, for example you can search for get > uint256 and see the oracles that return that data. You'll want to grab the oracle address, and then the jobid of the job that returns the uint256 and makes an httpget call.

For our demo, we will use:

address oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
bytes32 jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
Enter fullscreen mode Exit fullscreen mode

And this is on Kovan.

Step 3. Setup your remix

Hit this link!

This will bring you directly to remix, where you can compile & deploy your contract! From here, you can follow the Chainlink documentation to get the rest of it sent and your first Chainlink API call made!

Summary

I hope this gave you some insight into off-chain data and how Chainlink oracles power the web. These decentralized oracles are currently powering many of the top projects in defi, and this technology is securing billions of dollars.

More and more engineers are joining hackathons to build awesome projects, join the community, and become apart of something great.

Hope to see you in the technical discord!

Main image above from phive2015 from Getty Images Pro

Discussion (1)

pic
Editor guide
Collapse
patrickalphac profile image
Patrick Collins Author

Happy to answer any questions if someone has some!