Skip to content

Introduction to DIA Oracles

Introduction

DIA offers customizable oracles that are tailored to each dApp’s needs. Each oracle can be customized in several ways, including data sources, data cleansing filters, pricing and computational methodologies, update mechanisms, and more. This ensures that the data and oracle remain robust and resilient to market conditions and provide a global market price as well as specific individual or cross-chain market prices.

By collecting billions of raw trades directly from over 90 sources, including CEXs, DEXs, and NFT marketplaces, DIA enables full transparency, customization, and control throughout the entire value stack. DIA's data and oracle suite comprise price feeds for 20,000+ assets, including cryptocurrencies, NFT collections, and liquid-staked tokens, as well as random number generation and other data feed types.

You can visit DIA's documentation to learn how to Request a Custom Oracle.

The information presented herein is for informational purposes only and has been provided by third parties. Moonbeam does not endorse any project listed and described on the Moonbeam docs website (https://docs.moonbeam.network/).

Token Price Feeds

DIA token price feeds provide smart contracts with real-time price information for 3,000+ cryptocurrencies, sourced transparently from 90+ trusted, high-volume DEXs and CEXs.

Moonbeam Demo Price Oracles

DIA has deployed the following demo oracles for the Moonbeam community, which provide a limited selection of cryptocurrency price feeds with predefined configuration settings:

Network Contract Address
Moonbeam 0x1f1BAe8D7a2957CeF5ffA0d957cfEDd6828D728f
Moonriver 0x11f74b94afb5968119c98ea277a2b73208bb39ab
Moonbase Alpha 0xe23d8713aa3a0a2c102af772d2467064821b8d46

The demo oracle contracts deployed to Moonbeam are the DIA Key-Value Oracle Contract V2. The contract is structured as follows:

pragma solidity 0.7.4;

contract DIAOracleV2 {
    mapping (string => uint256) public values;
    address oracleUpdater;

    event OracleUpdate(string key, uint128 value, uint128 timestamp);
    event UpdaterAddressChange(address newUpdater);

    constructor() {
        oracleUpdater = msg.sender;
    }

    function setValue(string memory key, uint128 value, uint128 timestamp) public {
        require(msg.sender == oracleUpdater);
        uint256 cValue = (((uint256)(value)) << 128) + timestamp;
        values[key] = cValue;
        emit OracleUpdate(key, value, timestamp);
    }

    function getValue(string memory key) external view returns (uint128, uint128) {
        uint256 cValue = values[key];
        uint128 timestamp = (uint128)(cValue % 2**128);
        uint128 value = (uint128)(cValue >> 128);
        return (value, timestamp);
    }

    function updateOracleUpdaterAddress(address newOracleUpdaterAddress) public {
      require(msg.sender == oracleUpdater);
        oracleUpdater = newOracleUpdaterAddress;
        emit UpdaterAddressChange(newOracleUpdaterAddress);
    }
}

Note

DIA demo oracles are not intended for use in production environments. Developers can request a dedicated, production-ready oracle with custom price feeds and configuration settings. To start the request process, you can check out the Request a Custom Oracle documentation.

Included Price Feeds

The price feeds included with the demo oracles are:

How to Access DIA Oracles

The steps for accessing a price value on DIA oracles are as follows:

  1. Access your oracle smart contract on Moonbeam
  2. Call getValue(pair_name) with pair_name being the full pair name, such as BTC/USD. You can use the Read Contract functionality under the Contract tab of the contract on Moonscan to execute this call

The response contains two values:

  • The current asset price in USD with a fix-comma notation of 8 decimals
  • The UNIX timestamp of the last oracle update

You can find DIA's oracle integration samples in Solidity and Vyper languages by visiting the Access the Oracle guide on DIA's documentation site.

Supported Token API Endpoints

DIA also supports Rest and GraphQL endpoints to return cryptocurrency price data. You can visit the DIA documentation to see all API endpoints.

For example, you can use the following JavaScript scripts to access the BTC/USD price feed:

const axios = require('axios');

const options = {
  method: 'GET',
  url: 'https://api.diadata.org/v1/assetQuotation/Bitcoin/0x0000000000000000000000000000000000000000',
  headers: { 'Content-Type': 'application/json' },
};

axios
  .request(options)
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.error(error);
  });
const axios = require('axios');

const url = 'https://api.diadata.org/graphql/query';

const query = `
  {
    GetFeed(
      Filter: "mair",
      BlockSizeSeconds: 480,
      BlockShiftSeconds: 480,
      StartTime: 1690449575,
      EndTime: 1690535975,
      FeedSelection: [
        {
          Address: "0x0000000000000000000000000000000000000000",
          Blockchain:"Bitcoin",
          Exchangepairs:[],
        },
      ],
    )
    {
      Name
      Time
      Value
      Pools
      Pairs
    }
  }`;

const data = {
  query: query,
};

axios
  .post(url, data)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error('Request failed:', error.message);
  });

You can refer to DIA's documentation on Rest API Endpoints and the GraphQL Endpoint for information on the parameters and return data.

NFT Floor Price Feeds

DIA NFT floor price feeds provide smart contracts with real-time price information for 18,000+ NFT collections, sourced on-chain with 100% transparency from multiple cross-chain NFT marketplaces.

Please refer to DIA's documentation to find out how you can request a custom NFT oracle for NFTs on Moonbeam.

Supported NFT API Endpoints

DIA also supports API endpoints to return cryptocurrency price data. Developers can directly access the example endpoints listed in DIA's documentation.

Random Number Generation

DIA xRandom provides smart contracts with unpredictable and unbiased random numbers, facilitating the development of on-chain use cases such as lotteries, prediction markets, NFT launches, and more.

DIA leverages the Drand public randomness beacon, and updates its oracle with round numbers, randomness and a signature. Drand runs distributed nodes to produce their randomness beacon. Drand uses Pedersen's DKG (Distributed Key Generation) protocol to create collective private and public keys. Participants in their League of Entropy then generate randomness in rounds and broadcast it together with its signature.

To learn more about Drand’s randomness beacon, watch the On-Chain Randomness Oracle | DIA Developer Tutorial and read Drand’s documentation.

Moonbeam Demo Randomness Oracle

DIA has deployed a demo oracle on Moonbase Alpha, which can be accessed at the following address:

0x48d351ab7f8646239bbade95c3cc6de3ef4a6cec

The DIA randomness smart contract is structured as follows:

pragma solidity ^0.8.0;

contract DIARandomOracle {
  struct Random {
    string randomness;
    string signature;
    string previousSignature;
  }

  mapping(uint256 => Random) public values;
  uint256 public lastRound = 0;
  address public oracleUpdater;
  event OracleUpdate(string key, uint128 value, uint128 timestamp);
  event UpdaterAddressChange(address newUpdater);

  constructor() {
      oracleUpdater = msg.sender;
  }

  function setRandomValue(
    uint256 _round,
    string memory _randomness,
    string memory _signature,
    string memory _previousSignature
  ) public {
    require(msg.sender == oracleUpdater, "not a updater");
    require(lastRound < _round, "old round");
    lastRound = _round;
    values[_round] = Random(_randomness, _signature, _previousSignature);
  }

  function getValue(uint256 _round) external view returns (Random memory) {
    return values[_round];
  }

  function updateOracleUpdaterAddress(address newOracleUpdaterAddress)
    public
  {
    require(msg.sender == oracleUpdater, "not a updater");
    oracleUpdater = newOracleUpdaterAddress;
    emit UpdaterAddressChange(newOracleUpdaterAddress);
  }

  function getRandomValueFromRound(uint256 _round)
    external
    view
    returns (string memory)
  {
    return values[_round].randomness;
  }

  function getRandomValueFromRoundWithSignature(uint256 _round)
    external
    view
    returns (Random memory)
  {
    return values[_round];
  }

    function getLastRound() public view returns (uint256) {
    return lastRound;
  }
}

Note

DIA demo oracles are not intended for use in production environments. Developers can request a dedicated, production-ready randomness oracle. To start the request process, you can check out the Request a Random Oracle documentation.

How to Use the DIA Randomness Oracle

The steps for accessing a published random value are as follows:

  1. Access your randomness oracle smart contract on Moonbeam
  2. Call getLastRound()to obtain the ID of the latest published round. You can use the Read Contract functionality under the Contract tab of the contract on Moonscan to execute this call
  3. Call getRandomValueFromRound(uint256 _round) using the obtained round ID. Again, you can use Moonscan to quickly execute this call

The response contains the randomness value.

The signature can also be requested by calling getRandomValueFromRoundWithSignature(uint256 _round), which returns a tuple containing the randomness value, the signature, and the previous signature.

To learn how to deploy a randomness-consuming contract on Moonbeam, please refer to the Deploying a Randomness Consuming Smart Contract on EVM chains with DIA xRandom Oracle video tutorial.

Resources

The information presented herein has been provided by third parties and is made available solely for general information purposes. Moonbeam does not endorse any project listed and described on the Moonbeam Doc Website (https://docs.moonbeam.network/). Moonbeam Foundation does not warrant the accuracy, completeness or usefulness of this information. Any reliance you place on such information is strictly at your own risk. Moonbeam Foundation disclaims all liability and responsibility arising from any reliance placed on this information by you or by anyone who may be informed of any of its contents. All statements and/or opinions expressed in these materials are solely the responsibility of the person or entity providing those materials and do not necessarily represent the opinion of Moonbeam Foundation. The information should not be construed as professional or financial advice of any kind. Advice from a suitably qualified professional should always be sought in relation to any particular matter or circumstance. The information herein may link to or integrate with other websites operated or content provided by third parties, and such other websites may link to this website. Moonbeam Foundation has no control over any such other websites or their content and will have no liability arising out of or related to such websites or their content. The existence of any such link does not constitute an endorsement of such websites, the content of the websites, or the operators of the websites. These links are being provided to you only as a convenience and you release and hold Moonbeam Foundation harmless from any and all liability arising from your use of this information or the information provided by any third-party website or service.
Last update: May 28, 2024
| Created: October 3, 2023