From 33c3efd0d0eae219e4bbce7d181d3f618cf153fd Mon Sep 17 00:00:00 2001 From: Kapten boneng Date: Sun, 5 Jan 2025 12:30:38 +0700 Subject: [PATCH] Create Kode Oracle untuk QPi-Stablecoin Fitur Utama 1. Keamanan Tinggi: Hanya authorizedUpdaters yang dapat memperbarui harga menggunakan updatePrice. Fungsi ini memeriksa apakah pengirim diizinkan dan memastikan jeda waktu (interval) antar pembaruan. 2. Interval Pembaruan: priceUpdateInterval memastikan pembaruan harga hanya terjadi setelah jangka waktu tertentu untuk mencegah serangan pembaruan berulang. 3. Fleksibilitas: Pemilik kontrak dapat menambahkan atau menghapus akun yang diizinkan menggunakan setAuthorizedUpdater. Interval pembaruan dapat diubah dengan fungsi setUpdateInterval. 4. Kompatibilitas Lintas Kontrak: Data harga dapat digunakan oleh kontrak lain melalui fungsi fetchCurrentPrice. 5. Transparansi: Semua perubahan harga dan otorisasi pembaruan dicatat melalui event seperti PriceUpdated dan UpdaterAuthorized. --- Cara Menggunakan 1. Inisialisasi Kontrak: Deploy kontrak ini dengan harga awal (contoh: 314159) dan interval pembaruan (contoh: 300 detik). 2. Integrasi dengan Kontrak Utama: Gunakan fungsi fetchCurrentPrice dalam kontrak QPi-Stablecoin untuk mendapatkan harga terkini. 3. Pembaruan Harga: Tetapkan updater yang sah menggunakan setAuthorizedUpdater. Perbarui harga dengan memanggil updatePrice. 4. Uji dan Audit: Lakukan pengujian menyeluruh untuk memastikan integritas data harga dan keamanan kontrak. --- Pengembangan Lanjutan Integrasi API: Tambahkan fungsi untuk mengintegrasikan data harga dari sumber eksternal (seperti Chainlink). Kriptografi Tahan Kuantum: Tambahkan mekanisme untuk mengamankan komunikasi data menggunakan algoritma tahan kuantum. Decentralized Oracle Network (DON): Integrasikan dengan jaringan oracle terdesentralisasi untuk meningkatkan keandalan data. --- Kode Oracle untuk QPi-Stablecoin | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Kode Oracle untuk QPi-Stablecoin diff --git a/Kode Oracle untuk QPi-Stablecoin b/Kode Oracle untuk QPi-Stablecoin new file mode 100644 index 0000000..f58f95b --- /dev/null +++ b/Kode Oracle untuk QPi-Stablecoin @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +import "@openzeppelin/contracts/access/Ownable.sol"; +import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; + +/// @title Quantum Oracle for QPi-Stablecoin +/// @notice Provides secure and reliable price feeds for QPi Stablecoin +contract QPiOracle is Ownable, ReentrancyGuard { + uint256 public lastUpdated; // Timestamp of the last update + uint256 public currentPrice; // Current price in USD (with 6 decimals precision) + uint256 public priceUpdateInterval; // Minimum interval between updates + + mapping(address => bool) public authorizedUpdaters; // Addresses allowed to update the price + + event PriceUpdated(uint256 newPrice, uint256 timestamp); + event UpdaterAuthorized(address indexed updater, bool status); + event UpdateIntervalChanged(uint256 newInterval); + + constructor(uint256 _initialPrice, uint256 _priceUpdateInterval) { + require(_initialPrice > 0, "Initial price must be greater than zero"); + require(_priceUpdateInterval > 0, "Update interval must be greater than zero"); + + currentPrice = _initialPrice; + priceUpdateInterval = _priceUpdateInterval; + lastUpdated = block.timestamp; + + authorizedUpdaters[msg.sender] = true; // Contract owner as default updater + } + + /// @notice Updates the price feed + /// @param _newPrice The new price to be set (in USD with 6 decimals precision) + function updatePrice(uint256 _newPrice) external nonReentrant { + require(authorizedUpdaters[msg.sender], "Caller is not an authorized updater"); + require(_newPrice > 0, "Price must be greater than zero"); + require(block.timestamp >= lastUpdated + priceUpdateInterval, "Update interval not reached"); + + currentPrice = _newPrice; + lastUpdated = block.timestamp; + + emit PriceUpdated(_newPrice, block.timestamp); + } + + /// @notice Authorizes or revokes authorization for an updater + /// @param _updater Address of the updater + /// @param _status True to authorize, false to revoke + function setAuthorizedUpdater(address _updater, bool _status) external onlyOwner { + authorizedUpdaters[_updater] = _status; + emit UpdaterAuthorized(_updater, _status); + } + + /// @notice Changes the price update interval + /// @param _newInterval The new interval in seconds + function setUpdateInterval(uint256 _newInterval) external onlyOwner { + require(_newInterval > 0, "Update interval must be greater than zero"); + priceUpdateInterval = _newInterval; + emit UpdateIntervalChanged(_newInterval); + } + + /// @notice Fetches the current price and last updated timestamp + /// @return price The current price in USD with 6 decimals precision + /// @return timestamp The timestamp of the last update + function getPrice() external view returns (uint256 price, uint256 timestamp) { + return (currentPrice, lastUpdated); + } + + /// @notice Fetches the current price for use in external smart contracts + /// @return The current price in USD with 6 decimals precision + function fetchCurrentPrice() external view returns (uint256) { + return currentPrice; + } +}