Most software engineers, when looking to get data from outside their programs, look to get data from API calls or HTTP GET/POST requests. Similarly, on a blockchain, people look to get data from an external API as well. This article will teach you how to do that! Let’s get going, here is step one:

You actually can’t get data from an API call the same way you get data from normal software applications. However, in order for our smart contracts to do anything worthwhile, we would want them to interact with the outside world. So what gives?

The Ethereum blockchain was designed to be entirely deterministic. This means, that if I took the whole history of the network, then replayed it on my computer, I should always end up with the correct state.

Since the internet is non-deterministic and changes over time, then every time I replayed all of the transactions on the network, I would receive a different answer.

Instead of the contract calling the API directly, we need to have it call an application that can interact with the outside world, and have it record its findings on-chain. These are called Oracles. Oracles are any application that interacts with data from the outside world and reports it back on-chain.

If we replayed the chain without oracles and used regular API calls, the API calls might have changed, and we’d get different results.

Thanks for the technicality definition update, but how do I get my data into my solidity application?

Ok ok, let’s get into what you actually came here for. In our example, we will be just trying to get the price of ETH in USD.

For beginners:

Now if you’re a TOTAL NOOB here with this Remix, solidity, Chainlink, and want a step by step guide, feel free to check out my more in-depth guide: Write your first solidity contract or Chainlink’s documentation. We all were imposters once :D

For slightly experienced or advanced users, including remix:

If you want to follow along and just press the buttons without having to choose your own oracles, you can follow along with all the code and deploy it with me in Remix here (code included in the hyperlink). HOWEVER, THAT CODE IS FOR DEMO ONLY AND YOU SHOULD NOT USE IT FOR PRODUCTION. You should put JobIDs and addresses as parameters. You can follow that along with us in Remix here for the more productized version. You’ll notice two of the steps are missing. This is intentional ;)

Go to the gists section. Be sure to send ROPSTEN LINK to your contracts after deploying them, otherwise, you’ll get a gas estimation error. You can get ropsten LINK here. DO NOT SEND YOUR ACTUAL LINK WHEN YOU ARE TESTING. Please. Please. Please. You can use the withdrawLink function if you accidentally send it LINK.

One other note: I tend to use oracle and node interchangeably, while technically not correct, the distinction between the two for this article is negligible.

1. The naive approach

The first approach most blockchain engineers start with is just finding an oracle technology, giving it the URL of our API call, and then having it do its thing of reporting the data on-chain for us to interact with. There are a number of oracles we can use to do this. We will be using Chainlink for reasons you’ll see soon.

To use Chainlink, we first have to pick a Chainlink oracle/node. These are independently operated smart contracts on the blockchain that allow you to interact with the world. To find one, we can go to node listing services like Linkpool’s Market.Link, and just choose a node. We then have to make sure that the node has a “http Get > uint256” job. Not every node can make URL calls and return a Uint256 (basically an Int), but most do!

The node we choose was a Linkpool node at the address in the ORACLE variable. All the magic happens in the requestEthereumPrice function. If you’ve followed Chainlink’s tutorials before, you know that this is the most simple way to get data using Chainlink.

This is great! We can get data. This is fine if you’re just testing and want to quickly develop your code, but please do not use this for a production smart contract. Why? It comes with one major issue:

trustless, decentralized, smart contracts

You are pulling from one oracle and one data provider. Now Linkpool is one of the most trusted Chainlink node services out there, but your application needs to be trustless. Not only that, but you’re also getting your data from one source as well, now you have TWO points of failure in your application. CryptoCompare and LinkPool are both massive liabilities that can be easily remedied. This is a rant worthy point that I will spare you from here, but let me drill it once more.

Your smart contract needs to never have a single point of failure, as that point can be bribed, hacked, be out-of-service at execution time, or many other reasons that leave your smart contract worthless.

So how do we fix this issue?

#api #blockchain #solidity #decentralization #cryptocurrency

How to Make API calls on Blockchain
3.45 GEEK