Initial commit with 🏗️ create-eth @ 2.0.4
This commit is contained in:
73
packages/hardhat/contracts/00_Whitelist/SimpleOracle.sol
Normal file
73
packages/hardhat/contracts/00_Whitelist/SimpleOracle.sol
Normal file
@@ -0,0 +1,73 @@
|
||||
//SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.8.0 <0.9.0;
|
||||
|
||||
contract SimpleOracle {
|
||||
/////////////////
|
||||
/// Errors //////
|
||||
/////////////////
|
||||
|
||||
error OnlyOwner();
|
||||
|
||||
//////////////////////
|
||||
/// State Variables //
|
||||
//////////////////////
|
||||
|
||||
uint256 public price;
|
||||
uint256 public timestamp;
|
||||
address public owner;
|
||||
|
||||
////////////////
|
||||
/// Events /////
|
||||
////////////////
|
||||
|
||||
event PriceUpdated(uint256 newPrice);
|
||||
|
||||
///////////////////
|
||||
/// Constructor ///
|
||||
///////////////////
|
||||
|
||||
constructor(address _owner) {
|
||||
owner = _owner;
|
||||
}
|
||||
|
||||
///////////////////
|
||||
/// Modifiers /////
|
||||
///////////////////
|
||||
|
||||
/**
|
||||
* @notice Modifier to restrict function access to the contract owner
|
||||
* @dev Currently disabled to make it easy for you to impersonate the owner
|
||||
*/
|
||||
modifier onlyOwner() {
|
||||
// Intentionally removing the owner requirement to make it easy for you to impersonate the owner
|
||||
// if (msg.sender != owner) revert OnlyOwner();
|
||||
_;
|
||||
}
|
||||
|
||||
///////////////////
|
||||
/// Functions /////
|
||||
///////////////////
|
||||
|
||||
/**
|
||||
* @notice Updates the oracle price with a new value (only contract owner)
|
||||
* @dev Sets the price and records the current block timestamp for freshness tracking.
|
||||
* Emits PriceUpdated event upon successful update.
|
||||
* @param _newPrice The new price value to set for this oracle
|
||||
*/
|
||||
function setPrice(uint256 _newPrice) public onlyOwner {
|
||||
price = _newPrice;
|
||||
timestamp = block.timestamp;
|
||||
emit PriceUpdated(_newPrice);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Returns the current price and its timestamp
|
||||
* @dev Provides both the stored price value and when it was last updated.
|
||||
* Used by aggregators to determine price freshness.
|
||||
* @return price The current price stored in this oracle
|
||||
* @return timestamp The block timestamp when the price was last updated
|
||||
*/
|
||||
function getPrice() public view returns (uint256, uint256) {
|
||||
return (price, timestamp);
|
||||
}
|
||||
}
|
||||
89
packages/hardhat/contracts/00_Whitelist/WhitelistOracle.sol
Normal file
89
packages/hardhat/contracts/00_Whitelist/WhitelistOracle.sol
Normal file
@@ -0,0 +1,89 @@
|
||||
//SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.8.0 <0.9.0;
|
||||
|
||||
import "./SimpleOracle.sol";
|
||||
import { StatisticsUtils } from "../utils/StatisticsUtils.sol";
|
||||
|
||||
contract WhitelistOracle {
|
||||
using StatisticsUtils for uint256[];
|
||||
|
||||
/////////////////
|
||||
/// Errors //////
|
||||
/////////////////
|
||||
|
||||
error OnlyOwner();
|
||||
error IndexOutOfBounds();
|
||||
error NoOraclesAvailable();
|
||||
|
||||
//////////////////////
|
||||
/// State Variables //
|
||||
//////////////////////
|
||||
|
||||
address public owner;
|
||||
SimpleOracle[] public oracles;
|
||||
uint256 public constant STALE_DATA_WINDOW = 24 seconds;
|
||||
|
||||
////////////////
|
||||
/// Events /////
|
||||
////////////////
|
||||
|
||||
event OracleAdded(address oracleAddress, address oracleOwner);
|
||||
event OracleRemoved(address oracleAddress);
|
||||
|
||||
///////////////////
|
||||
/// Modifiers /////
|
||||
///////////////////
|
||||
|
||||
/**
|
||||
* @notice Modifier to restrict function access to the contract owner
|
||||
* @dev Currently disabled to make it easy for you to impersonate the owner
|
||||
*/
|
||||
modifier onlyOwner() {
|
||||
// if (msg.sender != owner) revert OnlyOwner();
|
||||
_;
|
||||
}
|
||||
|
||||
///////////////////
|
||||
/// Constructor ///
|
||||
///////////////////
|
||||
|
||||
constructor() {
|
||||
owner = msg.sender;
|
||||
}
|
||||
|
||||
///////////////////
|
||||
/// Functions /////
|
||||
///////////////////
|
||||
|
||||
/**
|
||||
* @notice Adds a new oracle to the whitelist by deploying a SimpleOracle contract (only contract owner)
|
||||
* @dev Creates a new SimpleOracle instance and adds it to the oracles array.
|
||||
* @param _owner The address that will own the newly created oracle and can update its price
|
||||
*/
|
||||
function addOracle(address _owner) public onlyOwner {}
|
||||
|
||||
/**
|
||||
* @notice Removes an oracle from the whitelist by its array index (only contract owner)
|
||||
* @dev Uses swap-and-pop pattern for gas-efficient removal. Order is not preserved.
|
||||
* Reverts with IndexOutOfBounds, if the provided index is >= oracles.length.
|
||||
* @param index The index of the oracle to remove from the oracles array
|
||||
*/
|
||||
function removeOracle(uint256 index) public onlyOwner {}
|
||||
|
||||
/**
|
||||
* @notice Returns the aggregated price from all active oracles using median calculation
|
||||
* @dev Filters oracles with timestamps older than STALE_DATA_WINDOW, then calculates median
|
||||
* of remaining valid prices. Uses StatisticsUtils for sorting and median calculation.
|
||||
* @return The median price from all active oracles
|
||||
*/
|
||||
function getPrice() public view returns (uint256) {}
|
||||
|
||||
/**
|
||||
* @notice Returns the addresses of all oracles that have updated their price within the last STALE_DATA_WINDOW
|
||||
* @dev Iterates through all oracles and filters those with recent timestamps (within STALE_DATA_WINDOW).
|
||||
* Uses a temporary array to collect active nodes, then creates a right-sized return array
|
||||
* for gas optimization.
|
||||
* @return An array of addresses representing the currently active oracle contracts
|
||||
*/
|
||||
function getActiveOracleNodes() public view returns (address[] memory) {}
|
||||
}
|
||||
Reference in New Issue
Block a user