In some cases, you may want to know the slot number of a variable in Solidity. Here is a very simple sample to show that.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;
contract SlotLocation{
uint256 private fourth;
uint256 private third;
uint256 private second;
uint256 private first;
function storageLocation() external pure returns(uint256,uint256) {
uint256 first_slotLocation;
uint256 fourth_slotLocation;
assembly {
first_slotLocation := first.slot
fourth_slotLocation := fourth.slot
}
return (first_slotLocation,fourth_slotLocation);
}
}
SolidityAs we expect, the result is:
- 0:uint256: 3
- 1:uint256: 0