|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/access/AccessControl.sol"; |
| 5 | +import "@openzeppelin/contracts/utils/math/SafeMath.sol"; |
| 6 | + |
| 7 | +contract Oracle is AccessControl { |
| 8 | + using SafeMath for uint256; |
| 9 | + |
| 10 | + // Define roles |
| 11 | + bytes32 public constant ORACLE_ROLE = keccak256("ORACLE_ROLE"); |
| 12 | + bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); |
| 13 | + |
| 14 | + // Event to emit when new data is submitted |
| 15 | + event DataUpdated(string indexed dataType, string data, address indexed oracle, uint256 timestamp); |
| 16 | + |
| 17 | + // Struct to hold the data |
| 18 | + struct Data { |
| 19 | + string data; |
| 20 | + uint256 timestamp; |
| 21 | + address[] oracles; |
| 22 | + uint256 confirmations; |
| 23 | + } |
| 24 | + |
| 25 | + // Mapping to store data by type |
| 26 | + mapping(string => Data) private dataStore; |
| 27 | + |
| 28 | + // Maximum confirmations required for data to be considered valid |
| 29 | + uint256 public constant MAX_CONFIRMATIONS = 3; |
| 30 | + |
| 31 | + constructor() { |
| 32 | + _setupRole(ADMIN_ROLE, msg.sender); |
| 33 | + _setupRole(ORACLE_ROLE, msg.sender); // Initially, the deployer is an oracle |
| 34 | + } |
| 35 | + |
| 36 | + // Modifier to check if the sender is an oracle |
| 37 | + modifier onlyOracle() { |
| 38 | + require(hasRole(ORACLE_ROLE, msg.sender), "Caller is not an oracle"); |
| 39 | + _; |
| 40 | + } |
| 41 | + |
| 42 | + // Function to submit data from an oracle |
| 43 | + function submitData(string memory dataType, string memory data) external onlyOracle { |
| 44 | + require(bytes(dataType).length > 0, "Data type cannot be empty"); |
| 45 | + require(bytes(data).length > 0, "Data cannot be empty"); |
| 46 | + |
| 47 | + Data storage dataEntry = dataStore[dataType]; |
| 48 | + |
| 49 | + // Check if the data is already submitted |
| 50 | + if (dataEntry.timestamp == 0) { |
| 51 | + // New data entry |
| 52 | + dataEntry.data = data; |
| 53 | + dataEntry.timestamp = block.timestamp; |
| 54 | + dataEntry.oracles.push(msg.sender); |
| 55 | + dataEntry.confirmations = 1; |
| 56 | + } else { |
| 57 | + // Existing data entry, check for duplicates |
| 58 | + for (uint256 i = 0; i < dataEntry.oracles.length; i++) { |
| 59 | + require(dataEntry.oracles[i] != msg.sender, "Oracle has already submitted data for this type"); |
| 60 | + } |
| 61 | + // Update data and increment confirmations |
| 62 | + dataEntry.confirmations = dataEntry.confirmations.add(1); |
| 63 | + dataEntry.oracles.push(msg.sender); |
| 64 | + } |
| 65 | + |
| 66 | + // Emit an event for the new data |
| 67 | + emit DataUpdated(dataType, data, msg.sender, block.timestamp); |
| 68 | + |
| 69 | + // Check if we have enough confirmations |
| 70 | + if (dataEntry.confirmations >= MAX_CONFIRMATIONS) { |
| 71 | + // Data is considered valid, further actions can be taken here |
| 72 | + // For example, you could trigger another event or call another contract |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Function to retrieve data by type |
| 77 | + function getData(string memory dataType) external view returns (string memory, uint256, address[] memory) { |
| 78 | + Data memory dataEntry = dataStore[dataType]; |
| 79 | + require(bytes(dataEntry.data).length > 0, "No data found for this type"); |
| 80 | + |
| 81 | + return (dataEntry.data, dataEntry.timestamp, dataEntry.oracles); |
| 82 | + } |
| 83 | + |
| 84 | + // Function to delete data (only for admin) |
| 85 | + function deleteData(string memory dataType) external onlyRole(ADMIN_ROLE) { |
| 86 | + require(bytes(dataType).length > 0, "Data type cannot be empty"); |
| 87 | + require(bytes(dataStore[dataType].data).length > 0, "No data found for this type"); |
| 88 | + |
| 89 | + delete dataStore[dataType]; |
| 90 | + } |
| 91 | + |
| 92 | + // Function to add an oracle (only for admin) |
| 93 | + function addOracle(address oracle) external onlyRole(ADMIN_ROLE) { |
| 94 | + grantRole(ORACLE_ROLE, oracle); |
| 95 | + } |
| 96 | + |
| 97 | + // Function to remove an oracle (only for admin) |
| 98 | + function removeOracle(address oracle) external onlyRole(ADMIN_ROLE) { |
| 99 | + revokeRole(ORACLE_ROLE, oracle); |
| 100 | + } |
| 101 | +} |
0 commit comments