contract NeuroCredit is Context, Ownable, ERC20 {
    // gear contract is available at https://etherscan.io/address/0xff796cbbe32b2150a4585a3791cadb213d0f35a3
    address public gearContractAddress =
        0xFf796cbbe32B2150A4585a3791CADb213D0F35A3;
    IERC721Enumerable public gearContract;

    // Give out 25,000 Neuro Credit for every gear Bag that a user holds
    uint256 public NeuroCreditPerTokenId = 25000 * (10**decimals());

    // tokenIdStart of 1 is based on the following lines in the gear contract:
    /** 
    function claim(uint256 tokenId) public nonReentrant {
        require(tokenId > 0 && tokenId < 7778, "Token ID invalid");
        _safeMint(_msgSender(), tokenId);
    }
    */
    uint256 public tokenIdStart = 1;

    // tokenIdEnd of 7777 is based on the following lines in the gear contract:
    /**
        function ownerClaim(uint256 tokenId) public nonReentrant onlyOwner {
        require(tokenId > 7777 && tokenId < 8001, "Token ID invalid");
        _safeMint(owner(), tokenId);
    }
    */
    uint256 public tokenIdEnd = 7777;

    // Seasons are used to allow users to claim tokens regularly. Seasons are
    // decided by the DAO.
    uint256 public season = 0;

    // Track claimed tokens within a season
    // IMPORTANT: The format of the mapping is:
    // claimedForSeason[season][tokenId][claimed]
    mapping(uint256 => mapping(uint256 => bool)) public seasonClaimedByTokenId;

    constructor() Ownable() ERC20("Neuro Credits", "NEURO") {
        // Transfer ownership to the gear DAO
        // Ownable by OpenZeppelin automatically sets owner to msg.sender, but
        // we're going to be using a separate wallet for deployment
        transferOwnership(_msgSender());
        gearContract = IERC721Enumerable(gearContractAddress);
    }

    /// @notice Claim Neuro Credits for a given gear ID
    /// @param tokenId The tokenId of the gear NFT
    function claimById(uint256 tokenId) external {
        // Follow the Checks-Effects-Interactions pattern to prevent reentrancy
        // attacks

        // Checks

        // Check that the msgSender owns the token that is being claimed
        require(
            _msgSender() == gearContract.ownerOf(tokenId),
            "MUST_OWN_TOKEN_ID"
        );

        // Further Checks, Effects, and Interactions are contained within the
        // _claim() function
        _claim(tokenId, _msgSender());
    }

    /// @notice Claim Neuro Credits for all tokens owned by the sender
    /// @notice This function will run out of gas if you have too much gear! If
    /// this is a concern, you should use claimRangeForOwner and claim Neuro
    /// Credits in batches.
    function claimAllForOwner() external {
        uint256 tokenBalanceOwner = gearContract.balanceOf(_msgSender());

        // Checks
        require(tokenBalanceOwner > 0, "NO_TOKENS_OWNED");

        // i < tokenBalanceOwner because tokenBalanceOwner is 1-indexed
        for (uint256 i = 0; i < tokenBalanceOwner; i++) {
            // Further Checks, Effects, and Interactions are contained within
            // the _claim() function
            _claim(
                gearContract.tokenOfOwnerByIndex(_msgSender(), i),
                _msgSender()
            );
        }
    }

    /// @notice Claim Neuro Credits for all tokens owned by the sender within a
    /// given range
    /// @notice This function is useful if you own too much gear to claim all at
    /// once or if you want to leave some gear unclaimed. If you leave gear
    /// unclaimed, however, you cannot claim it once the next season starts.
    function claimRangeForOwner(uint256 ownerIndexStart, uint256 ownerIndexEnd)
        external
    {
        uint256 tokenBalanceOwner = gearContract.balanceOf(_msgSender());

        // Checks
        require(tokenBalanceOwner > 0, "NO_TOKENS_OWNED");

        // We use < for ownerIndexEnd and tokenBalanceOwner because
        // tokenOfOwnerByIndex is 0-indexed while the token balance is 1-indexed
        require(
            ownerIndexStart >= 0 && ownerIndexEnd < tokenBalanceOwner,
            "INDEX_OUT_OF_RANGE"
        );

        // i <= ownerIndexEnd because ownerIndexEnd is 0-indexed
        for (uint256 i = ownerIndexStart; i <= ownerIndexEnd; i++) {
            // Further Checks, Effects, and Interactions are contained within
            // the _claim() function
            _claim(
                gearContract.tokenOfOwnerByIndex(_msgSender(), i),
                _msgSender()
            );
        }
    }

    /// @dev Internal function to mint gear upon claiming
    function _claim(uint256 tokenId, address tokenOwner) internal {
        // Checks
        // Check that the token ID is in range
        // We use >= and <= to here because all of the token IDs are 0-indexed
        require(
            tokenId >= tokenIdStart && tokenId <= tokenIdEnd,
            "TOKEN_ID_OUT_OF_RANGE"
        );

        // Check that Neuro Credits have not already been claimed this season
        // for a given tokenId
        require(
            !seasonClaimedByTokenId[season][tokenId],
            "CREDIT_CLAIMED_FOR_TOKEN_ID"
        );

        // Effects

        // Mark that Neuro Credits has been claimed for this season for the
        // given tokenId
        seasonClaimedByTokenId[season][tokenId] = true;

        // Interactions

        // Send Neuro Credits to the owner of the token ID
        _mint(tokenOwner, NeuroCreditPerTokenId);
    }

    /// @notice Allows the DAO to mint new tokens for use within the gear
    /// Ecosystem
    /// @param amountDisplayValue The amount of gear to mint. This should be
    /// input as the display value, not in raw decimals. If you want to mint
    /// 100 gear, you should enter "100" rather than the value of 100 * 10^18.
    function daoMint(uint256 amountDisplayValue) external onlyOwner {
        _mint(owner(), amountDisplayValue * (10**decimals()));
    }

    /// @notice Allows the DAO to set a new contract address for gear. This is
    /// relevant in the event that gear migrates to a new contract.
    /// @param gearContractAddress_ The new contract address for gear
    function daoSetgearContractAddress(address gearContractAddress_)
        external
        onlyOwner
    {
        gearContractAddress = gearContractAddress_;
        gearContract = IERC721Enumerable(gearContractAddress);
    }

    /// @notice Allows the DAO to set the token IDs that are eligible to claim
    /// gear
    /// @param tokenIdStart_ The start of the eligible token range
    /// @param tokenIdEnd_ The end of the eligible token range
    /// @dev This is relevant in case a future gear contract has a different
    /// total supply of gear
    function daoSetTokenIdRange(uint256 tokenIdStart_, uint256 tokenIdEnd_)
        external
        onlyOwner
    {
        tokenIdStart = tokenIdStart_;
        tokenIdEnd = tokenIdEnd_;
    }

    /// @notice Allows the DAO to set a season for new Neuro Credits claims
    /// @param season_ The season to use for claiming gear
    function daoSetSeason(uint256 season_) public onlyOwner {
        season = season_;
    }

    /// @notice Allows the DAO to set the amount of Neuro Credits that is
    /// claimed per token ID
    /// @param NeuroCreditDisplayValue The amount of gear a user can claim.
    /// This should be input as the display value, not in raw decimals. If you
    /// want to mint 100 gear, you should enter "100" rather than the value of
    /// 100 * 10^18.
    function daoSetNeuroCreditPerTokenId(uint256 NeuroCreditDisplayValue)
        public
        onlyOwner
    {
        NeuroCreditPerTokenId = NeuroCreditDisplayValue * (10**decimals());
    }

    /// @notice Allows the DAO to set the season and Neuro Credits per token ID
    /// in one transaction. This ensures that there is not a gap where a user
    /// can claim more Neuro Credits than others
    /// @param season_ The season to use for claiming gear
    /// @param NeuroCreditDisplayValue The amount of gear a user can claim.
    /// This should be input as the display value, not in raw decimals. If you
    /// want to mint 100 gear, you should enter "100" rather than the value of
    /// 100 * 10^18.
    /// @dev We would save a tiny amount of gas by modifying the season and
    /// NeuroCredit variables directly. It is better practice for security,
    /// however, to avoid repeating code. This function is so rarely used that
    /// it's not worth moving these values into their own internal function to
    /// skip the gas used on the modifier check.
    function daoSetSeasonAndNeuroCreditPerTokenID(
        uint256 season_,
        uint256 NeuroCreditDisplayValue
    ) external onlyOwner {
        daoSetSeason(season_);
        daoSetNeuroCreditPerTokenId(NeuroCreditDisplayValue);
    }
}
/// @title Neuro Credits for gear holders!
/// Original author: Will Papper <https://twitter.com/WillPapper>
/// @notice This contract mints Neuro Credits for gear holders and provides
/// administrative functions to the gear DAO. It allows:
/// * gear holders to claim Neuro Credits
/// * A DAO to set seasons for new opportunities to claim Neuro Credits
/// * A DAO to mint Neuro Credits for use within the gear ecosystem
/// @custom:unaudited This contract has not been audited. Use at your own risk.
function daoMint(uint256 amountDisplayValue) external onlyOwner {
        _mint(owner(), amountDisplayValue * (10**decimals()));
    }
/// @notice Allows the DAO to mint new tokens for use within the gear
/// Ecosystem
/// @param amountDisplayValue The amount of gear to mint. This should be
/// input as the display value, not in raw decimals. If you want to mint
/// 100 gear, you should enter "100" rather than the value of 100 * 10^18.