-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostcardContract.sol
49 lines (40 loc) · 1.26 KB
/
PostcardContract.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
pragma solidity ^0.4.21;
contract PostcardContract {
struct Postcard{
string username;
string content;
string ip;
string timestamp;
}
Postcard[] public cards;
address manager;
modifier onlymanager(){
require(msg.sender==manager);
_;
}
function PostcardContract(){
manager=msg.sender;
}
function countCards() public view onlymanager returns(uint){
return cards.length;
}
function sendCard(string username, string content, string ip, string timestamp) public onlymanager {
Postcard memory card=Postcard({
username: username,
content: content,
ip: ip,
timestamp: timestamp
});
cards.push(card);
}
function openCard() public view onlymanager returns(string,string,string,string){
uint index=random()%cards.length;
return (cards[index].username, cards[index].content, cards[index].ip, cards[index].timestamp);
}
function random() private view returns (uint) {
//sha3 is also a global function
//block is global variable that we can access any time
//now is also global
return uint(keccak256(block.difficulty, now, cards.length));
}
}