DIA - Example
This section covers what the DIA is about, oracles on DIA and how to interact with published data. It also gives a short tutorial about building a sample price feed.

What is DIA?

DIA is an ecosystem for open financial data in a financial smart contract ecosystem. The target of DIA is to bring together data analysts, data providers and data users. In general, DIA provides a reliable and verifiable bridge between off-chain data from various sources and on-chain smart contracts that can be used to build a variety of financial dApps. There are quite a number of things you can do with DIA. You can develop applications and sources on top of DIA.

How to use DIA

We would specifically be building the one for the pair ETH/USD but DIA allows you do several other pairs such as BTC, DIA, USDC, FTM, SDN, KSM, MOVR which are all USD based currently.
Writing your smart contracts On Metis Andromeda, you can initialize the oracle address in your wrapper contract by calling setOracleAddress() with the address of our deployed demo contract 0x6E6E633320Ca9f2c8a8722c5f4a993D9a093462E
ChainName
Oracle Contract Address
Published Assets
Metis Andromeda Mainnet
DIA Asset Prices
You can also use the explorer to query assets. Just go to the Read Contract tab and get or set values. You would also need to connect your wallet. Here is the contract code that is deployed. You can use it to perform functions like getting price feed value from a pair. We would be interacting with this soon to build a simple price feed dApp in a few.
To access the key-value oracle, you first need to know the key of the data you are interested in. On querying getValue(key), the oracle returns the latest value for that key and the timestamp of its last update. This key is a string and each update emits an event containing the key of the updated value. The response value is an integer in a fix comma format and the timestamp associated with each value is a Unix timestamp for the UTC timezone.
Building a sample Price feed DApp using the oracle You can interact with the oracle deployed contract on the explorer and build a DApp of your choice. We would be building a simple price feed DApp that will display the ETH to USD price in real time. Create/Navigate to your project folder:
1
mkdir pricefeed
2
cd pricefeed
Copied!
Create index.html and index.js files:
1
touch index.html
2
touch index.js
Copied!
In your index.js, you can start by declaring the constant, abi. The abi can be found on the explorer under the code tab. Copy the contract abi as shown in the image below:
Recall that there is a getValue method in the contract, so in order to get the response from the method, make a call to the method as shown below. We are also using the key/value pair ‘ETH/USD’:
1
const currentPriceResponse = await contract.methods.getValue('ETH/USD').call()
Copied!
In the end, your index.js should almost look like this:
index.js
1
async function connect() {
2
const priceElem = document.querySelector('#price')
3
4
try {
5
priceElem.textContent = 'Updating price...';
6
const abi = [abi] // the contract’s ABI
7
const web3 = new Web3(new Web3.providers.HttpProvider("https://andromeda.metis.io/?owner=1088"))
8
const contract = new web3.eth.Contract(ABI, "0x6E6E633320Ca9f2c8a8722c5f4a993D9a093462E");
9
const currentPriceResponse = await contract.methods.getValue('ETH/USD').call()
10
11
if (currentPriceResponse[0]) {
12
const formatter = new Intl.NumberFormat('en-US', {
13
style: 'currency',
14
currency: 'USD',
15
minimumFractionDigits: 2,
16
});
17
priceElem.textContent = formatter.format(currentPriceResponse[0] / 100000000); // the output is in gwei so you would have to format to ether
18
}
19
} catch (e) {
20
priceElem.textContent = 'An error occurred while updating the price, please refresh your page.'; }
21
}
22
23
window.onload = function() {
24
connect();
25
}
Copied!
Now head back to the index.html and style your page to your preference. You can have a simple div or a header that will show the price like below:
index.html
1
​​ <h5>
2
<span>Current Value: <i id="price"></i></span>
3
</h5>
Copied!
Running your index.html on the browser and you would see that you are getting ETH/USD prices in real time. That’s pretty much it! A sample is deployed at price feed sample app.