(Simple) Integrating Existing Feeds
A simple read of existing Tellor feeds.
We will install the usingtellorlayer npm package to gain access to the TellorDataBank interface:
npm i usingtellorlayerThe code block below shows how you can connect your contract to TellorDataBank and read the data:
pragma solidity 0.8.19;
import "usingtellorlayer/contracts/interfaces/ITellorDataBank.sol";
contract SampleLendingPoolUser {
ITellorDataBank public dataBank;
bytes32 public ethUsdQueryId;
uint256 public constant MAX_PRICE_AGE = 4 hours;
uint256 public constant LTV_BPS = 8000;
mapping(address => uint256) public collateral;
mapping(address => uint256) public debt;
constructor(address _dataBank) {
dataBank = ITellorDataBank(_dataBank);
ethUsdQueryId = keccak256(abi.encode("SpotPrice", abi.encode("eth", "usd")));
}
function deposit() external payable {
collateral[msg.sender] += msg.value;
}
function borrow(uint256 _stableAmount) external {
ITellorDataBank.AggregateData memory _report = dataBank.getCurrentAggregateData(ethUsdQueryId);
require(_report.value.length >= 32, "no report");
require(block.timestamp - (_report.aggregateTimestamp / 1000) < MAX_PRICE_AGE, "stale price");
uint256 _price = abi.decode(_report.value, (uint256));
uint256 _collateralValue = collateral[msg.sender] * _price / 1e18;
debt[msg.sender] += _stableAmount;
require(debt[msg.sender] * 10000 <= _collateralValue * LTV_BPS, "ltv");
}
}Understanding Checks on a DataBank read (above)
We need to make sure our data is not too old and that it can be correctly decoded:
The query id is the argument you pass to
getCurrentAggregateData.aggregateTimestampis milliseconds.Require the aggregate timestamp to be inside the age your action can tolerate.
MAX_PRICE_AGEwider than the feed's heartbeat on feeds.tellor.io (Typically 4hrs by default)Spot prices are a
uint256with 18 decimals.
Last updated
Was this helpful?

