For the complete documentation index, see llms.txt. This page is also available as Markdown.

(Simple) Integrating Existing Feeds

A simple read of existing Tellor feeds.

See the Contracts Reference page for deployed contract addresses. You are also welcome to deploy your own.

We will install the usingtellorlayer npm package to gain access to the TellorDataBank interface:

npm i usingtellorlayer

The 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:

  1. The query id is the argument you pass to getCurrentAggregateData.

  2. aggregateTimestamp is milliseconds.

  3. Require the aggregate timestamp to be inside the age your action can tolerate. MAX_PRICE_AGE wider than the feed's heartbeat on feeds.tellor.io (Typically 4hrs by default)

  4. Spot prices are a uint256 with 18 decimals.

See the SampleLayerUser repo for a more example Tellor use case implementations.

Last updated

Was this helpful?