Created
May 27, 2025 19:43
-
-
Save CeoFred/1ca271e2dfe0ceb8b6c44fff02b3ce66 to your computer and use it in GitHub Desktop.
PriceAggregator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
interface AggregatorInterface { | |
function latestAnswer() external view returns (int256); | |
function latestTimestamp() external view returns (uint256); | |
function latestRound() external view returns (uint256); | |
function getAnswer(uint256 roundId) external view returns (int256); | |
function getTimestamp(uint256 roundId) external view returns (uint256); | |
event AnswerUpdated(int256 indexed current, uint256 indexed roundId, uint256 updatedAt); | |
event NewRound(uint256 indexed roundId, address indexed startedBy, uint256 startedAt); | |
} | |
contract PriceAggregator is AggregatorInterface { | |
struct Round { | |
int256 answer; | |
uint256 timestamp; | |
bool exists; | |
} | |
address public owner; | |
string public description; | |
uint16 public decimals; | |
mapping(address => bool) public authorizedAddresses; | |
mapping(uint256 => Round) private rounds; | |
uint256 private currentRound; | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only owner can call this function"); | |
_; | |
} | |
modifier onlyAuthorized() { | |
require(authorizedAddresses[msg.sender], "Only authorized address can update"); | |
_; | |
} | |
constructor(int256 _initialAnswer, string memory _description,uint16 _decimals) { | |
owner = msg.sender; | |
authorizedAddresses[msg.sender] = true; | |
description = _description; | |
decimals = _decimals; | |
// Initialize first round | |
currentRound = 1; | |
rounds[currentRound] = Round({ | |
answer: _initialAnswer, | |
timestamp: block.timestamp, | |
exists: true | |
}); | |
emit AnswerUpdated(_initialAnswer, currentRound, block.timestamp); | |
emit NewRound(currentRound, msg.sender, block.timestamp); | |
} | |
// Owner functions | |
function addAuthorizedAddress(address _address) external onlyOwner { | |
require(_address != address(0), "Cannot authorize zero address"); | |
authorizedAddresses[_address] = true; | |
} | |
function removeAuthorizedAddress(address _address) external onlyOwner { | |
require(_address != owner, "Cannot remove owner authorization"); | |
authorizedAddresses[_address] = false; | |
} | |
function transferOwnership(address newOwner) external onlyOwner { | |
require(newOwner != address(0), "New owner cannot be zero address"); | |
owner = newOwner; | |
authorizedAddresses[newOwner] = true; | |
} | |
// Price update function | |
function updateAnswer(int256 _answer) external onlyAuthorized { | |
require(_answer > 0, "Price must be positive"); | |
uint256 roundId = currentRound + 1; | |
currentRound = roundId; | |
rounds[roundId] = Round({ | |
answer: _answer, | |
timestamp: block.timestamp, | |
exists: true | |
}); | |
emit NewRound(roundId, msg.sender, block.timestamp); | |
emit AnswerUpdated(_answer, roundId, block.timestamp); | |
} | |
// View functions implementing AggregatorInterface | |
function latestAnswer() external view override returns (int256) { | |
return rounds[currentRound].answer; | |
} | |
function latestTimestamp() external view override returns (uint256) { | |
return rounds[currentRound].timestamp; | |
} | |
function latestRound() external view override returns (uint256) { | |
return currentRound; | |
} | |
function getAnswer(uint256 roundId) external view override returns (int256) { | |
require(rounds[roundId].exists, "Round does not exist"); | |
return rounds[roundId].answer; | |
} | |
function getTimestamp(uint256 roundId) external view override returns (uint256) { | |
require(rounds[roundId].exists, "Round does not exist"); | |
return rounds[roundId].timestamp; | |
} | |
// Additional helper functions | |
function getRoundData(uint256 _roundId) external view returns ( | |
uint256 roundId, | |
int256 answer, | |
uint256 timestamp, | |
bool exists | |
) { | |
Round memory round = rounds[_roundId]; | |
return ( | |
_roundId, | |
round.answer, | |
round.timestamp, | |
round.exists | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment