Last active
February 23, 2022 00:28
-
-
Save walteh/b409a99f230845fa8f7a134bf1cf31d4 to your computer and use it in GitHub Desktop.
simple example of how solidity calculates storage refs for structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.8.12; | |
// contains refs to hardhat console.sol and DSTest.sol contract | |
import '../utils/forge.sol'; | |
contract test is t { | |
struct Test { | |
uint256 a; | |
uint256 b; | |
uint256 c; | |
} | |
mapping(uint256 => Test) tmp; | |
function test__structStoragePointer() public { | |
assembly { | |
mstore(0x00, 0x4444) | |
mstore(0x20, tmp.slot) | |
let ptr := keccak256(0x00, 0x40) | |
sstore(add(ptr, 0x00), 0xfff0) | |
sstore(add(ptr, 0x01), 0xfff1) | |
sstore(add(ptr, 0x02), 0xfff2) | |
} | |
console.log(tmp[0x4444].a); | |
console.log(tmp[0x4444].b); | |
console.log(tmp[0x4444].c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment