سالیدیفا

چطور توی سالیدیتی شماره اسلات یه متغیر رو بدست بیاریم؟

storage

شاید بعضی وقت ها نیاز داشته باشیم که شماره اسلات یه متغیر رو بدونیم. با یه مثال ساده این کار رو انجام می دیم.

// 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);
    }
}
Solidity

و همانطور که انتظار داریم با نتیجه زیر مواجه می شویم:

    • 0:uint256: 3
    • 1:uint256: 0