// Contract: Ghostlers_Staking
// Target function: function stakesOfOwner(address a) public view returns(Stake[] memory)
ForZeroAddress();

    /**
     * Cannot mint to the zero address.
     */
    error MintToZeroAddress();

    /**
     * The quantity of tokens minted must be more than zero.
     */
    error MintZeroQuantity();

    /**
     * The token does not exist.
     */
    error OwnerQueryForNonexistentToken();

    /**
     * The caller must own the token or be an approved operator.
     */
    error TransferCallerNotOwnerNorApproved();

    /**
     * The token must be owned by `from`.
     */
    error TransferFromIncorrectOwner();

    /**
     * Cannot safely transfer to a contract that does not implement the
     * ERC721Receiver interface.
     */
    error TransferToNonERC721ReceiverImplementer();

    /**
     * Cannot transfer to the zero address.
     */
    error TransferToZeroAddress();

    /**
     * The token does not exist.
     */
    error URIQueryForNonexistentToken();

    /**
     * The `quantity` minted with ERC2309 exceeds the safety limit.
     */
    error MintERC2309QuantityExceedsLimit();

    /**
     * The `extraData` cannot be set on an unintialized ownership slot.
     */
    error OwnershipNotInitializedForExtraData();

    // =============================================================
    //                            STRUCTS
    // =============================================================

    struct TokenOwnership {
        // The address of the owner.
        address addr;
        // Stores the start time of ownership with minimal overhead for tokenomics.
        uint64 startTimestamp;
        // Whether the token has been burned.
        bool burned;
        // Arbitrary data similar to `startTimestamp` that can be set via {_extraData}.
        uint24 extraData;
    }

    // =============================================================
    //                         TOKEN COUNTERS
    // =============================================================

    /**
     * @dev Returns the total number of tokens in existence.
     * Burned tokens will reduce the count.
     * To get the total number of tokens minted, please see {_totalMinted}.
     */
    function totalSupply() external view returns (uint256);

    // =============================================================
    //                            IERC165
    // =============================================================

    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);

    // =============================================================
    //                            IERC721
    // =============================================================

    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables
     * (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in `owner`'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`,
     * checking first that contract recipients are aware of the ERC721 protocol
     * to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be have been allowed to move
     * this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement
     * {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external payable;

    /**
     * @dev Equivalent to `safeTransferFrom(from, to, tokenId, '')`.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external payable;

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom}
     * whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token
     * by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external payable;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the
     * zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external payable;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom}
     * for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}.
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);

    // =============================================================
    //                        IERC721Metadata
    // =============================================================

    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);

    // =============================================================
    //                           IERC2309
    // =============================================================

    /**
     * @dev Emitted when tokens in `fromTokenId` to `toTokenId`
     * (inclusive) is transferred from `from` to `to`, as defined in the
     * [ERC2309](https://eips.ethereum.org/EIPS/eip-2309) standard.
     *
     * See {_mintERC2309} for more details.
     */
    event ConsecutiveTransfer(uint256 indexed fromTokenId, uint256 toTokenId, address indexed from, address indexed to);
}
interface IERC721AQueryable is IERC721A {
    /**
     * Invalid query range (`start` >= `stop`).
     */
    error InvalidQueryRange();

    /**
     * @dev Returns the `TokenOwnership` struct at `tokenId` without reverting.
     *
     * If the `tokenId` is out of bounds:
     *
     * - `addr = address(0)`
     * - `startTimestamp = 0`
     * - `burned = false`
     * - `extraData = 0`
     *
     * If the `tokenId` is burned:
     *
     * - `addr = <Address of owner before token was burned>`
     * - `startTimestamp = <Timestamp when token was burned>`
     * - `burned = true`
     * - `extraData = <Extra data when token was burned>`
     *
     * Otherwise:
     *
     * - `addr = <Address of owner>`
     * - `startTimestamp = <Timestamp of start of ownership>`
     * - `burned = false`
     * - `extraData = <Extra data at start of ownership>`
     */
    function explicitOwnershipOf(uint256 tokenId) external view returns (TokenOwnership memory);

    /**
     * @dev Returns an array of `TokenOwnership` structs at `tokenIds` in order.
     * See {ERC721AQueryable-explicitOwnershipOf}
     */
    function explicitOwnershipsOf(uint256[] memory tokenIds) external view returns (TokenOwnership[] memory);

    /**
     * @dev Returns an array of token IDs owned by `owner`,
     * in the range [`start`, `stop`)
     * (i.e. `start <= tokenId < stop`).
     *
     * This function allows for tokens to be queried if the collection
     * grows too big for a single call of {ERC721AQueryable-tokensOfOwner}.
     *
     * Requirements:
     *
     * - `start < stop`
     */
    function tokensOfOwnerIn(
        address owner,
        uint256 start,
        uint256 stop
    ) external view returns (uint256[] memory);

    /**
     * @dev Returns an array of token IDs owned by `owner`.
     *
     * This function scans the ownership mapping and is O(`totalSupply`) in complexity.
     * It is meant to be called off-chain.
     *
     * See {ERC721AQueryable-tokensOfOwnerIn} for splitting the scan into
     * multiple smaller scans if the collection is large enough to cause
     * an out-of-gas error (10K collections should be fine).
     */
    function tokensOfOwner(address owner) external view returns (uint256[] memory);
}
contract Ghostlers_Staking is Ownable{
    
    mapping(uint => mapping(string => uint)) public reward;
    function setReward(uint locking_months, string memory level, uint BOOCOINS_in_wei) public onlyOwner{
        require(locking_months == 1 || locking_months == 3 || locking_months == 6 || locking_months == 12, "Invalid months given.");
        reward[locking_months][level] = BOOCOINS_in_wei;
    }

    mapping(string => bytes32) public merkleRoot;
    function setMerkleRoot(string memory level, bytes32 m) public onlyOwner{
        require(keccak256(bytes(level)) == keccak256(bytes("Basic")) || 
                keccak256(bytes(level)) == keccak256(bytes("Ordinary")) || 
                keccak256(bytes(level)) == keccak256(bytes("ExtraOrdinary")) || 
                keccak256(bytes(level)) == keccak256(bytes("Legendary")) || 
                keccak256(bytes(level)) == keccak256(bytes("Rare")) || 
                keccak256(bytes(level)) == keccak256(bytes("SuperRare")), "Invalid level given.");
        merkleRoot[level] = m;
    }

    address public BOOCOIN_Address = 0xcA252612baE035b6D49c8FD8908B87723084fbC0;
    function set_BOOCOIN_Address(address a) public onlyOwner{
        BOOCOIN_Address = a;
    }
    
    address public GHOSTLERS_NFT = 0xdbE15A1DA3A58f96dE9C64233744F58F19adA1cE;
    function setGHOSTLERS_NFT(address a) public onlyOwner{
        GHOSTLERS_NFT = a;
    }

    uint public thirty_day_constant = 2592000;
    function set_thirty_day_constant(uint t) public onlyOwner {
        thirty_day_constant = t;
    }

    struct Stake {
        uint id;
        address address_;
        uint staked_time;
        uint locking_months;
        string level;
        bool staked;
    }
    
    mapping(uint => Stake) public STAKES;
    mapping(address => uint) public _stakeBalanceOfUser;

////////////////////////////////////////////////////////////////////////////////////////

    function stake(uint256[] memory ids, bytes32[][] calldata merkleproof, uint[] memory locking_months, string[] memory levels) public {
                
        for(uint i=0 ; i<ids.length; i++) {
            require(locking_months[i] == 1 || locking_months[i] == 3 || locking_months[i] == 6 || locking_months[i] == 12, "Invalid months given.");
            require(keccak256(bytes(levels[i])) == keccak256(bytes("Basic")) || 
                    keccak256(bytes(levels[i])) == keccak256(bytes("Ordinary")) || 
                    keccak256(bytes(levels[i])) == keccak256(bytes("ExtraOrdinary")) || 
                    keccak256(bytes(levels[i])) == keccak256(bytes("Legendary")) || 
                    keccak256(bytes(levels[i])) == keccak256(bytes("Rare")) || 
                    keccak256(bytes(levels[i])) == keccak256(bytes("SuperRare")), "Invalid level given.");

            string memory indexNum = Strings.toString(ids[i]);
            require(MerkleProof.verify( merkleproof[i], merkleRoot[levels[i]], keccak256(abi.encodePacked(indexNum))), "Invalid level for Id!!");
            require(IERC721A(GHOSTLERS_NFT).ownerOf(ids[i]) == msg.sender, "Invalid! id not found in user wallet!");
            require(STAKES[ids[i]].staked == false, "ID is alrady Staked!");
        
            IERC721A(GHOSTLERS_NFT).transferFrom(msg.sender, address(this), ids[i]);
        
            STAKES[ids[i]].id = ids[i];
            STAKES[ids[i]].address_ = msg.sender;
            STAKES[ids[i]].staked_time = block.timestamp;
            STAKES[ids[i]].locking_months = locking_months[i];
            STAKES[ids[i]].level = levels[i];
            STAKES[ids[i]].staked = true;

            _stakeBalanceOfUser[msg.sender] += 1;
        }
    }

    function claim_and_unstake(uint id) public {
        Stake memory s = STAKES[id];
        
        require(s.address_ == msg.sender, "Invalid! address does not match user!");
        require(s.staked == true, "ID is not Staked!");
        require(block.timestamp >= s.staked_time + (s.locking_months * thirty_day_constant), "Staking duration incomplete!");

        IERC20(BOOCOIN_Address).stakeReward(msg.sender, reward[s.locking_months][s.level]);
        IERC721A(GHOSTLERS_NFT).transferFrom(address(this), msg.sender, id);

        _stakeBalanceOfUser[msg.sender] -= 1;
        delete STAKES[id];

        _total_reward += reward[s.locking_months][s.level];
        _total_earnings_of_owner[msg.sender] += reward[s.locking_months][s.level];
    }

    function batch_claim_and_unstake(uint[] memory ids) public {
        uint total = 0;

        for(uint i=0 ; i<ids.length ; i++) {        
            require(STAKES[ids[i]].address_ == msg.sender, "Invalid! address does not match user!");
            require(STAKES[ids[i]].staked == true, "ID is not Staked!");
            require(block.timestamp >= STAKES[ids[i]].staked_time + (STAKES[ids[i]].locking_months * thirty_day_constant), "Staking duration incomplete!");

            IERC721A(GHOSTLERS_NFT).transferFrom(address(this), msg.sender, STAKES[ids[i]].id);

            total += reward[STAKES[ids[i]].locking_months][STAKES[ids[i]].level];
            
            STAKES[ids[i]].staked = false;
            delete STAKES[ids[i]];
        }
        IERC20(BOOCOIN_Address).stakeReward(msg.sender, total);
        _stakeBalanceOfUser[msg.sender] -= ids.length;

        _total_reward += total;
        _total_earnings_of_owner[msg.sender] += total;
    }

    function emergency_unstake(uint[]memory ids) public {
        uint total = 0;
        
        for(uint i=0 ; i<ids.length ; i++) {        
            require(STAKES[ids[i]].address_ == msg.sender, "Invalid! address does not match user!");
            require(STAKES[ids[i]].staked == true, "ID is not Staked!");

            IERC721A(GHOSTLERS_NFT).transferFrom(address(this), msg.sender, STAKES[ids[i]].id);

            total += get_available_reward(STAKES[ids[i]].id);
            
            STAKES[ids[i]].staked = false;
            delete STAKES[ids[i]];
        }
        IERC20(BOOCOIN_Address).stakeReward(msg.sender, total / 2);
        _stakeBalanceOfUser[msg.sender] -= ids.length;

        _total_reward += total/2;
        _total_earnings_of_owner[msg.sender] += total/2;
    }