Is there a way to read private variables of a contract using the cast storage command in Foundry?
I know that even if a variable in a contract is declared private, its value can be read. So I'm trying to read the value using the cast storage command, but it seems to be reading the wrong value. I would like to know the correct way.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SampleContract {
bool public locked = true;
uint256 public ID = block.timestamp;
uint8 private flattening = 10;
uint8 private denomination = 255;
uint16 private awkwardness = uint16(block.timestamp);
bytes32[3] private data;
}
Assuming that there is a contract like the one above,
$ cast storage 0xddC77c1407A0cB33D4652C71F600baB6fa1f84D5 0
0x0000000000000000000000000000000000000000000000000000000000000001
By doing this, the value of the locked variable in Slot 0 can be read, and it was confirmed that the value was read correctly. However, when I tried to read the value of the flattening variable in Slot 2 as follows:
$ cast storage 0xddC77c1407A0cB33D4652C71F600baB6fa1f84D5 2
0x00000000000000000000000000000000000000000000000000000000119cff0a
Completely strange values are read. Since the value of 10 is assigned to the flattening variable in the code, I expect the value to be "0x000000000000000000000000000000000000000000000000000000000000000a," which is the hexadecimal value of decimal 10.
What is wrong?