-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract.sol
More file actions
237 lines (206 loc) · 6.05 KB
/
contract.sol
File metadata and controls
237 lines (206 loc) · 6.05 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "./interfaces/IMemeCourtVoting.sol";
/**
* @title MemeCourtVoting
* @dev Core voting contract for MemeCourt platform
* @notice Records like/dislike votes on meme posts immutably on MemeCore blockchain
*/
contract MemeCourtVoting is IMemeCourtVoting {
// State variables
mapping(bytes32 => Vote) private _votes; // keccak256(voter, postId) => Vote
mapping(string => PostStats) private _postStats;
mapping(string => bool) private _registeredPosts;
mapping(address => string[]) private _userVotes; // Track user's voted posts
address public owner;
uint256 public totalVotes;
uint256 public totalPosts;
// Modifiers
modifier onlyOwner() {
require(msg.sender == owner, "MCV: Only owner");
_;
}
modifier validPostId(string calldata postId) {
require(bytes(postId).length > 0, "MCV: Invalid post ID");
_;
}
constructor() {
owner = msg.sender;
}
/**
* @dev Register a new post (can be called by anyone)
* @param postId Unique identifier for the post
* @param contentHash IPFS or content hash for verification
*/
function registerPost(
string calldata postId,
string calldata contentHash
)
external
validPostId(postId)
{
require(!_registeredPosts[postId], "MCV: Post already registered");
_registeredPosts[postId] = true;
_postStats[postId] = PostStats({
funnyVotes: 0,
notFunnyVotes: 0,
totalVotes: 0,
exists: true
});
totalPosts++;
emit PostRegistered(postId, msg.sender, contentHash, block.timestamp);
}
/**
* @dev Cast a vote on a post (like/dislike)
* @param postId ID of the post to vote on
* @param isFunny true for like, false for dislike
*/
function castVote(
string calldata postId,
bool isFunny
)
external
validPostId(postId)
{
bytes32 voteKey = _getVoteKey(msg.sender, postId);
require(_votes[voteKey].voter == address(0), "MCV: Already voted");
// Auto-register post if not exists
if (!_registeredPosts[postId]) {
_registeredPosts[postId] = true;
_postStats[postId] = PostStats({
funnyVotes: 0,
notFunnyVotes: 0,
totalVotes: 0,
exists: true
});
totalPosts++;
emit PostRegistered(postId, msg.sender, "", block.timestamp);
}
// Record vote
_votes[voteKey] = Vote({
voter: msg.sender,
postId: postId,
isFunny: isFunny,
timestamp: block.timestamp
});
// Update statistics
PostStats storage stats = _postStats[postId];
if (isFunny) {
stats.funnyVotes++;
} else {
stats.notFunnyVotes++;
}
stats.totalVotes++;
// Track user votes
_userVotes[msg.sender].push(postId);
totalVotes++;
emit VoteCast(msg.sender, postId, isFunny, block.timestamp);
}
/**
* @dev Change an existing vote
* @param postId ID of the post
* @param newVote New vote value
*/
function changeVote(
string calldata postId,
bool newVote
)
external
validPostId(postId)
{
bytes32 voteKey = _getVoteKey(msg.sender, postId);
Vote storage vote = _votes[voteKey];
require(vote.voter != address(0), "MCV: No existing vote");
require(vote.isFunny != newVote, "MCV: Same vote value");
bool oldVote = vote.isFunny;
// Update vote
vote.isFunny = newVote;
vote.timestamp = block.timestamp;
// Update statistics
PostStats storage stats = _postStats[postId];
if (oldVote) {
stats.funnyVotes--;
stats.notFunnyVotes++;
} else {
stats.notFunnyVotes--;
stats.funnyVotes++;
}
emit VoteChanged(msg.sender, postId, oldVote, newVote, block.timestamp);
}
/**
* @dev Get vote information for a user and post
*/
function getVote(
address voter,
string calldata postId
)
external
view
returns (bool isFunny, uint256 timestamp, bool hasVoted)
{
bytes32 voteKey = _getVoteKey(voter, postId);
Vote memory vote = _votes[voteKey];
if (vote.voter != address(0)) {
return (vote.isFunny, vote.timestamp, true);
}
return (false, 0, false);
}
/**
* @dev Get statistics for a post
*/
function getPostStats(string calldata postId)
external
view
returns (PostStats memory)
{
return _postStats[postId];
}
/**
* @dev Check if user has voted on a post
*/
function hasVoted(address voter, string calldata postId)
external
view
returns (bool)
{
bytes32 voteKey = _getVoteKey(voter, postId);
return _votes[voteKey].voter != address(0);
}
/**
* @dev Get all posts a user has voted on
*/
function getUserVotedPosts(address user)
external
view
returns (string[] memory)
{
return _userVotes[user];
}
/**
* @dev Get user vote count
*/
function getUserVoteCount(address user)
external
view
returns (uint256)
{
return _userVotes[user].length;
}
/**
* @dev Emergency pause (only owner)
*/
function pause() external onlyOwner {
// Implementation for emergency pause if needed
revert("MCV: Not implemented");
}
/**
* @dev Generate unique key for vote mapping
*/
function _getVoteKey(address voter, string calldata postId)
private
pure
returns (bytes32)
{
return keccak256(abi.encodePacked(voter, postId));
}
}