Ethereum: Experimenting with Oracles & Why It Matters

Karen Scarbrough
8 min readJan 29, 2018

--

You can have data without information, but you cannot have information without data. — Daniel Keys Moran

Ethereum continues to be incredibly dynamic, and while the developments within Ethereum are pretty captivating, similarly, the ecosystem surrounding Ethereum is equally as compelling. Of significant note, there are tools available that allow you to pull data from an API to be used in smart contracts. From Houston, referencing API, you might think American Petroleum Institute. But in this world, API refers to the application programming interface, and this is what allows different websites to communicate with one another. For example, what allows Kayak to pull in flight information from hundreds of different airlines in one swift search? The API. What allows Booking.com to pull in hundreds of hotels rate in a click? The API.

Why APIs Matter

Now, Kayak was only founded back in 2004. For comparison, Amazon was founded in 1994. Why? In 1994, there were less than 3,000 websites. In 1995, there were 23,500 websites. (And to satisfy curiosity, today, there are over 1.3 billion websites in existence.) Why didn’t anyone pick up on the advantages of using an APIs until a decade later? Well, although decades in the making, the internet’s work with APIs didn’t start until the early 2000s when Roy Thomas Fielding published his Architectural Styles and
the Design of Network-based Software Architectures
, launching a whole new aspect to web applications. The ability to dynamically access information transformed our usage of the internet, and most of us never knew it happened.

An Overview of Oraclize

I referenced Oraclize in my last writing in relation to leveraging data to create smart contracts capable of responding to changing pricing data to determine true manufacturing cost and pricing. To expand upon that idea, I put together a simple example here with some more in depth discussion on how oracles with Oraclize currently work in Ethereum .

Let’s go over how the process with Oraclize currently works by understanding a bit more about how blockchains and smart contracts work.

A pivotal aspect of a public (and private) blockchain is its ability to deterministically verify transactions. This means that a proof of a transaction on the Bitcoin blockchain that happened in 2010 should still be verifiable on the Bitcoin blockchain today. Consider that transactions on public blockchains like Ethereum and Bitcoin must be generated from what is called an Externally Owned Account (EOA). This means that a transaction must be initiated from off the blockchain. More importantly, smart contracts cannot watch and respond to data like the web applications that we have become familiar with because the transaction must be initiated off chain.

Now, we can watch what happens on a blockchain, sure. There are several applications that currently watch the blockchain. Again, Crytokitties is such an easy example. Check out their marketplace, and refresh the page after a couple of minutes and you’ll see that the price updates. This is the webpage “watching” the Ethereum blockchain, not a smart contract watching for changing data.

Why can’t smart contracts watch and respond to data? Well, without specifically inputted data on a verifiable transaction, how is the blockchain going to verify that a certain piece of information off-chain was valid at a specific value at a certain time of proof? At this point, they cannot. Data on a blockchain cannot be trusted if it cannot be repeatedly proven.

This is where Oraclize comes in with a solution. We want smart contracts to be able to verify data coming from external data sources, like an API. Oraclize provides a smart contract interface as a “centralized party” that verifies the data coming from an API via a TLSNotary proof. How does this work? Oraclize has set up a smart contract that allows you to query data from an API. This is open source and available on the Ethereum blockchain, so that you can use this contract in your own applications to ask Oraclize to grab data from an API. Once the information from the API is fetched, Oraclize uses a TLSNotary proof to verify that a result was given by a specific server at a specific time. Now, currently, this proof is actually performed on an AWS (Amazon Web Services) virtual machine. Here’s the give and take, TLSNotary proofs on the blockchain would be significantly more expensive and since the content of a TLSNotary proof can vary in size, this would add to complexity. Also, although a malicious attacker could still theoretically hijack the server to provide faulty data (which could theoretically happen anywhere), with this design, there is no ability to fake results through breaking into the smart contract. Although the centralization around AWS and Oraclize can seem paradoxical to a decentralized blockchain economy, if you look at the whitepaper for the TLSNotary proof, it was only published in 2014. After proposal in 2013, Ethereum only went live in 2015. All of this is still very new and developing, and it is the edge of what we have available now. We did not wait for the smart phone to start developing messaging applications in the 90’s, and we probably will not wait for a “better” oracle to start using them now.

A Simple Example

In a nod to one of the best comedies to come out of the ’80s, let’s take a look at pulling the producer price index of frozen concentrated orange juice into a smart contract from Quandl. Each month Quandl pulls together the producer price index for frozen concentrated orange juice into a table in which you can use the API to pull this data into your own application.

Now to the smart contract below.

Let’s take it from top to bottom — just a high level overview.

Line 3–5: At the top, you can see that I am importing oraclizeAPI_0.5.sol, Oraclize’s smart contract that allows you to ask for an oracle. By inheriting the contract usingOraclize that is within oraclizeAPI_0.5.sol into my contract FrozenOJ through line 5, I am importing (inheriting) all functions for me to access through the use of usingOraclize.

Line 6: Next, I make global variable for the producer purchase index of frozen concentrated orange juice, FrozenOJPPI. This is something that if desired, I can pull into other smart contracts in the future or use with additional functionality in this contract.

Lines 7 & 8: Recall above when we discussed that webpages can “watch” for changes on the Ethereum blockchain? This is done through events — here we set up the ability to watch for each time a query is made and the ability to watch for each time there is a new Frozen OJ PPI.

Line 10: This function only runs when we first create the contract, and it calls the function update() so that the FrozenOJPPI is automatically grabbed upon creation of the contract. This first call using update() is free, after that it will cost us.

Jump to Lines 20–23: This is where the query is sent to Quandl’s webpage using Oraclize’s contracts.

Back to Lines 14–18: Here is where Oraclize calls back to our contract after having verified the information that we requested, and a new value for FrozenOJPPI is stored.

Next, using Truffle, which is used to develop Ethereum applications, I deployed the above contract on the Rinkeby Ethereum test network. Ethereum has a few running test network that work with “fake” ether, and here developers can test their applications without having to spend real ether. These networks work nearly exactly as the real Ethereum main network except that the mining is slightly different — that’s another story.

One of the easiest ways to connect your frontend to the Ethereum test networks (and main network for that matter) is through the MetaMask Chrome extension. I used webpack to build my simple site, and serving the application locally on my computer via http://localhost:8080. Upon launch, I am asked to verify a transaction as below. This is because on loading the webpage, I ask that a call be made to the FrozenOJ smart contract to get the latest FrozenOJPPI from Quandl using the function update().

In the documentation, you’ll see that the first call to oraclize_query is free on contract creation; however, additional calls using Oraclize do cost a cent or two as outline here. Since I am running on Rinkeby here and not using real money, under the Amount parameter above, I am sending an additional $1.05. If you look at Line 20 in the code above, this is why the update function is listed as payable .

After a few seconds, we see that the latest of PPI of Frozen Concentrated Orange Juice is just as expected from the Quandl site, $209 at the time of writing. And later, when we load the page, it will update accordingly as well.

Overall, this was an incredibly simplified, barebones example of how Oraclize can be used to pull outside data into smart contracts. In production, there would be much optimization needed — if you are not familiar with working in Ethereum applications, you can even see, where the heck did MetaMask come from? Yes, at the moment, using Ethereum applications is different even before we add oracles as we have to connect to the Ethereum blockchain in order to be able to use them.

Now, we understand how utilizing oracles in smart contract allows us to more broadly reach for information outside the world of blockchain. The pace of change is fast, but what matters more is the adoption rate. What drives adoption rates is value. In the new oilfield of data, there is much that can be aggregated to leverage that value, and tools like Oraclize are paving the path to get there.

Of course, you have to ask yourself, do you think smart contracts will be of more value due to their environment or because of their own “genetic” makeup or code? Well, in keeping with my frozen orange juice, Trading Places theme, I’d be ready to bet the Duke brothers one whole dollar either way…

All code is publish here with run instructions.

Questions? Comments? Please use the below or contact me via kscarbrough1@gmail.com. Thanks for reading.

--

--

Karen Scarbrough

“If anything’s gonna happen, it’s gonna happen out there…” Captn’ Ron