-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedMappingToDynamicArray.sol
37 lines (32 loc) · 1.13 KB
/
NestedMappingToDynamicArray.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract NestedMappingToDynamicArray {
// State variables
uint256 public totalSupply = 1_000_000e18; // slot 0
mapping(address => uint256[]) userNFTHoldings; // slot 1
function set(address _user, uint256[] calldata _amount) external {
userNFTHoldings[_user] = _amount;
}
function fetchNestedMappingData(
address key1,
uint256 arrayElementIndex
) external view returns (uint256 arrayElementLocation, bytes32 storedVal) {
uint256 mappingSlotInStorage;
assembly {
mappingSlotInStorage := userNFTHoldings.slot
}
uint256 dynamicArrayStorageSlot = uint256(
keccak256(abi.encode(key1, mappingSlotInStorage))
);
arrayElementLocation = uint256(
keccak256(abi.encode(dynamicArrayStorageSlot))
);
storedVal = getValFromSlot(arrayElementLocation + arrayElementIndex);
}
// Read from storage slots
function getValFromSlot(uint256 slot) public view returns (bytes32 val) {
assembly {
val := sload(slot)
}
}
}