-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleStorage.sol
53 lines (45 loc) · 1.88 KB
/
SimpleStorage.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// EVM: Ethereum Virtual Machine
// Avalanche, Fantom, Polygon
// contract is similar to the class in other programming languages
contract SimpleStorage {
// This get initialized to zero!
// <- This means that this section is a comment!
uint256 favoriteNumber = 0;
// bool, string, bytes32
// uint256 public brotherNumber;
// uint256 public sisterNumber;
// People public person = People({favoriteNumber: 2, name: "Jinwen"});
// mapping
mapping(string => uint256) public nameToFavoriteNumber;
// struct
struct People {
uint256 favoriteNumber;
string name;
}
// uint256[] public favoriteNumberList;
// array or list
People[] public people;
function store(uint256 _favoriteNumber) public virtual {
favoriteNumber = _favoriteNumber;
// favoriteNumber = favoriteNumber + 1;
// uint256 testvar = 5;
}
// view, pure——read some from the contract, disallow any modification of state, you can update the blockchain at all with a view function
// read don't cost gas, don't get a hash
// function
function retrieve() public view returns (uint256) {
return favoriteNumber;
}
// EVM can access and store information in six places: Stack, Memory, Storage, Calldata, Code, Logs
// calldata (can't be modified), memory (can be modified), storage (permanent variables)
// Data location can only be specified for array, struct or mapping types
// string (array, need to add this memory)
function addPerson(string memory _name, uint256 _favoriteNumber) public {
// People memory newPerson = People({favoriteNumber: _favoriteNumber, name: _name});
People memory newPerson = People(_favoriteNumber, _name);
people.push(newPerson);
nameToFavoriteNumber[_name] = _favoriteNumber;
}
}