LogoLogo
  • The Basics
    • Welcome
    • Fundamentals
    • Contracts Overview
    • Contracts Reference
    • Tutorials
  • Getting Data
    • Introduction
    • Solidity Integration
    • User Checklists
    • Local Testing
    • Testnet
    • Creating a Query
    • Funding a Feed
    • Tellor Functions
    • DataSpecs Registry
    • SnapShot Vote Results
  • Reporting Data
    • Introduction
    • Becoming a Reporter
    • Getting Paid
  • Disputing Data
    • Introduction
    • Monitoring
    • How to Dispute
    • Voting/Resolution
  • Vulnerability Disclosure
Powered by GitBook
On this page
  • Connecting to the Oracle
  • Installation
  • Importing
  • Reading data

Was this helpful?

  1. Getting Data

Solidity Integration

PreviousIntroductionNextUser Checklists

Last updated 1 year ago

Was this helpful?

Connecting to the Oracle

To use Tellor data, you can use the helper contract. After connecting it to the oracle, you can read a value using your queryId. This guide uses the BTC/USD SpotPrice as an example query.

Example:

Video:

Installation

To install usingtellor, run one the following commands:

Hardhat:

npm install usingtellor

Foundry:

forge install tellor-io/usingtellor

Importing

To import the UsingTellor contract into your Solidity file, pass the desired Tellor address (see the for the address) as a parameter:

pragma solidity >=0.8.0;

import "usingtellor/contracts/UsingTellor.sol";

contract MyContract is UsingTellor {

  constructor(address payable _tellorAddress) UsingTellor(_tellorAddress) {

  }

  // ...

}

Reading data

_getDataBefore(_queryId,block.timestamp - 20 minutes);

require(block.timestamp -`` _timestampRetrieved < 24 hours);

In the example below, we add a function getBtcSpotPrice that reads the BTC/USD price feed from the Oracle:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.3;

import "usingtellor/contracts/UsingTellor.sol";

contract ExampleContract is UsingTellor {

    ...

    function getBtcSpotPrice() external view returns(uint256) {
    
      bytes memory _queryData = abi.encode("SpotPrice", abi.encode("btc", "usd"));
      bytes32 _queryId = keccak256(_queryData);
      
      (bytes memory _value, uint256 _timestampRetrieved) =
          _getDataBefore(_queryId, block.timestamp - 20 minutes);
      if (_timestampRetrieved == 0) return 0;
      require(block.timestamp - _timestampRetrieved < 24 hours);
      return abi.decode(_value, (uint256));
    }

Note: In the constructor on line 7, you need to specify the Tellor . For testing, you can use a Tellor Playground address. In production, use the Oracle address on your chosen network.

You can either use ourtoand hardcode it, or use solidity to generate it. Once you have created a queryId, you can add the Tellor data feed to your contract code.

for reading Tellor data is to use the_getDataBefore function with a buffer time that allows time for bad values to be disputed:

It's also best practice to require/ For example:

Next up! Please be sure to review the which guide you through the necessary preparations & processes for using the Tellor oracle

UsingTellor
Sample project using Tellor
Integration Tutorial
references page
contract address
QueryId builder
create a queryId
User Checklists
The best practice
check that the data is not too old.