The Tellor oracle is a decentralized oracle. It provides an option for contracts to interact securely with and obtain data from off-chain.
This repository aims to provide an updated version of sample code that uses Tellor by using Ethers.js, Waffle,and Hardhat.
For more in-depth information about Tellor, check out our documentation, whitepaper and FAQ page.
Quick references are included below:
This repo already includes the usingTellor package.
git clone git@github.com:tellor-io/sampleUsingTellor.git
npm install
Just inherit the UsingTellor contract, passing the Tellor address as a constructor argument:
Here's an example:
contract PriceContract is UsingTellor {
uint256 public btcPrice;
//This Contract now has access to all functions in UsingTellor
constructor(address payable _tellorAddress) UsingTellor(_tellorAddress) public {}
function setBtcPrice() public {
bytes memory _b = abi.encode("SpotPrice",abi.encode("BTC","USD"));
bytes32 _queryID = keccak256(_b);
bool _didGet;
uint256 _timestamp;
bytes _value
(_didGet, _value, _timestamp) = getCurrentValue(btcQueryId);
//fast bytes to uint conversion //https://stackoverflow.com/questions/63252057/how-to-use-bytestouint-function-in-solidity-the-one-with-assembly
require(_value.length == 32);
assembly {
btcPrice := mload(add(_value, 0x20))
}
}
}
Mainnet - 0x88dF592F8eb5D7Bd38bFeF7dEb0fBc02cf3778a0
Rinkeby - 0x88dF592F8eb5D7Bd38bFeF7dEb0fBc02cf3778a0
/**
* @dev Retrieve value from oracle based on requestId/timestamp
* @param _queryId being requested
* @param _timestamp to retrieve data/value from
* @return uint value for queryId/timestamp submitted
*/
function retrieveData(bytes32 _queryId, uint256 _timestamp) public view returns(uint256);
/**
* @dev Gets if the mined value for the specified requestId/_timestamp is currently under dispute
* @param _queryId to lookup
* @param _timestamp is the timestamp to look up miners for
* @return bool true if requestId/timestamp is under dispute
*/
function isInDispute(bytes32 _queryId, uint256 _timestamp) public view returns(bool);
/**
* @dev Counts the number of values that have been submited for the request
* @param _queryId the query to look up
* @return uint count of the number of values received for the requestId
*/
function getNewValueCountbyRequestId(bytes32 _queryId) public view returns(uint);
/**
* @dev Gets the timestamp for the value based on their index
* @param _queryId is the query to look up
* @param _index is the value index to look up
* @return uint timestamp
*/
function getTimestampbyRequestIDandIndex(bytes32 _queryId, uint256 _index) public view returns(uint256);
/**
* @dev Allows the user to get the latest value for the requestId specified
* @param _queryId is the query to look up the value for
* @return bool true if it is able to retreive a value, the value, and the value's timestamp
*/
function getCurrentValue(bytes32 _queryId) public view returns (bool ifRetrieve, bytes memory _value, uint256 _timestampRetrieved);
/**
* @dev Allows the user to get the first value for the requestId before the specified timestamp
* @param _queryId is the query to look up the value for
* @param _timestamp before which to search for first verified value
* @return bool true if it is able to retreive a value, the value, and the value's timestamp
*/
function getDataBefore(bytes32 _queryId, uint256 _timestamp)
public
view
returns (bool _ifRetrieve, bytes memory _value, uint256 _timestampRetrieved);
For ease of use, the UsingTellor
repo comes with a version of Tellor Playground system for easier integration. This version contains a few helper functions:
/**
* @dev A mock function to submit a value to be read without miners needed
* @param _queryId The tellorId to associate the value to
* @param _value the value for the queryId
* @param _nonce the current value count for the query id
* @param _queryData the data used by reporters to fulfill the data query
*/
function submitValue(bytes32 _queryId, bytes calldata _value, uint256 _nonce, bytes memory _queryData) external;
/**
* @dev A mock function to create a dispute
* @param _queryId The tellorId to be disputed
* @param _timestamp the timestamp of the value to be disputed
*/
function beginDispute(bytes32 _queryId, uint256 _timestamp) external;
/**
* @dev Retreive value from oracle based on requestId/timestamp
* @param _queryId being requested
* @param _timestamp to retrieve data/value from
* @return bytes value for requestId/timestamp submitted
*/
function retrieveData(bytes32 _queryId, uint256 _timestamp) public view returns (bytes memory);
/**
* @dev Counts the number of values that have been submitted for the request
* @param _queryId the requestId to look up
* @return uint256 count of the number of values received for the requestId
*/
function getNewValueCountbyQueryId(bytes32 _queryId) public view returns(uint256);
/**
* @dev Gets the timestamp for the value based on their index
* @param _queryId is the requestId to look up
* @param _index is the value index to look up
* @return uint256 timestamp
*/
function getTimestampbyRequestIDandIndex(uint256 _queryId, uint256 index) public view returns(uint256);
/**
* @dev Adds a tip to a given query ID.
* @param _queryId is the queryId to look up
* @param _amount is the amount of tips
* @param _queryData is the extra bytes data needed to fulfill the request
*/
function tipQuery(bytes32 _queryId, uint256 _amount, bytes memory _queryData) external;
Rinkeby: 0x20374E579832859f180536A69093A126Db1c8aE9
Kovan: 0x20374E579832859f180536A69093A126Db1c8aE9
Ropsten: 0x20374E579832859f180536A69093A126Db1c8aE9
Goerli: 0x20374E579832859f180536A69093A126Db1c8aE9
BSC Testnet: 0xbc2f9E092ac5CED686440E5062D11D6543202B24
Polygon Mumbai Testnet: 0xbc2f9E092ac5CED686440E5062D11D6543202B24
Arbitrum Testnet: 0xbc2f9E092ac5CED686440E5062D11D6543202B24
npm hardhat test
Just run hardhat run with desired Network
npx hardhat run --network <your-network> scripts/deploy.js
Miner Documentation
General Tellor Developer's Documentation
Metamask - www.metamask.io
Hardhat - https://hardhat.org/
Waffle - https://getwaffle.io/
Check out our issues log here on Github or contribute to our future plans to build a better miner and more examples of data secured by Tellor.
This repository is maintained by the Tellor team - www.tellor.io
Tellor Inc. 2021