Content pfp
Content
@
0 reply
0 recast
0 reaction

Akuti pfp
Akuti
@akuti
One of the great features of foundry is that you can modify variables to test specific cases in your contracts. With `Std Storage` you easily modify public variables without needing to add a setter method. But you can even read and store directly to storage to interact with private variables.
1 reply
1 recast
6 reactions

Akuti pfp
Akuti
@akuti
Even more powerful is that you can also modify internal variables but it's more complex as it does not work with the simple StdStorage module. You first need to find the storage slot of the variable you want to modify. You can get it easily by using `forge inspect src/ContractName.sol:ContractName storage`.
1 reply
0 recast
0 reaction

Akuti pfp
Akuti
@akuti
Then you can use `vm.load(contractAddress, storageSlot)` to load the data as bytes32, you can then evaulate and potentially modify it and write it back using `vm.store(contractAddress, storageSlot, data)`.
1 reply
0 recast
0 reaction

Akuti pfp
Akuti
@akuti
This is a really powerful feature to test you contract works in certain states or assert the values of private variables. If you handle the pointer references, unpacking and packing you can even use it for complex datatypes like mappings and structs.
1 reply
0 recast
0 reaction

Akuti pfp
Akuti
@akuti
Here is an example of modifying a value in a nested struct in a private mapping variable:
0 reply
0 recast
0 reaction