Off-chain Data in Ethereum
Tags: blockchain, solidity, chainlink, offchaindata
Published: 2020-10-28 23:56:51

# 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](https://blog.chain.link/apis-smart-contracts-and-how-to-connect-them/) 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!](https://ethereum.stackexchange.com/questions/301/why-cant-contracts-make-api-calls)! 
 
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](https://polkadot.network/tag/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 
```

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:

```bash
API call result from API Y + API call result from API Z = 10
```
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](https://medium.com/better-programming/what-is-a-blockchain-oracle-f5ccab8dbd72) 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](https://dev-to-uploads.s3.amazonaws.com/i/spkl9x0xhacb391ljqz6.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/yl5u7mtgy5bsw39gebvc.png)

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](https://chain.link/) 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.](https://docs.chain.link/docs/install-metamask)

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](https://www.cryptocompare.com/). 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
```

### 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](https://market.link/). You'll want to switch to a testnet like kovan since we will be testing this on kovan!

![Chainlink Node Listing service](https://dev-to-uploads.s3.amazonaws.com/i/77txbzvtx5yu4mg0bpvp.png)

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";
```
And this is on Kovan.

### Step 3. Setup your remix

[Hit this link!](https://remix.ethereum.org/#version=soljson-v0.6.7+commit.b8d736ae.js&optimize=false&evmVersion=null&gist=8a173a65099261582a652ba18b7d96c1)

This will bring you directly to remix, where you can compile & deploy your contract! From here, you can follow the [Chainlink documentation](https://docs.chain.link/docs/deploy-your-first-contract) 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](https://defipulse.com/), and this technology is securing billions of dollars. 

More and more engineers are [joining hackathons](https://hack.chain.link/) to build awesome projects, join the community, and become apart of something great.

Hope to see you in the [technical discord](https://discord.gg/2YHSAey)!

_Main image above from phive2015 from Getty Images Pro_