-
Notifications
You must be signed in to change notification settings - Fork 91
/
TweetAccount.sol
96 lines (79 loc) · 2.78 KB
/
TweetAccount.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
/*
decentralized microblogging
Copyright (C) 2015 Jahn Bertsch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation in version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// "class" TweetAccount
contract TweetAccount {
// data structure of a single tweet
struct Tweet {
uint timestamp;
string tweetString;
}
// "array" of all tweets of this account: maps the tweet id to the actual tweet
mapping (uint => Tweet) _tweets;
// total number of tweets in the above _tweets mapping
uint _numberOfTweets;
// "owner" of this account: only admin is allowed to tweet
address _adminAddress;
// constructor
function TweetAccount() {
_numberOfTweets = 0;
_adminAddress = msg.sender;
}
// returns true if caller of function ("sender") is admin
function isAdmin() constant returns (bool isAdmin) {
return msg.sender == _adminAddress;
}
// create new tweet
function tweet(string tweetString) returns (int result) {
if (!isAdmin()) {
// only owner is allowed to create tweets for this account
result = -1;
} else if (bytes(tweetString).length > 160) {
// tweet contains more than 160 bytes
result = -2;
} else {
_tweets[_numberOfTweets].timestamp = now;
_tweets[_numberOfTweets].tweetString = tweetString;
_numberOfTweets++;
result = 0; // success
}
}
function getTweet(uint tweetId) constant returns (string tweetString, uint timestamp) {
// returns two values
tweetString = _tweets[tweetId].tweetString;
timestamp = _tweets[tweetId].timestamp;
}
function getLatestTweet() constant returns (string tweetString, uint timestamp, uint numberOfTweets) {
// returns three values
tweetString = _tweets[_numberOfTweets - 1].tweetString;
timestamp = _tweets[_numberOfTweets - 1].timestamp;
numberOfTweets = _numberOfTweets;
}
function getOwnerAddress() constant returns (address adminAddress) {
return _adminAddress;
}
function getNumberOfTweets() constant returns (uint numberOfTweets) {
return _numberOfTweets;
}
// other users can send donations to your account: use this function for donation withdrawal
function adminRetrieveDonations(address receiver) {
if (isAdmin()) {
receiver.send(this.balance);
}
}
function adminDeleteAccount() {
if (isAdmin()) {
suicide(_adminAddress); // this is a predefined function, it deletes the contract and returns all funds to the owner's address
}
}
}