ETH Price: $2,930.17 (+1.76%)

Contract

0xED35B7BBFf1f59e7764d3eF053C75A538d050Efa

Overview

ETH Balance

Linea Mainnet LogoLinea Mainnet LogoLinea Mainnet Logo0 ETH

ETH Value

$0.00

Token Holdings

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Block
From
To
Create Vault Nat...101769582024-10-01 10:28:38482 days ago1727778518IN
0xED35B7BB...38d050Efa
0.02096371 ETH0.000490650.1279285
Create Vault101376352024-09-30 12:23:57483 days ago1727699037IN
0xED35B7BB...38d050Efa
0 ETH0.000288520.075
Create Vault99622892024-09-26 10:08:29487 days ago1727345309IN
0xED35B7BB...38d050Efa
0 ETH0.000354350.09187856
Create Vault99087082024-09-25 4:08:40488 days ago1727237320IN
0xED35B7BB...38d050Efa
0 ETH0.000344480.08932014
Create Vault Nat...98208762024-09-23 2:53:31490 days ago1727060011IN
0xED35B7BB...38d050Efa
0.022 ETH0.000212040.0582
Create Vault Nat...96476562024-09-19 1:56:56494 days ago1726711016IN
0xED35B7BB...38d050Efa
0.0885 ETH0.000194160.05329277
Create Vault Nat...95786732024-09-17 11:36:24496 days ago1726572984IN
0xED35B7BB...38d050Efa
0.00008735 ETH0.00021860.06
Create Vault95183382024-09-16 2:04:08497 days ago1726452248IN
0xED35B7BB...38d050Efa
0 ETH0.000223460.0582
Create Vault Nat...93959172024-09-13 6:01:46500 days ago1726207306IN
0xED35B7BB...38d050Efa
0.125 ETH0.000212040.0582
Create Vault Nat...93908432024-09-13 3:12:38500 days ago1726197158IN
0xED35B7BB...38d050Efa
0.026 ETH0.000212040.0582
Create Vault Nat...93446432024-09-12 1:32:08502 days ago1726104728IN
0xED35B7BB...38d050Efa
0.05 ETH0.000188960.05186434
Create Vault Nat...93216982024-09-11 12:46:21502 days ago1726058781IN
0xED35B7BB...38d050Efa
0.00157107 ETH0.000212040.0582
Create Vault Nat...93091052024-09-11 5:46:35502 days ago1726033595IN
0xED35B7BB...38d050Efa
0.1026 ETH0.000192380.05280522
Create Vault92933012024-09-10 20:59:18503 days ago1726001958IN
0xED35B7BB...38d050Efa
0 ETH0.000210450.05400286
Create Vault Nat...92606762024-09-10 2:51:43503 days ago1725936703IN
0xED35B7BB...38d050Efa
0.1 ETH0.000195310.05360778
Create Vault Nat...92251222024-09-09 7:06:35504 days ago1725865595IN
0xED35B7BB...38d050Efa
0.3 ETH0.000212040.0582
Create Vault Nat...92167002024-09-09 2:25:49504 days ago1725848749IN
0xED35B7BB...38d050Efa
0.1 ETH0.000212040.0582
Create Vault Nat...90098532024-09-04 7:25:19509 days ago1725434719IN
0xED35B7BB...38d050Efa
0.07 ETH0.000212040.0582
Create Vault89479022024-09-02 20:52:18511 days ago1725310338IN
0xED35B7BB...38d050Efa
0 ETH0.000231970.06
Create Vault Nat...89228062024-09-02 6:54:35511 days ago1725260075IN
0xED35B7BB...38d050Efa
0.13555235 ETH0.000223210.0582
Create Vault88556222024-08-31 17:12:24513 days ago1725124344IN
0xED35B7BB...38d050Efa
0 ETH0.000225010.0582
Create Vault Nat...88028362024-08-30 11:48:28514 days ago1725018508IN
0xED35B7BB...38d050Efa
0.01 ETH0.000223210.0582
Create Vault87359432024-08-28 22:30:31516 days ago1724884231IN
0xED35B7BB...38d050Efa
0 ETH0.000234790.061
Create Vault Nat...84658432024-08-22 15:18:42522 days ago1724339922IN
0xED35B7BB...38d050Efa
0.11 ETH0.000212040.0582
Create Vault Nat...84495732024-08-22 6:16:19522 days ago1724307379IN
0xED35B7BB...38d050Efa
0.105 ETH0.000212040.0582
View all transactions

Latest 25 internal transactions (View All)

Advanced mode:
Parent Transaction Hash Block From To
101769582024-10-01 10:28:38482 days ago1727778518
0xED35B7BB...38d050Efa
0 ETH
101769582024-10-01 10:28:38482 days ago1727778518
0xED35B7BB...38d050Efa
0 ETH
101769582024-10-01 10:28:38482 days ago1727778518
0xED35B7BB...38d050Efa
0.02096371 ETH
101769582024-10-01 10:28:38482 days ago1727778518
0xED35B7BB...38d050Efa
0 ETH
101769582024-10-01 10:28:38482 days ago1727778518
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
101376352024-09-30 12:23:57483 days ago1727699037
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99622892024-09-26 10:08:29487 days ago1727345309
0xED35B7BB...38d050Efa
0 ETH
99087082024-09-25 4:08:40488 days ago1727237320
0xED35B7BB...38d050Efa
0 ETH
99087082024-09-25 4:08:40488 days ago1727237320
0xED35B7BB...38d050Efa
0 ETH
99087082024-09-25 4:08:40488 days ago1727237320
0xED35B7BB...38d050Efa
0 ETH
99087082024-09-25 4:08:40488 days ago1727237320
0xED35B7BB...38d050Efa
0 ETH
View All Internal Transactions
Cross-Chain Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
VaultFactoryZapper

Compiler Version
v0.8.23+commit.f704f362

Optimization Enabled:
Yes with 1000 runs

Other Settings:
paris EvmVersion
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.19;

import './interfaces/IVaultFactory.sol';
import './interfaces/IVault.sol';
import './interfaces/ITokenPriceFeed.sol';
import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';

/**
 * @title VaultFactoryZapper
 * @dev A contract that facilitates the creation of Vaults and manages their operations.
 */
contract VaultFactoryZapper is Ownable {
    using SafeERC20 for IERC20;

    IVaultFactory public vaultFactory; // Interface for interacting with VaultFactory
    string public prefix = 'MyVault'; // Prefix for the Vault name

    receive() external payable {} // Fallback function to receive Matic

    /**
     * @dev Sets the VaultFactory contract address.
     * @param _vaultFactory Address of the VaultFactory contract.
     */
    function setVaultFactory(address _vaultFactory) public onlyOwner {
        require(_vaultFactory != address(0), 'VaultFactory: zero address');
        vaultFactory = IVaultFactory(_vaultFactory);
    }

    /**
     * @dev Sets the prefix for Vault names.
     * @param _prefix New prefix for Vault names.
     */
    function setPrefix(string memory _prefix) public onlyOwner {
        prefix = _prefix;
    }

    /**
     * @dev Constructor to initialize the contract with the VaultFactory address.
     * @param _vaultFactory Address of the VaultFactory contract.
     */
    constructor(address _vaultFactory) {
        setVaultFactory(_vaultFactory);
    }

    /**
     * @dev Internal function to generate the name for the next Vault.
     * @param _owner Address of the Vault owner.
     * @return Name for the next Vault.
     */
    function _getNextVaultName(
        address _owner
    ) internal view returns (string memory) {
        uint256 vaultCount = vaultFactory.vaultsByOwnerLength(_owner) + 1;
        return string.concat(prefix, uint2str(vaultCount));
    }

    /**
     * @dev Creates a new Vault.
     * @param _collateralToken Address of the collateral token.
     * @param _collateralAmount Amount of collateral tokens to be deposited.
     * @param _borrowAmount Amount of tokens to be borrowed against the collateral.
     * @return _vault Address of the newly created Vault.
     */
    function createVault(
        address _collateralToken,
        uint256 _collateralAmount,
        uint256 _borrowAmount
    ) external returns (address _vault) {
        _vault = vaultFactory.createVault(_getNextVaultName(msg.sender));

        if (_collateralAmount > 0) {
            IERC20(_collateralToken).safeTransferFrom(
                msg.sender,
                address(this),
                _collateralAmount
            );
            IERC20(_collateralToken).safeApprove(
                address(vaultFactory),
                _collateralAmount
            );
            vaultFactory.addCollateral(
                _vault,
                _collateralToken,
                _collateralAmount
            );
            if (_borrowAmount > 0) {
                vaultFactory.borrow(_vault, _borrowAmount, msg.sender);
            }
        }

        vaultFactory.transferVaultOwnership(_vault, msg.sender);
    }

    /**
     * @dev Creates a new Vault with native (Matic) collateral.
     * @param _borrowAmount Amount of tokens to be borrowed against the collateral.
     * @return _vault Address of the newly created Vault.
     */
    function createVaultNative(
        uint256 _borrowAmount
    ) external payable returns (address _vault) {
        _vault = vaultFactory.createVault(_getNextVaultName(msg.sender));

        if (msg.value > 0) {
            vaultFactory.addCollateralNative{value: msg.value}(_vault);
            if (_borrowAmount > 0) {
                vaultFactory.borrow(_vault, _borrowAmount, msg.sender);
            }
        }
        vaultFactory.transferVaultOwnership(_vault, msg.sender);
    }

    /**
     * @dev Converts uint to a string.
     * @param _i Unsigned integer to be converted.
     * @return _uintAsString String representation of the input integer.
     */
    function uint2str(
        uint _i
    ) internal pure returns (string memory _uintAsString) {
        if (_i == 0) {
            return '0';
        }
        uint j = _i;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len;
        while (_i != 0) {
            k = k - 1;
            uint8 temp = (48 + uint8(_i - (_i / 10) * 10));
            bytes1 b1 = bytes1(temp);
            bstr[k] = b1;
            _i /= 10;
        }
        return string(bstr);
    }
}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface AggregatorInterface {
  function latestAnswer() external view returns (int256);

  function latestTimestamp() external view returns (uint256);

  function latestRound() external view returns (uint256);

  function getAnswer(uint256 roundId) external view returns (int256);

  function getTimestamp(uint256 roundId) external view returns (uint256);

  event AnswerUpdated(int256 indexed current, uint256 indexed roundId, uint256 updatedAt);

  event NewRound(uint256 indexed roundId, address indexed startedBy, uint256 startedAt);
}

File 3 of 38 : AggregatorV2V3Interface.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./AggregatorInterface.sol";
import "./AggregatorV3Interface.sol";

interface AggregatorV2V3Interface is AggregatorInterface, AggregatorV3Interface {}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface AggregatorV3Interface {
  function decimals() external view returns (uint8);

  function description() external view returns (string memory);

  function version() external view returns (uint256);

  function getRoundData(
    uint80 _roundId
  ) external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);

  function latestRoundData()
    external
    view
    returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}

// 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 6 of 38 : IERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../token/ERC20/extensions/IERC20Metadata.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;
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (token/ERC20/extensions/IERC20Permit.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 *
 * ==== Security Considerations
 *
 * There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
 * expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
 * considered as an intention to spend the allowance in any specific way. The second is that because permits have
 * built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
 * take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
 * generally recommended is:
 *
 * ```solidity
 * function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
 *     try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
 *     doThing(..., value);
 * }
 *
 * function doThing(..., uint256 value) public {
 *     token.safeTransferFrom(msg.sender, address(this), value);
 *     ...
 * }
 * ```
 *
 * Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
 * `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
 * {SafeERC20-safeTransferFrom}).
 *
 * Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
 * contracts should have entry points that don't rely on permit.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     *
     * CAUTION: See Security Considerations above.
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    /**
     * @dev Returns the current nonce for `owner`. This value must be
     * included whenever a signature is generated for {permit}.
     *
     * Every successful call to {permit} increases ``owner``'s nonce by one. This
     * prevents a signature from being used multiple times.
     */
    function nonces(address owner) external view returns (uint256);

    /**
     * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}

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

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using Address for address;

    /**
     * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    /**
     * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
     * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
     */
    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    /**
     * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 oldAllowance = token.allowance(address(this), spender);
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
    }

    /**
     * @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
        }
    }

    /**
     * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
     * to be set to zero before setting it to a non-zero value, such as USDT.
     */
    function forceApprove(IERC20 token, address spender, uint256 value) internal {
        bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);

        if (!_callOptionalReturnBool(token, approvalCall)) {
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
            _callOptionalReturn(token, approvalCall);
        }
    }

    /**
     * @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
     * Revert on invalid signature.
     */
    function safePermit(
        IERC20Permit token,
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        uint256 nonceBefore = token.nonces(owner);
        token.permit(owner, spender, value, deadline, v, r, s);
        uint256 nonceAfter = token.nonces(owner);
        require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
     */
    function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
        // and not revert is the subcall reverts.

        (bool success, bytes memory returndata) = address(token).call(data);
        return
            success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
    }
}

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

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     *
     * Furthermore, `isContract` will also return true if the target contract within
     * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
     * which only has an effect at the end of a transaction.
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
     * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
     *
     * _Available since v4.8._
     */
    function verifyCallResultFromTarget(
        address target,
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        if (success) {
            if (returndata.length == 0) {
                // only check isContract if the call was successful and the return data is empty
                // otherwise we already know that it was a contract
                require(isContract(target), "Address: call to non-contract");
            }
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    /**
     * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason or using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    function _revert(bytes memory returndata, string memory errorMessage) private pure {
        // Look for revert reason and bubble it up if present
        if (returndata.length > 0) {
            // The easiest way to bubble the revert reason is using memory via assembly
            /// @solidity memory-safe-assembly
            assembly {
                let returndata_size := mload(returndata)
                revert(add(32, returndata), returndata_size)
            }
        } else {
            revert(errorMessage);
        }
    }
}

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

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

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

pragma solidity ^0.8.0;

/**
 * @dev Library for managing
 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
 * types.
 *
 * Sets have the following properties:
 *
 * - Elements are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Elements are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```solidity
 * contract Example {
 *     // Add the library methods
 *     using EnumerableSet for EnumerableSet.AddressSet;
 *
 *     // Declare a set state variable
 *     EnumerableSet.AddressSet private mySet;
 * }
 * ```
 *
 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
 * and `uint256` (`UintSet`) are supported.
 *
 * [WARNING]
 * ====
 * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
 * unusable.
 * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
 *
 * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
 * array of EnumerableSet.
 * ====
 */
library EnumerableSet {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Set type with
    // bytes32 values.
    // The Set implementation uses private functions, and user-facing
    // implementations (such as AddressSet) are just wrappers around the
    // underlying Set.
    // This means that we can only create new EnumerableSets for types that fit
    // in bytes32.

    struct Set {
        // Storage of set values
        bytes32[] _values;
        // Position of the value in the `values` array, plus 1 because index 0
        // means a value is not in the set.
        mapping(bytes32 => uint256) _indexes;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._indexes[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function _remove(Set storage set, bytes32 value) private returns (bool) {
        // We read and store the value's index to prevent multiple reads from the same storage slot
        uint256 valueIndex = set._indexes[value];

        if (valueIndex != 0) {
            // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 toDeleteIndex = valueIndex - 1;
            uint256 lastIndex = set._values.length - 1;

            if (lastIndex != toDeleteIndex) {
                bytes32 lastValue = set._values[lastIndex];

                // Move the last value to the index where the value to delete is
                set._values[toDeleteIndex] = lastValue;
                // Update the index for the moved value
                set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
            }

            // Delete the slot where the moved value was stored
            set._values.pop();

            // Delete the index for the deleted slot
            delete set._indexes[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._indexes[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        return set._values[index];
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function _values(Set storage set) private view returns (bytes32[] memory) {
        return set._values;
    }

    // Bytes32Set

    struct Bytes32Set {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
        bytes32[] memory store = _values(set._inner);
        bytes32[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }

    // AddressSet

    struct AddressSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(AddressSet storage set) internal view returns (address[] memory) {
        bytes32[] memory store = _values(set._inner);
        address[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }

    // UintSet

    struct UintSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(UintSet storage set) internal view returns (uint256[] memory) {
        bytes32[] memory store = _values(set._inner);
        uint256[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface ILiquidationRouter {
    function addSeizedCollateral(address _collateral, uint256 _amount) external;

    function addUnderWaterDebt(address _vault, uint256 _amount) external;

    function removeUnderWaterDebt(uint256 _amount) external;

    function underWaterDebt() external view returns (uint256);

    function collaterals() external view returns (address[] memory);

    function collateral(address _collateral) external view returns (uint256);

    function tryLiquidate() external;

    function stabilityPool() external view returns (address);
    function auctionManager() external view returns (address);
    function lastResortLiquidation() external view returns (address);
    function distributeBadDebt(address _vault, uint256 _amount) external;
    function transferOwnership(address newOwner) external;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import './IOwnable.sol';

interface IMintableToken is IERC20, IOwnable {
    function mint(address recipient, uint256 amount) external;

    function burn(uint256 amount) external;

    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function approve(
        address spender,
        uint256 amount
    ) external override returns (bool);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import './IOwnable.sol';
import './IMintableToken.sol';

interface IMintableTokenOwner is IOwnable {
    function token() external view returns (IMintableToken);

    function mint(address _recipient, uint256 _amount) external;

    function transferTokenOwnership(address _newOwner) external;

    function addMinter(address _newMinter) external;

    function revokeMinter(address _minter) external;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IOwnable {
    /**
     * @dev Returns the address of the current owner.
     */
    function owner() external view returns (address);

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) external;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IPriceFeed {
    function token() external view returns (address);

    function price() external view returns (uint256);

    function pricePoint() external view returns (uint256);

    function emitPriceSignal() external;

    event PriceUpdate(address token, uint256 price, uint256 average);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import './IOwnable.sol';

interface ITokenPriceFeed is IOwnable {
    struct TokenInfo {
        address priceFeed;
        uint256 mcr; // Minimum Collateralization Ratio
        uint256 mlr; // Minimum Liquidation Ratio
        uint256 borrowRate;
        uint256 decimals;
    }

    function tokenPriceFeed(address) external view returns (address);

    function tokenPrice(address _token) external view returns (uint256);

    function mcr(address _token) external view returns (uint256);

    function decimals(address _token) external view returns (uint256);

    function mlr(address _token) external view returns (uint256);

    function borrowRate(address _token) external view returns (uint256);

    function setTokenPriceFeed(
        address _token,
        address _priceFeed,
        uint256 _mcr,
        uint256 _mlr,
        uint256 _borrowRate,
        uint256 /* _decimals */
    ) external;

    event NewTokenPriceFeed(
        address _token,
        address _priceFeed,
        string _name,
        string _symbol,
        uint256 _mcr,
        uint256 _mlr,
        uint256 _borrowRate,
        uint256 _decimals
    );
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IVault {
    function vaultOwner() external view returns (address);
    function debt() external view returns (uint256);
    function transferVaultOwnership(address _newOwner) external;
    function setName(string memory _name) external;
    function containsCollateral(
        address _collateral
    ) external view returns (bool);
    function collateralsLength() external view returns (uint256);
    function collateralAt(uint256 _index) external view returns (address);
    function collaterals() external view returns (address[] memory);
    function collateral(address _collateral) external view returns (uint256);
    function factory() external view returns (address);
    function addCollateral(address _collateral, uint256 _amount) external;
    function removeCollateral(
        address _collateral,
        uint256 _amount,
        address _to
    ) external;
    function addBadDebt(uint256 _amount) external;
    function borrowable()
        external
        view
        returns (uint256 _maxBorrowable, uint256 _borrowable);
    function borrow(uint256 _amount) external;
    function repay(uint256 _amount) external;
    function calcRedeem(
        address _collateral,
        uint256 _collateralAmount
    )
        external
        view
        returns (uint256 _stableAmountNeeded, uint256 _redemptionFee);
    function redeem(
        address _collateral,
        uint256 _collateralAmount
    ) external returns (uint256 _debtRepaid, uint256 _feeCollected);
    function healthFactor(
        bool _useMlr
    ) external view returns (uint256 _healthFactor);
    function newHealthFactor(
        uint256 _newDebt,
        bool _useMlr
    ) external view returns (uint256 _newHealthFactor);
    function borrowableWithDiff(
        address _collateral,
        uint256 _diffAmount,
        bool _isAdd,
        bool _useMlr
    ) external view returns (uint256 _maxBorrowable, uint256 _borrowable);
    function liquidate() external returns (uint256 _forgivenDebt);
}

File 22 of 38 : IVaultBorrowRate.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IVaultBorrowRate {
    function getBorrowRate(
        address _vaultAddress
    ) external view returns (uint256);
}

File 23 of 38 : IVaultDeployer.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IVaultDeployer {
    function deployVault(
        address _factory,
        address _vaultOwner,
        string memory _name
    ) external returns (address);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IVaultExtraSettings {
    function setMaxRedeemablePercentage(
        uint256 _debtTreshold,
        uint256 _maxRedeemablePercentage
    ) external;
    function setRedemptionKickback(uint256 _redemptionKickback) external;

    function getExtraSettings()
        external
        view
        returns (
            uint256 _debtTreshold,
            uint256 _maxRedeemablePercentage,
            uint256 _redemptionKickback
        );
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IVaultFactory {
    event NewVault(address indexed vault, string name, address indexed owner);
    event PriceFeedUpdated(address indexed priceFeed);

    function setPriceFeed(address _priceFeed) external;
    function vaultCount() external view returns (uint256);
    function lastVault() external view returns (address);
    function firstVault() external view returns (address);
    function nextVault(address _vault) external view returns (address);
    function prevVault(address _vault) external view returns (address);
    function liquidationRouter() external view returns (address);
    function MAX_TOKENS_PER_VAULT() external view returns (uint256);
    function priceFeed() external view returns (address);
    function transferVaultOwnership(address _vault, address _newOwner) external;
    function createVault(string memory _name) external returns (address);
    function addCollateralNative(address _vault) external payable;
    function removeCollateralNative(
        address _vault,
        uint256 _amount,
        address _to
    ) external;
    function addCollateral(
        address _vault,
        address _collateral,
        uint256 _amount
    ) external;
    function removeCollateral(
        address _vault,
        address _collateral,
        uint256 _amount,
        address _to
    ) external;
    function borrow(address _vault, uint256 _amount, address _to) external;
    function distributeBadDebt(address _vault, uint256 _amount) external;
    function closeVault(address _vault) external;
    function repay(address _vault, uint256 _amount) external;
    function redeem(
        address _vault,
        address _collateral,
        uint256 _collateralAmount,
        address _to
    ) external;
    function liquidate(address _vault) external;
    function isLiquidatable(address _vault) external view returns (bool);
    function isReedemable(
        address _vault,
        address _collateral
    ) external view returns (bool);
    function containsVault(address _vault) external view returns (bool);
    function stable() external view returns (address);
    function isCollateralSupported(
        address _collateral
    ) external view returns (bool);
    function vaultsByOwnerLength(
        address _owner
    ) external view returns (uint256);
    function redemptionHealthFactorLimit() external view returns (uint256);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IVaultFactoryConfig {
    event PriceFeedUpdated(address indexed priceFeed);
    event MaxTokensPerVaultUpdated(
        uint256 oldMaxTokensPerVault,
        uint256 newMaxTokensPerVault
    );
    event RedemptionRateUpdated(
        uint256 oldRedemptionRate,
        uint256 newRedemptionRate
    );
    event BorrowRateUpdated(uint256 oldBorrowRate, uint256 newBorrowRate);
    event RedemptionHealthFactorLimitUpdated(
        uint256 oldRedemptionHealthFactorLimit,
        uint256 newRedemptionHealthFactorLimit
    );

    function setMaxTokensPerVault(uint256 _maxTokensPerVault) external;
    function setPriceFeed(address _priceFeed) external;
    function setRedemptionRate(uint256 _redemptionRate) external;
    function setBorrowRate(uint256 _borrowRate) external;
    function setRedemptionHealthFactorLimit(
        uint256 _redemptionHealthFactorLimit
    ) external;
    function setBorrowFeeRecipient(address _borrowFeeRecipient) external;
    function setRedemptionFeeRecipient(
        address _redemptionFeeRecipient
    ) external;

    function priceFeed() external view returns (address);
    function MAX_TOKENS_PER_VAULT() external view returns (uint256);
    function redemptionRate() external view returns (uint256);
    function borrowRate() external view returns (uint256);
    function redemptionHealthFactorLimit() external view returns (uint256);
    function borrowFeeRecipient() external view returns (address);
    function redemptionFeeRecipient() external view returns (address);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

interface IWETH {
    function deposit() external payable;

    function approve(address, uint256) external returns (bool);

    function transfer(address _to, uint256 _value) external returns (bool);

    function withdraw(uint256) external;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import '@chainlink/contracts/src/v0.8/interfaces/AggregatorV2V3Interface.sol';
import '../interfaces/IPriceFeed.sol';
import '../interfaces/ITokenPriceFeed.sol';
import '../utils/constants.sol';

/**
 * @title ChainlinkPriceFeed
 * @dev Retrieves and manages price data from Chainlink's Oracle for specified tokens.
 */
contract ChainlinkPriceFeed is IPriceFeed, Constants {
    AggregatorV2V3Interface public immutable oracle;
    address public immutable override token;
    uint256 public immutable precision;
    uint256 public updateThreshold = 24 hours;

    /**
     * @dev Initializes the Chainlink price feed with the specified oracle and token.
     * @param _oracle The address of the Chainlink oracle contract.
     * @param _token The address of the associated token.
     */
    constructor(address _oracle, address _token) {

        require(
            _oracle != address(0x0),
            'e2637b _oracle must not be address 0x0'
        );
        require(
            _token != address(0x0),
            'e2637b _token must not be address 0x0'
        );
        token = _token;
        oracle = AggregatorV2V3Interface(_oracle);
        uint8 decimals = oracle.decimals();
        require(decimals > 0, 'e2637b decimals must be a positive number');
        precision = 10 ** decimals;
    }

    /**
     * @dev Retrieves the current price from the Chainlink oracle, ensuring it is not outdated.
     * @return The latest recorded price of the associated token.
     */
    function price() public view virtual override returns (uint256) {
        (, int256 _price, , uint256 _timestamp, ) = oracle.latestRoundData();
        require(_price > 0, 'e2637b _price must be a positive number');
        require(
            block.timestamp - _timestamp <= updateThreshold,
            'price-outdated'
        );
        return (uint256(_price) * DECIMAL_PRECISION) / precision;
    }

    /**
     * @dev Retrieves the current price point.
     * @return The current price of the associated token.
     */
    function pricePoint() public view override returns (uint256) {
        return price();
    }

    /**
     * @dev Emits a price update signal for the associated token.
     */
    function emitPriceSignal() public override {
        emit PriceUpdate(token, price(), price());
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import './ChainlinkPriceFeed.sol';

/**
 * @title ConvertedPriceFeed
 * @dev Manages a price feed by converting between two different price feeds and emitting price signals.
 */
contract ConvertedPriceFeed is IPriceFeed, Constants {
    IPriceFeed public immutable priceFeed;
    IPriceFeed public immutable conversionPriceFeed;
    address public immutable override token;

    /**
     * @dev Constructor sets up the price feeds and associated tokens for conversion.
     * @param _priceFeed The primary price feed address.
     * @param _conversionPriceFeed The conversion price feed address.
     * @param _token The token address associated with the price feed.
     */
    constructor(
        address _priceFeed,
        address _conversionPriceFeed,
        address _token
    ) {
        require(
            _priceFeed != address(0x0),
            'e2637b _priceFeed must not be address 0x0'
        );
        require(
            _conversionPriceFeed != address(0x0),
            'e2637b _conversionPriceFeed must not be address 0x0'
        );
        priceFeed = IPriceFeed(_priceFeed);
        conversionPriceFeed = IPriceFeed(_conversionPriceFeed);
        token = _token;
    }

    /**
     * @dev Retrieves the converted price by multiplying the primary price with the conversion price.
     */
    function price() public view override returns (uint256) {
        return
            (priceFeed.price() * DECIMAL_PRECISION) /
            conversionPriceFeed.price();
    }

    /**
     * @dev Retrieves the current price point by calling the 'price()' function.
     */
    function pricePoint() public view override returns (uint256) {
        return price();
    }

    /**
     * @dev Emits a price signal using the converted price.
     */
    function emitPriceSignal() public {
        emit PriceUpdate(token, price(), price());
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import './ChainlinkPriceFeed.sol';

contract FixedPriceOracle is IPriceFeed, Constants {
    IPriceFeed public immutable priceFeed = IPriceFeed(address(0));
    IPriceFeed public immutable conversionPriceFeed = IPriceFeed(address(0));
    address public immutable override token;
    uint256 public fixedPrice;
    constructor(address _token, uint256 _price) {
        fixedPrice = _price;
        token = _token;
    }

    function price() public view override returns (uint256) {
        return fixedPrice;
    }

    function pricePoint() public view override returns (uint256) {
        return price();
    }

    function emitPriceSignal() public {
        emit PriceUpdate(token, price(), price());
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import './ChainlinkPriceFeed.sol';

contract MockConvertedPriceFeed is IPriceFeed, Constants {
    IPriceFeed public immutable priceFeed = IPriceFeed(address(0));
    IPriceFeed public immutable conversionPriceFeed = IPriceFeed(address(0));
    address public immutable override token;

    address public constant DAI = 0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063;
    address public constant WETH = 0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619;
    address public constant WMATIC = 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270;
    address public constant QNT = 0x36B77a184bE8ee56f5E81C56727B20647A42e28E;
    address public constant PAXG = 0x553d3D295e0f695B9228246232eDF400ed3560B5;
    address public constant USDC = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;
    address public constant USDT = 0xc2132D05D31c914a87C6611C10748AEb04B58e8F;
    address public constant LINK = 0x53E0bca35eC356BD5ddDFebbD1Fc0fD03FaBad39;
    address public constant UNI = 0xb33EaAd8d922B1083446DC23f610c2567fB5180f;
    address public constant SUSHI = 0x0b3F868E0BE5597D5DB7fEB59E1CADBb0fdDa50a;

    constructor(address _token) {
        token = _token;
    }

    function price() public view override returns (uint256) {
        if (token == DAI) return 931895460772162633;
        if (token == WETH) return 1775994046278866046632;
        if (token == WMATIC) return 663421266959892649;
        if (token == QNT) return 75127544993710105763;
        if (token == PAXG) return 1828020314028793738060;
        if (token == USDC) return 935946997491670098;
        if (token == USDT) return 663421266959892649;
        if (token == LINK) return 75127544993710105763;
        if (token == UNI) return 1828020314028793738060;
        if (token == SUSHI) return 935946997491670098;
    }

    function pricePoint() public view override returns (uint256) {
        return price();
    }

    function emitPriceSignal() public {
        emit PriceUpdate(token, price(), price());
    }
}

File 32 of 38 : constants.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

/**
 * @title Constants
 * @dev This contract defines various constants used within the system.
 */
contract Constants {
    // Precision used for decimal calculations
    uint256 public constant DECIMAL_PRECISION = 1e18;

    // Reserve required for liquidation purposes
    uint256 public constant LIQUIDATION_RESERVE = 1e18;

    // Maximum value for uint256
    uint256 public constant MAX_INT = 2 ** 256 - 1;

    // Constants for percentage calculations
    uint256 public constant PERCENT = (DECIMAL_PRECISION * 1) / 100; // Represents 1%
    uint256 public constant PERCENT10 = PERCENT * 10; // Represents 10%
    uint256 public constant PERCENT_05 = PERCENT / 2; // Represents 0.5%

    // Maximum borrowing and redemption rates
    uint256 public constant MAX_BORROWING_RATE = (DECIMAL_PRECISION * 5) / 100; // Represents 5%
    uint256 public constant MAX_REDEMPTION_RATE =
        (DECIMAL_PRECISION * 10) / 100; // Represents 10%
}

// SPDX-License-Identifier: BUSL-1.1

pragma solidity ^0.8.4;

/**
 * @title LinkedAddressList
 * @dev Library implementing a linked list structure to store and operate sorted Troves.
 */
library LinkedAddressList {
    struct EntryLink {
        address prev;
        address next;
    }

    struct List {
        address _last;
        address _first;
        uint256 _size;
        mapping(address => EntryLink) _values;
    }

    /**
     * @dev Adds an element to the linked list.
     * @param _list The storage pointer to the linked list.
     * @param _element The element to be added.
     * @param _reference The reference element to determine the position for addition.
     * @param _before A boolean indicating whether to add the element before the reference.
     * @return A boolean indicating the success of the addition.
     */
    function add(
        List storage _list,
        address _element,
        address _reference,
        bool _before
    ) internal returns (bool) {
        require(
            _reference == address(0x0) ||
                _list._values[_reference].next != address(0x0),
            '79d3d _ref neither valid nor 0x'
        );

        // Element must not exist to be added
        EntryLink storage element_values = _list._values[_element];
        if (element_values.prev == address(0x0)) {
            if (_list._last == address(0x0)) {
                // If the list is empty, set the element as both first and last
                element_values.prev = _element;
                element_values.next = _element;
                _list._first = _element;
                _list._last = _element;
            } else {
                if (
                    _before &&
                    (_reference == address(0x0) || _reference == _list._first)
                ) {
                    // Adding the element as the first element
                    address first = _list._first;
                    _list._values[first].prev = _element;
                    element_values.prev = _element;
                    element_values.next = first;
                    _list._first = _element;
                } else if (
                    !_before &&
                    (_reference == address(0x0) || _reference == _list._last)
                ) {
                    // Adding the element as the last element
                    address last = _list._last;
                    _list._values[last].next = _element;
                    element_values.prev = last;
                    element_values.next = _element;
                    _list._last = _element;
                } else {
                    // Inserting the element between two elements
                    EntryLink memory ref = _list._values[_reference];
                    if (_before) {
                        element_values.prev = ref.prev;
                        element_values.next = _reference;
                        _list._values[_reference].prev = _element;
                        _list._values[ref.prev].next = _element;
                    } else {
                        element_values.prev = _reference;
                        element_values.next = ref.next;
                        _list._values[_reference].next = _element;
                        _list._values[ref.next].prev = _element;
                    }
                }
            }
            _list._size = _list._size + 1;
            return true;
        }
        return false;
    }

    /**
     * @dev Removes an element from the linked list.
     * @param _list The storage pointer to the linked list.
     * @param _element The element to be removed.
     * @return A boolean indicating the success of the removal.
     */
    function remove(
        List storage _list,
        address _element
    ) internal returns (bool) {
        EntryLink memory element_values = _list._values[_element];
        if (element_values.next != address(0x0)) {
            if (_element == _list._last && _element == _list._first) {
                // Removing the last and only element in the list
                delete _list._last;
                delete _list._first;
            } else if (_element == _list._first) {
                // Removing the first element
                address next = element_values.next;
                _list._values[next].prev = next;
                _list._first = next;
            } else if (_element == _list._last) {
                // Removing the last element
                address new_list_last = element_values.prev;
                _list._last = new_list_last;
                _list._values[new_list_last].next = new_list_last;
            } else {
                // Removing an element in between two other elements
                address next = element_values.next;
                address prev = element_values.prev;
                _list._values[next].prev = prev;
                _list._values[prev].next = next;
            }
            // Delete the element itself
            delete _list._values[_element];
            _list._size = _list._size - 1;
            return true;
        }
        return false;
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/interfaces/IERC20Metadata.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/Context.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';

import './interfaces/IPriceFeed.sol';
import './interfaces/IVaultFactory.sol';
import './interfaces/IVaultFactoryConfig.sol';
import './interfaces/ILiquidationRouter.sol';

import './utils/constants.sol';
import './interfaces/ITokenPriceFeed.sol';
import './interfaces/IVaultExtraSettings.sol';
import './utils/linked-address-list.sol';

/**
 * @title Vault
 * @dev Manages creation, collateralization, borrowing, and liquidation of Vaults.
 */
contract Vault is Context, Constants {
    string public constant VERSION = '1.2.0';

    // Events emitted by the contract
    event CollateralAdded(
        address indexed collateral,
        uint256 amount,
        uint256 newTotalAmount
    );
    event CollateralRemoved(
        address indexed collateral,
        uint256 amount,
        uint256 newTotalAmount
    );
    event CollateralRedeemed(
        address indexed collateral,
        uint256 amount,
        uint256 newTotalAmount,
        uint256 stableAmountUsed,
        uint256 feePaid
    );

    event DebtAdded(uint256 amount, uint256 newTotalDebt);
    event DebtRepaid(uint256 amount, uint256 newTotalDebt);

    modifier onlyFactory() {
        require(_msgSender() == factory, 'only-factory');
        _;
    }

    using SafeERC20 for IERC20;
    using EnumerableSet for EnumerableSet.AddressSet;

    address public immutable stable;
    address public immutable factory;
    address public vaultOwner;

    string public name;

    EnumerableSet.AddressSet private collateralSet;
    EnumerableSet.AddressSet private operators;

    IVaultExtraSettings public vaultExtraSettings;
    mapping(address => uint256) public collateral;

    uint256 public debt;

    modifier onlyVaultOwner() {
        require(_msgSender() == vaultOwner, 'only-vault-owner');
        _;
    }

    /**
     * @dev Constructor to initialize the Vault contract.
     * @param _factory Address of the VaultFactory contract.
     * @param _vaultOwner Address of the initial owner of the Vault.
     * @param _name Name of the Vault.
     */
    constructor(
        address _factory,
        address _vaultOwner,
        string memory _name,
        IVaultExtraSettings _vaultExtraSettings
    ) {
        require(_vaultOwner != address(0x0), 'vault-owner-is-0');
        require(bytes(_name).length > 0, 'name-is-empty');
        require(_factory != address(0x0), 'factory-is-0');
        require(
            address(_vaultExtraSettings) != address(0x0),
            'vault-extra-settings-is-0'
        );

        factory = _factory;
        vaultOwner = _vaultOwner;
        stable = IVaultFactory(factory).stable();
        name = _name;
        vaultExtraSettings = _vaultExtraSettings;
    }

    /**
     * @dev Transfers ownership of the Vault to a new owner.
     * @param _newOwner Address of the new owner.
     */
    function transferVaultOwnership(address _newOwner) external onlyFactory {
        vaultOwner = _newOwner;
    }

    /**
     * @dev Sets a new name for the Vault.
     * @param _name New name for the Vault.
     */
    function setName(string memory _name) external onlyVaultOwner {
        require(bytes(_name).length > 0, 'name-is-empty');
        name = _name;
    }

    /**
     * @dev Adds an operator to the Vault, allowing them certain permissions.
     * @param _operator Address of the operator to be added.
     */
    function addOperator(address _operator) external onlyVaultOwner {
        require(_operator != address(0x0), 'operator-is-0');
        operators.add(_operator);
    }

    /**
     * @dev Removes an operator from the Vault, revoking their permissions.
     * @param _operator Address of the operator to be removed.
     */
    function removeOperator(address _operator) external onlyVaultOwner {
        require(_operator != address(0x0), 'operator-is-0');
        operators.remove(_operator);
    }

    /**
     * @dev Checks if an address is an operator for this Vault.
     * @param _operator Address to check.
     * @return Boolean indicating whether the address is an operator.
     */
    function isOperator(address _operator) external view returns (bool) {
        return operators.contains(_operator);
    }

    /**
     * @dev Returns the number of operators in the Vault.
     * @return Length of the operators set.
     */
    function operatorsLength() external view returns (uint256) {
        return operators.length();
    }

    /**
     * @dev Returns the operator at a given index in the operators set.
     * @param _index Index of the operator.
     * @return Address of the operator at the given index.
     */
    function operatorAt(uint256 _index) external view returns (address) {
        return operators.at(_index);
    }

    /**
     * @dev Checks if a collateral token is added to the Vault.
     * @param _collateral Address of the collateral token to check.
     * @return Boolean indicating whether the collateral token is added.
     */
    function containsCollateral(
        address _collateral
    ) external view returns (bool) {
        return collateralSet.contains(_collateral);
    }

    /**
     * @dev Returns the number of collateral tokens added to the Vault.
     * @return Length of the collateral set.
     */
    function collateralsLength() external view returns (uint256) {
        return collateralSet.length();
    }

    /**
     * @dev Returns the collateral token address at a given index in the collateral set.
     * @param _index Index of the collateral token.
     * @return Address of the collateral token at the given index.
     */
    function collateralAt(uint256 _index) external view returns (address) {
        return collateralSet.at(_index);
    }

    /**
     * @dev Returns an array containing all collateral token addresses in the Vault.
     * @return Array of collateral token addresses.
     */
    function collaterals() external view returns (address[] memory) {
        address[] memory _collaterals = new address[](collateralSet.length());
        for (uint256 i = 0; i < collateralSet.length(); i++) {
            _collaterals[i] = collateralSet.at(i);
        }
        return _collaterals;
    }

    /**
     * @dev Adds a new collateral token to the Vault and updates the collateral amount.
     * @param _collateral Address of the collateral token to add.
     * @param _amount Amount of the collateral token to add.
     */
    function addCollateral(
        address _collateral,
        uint256 _amount
    ) external onlyFactory {
        require(_collateral != address(0x0), 'collateral-is-0');
        require(_amount > 0, 'amount-is-0');

        collateralSet.add(_collateral);
        uint256 _maxTokens = IVaultFactory(factory).MAX_TOKENS_PER_VAULT();
        require(collateralSet.length() <= _maxTokens, 'max-tokens-reached');

        collateral[_collateral] += _amount;

        emit CollateralAdded(_collateral, _amount, collateral[_collateral]);
    }

    /**
     * @dev Removes a collateral token from the Vault and transfers it back to the sender.
     * @param _collateral Address of the collateral token to remove.
     * @param _amount Amount of the collateral token to remove.
     * @param _to Address to receive the removed collateral.
     */
    function removeCollateral(
        address _collateral,
        uint256 _amount,
        address _to
    ) external onlyFactory {
        require(_collateral != address(0x0), 'collateral-is-0');
        require(_amount > 0, 'amount-is-0');

        collateral[_collateral] -= _amount;
        if (collateral[_collateral] == 0) {
            collateralSet.remove(_collateral);
        }

        uint256 _healthFactor = healthFactor(false);
        require(_healthFactor >= DECIMAL_PRECISION, 'health-factor-below-1');

        IERC20(_collateral).safeTransfer(_to, _amount);

        emit CollateralRemoved(_collateral, _amount, collateral[_collateral]);
    }

    /**
     * @dev Adds bad debt to the Vault.
     * @param _amount Amount of bad debt to add.
     */
    function addBadDebt(uint256 _amount) external onlyFactory {
        require(_amount > 0, 'amount-is-0');

        debt += _amount;
        emit DebtAdded(_amount, debt);
    }

    /**
     * @dev Calculates the maximum borrowable amount and the current borrowable amount.
     * @return _maxBorrowable Maximum borrowable amount.
     * @return _borrowable Current borrowable amount.
     */
    function borrowable()
        public
        view
        returns (uint256 _maxBorrowable, uint256 _borrowable)
    {
        (_maxBorrowable, _borrowable) = borrowableWithDiff(
            address(0x0),
            0,
            false,
            false
        );
    }

    /**
     * @dev Borrows a specified amount from the Vault.
     * @param _amount Amount to borrow.
     */
    function borrow(uint256 _amount) external onlyFactory {
        require(_amount > 0, 'amount-is-0');

        (uint256 _maxBorrowable, uint256 _borrowable) = borrowable();
        require(_amount <= _borrowable, 'not-enough-borrowable');

        debt += _amount;
        require(debt <= _maxBorrowable, 'max-borrowable-reached');

        emit DebtAdded(_amount, debt);
    }

    /**
     * @dev Repays a specified amount to the Vault's debt.
     * @param _amount Amount to repay.
     */
    function repay(uint256 _amount) external onlyFactory {
        require(_amount <= debt, 'amount-exceeds-debt');

        debt -= _amount;
        emit DebtRepaid(_amount, debt);
    }

    /**
     * @dev Calculates the stable amount needed and the redemption fee for redeeming collateral.
     * @param _collateral Address of the collateral token.
     * @param _collateralAmount Amount of collateral to redeem.
     * @return _stableAmountNeeded Stablecoin amount required to redeem collateral.
     * @return _redemptionFee Fee charged for the redemption.
     */
    function calcRedeem(
        address _collateral,
        uint256 _collateralAmount
    )
        public
        view
        returns (uint256 _stableAmountNeeded, uint256 _redemptionFee)
    {
        ITokenPriceFeed _priceFeed = ITokenPriceFeed(
            IVaultFactory(factory).priceFeed()
        );
        uint256 _price = _priceFeed.tokenPrice(_collateral);

        uint256 _normalizedCollateralAmount = _collateralAmount *
            (10 ** (18 - _priceFeed.decimals(_collateral)));
        _stableAmountNeeded =
            (_normalizedCollateralAmount * _price) /
            DECIMAL_PRECISION;

        (, , uint256 _redemptionKickbackRate) = vaultExtraSettings
            .getExtraSettings();

        if (_redemptionKickbackRate > 0) {
            uint256 _kickbackAmount = (_stableAmountNeeded *
                _redemptionKickbackRate) / DECIMAL_PRECISION;
            _stableAmountNeeded += _kickbackAmount;
        }
        uint256 _redemptionRate = IVaultFactoryConfig(factory).redemptionRate();
        _redemptionFee =
            (_stableAmountNeeded * _redemptionRate) /
            DECIMAL_PRECISION;
    }

    /**
     * @dev Redeems a specified amount of collateral, repays debt, and transfers collateral back to the redeemer.
     * @param _collateral Address of the collateral token to redeem.
     * @param _collateralAmount Amount of collateral to redeem.
     * @return _debtRepaid Amount of debt repaid.
     * @return _feeCollected Fee collected for the redemption.
     */
    function redeem(
        address _collateral,
        uint256 _collateralAmount
    )
        external
        onlyFactory
        returns (uint256 _debtRepaid, uint256 _feeCollected)
    {
        require(_collateral != address(0x0), 'collateral-is-0');
        require(_collateralAmount > 0, 'amount-is-0');
        require(collateralSet.contains(_collateral), 'collateral-not-added');
        require(
            collateral[_collateral] >= _collateralAmount,
            'not-enough-collateral'
        );

        uint256 _currentHealthFactor = healthFactor(true);
        uint256 _redemptionHealthFactorLimit = IVaultFactoryConfig(factory)
            .redemptionHealthFactorLimit();
        require(
            _currentHealthFactor < _redemptionHealthFactorLimit,
            'health-factor-above-redemption-limit'
        );

        (uint256 _debtTreshold, uint256 _maxRedeemablePercentage, ) = vaultExtraSettings
            .getExtraSettings();

        
        collateral[_collateral] -= _collateralAmount;
        (_debtRepaid, _feeCollected) = calcRedeem(
            _collateral,
            _collateralAmount
        );

        
        if (debt > _debtTreshold) {
            uint256 _redeemableDebt = (debt * _maxRedeemablePercentage) /
                DECIMAL_PRECISION;
            require(_debtRepaid <= _redeemableDebt, 'redeemable-debt-exceeded');

        }

        debt -= _debtRepaid;

        if (collateral[_collateral] == 0) {
            collateralSet.remove(_collateral);
        }

        IERC20(_collateral).safeTransfer(_msgSender(), _collateralAmount);

        emit CollateralRedeemed(
            _collateral,
            _collateralAmount,
            collateral[_collateral],
            _debtRepaid,
            _feeCollected
        );
        emit DebtRepaid(_debtRepaid, debt);
    }

    /**
     * @dev Computes the health factor of the Vault.
     * @param _useMlr Flag to use Minimum Loan Ratio (MLR) in health factor computation.
     * @return _healthFactor Current health factor.
     */
    function healthFactor(
        bool _useMlr
    ) public view returns (uint256 _healthFactor) {
        if (debt == 0) {
            return type(uint256).max;
        }

        (uint256 _maxBorrowable, ) = borrowableWithDiff(
            address(0x0),
            0,
            false,
            _useMlr
        );

        _healthFactor = (_maxBorrowable * DECIMAL_PRECISION) / debt;
    }

    /**
     * @dev Computes a new health factor given a new debt value.
     * @param _newDebt New debt amount to calculate the health factor.
     * @param _useMlr Flag to use Minimum Loan Ratio (MLR) in health factor computation.
     * @return _newHealthFactor Calculated new health factor based on the new debt value.
     */
    function newHealthFactor(
        uint256 _newDebt,
        bool _useMlr
    ) public view returns (uint256 _newHealthFactor) {
        if (_newDebt == 0) {
            return type(uint256).max;
        }

        (uint256 _maxBorrowable, ) = borrowableWithDiff(
            address(0x0),
            0,
            false,
            _useMlr
        );
        _newHealthFactor = (_maxBorrowable * DECIMAL_PRECISION) / _newDebt;
    }

    /**
     * @dev Computes the maximum borrowable amount and the current borrowable amount.
     * @param _collateral Address of the collateral token (0x0 for total vault borrowable).
     * @param _diffAmount Difference in collateral amount when adding/removing collateral.
     * @param _isAdd Flag indicating whether the collateral is added or removed.
     * @param _useMlr Flag to use Minimum Loan Ratio (MLR) in borrowable computation.
     * @return _maxBorrowable Maximum borrowable amount.
     * @return _borrowable Current borrowable amount based on the collateral.
     */
    function borrowableWithDiff(
        address _collateral,
        uint256 _diffAmount,
        bool _isAdd,
        bool _useMlr
    ) public view returns (uint256 _maxBorrowable, uint256 _borrowable) {
        uint256 _newCollateralAmount = collateral[_collateral];

        uint256 _borrowableAmount = 0;

        if (_collateral != address(0x0)) {
            require(
                IVaultFactory(factory).isCollateralSupported(_collateral),
                'collateral-not-supported'
            );
            if (_isAdd) {
                _newCollateralAmount += _diffAmount;
            } else {
                _newCollateralAmount -= _diffAmount;
            }
        }

        ITokenPriceFeed _priceFeed = ITokenPriceFeed(
            IVaultFactory(factory).priceFeed()
        );

        for (uint256 i = 0; i < collateralSet.length(); i++) {
            address _c = collateralSet.at(i);
            uint256 _collateralAmount = _c == _collateral
                ? _newCollateralAmount
                : collateral[_c];
            uint256 _price = _priceFeed.tokenPrice(_c);
            uint256 _divisor = _useMlr
                ? _priceFeed.mlr(_c)
                : _priceFeed.mcr(_c);
            uint256 _normalizedCollateralAmount = _collateralAmount *
                (10 ** (18 - _priceFeed.decimals(_c)));

            uint256 _collateralBorrowable = (_normalizedCollateralAmount *
                _price) / DECIMAL_PRECISION;

            _borrowableAmount +=
                (_collateralBorrowable * DECIMAL_PRECISION) /
                _divisor;
        }

        return (
            _borrowableAmount,
            (_borrowableAmount > debt) ? _borrowableAmount - debt : 0
        );
    }

    /**
     * @dev Liquidates the vault by repaying all debts with seized collateral.
     * @return _forgivenDebt Amount of debt forgiven during liquidation.
     */
    function liquidate() external onlyFactory returns (uint256 _forgivenDebt) {
        require(
            healthFactor(true) < DECIMAL_PRECISION,
            'liquidation-factor-above-1'
        );

        uint256 _debt = debt;
        debt = 0;
        ILiquidationRouter router = ILiquidationRouter(
            IVaultFactory(factory).liquidationRouter()
        );
        for (uint256 i = 0; i < collateralSet.length(); i++) {
            address _collateral = collateralSet.at(i);
            uint256 _collateralAmount = collateral[_collateral];
            uint256 _actualCollateralBalance = IERC20(_collateral).balanceOf(
                address(this)
            );
            if (_actualCollateralBalance < _collateralAmount) {
                _collateralAmount = _actualCollateralBalance;
            }
            collateral[_collateral] = 0;

            IERC20(_collateral).safeApprove(
                IVaultFactory(factory).liquidationRouter(),
                0
            );
            IERC20(_collateral).safeApprove(
                IVaultFactory(factory).liquidationRouter(),
                _collateralAmount
            );

            router.addSeizedCollateral(_collateral, _collateralAmount);
        }
        router.addUnderWaterDebt(address(this), _debt);
        router.tryLiquidate();
        _forgivenDebt = _debt;
    }
}

File 35 of 38 : VaultDeployer.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import './Vault.sol';
import './interfaces/IVaultExtraSettings.sol';
/**
 * @title VaultDeployer
 * @notice A contract responsible for deploying new instances of the Vault contract.
 */
contract VaultDeployer {
    IVaultExtraSettings public immutable vaultExtraSettings;

    constructor(address _vaultExtraSettings) {
        require(
            _vaultExtraSettings != address(0x0),
            'vault-extra-settings-is-zero'
        );
        vaultExtraSettings = IVaultExtraSettings(_vaultExtraSettings);
    }
    /**
     * @notice Deploys a new Vault contract.
     * @param _factory The address of the factory contract managing the vaults.
     * @param _vaultOwner The address of the intended owner of the new vault.
     * @param _name The name of the new vault.
     * @return The address of the newly created Vault contract.
     */
    function deployVault(
        address _factory,
        address _vaultOwner,
        string memory _name
    ) external returns (address) {
        // Deploy a new instance of the Vault contract
        Vault vault = new Vault(
            _factory,
            _vaultOwner,
            _name,
            vaultExtraSettings
        );
        return address(vault);
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;

import '@openzeppelin/contracts/interfaces/IERC20Metadata.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';
import '@openzeppelin/contracts/security/ReentrancyGuard.sol';

import './utils/linked-address-list.sol';
import './Vault.sol';
import './VaultFactoryConfig.sol';
import './VaultFactoryList.sol';
import './interfaces/IWETH.sol';
import './interfaces/ITokenPriceFeed.sol';
import './interfaces/IPriceFeed.sol';
import './interfaces/IMintableTokenOwner.sol';
import './interfaces/IMintableToken.sol';
import './interfaces/IVaultDeployer.sol';
import './interfaces/IVaultBorrowRate.sol';

/**
 * @title VaultFactory
 * @dev Manages the creation, configuration, and operations of Vaults with collateral and borrowing functionality.
 */
contract VaultFactory is ReentrancyGuard, VaultFactoryConfig, VaultFactoryList {
    // Events emitted by the contract
    event NewVault(address indexed vault, string name, address indexed owner);
    event VaultOwnerChanged(
        address indexed vault,
        address indexed oldOwner,
        address indexed newOwner
    );

    // Libraries used by the contract
    using LinkedAddressList for LinkedAddressList.List;
    using SafeERC20 for IERC20;
    using SafeERC20 for IMintableToken;

    // Immutable state variables
    address public immutable stable;
    address public immutable nativeWrapped;
    IMintableTokenOwner public immutable mintableTokenOwner;

    // State variables
    mapping(address => uint256) public collateral;
    uint256 public totalDebt;

    /**
     * @dev Constructor to initialize essential addresses and contracts for VaultFactory.
     * @param _mintableTokenOwner Address of the Mintable Token Owner contract.
     * @param _nativeWrapped Address of the native wrapped token.
     * @param _priceFeed Address of the price feed contract.
     * @param _vaultDeployer Address of the Vault Deployer contract.
     * @param _liquidationRouter Address of the liquidation router contract.
     * @param _borrowRate Address of the borrow rate contract.
     */
    constructor(
        address _mintableTokenOwner,
        address _nativeWrapped,
        address _priceFeed,
        address _vaultDeployer,
        address _liquidationRouter,
        address _borrowRate
    ) VaultFactoryConfig(_vaultDeployer, _liquidationRouter) {
        require(
            _mintableTokenOwner != address(0x0),
            'mintable-token-owner-is-0'
        );

        mintableTokenOwner = IMintableTokenOwner(_mintableTokenOwner);
        stable = address(mintableTokenOwner.token());

        require(stable != address(0x0), 'stable-is-0');
        require(_nativeWrapped != address(0x0), 'nativew-is-0');
        require(_priceFeed != address(0x0), 'pricefeed-is-0');
        require(_borrowRate != address(0x0), 'borrow-rate-is-0');

        borrowRate = _borrowRate;
        nativeWrapped = _nativeWrapped;
        priceFeed = _priceFeed;
    }

    /**
     * @dev Fallback function to receive Ether and restricts its usage to a designated sender.
     */
    receive() external payable {
        require(msg.sender == nativeWrapped, 'only-native-wrapped');
    }

    /**
     * @dev Modifier: Allows function execution only by the owner of a specific vault.
     * @param _vault The address of the vault to check ownership.
     */
    modifier onlyVaultOwner(address _vault) {
        require(Vault(_vault).vaultOwner() == _msgSender(), 'only-vault-owner');
        _;
    }

    /**
     * @dev Modifier: Allows function execution by the owner or an operator of a specific vault.
     * @param _vault The address of the vault to check ownership or operator status.
     */
    modifier onlyVaultOwnerOrOperator(address _vault) {
        require(
            Vault(_vault).vaultOwner() == _msgSender() ||
                Vault(_vault).isOperator(_msgSender()),
            'only-vault-owner-or-operator'
        );
        _;
    }

    /**
     * @dev Modifier: Allows function execution only by the liquidation router.
     */
    modifier onlyLiquidationRouter() {
        require(liquidationRouter == _msgSender(), 'only-liquidation-router');
        _;
    }

    /**
     * @dev Checks if a given collateral token is supported.
     * @param _collateral The address of the collateral token.
     * @return A boolean indicating whether the collateral token is supported.
     */
    function isCollateralSupported(
        address _collateral
    ) external view returns (bool) {
        return _isCollateralSupported(_collateral);
    }

    /**
     * @dev Transfers ownership of a vault to a new owner.
     * @param _vault The address of the vault to transfer ownership.
     * @param _newOwner The address of the new owner to receive the vault ownership.
     */
    function transferVaultOwnership(
        address _vault,
        address _newOwner
    ) external onlyVaultOwner(_vault) {
        address _msgSender = _msgSender();
        require(_newOwner != address(0x0), 'new-owner-is-0');
        require(containsVault(_vault), 'vault-not-found');

        emit VaultOwnerChanged(_vault, _msgSender, _newOwner);
        Vault(_vault).transferVaultOwnership(_newOwner);
        _transferVault(_msgSender, _newOwner, _vault);
    }

    /**
     * @dev Creates a new vault with a specified name.
     * @param _name The name of the new vault.
     * @return The address of the newly created vault.
     */
    function createVault(string memory _name) public returns (address) {
        address _msgSender = _msgSender();
        address _vaultAddress = IVaultDeployer(vaultDeployer).deployVault(
            address(this),
            _msgSender,
            _name
        );
        _addVault(_msgSender, _vaultAddress);
        emit NewVault(_vaultAddress, _name, _msgSender);

        return _vaultAddress;
    }

    /**
     * @dev Checks if a specific collateral token is supported for the vault.
     * @param _collateral The address of the collateral token to check.
     * @return A boolean indicating whether the collateral token is supported.
     */
    function _isCollateralSupported(
        address _collateral
    ) internal view returns (bool) {
        ITokenPriceFeed _priceFeed = ITokenPriceFeed(priceFeed);
        return (_priceFeed.tokenPriceFeed(_collateral) != address(0x0));
    }

    /**
     * @dev Adds native-wrapped collateral to a specific vault.
     * @param _vault The address of the vault to add collateral.
     */
    function addCollateralNative(address _vault) external payable {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(nativeWrapped),
            'collateral-not-supported'
        );
        uint256 _amount = msg.value;

        collateral[nativeWrapped] += _amount;

        require(
            collateral[nativeWrapped] <= collateralCap[nativeWrapped],
            'collateral-cap-reached'
        );

        IWETH(nativeWrapped).deposit{value: _amount}();
        IERC20(nativeWrapped).safeTransferFrom(address(this), _vault, _amount);

        Vault(_vault).addCollateral(nativeWrapped, _amount);
    }

    /**
     * @dev Removes native-wrapped collateral from a specific vault.
     * @param _vault The address of the vault to remove collateral.
     * @param _amount The amount of collateral to be removed.
     * @param _to The address where the removed collateral is transferred.
     */
    function removeCollateralNative(
        address _vault,
        uint256 _amount,
        address _to
    ) external onlyVaultOwner(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(nativeWrapped),
            'collateral-not-supported'
        );

        Vault(_vault).removeCollateral(nativeWrapped, _amount, address(this));

        collateral[nativeWrapped] -= _amount;

        IWETH(nativeWrapped).withdraw(_amount);
        (bool success, ) = payable(_to).call{value: _amount}("");
        require(success, 'transfer-failed');
    }

    /**
     * @dev Adds a specific collateral to a vault.
     * @param _vault The address of the vault to add collateral.
     * @param _collateral The address of the collateral token to add.
     * @param _amount The amount of collateral to add.
     */
    function addCollateral(
        address _vault,
        address _collateral,
        uint256 _amount
    ) external {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(_collateral),
            'collateral-not-supported'
        );

        collateral[_collateral] += _amount;

        require(
            collateral[_collateral] <= collateralCap[_collateral],
            'collateral-cap-reached'
        );

        IERC20(_collateral).safeTransferFrom(_msgSender(), _vault, _amount);
        Vault(_vault).addCollateral(_collateral, _amount);
    }

    /**
     * @dev Removes a specific collateral from a vault.
     * @param _vault The address of the vault to remove collateral.
     * @param _collateral The address of the collateral token to remove.
     * @param _amount The amount of collateral to remove.
     * @param _to The address where the removed collateral is transferred.
     */
    function removeCollateral(
        address _vault,
        address _collateral,
        uint256 _amount,
        address _to
    ) external onlyVaultOwner(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(_collateral),
            'collateral-not-supported'
        );

        collateral[_collateral] -= _amount;
        Vault(_vault).removeCollateral(_collateral, _amount, _to);
    }

    /**
     * @dev Borrows funds from a vault by its owner or an operator.
     * @param _vault The address of the vault from which funds are borrowed.
     * @param _amount The amount of funds to borrow.
     * @param _to The address where borrowed funds are sent.
     */
    function borrow(
        address _vault,
        uint256 _amount,
        address _to
    ) external onlyVaultOwnerOrOperator(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(_to != address(0x0), 'to-is-0');

        totalDebt += _amount;
        _updateDebtWindow(_amount);
        Vault(_vault).borrow(_amount);
        uint256 _borrowRate = IVaultBorrowRate(borrowRate).getBorrowRate(
            _vault
        );
        uint256 _feeAmount = (_amount * _borrowRate) / DECIMAL_PRECISION;

        mintableTokenOwner.mint(_to, _amount - _feeAmount);
        mintableTokenOwner.mint(borrowFeeRecipient, _feeAmount);
    }

    /**
     * @dev Distributes bad debt to a specific vault.
     * @param _vault The address of the vault to distribute bad debt.
     * @param _amount The amount of bad debt to be distributed.
     */
    function distributeBadDebt(
        address _vault,
        uint256 _amount
    ) external nonReentrant onlyLiquidationRouter {
        require(containsVault(_vault), 'vault-not-found');
        totalDebt += _amount;
        Vault(_vault).addBadDebt(_amount);
    }

    /**
     * @dev Closes a vault if it meets specific conditions.
     * @param _vault The address of the vault to close.
     */
    function closeVault(address _vault) external onlyVaultOwner(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(Vault(_vault).debt() == 0, 'debt-not-0');
        require(Vault(_vault).collateralsLength() == 0, 'collateral-not-0');

        _removeVault(_msgSender(), _vault);
    }

    /**
     * @dev Repays borrowed funds for a specific vault.
     * @param _vault The address of the vault for which funds are repaid.
     * @param _amount The amount of funds to repay.
     */
    function repay(address _vault, uint256 _amount) external {
        require(containsVault(_vault), 'vault-not-found');
        totalDebt -= _amount;
        Vault(_vault).repay(_amount);

        IMintableToken(stable).safeTransferFrom(
            _msgSender(),
            address(this),
            _amount
        );
        IMintableToken(stable).burn(_amount);
    }

    /**
     * @dev Redeems collateral from a vault after meeting specific conditions.
     * @param _vault The address of the vault from which collateral is redeemed.
     * @param _collateral The address of the collateral token to redeem.
     * @param _collateralAmount The amount of collateral to redeem.
     * @param _to The address where the redeemed collateral is transferred.
     */
    function redeem(
        address _vault,
        address _collateral,
        uint256 _collateralAmount,
        address _to
    ) external nonReentrant {
        require(containsVault(_vault), 'vault-not-found');
        require(_to != address(0x0), 'to-is-0');

        require(isReedemable(_vault, _collateral), 'not-redeemable');

        (uint256 _debtRepaid, uint256 _feeCollected) = Vault(_vault).redeem(
            _collateral,
            _collateralAmount
        );

        totalDebt -= _debtRepaid;
        collateral[_collateral] -= _collateralAmount;

        IMintableToken(stable).safeTransferFrom(
            _msgSender(),
            address(this),
            _debtRepaid + _feeCollected
        );
        IMintableToken(stable).burn(_debtRepaid);
        IMintableToken(stable).transfer(redemptionFeeRecipient, _feeCollected);

        IERC20(_collateral).safeTransfer(_to, _collateralAmount);
    }

    /**
     * @dev Liquidates a specific vault if it is eligible for liquidation.
     * @param _vault The address of the vault to be liquidated.
     */
    function liquidate(address _vault) external nonReentrant {
        require(containsVault(_vault), 'vault-not-found');

        address _vaultOwner = Vault(_vault).vaultOwner();
        uint256 _forgivenDebt = Vault(_vault).liquidate();

        totalDebt -= _forgivenDebt;

        _removeVault(_vaultOwner, _vault);
    }

    /**
     * @dev Checks if a vault is eligible for liquidation.
     * @param _vault The address of the vault to check for liquidation eligibility.
     * @return A boolean indicating whether the vault is liquidatable.
     */
    function isLiquidatable(address _vault) external view returns (bool) {
        require(containsVault(_vault), 'vault-not-found');
        return Vault(_vault).healthFactor(true) < DECIMAL_PRECISION;
    }

    /**
     * @dev Checks if a specific collateral can be redeemed from a vault based on conditions.
     * @param _vault The address of the vault to check for collateral redemption.
     * @param _collateral The address of the collateral token to check for redemption.
     * @notice Collateral with higher MCR can be redeemed first
     * @return A boolean indicating whether the collateral is redeemable.
     */
    function isReedemable(
        address _vault,
        address _collateral
    ) public view returns (bool) {
        require(
            _isCollateralSupported(_collateral),
            'collateral-not-supported'
        );
        if (!Vault(_vault).containsCollateral(_collateral)) {
            return false;
        }
        uint256 _healthFactor = Vault(_vault).healthFactor(false);
        if (_healthFactor >= redemptionHealthFactorLimit) {
            return false;
        }

        ITokenPriceFeed _priceFeed = ITokenPriceFeed(priceFeed);
        uint256 _collateralMcr = _priceFeed.mcr(_collateral);

        address[] memory _collaterals = Vault(_vault).collaterals();
        uint256 _length = _collaterals.length;

        for (uint256 i; i < _length; i++) {
            if (_collaterals[i] != _collateral) {
                uint256 _mcr = _priceFeed.mcr(_collaterals[i]);
                if (_mcr > _collateralMcr) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * @dev Updates the debt window with the newly incurred debt.
     * @param _newDebt The amount of new debt to update in the debt window.
     */
    function _updateDebtWindow(uint256 _newDebt) internal {
        require(totalDebt <= debtCeiling, 'debt-ceiling-reached');

        if (block.timestamp > lastDebtWindow + debtWindowSize) {
            debtWindowAmount = _newDebt;
            lastDebtWindow = block.timestamp;
        } else {
            debtWindowAmount += _newDebt;
        }
        require(
            debtWindowAmount <= maxDebtPerWindow,
            'debt-window-amount-reached'
        );
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;
import './utils/constants.sol';
import '@openzeppelin/contracts/access/Ownable.sol';

abstract contract VaultFactoryConfig is Constants, Ownable {
    event PriceFeedUpdated(address indexed priceFeed);
    event MaxTokensPerVaultUpdated(
        uint256 oldMaxTokensPerVault,
        uint256 newMaxTokensPerVault
    );
    event RedemptionRateUpdated(
        uint256 oldRedemptionRate,
        uint256 newRedemptionRate
    );
    event BorrowRateUpdated(address oldBorrowRate, address newBorrowRate);
    event RedemptionHealthFactorLimitUpdated(
        uint256 oldRedemptionHealthFactorLimit,
        uint256 newRedemptionHealthFactorLimit
    );
    event DebtCeilingUpdated(uint256 oldDebtCeiling, uint256 newDebtCeiling);
    event MaxDebtPerWindowUpdated(
        uint256 oldMaxDebtPerWindow,
        uint256 newMaxDebtPerWindow
    );
    event DebtWindowSizeUpdated(
        uint256 oldDebtWindowSize,
        uint256 newDebtWindowSize
    );
    event CollateralCapacityUpdated(
        address indexed collateral,
        uint256 oldCapacity,
        uint256 newCapacity
    );
    event liquidationRouterUpdated(address indexed liquidationRouter);

    // Various configuration parameters
    address public priceFeed;
    address public borrowRate;

    uint256 public MAX_TOKENS_PER_VAULT = 5;
    uint256 public redemptionRate = PERCENT_05; // 0.5%

    uint256 public redemptionHealthFactorLimit = 1.5 ether; // 2.0 HF

    address public borrowFeeRecipient;
    address public redemptionFeeRecipient;

    mapping(address => uint256) public collateralCap;

    uint256 public debtCeiling = type(uint256).max; // max stablecoin debt issued by the protocol

    uint256 public maxDebtPerWindow = 2000 ether; // 1M
    uint256 public debtWindowSize = 1 hours;
    uint256 public lastDebtWindow;
    uint256 public debtWindowAmount;

    address public vaultDeployer;
    address public liquidationRouter;

    /**
     * @dev Set the address for the Vault Deployer
     * @param _vaultDeployer Address of the Vault Deployer
     */
    function setVaultDeployer(address _vaultDeployer) external onlyOwner {
        require(_vaultDeployer != address(0x0), 'vault-deployer-is-0');
        vaultDeployer = _vaultDeployer;
    }

    /**
     * @dev Set the address for the Liquidation Router
     * @param _liquidationRouter Address of the Liquidation Router
     */
    function setLiquidationRouter(
        address _liquidationRouter
    ) external onlyOwner {
        require(_liquidationRouter != address(0x0), 'liquidation-router-is-0');
        liquidationRouter = _liquidationRouter;
        emit liquidationRouterUpdated(_liquidationRouter);
    }

    /**
     * @dev Set the collateral capacity for a specific collateral token
     * @param _collateral Address of the collateral token
     * @param _cap The new capacity for the collateral token
     */
    function setCollateralCapacity(
        address _collateral,
        uint256 _cap
    ) external onlyOwner {
        require(_collateral != address(0x0), 'collateral-is-0');
        emit CollateralCapacityUpdated(
            _collateral,
            collateralCap[_collateral],
            _cap
        );
        collateralCap[_collateral] = _cap;
    }

    /**
     * @dev Set the debt ceiling value.
     * @param _debtCeiling The new debt ceiling value to be set.
     */
    function setDebtCeiling(uint256 _debtCeiling) external onlyOwner {
        emit DebtCeilingUpdated(debtCeiling, _debtCeiling);
        debtCeiling = _debtCeiling;
    }

    /**
     * @dev Set the maximum debt allowed per window.
     * @param _maxDebtPerWindow The new maximum debt per window value to be set.
     */
    function setMaxDebtPerWindow(uint256 _maxDebtPerWindow) external onlyOwner {
        emit MaxDebtPerWindowUpdated(maxDebtPerWindow, _maxDebtPerWindow);
        maxDebtPerWindow = _maxDebtPerWindow;
    }

    /**
     * @dev Set the window size for debt.
     * @param _debtWindowSize The new debt window size value to be set.
     */
    function setDebtWindowSize(uint256 _debtWindowSize) external onlyOwner {
        emit DebtWindowSizeUpdated(debtWindowSize, _debtWindowSize);
        debtWindowSize = _debtWindowSize;
    }

    /**
     * @dev Set the maximum tokens allowed per vault.
     * @param _maxTokensPerVault The new maximum tokens per vault value to be set.
     */
    function setMaxTokensPerVault(
        uint256 _maxTokensPerVault
    ) external onlyOwner {
        require(_maxTokensPerVault > 0, 'max-tokens-per-vault-is-0');
        emit MaxTokensPerVaultUpdated(MAX_TOKENS_PER_VAULT, _maxTokensPerVault);
        MAX_TOKENS_PER_VAULT = _maxTokensPerVault;
    }

    /**
     * @dev Set the address for the price feed.
     * @param _priceFeed Address of the new price feed contract.
     */
    function setPriceFeed(address _priceFeed) external onlyOwner {
        require(_priceFeed != address(0x0), 'pricefeed-is-0');
        priceFeed = _priceFeed;
        emit PriceFeedUpdated(_priceFeed);
    }

    /**
     * @dev Set the redemption rate for the protocol.
     * @param _redemptionRate The new redemption rate value to be set.
     */
    function setRedemptionRate(uint256 _redemptionRate) external onlyOwner {
        require(
            _redemptionRate <= MAX_REDEMPTION_RATE,
            'redemption-rate-too-high'
        );
        emit RedemptionRateUpdated(redemptionRate, _redemptionRate);
        redemptionRate = _redemptionRate;
    }

    /**
     * @dev Set the address for the borrow rate.
     * @param _borrowRate Address of the new borrow rate contract.
     */
    function setBorrowRate(address _borrowRate) external onlyOwner {
        require(_borrowRate != address(0), 'borrow-rate-is-0');
        emit BorrowRateUpdated(borrowRate, _borrowRate);
        borrowRate = _borrowRate;
    }

    /**
     * @dev Set the redemption health factor limit.
     * @param _redemptionHealthFactorLimit The new redemption health factor limit to be set.
     */
    function setRedemptionHealthFactorLimit(
        uint256 _redemptionHealthFactorLimit
    ) external onlyOwner {
        emit RedemptionHealthFactorLimitUpdated(
            redemptionHealthFactorLimit,
            _redemptionHealthFactorLimit
        );
        redemptionHealthFactorLimit = _redemptionHealthFactorLimit;
    }

    /**
     * @dev Set the address for the borrow fee recipient.
     * @param _borrowFeeRecipient Address of the new borrow fee recipient.
     */
    function setBorrowFeeRecipient(
        address _borrowFeeRecipient
    ) external onlyOwner {
        require(
            _borrowFeeRecipient != address(0x0),
            'borrow-fee-recipient-is-0'
        );
        borrowFeeRecipient = _borrowFeeRecipient;
    }

    /**
     * @dev Set the address for the redemption fee recipient.
     * @param _redemptionFeeRecipient Address of the new redemption fee recipient.
     */
    function setRedemptionFeeRecipient(
        address _redemptionFeeRecipient
    ) external onlyOwner {
        require(
            _redemptionFeeRecipient != address(0x0),
            'redemption-fee-recipient-is-0'
        );
        redemptionFeeRecipient = _redemptionFeeRecipient;
    }

    /**
     * @dev Constructor to initialize the configuration settings upon deployment
     * @param _vaultDeployer Address of the Vault Deployer
     * @param _liquidationRouter Address of the Liquidation Router
     */
    constructor(address _vaultDeployer, address _liquidationRouter) {
        require(_vaultDeployer != address(0x0), 'vault-deployer-is-0');
        require(_liquidationRouter != address(0x0), 'liquidation-factory-is-0');
        vaultDeployer = _vaultDeployer;
        borrowFeeRecipient = _msgSender();
        redemptionFeeRecipient = _msgSender();
        lastDebtWindow = block.timestamp;
        liquidationRouter = _liquidationRouter;
    }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.4;
import './utils/constants.sol';
import './utils/linked-address-list.sol';
// import openzeppelin context
import '@openzeppelin/contracts/utils/Context.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';

/**
 * @title VaultFactoryList
 * @dev Manages a list of vaults by their owners, allowing addition, removal, and transfer of vaults.
 */
abstract contract VaultFactoryList is Context {
    using LinkedAddressList for LinkedAddressList.List;
    using EnumerableSet for EnumerableSet.AddressSet;

    LinkedAddressList.List _vaults;
    mapping(address => EnumerableSet.AddressSet) private _vaultsByOwner;

    function vaultsByOwnerLength(
        address _owner
    ) external view returns (uint256) {
        return _vaultsByOwner[_owner].length();
    }

    function vaultsByOwner(
        address _owner,
        uint256 _index
    ) external view returns (address) {
        return _vaultsByOwner[_owner].at(_index);
    }

    function _addVault(address _owner, address _vault) internal {
        require(
            _vaults.add(_vault, address(0x0), false),
            'vault-could-not-be-added'
        );
        _vaultsByOwner[_owner].add(_vault);
    }

    function _transferVault(
        address _from,
        address _to,
        address _vault
    ) internal {
        _vaultsByOwner[_from].remove(_vault);
        _vaultsByOwner[_to].add(_vault);
    }

    function _removeVault(address _owner, address _vault) internal {
        require(_vaults.remove(_vault), 'vault-could-not-be-removed');
        _vaultsByOwner[_owner].remove(_vault);
    }

    /**
     * @dev returns the number of vaults for specific token
     */
    function vaultCount() public view returns (uint256) {
        return _vaults._size;
    }

    /**
     * @dev returns the last vault by maximum collaterization ratio
     */
    function lastVault() public view returns (address) {
        return _vaults._last;
    }

    /**
     * @dev returns the first vault by minimal collaterization ratio
     */
    function firstVault() public view returns (address) {
        return _vaults._first;
    }

    /**
     * @dev returns the next vault by collaterization ratio
     */
    function nextVault(address _vault) public view returns (address) {
        return _vaults._values[_vault].next;
    }

    /**
     * @dev returns the previous vault by collaterization ratio
     */
    function prevVault(address _vault) public view returns (address) {
        return _vaults._values[_vault].prev;
    }

    /**
     * @dev Checks if a vault exists for a specific token.
     * @param _vault The address of the vault to check.
     * @return A boolean indicating whether the vault exists.
     */
    function containsVault(address _vault) public view returns (bool) {
        return _vaults._values[_vault].next != address(0x0);
    }
}

Settings
{
  "optimizer": {
    "enabled": true,
    "runs": 1000
  },
  "evmVersion": "paris",
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  }
}

Contract Security Audit

Contract ABI

API
[{"inputs":[{"internalType":"address","name":"_vaultFactory","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"address","name":"_collateralToken","type":"address"},{"internalType":"uint256","name":"_collateralAmount","type":"uint256"},{"internalType":"uint256","name":"_borrowAmount","type":"uint256"}],"name":"createVault","outputs":[{"internalType":"address","name":"_vault","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_borrowAmount","type":"uint256"}],"name":"createVaultNative","outputs":[{"internalType":"address","name":"_vault","type":"address"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"prefix","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"_prefix","type":"string"}],"name":"setPrefix","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_vaultFactory","type":"address"}],"name":"setVaultFactory","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"vaultFactory","outputs":[{"internalType":"contract IVaultFactory","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]

60c06040526007608090815266135e55985d5b1d60ca1b60a05260029062000028908262000253565b503480156200003657600080fd5b50604051620017633803806200176383398101604081905262000059916200031f565b620000643362000076565b6200006f81620000c6565b5062000351565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b620000d06200014e565b6001600160a01b0381166200012c5760405162461bcd60e51b815260206004820152601a60248201527f5661756c74466163746f72793a207a65726f206164647265737300000000000060448201526064015b60405180910390fd5b600180546001600160a01b0319166001600160a01b0392909216919091179055565b6000546001600160a01b03163314620001aa5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640162000123565b565b634e487b7160e01b600052604160045260246000fd5b600181811c90821680620001d757607f821691505b602082108103620001f857634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156200024e576000816000526020600020601f850160051c81016020861015620002295750805b601f850160051c820191505b818110156200024a5782815560010162000235565b5050505b505050565b81516001600160401b038111156200026f576200026f620001ac565b6200028781620002808454620001c2565b84620001fe565b602080601f831160018114620002bf5760008415620002a65750858301515b600019600386901b1c1916600185901b1785556200024a565b600085815260208120601f198616915b82811015620002f057888601518255948401946001909101908401620002cf565b50858210156200030f5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6000602082840312156200033257600080fd5b81516001600160a01b03811681146200034a57600080fd5b9392505050565b61140280620003616000396000f3fe60806040526004361061009a5760003560e01c806375dadb32116100695780638da5cb5b1161004e5780638da5cb5b1461016f578063d8a06f731461018d578063f2fde38b146101ad57600080fd5b806375dadb321461012d57806385cb593b1461014f57600080fd5b806325041986146100a65780633ea7fbdb146100d6578063486b0acb146100f8578063715018a61461011857600080fd5b366100a157005b600080fd5b6100b96100b4366004610f1f565b6101cd565b6040516001600160a01b0390911681526020015b60405180910390f35b3480156100e257600080fd5b506100f66100f1366004610f4d565b6103ab565b005b34801561010457600080fd5b506100b9610113366004610f71565b61043d565b34801561012457600080fd5b506100f6610659565b34801561013957600080fd5b5061014261066d565b6040516100cd9190610fca565b34801561015b57600080fd5b506100f661016a366004611013565b6106fb565b34801561017b57600080fd5b506000546001600160a01b03166100b9565b34801561019957600080fd5b506001546100b9906001600160a01b031681565b3480156101b957600080fd5b506100f66101c8366004610f4d565b610713565b6001546000906001600160a01b0316633fe1da886101ea336107a3565b6040518263ffffffff1660e01b81526004016102069190610fca565b6020604051808303816000875af1158015610225573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061024991906110c4565b90503415610341576001546040517f4349d0810000000000000000000000000000000000000000000000000000000081526001600160a01b03838116600483015290911690634349d0819034906024016000604051808303818588803b1580156102b257600080fd5b505af11580156102c6573d6000803e3d6000fd5b5050505050600082111561034157600154604051636c665a5560e01b81526001600160a01b0383811660048301526024820185905233604483015290911690636c665a5590606401600060405180830381600087803b15801561032857600080fd5b505af115801561033c573d6000803e3d6000fd5b505050505b60015460405163fdc10daf60e01b81526001600160a01b0383811660048301523360248301529091169063fdc10daf90604401600060405180830381600087803b15801561038e57600080fd5b505af11580156103a2573d6000803e3d6000fd5b50505050919050565b6103b361086f565b6001600160a01b03811661040e5760405162461bcd60e51b815260206004820152601a60248201527f5661756c74466163746f72793a207a65726f206164647265737300000000000060448201526064015b60405180910390fd5b6001805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0392909216919091179055565b6001546000906001600160a01b0316633fe1da8861045a336107a3565b6040518263ffffffff1660e01b81526004016104769190610fca565b6020604051808303816000875af1158015610495573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104b991906110c4565b905082156105ed576104d66001600160a01b0385163330866108c9565b6001546104f0906001600160a01b03868116911685610980565b6001546040517f597810340000000000000000000000000000000000000000000000000000000081526001600160a01b03838116600483015286811660248301526044820186905290911690635978103490606401600060405180830381600087803b15801561055f57600080fd5b505af1158015610573573d6000803e3d6000fd5b5050505060008211156105ed57600154604051636c665a5560e01b81526001600160a01b0383811660048301526024820185905233604483015290911690636c665a5590606401600060405180830381600087803b1580156105d457600080fd5b505af11580156105e8573d6000803e3d6000fd5b505050505b60015460405163fdc10daf60e01b81526001600160a01b0383811660048301523360248301529091169063fdc10daf90604401600060405180830381600087803b15801561063a57600080fd5b505af115801561064e573d6000803e3d6000fd5b505050509392505050565b61066161086f565b61066b6000610ad3565b565b6002805461067a906110e1565b80601f01602080910402602001604051908101604052809291908181526020018280546106a6906110e1565b80156106f35780601f106106c8576101008083540402835291602001916106f3565b820191906000526020600020905b8154815290600101906020018083116106d657829003601f168201915b505050505081565b61070361086f565b600261070f828261116b565b5050565b61071b61086f565b6001600160a01b0381166107975760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201527f64647265737300000000000000000000000000000000000000000000000000006064820152608401610405565b6107a081610ad3565b50565b6001546040517f8ffadd0c0000000000000000000000000000000000000000000000000000000081526001600160a01b038381166004830152606092600092911690638ffadd0c90602401602060405180830381865afa15801561080b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061082f919061122b565b61083a90600161125a565b9050600261084782610b30565b604051602001610858929190611273565b604051602081830303815290604052915050919050565b6000546001600160a01b0316331461066b5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152606401610405565b6040516001600160a01b038085166024830152831660448201526064810182905261097a9085907f23b872dd00000000000000000000000000000000000000000000000000000000906084015b60408051601f198184030181529190526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff0000000000000000000000000000000000000000000000000000000090931692909217909152610c90565b50505050565b801580610a1357506040517fdd62ed3e0000000000000000000000000000000000000000000000000000000081523060048201526001600160a01b03838116602483015284169063dd62ed3e90604401602060405180830381865afa1580156109ed573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610a11919061122b565b155b610a855760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527f20746f206e6f6e2d7a65726f20616c6c6f77616e6365000000000000000000006064820152608401610405565b6040516001600160a01b038316602482015260448101829052610ace9084907f095ea7b30000000000000000000000000000000000000000000000000000000090606401610916565b505050565b600080546001600160a01b0383811673ffffffffffffffffffffffffffffffffffffffff19831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b606081600003610b7357505060408051808201909152600181527f3000000000000000000000000000000000000000000000000000000000000000602082015290565b8160005b8115610b9d5780610b87816112fa565b9150610b969050600a83611313565b9150610b77565b60008167ffffffffffffffff811115610bb857610bb8610ffd565b6040519080825280601f01601f191660200182016040528015610be2576020820181803683370190505b509050815b8515610c8757610bf8600182611335565b90506000610c07600a88611313565b610c1290600a611348565b610c1c9088611335565b610c2790603061135f565b905060008160f81b905080848481518110610c4457610c44611378565b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350610c7e600a89611313565b97505050610be7565b50949350505050565b6000610ce5826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316610d789092919063ffffffff16565b9050805160001480610d06575080806020019051810190610d06919061138e565b610ace5760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f742073756363656564000000000000000000000000000000000000000000006064820152608401610405565b6060610d878484600085610d8f565b949350505050565b606082471015610e075760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f60448201527f722063616c6c00000000000000000000000000000000000000000000000000006064820152608401610405565b600080866001600160a01b03168587604051610e2391906113b0565b60006040518083038185875af1925050503d8060008114610e60576040519150601f19603f3d011682016040523d82523d6000602084013e610e65565b606091505b5091509150610e7687838387610e81565b979650505050505050565b60608315610ef0578251600003610ee9576001600160a01b0385163b610ee95760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610405565b5081610d87565b610d878383815115610f055781518083602001fd5b8060405162461bcd60e51b81526004016104059190610fca565b600060208284031215610f3157600080fd5b5035919050565b6001600160a01b03811681146107a057600080fd5b600060208284031215610f5f57600080fd5b8135610f6a81610f38565b9392505050565b600080600060608486031215610f8657600080fd5b8335610f9181610f38565b95602085013595506040909401359392505050565b60005b83811015610fc1578181015183820152602001610fa9565b50506000910152565b6020815260008251806020840152610fe9816040850160208701610fa6565b601f01601f19169190910160400192915050565b634e487b7160e01b600052604160045260246000fd5b60006020828403121561102557600080fd5b813567ffffffffffffffff8082111561103d57600080fd5b818401915084601f83011261105157600080fd5b81358181111561106357611063610ffd565b604051601f8201601f19908116603f0116810190838211818310171561108b5761108b610ffd565b816040528281528760208487010111156110a457600080fd5b826020860160208301376000928101602001929092525095945050505050565b6000602082840312156110d657600080fd5b8151610f6a81610f38565b600181811c908216806110f557607f821691505b60208210810361111557634e487b7160e01b600052602260045260246000fd5b50919050565b601f821115610ace576000816000526020600020601f850160051c810160208610156111445750805b601f850160051c820191505b8181101561116357828155600101611150565b505050505050565b815167ffffffffffffffff81111561118557611185610ffd565b6111998161119384546110e1565b8461111b565b602080601f8311600181146111ce57600084156111b65750858301515b600019600386901b1c1916600185901b178555611163565b600085815260208120601f198616915b828110156111fd578886015182559484019460019091019084016111de565b508582101561121b5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b60006020828403121561123d57600080fd5b5051919050565b634e487b7160e01b600052601160045260246000fd5b8082018082111561126d5761126d611244565b92915050565b6000808454611281816110e1565b6001828116801561129957600181146112ae576112dd565b60ff19841687528215158302870194506112dd565b8860005260208060002060005b858110156112d45781548a8201529084019082016112bb565b50505082870194505b5050505083516112f1818360208801610fa6565b01949350505050565b60006001820161130c5761130c611244565b5060010190565b60008261133057634e487b7160e01b600052601260045260246000fd5b500490565b8181038181111561126d5761126d611244565b808202811582820484141761126d5761126d611244565b60ff818116838216019081111561126d5761126d611244565b634e487b7160e01b600052603260045260246000fd5b6000602082840312156113a057600080fd5b81518015158114610f6a57600080fd5b600082516113c2818460208701610fa6565b919091019291505056fea2646970667358221220b5e0e89f97bb1659bce5eb97cf74ca773fd99b740b5aef14c810454181e815a564736f6c6343000817003300000000000000000000000065c6fd9b3a2a892096881e28f07c732ed128893e

Deployed Bytecode

0x60806040526004361061009a5760003560e01c806375dadb32116100695780638da5cb5b1161004e5780638da5cb5b1461016f578063d8a06f731461018d578063f2fde38b146101ad57600080fd5b806375dadb321461012d57806385cb593b1461014f57600080fd5b806325041986146100a65780633ea7fbdb146100d6578063486b0acb146100f8578063715018a61461011857600080fd5b366100a157005b600080fd5b6100b96100b4366004610f1f565b6101cd565b6040516001600160a01b0390911681526020015b60405180910390f35b3480156100e257600080fd5b506100f66100f1366004610f4d565b6103ab565b005b34801561010457600080fd5b506100b9610113366004610f71565b61043d565b34801561012457600080fd5b506100f6610659565b34801561013957600080fd5b5061014261066d565b6040516100cd9190610fca565b34801561015b57600080fd5b506100f661016a366004611013565b6106fb565b34801561017b57600080fd5b506000546001600160a01b03166100b9565b34801561019957600080fd5b506001546100b9906001600160a01b031681565b3480156101b957600080fd5b506100f66101c8366004610f4d565b610713565b6001546000906001600160a01b0316633fe1da886101ea336107a3565b6040518263ffffffff1660e01b81526004016102069190610fca565b6020604051808303816000875af1158015610225573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061024991906110c4565b90503415610341576001546040517f4349d0810000000000000000000000000000000000000000000000000000000081526001600160a01b03838116600483015290911690634349d0819034906024016000604051808303818588803b1580156102b257600080fd5b505af11580156102c6573d6000803e3d6000fd5b5050505050600082111561034157600154604051636c665a5560e01b81526001600160a01b0383811660048301526024820185905233604483015290911690636c665a5590606401600060405180830381600087803b15801561032857600080fd5b505af115801561033c573d6000803e3d6000fd5b505050505b60015460405163fdc10daf60e01b81526001600160a01b0383811660048301523360248301529091169063fdc10daf90604401600060405180830381600087803b15801561038e57600080fd5b505af11580156103a2573d6000803e3d6000fd5b50505050919050565b6103b361086f565b6001600160a01b03811661040e5760405162461bcd60e51b815260206004820152601a60248201527f5661756c74466163746f72793a207a65726f206164647265737300000000000060448201526064015b60405180910390fd5b6001805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0392909216919091179055565b6001546000906001600160a01b0316633fe1da8861045a336107a3565b6040518263ffffffff1660e01b81526004016104769190610fca565b6020604051808303816000875af1158015610495573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104b991906110c4565b905082156105ed576104d66001600160a01b0385163330866108c9565b6001546104f0906001600160a01b03868116911685610980565b6001546040517f597810340000000000000000000000000000000000000000000000000000000081526001600160a01b03838116600483015286811660248301526044820186905290911690635978103490606401600060405180830381600087803b15801561055f57600080fd5b505af1158015610573573d6000803e3d6000fd5b5050505060008211156105ed57600154604051636c665a5560e01b81526001600160a01b0383811660048301526024820185905233604483015290911690636c665a5590606401600060405180830381600087803b1580156105d457600080fd5b505af11580156105e8573d6000803e3d6000fd5b505050505b60015460405163fdc10daf60e01b81526001600160a01b0383811660048301523360248301529091169063fdc10daf90604401600060405180830381600087803b15801561063a57600080fd5b505af115801561064e573d6000803e3d6000fd5b505050509392505050565b61066161086f565b61066b6000610ad3565b565b6002805461067a906110e1565b80601f01602080910402602001604051908101604052809291908181526020018280546106a6906110e1565b80156106f35780601f106106c8576101008083540402835291602001916106f3565b820191906000526020600020905b8154815290600101906020018083116106d657829003601f168201915b505050505081565b61070361086f565b600261070f828261116b565b5050565b61071b61086f565b6001600160a01b0381166107975760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201527f64647265737300000000000000000000000000000000000000000000000000006064820152608401610405565b6107a081610ad3565b50565b6001546040517f8ffadd0c0000000000000000000000000000000000000000000000000000000081526001600160a01b038381166004830152606092600092911690638ffadd0c90602401602060405180830381865afa15801561080b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061082f919061122b565b61083a90600161125a565b9050600261084782610b30565b604051602001610858929190611273565b604051602081830303815290604052915050919050565b6000546001600160a01b0316331461066b5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152606401610405565b6040516001600160a01b038085166024830152831660448201526064810182905261097a9085907f23b872dd00000000000000000000000000000000000000000000000000000000906084015b60408051601f198184030181529190526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff0000000000000000000000000000000000000000000000000000000090931692909217909152610c90565b50505050565b801580610a1357506040517fdd62ed3e0000000000000000000000000000000000000000000000000000000081523060048201526001600160a01b03838116602483015284169063dd62ed3e90604401602060405180830381865afa1580156109ed573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610a11919061122b565b155b610a855760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527f20746f206e6f6e2d7a65726f20616c6c6f77616e6365000000000000000000006064820152608401610405565b6040516001600160a01b038316602482015260448101829052610ace9084907f095ea7b30000000000000000000000000000000000000000000000000000000090606401610916565b505050565b600080546001600160a01b0383811673ffffffffffffffffffffffffffffffffffffffff19831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b606081600003610b7357505060408051808201909152600181527f3000000000000000000000000000000000000000000000000000000000000000602082015290565b8160005b8115610b9d5780610b87816112fa565b9150610b969050600a83611313565b9150610b77565b60008167ffffffffffffffff811115610bb857610bb8610ffd565b6040519080825280601f01601f191660200182016040528015610be2576020820181803683370190505b509050815b8515610c8757610bf8600182611335565b90506000610c07600a88611313565b610c1290600a611348565b610c1c9088611335565b610c2790603061135f565b905060008160f81b905080848481518110610c4457610c44611378565b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350610c7e600a89611313565b97505050610be7565b50949350505050565b6000610ce5826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316610d789092919063ffffffff16565b9050805160001480610d06575080806020019051810190610d06919061138e565b610ace5760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f742073756363656564000000000000000000000000000000000000000000006064820152608401610405565b6060610d878484600085610d8f565b949350505050565b606082471015610e075760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f60448201527f722063616c6c00000000000000000000000000000000000000000000000000006064820152608401610405565b600080866001600160a01b03168587604051610e2391906113b0565b60006040518083038185875af1925050503d8060008114610e60576040519150601f19603f3d011682016040523d82523d6000602084013e610e65565b606091505b5091509150610e7687838387610e81565b979650505050505050565b60608315610ef0578251600003610ee9576001600160a01b0385163b610ee95760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610405565b5081610d87565b610d878383815115610f055781518083602001fd5b8060405162461bcd60e51b81526004016104059190610fca565b600060208284031215610f3157600080fd5b5035919050565b6001600160a01b03811681146107a057600080fd5b600060208284031215610f5f57600080fd5b8135610f6a81610f38565b9392505050565b600080600060608486031215610f8657600080fd5b8335610f9181610f38565b95602085013595506040909401359392505050565b60005b83811015610fc1578181015183820152602001610fa9565b50506000910152565b6020815260008251806020840152610fe9816040850160208701610fa6565b601f01601f19169190910160400192915050565b634e487b7160e01b600052604160045260246000fd5b60006020828403121561102557600080fd5b813567ffffffffffffffff8082111561103d57600080fd5b818401915084601f83011261105157600080fd5b81358181111561106357611063610ffd565b604051601f8201601f19908116603f0116810190838211818310171561108b5761108b610ffd565b816040528281528760208487010111156110a457600080fd5b826020860160208301376000928101602001929092525095945050505050565b6000602082840312156110d657600080fd5b8151610f6a81610f38565b600181811c908216806110f557607f821691505b60208210810361111557634e487b7160e01b600052602260045260246000fd5b50919050565b601f821115610ace576000816000526020600020601f850160051c810160208610156111445750805b601f850160051c820191505b8181101561116357828155600101611150565b505050505050565b815167ffffffffffffffff81111561118557611185610ffd565b6111998161119384546110e1565b8461111b565b602080601f8311600181146111ce57600084156111b65750858301515b600019600386901b1c1916600185901b178555611163565b600085815260208120601f198616915b828110156111fd578886015182559484019460019091019084016111de565b508582101561121b5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b60006020828403121561123d57600080fd5b5051919050565b634e487b7160e01b600052601160045260246000fd5b8082018082111561126d5761126d611244565b92915050565b6000808454611281816110e1565b6001828116801561129957600181146112ae576112dd565b60ff19841687528215158302870194506112dd565b8860005260208060002060005b858110156112d45781548a8201529084019082016112bb565b50505082870194505b5050505083516112f1818360208801610fa6565b01949350505050565b60006001820161130c5761130c611244565b5060010190565b60008261133057634e487b7160e01b600052601260045260246000fd5b500490565b8181038181111561126d5761126d611244565b808202811582820484141761126d5761126d611244565b60ff818116838216019081111561126d5761126d611244565b634e487b7160e01b600052603260045260246000fd5b6000602082840312156113a057600080fd5b81518015158114610f6a57600080fd5b600082516113c2818460208701610fa6565b919091019291505056fea2646970667358221220b5e0e89f97bb1659bce5eb97cf74ca773fd99b740b5aef14c810454181e815a564736f6c63430008170033

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

00000000000000000000000065c6fd9b3a2a892096881e28f07c732ed128893e

-----Decoded View---------------
Arg [0] : _vaultFactory (address): 0x65c6FD9B3a2A892096881e28f07c732ed128893E

-----Encoded View---------------
1 Constructor Arguments found :
Arg [0] : 00000000000000000000000065c6fd9b3a2a892096881e28f07c732ed128893e


Block Transaction Gas Used Reward
view all blocks sequenced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading
Loading...
Loading

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading
[ Download: CSV Export  ]
[ Download: CSV Export  ]

A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.