-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGovernanceContract.sol
63 lines (51 loc) · 1.73 KB
/
GovernanceContract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// GovernanceContract.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract GovernanceContract {
address public admin;
uint256 public proposalCount;
struct Proposal {
uint256 id;
string description;
uint256 forVotes;
uint256 againstVotes;
bool executed;
}
mapping(uint256 => Proposal) public proposals;
event ProposalCreated(uint256 id, string description);
event Voted(uint256 id, bool inSupport, address voter);
event ProposalExecuted(uint256 id);
constructor() {
admin = msg.sender;
}
modifier onlyAdmin() {
require(msg.sender == admin, "Not the admin");
_;
}
function createProposal(string memory description) external onlyAdmin {
uint256 id = proposalCount++;
proposals[id] = Proposal(id, description, 0, 0, false);
emit ProposalCreated(id, description);
}
function vote(uint256 id, bool inSupport) external {
require(id < proposalCount, "Invalid proposal ID");
Proposal storage proposal = proposals[id];
require(!proposal.executed, "Proposal already executed");
if (inSupport) {
proposal.forVotes++;
} else {
proposal.againstVotes++;
}
emit Voted(id, inSupport, msg.sender);
}
function executeProposal(uint256 id) external onlyAdmin {
require(id < proposalCount, "Invalid proposal ID");
Proposal storage proposal = proposals[id];
require(!proposal.executed, "Proposal already executed");
if (proposal.forVotes > proposal.againstVotes) {
// Execute the proposal
proposal.executed = true;
emit ProposalExecuted(id);
}
}
}