
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.
—
By the above definition we can summarize by saying that oracles were 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 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 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 of 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 Oracles with . 😉
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 . It’s really neat to see what they are doing. You can find out more about the inner workings for Chainlink by reading their !
Where does Amberdata come in?
It’s in the name! 😃 Amberdata is a data company with big visions for and they’re always looking for innovative ways to advance the space.
They’ve doing been data for a few years but have recently added to their repertoire making them the one stop for all data involving blockchains. In addition, they’ve just partnered with 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 . Currently they offer the ability to access a ’s USD price by it’s address right inside your smart contract. Super rad, if you ask me.
TLDR; The full example is available on remix via this .
Setup
We’ll begin be creating a basic contract that inherits from the contract:
pragma solidity ^0.4.24;
contract AmberdataChainlink is ChainlinkClient
}
Job ID
Every Chainlink request requires a , 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 address and the Chainlink Oracle address. To do this we’ll use methods that we’ve inherited from the ChainlinkClient contract: and :
constructor() public
Each network has a different address for the LINK contract an Oracle contract. Chainlink’s documentation has a list of available networks and their corresponding addresses .
Note: You contract will need LINK sent to your deployed contract, the same LINK used for the network you deploy to. We will walk through adding the LINK via faucet later.
The Request Method
Next, let’s create a method:
function requestPrice() public
Again, taking full advantage of we going to use a few more inherited methods: and t .
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 . First we’ll add the address of the that we want get the price for:
req.add("token", "0x514910771AF9Ca656af840dff83E8264EcF986CA");
That’s the address for the . 👍
Next, we’ll specify what we want to multiply the input by with the .
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 method from the contract:
sendChainlinkRequest(req, 1 * LINK);
Each request requires payment of 1 LINK . HereLINK is a reference to a constant :
uint256 constant internal LINK = 10**18
You can read more about making requests with Chainlink .
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)
We’ll name the second parameter _price 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 ready to rock and roll. 🤘
When you have the time go through . This will be a high level speed through version focused just on our method we just built.
Pre-requisites:
- Have installed
- on your Ropsten account
- for the same account
More detailed explanation .
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!
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 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
balanceOfmethod in the LINK 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 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 …
- — for any asset pair, , etc.
- Blockchain Data —Access to any and all data pertaining to , , etc. (full transaction or block validation on chain!? 🤪)
…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 is headed. It’s fascinating to see all of the different solutions that are being proposed.
More Tutorials:
For more information about Amberdata.io:
- Website:
- Twitter:
Published at Thu, 23 May 2019 18:06:03 +0000