Std Storage
Std Storage is a library that makes manipulating storage easy.
To use Std Storage, import the following in your test contract:
import {stdStorage, StdStorage} from "forge-std/Test.sol";
Add the following line in your test contract:
using stdStorage for StdStorage;
Then, access Std Storage via the stdstore
instance.
Functions
Query functions:
target
: Set the address of the target contractsig
: Set the 4-byte selector of the function to static callwith_key
: Pass an argument to the function (can be used multiple times)depth
: Set the position of the value in thetuple
(e.g. inside astruct
)
Terminator functions:
find
: Return the slot numberchecked_write
: Set the data to be written to the storage slot(s)read_<type>
: Read the value from the storage slot as<type>
Example
playerToCharacter
tracks info about players’ characters.
// MetaRPG.sol
struct Character {
string name;
uint256 level;
}
mapping (address => Character) public playerToCharacter;
Let’s say we want to set the level of our character to 120.
// MetaRPG.t.sol
stdstore
.target(address(metaRpg))
.sig("playerToCharacter(address)")
.with_key(address(this))
.depth(1)
.checked_write(120);
Limitations
- Accessing packed slots is not supported
Known issues
- Slot(s) may not be found if the
tuple
contains types shorter than 32 bytes