-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainPosts.sol
More file actions
75 lines (57 loc) · 1.8 KB
/
Copy pathChainPosts.sol
File metadata and controls
75 lines (57 loc) · 1.8 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
contract ChainPosts {
uint256 public constant MAX_POST_LENGTH = 280;
struct Post {
uint256 id;
address author;
string content;
uint256 timestamp;
}
error PostEmpty();
error PostTooLong(uint256 length, uint256 maxLength);
error PostNotFound(uint256 id);
event PostCreated(
uint256 indexed id,
address indexed author,
string content,
uint256 timestamp
);
uint256 public totalPosts;
mapping(uint256 => Post) private postsById;
function createPost(string calldata content) external {
uint256 contentLength = bytes(content).length;
if (contentLength == 0) {
revert PostEmpty();
}
if (contentLength > MAX_POST_LENGTH) {
revert PostTooLong(contentLength, MAX_POST_LENGTH);
}
uint256 postId = totalPosts;
postsById[postId] = Post({
id: postId,
author: msg.sender,
content: content,
timestamp: block.timestamp
});
totalPosts = postId + 1;
emit PostCreated(postId, msg.sender, content, block.timestamp);
}
function getPost(uint256 id) external view returns (Post memory) {
if (id >= totalPosts) {
revert PostNotFound(id);
}
return postsById[id];
}
function getRecentPosts(uint256 count) external view returns (Post[] memory) {
uint256 availablePosts = totalPosts;
if (count > availablePosts) {
count = availablePosts;
}
Post[] memory recentPosts = new Post[](count);
for (uint256 i = 0; i < count; i++) {
recentPosts[i] = postsById[availablePosts - i - 1];
}
return recentPosts;
}
}