|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 5 | +import "@openzeppelin/contracts/utils/Counters.sol"; |
| 6 | + |
| 7 | +contract PiDAO is Ownable { |
| 8 | + using Counters for Counters.Counter; |
| 9 | + |
| 10 | + struct Proposal { |
| 11 | + uint256 value; |
| 12 | + uint256 voteCount; |
| 13 | + uint256 endTime; |
| 14 | + bool executed; |
| 15 | + mapping(address => bool) hasVoted; |
| 16 | + } |
| 17 | + |
| 18 | + Counters.Counter private proposalIdCounter; |
| 19 | + mapping(uint256 => Proposal) public proposals; |
| 20 | + uint256 public constant GCV_VALUE = 314159 * 10**18; // $314,159 |
| 21 | + uint256 public quorum; // Minimum votes required to pass a proposal |
| 22 | + |
| 23 | + event ProposalCreated(uint256 indexed proposalId, uint256 value, uint256 endTime); |
| 24 | + event Voted(uint256 indexed proposalId, address indexed voter); |
| 25 | + event ProposalExecuted(uint256 indexed proposalId, uint256 value); |
| 26 | + |
| 27 | + constructor(uint256 _quorum) { |
| 28 | + require(_quorum > 0, "Quorum must be greater than 0"); |
| 29 | + quorum = _quorum; |
| 30 | + } |
| 31 | + |
| 32 | + function proposeValue(uint256 value) public { |
| 33 | + require(value == GCV_VALUE, "Value must be $314,159"); |
| 34 | + |
| 35 | + proposalIdCounter.increment(); |
| 36 | + uint256 proposalId = proposalIdCounter.current(); |
| 37 | + |
| 38 | + Proposal storage newProposal = proposals[proposalId]; |
| 39 | + newProposal.value = value; |
| 40 | + newProposal.endTime = block.timestamp + 7 days; // Voting period of 7 days |
| 41 | + |
| 42 | + emit ProposalCreated(proposalId, value, newProposal.endTime); |
| 43 | + } |
| 44 | + |
| 45 | + function vote(uint256 proposalId) public { |
| 46 | + Proposal storage proposal = proposals[proposalId]; |
| 47 | + |
| 48 | + require(block.timestamp < proposal.endTime, "Voting period has ended"); |
| 49 | + require(!proposal.hasVoted[msg.sender], "You have already voted on this proposal"); |
| 50 | + |
| 51 | + proposal.voteCount += 1; |
| 52 | + proposal.hasVoted[msg.sender] = true; |
| 53 | + |
| 54 | + emit Voted(proposalId, msg.sender); |
| 55 | + |
| 56 | + // Check if the proposal has reached quorum |
| 57 | + if (proposal.voteCount >= quorum) { |
| 58 | + executeProposal(proposalId); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + function executeProposal(uint256 proposalId) internal { |
| 63 | + Proposal storage proposal = proposals[proposalId]; |
| 64 | + |
| 65 | + require(block.timestamp >= proposal.endTime, "Voting period has not ended"); |
| 66 | + require(!proposal.executed, "Proposal has already been executed"); |
| 67 | + |
| 68 | + proposal.executed = true; |
| 69 | + |
| 70 | + // Logic to affirm the GCV value can be added here |
| 71 | + // For example, updating a state variable or triggering an event |
| 72 | + |
| 73 | + emit ProposalExecuted(proposalId, proposal.value); |
| 74 | + } |
| 75 | + |
| 76 | + function getProposal(uint256 proposalId) public view returns (uint256 value, uint256 voteCount, uint256 endTime, bool executed) { |
| 77 | + Proposal storage proposal = proposals[proposalId]; |
| 78 | + return (proposal.value, proposal.voteCount, proposal.endTime, proposal.executed); |
| 79 | + } |
| 80 | + |
| 81 | + function setQuorum(uint256 newQuorum) public onlyOwner { |
| 82 | + require(newQuorum > 0, "Quorum must be greater than 0"); |
| 83 | + quorum = newQuorum; |
| 84 | + } |
| 85 | +} |
0 commit comments