A demo for test smart contract's dynamic data's storage slot's, such as array, map, using Foundry.
Install Modified Forge
$ git clone https://github.com/0xevm/foundry.git
$ cd foundry
$ git checkout beijing-eth
$ cargo build --release -p foundry-cli --bin forge
# !! update the PATH to your foundry build release path
$ export PATH="/Users/flyq/workspace/github/0xevm/foundry/target/release:$PATH"
# confirm the forge is the modified forge
$ which forge
/Users/flyq/workspace/github/0xevm/foundry/target/release/forge
Building & Testing
$ forge build
$ forge test -vvv
Get the storage slot
$ forge inspect ./src/RecordMapping.sol:RecordMapping storage --pretty
| Name | Type | Slot | Offset | Bytes | Contract |
| ---------- | -------------------------------------------- | ---- | ------ | ----- | ----------------------------------- |
| length | int256 | 0 | 0 | 32 | src/RecordMapping.sol:RecordMapping |
| data | mapping(address => int256) | 1 | 0 | 32 | src/RecordMapping.sol:RecordMapping |
| nestedData | mapping(int256 => mapping(int256 => int256)) | 2 | 0 | 32 | src/RecordMapping.sol:RecordMapping |
Debug to see the stack&memory&storage
$ forge test --debug testRecordMapping
/// Let foundry-evm start recording Storage slot related data.
function startMappingRecording() external;
/// Get the length of a map in a smart contract.
/// @param `target`, the address of smart contract.
/// @param `slot`, the slot of a map or a array.
function getMappingLength(address target, bytes32 slot) external returns (uint);
/// Get the storage slot index of one item of a map
function getMappingSlotAt(address target, bytes32 slot, uint256 idx) external returns (bytes32);
/// Get the map item's key in the slot.
function getMappingKeyOf(address target, bytes32 slot) external returns (uint);
/// Get the slot below which map in a contract
function getMappingParentOf(address target, bytes32 slot) external returns (bytes32);
update the interface name, as they also can used in array.
These smart contracts are being provided as is. No guarantee, representation or warranty is being made, express or implied, as to the safety or correctness of the user interface or the smart contracts. They have not been audited and as such there can be no assurance they will work as intended, and users may experience delays, failures, errors, omissions, loss of transmitted information or loss of funds. The creators are not liable for any of the foregoing. Users should proceed with caution and use at their own risk.
See LICENSE for more details.