WASM Oracle

DIA has a dedicated WASM-based oracle. It can be universally deployed on any chain that supports substrate WASM environment.

WASM Oracle Functions

get : Gets the latest value of the asset symbol with timestamp. Can be called by anyone.

set : Sets latest value of asset, requires price and timestamp. Can be called only by the owner of contract.

Using the Oracle

To access values from DIA wasm oracles you need to copy the diadata directory to your contract so that you can access DIA structs, that contain the oracle data.

Contract integration

Create storage with DiaDataref, this is used to access values from oracle

    #[ink(storage)]
    pub struct OracleExample {
        diadata: DiadataRef,
        ....
        ....
    }

This struct can be used to access data from pub functions from the oracle contract.

To instantiate a contract's access to the oracle you need to pass the DIA oracle address, either using the constructor or by creating a separate write function to update with the value of oracle at a later stage.

Example using constructor:

    #[ink(constructor)]
    pub fn new(
        oracle_address: AccountId, 
    ) -> Self {
        let diadata: DiadataRef = ink_env::call::FromAccountId::from_account_id(oracle_address);  
        Self {
            diadata
        }
    }

Here, oracle_address refers to the DIA oracle address of a deployed oracle contract.

Access the value

Next, to access an oracle value you can simple call the get() function

 pub fn get(&self ) -> diadata::ValueTime {
            return self.diadata.get(String::from("ETH"));
        }

This gives the ETH price value time given by the oracle.

Config changes

Make sure you add diadata/std in you config

std = [
    "ink_metadata/std",
    "ink_env/std",
    "ink_storage/std",
    "ink_primitives/std",
    "scale/std",
    "scale-info/std",
    "diadata/std",
]

Make sure the version of ink you are on is v3.0.1

See the entire oracle code and instructions on how to run and oracle service by yourself in our github repo:

Demo oracles

See deployed WASM demo oracles in action:

If you want a custom WASM oracle for your dapp, request a custom oracle:

📃pageRequest custom data delivery via Forum

Last updated