// File: lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.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: lib/openzeppelin-contracts/contracts/utils/math/SafeCast.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.

pragma solidity ^0.8.0;

/**
 * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
 * checks.
 *
 * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
 * easily result in undesired exploitation or bugs, since developers usually
 * assume that overflows raise errors. `SafeCast` restores this intuition by
 * reverting the transaction when such an operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 *
 * Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
 * all math on `uint256` and `int256` and then downcasting.
 */
library SafeCast {
    /**
     * @dev Returns the downcasted uint248 from uint256, reverting on
     * overflow (when the input is greater than largest uint248).
     *
     * Counterpart to Solidity's `uint248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     *
     * _Available since v4.7._
     */
    function toUint248(uint256 value) internal pure returns (uint248) {
        require(value <= type(uint248).max, "SafeCast: value doesn't fit in 248 bits");
        return uint248(value);
    }

    /**
     * @dev Returns the downcasted uint240 from uint256, reverting on
     * overflow (when the input is greater than largest uint240).
     *
     * Counterpart to Solidity's `uint240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     *
     * _Available since v4.7._
     */
    function toUint240(uint256 value) internal pure returns (uint240) {
        require(value <= type(uint240).max, "SafeCast: value doesn't fit in 240 bits");
        return uint240(value);
    }

    /**
     * @dev Returns the downcasted uint232 from uint256, reverting on
     * overflow (when the input is greater than largest uint232).
     *
     * Counterpart to Solidity's `uint232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     *
     * _Available since v4.7._
     */
    function toUint232(uint256 value) internal pure returns (uint232) {
        require(value <= type(uint232).max, "SafeCast: value doesn't fit in 232 bits");
        return uint232(value);
    }

    /**
     * @dev Returns the downcasted uint224 from uint256, reverting on
     * overflow (when the input is greater than largest uint224).
     *
     * Counterpart to Solidity's `uint224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     *
     * _Available since v4.2._
     */
    function toUint224(uint256 value) internal pure returns (uint224) {
        require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
        return uint224(value);
    }

    /**
     * @dev Returns the downcasted uint216 from uint256, reverting on
     * overflow (when the input is greater than largest uint216).
     *
     * Counterpart to Solidity's `uint216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     *
     * _Available since v4.7._
     */
    function toUint216(uint256 value) internal pure returns (uint216) {
        require(value <= type(uint216).max, "SafeCast: value doesn't fit in 216 bits");
        return uint216(value);
    }

    /**
     * @dev Returns the downcasted uint208 from uint256, reverting on
     * overflow (when the input is greater than largest uint208).
     *
     * Counterpart to Solidity's `uint208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     *
     * _Available since v4.7._
     */
    function toUint208(uint256 value) internal pure returns (uint208) {
        require(value <= type(uint208).max, "SafeCast: value doesn't fit in 208 bits");
        return uint208(value);
    }

    /**
     * @dev Returns the downcasted uint200 from uint256, reverting on
     * overflow (when the input is greater than largest uint200).
     *
     * Counterpart to Solidity's `uint200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     *
     * _Available since v4.7._
     */
    function toUint200(uint256 value) internal pure returns (uint200) {
        require(value <= type(uint200).max, "SafeCast: value doesn't fit in 200 bits");
        return uint200(value);
    }

    /**
     * @dev Returns the downcasted uint192 from uint256, reverting on
     * overflow (when the input is greater than largest uint192).
     *
     * Counterpart to Solidity's `uint192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     *
     * _Available since v4.7._
     */
    function toUint192(uint256 value) internal pure returns (uint192) {
        require(value <= type(uint192).max, "SafeCast: value doesn't fit in 192 bits");
        return uint192(value);
    }

    /**
     * @dev Returns the downcasted uint184 from uint256, reverting on
     * overflow (when the input is greater than largest uint184).
     *
     * Counterpart to Solidity's `uint184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     *
     * _Available since v4.7._
     */
    function toUint184(uint256 value) internal pure returns (uint184) {
        require(value <= type(uint184).max, "SafeCast: value doesn't fit in 184 bits");
        return uint184(value);
    }

    /**
     * @dev Returns the downcasted uint176 from uint256, reverting on
     * overflow (when the input is greater than largest uint176).
     *
     * Counterpart to Solidity's `uint176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     *
     * _Available since v4.7._
     */
    function toUint176(uint256 value) internal pure returns (uint176) {
        require(value <= type(uint176).max, "SafeCast: value doesn't fit in 176 bits");
        return uint176(value);
    }

    /**
     * @dev Returns the downcasted uint168 from uint256, reverting on
     * overflow (when the input is greater than largest uint168).
     *
     * Counterpart to Solidity's `uint168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     *
     * _Available since v4.7._
     */
    function toUint168(uint256 value) internal pure returns (uint168) {
        require(value <= type(uint168).max, "SafeCast: value doesn't fit in 168 bits");
        return uint168(value);
    }

    /**
     * @dev Returns the downcasted uint160 from uint256, reverting on
     * overflow (when the input is greater than largest uint160).
     *
     * Counterpart to Solidity's `uint160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     *
     * _Available since v4.7._
     */
    function toUint160(uint256 value) internal pure returns (uint160) {
        require(value <= type(uint160).max, "SafeCast: value doesn't fit in 160 bits");
        return uint160(value);
    }

    /**
     * @dev Returns the downcasted uint152 from uint256, reverting on
     * overflow (when the input is greater than largest uint152).
     *
     * Counterpart to Solidity's `uint152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     *
     * _Available since v4.7._
     */
    function toUint152(uint256 value) internal pure returns (uint152) {
        require(value <= type(uint152).max, "SafeCast: value doesn't fit in 152 bits");
        return uint152(value);
    }

    /**
     * @dev Returns the downcasted uint144 from uint256, reverting on
     * overflow (when the input is greater than largest uint144).
     *
     * Counterpart to Solidity's `uint144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     *
     * _Available since v4.7._
     */
    function toUint144(uint256 value) internal pure returns (uint144) {
        require(value <= type(uint144).max, "SafeCast: value doesn't fit in 144 bits");
        return uint144(value);
    }

    /**
     * @dev Returns the downcasted uint136 from uint256, reverting on
     * overflow (when the input is greater than largest uint136).
     *
     * Counterpart to Solidity's `uint136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     *
     * _Available since v4.7._
     */
    function toUint136(uint256 value) internal pure returns (uint136) {
        require(value <= type(uint136).max, "SafeCast: value doesn't fit in 136 bits");
        return uint136(value);
    }

    /**
     * @dev Returns the downcasted uint128 from uint256, reverting on
     * overflow (when the input is greater than largest uint128).
     *
     * Counterpart to Solidity's `uint128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     *
     * _Available since v2.5._
     */
    function toUint128(uint256 value) internal pure returns (uint128) {
        require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
        return uint128(value);
    }

    /**
     * @dev Returns the downcasted uint120 from uint256, reverting on
     * overflow (when the input is greater than largest uint120).
     *
     * Counterpart to Solidity's `uint120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     *
     * _Available since v4.7._
     */
    function toUint120(uint256 value) internal pure returns (uint120) {
        require(value <= type(uint120).max, "SafeCast: value doesn't fit in 120 bits");
        return uint120(value);
    }

    /**
     * @dev Returns the downcasted uint112 from uint256, reverting on
     * overflow (when the input is greater than largest uint112).
     *
     * Counterpart to Solidity's `uint112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     *
     * _Available since v4.7._
     */
    function toUint112(uint256 value) internal pure returns (uint112) {
        require(value <= type(uint112).max, "SafeCast: value doesn't fit in 112 bits");
        return uint112(value);
    }

    /**
     * @dev Returns the downcasted uint104 from uint256, reverting on
     * overflow (when the input is greater than largest uint104).
     *
     * Counterpart to Solidity's `uint104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     *
     * _Available since v4.7._
     */
    function toUint104(uint256 value) internal pure returns (uint104) {
        require(value <= type(uint104).max, "SafeCast: value doesn't fit in 104 bits");
        return uint104(value);
    }

    /**
     * @dev Returns the downcasted uint96 from uint256, reverting on
     * overflow (when the input is greater than largest uint96).
     *
     * Counterpart to Solidity's `uint96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     *
     * _Available since v4.2._
     */
    function toUint96(uint256 value) internal pure returns (uint96) {
        require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
        return uint96(value);
    }

    /**
     * @dev Returns the downcasted uint88 from uint256, reverting on
     * overflow (when the input is greater than largest uint88).
     *
     * Counterpart to Solidity's `uint88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     *
     * _Available since v4.7._
     */
    function toUint88(uint256 value) internal pure returns (uint88) {
        require(value <= type(uint88).max, "SafeCast: value doesn't fit in 88 bits");
        return uint88(value);
    }

    /**
     * @dev Returns the downcasted uint80 from uint256, reverting on
     * overflow (when the input is greater than largest uint80).
     *
     * Counterpart to Solidity's `uint80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     *
     * _Available since v4.7._
     */
    function toUint80(uint256 value) internal pure returns (uint80) {
        require(value <= type(uint80).max, "SafeCast: value doesn't fit in 80 bits");
        return uint80(value);
    }

    /**
     * @dev Returns the downcasted uint72 from uint256, reverting on
     * overflow (when the input is greater than largest uint72).
     *
     * Counterpart to Solidity's `uint72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     *
     * _Available since v4.7._
     */
    function toUint72(uint256 value) internal pure returns (uint72) {
        require(value <= type(uint72).max, "SafeCast: value doesn't fit in 72 bits");
        return uint72(value);
    }

    /**
     * @dev Returns the downcasted uint64 from uint256, reverting on
     * overflow (when the input is greater than largest uint64).
     *
     * Counterpart to Solidity's `uint64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     *
     * _Available since v2.5._
     */
    function toUint64(uint256 value) internal pure returns (uint64) {
        require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
        return uint64(value);
    }

    /**
     * @dev Returns the downcasted uint56 from uint256, reverting on
     * overflow (when the input is greater than largest uint56).
     *
     * Counterpart to Solidity's `uint56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     *
     * _Available since v4.7._
     */
    function toUint56(uint256 value) internal pure returns (uint56) {
        require(value <= type(uint56).max, "SafeCast: value doesn't fit in 56 bits");
        return uint56(value);
    }

    /**
     * @dev Returns the downcasted uint48 from uint256, reverting on
     * overflow (when the input is greater than largest uint48).
     *
     * Counterpart to Solidity's `uint48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     *
     * _Available since v4.7._
     */
    function toUint48(uint256 value) internal pure returns (uint48) {
        require(value <= type(uint48).max, "SafeCast: value doesn't fit in 48 bits");
        return uint48(value);
    }

    /**
     * @dev Returns the downcasted uint40 from uint256, reverting on
     * overflow (when the input is greater than largest uint40).
     *
     * Counterpart to Solidity's `uint40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     *
     * _Available since v4.7._
     */
    function toUint40(uint256 value) internal pure returns (uint40) {
        require(value <= type(uint40).max, "SafeCast: value doesn't fit in 40 bits");
        return uint40(value);
    }

    /**
     * @dev Returns the downcasted uint32 from uint256, reverting on
     * overflow (when the input is greater than largest uint32).
     *
     * Counterpart to Solidity's `uint32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     *
     * _Available since v2.5._
     */
    function toUint32(uint256 value) internal pure returns (uint32) {
        require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
        return uint32(value);
    }

    /**
     * @dev Returns the downcasted uint24 from uint256, reverting on
     * overflow (when the input is greater than largest uint24).
     *
     * Counterpart to Solidity's `uint24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     *
     * _Available since v4.7._
     */
    function toUint24(uint256 value) internal pure returns (uint24) {
        require(value <= type(uint24).max, "SafeCast: value doesn't fit in 24 bits");
        return uint24(value);
    }

    /**
     * @dev Returns the downcasted uint16 from uint256, reverting on
     * overflow (when the input is greater than largest uint16).
     *
     * Counterpart to Solidity's `uint16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     *
     * _Available since v2.5._
     */
    function toUint16(uint256 value) internal pure returns (uint16) {
        require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
        return uint16(value);
    }

    /**
     * @dev Returns the downcasted uint8 from uint256, reverting on
     * overflow (when the input is greater than largest uint8).
     *
     * Counterpart to Solidity's `uint8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     *
     * _Available since v2.5._
     */
    function toUint8(uint256 value) internal pure returns (uint8) {
        require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
        return uint8(value);
    }

    /**
     * @dev Converts a signed int256 into an unsigned uint256.
     *
     * Requirements:
     *
     * - input must be greater than or equal to 0.
     *
     * _Available since v3.0._
     */
    function toUint256(int256 value) internal pure returns (uint256) {
        require(value >= 0, "SafeCast: value must be positive");
        return uint256(value);
    }

    /**
     * @dev Returns the downcasted int248 from int256, reverting on
     * overflow (when the input is less than smallest int248 or
     * greater than largest int248).
     *
     * Counterpart to Solidity's `int248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     *
     * _Available since v4.7._
     */
    function toInt248(int256 value) internal pure returns (int248 downcasted) {
        downcasted = int248(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 248 bits");
    }

    /**
     * @dev Returns the downcasted int240 from int256, reverting on
     * overflow (when the input is less than smallest int240 or
     * greater than largest int240).
     *
     * Counterpart to Solidity's `int240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     *
     * _Available since v4.7._
     */
    function toInt240(int256 value) internal pure returns (int240 downcasted) {
        downcasted = int240(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 240 bits");
    }

    /**
     * @dev Returns the downcasted int232 from int256, reverting on
     * overflow (when the input is less than smallest int232 or
     * greater than largest int232).
     *
     * Counterpart to Solidity's `int232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     *
     * _Available since v4.7._
     */
    function toInt232(int256 value) internal pure returns (int232 downcasted) {
        downcasted = int232(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 232 bits");
    }

    /**
     * @dev Returns the downcasted int224 from int256, reverting on
     * overflow (when the input is less than smallest int224 or
     * greater than largest int224).
     *
     * Counterpart to Solidity's `int224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     *
     * _Available since v4.7._
     */
    function toInt224(int256 value) internal pure returns (int224 downcasted) {
        downcasted = int224(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 224 bits");
    }

    /**
     * @dev Returns the downcasted int216 from int256, reverting on
     * overflow (when the input is less than smallest int216 or
     * greater than largest int216).
     *
     * Counterpart to Solidity's `int216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     *
     * _Available since v4.7._
     */
    function toInt216(int256 value) internal pure returns (int216 downcasted) {
        downcasted = int216(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 216 bits");
    }

    /**
     * @dev Returns the downcasted int208 from int256, reverting on
     * overflow (when the input is less than smallest int208 or
     * greater than largest int208).
     *
     * Counterpart to Solidity's `int208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     *
     * _Available since v4.7._
     */
    function toInt208(int256 value) internal pure returns (int208 downcasted) {
        downcasted = int208(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 208 bits");
    }

    /**
     * @dev Returns the downcasted int200 from int256, reverting on
     * overflow (when the input is less than smallest int200 or
     * greater than largest int200).
     *
     * Counterpart to Solidity's `int200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     *
     * _Available since v4.7._
     */
    function toInt200(int256 value) internal pure returns (int200 downcasted) {
        downcasted = int200(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 200 bits");
    }

    /**
     * @dev Returns the downcasted int192 from int256, reverting on
     * overflow (when the input is less than smallest int192 or
     * greater than largest int192).
     *
     * Counterpart to Solidity's `int192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     *
     * _Available since v4.7._
     */
    function toInt192(int256 value) internal pure returns (int192 downcasted) {
        downcasted = int192(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 192 bits");
    }

    /**
     * @dev Returns the downcasted int184 from int256, reverting on
     * overflow (when the input is less than smallest int184 or
     * greater than largest int184).
     *
     * Counterpart to Solidity's `int184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     *
     * _Available since v4.7._
     */
    function toInt184(int256 value) internal pure returns (int184 downcasted) {
        downcasted = int184(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 184 bits");
    }

    /**
     * @dev Returns the downcasted int176 from int256, reverting on
     * overflow (when the input is less than smallest int176 or
     * greater than largest int176).
     *
     * Counterpart to Solidity's `int176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     *
     * _Available since v4.7._
     */
    function toInt176(int256 value) internal pure returns (int176 downcasted) {
        downcasted = int176(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 176 bits");
    }

    /**
     * @dev Returns the downcasted int168 from int256, reverting on
     * overflow (when the input is less than smallest int168 or
     * greater than largest int168).
     *
     * Counterpart to Solidity's `int168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     *
     * _Available since v4.7._
     */
    function toInt168(int256 value) internal pure returns (int168 downcasted) {
        downcasted = int168(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 168 bits");
    }

    /**
     * @dev Returns the downcasted int160 from int256, reverting on
     * overflow (when the input is less than smallest int160 or
     * greater than largest int160).
     *
     * Counterpart to Solidity's `int160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     *
     * _Available since v4.7._
     */
    function toInt160(int256 value) internal pure returns (int160 downcasted) {
        downcasted = int160(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 160 bits");
    }

    /**
     * @dev Returns the downcasted int152 from int256, reverting on
     * overflow (when the input is less than smallest int152 or
     * greater than largest int152).
     *
     * Counterpart to Solidity's `int152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     *
     * _Available since v4.7._
     */
    function toInt152(int256 value) internal pure returns (int152 downcasted) {
        downcasted = int152(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 152 bits");
    }

    /**
     * @dev Returns the downcasted int144 from int256, reverting on
     * overflow (when the input is less than smallest int144 or
     * greater than largest int144).
     *
     * Counterpart to Solidity's `int144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     *
     * _Available since v4.7._
     */
    function toInt144(int256 value) internal pure returns (int144 downcasted) {
        downcasted = int144(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 144 bits");
    }

    /**
     * @dev Returns the downcasted int136 from int256, reverting on
     * overflow (when the input is less than smallest int136 or
     * greater than largest int136).
     *
     * Counterpart to Solidity's `int136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     *
     * _Available since v4.7._
     */
    function toInt136(int256 value) internal pure returns (int136 downcasted) {
        downcasted = int136(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 136 bits");
    }

    /**
     * @dev Returns the downcasted int128 from int256, reverting on
     * overflow (when the input is less than smallest int128 or
     * greater than largest int128).
     *
     * Counterpart to Solidity's `int128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     *
     * _Available since v3.1._
     */
    function toInt128(int256 value) internal pure returns (int128 downcasted) {
        downcasted = int128(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 128 bits");
    }

    /**
     * @dev Returns the downcasted int120 from int256, reverting on
     * overflow (when the input is less than smallest int120 or
     * greater than largest int120).
     *
     * Counterpart to Solidity's `int120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     *
     * _Available since v4.7._
     */
    function toInt120(int256 value) internal pure returns (int120 downcasted) {
        downcasted = int120(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 120 bits");
    }

    /**
     * @dev Returns the downcasted int112 from int256, reverting on
     * overflow (when the input is less than smallest int112 or
     * greater than largest int112).
     *
     * Counterpart to Solidity's `int112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     *
     * _Available since v4.7._
     */
    function toInt112(int256 value) internal pure returns (int112 downcasted) {
        downcasted = int112(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 112 bits");
    }

    /**
     * @dev Returns the downcasted int104 from int256, reverting on
     * overflow (when the input is less than smallest int104 or
     * greater than largest int104).
     *
     * Counterpart to Solidity's `int104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     *
     * _Available since v4.7._
     */
    function toInt104(int256 value) internal pure returns (int104 downcasted) {
        downcasted = int104(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 104 bits");
    }

    /**
     * @dev Returns the downcasted int96 from int256, reverting on
     * overflow (when the input is less than smallest int96 or
     * greater than largest int96).
     *
     * Counterpart to Solidity's `int96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     *
     * _Available since v4.7._
     */
    function toInt96(int256 value) internal pure returns (int96 downcasted) {
        downcasted = int96(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 96 bits");
    }

    /**
     * @dev Returns the downcasted int88 from int256, reverting on
     * overflow (when the input is less than smallest int88 or
     * greater than largest int88).
     *
     * Counterpart to Solidity's `int88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     *
     * _Available since v4.7._
     */
    function toInt88(int256 value) internal pure returns (int88 downcasted) {
        downcasted = int88(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 88 bits");
    }

    /**
     * @dev Returns the downcasted int80 from int256, reverting on
     * overflow (when the input is less than smallest int80 or
     * greater than largest int80).
     *
     * Counterpart to Solidity's `int80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     *
     * _Available since v4.7._
     */
    function toInt80(int256 value) internal pure returns (int80 downcasted) {
        downcasted = int80(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 80 bits");
    }

    /**
     * @dev Returns the downcasted int72 from int256, reverting on
     * overflow (when the input is less than smallest int72 or
     * greater than largest int72).
     *
     * Counterpart to Solidity's `int72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     *
     * _Available since v4.7._
     */
    function toInt72(int256 value) internal pure returns (int72 downcasted) {
        downcasted = int72(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 72 bits");
    }

    /**
     * @dev Returns the downcasted int64 from int256, reverting on
     * overflow (when the input is less than smallest int64 or
     * greater than largest int64).
     *
     * Counterpart to Solidity's `int64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     *
     * _Available since v3.1._
     */
    function toInt64(int256 value) internal pure returns (int64 downcasted) {
        downcasted = int64(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 64 bits");
    }

    /**
     * @dev Returns the downcasted int56 from int256, reverting on
     * overflow (when the input is less than smallest int56 or
     * greater than largest int56).
     *
     * Counterpart to Solidity's `int56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     *
     * _Available since v4.7._
     */
    function toInt56(int256 value) internal pure returns (int56 downcasted) {
        downcasted = int56(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 56 bits");
    }

    /**
     * @dev Returns the downcasted int48 from int256, reverting on
     * overflow (when the input is less than smallest int48 or
     * greater than largest int48).
     *
     * Counterpart to Solidity's `int48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     *
     * _Available since v4.7._
     */
    function toInt48(int256 value) internal pure returns (int48 downcasted) {
        downcasted = int48(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 48 bits");
    }

    /**
     * @dev Returns the downcasted int40 from int256, reverting on
     * overflow (when the input is less than smallest int40 or
     * greater than largest int40).
     *
     * Counterpart to Solidity's `int40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     *
     * _Available since v4.7._
     */
    function toInt40(int256 value) internal pure returns (int40 downcasted) {
        downcasted = int40(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 40 bits");
    }

    /**
     * @dev Returns the downcasted int32 from int256, reverting on
     * overflow (when the input is less than smallest int32 or
     * greater than largest int32).
     *
     * Counterpart to Solidity's `int32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     *
     * _Available since v3.1._
     */
    function toInt32(int256 value) internal pure returns (int32 downcasted) {
        downcasted = int32(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 32 bits");
    }

    /**
     * @dev Returns the downcasted int24 from int256, reverting on
     * overflow (when the input is less than smallest int24 or
     * greater than largest int24).
     *
     * Counterpart to Solidity's `int24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     *
     * _Available since v4.7._
     */
    function toInt24(int256 value) internal pure returns (int24 downcasted) {
        downcasted = int24(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 24 bits");
    }

    /**
     * @dev Returns the downcasted int16 from int256, reverting on
     * overflow (when the input is less than smallest int16 or
     * greater than largest int16).
     *
     * Counterpart to Solidity's `int16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     *
     * _Available since v3.1._
     */
    function toInt16(int256 value) internal pure returns (int16 downcasted) {
        downcasted = int16(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 16 bits");
    }

    /**
     * @dev Returns the downcasted int8 from int256, reverting on
     * overflow (when the input is less than smallest int8 or
     * greater than largest int8).
     *
     * Counterpart to Solidity's `int8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     *
     * _Available since v3.1._
     */
    function toInt8(int256 value) internal pure returns (int8 downcasted) {
        downcasted = int8(value);
        require(downcasted == value, "SafeCast: value doesn't fit in 8 bits");
    }

    /**
     * @dev Converts an unsigned uint256 into a signed int256.
     *
     * Requirements:
     *
     * - input must be less than or equal to maxInt256.
     *
     * _Available since v3.0._
     */
    function toInt256(uint256 value) internal pure returns (int256) {
        // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
        require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
        return int256(value);
    }
}

// File: src/interfaces/IERC20MintableBurnable.sol



pragma solidity 0.8.16;

import {IERC20} from "@oz/token/ERC20/IERC20.sol";

interface IERC20MintableBurnable is IERC20 {
    function mint(address from, uint256 quantity) external;
    function burn(address from, uint256 quantity) external;
    function burn(uint256 quantity) external;
}

// File: src/interfaces/IPRV.sol



pragma solidity 0.8.16;

interface IPRV {
    function depositFor(address account, uint256 amount) external;
    function previewWithdraw(uint256 amount) external view returns (uint256);
    function AUXO() external view returns (address);
}

// File: src/interfaces/IPRVRouter.sol



pragma solidity 0.8.16;

interface IPRVRouter {
    function convertAndStake(uint256 amount) external;
    function convertAndStake(uint256 amount, address _receiver) external;
    function convertAndStakeWithSignature(
        uint256 amount,
        address _receiver,
        uint256 _deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;
}

// File: src/interfaces/ITokenLocker.sol



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

interface ITokenLocker {
    struct Lock {
        uint192 amount;
        uint32 lockedAt;
        uint32 lockDuration;
    }

    // State changing methods
    function depositByMonths(uint192 _amount, uint256 _months, address _receiver) external;
    function boostToMax() external;
    function increaseAmount(uint192 _amount) external;
    function increaseByMonths(uint256 _months) external;
    function migrate(address staker) external;

    /**
     * @notice Ejects a list of lock accounts from the contract.
     * @dev    Smart contracts with locked rewardToken balances should be mindful that
     *         they may be ejected from the contract by external users.
     */
    function eject(address[] calldata _lockAccounts) external;

    // View methods
    function getLock(address _depositor) external returns (Lock memory);
    function lockOf(address account) external view returns (uint192, uint32, uint32);
    function minLockAmount() external returns (uint256);
    function maxLockDuration() external returns (uint32);
    function getLockMultiplier(uint32 _duration) external view returns (uint256);
    function getSecondsMonths() external view returns (uint256);
    function previewDepositByMonths(uint192 _amount, uint256 _months, address _receiver) external view returns (uint256);
    function whitelisted(address _reciever) external returns (bool);
}

// File: src/modules/vedough-bridge/Upgradoor.sol



/// ===============================
/// ===== Audit: NOT IN SCOPE =====
/// ===============================

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

import {IERC20} from "@oz/token/ERC20/IERC20.sol";
import {IERC20MintableBurnable} from "@interfaces/IERC20MintableBurnable.sol";
import {ITokenLocker} from "@interfaces/ITokenLocker.sol";
import {IPRV} from "@interfaces/IPRV.sol";
import {IPRVRouter} from "@interfaces/IPRVRouter.sol";

import {SafeCast} from "@oz/utils/math/SafeCast.sol";

interface ISharesTimelocker {
    function getLocksOfLength(address account) external view returns (uint256);

    function locksOf(address account, uint256 id) external view returns (uint256, uint32, uint32);

    function migrate(address staker, uint256 lockId) external;

    function canEject(address account, uint256 lockId) external view returns (bool);

    function migrateMany(address staker, uint256[] calldata lockIds) external returns (uint256);
}

/// @dev This contract assumes all locks are properly ejected
contract Upgradoor {
    using SafeCast for uint256;
    using SafeCast for uint32;

    address public immutable AUXO;
    address public immutable PRV;
    address public immutable DOUGH;
    address public immutable veDOUGH;
    address public immutable prvRouter;
    ITokenLocker public tokenLocker;

    // Old lock AVG_SECONDS_MONTH constant (private in that contract)
    uint32 public constant AVG_SECONDS_MONTH = 2628000;
    ISharesTimelocker public oldLock;

    event AggregatedAndBoosted(address owner, uint256 amountMigrated);
    event AggregatedToARV(address owner, uint256 amountMigrated);
    event AggregateToPRV(address owner, uint256 amountMigrated);
    event AggregateToPRVAndStake(address owner, uint256 amountMigratedAndStaker);
    event LockUpgradedARV(address receiver, uint256 idx, uint256 amountMigrated);
    event LockUpgradedPRV(address receiver, uint256 idx, uint256 amountMigrated);
    event LockUpgradedPRVAndStake(address receiver, uint256 idx, uint256 amountMigratedAndStaker);

    constructor(
        address _oldLock, // old timelock
        address _auxo,
        address _dough,
        address _tokenLocker,
        address _prv,
        address _veDOUGH,
        address _router
    ) {
        oldLock = ISharesTimelocker(_oldLock);
        AUXO = _auxo;
        DOUGH = _dough;
        veDOUGH = _veDOUGH;
        PRV = _prv;
        tokenLocker = ITokenLocker(_tokenLocker);
        prvRouter = _router;

        IERC20(AUXO).approve(_tokenLocker, type(uint256).max);
        IERC20(AUXO).approve(_prv, type(uint256).max);
        IERC20(AUXO).approve(_router, type(uint256).max);
    }

    // -----------------
    // --- Modifiers ---
    // -----------------

    /// @notice restricts depositing into smart contracts unless whitelisted
    /// @dev fetches whitelisting data from the *destination* locker
    ///      This is not a perfect approach, code size in constructors is zero and addresses can be precomputed.
    ///      However, it prevents accidental transfers by honest users.
    modifier onlyEOAorWL(address _receiver) {
        require(_receiver.code.length == 0 || tokenLocker.whitelisted(_receiver), "Not EOA or WL");
        _;
    }

    // -----------------
    // ---- Preview ----
    // -----------------

    /// @notice Returns the expected PRV return amount when upgrading all veDOUGH locks to PRV.
    function previewAggregateToPRV(address receiver) external view returns (uint256) {
        (, uint256 amount,) = getAmountAndLongestDuration(receiver);
        return getRate(amount);
    }

    /// @notice Returns the expected AUXO return amount when aggregating all veDOUGH locks and boosting to 36 months
    /// @dev boosting to max will yield a 1:1 conversion from veDOUGH -> AUXO
    function previewAggregateAndBoost(address receiver) external view returns (uint256) {
        (, uint256 amount,) = getAmountAndLongestDuration(receiver);
        return getRate(amount);
    }

    /// @notice Aggregate all locks to ARV based on the remaining months of the longest lock
    function previewAggregateARV(address receiver) external view returns (uint256) {
        (, uint256 totalAmount,) = getAmountAndLongestDuration(receiver);
        (, uint32 longestLockedAt, uint32 longestDuration,) = getNextLongestLock(receiver);
        uint256 month = getMonthsNewLock(longestLockedAt, longestDuration);
        return tokenLocker.previewDepositByMonths(getRate(totalAmount).toUint192(), month, receiver);
    }

    function previewUpgradeSingleLockARV(address lockOwner, address receiver) external view returns (uint256) {
        if (lockOwner != receiver) {
            require(IERC20(veDOUGH).balanceOf(address(receiver)) == 0, "Invalid receiver");
        }
        (uint256 amount, uint32 longestLockedAt, uint32 duration,) = getNextLongestLock(lockOwner);

        uint256 month = getMonthsNewLock(longestLockedAt, duration);
        // If returned value is zero the lock is over
        require(month > 0, "Lock expired");
        return tokenLocker.previewDepositByMonths(getRate(amount).toUint192(), month, receiver);
    }

    /// @notice Returns the expected PRV return amount when upgrading a single lock.
    function previewUpgradeSingleLockPRV(address lockOwner) external view returns (uint256) {
        (uint256 amount,,,) = getNextLongestLock(lockOwner);
        return getRate(amount);
    }

    // -------------------------
    // ---- Write functions ----
    // -------------------------

    /// @notice aggregates all the locks to one at max time
    function aggregateAndBoost() external {
        uint256 amountMigrated = _runMigrationAll();
        tokenLocker.depositByMonths(amountMigrated.toUint192(), 36, msg.sender);
        emit AggregatedAndBoosted(msg.sender, amountMigrated);
    }

    /// @notice aggregates all the locks to one ARV
    /// @dev Aggregated to *REMAINING* months on the longest lock
    /// @dev If the remaining months are < 6 than we default to 6
    function aggregateToARV() external {
        (, uint32 longestLockedAt, uint32 duration,) = getNextLongestLock(msg.sender);
        uint256 month = getMonthsNewLock(longestLockedAt, duration);
        uint256 amountMigrated = _runMigrationAll();
        tokenLocker.depositByMonths(amountMigrated.toUint192(), month, msg.sender);
        emit AggregatedToARV(msg.sender, amountMigrated);
    }

    /// @notice aggregates all the locks to PRV
    function aggregateToPRV() external {
        // Migrate function will also mint AUXO to this contract
        uint256 amountMigrated = _runMigrationAll();
        IPRV(PRV).depositFor(msg.sender, amountMigrated);
        emit AggregateToPRV(msg.sender, amountMigrated);
    }

    /// @notice aggregates all the locks to PRV
    function aggregateToPRVAndStake() external {
        // Migrate function will also mint AUXO to this contract
        uint256 amountMigrated = _runMigrationAll();
        IPRVRouter(prvRouter).convertAndStake(amountMigrated, msg.sender);
        emit AggregateToPRVAndStake(msg.sender, amountMigrated);
    }

    /// @notice If the remaning months are < 6 than we default to 6
    function upgradeSingleLockARV(address receiver) external onlyEOAorWL(receiver) {
        if (msg.sender != receiver) {
            require(IERC20(veDOUGH).balanceOf(address(receiver)) == 0, "Invalid receiver");
        }

        (, uint32 longestLockedAt, uint32 duration, uint256 idx) = getNextLongestLock(msg.sender);
        uint256 month = getMonthsNewLock(longestLockedAt, duration);

        // If returned value is zero the lock is over
        require(month > 0, "Lock expired");
        uint256 amountMigrated = _runMigrationOne(idx);
        tokenLocker.depositByMonths(amountMigrated.toUint192(), month, receiver);
        emit LockUpgradedARV(receiver, idx, amountMigrated);
    }

    /// @notice If receiver has an existing lock it will revert
    /// because otherwise receiver would not be able to migrate his own lock
    function upgradeSingleLockPRV(address receiver) external {
        (,,, uint256 idx) = getNextLongestLock(msg.sender);
        uint256 amountMigrated = _runMigrationOne(idx);
        IPRV(PRV).depositFor(receiver, amountMigrated);
        emit LockUpgradedPRV(receiver, idx, amountMigrated);
    }

    function upgradeSingleLockPRVAndStake(address receiver) external {
        (,,, uint256 idx) = getNextLongestLock(msg.sender);
        uint256 amountMigrated = _runMigrationOne(idx);
        IPRVRouter(prvRouter).convertAndStake(amountMigrated, receiver);
        emit LockUpgradedPRVAndStake(receiver, idx, amountMigrated);
    }

    // ----------------
    // --- Internal ---
    // ----------------

    function _runMigrationAll() internal returns (uint256) {
        uint256 numLocks = oldLock.getLocksOfLength(msg.sender);
        require(numLocks > 0, "Nothing to migrate");

        uint256 j;
        uint256[] memory ids = new uint256[](numLocks);

        for (uint256 i = 0; i < numLocks;) {
            (uint256 amount, uint32 lockedAt, uint32 duration) = oldLock.locksOf(msg.sender, i);

            // We don't call migrate on expired/empty because it would revert
            // It might happen that migrate gets never called because all locks are expired
            if (uint256(lockedAt + duration) <= block.timestamp || amount == 0) {
                unchecked {
                    ++i;
                }
                continue;
            }

            ids[j] = i;

            unchecked {
                ++i;
                ++j;
            }
        }

        uint256 balanceMigrated = oldLock.migrateMany(msg.sender, ids);
        uint256 delta = getRate(balanceMigrated);
        require(delta > 0, "Nothing to mint");

        IERC20MintableBurnable(AUXO).mint(address(this), delta);
        return delta;
    }

    function _runMigrationOne(uint256 idx) internal returns (uint256) {
        uint256 balanceDoughBefore = IERC20(DOUGH).balanceOf(address(this));

        oldLock.migrate(msg.sender, idx);

        uint256 balanceDoughAfter = IERC20(DOUGH).balanceOf(address(this));
        uint256 delta = getRate(balanceDoughAfter - balanceDoughBefore);
        require(delta > 0, "Nothing to mint");

        IERC20MintableBurnable(AUXO).mint(address(this), delta);
        return delta;
    }

    // ------------
    // --- View ---
    // ------------

    /// @notice Returns the number of months remaining for the lock, with a minimum of 6 months
    function getMonthsNewLock(uint32 lockedAt, uint32 duration) public view returns (uint256) {
        // If Lock is already expired return 0
        if (uint256(lockedAt + duration) <= block.timestamp) return 0;

        uint256 diff = block.timestamp - lockedAt;
        // How many months were selected originally?
        uint256 selectedMonths = uint256(duration) / AVG_SECONDS_MONTH;

        // How many months have passed?
        uint256 monthDelta = diff / AVG_SECONDS_MONTH;
        uint256 remainingMonths = selectedMonths - monthDelta;

        // If the remaining months are < 6 than we default to 6
        return remainingMonths > 6 ? remainingMonths : 6;
    }

    function getOldLock(address owner, uint256 oldLockId) public view returns (uint256, uint32, uint32) {
        return oldLock.locksOf(owner, oldLockId);
    }

    /// @notice Return the cumulative amount migratable together with the longest duration
    /// @param guy the user with the existing veDOUGH lock
    /// @dev ignores expired locks.
    /// @dev returns Tuple containing:
    ///         1. `longestDuration` in seconds that DOUGH was locked for i.e 35 months in seconds
    ///         2. Cumulative `longestAmount` migrateable together
    ///         3. `longestIndex` representing the lockID of the longest lock duration. In the event of multiple locks with
    ///             the same duration, we use the first id that appears
    function getAmountAndLongestDuration(address guy) public view returns (uint32, uint256, uint256) {
        uint32 longestDuration = 0;
        uint256 longestAmount = 0;
        uint256 longestIndex = 0;
        for (uint32 index = 0; index < oldLock.getLocksOfLength(guy); index++) {
            (uint256 lockAmount, uint32 lockedAt, uint32 lockDuration) = getOldLock(guy, index);

            if (lockAmount == 0) continue;
            if (uint256(lockedAt + lockDuration) <= block.timestamp) continue;

            longestAmount += lockAmount;

            if (lockDuration > longestDuration) {
                longestDuration = lockDuration;
                longestIndex = index;
            }
        }

        return (longestDuration, longestAmount, longestIndex);
    }

    /// @notice Returns the single longest lock by duration in order of creation
    /// @param guy user to fetch locks for
    /// @dev skips empty locks and expired locks
    /// @dev if two locks have the same duration, the first one in order of creation will be user
    function getNextLongestLock(address guy) public view returns (uint256, uint32, uint32, uint256) {
        uint256 longestAmount;
        uint32 longestLockedAt;
        uint32 longestDuration;
        uint256 longestIndex;
        for (uint256 index = 0; index < oldLock.getLocksOfLength(guy); index++) {
            (uint256 lockAmount, uint32 lockedAt, uint32 lockDuration) = getOldLock(guy, index);

            if (lockAmount == 0) continue;
            if (uint256(lockedAt + lockDuration) <= block.timestamp) continue;
            if (lockDuration <= longestDuration) continue;

            longestAmount = lockAmount;
            longestDuration = lockDuration;
            longestLockedAt = lockedAt;
            longestIndex = index;
        }

        return (longestAmount, longestLockedAt, longestDuration, longestIndex);
    }

    function getRate(uint256 amount) public pure returns (uint256) {
        return amount / 100;
    }
}