github.com/diadata-org/diadata@v1.4.593/documentation/oracle-documentation/access-the-oracle.md (about) 1 --- 2 description: >- 3 This page contains an overview over the different smart contract interfaces 4 deployed by DIA. 5 --- 6 7 # Access the Oracle 8 9 DIA uses three 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](https://ethereum.org/en/developers/tutorials/interact-with-other-contracts-from-solidity/). 10 11 ### DIA Key-Value Oracle Contract V2 12 13 ``` 14 pragma solidity 0.7.4; 15 16 contract DIAOracleV2 { 17 mapping (string => uint256) public values; 18 address oracleUpdater; 19 20 event OracleUpdate(string key, uint128 value, uint128 timestamp); 21 event UpdaterAddressChange(address newUpdater); 22 23 constructor() { 24 oracleUpdater = msg.sender; 25 } 26 27 function setValue(string memory key, uint128 value, uint128 timestamp) public { 28 require(msg.sender == oracleUpdater); 29 uint256 cValue = (((uint256)(value)) << 128) + timestamp; 30 values[key] = cValue; 31 emit OracleUpdate(key, value, timestamp); 32 } 33 34 function getValue(string memory key) external view returns (uint128, uint128) { 35 uint256 cValue = values[key]; 36 uint128 timestamp = (uint128)(cValue % 2**128); 37 uint128 value = (uint128)(cValue >> 128); 38 return (value, timestamp); 39 } 40 41 function updateOracleUpdaterAddress(address newOracleUpdaterAddress) public { 42 require(msg.sender == oracleUpdater); 43 oracleUpdater = newOracleUpdaterAddress; 44 emit UpdaterAddressChange(newOracleUpdaterAddress); 45 } 46 } 47 48 ``` 49 50 Version 2 of the Key/Value oracle can be added as an interface in other contracts using all current Solidity versions with the visibility change of the `getValue()` function from `public` to `external`. 51 52 The smart contract is audited and the audit can be found here: 53 54 {% file src="../../.gitbook/assets/02_Smart Contract Audit_DIA_Oracle_v2.pdf" %} 55 56 ### DIA Key-Value Oracle Contract 57 58 ``` 59 pragma solidity 0.7.4; 60 61 contract DIAOracle { 62 mapping (string => uint256) public values; 63 address oracleUpdater; 64 65 event OracleUpdate(string key, uint128 value, uint128 timestamp); 66 event UpdaterAddressChange(address newUpdater); 67 68 constructor() { 69 oracleUpdater = msg.sender; 70 } 71 72 function setValue(string memory key, uint128 value, uint128 timestamp) public { 73 require(msg.sender == oracleUpdater); 74 uint256 cValue = (((uint256)(value)) << 128) + timestamp; 75 values[key] = cValue; 76 emit OracleUpdate(key, value, timestamp); 77 } 78 79 function getValue(string memory key) public view returns (uint128, uint128) { 80 uint256 cValue = values[key]; 81 uint128 timestamp = (uint128)(cValue % 2**128); 82 uint128 value = (uint128)(cValue >> 128); 83 return (value, timestamp); 84 } 85 86 function updateOracleUpdaterAddress(address newOracleUpdaterAddress) public { 87 require(msg.sender == oracleUpdater); 88 oracleUpdater = newOracleUpdaterAddress; 89 emit UpdaterAddressChange(newOracleUpdaterAddress); 90 } 91 } 92 ``` 93 94 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. 95 96 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. 97 98 ### DIA CoinInfo Oracle Contract 99 100 ``` 101 pragma solidity ^0.4.21; 102 103 contract DiaOracle { 104 address owner; 105 106 struct CoinInfo { 107 uint256 price; 108 uint256 supply; 109 uint256 lastUpdateTimestamp; 110 string symbol; 111 } 112 113 mapping(string => CoinInfo) diaOracles; 114 115 event newCoinInfo( 116 string name, 117 string symbol, 118 uint256 price, 119 uint256 supply, 120 uint256 lastUpdateTimestamp 121 ); 122 123 constructor() public { 124 owner = msg.sender; 125 } 126 127 function changeOwner(address newOwner) public { 128 require(msg.sender == owner); 129 owner = newOwner; 130 } 131 132 function updateCoinInfo(string name, string symbol, uint256 newPrice, uint256 newSupply, uint256 newTimestamp) public { 133 require(msg.sender == owner); 134 diaOracles[name] = (CoinInfo(newPrice, newSupply, newTimestamp, symbol)); 135 emit newCoinInfo(name, symbol, newPrice, newSupply, newTimestamp); 136 } 137 138 function getCoinInfo(string name) public view returns (uint256, uint256, uint256, string) { 139 return ( 140 diaOracles[name].price, 141 diaOracles[name].supply, 142 diaOracles[name].lastUpdateTimestamp, 143 diaOracles[name].symbol 144 ); 145 } 146 } 147 ``` 148 149 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. 150 151 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.