Access the Oracle

This page contains an overview over how to access crypto asset information using the DIA oracle system

How to access DIA's oracle?

Here 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: 0xa935...5856.

  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 price in USD with a fix-comma notation of 8 decimals.

    2. The UNIX timestamp of the last oracle update.

The oracle exists on various blockchains and contains information about crypto assets. You can access a price quotation (see sources and methodology) and the current circulating supply as well as the timestamp of the last update.

See DIA's oracle structure for more information:

Oracle structures

Oracle integration example

You can find DIA's oracle integration samples in Solidity and Vyper languages for the EVM based chains.

Solidity

Here is an example on how you can integrate DIA's oracle into your smart contract with Solidity:

pragma solidity ^0.8.13;

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

contract IntegrationSample{

    address immutable ORACLE = 0xa93546947f3015c986695750b8bbEa8e26D65856;
    uint128 public latestPrice; 
    uint128 public timestampOflatestPrice; 
   
    function getPriceInfo(string memory key) external {
        (latestPrice, timestampOflatestPrice) = IDIAOracleV2(ORACLE).getValue(key); 
    }
   
    function checkPriceAge(uint128 maxTimePassed) external view returns (bool inTime){
         if((block.timestamp - timestampOflatestPrice) < maxTimePassed){
             inTime = true;
         } else {
             inTime = false;
         }
    }
}

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

Vyper

Here is an example on how you can integrate DIA's oracle into your smart contract with Vyper:

# @version ^0.3.6

ORACLE: constant(address) = 0xa93546947f3015c986695750b8bbEa8e26D65856
latestPrice: public(uint128)
timestampOfLatestPrice: public(uint128)

interface IDIAOracleV2:
    def getValue(key: String[7]) -> (uint128, uint128): nonpayable

@external
def getPriceInfo(key: String[7]):
    self.latestPrice, self.timestampOfLatestPrice = IDIAOracleV2(ORACLE).getValue(key)

@view
@external
def checkPriceAge(maxTimePassed: uint128) -> bool:
    inTime: bool = False
    if (convert(block.timestamp, uint128) - self.timestampOfLatestPrice) < maxTimePassed:
        inTime = True
    else:
        inTime = False 

    return inTime

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

Accessing DIA's oracles with a dedicated Solidity library

DIA has a dedicated Solidity library to facilitate 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";

getPrice

function getPrice(address oracle, string memory key) internal returns (uint128 price)

Returns the price of a specified asset.

Parameters:

NameTypeDescription

oracle

address

Address of the oracle that we want to use

key

string

The asset that we want to use e.g. "ETH/USD"

Return Values:

NameTypeDescription

Price

uint128

Price of the specified asset returned by the DIAOracle

getPriceIfNotOlderThan

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

Checks if the oracle price is older than maxTimePassed

Parameters:

NameTypeDescription

oracle

address

Address of the oracle that we want to use

key

string

The asset that we want to use e.g. "ETH/USD"

maxTimePassed

uint128

The maximum acceptable time passed in seconds since the the price was updated

Return Values:

NameTypeDescription

Price

uint128

Price of the specified asset returned by the DIAOracle

inTime

uint128

A 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 test on our GitHub page:

Accessing 3rd party data oracles

The oracle contains information about price quotations of crypto assets from CoinMarketCap and Coingecko. You can access quotations for the top 50 assets ranked by market capitalization. You can execute an oracle call as follows:

  1. Access DIA's guest quotation oracles (find them here).

  2. Call getValue(key) where key is the symbol for the asset in capital letters, for instance BTC for Bitcoin. You can use the "Read" section on Etherscan to execute this call.

  3. The response of the call contains two values:

    1. The current asset price in USD.

    2. The UNIX timestamp of the last oracle update.

You can find out more about 3rd party data here:

3rd Party Data Aggregation

Solidity DIA Oracles Integration Library

DIA has a dedicated library for Solidity developers to facilitate your integration journey.

Below is a sample function that allows to quote Asset Price from any of the DIA's oracles

import {DIAOracleLib} from "./libraries/DIAOracleLib.sol";
function exampleGetPrice(string memory key) external returns (uint128){
        return DIAOracleLib.getPrice(ORACLE, key);
    }

Last updated