Smart Contract Oracles with Amberdata.io and Chainlink!

Taylor Dawson
May 23 · 7 min read
Lycurgus Consulting the Pythia (1835/1845), as imagined by Eugène Delacroix

What are oracles?

An oracle is a person or agency considered to provide wise and insightful counsel or prophetic predictions or precognition of the future, inspired by the gods. As such it is a form of divination.
Wikipedia

By the above definition we can summarize by saying that oracles were a bridge between our realm and the spiritual realm — a source of knowledge, truth, wisdom.

What does this have to do with Blockchain?

As you may know when it comes to the blockchain realm, it is self contained, restricted, and unable to access data outside of it’s own network. It has no context for what is occuring in the real world. Thus, oracles provide a way for the blockchain to access external resources that would otherwise be unavailable to them.

Why do we need oracles?

There are many many reasons for using oracles and accessing external data. In fact, it is one of the main barriers for widespread adoption of blockchain integrated applications. Blockchains are trustless but the world isn’t. Bridging the gap between a trustless and trusted system is a huge undertaking and many are actively working at finding solutions.

And that’s just the tip of the iceberg, you can learn more about blockchain oracles with a quick google search. 😉

How’s it work?

Chainlink is a decentralised oracle system that aims to protect the integrity of data by aggregating it from multiple sources, finding a consensus amongst the nodes and then sending that data to the blockchain. It’s really neat to see what they are doing. You can find out more about the inner workings for Chainlink by reading their whitepaper!

Where does Amberdata come in?

It’s in the name! 😃 Amberdata is a blockchain data company with big visions for blockchain adoption and they’re always looking for innovative ways to advance the space.

They’ve doing been blockchain data for a few years but have recently added Market Data to their repertoire making them the one stop shop for all data involving blockchains. In addition, they’ve just partnered with Chainlink to become an oracle for Market Data.

Intro

We’re going to walk you through a quick example of what it looks like to use Amberdata as an oracle on the Ethereum blockchain. Currently they offer the ability to access a token’s USD price by it’s address right inside your smart contract. Super rad, if you ask me.

TLDR; The full example is available here on remix via this gist.

Click the gist drop down in the left panel.

Setup

We’ll begin be creating a basic contract that inherits from the ChainlinkClient contract:

Job ID

Every Chainlink request requires a Job ID, which is a pre-determined string identifier allowing methods to be triggered according to the method associated to the string.

So right above the contract declaration we are going to define the Job ID:

In the constructor we are going to specify the LINK Token address and the Chainlink oracle address. To do this we’ll use methods that we’ve inherited from the ChainlinkClient contract: and :

Each network has a different address for the LINK token contract an oracle contract. Chainlink’s documentation has a list of available networks and their corresponding addresses here.

Note: You contract will need LINK token sent to your deployed contract, the same LINK tokens used for the network you deploy to. We will walk through adding the LINK token via faucet later.

The Request Method

Next, let’s create a method:

Again, taking full advantage of OOP we going to use a few more inherited methods: and .

We’ll start by defining the Chainlink Request:

You may find this familiar as it is similar to making an http request in javascript. The last parameter, is the callback function that will handle the returned data. We’ll implement this in the next step.

We’re going to add parameters to the request using Adapters. First we’ll add the address of the token that we want get the price for:

That’s the address for the LINK Token. 👍

Next, we’ll specify what we want to multiply the input by with the multiply adapter.

We multiply the price by 100 because Solidity can’t handle decimal places.

Finally, we’ll initiate the request using the method from the ChainlinkClient contract:

Each request requires payment of 1 LINK token. Here is a reference to a constant declared in the ChainlinkClient contract:

You can read more about making requests with Chainlink here.

The Fulfill Method

If we stopped now we’d get an error. We need to implement the callback function. Don’t worry it’s dead simple.

Declare a variable at the top of the contract:

Now for the method:

We’ll name the second parameter although this could be any data returned from the oracle.

You’ll notice the function modifier . This is a check to ensure that the caller and request Id are valid.

Here’s what the entire method should look like:

Play Time

If you haven’t already head over to remix where I have the contract ready to rock and roll. 🤘

When you have the time go through Chainlink’s Example Walkthrough. This will be a high level speed through version focused just on our method we just built.

Pre-requisites:

More detailed explanation here.

Once you’ve done all of that you’re ready for the fun part.

Compile

The right panel should show no compile errors.

Deploy

Go to the run tab and then make sure your environment is set to Injected Web3.

If you are on the Ropsten Test Network in Metamask you should see ropsten as in the above image. If not make sure set it in Metamask.

Deploy!

*click*

This should open a window and ask for confirmation.

Once the transaction goes through you’ll see your contract below the deploy button.

Click the copy icon to copy the contract address and then we’ll use Metamask to send LINK to our contract (the contract needs LINK to make requests).

Now that your contract is funded with LINK, it’s time to get the token price!

Although out of scope for this tutorial, it would be best practice to have a function that checks the balance of your contract’s account (using the method in the LINK token contract) to ensure that your contract does not run out and break your dApp!

Click the requestPrice button, confirm the transaction and wait for the magic to happen. It could take up to 15 seconds depending on network conditions.

Metamask and Remix should notify you of the successful transaction. Excellent.

Next click the currentPrice button to access the current price variable that was set by our fulfill method and then…

Heyyo!

But wait!! Let’s confirm before we get too excited:

Booyah!

Okay that’s pretty cool! I bet you’re already starting to imagine all of the different possibilities.

That’s just the tip of the iceberg. Amberedata.io has a lot more endpoints available

…so look at for future tutorials on how to use them!

Conclusion

We just went over what oracles are, why they are useful, and how to interact with one using Amberdata.io and Chainlink.

This is definitely where the world of blockchain is headed. It’s fascinating to see all of the different solutions that are being proposed.

Learn more by visiting the Chainlink website or joining them on Telegram and Twitter.

More Tutorials:

For more information about Amberdata.io:

amberdata

We provide a platform for monitoring, searching, analyzing, and securing public and private blockchains. This platform allows for operational intelligence for blockchain infrastructure, on-chain decentralized applications, token utilization, and transactions.

Thanks to Trevor Clarke

Taylor Dawson

Written by

Open Source / Community Developer @Amberdata.io | Technophile | Blockchain Enthusiast

amberdata

amberdata

We provide a platform for monitoring, searching, analyzing, and securing public and private blockchains. This platform allows for operational intelligence for blockchain infrastructure, on-chain decentralized applications, token utilization, and transactions.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade