How to find storage slot
1. Using foundry
Foundry's forge & cast functions have a built-in way to get a layout of storage slots.
If working in a foundry project:
forge inspect <CONTRACT_NAME> storage
or, a deployed contract:
cast storage <ADDRESS> [SLOT]
2. Count yourself
You could also count the storage slots manually. You'd have to walk through the versions storage slot layout
More information
However... diving into this, I'm actually not sure how to make viewing storage more gas efficient in assembly/yul than just regular solidity.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract GetWarmStorage {
// 2,327 gas (gas to compare)
uint256 public myNumberOne;
uint256 private constant SLOT = 0;
constructor() {
myNumberOne = 123;
}
// 2,247 gas (-80 gas)
function readStorageNormal() external view returns(uint256) {
return myNumberOne;
}
// 2,332 gas (+5 gas)
function readStorageAssembly() external view returns (uint256) {
assembly {
let result := sload(myNumberOne.slot)
mstore(0, result)
return(0, 0x20)
}
}
// 2,291 gas (-36 gas)
function readStorageAssemblyTwo() external view returns (uint256 result) {
assembly {
result := sload(myNumberOne.slot)
}
}
// 2,310 gas (-17 gas)
function readStorageAssemblyKnowingSlot() external view returns (uint256) {
assembly {
let result := sload(SLOT)
mstore(0, result)
return(0, 0x20)
}
}
// 2,269 gas (-58 gas)
function readStorageAssemblyKnowingSlotTwo() external view returns (uint256 result) {
assembly {
result := sload(SLOT)
}
}
}