-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-Twitter-Struct-Solution.sol
41 lines (31 loc) · 954 Bytes
/
13-Twitter-Struct-Solution.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// 1️⃣ Define a Tweet Struct with author, content, timestamp, likes
// 2️⃣ Add the struct to array
// 3️⃣ Test Tweets
contract Twitter {
// define our struct
struct Tweet {
address author;
string content;
uint256 timestamp;
uint256 likes;
}
// add our code
mapping(address => Tweet[] ) public tweets;
function createTweet(string memory _tweet) public {
Tweet memory newTweet = Tweet({
author: msg.sender,
content: _tweet,
timestamp: block.timestamp,
likes: 0
});
tweets[msg.sender].push(newTweet);
}
function getTweet(address _owner, uint _i) public view returns (Tweet memory) {
return tweets[_owner][_i];
}
function getAllTweets(address _owner) public view returns (Tweet[] memory ){
return tweets[_owner];
}
}