Skip to content

Instantly share code, notes, and snippets.

@krogla
Created February 7, 2023 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krogla/3e9358fdea558f0123b6644da2e13980 to your computer and use it in GitHub Desktop.
Save krogla/3e9358fdea558f0123b6644da2e13980 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
// solhint-disable-next-line lido/fixed-compiler-version
pragma solidity >=0.4.24 <0.9.0;
library UnstructuredStorageMap {
function getStorageMappingUint256(bytes32 position, uint256 key) internal view returns (uint256 data) {
return getStorageMappingUint256Offset(position, key, 0);
}
function getStorageMappingUint256Offset(bytes32 position, uint256 key, uint256 offset)
internal
view
returns (uint256 data)
{
assembly {
mstore(0, key)
mstore(32, position)
let hash := add(keccak256(0, 64), offset)
data := sload(hash)
}
}
function setStorageMappingUint256(bytes32 position, uint256 key, uint256 data) internal {
setStorageMappingUint256Offset(position, key, 0, data);
}
function setStorageMappingUint256Offset(bytes32 position, uint256 key, uint256 offset, uint256 data) internal {
assembly {
mstore(0, key)
mstore(32, position)
let hash := add(keccak256(0, 64), offset)
sstore(hash, data)
}
}
}
@krogla
Copy link
Author

krogla commented Feb 7, 2023

used for mapping(uint256 => uint256) or mapping(uint256 => struct)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment