-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proposal_Contract.sol
115 lines (90 loc) · 3.08 KB
/
Proposal_Contract.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract ProposalContract {
address owner;
uint256 private counter;
address[] private voted_addresses;
struct Proposal {
string title;
string description;
uint256 approve;
uint256 reject;
uint256 pass;
uint256 total_vote_to_end;
bool current_state;
bool is_active;
}
mapping (uint256 => Proposal) proposal_history;
constructor(){
owner = msg.sender;
voted_addresses.push(msg.sender);
}
modifier only_owner() {
require(owner == msg.sender, "You have to be a sender to create a contract!");
_;
}
modifier active() {
require(proposal_history[counter].is_active == true, "The proposal is not active");
_;
}
modifier new_voter(address _address){
require(!is_voted(_address), "Address has already voted.");
_;
}
function set_owner(address new_owner) external only_owner {
owner = new_owner;
}
function create(string calldata _title, string calldata _description, uint256 _total_vote_to_end) external only_owner {
counter += 1;
proposal_history[counter] = Proposal(_title, _description, 0, 0, 0, _total_vote_to_end, false, true);
}
function vote(uint8 choice) external {
Proposal storage proposal = proposal_history[counter];
if (choice == 1) {
proposal.approve += 1 ;
} else if (choice == 2) {
proposal.reject += 1;
} else if (choice == 0) {
proposal.pass += 1;
}
voted_addresses.push(msg.sender);
uint256 total_vote = proposal.approve + proposal.reject + proposal.pass;
proposal.current_state = calculateCurrentState();
if ((proposal.total_vote_to_end - total_vote == 0) && (choice == 1 || choice == 2 || choice == 3)){
proposal.is_active = false;
voted_addresses = [owner];
}
}
function calculateCurrentState() private view returns (bool) {
Proposal storage proposal = proposal_history[counter];
uint256 approve = proposal.approve;
uint256 reject = proposal.reject;
uint256 pass = proposal.pass;
if (proposal.pass % 2 == 1){
pass += 1;
}
pass = pass / 2;
if (approve > reject + pass){
return true;
} else {
return false;
}
}
function terminate_proposal() external only_owner active {
proposal_history[counter].is_active = false;
}
function is_voted(address _address) public view returns (bool) {
for (uint i = 0; i < voted_addresses.length; i++){
if (voted_addresses[i] == _address){
return true;
}
}
return false;
}
function getCurrentProposal() external view returns (Proposal memory) {
return proposal_history[counter];
}
function getProposal(uint256 number) external view returns (Proposal memory) {
return proposal_history[number];
}
}