read mapping

// TODO: Read the mapping value without calling ownerOf[tokenId]

contract Question3 {
    mapping(uint256 => address) private ownerOf;

    constructor() {
        ownerOf[0] = address(1);
        ownerOf[1] = address(2);
        ownerOf[2] = address(3);
        ownerOf[3] = address(4);
        ownerOf[4] = address(5);
    }

    function readOwnerOf(uint256 tokenId) external view returns (address) {
        // TODO: Read the mapping value without calling ownerOf[tokenId]
        uint256 slot;
        address ret;

        assembly {
            slot := ownerOf.slot
        }

        bytes32 location = keccak256(abi.encode(tokenId, uint256(slot)));

        assembly {
            ret := sload(location)
        }

        return ret;
    }
}

Last updated