Skip to content

diadata-org/dia-oracle-anchor

Repository files navigation

DIA Oracles on Aleph Zero

This repository contains the source code for DIA oracles on Aleph Zero.

Oracle Types

DIA offers two types of oracles on Aleph Zero.

Asset Price Oracle

DIA token price feeds provide smart contract real-time price information cryptocurrency assets, transparently sourced from 80+ trusted, high-volume DEXs and CEXs. The feeds facilitate the development of DeFi use cases such as money markets, lending/borrowing, synthetic asset issuance, options, derivatives and futures markets, and many more. New assets can also be supported on demand.

Supported assets

The deployed DIA oracles support the following assets

Asset Query String Methodology Update Threshold
Bitcoin BTC/USD MAIR120 0.1%
Ether ETH/USD MAIR120 0.1%
USDC USDC/USD MAIR120 0.1%
USDT USDT/USD MAIR120 0.1%
DOT DOT/USD MAIR120 0.1%
SOL SOL/USD MAIR120 0.1%
AVAX AVAX/USD MAIR120 0.1%
EUR EUR/USD MAIR120 0.1%
BNB BNB/USD MAIR120 0.1%
DOGE DOGE/USD MAIR120 0.1%
MATIC MATIC/USD MAIR120 0.1%
DAI DAI/USD MAIR120 0.1%
AZERO AZERO/USD MAIR120 0.1%

The MAIR120 methodology is defined in detail in the DIA documentation.

Randomness Oracle

DIA xRandom provides smart contracts with unpredictable random numbers. DIA leverages drand’s distributed randomness beacon, enabling verifiable, unpredictable and unbiased random numbers. The randomness oracle enables the creation of on-chain applications including but not limited to on-chain gaming, lotteries, prediction markets, and NFT launches.

The randomness oracle is defined in detail in the DIA documentation.

Deployed Oracles

Deployed oracles can be accessed at these addresses:

Network Oracle Name Address Metadata JSON
Testnet Asset Price Oracle 5F5z8pZoLgkGapEksFWc2h7ZxH2vdh1A9agnhXvfdCeAfS9b Price Oracle Metadata
Testnet Randomness Oracle 5CSQdMyKCxtoeVsBC8xbufeapux3YDV74eYXcHV4UKUu1NeF Randomness Oracle Metadata
Mainnet Asset Price Oracle 5F7wPCMXX65RmL8oiuAFNKu2ydhvgcissDZ3NWZ5X85n2WPG Price Oracle Metadata
Mainnet Randomness Oracle 5HSkrQwqJtmdWL99ocRf1nMwJF9k54uHcQCU5CCctA3zf3q7 Randomness Oracle Metadata

Asset price oracle

To facilitate development, the DIA oracles are deployed on Aleph Zero mainnet and testnet. Any developer can interact with these oracles without any authentication.

Oracle contract

The smart contract is a key/value store and contains two values per asset, the timestamp of the last update and the value of the asset price. The asset price is stored with 18 decimals by default.

To interact with this contract via the aleph zero UI, you can import the metadata of the deployed contract in the Contracts UI by subscan:

  • Go to https://contracts-ui.substrate.io/contract
  • Choose aleph zero or aleph zero testnet on top left
  • Choose Add new contract
  • Choose Use Onchain Contract
  • Enter the contract address
  • Import the metadata json file

Compiling the smart contract

Alternatively to using the provided metadata json, you can generate it yourself. Generate the smart contract files for interacting with the contact and import the resulting file into the Aleph Zero UI.

Interacting with the oracle in your dApp

The example directory contains an example for how the oracle can be called by a dApp. This piece of code shows how an asset can be retrieved using the getLatestPrice() function.

        #[ink(message)]
        pub fn get(&self, key: String) -> Option<(u64, u128)> {
            self.oracle.get_latest_price(key)
        }

The key usually is the string symbol of an asset pair, for example, "BTC/USD" for the price of Bitcoin. The return value contains two values per asset, the timestamp of the last update and the value of the asset price. The asset price is stored with 18 decimals by default.

Randomness oracle

The randomness oracle is available on Aleph Zero mainnet and testnet and can be used to retrieve randomness from drand.love.

Oracle contract

Randomness is produced in numbered rounds. Each round's randomness can be retirved individually and the latest round can be queried from the contract directly. To interact with this contract via the aleph zero UI, you can import the deployed contract above.

Interacting with the oracle

The example-randomness-oracle directory contains an example for how the radnomness oracle can be called by a dApp. This piece of code shows how an asset can be retrieved using the getRandomValueForRound() function. The latest round (the required input parameter for this call) can be retrieved using the getLatestRound() function.

        #[ink(message)]
        pub fn get(&self, key: u64) -> Option<Vec<u8>> {
            self.oracle.get_random_value_for_round(key)
        }

The key is the integer round number, for example 3353171. The return value contains three values per round, the radnomness value, its signature, and the previous round's signature.

Other functions include the retrieval of the latest round ID and the historical round randomnesses.

Behind the scenes: Feeder setup

The smart contract is fed by a piece of software called the Feeder which is available as a docker image. Unless you want to re-create the entire setup and operate your own oracle, the following section is not relevant.

Environment for testing the feeder

These variables will be read from helm configs as environment variables

Name Default Value Description
DATABASE_URL postgresql://user:password@host:port/dbname Postgres database URL (optional, for recording transaction IDs)
BLOCKCHAIN_NODE https://rpc.test.azero.dev Blockchain RPC
PRIVATE_KEY *insert your private updater key here* PK of price updater
DEPLOYED_CONTRACT 5FmmcSEPiT4sZniwBMs6G89128GTTfPtaWK3jEJPJ9Z77v7U Price anchor contract deployed
ASSETS " Bitcoin-0x0000000000000000000000000000000000000000, Ethereum-0x0000000000000000000000000000000000000000, Ethereum-0x84cA8bc7997272c7CfB4D0Cd3D55cd942B3c9419, Ethereum-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48, Ethereum-0xdAC17F958D2ee523a2206206994597C13D831ec7, Ethereum-0x6B175474E89094C44Da98b954EedeAC495271d0F, Ethereum-0x853d955aCEf822Db058eb8505911ED77F175b99e, BinanceSmartChain-0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56, Solana-0x0000000000000000000000000000000000000000, Polkadot-0x0000000000000000000000000000000000000000 " Assets, whose price is to be stored in the oracle
FREQUENCY_SECONDS 86400 How often to feed price onchain
SLEEP_SECONDS 120 How often to check DEVIATION_PERMILLE, store new price if need
ORACLE_TYPE 0 Type of oracle for pricing oracle 0, and for randomness oracle 1

Note: these values are just used for testnet. Never share your production private key with anyone.

How to run to the test with your own feeder

  • Fill your env vars
  • Install docker
  • Run:
    • docker build --tag dia-oracle:latest .
    • docker run --rm -p 3000:3000 -d dia-oracle:latest

Output

The feeder will auto feed price onchain with 18 decimals. If you want to track transaction logs, youcan access them at http://${host}/api/v1/oracle?page_size=${page_size}&page_index=${page_index}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages