-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMappingLayout.sol
More file actions
29 lines (24 loc) · 833 Bytes
/
Copy pathMappingLayout.sol
File metadata and controls
29 lines (24 loc) · 833 Bytes
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MappingLayout {
// State variables
uint256 public totalSupply = 1_000_000e18; // slot 0
mapping(address => uint256) balanceOf; // slot 1
mapping(uint256 => uint256) somethingElse; // slot 2
function setBalance(address _user, uint256 _amount) external {
balanceOf[_user] = _amount;
somethingElse[_amount] = _amount * 5;
}
function fetchMappingLocation(
uint256 mappingKey,
uint256 mappingStorageSlot
) external pure returns (uint256) {
return uint256(keccak256(abi.encode(mappingKey, mappingStorageSlot)));
}
// Read from storage slots
function getValFromSlot(uint256 slot) external view returns (bytes32 val) {
assembly {
val := sload(slot)
}
}
}