Skip to content

Instantly share code, notes, and snippets.

@walteh
Last active February 23, 2022 00:28
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 walteh/b409a99f230845fa8f7a134bf1cf31d4 to your computer and use it in GitHub Desktop.
Save walteh/b409a99f230845fa8f7a134bf1cf31d4 to your computer and use it in GitHub Desktop.
simple example of how solidity calculates storage refs for structs
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