-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingSystem.sol
More file actions
133 lines (114 loc) · 4.25 KB
/
VotingSystem.sol
File metadata and controls
133 lines (114 loc) · 4.25 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HeThongBieuQuyet {
struct DeXuat {
uint256 maDeXuat;
string tieuDe;
string moTa;
uint256 thoiGianBatDau;
uint256 thoiGianKetThuc;
uint256 soPhieuDongY;
uint256 soPhieuPhanDoi;
bool dangHoatDong;
mapping(address => bool) daBieuQuyet;
}
struct ThongTinNguoiBieuQuyet {
address vi;
bytes32 maHoaThongTin;
}
address public chuSoHuu;
uint256 public tongSoDeXuat;
mapping(uint256 => DeXuat) public danhSachDeXuat;
mapping(uint256 => ThongTinNguoiBieuQuyet[]) public danhSachNguoiBieuQuyet;
event TaoDeXuat(uint256 indexed maDeXuat, string tieuDe, uint256 thoiGianBatDau, uint256 thoiGianKetThuc);
event DaBieuQuyet(uint256 indexed maDeXuat, address nguoiBieuQuyet, bool luaChon);
event DongDeXuat(uint256 indexed maDeXuat, uint256 soPhieuDongY, uint256 soPhieuPhanDoi);
modifier chiChuSoHuu() {
require(msg.sender == chuSoHuu, "Chi co chu so huu moi co quyen nay");
_;
}
modifier deXuatTonTai(uint256 _maDeXuat) {
require(_maDeXuat > 0 && _maDeXuat <= tongSoDeXuat, "De xuat khong ton tai");
_;
}
constructor() {
chuSoHuu = msg.sender;
tongSoDeXuat = 0;
}
function taoDeXuat(
string memory _tieuDe,
string memory _moTa,
uint256 _thoiGianPhut
) public chiChuSoHuu {
tongSoDeXuat++;
uint256 thoiGianBatDau = block.timestamp;
uint256 thoiGianKetThuc = thoiGianBatDau + (_thoiGianPhut * 1 minutes);
DeXuat storage dx = danhSachDeXuat[tongSoDeXuat];
dx.maDeXuat = tongSoDeXuat;
dx.tieuDe = _tieuDe;
dx.moTa = _moTa;
dx.thoiGianBatDau = thoiGianBatDau;
dx.thoiGianKetThuc = thoiGianKetThuc;
dx.dangHoatDong = true;
emit TaoDeXuat(tongSoDeXuat, _tieuDe, thoiGianBatDau, thoiGianKetThuc);
}
function bieuQuyet(
uint256 _maDeXuat,
bool _luaChon,
string memory _hoTen,
string memory _email,
string memory _sdt
) public deXuatTonTai(_maDeXuat) {
DeXuat storage dx = danhSachDeXuat[_maDeXuat];
require(dx.dangHoatDong, "De xuat da ket thuc");
require(block.timestamp >= dx.thoiGianBatDau && block.timestamp <= dx.thoiGianKetThuc, "Khong trong thoi gian bieu quyet");
require(!dx.daBieuQuyet[msg.sender], "Ban da bieu quyet roi");
dx.daBieuQuyet[msg.sender] = true;
if (_luaChon) {
dx.soPhieuDongY++;
} else {
dx.soPhieuPhanDoi++;
}
// Tạo hash từ thông tin cá nhân và dữ liệu biểu quyết
bytes32 maHoa = sha256(
abi.encodePacked(
_hoTen,
_email,
_sdt,
_maDeXuat,
block.timestamp,
_luaChon
)
);
danhSachNguoiBieuQuyet[_maDeXuat].push(
ThongTinNguoiBieuQuyet({
vi: msg.sender,
maHoaThongTin: maHoa
})
);
emit DaBieuQuyet(_maDeXuat, msg.sender, _luaChon);
}
function dongDeXuat(uint256 _maDeXuat) public deXuatTonTai(_maDeXuat) {
DeXuat storage dx = danhSachDeXuat[_maDeXuat];
require(dx.dangHoatDong, "De xuat da ket thuc");
require(block.timestamp > dx.thoiGianKetThuc, "Chua den thoi gian ket thuc");
dx.dangHoatDong = false;
emit DongDeXuat(_maDeXuat, dx.soPhieuDongY, dx.soPhieuPhanDoi);
}
function layDanhSachNguoiBieuQuyet(uint256 _maDeXuat) public view returns (
address[] memory vis, bytes32[] memory hashThongTins
) {
uint256 len = danhSachNguoiBieuQuyet[_maDeXuat].length;
vis = new address[](len);
hashThongTins = new bytes32[](len);
for (uint256 i = 0; i < len; i++) {
vis[i] = danhSachNguoiBieuQuyet[_maDeXuat][i].vi;
hashThongTins[i] = danhSachNguoiBieuQuyet[_maDeXuat][i].maHoaThongTin;
}
return (vis, hashThongTins);
}
// Hàm kiểm tra đã biểu quyết
function daBieuQuyet(uint256 _maDeXuat, address _nguoi) public view returns (bool) {
return danhSachDeXuat[_maDeXuat].daBieuQuyet[_nguoi];
}
}