• Calculates the storage slot key for a Solidity mapping given its slot number and key. Implements the storage layout algorithm described in the Solidity documentation. See: https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html

    Parameters

    • mappingSlot: bigint

      The storage slot where the mapping is declared (position in contract storage)

    • key: `0x${string}`

      The mapping key (can be an address or number in hex format)

    Returns `0x${string}`

    The keccak256 hash of the concatenated and padded slot and key

    // For an address key
    calculateMappingStorageKey(0n, "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199");

    // For a number key
    calculateMappingStorageKey(2n, "0x1"); // For mapping at slot 2 with key 1

    For mappings in Solidity like mapping(address => uint256), the storage slot of a value is calculated by keccak256(abi.encode(key, uint256(slot)))