Skip to content
Verified contractPartialBUSL-1.1

LendingMarketController

Address0x6588c630305929bc9da253a0724fd094e0695ce5
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.19

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.19+commit.7dd6d404
Language
solidity
Optimization
Yes, 200 runs
License
BUSL-1.1
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,485,250
Creation transaction
0x4eec084182…13b31fa5dd
Libraries
contracts/protocol/LendingMarketController.sol:LiquidationLogic0xb196…fd93eacontracts/protocol/LendingMarketController.sol:FundManagementLogic0xdcf7…be81bbcontracts/protocol/LendingMarketController.sol:LendingMarketUserLogic0x511b…95fd6fcontracts/protocol/LendingMarketController.sol:LendingMarketOperationLogic0x8e5c…417a98

58 files

The source code is large: one file is shown at a time. Choose a file in the list.

contracts/protocol/LendingMarketController.sol

// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.19;

// dependencies
import {ReentrancyGuard} from "../dependencies/openzeppelin/security/ReentrancyGuard.sol";
import {EnumerableSet} from "../dependencies/openzeppelin/utils/structs/EnumerableSet.sol";
import {Multicall} from "../dependencies/openzeppelin/utils/Multicall.sol";
// interfaces
import {ILendingMarketController} from "./interfaces/ILendingMarketController.sol";
import {ILendingMarket} from "./interfaces/ILendingMarket.sol";
// libraries
import {Contracts} from "./libraries/Contracts.sol";
import {FilledOrder, PartiallyFilledOrder} from "./libraries/OrderBookLib.sol";
import {FundManagementLogic} from "./libraries/logics/FundManagementLogic.sol";
import {LendingMarketOperationLogic} from "./libraries/logics/LendingMarketOperationLogic.sol";
import {LendingMarketUserLogic} from "./libraries/logics/LendingMarketUserLogic.sol";
import {LiquidationLogic} from "./libraries/logics/LiquidationLogic.sol";
import {ItayoseProcessStatus} from "./libraries/logics/OrderBookLogic.sol";
// mixins
import {MixinAccessControl} from "./mixins/MixinAccessControl.sol";
import {MixinAddressResolver} from "./mixins/MixinAddressResolver.sol";
import {MixinLendingMarketConfiguration} from "./mixins/MixinLendingMarketConfiguration.sol";
// types
import {ProtocolTypes} from "./types/ProtocolTypes.sol";
// utils
import {Proxyable} from "./utils/Proxyable.sol";
import {LockAndMsgSender} from "./utils/LockAndMsgSender.sol";
// storages
import {LendingMarketControllerStorage as Storage, ZCTokenInfo, TerminationCurrencyCache} from "./storages/LendingMarketControllerStorage.sol";

/**
 * @notice Implements the module to manage separated lending market contracts per currency.
 *
 * This contract also works as a factory contract that can deploy (start) a new lending market & order book
 * for selected currency and maturity and has the calculation logic for the following user's funds in addition.
 * - Present Value(PV)
 * - Future Value(FV)
 * - Genesis Value(GV)
 *
 * Once the order book is created, it will be rotated and reused once it reaches its maturity date. At the time of rotation,
 * a new maturity date is set and the compound factor is updated.
 *
 * The users mainly call this contract to execute orders to lend or borrow funds.
 */
contract LendingMarketController is
    ILendingMarketController,
    MixinLendingMarketConfiguration,
    MixinAccessControl,
    MixinAddressResolver,
    ReentrancyGuard,
    Proxyable,
    LockAndMsgSender,
    Multicall
{
    using EnumerableSet for EnumerableSet.Bytes32Set;

    /**
     * @notice Modifier to check if there is an order book in the maturity.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     */
    modifier ifValidMaturity(bytes32 _ccy, uint256 _maturity) {
        _requireValidMaturity(_ccy, _maturity);
        _;
    }

    /**
     * @notice Modifier to check if the protocol is active.
     */
    modifier ifActive() {
        _requireActive();
        _;
    }

    /**
     * @notice Modifier to check if the protocol is inactive.
     */
    modifier ifInactive() {
        if (!isTerminated()) revert NotTerminated();
        _;
    }

    function _requireValidMaturity(bytes32 _ccy, uint256 _maturity) private view {
        if (!isValidMaturity(_ccy, _maturity)) revert InvalidMaturity();
    }

    function _requireActive() private view {
        if (isTerminated()) revert MarketTerminated();
    }

    /**
     * @notice Initializes the contract.
     * @dev Function is invoked by the proxy contract when the contract is added to the ProxyController.
     * @param _owner The address of the contract owner
     * @param _resolver The address of the Address Resolver contract
     * @param _marketBasePeriod The base period for market maturity
     */
    function initialize(
        address _owner,
        address _resolver,
        uint256 _marketBasePeriod
    ) public initializer onlyProxy {
        Storage.slot().marketBasePeriod = _marketBasePeriod;
        MixinLendingMarketConfiguration._initialize(_owner);
        MixinAccessControl._setupInitialRoles(_owner);
        registerAddressResolver(_resolver);
    }

    // @inheritdoc MixinAddressResolver
    function requiredContracts() public pure override returns (bytes32[] memory contracts) {
        contracts = new bytes32[](5);
        contracts[0] = Contracts.BEACON_PROXY_CONTROLLER;
        contracts[1] = Contracts.CURRENCY_CONTROLLER;
        contracts[2] = Contracts.GENESIS_VALUE_VAULT;
        contracts[3] = Contracts.RESERVE_FUND;
        contracts[4] = Contracts.TOKEN_VAULT;
    }

    /**
     * @notice Gets if the maturity is valid.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     * @return The boolean if the maturity is valid
     */
    function isValidMaturity(bytes32 _ccy, uint256 _maturity) public view override returns (bool) {
        return Storage.slot().maturityOrderBookIds[_ccy][_maturity] != 0;
    }

    /**
     * @notice Gets if the protocol has not been terminated.
     * @return The boolean if the protocol has not been terminated
     */
    function isTerminated() public view override returns (bool) {
        return Storage.slot().terminationDate > 0;
    }

    /**
     * @notice Gets if the user needs to redeem the funds.
     * @return The boolean if the user needs to redeem the funds
     */
    function isRedemptionRequired(address _user) external view override returns (bool) {
        return isTerminated() && !Storage.slot().isRedeemed[_user];
    }

    // @inheritdoc Proxyable
    function getRevision() external pure override returns (uint256) {
        return 0x4;
    }

    /**
     * @notice Gets the base period for market maturity
     * @return The base period
     */
    function getMarketBasePeriod() external view override returns (uint256) {
        return Storage.slot().marketBasePeriod;
    }

    /**
     * @notice Gets the date at the emergency termination.
     * @return The termination date
     */
    function getTerminationDate() external view override returns (uint256) {
        return Storage.slot().terminationDate;
    }

    /**
     * @notice Gets the currency information cached at the emergency termination.
     * @param _ccy Currency name in bytes32
     * @return The price cached
     */
    function getTerminationCurrencyCache(
        bytes32 _ccy
    ) external view override returns (TerminationCurrencyCache memory) {
        return Storage.slot().terminationCurrencyCaches[_ccy];
    }

    /**
     * @notice Gets the collateral ratio of each token in TokenVault at the emergency termination.
     * @param _ccy Currency name in bytes32
     * @return The ratio
     */
    function getTerminationCollateralRatio(bytes32 _ccy) external view override returns (uint256) {
        return Storage.slot().terminationCollateralRatios[_ccy];
    }

    /**
     * @notice Gets the min debt unit price for the selected currency.
     * This price is based on a one-year maturity.
     * @param _ccy Currency name in bytes32
     * @return The genesis date
     */
    function getMinDebtUnitPrice(bytes32 _ccy) external view override returns (uint256) {
        return Storage.slot().minDebtUnitPrices[_ccy];
    }

    /**
     * @notice Gets the current min debt unit price.
     * This price fluctuates depending on the current maturity.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     * @return The current min debt unit price
     */
    function getCurrentMinDebtUnitPrice(
        bytes32 _ccy,
        uint256 _maturity
    ) external view override returns (uint256) {
        return
            FundManagementLogic.getCurrentMinDebtUnitPrice(
                _maturity,
                Storage.slot().minDebtUnitPrices[_ccy]
            );
    }

    /**
     * @notice Gets the min debt unit price at an arbitrary reference timestamp.
     */
    function getMinDebtUnitPriceAt(
        bytes32 _ccy,
        uint256 _maturity,
        uint256 _referenceTimestamp
    ) external view override returns (uint256) {
        return
            FundManagementLogic.getMinDebtUnitPriceAt(
                _maturity,
                Storage.slot().minDebtUnitPrices[_ccy],
                _referenceTimestamp
            );
    }

    /**
     * @notice Gets the currently accepted limit-order unit price range.
     * @dev The range does not indicate whether order entry is currently executable.
     */
    function getOrderUnitPriceRange(
        bytes32 _ccy,
        uint256 _maturity
    )
        external
        view
        override
        ifValidMaturity(_ccy, _maturity)
        returns (
            uint256 minLendUnitPrice,
            uint256 maxLendUnitPrice,
            uint256 minBorrowUnitPrice,
            uint256 maxBorrowUnitPrice,
            uint256 referenceUnitPrice,
            bool isMinDebtUnitPriceReference
        )
    {
        return LendingMarketOperationLogic.getOrderUnitPriceRange(_ccy, _maturity);
    }

    /**
     * @notice Gets the resumable Itayose process status.
     */
    function getItayoseProcessStatus(
        bytes32 _ccy,
        uint256 _maturity
    )
        external
        view
        override
        ifValidMaturity(_ccy, _maturity)
        returns (ItayoseProcessStatus memory)
    {
        return LendingMarketOperationLogic.getItayoseProcessStatus(_ccy, _maturity);
    }

    /**
     * @notice Gets the genesis date when the first market opens for the selected currency.
     * @param _ccy Currency name in bytes32
     * @return The genesis date
     */
    function getGenesisDate(bytes32 _ccy) external view override returns (uint256) {
        return Storage.slot().genesisDates[_ccy];
    }

    /**
     * @notice Gets the lending market contract address for the selected currency.
     * @param _ccy Currency name in bytes32
     * @return Array with the lending market address
     */
    function getLendingMarket(bytes32 _ccy) external view override returns (address) {
        return Storage.slot().lendingMarkets[_ccy];
    }

    /**
     * @notice Gets the order book id for the selected currency and maturity.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     * @return The order book id
     */
    function getOrderBookId(
        bytes32 _ccy,
        uint256 _maturity
    ) external view override returns (uint8) {
        return Storage.slot().maturityOrderBookIds[_ccy][_maturity];
    }

    /**
     * @notice Gets the future value contract address for the selected currency and maturity.
     * @param _ccy Currency name in bytes32
     * @return The future value vault address
     */
    function getFutureValueVault(bytes32 _ccy) public view override returns (address) {
        return Storage.slot().futureValueVaults[_ccy];
    }

    /**
     * @notice Gets the total amount of pending orders that is not cleaned up yet.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     * @return The total amount
     */
    function getPendingOrderAmount(
        bytes32 _ccy,
        uint256 _maturity
    ) external view override returns (uint256) {
        return Storage.slot().pendingOrderAmounts[_ccy][_maturity];
    }

    /**
     * @notice Gets the estimated order result by the calculation of the amount to be filled when executing an order in the order books.
     * @param _params The parameters to calculate the order estimation <br>
     * - ccy: Currency name in bytes32 of the selected market <br>
     * - maturity: The maturity of the market <br>
     * - side: Order position type, Borrow or Lend <br>
     * - amount: Amount of funds the maker wants to borrow/lend <br>
     * - unitPrice: Amount of unit price taker wish to borrow/lend <br>
     * - additionalDepositAmount: Additional amount to be deposited with the lending order <br>
     * - ignoreBorrowedAmount: The boolean if the borrowed amount is ignored and not used as collateral or not
     * @return lastUnitPrice The last unit price that is filled on the order book
     * @return filledAmount The amount that is filled on the order book
     * @return filledAmountInFV The amount in the future value that is filled on the order book
     * @return orderFeeInFV The order fee amount in the future value
     * @return placedAmount The amount that is placed to the order book
     * @return coverage The rate of collateral used
     * @return isInsufficientDepositAmount The boolean if the order amount for lending in the selected currency is insufficient
     * for the deposit amount or not
     */
    function getOrderEstimation(
        GetOrderEstimationParams calldata _params
    )
        external
        view
        override
        returns (
            uint256 lastUnitPrice,
            uint256 filledAmount,
            uint256 filledAmountInFV,
            uint256 orderFeeInFV,
            uint256 placedAmount,
            uint256 coverage,
            bool isInsufficientDepositAmount
        )
    {
        return LendingMarketUserLogic.getOrderEstimation(_params);
    }

    /**
     * @notice Gets the estimated order result by the calculation of the amount to be filled when executing an order in the order books.
     * This function is used to estimate the order from future value.
     * @param _params The parameters to calculate the order estimation <br>
     * - ccy: Currency name in bytes32 of the selected market <br>
     * - maturity: The maturity of the market <br>
     * - user: User's address <br>
     * - side: Order position type, Borrow or Lend <br>
     * - amountInFV: Amount of funds in future value the maker wants to borrow/lend <br>
     * - additionalDepositAmount: Additional amount to be deposited with the lending order <br>
     * - ignoreBorrowedAmount: The boolean if the borrowed amount is ignored and not used as collateral or not
     * @return lastUnitPrice The last unit price that is filled on the order book
     * @return filledAmount The amount that is filled on the order book
     * @return filledAmountInFV The amount in the future value that is filled on the order book
     * @return orderFeeInFV The order fee amount in the future value
     * @return coverage The rate of collateral used
     * @return isInsufficientDepositAmount The boolean if the order amount for lending in the selected currency is insufficient
     * for the deposit amount or not
     */
    function getOrderEstimationFromFV(
        GetOrderEstimationFromFVParams calldata _params
    )
        external
        view
        override
        returns (
            uint256 lastUnitPrice,
            uint256 filledAmount,
            uint256 filledAmountInFV,
            uint256 orderFeeInFV,
            uint256 coverage,
            bool isInsufficientDepositAmount
        )
    {
        return LendingMarketUserLogic.getOrderEstimationFromFV(_params);
    }

    /**
     * @notice Gets maturities for the selected currency.
     * @param _ccy Currency name in bytes32
     * @return Array with the lending market maturity
     */
    function getMaturities(bytes32 _ccy) public view override returns (uint256[] memory) {
        return
            ILendingMarket(Storage.slot().lendingMarkets[_ccy]).getMaturities(
                Storage.slot().orderBookIdLists[_ccy]
            );
    }

    /**
     * @notice Gets the order book ids.
     * @param _ccy Currency name in bytes32
     * @return The array of order book id
     */
    function getOrderBookIds(bytes32 _ccy) external view override returns (uint8[] memory) {
        return Storage.slot().orderBookIdLists[_ccy];
    }

    /**
     * @notice Get all the currencies in which the user has lending positions or orders.
     * @param _user User's address
     * @return The array of the currency
     */
    function getUsedCurrencies(address _user) external view override returns (bytes32[] memory) {
        return Storage.slot().usedCurrencies[_user].values();
    }

    /**
     * @notice Get all the maturities in which the user has lending positions or orders for the selected currency.
     * @param _ccy Currency name in bytes32
     * @param _user User's address
     * @return The array of the maturity
     */
    function getUsedMaturities(
        bytes32 _ccy,
        address _user
    ) external view override returns (uint256[] memory) {
        return FundManagementLogic.getUsedMaturities(_ccy, _user);
    }

    /**
     * @notice Gets the total present value of the account for selected currency.
     * @param _ccy Currency name in bytes32 for Lending Market
     * @param _user User's address
     * @return totalPresentValue The total present value
     */
    function getTotalPresentValue(
        bytes32 _ccy,
        address _user
    ) external view override returns (int256 totalPresentValue) {
        totalPresentValue = FundManagementLogic
            .getActualFunds(_ccy, 0, _user, 0, false)
            .presentValue;
    }

    /**
     * @notice Gets the total present value of the account converted to base currency.
     * @param _user User's address
     * @return totalPresentValue The total present value in base currency
     */
    function getTotalPresentValueInBaseCurrency(
        address _user
    ) external view override returns (int256 totalPresentValue) {
        EnumerableSet.Bytes32Set storage currencySet = Storage.slot().usedCurrencies[_user];

        for (uint256 i; i < currencySet.length(); ) {
            bytes32 ccy = currencySet.at(i);
            int256 amount = FundManagementLogic
                .getActualFunds(ccy, 0, _user, 0, false)
                .presentValue;
            totalPresentValue += currencyController().convertToBaseCurrency(ccy, amount);
            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Gets the genesis value of the account.
     * @param _ccy Currency name in bytes32 for Lending Market
     * @param _user User's address
     * @return amount The genesis value amount
     * @return amountInPV The genesis value amount in present value
     * @return amountInFV The genesis value amount in future value
     */
    function getGenesisValue(
        bytes32 _ccy,
        address _user
    ) external view override returns (int256 amount, int256 amountInPV, int256 amountInFV) {
        return FundManagementLogic.getGenesisValue(_ccy, _user);
    }

    /**
     * @notice Gets user's active position from the future value vault
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the selected order book
     * @param _user User's address
     * @return presentValue The present value of the position
     * @return futureValue The future value of the position
     */
    function getPosition(
        bytes32 _ccy,
        uint256 _maturity,
        address _user
    ) external view override returns (int256 presentValue, int256 futureValue) {
        (presentValue, futureValue) = FundManagementLogic.getPosition(_ccy, _maturity, _user);
    }

    /**
     * @notice Gets ZC Token address for the selected currency and maturity.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the selected order book
     * @return The zcToken address
     */
    function getZCToken(bytes32 _ccy, uint256 _maturity) external view override returns (address) {
        return Storage.slot().zcTokens[_ccy][_maturity];
    }

    /**
     * @notice Gets ZC Token info for the selected ZC Token address.
     * @param _zcToken The zcToken address
     * @return The zcToken data
     */
    function getZCTokenInfo(address _zcToken) external view override returns (ZCTokenInfo memory) {
        return Storage.slot().zcTokenInfo[_zcToken];
    }

    /**
     * @notice Gets the withdrawable ZCToken amount for the selected currency and maturity.
     * ZC perpetual token amount is returned only when the maturity is 0.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     * @param _user User's address
     * @return amount The total amount of ZCToken that can be minted
     */
    function getWithdrawableZCTokenAmount(
        bytes32 _ccy,
        uint256 _maturity,
        address _user
    ) external view override returns (uint256 amount) {
        return LendingMarketUserLogic.getWithdrawableZCTokenAmount(_ccy, _maturity, _user);
    }

    /**
     * @notice Gets the funds that are calculated from the user's lending and borrowing order list
     * for the selected currency.
     * @param _ccy Currency name in bytes32
     * @param _user User's address
     * @param _liquidationThresholdRate The liquidation threshold rate
     * @return funds The funds calculated from the user's lending and borrowing order list
     */
    function calculateFunds(
        bytes32 _ccy,
        address _user,
        uint256 _liquidationThresholdRate
    ) external view override returns (CalculatedFunds memory funds) {
        if (Storage.slot().usedCurrencies[_user].contains(_ccy)) {
            AdditionalFunds memory emptyAdditionalFunds;

            return
                FundManagementLogic.calculateFunds(
                    _ccy,
                    _user,
                    emptyAdditionalFunds,
                    _liquidationThresholdRate,
                    false
                );
        }
    }

    /**
     * @notice Gets the funds that are calculated from the user's lending and borrowing order list
     * for all currencies in base currency.
     * @param _user User's address
     * @param _additionalFunds The funds to be added for calculating the total funds
     * @param _liquidationThresholdRate The liquidation threshold rate
     * @param _useConservativePrice Use the less favorable of the valuation and live prices
     * @return funds The total funds calculated from the user's lending and borrowing order list
     */
    function calculateTotalFundsInBaseCurrency(
        address _user,
        AdditionalFunds calldata _additionalFunds,
        uint256 _liquidationThresholdRate,
        bool _useConservativePrice
    ) external view override returns (CalculatedTotalFunds memory funds) {
        return
            FundManagementLogic.calculateTotalFundsInBaseCurrency(
                _user,
                _additionalFunds,
                _liquidationThresholdRate,
                _useConservativePrice
            );
    }

    /**
     * @notice Gets if the lending market is initialized.
     * @param _ccy Currency name in bytes32
     * @return The boolean if the lending market is initialized or not
     */
    function isInitializedLendingMarket(bytes32 _ccy) public view override returns (bool) {
        return Storage.slot().genesisDates[_ccy] != 0;
    }

    /**
     * @notice Initialize the lending market to set a genesis date and compound factor
     * @param _ccy Currency name in bytes32
     * @param _genesisDate The genesis date when the initial market is opened
     * @param _compoundFactor The initial compound factor when the initial market is opened
     * @param _orderFeeRate The order fee rate received by protocol
     * @param _circuitBreakerLimitRange The circuit breaker limit range
     */
    function initializeLendingMarket(
        bytes32 _ccy,
        uint256 _genesisDate,
        uint256 _compoundFactor,
        uint256 _orderFeeRate,
        uint256 _circuitBreakerLimitRange,
        uint256 _minDebtUnitPrice
    ) external override onlyOwner {
        if (isInitializedLendingMarket(_ccy)) revert AlreadyInitialized();

        LendingMarketOperationLogic.initializeLendingMarket(
            _ccy,
            _genesisDate,
            _compoundFactor,
            _orderFeeRate,
            _circuitBreakerLimitRange,
            _minDebtUnitPrice
        );
    }

    /**
     * @notice Creates new order book.
     * @param _ccy Main currency for new order book
     * @param _openingDate The timestamp when the order book opens
     * @param _preOpeningDate The timestamp when the order book pre-opens
     */
    function createOrderBook(
        bytes32 _ccy,
        uint256 _openingDate,
        uint256 _preOpeningDate
    ) external override ifActive onlyOwner {
        LendingMarketOperationLogic.createOrderBook(_ccy, _openingDate, _preOpeningDate);
    }

    /**
     * @notice Executes an order. Takes orders if the order is matched,
     * and places new order if not match it.
     *
     * In addition, converts the future value to the genesis value if there is future value in past maturity
     * before the execution of order creation.
     *
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _side Order position type, Borrow or Lend
     * @param _amount Amount of funds the maker wants to borrow/lend
     * @param _unitPrice Amount of unit price taker wish to borrow/lend
     * @return True if the execution of the operation succeeds
     */
    function executeOrder(
        bytes32 _ccy,
        uint256 _maturity,
        ProtocolTypes.Side _side,
        uint256 _amount,
        uint256 _unitPrice
    ) public override nonReentrant ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        LendingMarketUserLogic.executeOrder(
            _ccy,
            _maturity,
            msg.sender,
            _side,
            _amount,
            _unitPrice
        );
        return true;
    }

    /**
     * @notice Deposits funds and executes an order at the same time.
     *
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _side Order position type, Borrow or Lend
     * @param _amount Amount of funds the maker wants to borrow/lend
     * @param _unitPrice Amount of unit price taker wish to borrow/lend
     * @return True if the execution of the operation succeeds
     */
    function depositAndExecuteOrder(
        bytes32 _ccy,
        uint256 _maturity,
        ProtocolTypes.Side _side,
        uint256 _amount,
        uint256 _unitPrice
    ) external payable override ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        tokenVault().depositFrom{value: msg.value}(msg.sender, _ccy, _amount);
        executeOrder(_ccy, _maturity, _side, _amount, _unitPrice);
        return true;
    }

    /**
     * @notice Deposits funds with transfer approval of asset via permit function
     * and executes an order at the same time.
     *
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _side Order position type, Borrow or Lend
     * @param _amount Amount of funds the maker wants to borrow/lend
     * @param _unitPrice Amount of unit price taker wish to borrow/lend
     * @param _deadline The deadline timestamp that the permit is valid
     * @param _permitV The V parameter of ERC712 permit sig
     * @param _permitR The R parameter of ERC712 permit sig
     * @param _permitS The S parameter of ERC712 permit sig
     * @return True if the execution of the operation succeeds
     */
    function depositWithPermitAndExecuteOrder(
        bytes32 _ccy,
        uint256 _maturity,
        ProtocolTypes.Side _side,
        uint256 _amount,
        uint256 _unitPrice,
        uint256 _deadline,
        uint8 _permitV,
        bytes32 _permitR,
        bytes32 _permitS
    ) external override ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        tokenVault().depositWithPermitFrom(
            msg.sender,
            _ccy,
            _amount,
            _deadline,
            _permitV,
            _permitR,
            _permitS
        );

        executeOrder(_ccy, _maturity, _side, _amount, _unitPrice);
        return true;
    }

    /**
     * @notice Executes a pre-order. A pre-order will only be accepted from 168 hours (7 days) to 1 hour
     * before the order book opens (Pre-order period). At the end of this period, Itayose will be executed.
     *
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _side Order position type, Borrow or Lend
     * @param _amount Amount of funds the maker wants to borrow/lend
     * @param _unitPrice Amount of unit price taker wish to borrow/lend
     * @return True if the execution of the operation succeeds
     */
    function executePreOrder(
        bytes32 _ccy,
        uint256 _maturity,
        ProtocolTypes.Side _side,
        uint256 _amount,
        uint256 _unitPrice
    ) public override nonReentrant ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        LendingMarketUserLogic.executePreOrder(
            _ccy,
            _maturity,
            msg.sender,
            _side,
            _amount,
            _unitPrice
        );

        return true;
    }

    /**
     * @notice Deposits funds and executes a pre-order at the same time.
     *
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _side Order position type, Borrow or Lend
     * @param _amount Amount of funds the maker wants to borrow/lend
     * @param _unitPrice Amount of unit price taker wish to borrow/lend
     * @return True if the execution of the operation succeeds
     */
    function depositAndExecutesPreOrder(
        bytes32 _ccy,
        uint256 _maturity,
        ProtocolTypes.Side _side,
        uint256 _amount,
        uint256 _unitPrice
    ) external payable override ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        tokenVault().depositFrom{value: msg.value}(msg.sender, _ccy, _amount);
        executePreOrder(_ccy, _maturity, _side, _amount, _unitPrice);

        return true;
    }

    /**
     * @notice Deposits funds with transfer approval of asset via permit function
     * and executes a pre-order at the same time.
     *
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _side Order position type, Borrow or Lend
     * @param _amount Amount of funds the maker wants to borrow/lend
     * @param _unitPrice Amount of unit price taker wish to borrow/lend
     * @param _deadline The deadline timestamp that the permit is valid
     * @param _permitV The V parameter of ERC712 permit sig
     * @param _permitR The R parameter of ERC712 permit sig
     * @param _permitS The S parameter of ERC712 permit sig
     * @return True if the execution of the operation succeeds
     */
    function depositWithPermitAndExecutePreOrder(
        bytes32 _ccy,
        uint256 _maturity,
        ProtocolTypes.Side _side,
        uint256 _amount,
        uint256 _unitPrice,
        uint256 _deadline,
        uint8 _permitV,
        bytes32 _permitR,
        bytes32 _permitS
    ) external override ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        tokenVault().depositWithPermitFrom(
            msg.sender,
            _ccy,
            _amount,
            _deadline,
            _permitV,
            _permitR,
            _permitS
        );

        executePreOrder(_ccy, _maturity, _side, _amount, _unitPrice);

        return true;
    }

    /**
     * @notice Unwinds user's lending or borrowing positions by creating an opposite position order.
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     */
    function unwindPosition(
        bytes32 _ccy,
        uint256 _maturity
    ) external override nonReentrant ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        LendingMarketUserLogic.unwindPosition(_ccy, _maturity, msg.sender, 0);
        return true;
    }

    /**
     * @notice Unwinds user's lending or borrowing positions by creating an opposite position order with a cap.
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _maxAmountInFV Maximum amount in future value to unwind. If 0, no cap is applied.
     * @return filledAmount The amount that is filled on the order book
     * @return filledAmountInFV The amount in the future value that is filled on the order book
     * @return feeInFV The order fee amount in the future value
     */
    function unwindPositionWithCap(
        bytes32 _ccy,
        uint256 _maturity,
        uint256 _maxAmountInFV
    )
        external
        override
        nonReentrant
        ifValidMaturity(_ccy, _maturity)
        ifActive
        returns (uint256 filledAmount, uint256 filledAmountInFV, uint256 feeInFV)
    {
        return LendingMarketUserLogic.unwindPosition(_ccy, _maturity, msg.sender, _maxAmountInFV);
    }

    /**
     * @notice Redeem user's lending positions.
     * Redemption can only be executed once the order book has matured after the currency has been delisted.
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     */
    function executeRedemption(
        bytes32 _ccy,
        uint256 _maturity
    ) external override nonReentrant ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        FundManagementLogic.executeRedemption(_ccy, _maturity, msg.sender);
        return true;
    }

    /**
     * @notice Repay user's borrowing positions.
     * Repayment can only be executed once the order book has matured after the currency has been delisted.
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     */
    function executeRepayment(
        bytes32 _ccy,
        uint256 _maturity
    ) external override nonReentrant ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        FundManagementLogic.executeRepayment(_ccy, _maturity, msg.sender, 0);
        return true;
    }

    /**
     * @notice Force settlement of all lending and borrowing positions.
     * This function is executed under the present value as of the termination date.
     *
     * @return True if the execution of the operation succeeds
     */
    function executeEmergencySettlement() external override nonReentrant ifInactive returns (bool) {
        FundManagementLogic.executeEmergencySettlement(msg.sender);
        return true;
    }

    /**
     * @notice Executes the Itayose call per selected currency.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the selected order book
     */
    function executeItayoseCall(
        bytes32 _ccy,
        uint256 _maturity
    ) external override nonReentrant ifActive returns (bool) {
        LendingMarketOperationLogic.executeItayoseCall(_ccy, _maturity);
        return true;
    }

    /**
     * @notice Executes one resumable Itayose process step.
     * @return completed True when this step finalized the order book
     */
    function executeItayoseStep(
        bytes32 _ccy,
        uint256 _maturity
    ) external override nonReentrant ifActive returns (bool completed) {
        return LendingMarketOperationLogic.executeItayoseStep(_ccy, _maturity);
    }

    /**
     * @notice Cancels the own order.
     * @param _ccy Currency name in bytes32 of the selected order book
     * @param _maturity The maturity of the selected order book
     * @param _orderId Market order id
     */
    function cancelOrder(
        bytes32 _ccy,
        uint256 _maturity,
        uint48 _orderId
    ) external override nonReentrant ifValidMaturity(_ccy, _maturity) ifActive returns (bool) {
        ILendingMarket(Storage.slot().lendingMarkets[_ccy]).cancelOrder(
            Storage.slot().maturityOrderBookIds[_ccy][_maturity],
            msg.sender,
            _orderId
        );

        return true;
    }

    /**
     * @notice Liquidates a lending or borrowing position if the user's coverage is hight.
     * @param _collateralCcy Currency name to be used as collateral
     * @param _debtCcy Currency name to be used as debt
     * @param _debtMaturity The order book maturity of the debt
     * @param _user User's address
     * @return True if the execution of the operation succeeds
     */
    function executeLiquidationCall(
        bytes32 _collateralCcy,
        bytes32 _debtCcy,
        uint256 _debtMaturity,
        address _user
    )
        external
        override
        isNotLocked
        ifValidMaturity(_debtCcy, _debtMaturity)
        ifActive
        returns (bool)
    {
        LiquidationLogic.executeLiquidation(
            msg.sender,
            _user,
            _collateralCcy,
            _debtCcy,
            _debtMaturity
        );

        return true;
    }

    /**
     * @notice Execute forced repayment for a borrowing position if repayment date is over.
     * @param _collateralCcy Currency name to be used as collateral
     * @param _debtCcy Currency name to be used as debt
     * @param _debtMaturity The order book maturity of the debt
     * @param _user User's address
     * @return True if the execution of the operation succeeds
     */
    function executeForcedRepayment(
        bytes32 _collateralCcy,
        bytes32 _debtCcy,
        uint256 _debtMaturity,
        address _user
    )
        external
        override
        isNotLocked
        ifValidMaturity(_debtCcy, _debtMaturity)
        ifActive
        returns (bool)
    {
        LiquidationLogic.executeForcedRepayment(
            msg.sender,
            _user,
            _collateralCcy,
            _debtCcy,
            _debtMaturity
        );

        return true;
    }

    /**
     * @notice Rotates the order books. In this rotation, the following actions are happened.
     * - Updates the maturity at the beginning of the order book array.
     * - Moves the beginning of the order book array to the end of it (Market rotation).
     * - Update the compound factor in this contract using the next order book unit price. (Auto-rolls)
     * - Clean up the reserve fund contract
     *
     * @param _ccy Currency name in bytes32 of the selected order book
     */
    function rotateOrderBooks(bytes32 _ccy) external override nonReentrant ifActive {
        LendingMarketOperationLogic.rotateOrderBooks(_ccy);
        FundManagementLogic.cleanUpFunds(_ccy, address(reserveFund()));
    }

    /**
     * @notice Executes an emergency termination to stop the protocol. Once this function is executed,
     * the protocol cannot be run again. Also, users will only be able to redeem and withdraw.
     */
    function executeEmergencyTermination() external override ifActive onlyOwner {
        LendingMarketOperationLogic.executeEmergencyTermination();
    }

    /**
     * @notice Pauses the lending market by currency
     * @param _ccy Currency for pausing all lending markets
     * @return True if the execution of the operation succeeds
     */
    function pauseLendingMarket(
        bytes32 _ccy
    ) external override ifActive onlyOperator returns (bool) {
        LendingMarketOperationLogic.pauseLendingMarket(_ccy);
        return true;
    }

    /**
     * @notice Unpauses the lending market by currency
     * @param _ccy Currency name in bytes32
     * @return True if the execution of the operation succeeds
     */
    function unpauseLendingMarket(
        bytes32 _ccy
    ) external override ifActive onlyOperator returns (bool) {
        LendingMarketOperationLogic.unpauseLendingMarket(_ccy);
        return true;
    }

    /**
     * @notice Clean up user funds used for lazy evaluation by the following actions:
     * - Removes order IDs that is already filled on the order book.
     * - Convert Future values that have already been auto-rolled to Genesis values.
     * @param _ccy Currency name in bytes32
     * @param _user User's address
     */
    function cleanUpFunds(
        bytes32 _ccy,
        address _user
    ) external override nonReentrant returns (uint256 totalActiveOrderCount) {
        return FundManagementLogic.cleanUpFunds(_ccy, _user);
    }

    /**
     * @notice Updates the min debt unit price for the selected currency.
     * @param _ccy Currency name in bytes32
     * @param _minDebtUnitPrice The min debt unit price
     */
    function updateMinDebtUnitPrice(
        bytes32 _ccy,
        uint256 _minDebtUnitPrice
    ) external override ifActive onlyOwner {
        LendingMarketOperationLogic.updateMinDebtUnitPrice(_ccy, _minDebtUnitPrice);
    }

    /**
     * @notice Withdraws ZCToken for the selected currency and maturity.
     * ZC perpetual token can be withdrawn only when the maturity is 0.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     * @param _amount The amount of ZCToken to mint
     * @return withdrawnAmount Actual amount withdrawn
     */
    function withdrawZCToken(
        bytes32 _ccy,
        uint256 _maturity,
        uint256 _amount
    ) external override nonReentrant ifActive returns (uint256 withdrawnAmount) {
        if (_maturity != 0 && !isValidMaturity(_ccy, _maturity)) revert InvalidMaturity();

        return LendingMarketUserLogic.withdrawZCToken(_ccy, _maturity, msg.sender, _amount);
    }

    /**
     * @notice Deposits ZCToken for the selected currency and maturity.
     * ZC perpetual token can be deposited only when the maturity is 0.
     * This function is not inactivated after the emergency termination to allow users to redeem ZCToken.
     * @param _ccy Currency name in bytes32
     * @param _maturity The maturity of the order book
     * @param _amount The amount of ZCToken to burn
     */
    function depositZCToken(
        bytes32 _ccy,
        uint256 _maturity,
        uint256 _amount
    ) external override nonReentrant {
        if (_maturity != 0 && !isValidMaturity(_ccy, _maturity)) revert InvalidMaturity();

        LendingMarketUserLogic.depositZCToken(_ccy, _maturity, msg.sender, _amount);
    }
}
Compiler settingsShow
{
  "metadata": {
    "useLiteralContent": true
  },
  "libraries": {
    "contracts/protocol/LendingMarketController.sol": {
      "LiquidationLogic": "0xb1963d4535e6ffa7a1b4fbef0965f77b93fd93ea",
      "FundManagementLogic": "0xdcf7206154188660e46b8c8991e219736dbe81bb",
      "LendingMarketUserLogic": "0x511b3956a07fb24b80776562f4719d61c195fd6f",
      "LendingMarketOperationLogic": "0x8e5c9d119f61f0c7e9eb484f6e82218223417a98"
    }
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  }
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.