-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mapping.sol
52 lines (43 loc) · 1.6 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
47
48
49
50
51
52
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
contract Mapping {
// slotKey = keccak256(key, base_slot key)
mapping (address => uint256) public map;
mapping (address => mapping(address => uint256)) public map2;
mapping (address => uint256[]) private map3;
address public constant ADDR_1 = address(1);
address public constant ADDR_2 = address(2);
address public constant ADDR_3 = address(3);
constructor(){
map[ADDR_1] = 10;
map[ADDR_2] = 20;
map[ADDR_3] = 30;
// map2[key0, key1]
map2[ADDR_1][ADDR_2] = 100;
map2[ADDR_2][ADDR_3] = 200;
map2[ADDR_3][ADDR_1] = 300;
map3[ADDR_1].push(1000);
map3[ADDR_2].push(2000);
map3[ADDR_3].push(3000);
}
function getSimple (address _key) public view returns (uint256 value){
bytes32 slotKey = keccak256(abi.encode(_key, uint256(0)));
assembly {
value := sload(slotKey)
}
}
function getNested (address _keyO, address _key1) public view returns (uint256 value) {
bytes32 slotKey = keccak256(abi.encode(_key1, keccak256(abi.encode(_keyO, uint256(1)))));
assembly {
value := sload(slotKey)
}
}
function getMappingArray (address _key, uint256 _i) public view returns (uint256 length, uint256 value) {
bytes32 mapElementKey = keccak256(abi.encode(_key, uint256(2)));
bytes32 finalElementKey = keccak256(abi.encode(mapElementKey));
assembly {
length := sload(mapElementKey)
value := sload(add(finalElementKey, _i))
}
}
}