Tellor School – How to integrate Tellor 

By Tamsay | Tellor | 17 Dec 2022


Links 

Youtube link for integrating: https://www.youtube.com/watch?v=GLGCoI3Y40Y&t=2s&ab_channel=Tellor 

Youtube link for testing: https://www.youtube.com/watch?v=LDiMJNV33Sk&ab_channel=Tellor

Official links: https://linktr.ee/ 

 

Summary 

Tim explains how to integrate and test the Tellor oracle 

 

Integration

Hey everyone, we will show how to integrate the Tellor Oracle into your smart contract and then we will show how to test that integration using hard hat. So we will be using the sample using Tellor project which can be found on the Tellor GitHub. So we will be working with just this one simple contract sample using Tellor and this contract is going to inherit the using Tellor contract so to get using Tellor into your own project you just use npm install using Tellor and now your contract can import using Tellor so you just import using Tellor and then you inherit using Tellor in your own contract sample using Tellor is using Tellor. And finally we input the Tellor Oracle address into the Constructor and then you pass that Oracle address over to the using Tellor Constructor. So now your contract can read data from the Tellor Oracle. This sample using Tellor contract just has one simple function, read eth price and this function will just go and retrieve The Ether price from the oracle and then it saves it to this eth price variable. In order to read a value from the Oracle we will be using get data before which comes from using Tellor so the get data before function takes two inputs a query ID and a timestamp and it will go retrieve the latest value with this unique identifier the query ID before this time stamp. So to generate our query ID we first need to generate our query data so we want The Ether US dollar price. For that we will use the spot price query type and that takes two arguments an asset and a currency so we just input eth and USD as strings and we encode this all into bytes to get our query data once we have our query data we hash it to get our query ID which we input and to get data before and then we need a timestamp so we are going to use the current timestamp minus 15 minutes so that builds in a buffer time to allow people to Monitor and dispute any bad data that gets reported to Tellor. And then get data before is going to return a value and a time stamp retrieved so if git Data before returns a time stamp of zero that means that no value was found. Otherwise we will save the value that we found to this each price variable but since all Oracle data is saved as bytes we want to first convert those bytes into an integer so we use this slice uint function which is included in using Tellor and that will decode the value into a uint 256 value and then we save that value to eth price and now we have a simple contract which is able to use the Tellor Oracle.  

 

Testing 

Next let's go to our test file and make sure that our contract can actually retrieve data from the Oracle now in this test file we want to deploy an oracle contract submit some data to it and then we want to retrieve that data in our own sample using Tellor contract. So we probably don't want to have to deploy the whole Tellor system instead we're going to use the tower playground which is a mock Oracle. Which is included in using Tellor and it just lets you easily submit data to the Oracle and retrieve data and make sure that your own contract can work with the real Tellor but you don't have to deal with all the overhead of the real system. So we are able to access the Tellor playground through using Tellor by importing it like this. We get an ABI and a byte code and then we can deploy that contract in our test file first creating a contract Factory and we input the Avi and bytecode and then we deploy our Tellor playground and then once the playground is deployed, we can deploy the sample using Tellor contract. Remember we need to input the Tellor Oracle address so in this case we're inputting the playground which we just deployed and now we can go on to testing our contract so in the read eth price test we are just going to submit a value and then read that value from our sample using Tellor contract and then we'll make sure that the value that was saved in this contract was the value that we expected. So to submit a value we're going to use this submit value function which takes four arguments a query ID, the Ether price that we are reporting, a nonce and the query data so first let's generate our query data and query ID. So we need our query data in bytes and to do that we will use the ABI coder which is included in the ethers.js library. So first let's encode our query data arguments and because we're using a spot price query type, we need these two arguments the asset and the currency so we have eth USD once we have those arguments we can encode our query data so we input our query type spot price and then the encoded arguments and now we have our query data and then once we have our query data we can hash that to get our query ID so now we have two of the arguments needed for submit value and now let's generate our ether price so here let's report a price of two thousand and we need a decimal precision of 18 decimal places. Now we need to encode that value into bytes to report it. So again we'll use ABI coder and encode it into bytes and now we can submit a value to the oracle so we just input the query ID, the value encoded into bytes the knots which you can always use zero so just use zero in your tests and then the query data now after we submit that value we want to increase the block timestamp by 15 minutes because remember we included a buffer time in the sample using Tellor contract so that contract won't be able to retrieve the value we just submitted until 15 minutes have passed. So, we input 15 minutes in seconds plus one second and now our contract should be able to read the Ether price from the oracle. So we'll call the read eat price function from sample using Tellor and then let's read the saved price from sample using Tellor and then we ensure that the price that was saved is the value that we expected and that's just a simple use case of integrating the Tellor Oracle into a contract and testing that integration. 

 

How do you rate this article?

9



Tellor
Tellor

In this blog I will post transcriptions of videos made by the Tellor team.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.