How to consume 3rd party web services over Ethereum Network?

Ugur Cem Ozturk
Coinmonks
Published in
3 min readMay 23, 2018

--

May sound confusing, but trust me it’s an easy-peasy work to do. So how do I come up with this tutorial? There are tons and millions of Solidity code samples that gives a simple ‘Hello World’ output. It’s cool, at least you work with a new language but not enough, not useful.

In the era of Web Development, 3rd party API call is like breathing. So I start to do research about making API calls with Solidity over Ethereum network. The solution I come up with is Oraclize, -data carrier for decentralized apps.- Unfortunatelly, this is a paid service. Fortunatelly, we will work on Ropsten Network which is a test network that we pay fake ETHs for any kind of transaction.

Requirements

  • Ethereum Address on that will work on Ropsten Network
  • Some amount of free test Ethereum

To get free Ropsten ETHs, there are some faucets. Such as: http://faucet.ropsten.be:3001/ .Simply paste your ETH address and Test Ethers will be loaded in your account within minutes.

Roadmap

1 Find a web service to consume. I’ve just build one with Express, simply returns a string. You could wotk with it, feel free to use. (https://ihk.herokuapp.com/)

2 Finding which environment fits the best for demo issues. There’re two options to compile and run our smart contract: Remix and Oraclize IDE. I’ll go with Oraclize IDE.

If you’ll work with Remix , you have to select Injected Web3, otherwise the code won’t work with Javascript VM as expected.

Let’s Code!

There it is, a Solidity code at its simplest and clearest.

So let’s go step by step.

#6 We’ll work with version 0.4.19 Compiler. Because Oraclize supports the versions between 0.4.1 and 0.4.2. To do this, do not forget the change the compiler version(0.4.19+commit.####) from Settings Tab.

#7 Let’s import the Oraclize repository.

#11 The only variable that contract stores. The result of the API call will be assigned to this variable.

#15–17 Simply, the constructor of our smart contract. We’ll talk about the update method. It’s not built-in, it’s declared by our contract that you can see at the last lines of the contract.

#19–22 The callback state that triggers after request execution. We could manipulate the resulted data here. But for this case, we don’t need any kind of manipulation.

#24–27 There it is. The place where sun never sets, the place we send the request. There is a extreamly important , life-saver point to modify a method argument based on your MIME type of the services response. We have to re-write second argument based on what our service serves. JSON,XML, etc..

If it’s JSON ;
oraclize_query(“URL”, “json(https://ihk.herokuapp.com/)");

Or XML;
oraclize_query(“URL”, “xml(https://ihk.herokuapp.com/)");

Our service will return just a text without a fancy format, simple http/text, so no remove there is casting process.

Let’s Compile our code. The result screen should seem similar to this:

Let’s click to “Create” button. The contract run, mined and done. Now, there should be section that shows the list of method that we can run over our contract. Seems like that:

The update method is the method that we consume the service, we get the data. The request will be done between 10–20 seconds.

Let’s take a deep breath. Hold it. Hold it. A little bit more. Now, slowly, exhale. Very slow. Perfect. The API call should be done.

Let’s jump to the 4th section on the top, named “Queries”.

There it is. The result. Says, “Imam hatipler kapatilsin.” -A famous, funny Turkish meme-.

See you soon.

Cheers,
Ugur Cem.

--

--

Ugur Cem Ozturk
Coinmonks

Senior DevOps Engineer @Cherry. Talks about software, cloud, and web engineering. https://www.linkedin.com/in/ugurcemozturk/