forked from Communication-Systems-Group/solidity-examples
/
example4.sol
43 lines (36 loc) · 1009 Bytes
/
example4.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
pragma solidity >0.4.10;
//the very fourth example
contract Example4 {
event Message(
string msg
);
struct Account {
string addr;
uint amount; //default is 256bits
}
uint counter=1;
mapping (uint => Account) accounts;
address public owner;
constructor() public {
owner = msg.sender;
}
function create(string memory addr) public {
accounts[counter++] = Account(addr, 42);
owner = msg.sender;
}
function get(uint nr) public view returns (string memory) {
return accounts[nr].addr;
}
function getAmount(uint nr) public view returns (uint) {
return accounts[nr].amount;
}
function set(uint nr, string memory addr) public returns (bool) {
if(owner == msg.sender) {
accounts[counter++] = Account(addr, nr);
emit Message("all set!"); //raises the event "Message"
return true;
} else {
return false;
}
}
}