DIA Nexus Documentation
  • Nexus Overview
  • Intro to Nexus
    • How it works
    • Nexus vs. Lumina
    • Integrated Chains
  • Data products
    • Token Price Feeds
    • RWA Price Feeds
    • Randomness
    • Fair-value Feeds
  • How-to Guides
    • Fetch Price Data
      • Solidity
      • Vyper
      • Demo Oracles
      • Chain-specific Guide
        • Aleph Zero
        • Alephium
        • Edu Chain
        • Hydration
        • Kadena
        • LUKSO
        • Somnia
        • Stacks
        • Superseed
        • XRP Ledger (XRPL)
    • Generate Randomness
      • Solidity
      • WASM
      • Demo Oracles
      • Chain-specific Guide
        • Alephium
    • Migrate to DIA
    • Fund the Oracle
    • Build a Scraper
      • Development Cluster Stack
      • DIA Test‐Space with Docker Compose
      • DIA Test‐Space with Minikube
      • Add a new exchange scraper
      • Add a new foreign scraper
      • Add a new liquidity scraper
      • Additional notes
  • Request a Custom Oracle
  • Reference
    • Architecture
      • Data sourcing
      • Data computation
      • Data delivery
    • APIs
      • Token prices
        • RestAPI
          • Request Samples
        • GraphQL
          • Request Samples
      • RWA prices
    • Pricing Methodologies
      • IR: Interquartile Range Filter
      • MAIR: Moving Average with Interquartile Range Filter
      • VWAP: Volume Weighted Average Price
      • VWAPIR: Volume Weighted Average Price with Interquartile Range Filter
      • LST fair price
    • Data Sources
      • CEXes Data
      • DEXes Data
    • Smart Contracts
      • DIAOracleV2.sol
      • DIARandomOracle.sol
    • Randomness Protocol
  • Resources
    • Audits
    • Community & Support
    • Security Bounty Program
    • Research
      • Return Rates in Crypto Farming
      • Crypto Volatility Index
      • Compounded Rates
      • Polkadot Medianizer
    • T&C
      • Licence Agreement
      • Contributor Covenant Code of Conduct
      • Disclaimer
Powered by GitBook
On this page
  • Using DIAOracleV2 Interface
  • Using Solidity Library
  • Access the library
  • Methods
  • Sample contract
Export as PDF
  1. How-to Guides
  2. Fetch Price Data

Solidity

PreviousFetch Price DataNextVyper

Last updated 1 month ago

There are two ways that you can access the oracle in your dApp. You can either declare an IDIAOracleV2 interface or import the solidity library.

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

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

Name
Type
Description

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:

Name
Type
Description

latestPrice

uint128

Price of the specified asset returned by the DIAOracle

timestampOflatestPrice

uint128

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

Name
Type
Description

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:

Name
Type
Description

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

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();
    }
}
0xCD5F...f3cB
UNIX timestamp
GitHub - diadata-org/DIA-integration-sampleGitHub
Solidity integration example
GitHub - diadata-org/DIAOracleLibGitHub
Logo
Logo