forked from cryptoleek-team/learn-smartcontracts-yt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mapping.sol
46 lines (34 loc) · 1.15 KB
/
Mapping.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract Mapping {
event LOG(address, uint);
mapping(address => uint) public myMap;
address[] public myMappingAddr;
function setMapping(address _myAddr, uint _number) public {
myMap[_myAddr] = _number;
myMappingAddr.push(_myAddr);
}
function getMapping(address _myAddr) public view returns (uint) {
return myMap[_myAddr];
}
function deleteMapping(address _myAddr) public {
delete myMap[_myAddr];
}
function getTotal() public view returns (uint) {
require(myMappingAddr.length > 0, "The mapping has no values!");
uint sum = 0;
for (uint i = 0; i < myMappingAddr.length; i++) {
address key = myMappingAddr[i];
uint value = myMap[key];
sum += value;
}
return sum;
}
function test () public {
setMapping(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4, 10);
setMapping(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2, 20);
setMapping(0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db, 30);
uint sum = getTotal();
assert(sum == 60);
}
}