// File: contracts/task-manager/TaskManager.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/interfaces/IERC721.sol";
import "@openzeppelin/contracts/interfaces/IERC1155.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "./abstract/task-manager/AdminTaskManaged.sol";
import "./abstract/task-manager/CreatorRoleEnabled.sol";
import "./abstract/task-manager/ApproverRoleEnabled.sol";
import "./abstract/task-manager/ExecutorRoleEnabled.sol";
import "./abstract/task-manager/FinalizerRoleEnabled.sol";
import "./abstract/task-manager/ETHVaultEnabled.sol";
import "./abstract/task-manager/ERC20VaultEnabled.sol";
import "./abstract/task-manager/ERC721VaultEnabled.sol";
import "./abstract/task-manager/ERC1155VaultEnabled.sol";
import "./ITaskExecutor.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
contract TaskManager is
  ITaskExecutor,
  AdminTaskManaged,
  CreatorRoleEnabled,
  ApproverRoleEnabled,
  ExecutorRoleEnabled,
  FinalizerRoleEnabled,
  ETHVaultEnabled,
  ERC20VaultEnabled,
  ERC721VaultEnabled,
  ERC1155VaultEnabled,
  ERC165
{
    modifier onlyPrivileged() {
        require(
            _isAdmin(msg.sender) ||
            _isCreator(msg.sender) ||
            _isApprover(msg.sender) ||
            _isExecutor(msg.sender),
            "TaskManager: not a privileged account"
        );
        _;
    }

    constructor(
      address[] memory initialAdmins,
      address[] memory initialCreators,
      address[] memory initialApprovers,
      address[] memory initialExecutors,
      bool enableDeposit
    ) {
        require(initialAdmins.length >= MIN_NR_OF_ADMINS, "TaskManager: not enough initial admins");
        for (uint i = 0; i < initialAdmins.length; i++) {
            _addAdmin(initialAdmins[i]);
        }
        require(initialCreators.length >= 1, "TaskManager: not enough initial creators");
        for (uint i = 0; i < initialCreators.length; i++) {
            _addCreator(initialCreators[i]);
        }
        require(initialApprovers.length >= 3, "TaskManager: not enough initial approvers");
        for (uint i = 0; i < initialApprovers.length; i++) {
            _addApprover(initialApprovers[i]);
        }
        require(initialExecutors.length >= 1, "TaskManager: not enough initial executors");
        for (uint i = 0; i < initialExecutors.length; i++) {
            _addExecutor(initialExecutors[i]);
        }
        _setEnableDeposit(enableDeposit);
    }

    function supportsInterface(bytes4 interfaceId)
      public view virtual override(ERC1155Vault, ERC165) returns (bool)
    {
        return interfaceId == type(ITaskExecutor).interfaceId ||
               interfaceId == type(IERC1155Receiver).interfaceId ||
               interfaceId == type(IERC721Receiver).interfaceId;
    }

    function stats() external view onlyAdmin returns (uint, uint, uint, uint, uint) {
        return (_nrOfAdmins, _nrOfCreators, _nrOfApprovers, _nrOfExecutors, _nrOfFinalizers);
    }

    function isFinalized(uint256 taskId) external view
      onlyPrivileged
      taskMustExist(taskId)
      returns (bool)
    {
        if (_isTaskAdministrative(taskId)) {
            require(_isAdmin(msg.sender), "TaskManager: not an admin account");
        }
        return _isTaskFinalized(taskId);
    }

    function getTaskURI(uint256 taskId) external view
      onlyPrivileged
      taskMustExist(taskId)
      returns (string memory)
    {
        if (_isTaskAdministrative(taskId)) {
            require(_isAdmin(msg.sender), "TaskManager: not an admin account");
        }
        return _getTaskURI(taskId);
    }

    function setTaskURI(uint256 taskId, string memory uri) external
      onlyPrivileged
      taskMustExist(taskId)
    {
        if (_isTaskAdministrative(taskId)) {
            require(_isAdmin(msg.sender), "TaskManager: not an admin account");
        }
        _setTaskURI(taskId, uri);
    }

    function isTaskAdministrative(uint256 taskId) external view
      onlyPrivileged
      taskMustExist(taskId)
      returns (bool)
    {
        return _isTaskAdministrative(taskId);
    }

    function getNrOfTasks() external view
      onlyPrivileged
      returns (uint256)
    {
        return _getNrOfTasks();
    }


    function getNrOfApprovals(uint256 taskId) external view
      onlyPrivileged
      taskMustExist(taskId)
      returns (uint)
    {
        if (_isTaskAdministrative(taskId)) {
            require(_isAdmin(msg.sender), "TaskManager: not an admin account");
        }
        return _getTaskNrApprovals(taskId);
    }

    function createTask(string memory taskURI) external
      onlyCreator
    {
        _createTask(taskURI, false);
    }

    function finalizeTask(uint256 taskId, string memory reason) external
      onlyCreatorOrAdmin
      taskMustExist(taskId)
      taskMustNotBeAdministrative(taskId)
      taskMustNotBeFinalized(taskId)
    {
        _finalizeTask(taskId, reason);
    }

    function approveTask(uint256 taskId) external
      onlyApprover
      taskMustExist(taskId)
      taskMustNotBeAdministrative(taskId)
      taskMustNotBeFinalized(taskId)
    {
        _approveTask(msg.sender, taskId);
    }

    function withdrawTaskApproval(uint256 taskId) external
      onlyApprover
      taskMustExist(taskId)
      taskMustNotBeAdministrative(taskId)
      taskMustNotBeFinalized(taskId)
    {
        _withdrawTaskApproval(msg.sender, taskId);
    }

    function executeTask(address origin, uint256 taskId) external virtual override
      onlyFinalizer
      mustBeExecutor(origin)
      taskMustExist(taskId)
      taskMustNotBeAdministrative(taskId)
      taskMustBeApproved(taskId)
      taskMustNotBeFinalized(taskId)
    {
        _finalizeTask(taskId, "");
        emit TaskExecuted(msg.sender, origin, taskId);
    }

    function executeAdminTask(address origin, uint256 taskId) external virtual override
      onlyFinalizer
      mustBeExecutor(origin)
      taskMustExist(taskId)
      taskMustBeAdministrative(taskId)
      taskMustBeApproved(taskId)
      taskMustNotBeFinalized(taskId)
    {
        _finalizeTask(taskId, "");
        emit TaskExecuted(msg.sender, origin, taskId);
    }

    function _getRequiredNrApprovals(uint256 taskId)
      internal view virtual override(AdminTaskManaged, TaskManaged) returns (uint) {
        require(_taskExists(taskId), "TaskManager: task does not exist");
        if (_isTaskAdministrative(taskId)) {
            return (1 + _nrOfAdmins / 2);
        } else {
            return (1 + _nrOfApprovers / 2);
        }
    }

    receive() external payable {
        require(_isDepositEnabled(), "TaskManager: cannot accept ether");
    }

    fallback() external payable {
        revert("TaskManager: fallback always fails");
    }
}

// File: @openzeppelin/contracts/interfaces/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC20.sol)

pragma solidity ^0.8.0;

import "../token/ERC20/IERC20.sol";

// File: @openzeppelin/contracts/interfaces/IERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC721.sol)

pragma solidity ^0.8.0;

import "../token/ERC721/IERC721.sol";

// File: @openzeppelin/contracts/interfaces/IERC1155.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC1155.sol)

pragma solidity ^0.8.0;

import "../token/ERC1155/IERC1155.sol";

// File: @openzeppelin/contracts/utils/introspection/ERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)

pragma solidity ^0.8.0;

import "./IERC165.sol";

/**
 * @dev Implementation of the {IERC165} interface.
 *
 * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
 * for the additional interface id that will be supported. For example:
 *
 * ```solidity
 * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
 *     return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
 * }
 * ```
 *
 * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
 */
abstract contract ERC165 is IERC165 {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IERC165).interfaceId;
    }
}

// File: contracts/task-manager/abstract/task-manager/AdminTaskManaged.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract AdminTaskManaged is AdminRoleEnabled {

    function createAdminTask(string memory taskURI) external
      onlyAdmin
    {
        _createTask(taskURI, true);
    }

    function approveAdminTask(uint256 adminTaskId) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
    {
        _approveTask(msg.sender, adminTaskId);
    }

    function withdrawAdminTaskApproval(uint256 adminTaskId) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
    {
        _withdrawTaskApproval(msg.sender, adminTaskId);
    }

    function finalizeAdminTask(uint256 adminTaskId, string memory reason) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
    {
        _finalizeTask(adminTaskId, reason);
    }

    function _getRequiredNrApprovals(uint256 taskId)
      internal view virtual override (TaskManaged) returns (uint) {
        require(_taskExists(taskId), "ATM: non-exsiting task");
        return (1 + _nrOfAdmins / 2);
    }
}

// File: contracts/task-manager/abstract/task-manager/CreatorRoleEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract CreatorRoleEnabled is AdminRoleEnabled {

    mapping (address => bool) private _creators;

    uint internal _nrOfCreators;

    event CreatorAdded(address account);
    event CreatorRemoved(address account);

    modifier onlyCreator() {
        require(_isCreator(msg.sender), "CRE: not creator");
        _;
    }

    modifier onlyCreatorOrAdmin() {
        require(_isCreator(msg.sender) || _isAdmin(msg.sender),
                "CRE: not creator nor admin");
        _;
    }

    constructor() {
        _nrOfCreators = 0;
    }

    function isCreator(address account) external view
      onlyAdmin
      returns (bool)
    {
        return _isCreator(account);
    }

    function addCreator(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _addCreator(account);
        _finalizeTask(adminTaskId, "");
    }

    function removeCreator(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _removeCreator(account);
        _finalizeTask(adminTaskId, "");
    }

    function _isCreator(address account) internal view returns (bool) {
        return _creators[account];
    }

    function _addCreator(address account) internal {
        require(account != address(0), "CRE: zero account");
        require(!_creators[account], "CRE: is creator");
        _creators[account] = true;
        _nrOfCreators += 1;
        emit CreatorAdded(account);
    }

    function _removeCreator(address account) internal {
        require(account != address(0), "CRE: zero account");
        require(_creators[account], "CRE: not creator");
        _creators[account] = false;
        _nrOfCreators -= 1;
        emit CreatorRemoved(account);
    }
}

// File: contracts/task-manager/abstract/task-manager/ApproverRoleEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ApproverRoleEnabled is AdminRoleEnabled {

    mapping (address => bool) private _approvers;

    uint internal _nrOfApprovers;

    event ApproverAdded(address account);
    event ApproverRemoved(address account);

    modifier onlyApprover() {
        require(_isApprover(msg.sender), "ApRE: not approver");
        _;
    }

    constructor() {
        _nrOfApprovers = 0;
    }

    function isApprover(address account) external view
      onlyAdmin
      returns (bool)
    {
        return _isApprover(account);
    }

    function addApprover(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _addApprover(account);
        _finalizeTask(adminTaskId, "");
    }

    function removeApprover(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _removeApprover(account);
        _finalizeTask(adminTaskId, "");
    }

    function _isApprover(address account) internal view returns (bool) {
        return _approvers[account];
    }

    function _addApprover(address account) internal {
        require(account != address(0), "ApRE: zero account");
        require(!_approvers[account], "ApRE: is approver");
        _approvers[account] = true;
        _nrOfApprovers += 1;
        emit ApproverAdded(account);
    }

    function _removeApprover(address account) internal {
        require(account != address(0), "ApRE: zero account");
        require(_approvers[account], "ApRE: not approver");
        _approvers[account] = false;
        _nrOfApprovers -= 1;
        emit ApproverRemoved(account);
    }
}

// File: contracts/task-manager/abstract/task-manager/ExecutorRoleEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ExecutorRoleEnabled is AdminRoleEnabled {

    mapping (address => bool) private _executors;

    uint internal _nrOfExecutors;

    event ExecutorAdded(address account);
    event ExecutorRemoved(address account);

    modifier mustBeExecutor(address account) {
        require(_isExecutor(account), "ERE: not executor");
        _;
    }

    constructor() {
        _nrOfExecutors = 0;
    }

    function isExecutor(address account) external view
      onlyAdmin
      returns (bool)
    {
        return _isExecutor(account);
    }

    function addExecutor(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _addExecutor(account);
        _finalizeTask(adminTaskId, "");
    }

    function removeExecutor(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _removeExecutor(account);
        _finalizeTask(adminTaskId, "");
    }

    function _isExecutor(address account) internal view returns (bool) {
        return _executors[account];
    }

    function _addExecutor(address account) internal {
        require(account != address(0), "ERE: zero account");
        require(!_executors[account], "ERE: is executor");
        _executors[account] = true;
        _nrOfExecutors += 1;
        emit ExecutorAdded(account);
    }

    function _removeExecutor(address account) internal {
        require(account != address(0), "ERE: zero account");
        require(_executors[account], "ERE: not executor");
        _executors[account] = false;
        _nrOfExecutors -= 1;
        emit ExecutorRemoved(account);
    }
}

// File: contracts/task-manager/abstract/task-manager/FinalizerRoleEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract FinalizerRoleEnabled is AdminRoleEnabled {

    mapping (address => bool) private _finalizers;

    uint internal _nrOfFinalizers;

    event FinalizerAdded(address account);
    event FinalizerRemoved(address account);

    modifier onlyFinalizer() {
        require(_isFinalizer(msg.sender), "FinalizerRoleEnabled: not a finalizer account");
        _;
    }

    constructor() {
        _nrOfFinalizers = 0;
    }

    function isFinalizer(address account) external view
      onlyAdmin
      returns (bool)
    {
        return _isFinalizer(account);
    }

    function addFinalizer(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _addFinalizer(account);
        _finalizeTask(adminTaskId, "");
    }

    function removeFinalizer(uint256 adminTaskId, address account) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _removeFinalizer(account);
        _finalizeTask(adminTaskId, "");
    }

    function _isFinalizer(address account) internal view returns (bool) {
        return _finalizers[account];
    }

    function _addFinalizer(address account) internal {
        require(account != address(0), "FRE: zero account");
        require(!_finalizers[account], "FRE: is finalizer");
        _finalizers[account] = true;
        _nrOfFinalizers += 1;
        emit FinalizerAdded(account);
    }

    function _removeFinalizer(address account) internal {
        require(account != address(0), "FRE: zero account");
        require(_finalizers[account], "FRE: not finalizer");
        _finalizers[account] = false;
        _nrOfFinalizers -= 1;
        emit FinalizerRemoved(account);
    }
}

// File: contracts/task-manager/abstract/task-manager/ETHVaultEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "../ETHVault.sol";
import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ETHVaultEnabled is AdminRoleEnabled, ETHVault {

    function isDepositEnabled() external view onlyAdmin returns (bool) {
        return _isDepositEnabled();
    }

    function setEnableDeposit(
        uint256 adminTaskId,
        bool enableDeposit
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        require(_isDepositEnabled() != enableDeposit, "EVE: the same value");
        _setEnableDeposit(enableDeposit);
        _finalizeTask(adminTaskId, "");
    }

    /* solhint-disable func-name-mixedcase */
    function ETHTransfer(
        uint256 adminTaskId,
        address to,
        uint256 amount
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ETHTransfer(to, amount);
        _finalizeTask(adminTaskId, "");
    }
}

// File: contracts/task-manager/abstract/task-manager/ERC20VaultEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "../ERC20Vault.sol";
import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ERC20VaultEnabled is AdminRoleEnabled, ERC20Vault {

    /* solhint-disable func-name-mixedcase */
    function ERC20Transfer(
        uint256 adminTaskId,
        address tokenContract,
        address to,
        uint256 amount
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ERC20Transfer(tokenContract, to, amount);
        _finalizeTask(adminTaskId, "");
    }

    function ERC20Approve(
        uint256 adminTaskId,
        address tokenContract,
        address spender,
        uint256 amount
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ERC20Approve(tokenContract, spender, amount);
        _finalizeTask(adminTaskId, "");
    }
}

// File: contracts/task-manager/abstract/task-manager/ERC721VaultEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "../ERC721Vault.sol";
import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ERC721VaultEnabled is AdminRoleEnabled, ERC721Vault {

    /* solhint-disable func-name-mixedcase */
    function ERC721Transfer(
        uint256 adminTaskId,
        address tokenContract,
        address to,
        uint256 tokenId
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ERC721Transfer(tokenContract, to, tokenId);
        _finalizeTask(adminTaskId, "");
    }

    function ERC721Approve(
        uint256 adminTaskId,
        address tokenContract,
        address operator,
        uint256 tokenId
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ERC721Approve(tokenContract, operator, tokenId);
        _finalizeTask(adminTaskId, "");
    }

    function ERC721SetApprovalForAll(
        uint256 adminTaskId,
        address tokenContract,
        address operator,
        bool approved
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ERC721SetApprovalForAll(tokenContract, operator, approved);
        _finalizeTask(adminTaskId, "");
    }
}

// File: contracts/task-manager/abstract/task-manager/ERC1155VaultEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "../ERC1155Vault.sol";
import "./AdminRoleEnabled.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ERC1155VaultEnabled is AdminRoleEnabled, ERC1155Vault {

    /* solhint-disable func-name-mixedcase */
    function ERC1155Transfer(
        uint256 adminTaskId,
        address tokenContract,
        address to,
        uint256 tokenId,
        uint256 amount
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ERC1155Transfer(tokenContract, to, tokenId, amount);
        _finalizeTask(adminTaskId, "");
    }

    function ERC1155SetApprovalForAll(
        uint256 adminTaskId,
        address tokenContract,
        address operator,
        bool approved
    ) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _ERC1155SetApprovalForAll(tokenContract, operator, approved);
        _finalizeTask(adminTaskId, "");
    }
}

// File: contracts/task-manager/ITaskExecutor.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
interface ITaskExecutor {

    event TaskExecuted(address finalizer, address executor, uint256 taskId);

    function executeTask(address executor, uint256 taskId) external;

    function executeAdminTask(address executor, uint256 taskId) external;
}

// File: @openzeppelin/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

// File: @openzeppelin/contracts/token/ERC721/IERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @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
    ) external;

    /**
     * @dev Transfers `tokenId` token 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;

    /**
     * @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;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @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 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);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * 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 approved 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;
}

// File: @openzeppelin/contracts/utils/introspection/IERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/IERC1155.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC1155 compliant contract, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1155[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155 is IERC165 {
    /**
     * @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`.
     */
    event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);

    /**
     * @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
     * transfers.
     */
    event TransferBatch(
        address indexed operator,
        address indexed from,
        address indexed to,
        uint256[] ids,
        uint256[] values
    );

    /**
     * @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
     * `approved`.
     */
    event ApprovalForAll(address indexed account, address indexed operator, bool approved);

    /**
     * @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
     *
     * If an {URI} event was emitted for `id`, the standard
     * https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
     * returned by {IERC1155MetadataURI-uri}.
     */
    event URI(string value, uint256 indexed id);

    /**
     * @dev Returns the amount of tokens of token type `id` owned by `account`.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id) external view returns (uint256);

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
        external
        view
        returns (uint256[] memory);

    /**
     * @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
     *
     * Emits an {ApprovalForAll} event.
     *
     * Requirements:
     *
     * - `operator` cannot be the caller.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
     *
     * See {setApprovalForAll}.
     */
    function isApprovedForAll(address account, address operator) external view returns (bool);

    /**
     * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If the caller is not `from`, it must be have been approved to spend ``from``'s tokens via {setApprovalForAll}.
     * - `from` must have a balance of tokens of type `id` of at least `amount`.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes calldata data
    ) external;

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] calldata ids,
        uint256[] calldata amounts,
        bytes calldata data
    ) external;
}

// File: contracts/task-manager/abstract/task-manager/AdminRoleEnabled.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "./TaskManaged.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract AdminRoleEnabled is TaskManaged {

    uint public constant MAX_NR_OF_ADMINS = 10;
    uint public constant MIN_NR_OF_ADMINS = 4;

    mapping (address => bool) private _admins;
    uint internal _nrOfAdmins;

    event AdminAdded(address account);
    event AdminRemoved(address account);

    modifier onlyAdmin() {
        require(_isAdmin(msg.sender), "ARE: not admin");
        _;
    }

    constructor() {
        _nrOfAdmins = 0;
    }

    function isAdmin(address account) external view
      onlyAdmin
      returns (bool)
    {
        return _isAdmin(account);
    }

    function getNrAdmins() external view
      onlyAdmin
      returns (uint)
    {
        return _nrOfAdmins;
    }

    function addAdmin(uint256 adminTaskId, address toBeAdded) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _addAdmin(toBeAdded);
        _finalizeTask(adminTaskId, "");
    }

    function replaceAdmin(uint256 adminTaskId, address toBeRemoved, address toBeReplaced) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        if (_nrOfAdmins == MAX_NR_OF_ADMINS) {
            _removeAdmin(toBeRemoved);
            _addAdmin(toBeReplaced);
        } else {
            _addAdmin(toBeReplaced);
            _removeAdmin(toBeRemoved);
        }
        _finalizeTask(adminTaskId, "");
    }

    function removeAdmin(uint256 adminTaskId, address toBeRemoved) external
      onlyAdmin
      taskMustExist(adminTaskId)
      taskMustBeAdministrative(adminTaskId)
      taskMustNotBeFinalized(adminTaskId)
      taskMustBeApproved(adminTaskId)
    {
        _removeAdmin(toBeRemoved);
        _finalizeTask(adminTaskId, "");
    }

    function _isAdmin(address account) internal view returns (bool) {
        return _admins[account];
    }

    function _addAdmin(address account) internal {
        require(account != address(0), "ARE: zero account");
        require(!_admins[account], "ARE: is admin");
        require((_nrOfAdmins + 1) <= MAX_NR_OF_ADMINS, "ARE: exceeds max");
        _admins[account] = true;
        _nrOfAdmins += 1;
        emit AdminAdded(account);
    }

    function _removeAdmin(address account) internal {
        require(account != address(0), "ARE: zero account");
        require(_admins[account], "ARE: not admin");
        require((_nrOfAdmins - 1) >= MIN_NR_OF_ADMINS, "ARE: below min");
        _admins[account] = false;
        _nrOfAdmins -= 1;
        emit AdminRemoved(account);
    }
}

// File: contracts/task-manager/abstract/task-manager/TaskManaged.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC20.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract TaskManaged {

    struct Task {
        uint256 id;
        string uri;
        bool administrative;
        uint nrApprovals;
        bool finalized;
    }
    mapping (uint256 => Task) private _tasks;
    mapping (uint256 => mapping(address => bool)) private _taskApprovals;
    uint256 private _taskIdCounter;

    event TaskCreated(uint256 indexed taskId, string uri, bool administrative);
    event TaskApproved(uint256 taskId);
    event TaskApprovalWithdrawn(uint256 taskId);
    event TaskFinalized(uint256 taskId, string reason);

    modifier taskMustExist(uint256 taskId) {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        _;
    }

    modifier taskMustBeAdministrative(uint256 taskId) {
        require(_isTaskAdministrative(taskId), "TaskManaged: invalid task type");
        _;
    }

    modifier taskMustNotBeAdministrative(uint256 taskId) {
        require(!_isTaskAdministrative(taskId), "TaskManaged: invalid task type");
        _;
    }

    modifier taskMustBeApproved(uint256 taskId) {
        require(_isTaskApproved(taskId), "TaskManaged: task is not approved");
        _;
    }

    modifier taskMustNotBeFinalized(uint256 taskId) {
        require(!_isTaskFinalized(taskId), "TaskManaged: task is finalized");
        _;
    }

    constructor() {
        _taskIdCounter = 1;
    }

    function _getRequiredNrApprovals(uint256 taskId) internal view virtual returns (uint);

    function _taskExists(uint256 taskId) internal view virtual returns (bool) {
        return _tasks[taskId].id > 0;
    }

    function _isTaskAdministrative(uint256 taskId) internal view virtual returns (bool) {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        return _tasks[taskId].administrative;
    }

    function _isTaskApproved(uint256 taskId) internal view virtual returns (bool) {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        return _tasks[taskId].nrApprovals >= _getRequiredNrApprovals(taskId);
    }

    function _isTaskFinalized(uint256 taskId) internal view virtual returns (bool) {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        return _tasks[taskId].finalized;
    }

    function _getTaskURI(uint256 taskId) internal view virtual returns (string memory) {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        return _tasks[taskId].uri;
    }

    function _setTaskURI(uint256 taskId, string memory uri) internal {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        _tasks[taskId].uri = uri;
    }

    function _getTaskNrApprovals(uint256 taskId) internal view virtual returns (uint) {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        return _tasks[taskId].nrApprovals;
    }

    function _getNrOfTasks() internal view virtual returns (uint) {
        return _taskIdCounter - 1;
    }

    function _createTask(
        string memory taskURI,
        bool isAdministrative
    ) internal virtual returns (uint256) {
        uint256 taskId = _taskIdCounter;
        _taskIdCounter++;
        Task memory task = Task(taskId, taskURI, isAdministrative, 0, false);
        _tasks[taskId] = task;
        emit TaskCreated(taskId, taskURI, isAdministrative);
        return taskId;
    }

    function _approveTask(address doer, uint256 taskId) internal virtual {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        require(!_taskApprovals[taskId][doer], "TaskManaged: task is already approved");
        _taskApprovals[taskId][doer] = true;
        _tasks[taskId].nrApprovals += 1;
        emit TaskApproved(taskId);
    }

    function _withdrawTaskApproval(address doer, uint256 taskId) internal virtual {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        require(_taskApprovals[taskId][doer], "TaskManaged: task is not approved");
        _taskApprovals[taskId][doer] = false;
        _tasks[taskId].nrApprovals -= 1;
        emit TaskApprovalWithdrawn(taskId);
    }

    function _finalizeTask(uint256 taskId, string memory reason) internal virtual {
        require(_taskExists(taskId), "TaskManaged: task does not exist");
        _tasks[taskId].finalized = true;
        emit TaskFinalized(taskId, reason);
    }
}

// File: contracts/task-manager/abstract/ETHVault.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk

abstract contract ETHVault {

    bool private _enableDeposit;

    event DepositEnabled();
    event DepositDisabled();
    event ETHTransferred(address to, uint256 amount);

    constructor() {
        _enableDeposit = false;
    }

    function _isDepositEnabled() internal view returns (bool) {
        return _enableDeposit;
    }

    function _setEnableDeposit(bool enableDeposit) internal {
        _enableDeposit = enableDeposit;
        if (_enableDeposit) {
            emit DepositEnabled();
        } else {
            emit DepositDisabled();
        }
    }

    /* solhint-disable func-name-mixedcase */
    function _ETHTransfer(
        address to,
        uint256 amount
    ) internal {
        require(to != address(0), "EV: zero target");
        require(amount > 0, "EV: zero amount");
        require(amount <= address(this).balance, "EV: more than balance");

        /* solhint-disable avoid-low-level-calls */
        (bool success, ) = to.call{value: amount}(new bytes(0));
        /* solhint-enable avoid-low-level-calls */
        require(success, "EV: failed to transfer");
        emit ETHTransferred(to, amount);
    }
}

// File: contracts/task-manager/abstract/ERC20Vault.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC20.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ERC20Vault {

    event ERC20Transferred(address tokenContract, address to, uint256 amount);
    event ERC20Approved(address tokenContract, address spender, uint256 amount);

    /* solhint-disable func-name-mixedcase */
    function _ERC20Transfer(
        address tokenContract,
        address to,
        uint256 amount
    ) internal {
        require(tokenContract != address(0), "E2V: zero address");
        require(to != address(0), "E2V: zero target");
        require(amount > 0, "E2V: zero amount");
        require(amount <= IERC20(tokenContract).balanceOf(address(this)),
                                "E2V: more than balance");

        IERC20(tokenContract).transfer(to, amount);
        emit ERC20Transferred(tokenContract, to, amount);
    }

    function _ERC20Approve(
        address tokenContract,
        address spender,
        uint256 amount
    ) internal {
        require(tokenContract != address(0), "E2V: zero address");
        require(spender != address(0), "E2V: zero spender");

        IERC20(tokenContract).approve(spender, amount);
        emit ERC20Approved(tokenContract, spender, amount);
    }
}

// File: contracts/task-manager/abstract/ERC721Vault.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ERC721Vault is IERC721Receiver {

    event ERC721Transferred(address tokenContract, address to, uint256 tokenId);
    event ERC721Approved(address tokenContract, address to, uint256 tokenId);
    event ERC721ApprovedForAll(address tokenContract, address operator, bool approved);

    function onERC721Received(
        address /* operator */,
        address /* from */,
        uint256 /* tokenId */,
        bytes calldata /* data */
    ) external pure override returns (bytes4) {
        return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
    }

    /* solhint-disable func-name-mixedcase */
    function _ERC721Transfer(
        address tokenContract,
        address to,
        uint256 tokenId
    ) internal {
        require(tokenContract != address(0), "E7V: zero address");
        require(to != address(0), "E7V: zero target");

        IERC721(tokenContract).safeTransferFrom(address(this), to, tokenId, "");
        emit ERC721Transferred(tokenContract, to, tokenId);
    }

    // operator can be the zero address.
    function _ERC721Approve(
        address tokenContract,
        address operator,
        uint256 tokenId
    ) internal {
        require(tokenContract != address(0), "E7V: zero address");

        IERC721(tokenContract).approve(operator, tokenId);
        emit ERC721Approved(tokenContract, operator, tokenId);
    }

    function _ERC721SetApprovalForAll(
        address tokenContract,
        address operator,
        bool approved
    ) internal {
        require(tokenContract != address(0), "E7V: zero address");
        require(operator != address(0), "E7V: zero operator");

        IERC721(tokenContract).setApprovalForAll(operator, approved);
        emit ERC721ApprovedForAll(tokenContract, operator, approved);
    }
}

// File: @openzeppelin/contracts/token/ERC721/IERC721Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

// File: contracts/task-manager/abstract/ERC1155Vault.sol



/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";

/// @author Kam Amini <kam@qomet.tech>
///
/// @notice Use at your own risk
abstract contract ERC1155Vault is IERC1155Receiver {

    event ERC1155Transferred(address tokenContract, address to, uint256 tokenId, uint256 amount);
    event ERC1155ApprovedForAll(address tokenContract, address operator, bool approved);

    function supportsInterface(bytes4 interfaceId) external view virtual override returns (bool) {
        return interfaceId == type(IERC1155Receiver).interfaceId;
    }

    function onERC1155Received(
        address /* operator */,
        address /* from */,
        uint256 /* id */,
        uint256 /* value */,
        bytes calldata /* data */
    ) external pure override returns (bytes4) {
        return bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"));
    }

    function onERC1155BatchReceived(
        address /* operator */,
        address /* from */,
        uint256[] calldata /* ids */,
        uint256[] calldata /* values */,
        bytes calldata /* data */
    ) external pure override returns (bytes4) {
        return bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"));
    }

    /* solhint-disable func-name-mixedcase */
    function _ERC1155Transfer(
        address tokenContract,
        address to,
        uint256 tokenId,
        uint256 amount
    ) internal {
        require(tokenContract != address(0), "E1V: zero address");
        require(to != address(0), "E1V: zero target");

        IERC1155(tokenContract).safeTransferFrom(address(this), to, tokenId, amount, "");
        emit ERC1155Transferred(tokenContract, to, tokenId, amount);
    }

    function _ERC1155SetApprovalForAll(
        address tokenContract,
        address operator,
        bool approved
    ) internal {
        require(tokenContract != address(0), "E1V: zero address");
        require(operator != address(0), "E1V: zero operator");

        IERC1155(tokenContract).setApprovalForAll(operator, approved);
        emit ERC1155ApprovedForAll(tokenContract, operator, approved);
    }
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/IERC1155Receiver.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev _Available since v3.1._
 */
interface IERC1155Receiver is IERC165 {
    /**
     * @dev Handles the receipt of a single ERC1155 token type. This function is
     * called at the end of a `safeTransferFrom` after the balance has been updated.
     *
     * NOTE: To accept the transfer, this must return
     * `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
     * (i.e. 0xf23a6e61, or its own function selector).
     *
     * @param operator The address which initiated the transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param id The ID of the token being transferred
     * @param value The amount of tokens being transferred
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
     */
    function onERC1155Received(
        address operator,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    ) external returns (bytes4);

    /**
     * @dev Handles the receipt of a multiple ERC1155 token types. This function
     * is called at the end of a `safeBatchTransferFrom` after the balances have
     * been updated.
     *
     * NOTE: To accept the transfer(s), this must return
     * `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
     * (i.e. 0xbc197c81, or its own function selector).
     *
     * @param operator The address which initiated the batch transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param ids An array containing ids of each token being transferred (order and length must match values array)
     * @param values An array containing amounts of each token being transferred (order and length must match ids array)
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
     */
    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external returns (bytes4);
}