Smart Contract Oracles with Amberdata.io and Chainlink!

Taylor Dawson
amberdata
Published in
7 min readMay 23, 2019

--

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:

pragma solidity ^0.4.24;contract AmberdataChainlink is ChainlinkClient {
constructor() public {}
}

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:

bytes32 constant JOB_ID = bytes32("447f3b6fac1240ab91d3679fba00baf6");

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: setChainlinkToken and setChainlinkOracle:

constructor() public {
setChainlinkToken(0x20fE562d797A42Dcb3399062AE9546cd06f63280);
setChainlinkOracle(0xc99B3D447826532722E41bc36e644ba3479E4365);
}

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:

function requestPrice() public {}

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

We’ll start by defining the Chainlink Request:

Chainlink.Request memory req = 
buildChainlinkRequest(
JOB_ID,
this,
this.fulfill.selector
);

You may find this familiar as it is similar to making an http request in javascript. The last parameter, this.fulfill.selectoris 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:

req.add("token", "0x514910771AF9Ca656af840dff83E8264EcF986CA");

That’s the address for the LINK Token. 👍

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

req.addInt("times", 100);

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

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

sendChainlinkRequest(req, 1 * LINK);

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

uint256 constant internal LINK = 10**18

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:

uint256 public currentPrice;

Now for the method:

function fulfill(bytes32 _requestId, uint256 _price) 
public recordChainlinkFulfillment(_requestId)
{
currentPrice = _price;
}

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

You’ll notice the function modifier recordChainlinkFulfillment . 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 balanceOf 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:

--

--

Taylor Dawson
amberdata

Engineer @Blocknative | Technophile | Blockchain Enthusiast