From 0b65ce2c0d731402bc74c994c4b5199e7cb52451 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 4 Dec 2024 16:49:14 +0700 Subject: [PATCH] Create DecentralizedOracle.sol --- .../oracles/DecentralizedOracle.sol | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 coin/QuantumCoin/oracles/DecentralizedOracle.sol diff --git a/coin/QuantumCoin/oracles/DecentralizedOracle.sol b/coin/QuantumCoin/oracles/DecentralizedOracle.sol new file mode 100644 index 000000000..eb974472b --- /dev/null +++ b/coin/QuantumCoin/oracles/DecentralizedOracle.sol @@ -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); + } +}