Using DIAOracleV2 Interface

The following is an example of how to retrieve price value from a standard DIA oracle. For the purpose of this example, we will be using the following demo oracle on Ethereum: 0xCD5F…f3cB.

1

Access any DIA oracle smart contract.

2

Call getValue(pair_name) with pair_name being the full pair name such as BTC/USD. You can use the “Read” section on Etherscan to execute this call.

3

The response of the call contains four values:

  1. The current asset’s price in USD with a fix-comma notation of 8 decimals.

  2. The UNIX timestamp of the last oracle update.

pragma solidity ^0.8.13;

interface IDIAOracleV2 {
    function getValue(string memory) external view returns (uint128,
             uint128);
}

contract DIAOracleSample {

    address diaOracle;

    constructor(address _oracle) {
        diaOracle = _oracle;
    }

    function getPrice(string memory key)
    external
    view
    returns (
        uint128 latestPrice,
        uint128 timestampOflatestPrice
    ) {
        (latestPrice, timestampOflatestPrice) =
                 IDIAOracleV2(diaOracle).getValue(key);
    }
}

Find the detailed description of the functions and how to run tests on our GitHub page:

Using Solidity Library

DIA has a dedicated Solidity library to facilitate the integration of DIA oracles in your own contracts. The library consists of two functions, getPrice and getPriceIfNotOlderThan.

Access the library

import { DIAOracleLib } from "./libraries/DIAOracleLib.sol";

Methods

getPrice

function getPrice(
        address oracle,
        string memory key
        )
        public
        view
        returns (uint128 latestPrice, uint128 timestampOflatestPrice);

Returns the price of a specified asset along with the update timestamp.

Parameters:

NameTypeDescription
oracleaddressAddress of the oracle that we want to use
keystringThe asset that we want to use e.g. “ETH/USD”

Return Values:

NameTypeDescription
latestPriceuint128Price of the specified asset returned by the DIAOracle
timestampOflatestPriceuint128The update timestamp of the latest price

getPriceIfNotOlderThan

function getPriceIfNotOlderThan(
        address oracle,
        string memory key,
        uint128 maxTimePassed
        )
        public
        view
        returns (uint128 price, bool inTime)
    {

Checks if the oracle price is older than maxTimePassed

Parameters:

NameTypeDescription
oracleaddressAddress of the oracle that we want to use
keystringThe asset that we want to use e.g. “ETH/USD”
maxTimePasseduint128The maximum acceptable time passed in seconds since the the price was updated

Return Values:

NameTypeDescription
priceuint128Price of the specified asset returned by the DIAOracle
inTimeboolA boolian that is true if the price was updated at most maxTimePassed seconds ago, otherwise false

Find a detailed integration example and how to run a test on our GitHub page:

GitHub - diadata-org/DIAOracleLib

Sample contract

pragma solidity ^0.8.13;

import { DIAOracleLib } from "./libraries/DIAOracleLib.sol";

contract DIAOracleSample {
    error PriceTooOld();

    address diaOracle;

    constructor(address _oracle) {
        diaOracle = _oracle;
    }

    function getPrice(
        string memory key
    ) external view returns (uint128 latestPrice) {
        (latestPrice, ) = DIAOracleLib.getPrice(diaOracle, key);
    }

    function checkPriceAge(
        string memory key,
        uint128 maxTimePassed
    ) external view returns (uint128 price) {
        bool inTime;
        (price, inTime) = DIAOracleLib.getPriceIfNotOlderThan(
            diaOracle,
            key,
            maxTimePassed
        );

        if (!inTime) revert PriceTooOld();
    }
}