From 798f40f8540829e1fceeb8928df056c521b4271d Mon Sep 17 00:00:00 2001 From: Thomas Clement Date: Tue, 21 May 2024 17:02:07 -0400 Subject: [PATCH] Add Generic Storage Slot Viewer --- src/FraxTest.sol | 13 ++++++++++++- test/TestSlotDump.t.sol | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/TestSlotDump.t.sol diff --git a/src/FraxTest.sol b/src/FraxTest.sol index 7bd0132..267f6cf 100644 --- a/src/FraxTest.sol +++ b/src/FraxTest.sol @@ -2,8 +2,8 @@ pragma solidity >=0.8.0; import { console2 as console, StdAssertions, StdChains, StdCheats, stdError, StdInvariant, stdJson, stdMath, StdStorage, stdStorage, StdUtils, Vm, StdStyle, TestBase, DSTest, Test } from "forge-std/Test.sol"; - import { VmHelper } from "./VmHelper.sol"; +import { Strings } from "src/StringsHelper.sol"; abstract contract FraxTest is VmHelper, Test { uint256[] internal snapShotIds; @@ -30,5 +30,16 @@ abstract contract FraxTest is VmHelper, Test { } } + function dumpStorageLayout(address target, uint256 slotsToDump) internal view { + console.log("==================================="); + console.log("Storage dump for: ", target); + console.log("==================================="); + for (uint i; i <= slotsToDump; i++) { + bytes32 slot = vm.load(target, bytes32(uint256(i))); + string memory exp = Strings.toHexString(uint256(slot), 32); + console.log("slot", i, ":", exp); + } + } + error VmDidNotRevert(uint256 _snapshotId); } diff --git a/test/TestSlotDump.t.sol b/test/TestSlotDump.t.sol new file mode 100644 index 0000000..e514eaf --- /dev/null +++ b/test/TestSlotDump.t.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: ISC +pragma solidity >=0.8.0; + +import "../src/FraxTest.sol"; + +contract TestSlotDump is FraxTest { + address instance; + + function testDumpSlots() public { + instance = address(new Bravo()); + dumpStorageLayout(instance, 15); + } +} + +// ================== Helpers ============= + +contract Alpha { + address owner = address(0xC0ffee); + address pendingOwner; +} + +contract Bravo is Alpha { + bytes32 someValue = bytes32(type(uint).max); + uint256[5] gap; + bytes32 someOtherValue = 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA; +}