// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: @openzeppelin/contracts/security/ReentrancyGuard.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be _NOT_ENTERED
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;
    }

    function _nonReentrantAfter() private {
        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
     * `nonReentrant` function in the call stack.
     */
    function _reentrancyGuardEntered() internal view returns (bool) {
        return _status == _ENTERED;
    }
}

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



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

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @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);

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

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



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (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`.
     *
     * 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;

    /**
     * @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 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: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
     * or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
     * understand this adds an external call which potentially creates a reentrancy vulnerability.
     *
     * 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 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);
}

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



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (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 `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/utils/ERC721Holder.sol)

pragma solidity ^0.8.0;

import "../IERC721Receiver.sol";

/**
 * @dev Implementation of the {IERC721Receiver} interface.
 *
 * Accepts all token transfers.
 * Make sure the contract is able to use its token with {IERC721-safeTransferFrom}, {IERC721-approve} or {IERC721-setApprovalForAll}.
 */
contract ERC721Holder is IERC721Receiver {
    /**
     * @dev See {IERC721Receiver-onERC721Received}.
     *
     * Always returns `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(address, address, uint256, bytes memory) public virtual override returns (bytes4) {
        return this.onERC721Received.selector;
    }
}

// File: @openzeppelin/contracts/utils/Context.sol



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

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// 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/utils/math/Math.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)

pragma solidity ^0.8.0;

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    enum Rounding {
        Down, // Toward negative infinity
        Up, // Toward infinity
        Zero // Toward zero
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return a > b ? a : b;
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow.
        return (a & b) + (a ^ b) / 2;
    }

    /**
     * @dev Returns the ceiling of the division of two numbers.
     *
     * This differs from standard division with `/` in that it rounds up instead
     * of rounding down.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b - 1) / b can overflow on addition, so we distribute.
        return a == 0 ? 0 : (a - 1) / b + 1;
    }

    /**
     * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
     * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
     * with further edits by Uniswap Labs also under MIT license.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
        unchecked {
            // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
            // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
            // variables such that product = prod1 * 2^256 + prod0.
            uint256 prod0; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(x, y, not(0))
                prod0 := mul(x, y)
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

            // Handle non-overflow cases, 256 by 256 division.
            if (prod1 == 0) {
                // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                // The surrounding unchecked block does not change this fact.
                // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                return prod0 / denominator;
            }

            // Make sure the result is less than 2^256. Also prevents denominator == 0.
            require(denominator > prod1, "Math: mulDiv overflow");

            ///////////////////////////////////////////////
            // 512 by 256 division.
            ///////////////////////////////////////////////

            // Make division exact by subtracting the remainder from [prod1 prod0].
            uint256 remainder;
            assembly {
                // Compute remainder using mulmod.
                remainder := mulmod(x, y, denominator)

                // Subtract 256 bit number from 512 bit number.
                prod1 := sub(prod1, gt(remainder, prod0))
                prod0 := sub(prod0, remainder)
            }

            // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
            // See https://cs.stackexchange.com/q/138556/92363.

            // Does not overflow because the denominator cannot be zero at this stage in the function.
            uint256 twos = denominator & (~denominator + 1);
            assembly {
                // Divide denominator by twos.
                denominator := div(denominator, twos)

                // Divide [prod1 prod0] by twos.
                prod0 := div(prod0, twos)

                // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
                twos := add(div(sub(0, twos), twos), 1)
            }

            // Shift in bits from prod1 into prod0.
            prod0 |= prod1 * twos;

            // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
            // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
            // four bits. That is, denominator * inv = 1 mod 2^4.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
            // in modular arithmetic, doubling the correct bits in each step.
            inverse *= 2 - denominator * inverse; // inverse mod 2^8
            inverse *= 2 - denominator * inverse; // inverse mod 2^16
            inverse *= 2 - denominator * inverse; // inverse mod 2^32
            inverse *= 2 - denominator * inverse; // inverse mod 2^64
            inverse *= 2 - denominator * inverse; // inverse mod 2^128
            inverse *= 2 - denominator * inverse; // inverse mod 2^256

            // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
            // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
            // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
            // is no longer required.
            result = prod0 * inverse;
            return result;
        }
    }

    /**
     * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
        uint256 result = mulDiv(x, y, denominator);
        if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
            result += 1;
        }
        return result;
    }

    /**
     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
     *
     * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
     */
    function sqrt(uint256 a) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
        //
        // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
        // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
        //
        // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
        // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
        // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
        //
        // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
        uint256 result = 1 << (log2(a) >> 1);

        // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
        // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
        // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
        // into the expected uint128 result.
        unchecked {
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            return min(result, a / result);
        }
    }

    /**
     * @notice Calculates sqrt(a), following the selected rounding direction.
     */
    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = sqrt(a);
            return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 2, rounded down, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 128;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 64;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 32;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 16;
            }
            if (value >> 8 > 0) {
                value >>= 8;
                result += 8;
            }
            if (value >> 4 > 0) {
                value >>= 4;
                result += 4;
            }
            if (value >> 2 > 0) {
                value >>= 2;
                result += 2;
            }
            if (value >> 1 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log2(value);
            return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 10, rounded down, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >= 10 ** 64) {
                value /= 10 ** 64;
                result += 64;
            }
            if (value >= 10 ** 32) {
                value /= 10 ** 32;
                result += 32;
            }
            if (value >= 10 ** 16) {
                value /= 10 ** 16;
                result += 16;
            }
            if (value >= 10 ** 8) {
                value /= 10 ** 8;
                result += 8;
            }
            if (value >= 10 ** 4) {
                value /= 10 ** 4;
                result += 4;
            }
            if (value >= 10 ** 2) {
                value /= 10 ** 2;
                result += 2;
            }
            if (value >= 10 ** 1) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log10(value);
            return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 256, rounded down, of a positive value.
     * Returns 0 if given 0.
     *
     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
     */
    function log256(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 16;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 8;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 4;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 2;
            }
            if (value >> 8 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log256(value);
            return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
        }
    }
}

// File: @openzeppelin/contracts/utils/math/SignedMath.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SignedMath.sol)

pragma solidity ^0.8.0;

/**
 * @dev Standard signed math utilities missing in the Solidity language.
 */
library SignedMath {
    /**
     * @dev Returns the largest of two signed numbers.
     */
    function max(int256 a, int256 b) internal pure returns (int256) {
        return a > b ? a : b;
    }

    /**
     * @dev Returns the smallest of two signed numbers.
     */
    function min(int256 a, int256 b) internal pure returns (int256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two signed numbers without overflow.
     * The result is rounded towards zero.
     */
    function average(int256 a, int256 b) internal pure returns (int256) {
        // Formula from the book "Hacker's Delight"
        int256 x = (a & b) + ((a ^ b) >> 1);
        return x + (int256(uint256(x) >> 255) & (a ^ b));
    }

    /**
     * @dev Returns the absolute unsigned value of a signed value.
     */
    function abs(int256 n) internal pure returns (uint256) {
        unchecked {
            // must be unchecked in order to support `n = type(int256).min`
            return uint256(n >= 0 ? n : -n);
        }
    }
}

// File: @openzeppelin/contracts/utils/Strings.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Strings.sol)

pragma solidity ^0.8.0;

import "./math/Math.sol";
import "./math/SignedMath.sol";

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant _SYMBOLS = "0123456789abcdef";
    uint8 private constant _ADDRESS_LENGTH = 20;

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        unchecked {
            uint256 length = Math.log10(value) + 1;
            string memory buffer = new string(length);
            uint256 ptr;
            /// @solidity memory-safe-assembly
            assembly {
                ptr := add(buffer, add(32, length))
            }
            while (true) {
                ptr--;
                /// @solidity memory-safe-assembly
                assembly {
                    mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
                }
                value /= 10;
                if (value == 0) break;
            }
            return buffer;
        }
    }

    /**
     * @dev Converts a `int256` to its ASCII `string` decimal representation.
     */
    function toString(int256 value) internal pure returns (string memory) {
        return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMath.abs(value))));
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        unchecked {
            return toHexString(value, Math.log256(value) + 1);
        }
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = _SYMBOLS[value & 0xf];
            value >>= 4;
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }

    /**
     * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
     */
    function toHexString(address addr) internal pure returns (string memory) {
        return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
    }

    /**
     * @dev Returns true if the two strings are equal.
     */
    function equal(string memory a, string memory b) internal pure returns (bool) {
        return keccak256(bytes(a)) == keccak256(bytes(b));
    }
}

// File: contracts/ethereum/core/CrowdFundHYPCPoolV2.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import '@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol';
import '@openzeppelin/contracts/security/ReentrancyGuard.sol';
import '@openzeppelin/contracts/access/Ownable.sol';
import '../interfaces/ICHYPC.sol';
import '../interfaces/IHYPC.sol';
import '../interfaces/IHYPCSwap.sol';

import '@openzeppelin/contracts/utils/Strings.sol';

/**
    @title  Crowd Funded HyPC Pool V2
    @author Barry Rowe, David Liendo
    @notice This contract allows users to pool their HyPC together to swap for c_HyPC (containerized HyPC) that
            can be used to back a license in the HyperCycle ecosystem. Unlike HyPC, which is an ERC20, c_HyPC
            is a ERC721, with each token corresponding to 2^19 = 524,288 HyPC, and can have a string assigned
            to it via its contract. These c_HyPC can be obtained by using the swap contract to deposit 524,288
            HyPC for 1 c_HyPC, and can be redeemed for the deposited HyPC again using the same swap contract.

            Since swapping HyPC for c_HyPC requires a lot of funds, a pooling contract is useful for users 
            wanting to have c_HyPC back their license (via the assignment string). In this case, a license 
            holder can create a proposal in the pool for 1 c_HyPC to back their license. They put up some 
            backing HyPC as collateral for this loan, that will be used as interest payments for the users
            that provide HyPC for the proposal. Unlike in V1 of this pool contract, a license holder can
            create a proposal for multiple c_HyPC instead just one.

            As an example, a manager wants to borrow a c_HyPC for 18 months (78 weeks). The manager puts up 
            50,000 HyPC as collateral to act as interest for the user that deposit to this proposal. This means
            that the yearly APR for a depositor to the proposal will be: 50,000/524,288 * (26/39) = 0.063578288
            or roughly 6.35% (26 being the number of 2 week periods in a year, and 39 the number of 2 week
            periods in the proposal's term). The depositors can then claim this interest every period (2 weeks) 
            until the end of the proposal, at which point they can then withdraw and get back their initial 
            deposit. While the proposal is active, the c_HyPC is held by the pool contract itself, though the 
            manager that created the proposal can change the assignement of the swapped for c_HyPC.

            All amounts of HyPC follow the token's native 6 decimals of percision. Dimensionless amounts,
            like that of the APR calcuation, are also held to 6 decimals of percision. General units are
            as follows:

            HyPC = 6 decimals, (eg: 1.2 HyPC = 1,200,000)
            timestamps = 0 decimals, seconds (eg: startTime = block.timestamp)
            periods = 0 decimals, 2 weeks (ie: 1 period = 2*7*24*60*60 seconds, 18 months = 34 periods)
            interestRate = 6 decimals, dimensionless ( 10% = 100,000 )
            
*/

/* General Errors (modifiers) */
///@dev Error for proposal index being invalid.
error InvalidProposalIndex();
///@dev Error for when the sender must be the owner of the proposal.
error MustBeProposalOwner();
///@dev Error for user deposit index being invalid.
error InvalidDepositIndex();

/* Constructor Errors */
///@dev Error if given an invalid HyPC token address on construction
error InvalidToken();
///@dev Error if given an invalid CHyPC NFT address on construction
error InvalidNFT();
///@dev Error if given an invalid Swap address on construction
error InvalidSwapContract();

/* Create Proposal Errors */
///@dev Error for when the term number is not < 3 (18, 24, and 36 months respectively)
error InvalidTermNumber();
///@dev Error for when the proposal deadline is not later than the current block plus 1 hour buffer.
error DeadlineMustBeInFutureByOneHour();
///@dev Error for when the number of NFTs in a proposal is 0.
error NumberNFTsPositive();
///@dev Error for when the number of NFTs is way too large (>4096).
error NumberNFTsTooLarge();

///@dev Errof for when the poolFee in the request doesn't match.
error PoolFeeDoesntMatch();

/* createDeposit Errors */
///@dev Error for when the proposal it not pending.
error ProposalIsNotPending();
///@dev Error for when the proposal is expired.
error ProposalIsExpired();
///@dev Error for when the HyPC deposit amount is 0.
error HYPCDepositMustBePositive();
///@dev Error for when the HyPC exceeds the requested amount.
error HYPCDepositExceedsProposalRequest();

/* transferDeposit Errors */
///@dev Error for when you try to transfer a deposit to yourself
error CantTransferDepositToSelf();
///@dev Error for when you try to transfer to an address that is not registered.
error AddressNotRegistered();

/* transferProposal Errors */
///@dev Error for when trying to transfer a proposal to yourself.
error CantTransferProposalToSelf();

/* startProposal Errors */
///@dev Error for when trying to start a proposal that hasn't been filled yet.
error ProposalMustBeFilled();

/* swapTokens Errors */
///@dev Error for when the proposal is not in the started state.
error ProposalMustBeInStartedState();
///@dev Error for when trying to swap too many tokens.
error SwappingTooManyTokens();

/* redeemTokens Errors */
///@dev Error for when the proposal must be in the completed state.
error ProposalMustBeCompleted();
///@dev Error for when trying to redeem too many tokens.
error RedeemingTooManyTokens();

/* withdrawDeposit Errors */
///@dev Error for when the proposal is in the started state
error ProposalMustNotBeInStartedState();
///@dev Error for when trying to withdraw a deposit before collecting the interest.
error DepositMustBeUpdatedBeforeWithdrawn();
///@dev Error for when trying to withdraw a deposit before redeeming the tokens.
error TokensMustBeRedeemedFirst();

/* updateDeposit Errors */
///@dev Error for when the proposal is not in the started or completed states.
error ProposalMustBeStartedOrCompleted();
///@dev Error for when not enough time has passed since the last interest collection.
error NotEnoughTimeSinceLastInterestCollection();

/* completeProposal Errors */
///@dev Error for when trying to complete a proposal before it reaches the end of its term.
error ProposalMustReachEndOfTerm();

/* finishProposal Errors */
///@dev Error for when there are still tokens left to redeem.
error TokensStillNeedToBeRedeemed();
///@dev Error for when users still need to withdraw from the proposal.
error UsersMustWithdrawFromProposal();
///@dev Error for when backing funds are 0.
error BackingFundsMustBePositive();

/* changeAssignment Errors */
///@dev Error for when the token index in the proposal is invalid
error InvalidTokenIndex();

contract CrowdFundHYPCPoolV2 is ERC721Holder, Ownable, ReentrancyGuard {
    enum Term {
        PENDING,
        STARTED,
        CANCELLED,
        COMPLETED
    }

    struct ContractProposal {
        address owner;
        uint256 term;
        uint256 interestRateAPR;
        uint256 deadline;
        uint256 startTime;
        uint256 depositedAmount;
        uint256 backingFunds;
        Term status;
        uint256[] tokenIds;
        uint256 numberNFTs;
    }

    struct UserDeposit {
        uint256 amount;
        uint256 proposalIndex;
        uint256 interestTime;
    }

    ContractProposal[] public proposals;
    mapping(address => UserDeposit[]) public userDeposits;

    mapping(address => bool) public transferRegistry;

    /// @notice The HyPC ERC20 contract
    IHYPC public immutable hypcToken;

    /// @notice The c_HyPC ERC721 contract
    ICHYPC public immutable hypcNFT;

    /// @notice The HyPC/c_HyPC swapping contract
    IHYPCSwap public immutable swapContract;

    /** 
        @notice The pool fee set by the pool owner and is collected for each created proposal. 
                This is given in HyPC with 6 decimals, and is per c_HyPC requested, so a
                proposal for two c_HyPC will collect two times the poolFee.
    */
    uint256 public poolFee = 0;

    uint256 private constant PROPOSAL_CREATION_DEADLINE_BUFFER = 3600; //1 hour
    //Timing is done PER WEEK, with the assumption that 1 year = 52 weeks
    uint256 private constant _2_WEEKS = 60 * 60 * 24 * 14;
    uint256 private constant _18_MONTHS = 60 * 60 * 24 * 7 * 78; //78 weeks
    uint256 private constant _24_MONTHS = 60 * 60 * 24 * 7 * 104; //104 weeks
    uint256 private constant _36_MONTHS = 60 * 60 * 24 * 7 * 156; //156 weeks

    uint256 private constant SIX_DECIMALS = 10 ** 6;
    uint256 private constant APR_DECIMALS = 10 ** 6;
    uint256 private constant PERIODS_PER_YEAR = 26;
    uint256 private constant PERIODS_PER_YEAR_TIMES_APR_DECIMALS = PERIODS_PER_YEAR * APR_DECIMALS;
    uint256 private constant HYPC_PER_CHYPC_SIX_DECIMALS = 2 ** 19 * SIX_DECIMALS;

    //@dev Rough leeway parameter for the last deposit of a proposal (1 HyPC).
    uint256 private constant DEPOSIT_FILL_LEEWAY = SIX_DECIMALS;

    //@dev Rough guideline for the maximum number nfts available in the CHYPC contract.
    uint256 private constant MAX_NFTS = 4096;

    //Events
    /// @dev   The event for when a manager creates a proposal.
    /// @param proposalIndex: the proposal that was created
    /// @param owner: the proposal creator's address
    /// @param numberNFTs: the number of NFTs for for this proposal
    /// @param deadline: the deadline in blocktime seconds for this proposal to be filled.
    event ProposalCreated(uint256 indexed proposalIndex, address indexed owner, uint256 numberNFTs, uint256 deadline);

    /// @dev   The event for when a proposal is canceled by its creator
    /// @param proposalIndex: the proposal that was canceled
    /// @param owner: The creator's address
    event ProposalCanceled(uint256 indexed proposalIndex, address indexed owner);

    /// @dev   The event for when a proposal is finished by its creator
    /// @param proposalIndex: the proposal that was finished
    /// @param owner: the creator of the proposal
    event ProposalFinished(uint256 indexed proposalIndex, address indexed owner);

    /// @dev   The event for when a user submits a deposit towards a proposal
    /// @param proposalIndex: the proposal this deposit was made towards
    /// @param user: the user address that submitted this deposit
    /// @param amount: the amount of HyPC the user deposited to this proposal.
    event DepositCreated(uint256 indexed proposalIndex, address indexed user, uint256 amount);

    /// @dev   The event for when a user withdraws a previously created deposit
    /// @param depositIndex: the user's deposit index that was withdrawn
    /// @param user: the user's address
    /// @param amount: the amount of HyPC that was withdrawn.
    event WithdrawDeposit(uint256 indexed depositIndex, address indexed user, uint256 amount);

    /// @dev   The event for when a user updates their deposit and gets interest.
    /// @param depositIndex: the deposit index for this user
    /// @param user: the address of the user
    /// @param interestChange: the amount of HyPC interest given to this user for this update.
    event UpdateDeposit(uint256 indexed depositIndex, address indexed user, uint256 interestChange);

    /// @dev   The event for when a user transfers their deposit to another user.
    /// @param depositIndex: the deposit index for this user
    /// @param user: the address of the user
    /// @param to: the address that this deposit was sent to
    /// @param amount: the amount of HyPC in this deposit.
    event TransferDeposit(uint256 indexed depositIndex, address indexed user, address indexed to, uint256 amount);

    /// @dev   The event for when a proposal owner transfers their proposal to another user.
    /// @param proposalIndex: the proposal index for this
    /// @param user: the address of the user
    /// @param to: the address that this proposal was sent to
    /// @param numberNFTs: the number of c_HyPC requested for this proposal.
    event TransferProposal(uint256 indexed proposalIndex, address indexed user, address indexed to, uint256 numberNFTs);

    /// @dev   The event for when a manager changes the assigned string of a token in a proposal.
    /// @param proposalIndex: Index of the changed proposal.
    /// @param owner: the address of the proposal's owner.
    /// @param assignment: string that the proposal's assignment was changed to.
    /// @param tokenIndex: the index inside the proposal.tokenIds array being changed.
    /// @param assignmentRef: string that the proposal's assignment was changed to.
    event AssignmentChanged(
        uint256 indexed proposalIndex,
        address indexed owner,
        string indexed assignment,
        uint256 tokenIndex,
        string assignmentRef
    );

    /// @dev The event for a token swap.
    /// @param user: Address of the user calling the swap function.
    /// @param proposalIndex: Proposal whose tokens are being swappped.
    /// @param tokensToSwap: Amount of tokens swapped.
    event TokensSwapped(address indexed user, uint256 indexed proposalIndex, uint256 tokensToSwap);

    /// @dev   The event for when tokens has been redeemed.
    /// @param user: Address of the user redeeming the tokens
    /// @param proposalIndex: Index of the proposal from where the tokens will be redeemed
    /// @param redeemedTokens: Amount of tokens redeemed.
    event TokensRedeemed(address indexed user, uint256 indexed proposalIndex, uint256 redeemedTokens);

    /// @dev   The event for when a proposal is started.
    /// @param user: Address of the user that started the proposal
    /// @param proposalIndex: Index of the proposal that was started.
    event ProposalStarted(address indexed user, uint256 indexed proposalIndex, uint256 timestamp);

    /// @dev   The event for when a proposal is started.
    /// @param user: Address of the user that completed the proposal
    /// @param proposalIndex: Index of the proposal that was completed.
    event ProposalCompleted(address indexed user, uint256 indexed proposalIndex, uint256 timestamp);

    /// @dev   The event for when pool fee has been set.
    /// @param poolFee: The fee per token to charge for creating a proposal..
    event PoolFeeSet(uint256 indexed poolFee);


    //Modifiers
    /// @dev   Checks that this proposal index has been created.
    /// @param proposalIndex: the proposal index to check
    modifier validIndex(uint256 proposalIndex) {
        if (proposalIndex >= proposals.length) {
            revert InvalidProposalIndex();
        }
        _;
    }

    /// @dev   Checks that the transaction sender is the proposal owner
    /// @param proposalIndex: the proposal index to check ownership of.
    modifier proposalOwner(uint256 proposalIndex) {
        if (msg.sender != proposals[proposalIndex].owner) {
            revert MustBeProposalOwner();
        }
        _;
    }

    /// @dev   Checks that the transaction sender's deposit index is valid.
    /// @param depositIndex: the sender's index to check.
    modifier validDeposit(uint256 depositIndex) {
        if (depositIndex >= userDeposits[msg.sender].length) {
            revert InvalidDepositIndex();
        }
        _;
    }

    /**
        @dev   The constructor takes in the HyPC token, c_HyPC token, and Swap contract addresses to populate
               the contract interfaces.
        @param hypcTokenAddress: the address for the HyPC token contract.
        @param hypcNFTAddress: the address for the CHyPC token contract.
        @param swapContractAddress: the address of the Swap contract.
    */
    constructor(address hypcTokenAddress, address hypcNFTAddress, address swapContractAddress, uint256 defaultFee) {
        if (hypcTokenAddress == address(0)) {
            revert InvalidToken();
        } else if (hypcNFTAddress == address(0)) {
            revert InvalidNFT();
        } else if (swapContractAddress == address(0)) {
            revert InvalidSwapContract();
        }

        hypcToken = IHYPC(hypcTokenAddress);
        hypcNFT = ICHYPC(hypcNFTAddress);
        swapContract = IHYPCSwap(swapContractAddress);

        //pool fee is set to the given default
        poolFee = defaultFee;
        emit PoolFeeSet(defaultFee);
    }

    /// @notice Allows the owner of the pool to set the fee on proposal creation.
    /// @param  fee: the fee in HyPC, per requested c_HyPC, to charge the proposal creator on creation.
    function setPoolFee(uint256 fee) external onlyOwner {
        poolFee = fee;
        emit PoolFeeSet(fee);
    }

    /**
        @notice Allows someone to create a proposal to have HyPC pooled together to swap for c_HyPC token(s).
                The creator specifies the term length for this proposal, the number of c_HyPCs to request,
                and then supplies an amount of HyPC to act as interest for the depositors of the
                proposal.
        @param  termNum: either 0, 1, or 2, corresponding to 18 months, 24 months or 36 months respectively.
        @param  backingFunds: the amount of HyPC that the creator puts up to create the proposal, which acts
                as the interest to give to the depositors during the course of the proposal's term.
        @param  numberNFTs: the number of c_HyPC that this proposal is requesting.
        @param  deadline: the block timestamp that this proposal must be filled by in order to be started.
        @param  specifiedFee: The fee that the creator expects to pay per token
        @dev    The specifiedFee parameter is used to prevent a pool owner from front-running a transaction
                to increase the poolFee after a creator has submitted a transaction.
        @dev    The interest rate calculation for the variable interestRateAPR is described in the contract's
                comment section. The only difference here is that there is an extra term in the numerator of
                APR_DECIMALS since we can't have floating point numbers by default in solidity.
    */
    function createProposal(
        uint256 termNum,
        uint256 backingFunds,
        uint256 numberNFTs,
        uint256 deadline,
        uint256 specifiedFee
    ) external {
        if (termNum >= 3) {
            revert InvalidTermNumber();
        } else if (deadline <= block.timestamp + PROPOSAL_CREATION_DEADLINE_BUFFER) {
            revert DeadlineMustBeInFutureByOneHour();
        } else if (numberNFTs == 0) {
            revert NumberNFTsPositive();
        } else if (numberNFTs > MAX_NFTS) {
            revert NumberNFTsTooLarge();
        } else if (specifiedFee != poolFee) {
            revert PoolFeeDoesntMatch();
        }

        uint256 termLength;
        if (termNum == 0) {
            termLength = _18_MONTHS;
        } else if (termNum == 1) {
            termLength = _24_MONTHS;
        } else {
            termLength = _36_MONTHS;
        }

        uint256 requiredFunds = HYPC_PER_CHYPC_SIX_DECIMALS * numberNFTs;
        uint256 periods = termLength / _2_WEEKS;

        uint256 interestRateAPR = (backingFunds * PERIODS_PER_YEAR_TIMES_APR_DECIMALS) / (requiredFunds * periods);

        proposals.push(
            ContractProposal({
                owner: msg.sender,
                term: termLength,
                interestRateAPR: interestRateAPR,
                deadline: deadline,
                backingFunds: backingFunds,
                tokenIds: new uint256[](0),
                numberNFTs: numberNFTs,
                startTime: 0,
                status: Term.PENDING,
                depositedAmount: 0
            })
        );

        hypcToken.transferFrom(msg.sender, address(this), backingFunds);
        hypcToken.transferFrom(msg.sender, owner(), poolFee * numberNFTs);
        emit ProposalCreated(proposals.length, msg.sender, numberNFTs, deadline);
    }

    /**
        @notice Lets a user creates a deposit for a pending proposal and submit the specified amount of 
                HyPC to back it.
        @param  proposalIndex: the proposal index that the user wants to back.
        @param  amount: the amount of HyPC the user wishes to deposit towards this proposal.
    */
    function createDeposit(uint256 proposalIndex, uint256 amount) external validIndex(proposalIndex) {
        ContractProposal memory proposalData = proposals[proposalIndex];

        if (proposalData.status != Term.PENDING) {
            revert ProposalIsNotPending();
        } else if (block.timestamp >= proposalData.deadline) {
            revert ProposalIsExpired();
        } else if (amount == 0) {
            revert HYPCDepositMustBePositive();
        }

        uint256 total_required_funds = HYPC_PER_CHYPC_SIX_DECIMALS * proposalData.numberNFTs;

        //If amount is bigger than the requested amount because of the leeway parameter,
        //truncate the amount to just the remaining bit.
        uint256 original_amount = amount;
        if (proposalData.depositedAmount + amount > total_required_funds) {
            amount = total_required_funds - proposalData.depositedAmount;
            if (original_amount - amount > DEPOSIT_FILL_LEEWAY) revert HYPCDepositExceedsProposalRequest();
        }

        //Register deposit into proposal's array
        proposals[proposalIndex].depositedAmount += amount;

        //Register user's deposit
        userDeposits[msg.sender].push(UserDeposit({proposalIndex: proposalIndex, amount: amount, interestTime: 0}));
        hypcToken.transferFrom(msg.sender, address(this), amount);
        emit DepositCreated(proposalIndex, msg.sender, amount);
    }

    /**
        @notice Lets a user that owns a deposit for a proposal to transfer the ownership of that
                deposit to another user. This is useful for liquidity since deposit can be tied up for
                fairly long periods of time.
        @param  depositIndex: the index of this users deposits array that they wish to transfer.
        @param  to: the address of the user to send this deposit to
        @dev    Deposit objects are deleted from the deposits array after being transferred. The deposit is 
                deleted and the last entry of the array is copied to that index so the array can be decreased
                in length, so we can avoid iterating through the array.
    */
    function transferDeposit(uint256 depositIndex, address to) external validDeposit(depositIndex) {
        if (to == msg.sender) {
            revert CantTransferDepositToSelf();
        } else if (transferRegistry[to] != true) {
            revert AddressNotRegistered();
        }

        UserDeposit[] memory depositDataArray = userDeposits[msg.sender];
        UserDeposit memory depositData = depositDataArray[depositIndex];

        //Copy deposit to the new address
        userDeposits[to].push(depositData);
        uint256 amount = depositData.amount;

        //Delete this user deposit now.
        //If the deposit is not the last one, then swap it with the last one.
        if (depositDataArray.length > 1 && depositIndex < depositDataArray.length - 1) {
            userDeposits[msg.sender][depositIndex] = depositDataArray[depositDataArray.length - 1];
        }
        userDeposits[msg.sender].pop();
        emit TransferDeposit(depositIndex, msg.sender, to, amount);
    }

    /**
        @notice Lets a proposal owner transfer their proposal to another address.
        @param  proposalIndex: the index of the proposal that they wish to transfer.
        @param  to: the address to send this proposal to
    */
    function transferProposal(
        uint256 proposalIndex,
        address to
    ) external validIndex(proposalIndex) proposalOwner(proposalIndex) {
        if (to == msg.sender) {
            revert CantTransferProposalToSelf();
        } else if (transferRegistry[to] != true) {
            revert AddressNotRegistered();
        }

        proposals[proposalIndex].owner = to;
        emit TransferProposal(proposalIndex, msg.sender, to, proposals[proposalIndex].numberNFTs);
    }

    /**
        @notice Marks a proposal as started after it has received enough HyPC. At this point the proposal
                sets the timestamp for the length of the term and interest payments. 
                periods.
        @param  proposalIndex: the proposal to start.
    */
    function startProposal(uint256 proposalIndex) external validIndex(proposalIndex) {
        ContractProposal memory proposalData = proposals[proposalIndex];

        if (proposalData.status != Term.PENDING) {
            revert ProposalIsNotPending();
        } else if (block.timestamp >= proposalData.deadline) {
            revert ProposalIsExpired();
        } else if (proposalData.depositedAmount != HYPC_PER_CHYPC_SIX_DECIMALS * proposalData.numberNFTs) {
            revert ProposalMustBeFilled();
        }

        //Start the proposal now:
        proposals[proposalIndex].status = Term.STARTED;
        proposals[proposalIndex].startTime = block.timestamp;
        emit ProposalStarted(msg.sender, proposalIndex, block.timestamp);
    }

    /**
        @notice Once a proposal has started, this swaps the deposited HyPC for c_HyPC so that it can
                be assigned by the proposal owner.
        @param  proposalIndex: the index of the proposal to use.
        @param  tokensToSwap: the number of tokens to swap with this call.
        @dev    This function is needed to swap HyPC for c_HyPC aftter a proposal has been started.
                This is split away from the startProposal function in PoolV1  since potentially there 
                could be many swaps required.
    */
    function swapTokens(uint256 proposalIndex, uint256 tokensToSwap) external nonReentrant validIndex(proposalIndex) {
        ContractProposal memory proposalData = proposals[proposalIndex];

        if (proposalData.status != Term.STARTED) {
            revert ProposalMustBeInStartedState();
        } else if (tokensToSwap + proposalData.tokenIds.length > proposalData.numberNFTs) {
            revert SwappingTooManyTokens();
        }

        //approve first
        hypcToken.approve(address(swapContract), tokensToSwap * HYPC_PER_CHYPC_SIX_DECIMALS);

        uint256 _i;
        for (_i = 0; _i < tokensToSwap; _i++) {
            uint256 tokenId = swapContract.nfts(0);
            proposals[proposalIndex].tokenIds.push(tokenId);

            //Swap for CHYPC
            swapContract.swap();
        }
        assert(proposals[proposalIndex].tokenIds.length >= tokensToSwap);
        emit TokensSwapped(msg.sender, proposalIndex, tokensToSwap);
    }

    /**
        @notice Once a proposal has completed, this redeems the previously swapped c_HyPC back to HyPC
                so that it can be withdrawn by the owners of the deposits.
        @param  proposalIndex: the index of the proposal to use.
        @param  tokensToRedeem: the number of tokens to reedem with this call.
        @dev    This function is needed to redeem c_HyPC for HyPC after a proposal has been completed.
                This allows a user with a deposit to reclaim their original deposited HyPC.
    */
    function redeemTokens(
        uint256 proposalIndex,
        uint256 tokensToRedeem
    ) external nonReentrant validIndex(proposalIndex) {
        ContractProposal memory proposalData = proposals[proposalIndex];

        if (proposalData.status != Term.COMPLETED) {
            revert ProposalMustBeCompleted();
        } else if (tokensToRedeem > proposalData.tokenIds.length) {
            revert RedeemingTooManyTokens();
        }

        uint256 _i;
        for (_i = 0; _i < tokensToRedeem; _i++) {
            //unassign token and redeem it.
            uint256 tokenId = proposalData.tokenIds[proposalData.tokenIds.length - _i - 1];
            proposals[proposalIndex].tokenIds.pop();

            hypcNFT.assign(tokenId, '');
            hypcNFT.approve(address(swapContract), tokenId);
            swapContract.redeem(tokenId);
        }

        emit TokensRedeemed(msg.sender, proposalIndex, tokensToRedeem);
    }

    /**
        @notice If a proposal hasn't been started yet, then the creator can cancel it and get back their
                backing HyPC. Users who have deposited can then withdraw their deposits with the withdrawDeposit
                function given below.
        @param  proposalIndex: the proposal index to cancel.
    */
    function cancelProposal(uint256 proposalIndex) external validIndex(proposalIndex) proposalOwner(proposalIndex) {
        if (proposals[proposalIndex].status != Term.PENDING) {
            revert ProposalIsNotPending();
        }

        uint256 amount = proposals[proposalIndex].backingFunds;
        proposals[proposalIndex].backingFunds = 0;
        proposals[proposalIndex].status = Term.CANCELLED;
        hypcToken.transfer(msg.sender, amount);

        emit ProposalCanceled(proposalIndex, msg.sender);
    }

    /**
        @notice Allows a user to withdraw their deposit from a proposal if that proposal has been canceled,
                passed its deadline, has not been started yet, or has come to term. In effect this means that
                to withdraw a token, all status states are ok except the STARTED state. For the case of a proposal
                that has come to term, then the user has to update their deposit to claim any remaining 
                interest first, and all of the proposal's c_HyPC need to be redeemed.
        @param  depositIndex: the index of this user's deposits array that they wish to withdraw.
    */
    function withdrawDeposit(uint256 depositIndex) external validDeposit(depositIndex) {
        UserDeposit[] memory depositDataArray = userDeposits[msg.sender];
        UserDeposit memory depositData = userDeposits[msg.sender][depositIndex];
        uint256 proposalIndex = depositData.proposalIndex;
        ContractProposal memory proposalData = proposals[proposalIndex];
        Term status = proposalData.status;

        if (status == Term.STARTED) {
            revert ProposalMustNotBeInStartedState();
        }

        if (status == Term.COMPLETED) {
            if (depositData.interestTime != proposalData.startTime + proposalData.term) {
                revert DepositMustBeUpdatedBeforeWithdrawn();
            }
            if (proposalData.tokenIds.length != 0) {
                revert TokensMustBeRedeemedFirst();
            }
        }

        uint256 amount = depositData.amount;
        proposals[proposalIndex].depositedAmount -= amount;

        //Delete this user deposit now.
        //If the deposit is not the last one, then swap it with the last one.
        if (depositDataArray.length > 1 && depositIndex < depositDataArray.length - 1) {
            userDeposits[msg.sender][depositIndex] = depositDataArray[depositDataArray.length - 1];
        }
        userDeposits[msg.sender].pop();

        hypcToken.transfer(msg.sender, amount);

        emit WithdrawDeposit(depositIndex, msg.sender, amount);
    }

    /**
        @notice Updates a user's deposit and sends them the accumulated interest from the amount of two week
                periods that have passed.
        @param  depositIndex: the index of this user's deposits array that they wish to update.
        @dev    The interestChange variable takes the user's deposit amount and multiplies it by the 
                proposal's calculated interestRateAPR to get the the yearly interest for this deposit with
                6 extra decimal places. It divides this by the number of periods in a year to get the interest
                from one two-week period, and multiplies it by the number of two week periods that have passed
                since this function was called to account for periods that were previously skipped. Finally,
                it divides the result by APR_DECIMALS to remove the extra decimal places.
    */
    function updateDeposit(uint256 depositIndex) external validDeposit(depositIndex) {
        //get some interest from this deposit
        UserDeposit memory deposit = userDeposits[msg.sender][depositIndex];

        ContractProposal memory proposalData = proposals[deposit.proposalIndex];

        if (proposalData.status != Term.STARTED && proposalData.status != Term.COMPLETED) {
            revert ProposalMustBeStartedOrCompleted();
        }

        if (deposit.interestTime == 0) {
            userDeposits[msg.sender][depositIndex].interestTime = proposalData.startTime;
        }

        uint256 endTime = block.timestamp;
        if (endTime > proposalData.startTime + proposalData.term) {
            endTime = proposalData.startTime + proposalData.term;
        }

        //@dev Don't use depositData since the interestTime could have been changed above
        uint256 periods = (endTime - userDeposits[msg.sender][depositIndex].interestTime) / _2_WEEKS;
        if (periods == 0) {
            revert NotEnoughTimeSinceLastInterestCollection();
        }

        uint256 interestChange = (deposit.amount * periods * proposalData.interestRateAPR) /
            (PERIODS_PER_YEAR_TIMES_APR_DECIMALS);

        //send this interestChange to the user and update both the backing funds and the interest time;
        userDeposits[msg.sender][depositIndex].interestTime += periods * _2_WEEKS;
        proposals[deposit.proposalIndex].backingFunds -= interestChange;

        hypcToken.transfer(msg.sender, interestChange);
        emit UpdateDeposit(depositIndex, msg.sender, interestChange);
    }

    /**
        @notice This completes the proposal after it has come to term, allowing the underlying c_HyPC to be
                redeemed by the contract so it can be given back to the depositors.
        @param  proposalIndex: the proposal's index to complete.
    */
    function completeProposal(uint256 proposalIndex) external validIndex(proposalIndex) {
        ContractProposal memory proposalData = proposals[proposalIndex];
        if (proposalData.status != Term.STARTED) {
            revert ProposalMustBeInStartedState();
        } else if (block.timestamp < proposalData.startTime + proposalData.term) {
            revert ProposalMustReachEndOfTerm();
        }

        proposals[proposalIndex].status = Term.COMPLETED;
        emit ProposalCompleted(msg.sender, proposalIndex, block.timestamp);
    }

    /**
        @notice This allows the creator of a completed proposal to claim any left over backingFunds interest
                after all users have withdrawn their deposits from this proposal.
        @param  proposalIndex: the proposal's index to be finished.
    */
    function finishProposal(uint256 proposalIndex) external validIndex(proposalIndex) proposalOwner(proposalIndex) {
        ContractProposal memory proposalData = proposals[proposalIndex];

        if (proposalData.status != Term.COMPLETED) {
            revert ProposalMustBeCompleted();
        } else if (proposalData.tokenIds.length != 0) {
            revert TokensStillNeedToBeRedeemed();
        } else if (proposalData.depositedAmount != 0) {
            revert UsersMustWithdrawFromProposal();
        } else if (proposalData.backingFunds == 0) {
            revert BackingFundsMustBePositive();
        }

        uint256 amountToSend = proposalData.backingFunds;
        proposals[proposalIndex].backingFunds = 0;

        hypcToken.transfer(msg.sender, amountToSend);
        emit ProposalFinished(proposalIndex, msg.sender);
    }

    /**
        @notice This allows a proposal creator to change the assignment of a c_HyPC token that was swapped for
                in a fulfilled proposal.
        @param  proposalIndex: the proposal's index to have its c_HyPC assignment changed.
        @param  tokenIndex: the index for the token inside the proposal.tokenIds array.
    */
    function changeAssignment(
        uint256 proposalIndex,
        uint256 tokenIndex,
        string memory assignmentString
    ) external validIndex(proposalIndex) proposalOwner(proposalIndex) {
        ContractProposal memory proposalData = proposals[proposalIndex];

        if (proposalData.status != Term.STARTED) {
            revert ProposalMustBeInStartedState();
        } else if (tokenIndex >= proposalData.tokenIds.length) {
            revert InvalidTokenIndex();
        }

        uint256 tokenId = proposalData.tokenIds[tokenIndex];
        hypcNFT.assign(tokenId, assignmentString);

        emit AssignmentChanged(proposalIndex, msg.sender, assignmentString, tokenId, assignmentString);
    }

    /**
        @notice This allows a receving user of a deposit or proposal to first register their address so they 
                can receive the deposit/proposal. This is a safeguard against the sender from fat-fingering
                their address and sending it an invalid address.
    */
    function addToTransferRegistry() external {
        transferRegistry[msg.sender] = true;
    }

    /**
        @notice This deletes a user from the transferRegistry. Mostly not needed, but is here for completeness.
    */
    function removeFromTransferRegistry() external {
        delete transferRegistry[msg.sender];
    }

    //Getters
    /// @notice Returns a user's deposits
    /// @param  user: the user's address.
    /// @return The UserDeposits array for this user
    function getUserDeposits(address user) external view returns (UserDeposit[] memory) {
        return userDeposits[user];
    }

    /// @notice Returns the length of a user's deposits array
    /// @param  user: the user's address
    /// @return The length of the user deposits array.
    function getDepositsLength(address user) external view returns (uint256) {
        return userDeposits[user].length;
    }

    /// @notice Returns the total number of proposals submitted to the contract so far.
    /// @return The length of the contract proposals array.
    function getProposalsLength() external view returns (uint256) {
        return proposals.length;
    }

    /// @notice Returns the number of tokens swapped for in the given  proposal.
    /// @return The length of the proposal's tokenIds array.
    function getProposalTokensLength(uint256 proposalIndex) external view returns (uint256) {
        return proposals[proposalIndex].tokenIds.length;
    }

    /// @notice Returns the tokenId for a tokenIndex in the proposal's tokenIds array.
    /// @return The tokenId for the tokenIndex index of tokenIds.
    function getProposalTokenId(uint256 proposalIndex, uint256 tokenIndex) external view returns (uint256) {
        return proposals[proposalIndex].tokenIds[tokenIndex];
    }

    /// @notice Returns the assignmentString for an tokenIndex in the proposal's tokenIds array.
    /// @return The length of the assignmentString for the tokenIndex of tokenIds.
    function getProposalAssignmentString(
        uint256 proposalIndex,
        uint256 tokenIndex
    ) external view returns (string memory) {
        return hypcNFT.getAssignment(proposals[proposalIndex].tokenIds[tokenIndex]);
    }
}

// File: contracts/ethereum/interfaces/ICHYPC.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

/// @notice Interface for the CHYPC.sol contract.
interface ICHYPC is IERC721 {
    /**
     * Accesses the assignment function of c_HyPC so the swap can remove 
     * the assignment data when a token is redeemed or swapped.
     */
    /// @notice Assigns a string to the given c_HyPC token.
    function assign(
        uint256 tokenId,
        string memory data
    ) external;

    /// @notice Returns the assigned string for this token.
    function getAssignment(
        uint256 tokenId
    ) external view  returns (string memory);
}

// File: contracts/ethereum/interfaces/IHYPC.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/// @notice Interface for the HyperCycleToken.sol contract.
interface IHYPC is IERC20 {
    /*
     * Accesses the ERC20 functions of the HYPC contract. The burn function
     * is also exposed for future contracts.
    */
    /// @notice Burns an amount of the HyPC ERC20.
    function burn(uint256 amount) external;
}

// File: contracts/ethereum/interfaces/IHYPCSwap.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

/// @notice Interface for the HYPCSwap.sol contract.
interface IHYPCSwap {
    /**
     * Accesses the addNFT function so that the CHYPC contract can
     * add the newly created NFT into this contract.
     */

    /// @notice Returns the nfts array inside the swap contract.
    function nfts(uint256 tokenId) external returns (uint256);

    /// @notice Adds a c_HyPC token to the swap contract from the c_HyPC contract.
    function addNFT(
        uint256 tokenId
    ) external;

    /// @notice Redeems a c_HyPC token for its amount of backing HyPC.
    function redeem(uint256 tokenId) external;

    /// @notice Swaps 524288 HyPC for 1 c_HyPC.
    function swap() external;
}