0
contract testContract {
   address private test1;
   string publictest2;
  }

How do I use foundry framework test cases to read the storage slots? https://book.getfoundry.sh/cheatcodes/load

// This does not work for me
    function test() public {
        vm.startPrank(address(1));

        bytes slot_0= vm.load(address(testContract), bytes32(uint256(0)));
        emit log_address((slot_0)); 
    }
}

1 Answer 1

0

vm.load returns bytes32 and not bytes memory. If you want convert bytes32 to address and log it to the console then you should write:

emit log_address(address(uint160(uint256(slot_0))));

And here is an example test:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import "forge-std/Test.sol";

contract ContractTest is Test {
    TestContract contractT;

    function testAddress() public {
        // Deploy contract
        contractT = new TestContract();

        bytes32 slot_0 = vm.load(address(contractT), bytes32(uint256(0)));
        // Following line will emit address which deployed TestContract instance
        emit log_address(address(uint160(uint256(slot_0))));
    }
}

contract TestContract {
    address private test1;
    string publictest2;

    constructor() {
        test1 = msg.sender;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.