contract DeafGold is Context, Ownable, ERC20 {
    // Deaf contract is available at https://etherscan.io/address/0xff9c1b15b16263c61d017ee9f65c50e4ae0113d7
    address public DeafContractAddress =
        0xc9Cb0FEe73f060Db66D2693D92d75c825B1afdbF;
    IERC721Enumerable public DeafContract;

    // Give out 10,000 Deaf Gold for every Deaf Bag that a user holds
    uint256 public DeafGoldPerTokenId = 10000 * (10**decimals());

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

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

    // 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("Deaf Gold", "DGLD") {
        // Transfer ownership to the Deaf DAO
        // Ownable by OpenZeppelin automatically sets owner to msg.sender, but
        // we're going to be using a separate wallet for deployment
        transferOwnership(_msgSender());
        DeafContract = IERC721Enumerable(DeafContractAddress);
    }

    /// @notice Claim Deaf Gold for a given Deaf ID
    /// @param tokenId The tokenId of the Deaf 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() == DeafContract.ownerOf(tokenId),
            "MUST_OWN_TOKEN_ID"
        );

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

    /// @notice Claim Deaf Gold for all tokens owned by the sender
    /// @notice This function will run out of gas if you have too much Deaf! If
    /// this is a concern, you should use claimRangeForOwner and claim Deaf
    /// Gold in batches.
    function claimAllForOwner() external {
        uint256 tokenBalanceOwner = DeafContract.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(
                DeafContract.tokenOfOwnerByIndex(_msgSender(), i),
                _msgSender()
            );
        }
    }

    /// @notice Claim Deaf Gold for all tokens owned by the sender within a
    /// given range
    /// @notice This function is useful if you own too much Deaf to claim all at
    /// once or if you want to leave some Deaf unclaimed. If you leave Deaf
    /// unclaimed, however, you cannot claim it once the next season starts.
    function claimRangeForOwner(uint256 ownerIndexStart, uint256 ownerIndexEnd)
        external
    {
        uint256 tokenBalanceOwner = DeafContract.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(
                DeafContract.tokenOfOwnerByIndex(_msgSender(), i),
                _msgSender()
            );
        }
    }

    /// @dev Internal function to mint Deaf 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 Deaf Gold have not already been claimed this season
        // for a given tokenId
        require(
            !seasonClaimedByTokenId[season][tokenId],
            "GOLD_CLAIMED_FOR_TOKEN_ID"
        );

        // Effects

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

        // Interactions

        // Send Deaf Gold to the owner of the token ID
        _mint(tokenOwner, DeafGoldPerTokenId);
    }

    /// @notice Allows the DAO to mint new tokens for use within the Deaf
    /// Ecosystem
    /// @param amountDisplayValue The amount of Deaf to mint. This should be
    /// input as the display value, not in raw decimals. If you want to mint
    /// 100 Deaf, 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 Deaf. This is
    /// relevant in the event that Deaf migrates to a new contract.
    /// @param DeafContractAddress_ The new contract address for Deaf
    function daoSetDeafContractAddress(address DeafContractAddress_)
        external
        onlyOwner
    {
        DeafContractAddress = DeafContractAddress_;
        DeafContract = IERC721Enumerable(DeafContractAddress);
    }

    /// @notice Allows the DAO to set the token IDs that are eligible to claim
    /// Deaf
    /// @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 Deaf contract has a different
    /// total supply of Deaf
    function daoSetTokenIdRange(uint256 tokenIdStart_, uint256 tokenIdEnd_)
        external
        onlyOwner
    {
        tokenIdStart = tokenIdStart_;
        tokenIdEnd = tokenIdEnd_;
    }

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

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

    /// @notice Allows the DAO to set the season and Deaf Gold per token ID
    /// in one transaction. This ensures that there is not a gap where a user
    /// can claim more Deaf Gold than others
    /// @param season_ The season to use for claiming Deaf
    /// @param DeafGoldDisplayValue The amount of Deaf a user can claim.
    /// This should be input as the display value, not in raw decimals. If you
    /// want to mint 100 Deaf, 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
    /// DeafGold 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 daoSetSeasonAndDeafGoldPerTokenID(
        uint256 season_,
        uint256 DeafGoldDisplayValue
    ) external onlyOwner {
        daoSetSeason(season_);
        daoSetDeafGoldPerTokenId(DeafGoldDisplayValue);
    }
}
function daoMint(uint256 amountDisplayValue) external onlyOwner {
        _mint(owner(), amountDisplayValue * (10**decimals()));
    }
/// @notice Allows the DAO to mint new tokens for use within the Deaf
/// Ecosystem
/// @param amountDisplayValue The amount of Deaf to mint. This should be
/// input as the display value, not in raw decimals. If you want to mint
/// 100 Deaf, you should enter "100" rather than the value of 100 * 10^18.