Skip to content

Commit

Permalink
Create DecentralizedOracle.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 4, 2024
1 parent 8f174a0 commit 0b65ce2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions coin/QuantumCoin/oracles/DecentralizedOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract DecentralizedOracle is Ownable {
struct DataFeed {
string name;
uint256 value;
uint256 timestamp;
address provider;
}

mapping(bytes32 => DataFeed) public dataFeeds;

event DataFeedUpdated(bytes32 indexed feedId, uint256 value, address indexed provider);

// Register a new data feed
function registerDataFeed(bytes32 feedId, string memory name) external onlyOwner {
require(dataFeeds[feedId].provider == address(0), "Data feed already exists");
dataFeeds[feedId] = DataFeed(name, 0, block.timestamp, msg.sender);
}

// Update the value of a data feed
function updateDataFeed(bytes32 feedId, uint256 value) external {
require(dataFeeds[feedId].provider == msg.sender, "Only the provider can update the data feed");
dataFeeds[feedId].value = value;
dataFeeds[feedId].timestamp = block.timestamp;

emit DataFeedUpdated(feedId, value, msg.sender);
}

// Retrieve the latest value of a data feed
function getDataFeed(bytes32 feedId) external view returns (string memory name, uint256 value, uint256 timestamp, address provider) {
DataFeed memory feed = dataFeeds[feedId];
return (feed.name, feed.value, feed.timestamp, feed.provider);
}
}

0 comments on commit 0b65ce2

Please sign in to comment.