Skip to content

Instantly share code, notes, and snippets.

@DonkeVerse
Last active January 11, 2022 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonkeVerse/514d6d47a0f451a17e5d0ab4a2db8e52 to your computer and use it in GitHub Desktop.
Save DonkeVerse/514d6d47a0f451a17e5d0ab4a2db8e52 to your computer and use it in GitHub Desktop.
    function claimTicketOrBlockTransactionV2(uint256 ticketNumber) external {
        require(ticketNumber < arr.length * 256, "bad ticket");
        uint256 storageOffset;
        uint256 offsetWithin256;
        uint256 localGroup;
        uint256 storedBit;
        unchecked {
            storageOffset = ticketNumber / 256;
            offsetWithin256 = ticketNumber % 256;
        }
        
        localGroup = arr[storageOffset];
 
        storedBit = (localGroup >> offsetWithin256) & uint256(1);
        require(storedBit == 1, "already claimed");
        localGroup = localGroup & ~(uint256(1) << offsetWithin256);

        arr[storageOffset] = localGroup;
    }

Gas cost: 26,740
Execution - transaction cost (21,000): 5,740

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment