Access the Oracle
This page contains an overview over the different smart contract interfaces deployed by DIA.
DIA uses two different smart contracts for its oracles, depending on the use cases for each deployment. Its definitions are listed here. To learn how to access a foreign smart contract (such as our oracles) from your own smart contract, have a look at this tutorial.

DIA Key-Value Oracle Contract

1
pragma solidity 0.7.4;
2
3
contract DIAOracle {
4
mapping (string => uint256) public values;
5
address oracleUpdater;
6
7
event OracleUpdate(string key, uint128 value, uint128 timestamp);
8
event UpdaterAddressChange(address newUpdater);
9
10
constructor() {
11
oracleUpdater = msg.sender;
12
}
13
14
function setValue(string memory key, uint128 value, uint128 timestamp) public {
15
require(msg.sender == oracleUpdater);
16
uint256 cValue = (((uint256)(value)) << 128) + timestamp;
17
values[key] = cValue;
18
emit OracleUpdate(key, value, timestamp);
19
}
20
21
function getValue(string memory key) public view returns (uint128, uint128) {
22
uint256 cValue = values[key];
23
uint128 timestamp = (uint128)(cValue % 2**128);
24
uint128 value = (uint128)(cValue >> 128);
25
return (value, timestamp);
26
}
27
28
function updateOracleUpdaterAddress(address newOracleUpdaterAddress) public {
29
require(msg.sender == oracleUpdater);
30
oracleUpdater = newOracleUpdaterAddress;
31
emit UpdaterAddressChange(newOracleUpdaterAddress);
32
}
33
}
Copied!
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. Typical keys are listed in the details section of each deployed contract.
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.

DIA CoinInfo Oracle Contract

1
pragma solidity ^0.4.21;
2
3
contract DiaOracle {
4
address owner;
5
6
struct CoinInfo {
7
uint256 price;
8
uint256 supply;
9
uint256 lastUpdateTimestamp;
10
string symbol;
11
}
12
13
mapping(string => CoinInfo) diaOracles;
14
15
event newCoinInfo(
16
string name,
17
string symbol,
18
uint256 price,
19
uint256 supply,
20
uint256 lastUpdateTimestamp
21
);
22
23
constructor() public {
24
owner = msg.sender;
25
}
26
27
function changeOwner(address newOwner) public {
28
require(msg.sender == owner);
29
owner = newOwner;
30
}
31
32
function updateCoinInfo(string name, string symbol, uint256 newPrice, uint256 newSupply, uint256 newTimestamp) public {
33
require(msg.sender == owner);
34
diaOracles[name] = (CoinInfo(newPrice, newSupply, newTimestamp, symbol));
35
emit newCoinInfo(name, symbol, newPrice, newSupply, newTimestamp);
36
}
37
38
function getCoinInfo(string name) public view returns (uint256, uint256, uint256, string) {
39
return (
40
diaOracles[name].price,
41
diaOracles[name].supply,
42
diaOracles[name].lastUpdateTimestamp,
43
diaOracles[name].symbol
44
);
45
}
46
}
Copied!
The CoinInfo oracle contract provides several information about an asset. Apart from the asset price, a response also contains the symbol name, its full name and (if applicable) its circulating supply. A timestamp of the last update is returned as well.
A call of getCoinInfo(name) triggers the response and it can then be used in other smart contracts. The timestamp associated with each response is a Unix timestamp for the UTC timezone.
Last modified 1mo ago