ETH Price: $2,669.29 (-0.53%)

Contract

0xCEcc755730904b3A6c20CE9aABA181Aa4593F869
 

Overview

ETH Balance

0 ETH

Eth Value

$0.00

Multichain Info

No addresses found
Transaction Hash
Method
Block
From
To
0x60808060173923572023-06-02 10:06:23484 days ago1685700383IN
 Create: PositionsManager
0 ETH0.1459438431.43595675

View more zero value Internal Transactions in Advanced View mode

Advanced mode:
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
PositionsManager

Compiler Version
v0.8.19+commit.7dd6d404

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion, GNU AGPLv3 license
File 1 of 57 : PositionsManager.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IPool} from "@aave-v3-core/interfaces/IPool.sol";
import {IPositionsManager} from "./interfaces/IPositionsManager.sol";

import {Types} from "./libraries/Types.sol";
import {Errors} from "./libraries/Errors.sol";
import {Events} from "./libraries/Events.sol";
import {PoolLib} from "./libraries/PoolLib.sol";
import {Constants} from "./libraries/Constants.sol";
import {MarketBalanceLib} from "./libraries/MarketBalanceLib.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

import {ERC20, SafeTransferLib} from "@solmate/utils/SafeTransferLib.sol";
import {ERC20 as ERC20Permit2, Permit2Lib} from "@permit2/libraries/Permit2Lib.sol";

import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {MorphoStorage} from "./MorphoStorage.sol";
import {PositionsManagerInternal} from "./PositionsManagerInternal.sol";

/// @title PositionsManager
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Abstract contract exposing logic functions delegate-called by the `Morpho` contract.
contract PositionsManager is IPositionsManager, PositionsManagerInternal {
    using PoolLib for IPool;
    using MarketBalanceLib for Types.MarketBalances;

    using Math for uint256;
    using PercentageMath for uint256;

    using SafeTransferLib for ERC20;
    using Permit2Lib for ERC20Permit2;

    using EnumerableSet for EnumerableSet.AddressSet;

    /* EXTERNAL */

    /// @notice Implements the supply logic.
    /// @param underlying The address of the underlying asset to supply.
    /// @param amount The amount of `underlying` to supply.
    /// @param from The address to transfer the underlying from.
    /// @param onBehalf The address that will receive the supply position.
    /// @param maxIterations The maximum number of iterations allowed during the matching process.
    /// @return The amount supplied (in underlying).
    function supplyLogic(address underlying, uint256 amount, address from, address onBehalf, uint256 maxIterations)
        external
        returns (uint256)
    {
        Types.Market storage market = _validateSupply(underlying, amount, onBehalf);

        Types.Indexes256 memory indexes = _updateIndexes(underlying);

        ERC20Permit2(underlying).transferFrom2(from, address(this), amount);

        Types.SupplyRepayVars memory vars = _executeSupply(underlying, amount, from, onBehalf, maxIterations, indexes);

        _pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay);
        _pool.supplyToPool(underlying, vars.toSupply, indexes.supply.poolIndex);

        return amount;
    }

    /// @notice Implements the supply collateral logic.
    /// @dev Relies on Aave to check the supply cap when supplying collateral.
    /// @param underlying The address of the underlying asset to supply.
    /// @param amount The amount of `underlying` to supply.
    /// @param from The address to transfer the underlying from.
    /// @param onBehalf The address that will receive the collateral position.
    /// @return The collateral amount supplied (in underlying).
    function supplyCollateralLogic(address underlying, uint256 amount, address from, address onBehalf)
        external
        returns (uint256)
    {
        _validateSupplyCollateral(underlying, amount, onBehalf);

        Types.Indexes256 memory indexes = _updateIndexes(underlying);

        ERC20Permit2(underlying).transferFrom2(from, address(this), amount);

        _executeSupplyCollateral(underlying, amount, from, onBehalf, indexes.supply.poolIndex);

        _pool.supplyToPool(underlying, amount, indexes.supply.poolIndex);

        return amount;
    }

    /// @notice Implements the borrow logic.
    /// @param underlying The address of the underlying asset to borrow.
    /// @param amount The amount of `underlying` to borrow.
    /// @param borrower The address that will receive the debt position.
    /// @param receiver The address that will receive the borrowed funds.
    /// @param maxIterations The maximum number of iterations allowed during the matching process.
    /// @return The amount borrowed (in underlying).
    function borrowLogic(address underlying, uint256 amount, address borrower, address receiver, uint256 maxIterations)
        external
        returns (uint256)
    {
        Types.Market storage market = _validateBorrow(underlying, amount, borrower, receiver);

        Types.Indexes256 memory indexes = _updateIndexes(underlying);

        _authorizeBorrow(underlying, amount, indexes);

        Types.BorrowWithdrawVars memory vars =
            _executeBorrow(underlying, amount, borrower, receiver, maxIterations, indexes);

        // The following check requires accounting to have been performed.
        Types.LiquidityData memory values = _liquidityData(borrower);
        if (values.debt > values.borrowable) revert Errors.UnauthorizedBorrow();

        _pool.withdrawFromPool(underlying, market.aToken, vars.toWithdraw);
        _pool.borrowFromPool(underlying, vars.toBorrow);

        ERC20(underlying).safeTransfer(receiver, amount);

        return amount;
    }

    /// @notice Implements the repay logic.
    /// @param underlying The address of the underlying asset to borrow.
    /// @param amount The amount of `underlying` to repay.
    /// @param repayer The address that repays the underlying debt.
    /// @param onBehalf The address whose position will be repaid.
    /// @return The amount repaid (in underlying).
    function repayLogic(address underlying, uint256 amount, address repayer, address onBehalf)
        external
        returns (uint256)
    {
        Types.Market storage market = _validateRepay(underlying, amount, onBehalf);

        Types.Indexes256 memory indexes = _updateIndexes(underlying);
        amount = Math.min(_getUserBorrowBalanceFromIndexes(underlying, onBehalf, indexes), amount);

        if (amount == 0) revert Errors.DebtIsZero();

        ERC20Permit2(underlying).transferFrom2(repayer, address(this), amount);

        Types.SupplyRepayVars memory vars =
            _executeRepay(underlying, amount, repayer, onBehalf, _defaultIterations.repay, indexes);

        _pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay);
        _pool.supplyToPool(underlying, vars.toSupply, indexes.supply.poolIndex);

        return amount;
    }

    /// @notice Implements the withdraw logic.
    /// @param underlying The address of the underlying asset to withdraw.
    /// @param amount The amount of `underlying` to withdraw.
    /// @param supplier The address whose position will be withdrawn.
    /// @param receiver The address that will receive the withdrawn funds.
    /// @param maxIterations The maximum number of iterations allowed during the matching process.
    /// @return The amount withdrawn (in underlying).
    function withdrawLogic(
        address underlying,
        uint256 amount,
        address supplier,
        address receiver,
        uint256 maxIterations
    ) external returns (uint256) {
        Types.Market storage market = _validateWithdraw(underlying, amount, supplier, receiver);

        Types.Indexes256 memory indexes = _updateIndexes(underlying);
        amount = Math.min(_getUserSupplyBalanceFromIndexes(underlying, supplier, indexes), amount);

        if (amount == 0) revert Errors.SupplyIsZero();

        Types.BorrowWithdrawVars memory vars = _executeWithdraw(
            underlying, amount, supplier, receiver, Math.max(_defaultIterations.withdraw, maxIterations), indexes
        );

        _pool.withdrawFromPool(underlying, market.aToken, vars.toWithdraw);
        _pool.borrowFromPool(underlying, vars.toBorrow);

        ERC20(underlying).safeTransfer(receiver, amount);

        return amount;
    }

    /// @notice Implements the withdraw collateral logic.
    /// @param underlying The address of the underlying asset to withdraw.
    /// @param amount The amount of `underlying` to withdraw.
    /// @param supplier The address whose position will be withdrawn.
    /// @param receiver The address that will receive the withdrawn funds.
    /// @return The collateral amount withdrawn (in underlying).
    function withdrawCollateralLogic(address underlying, uint256 amount, address supplier, address receiver)
        external
        returns (uint256)
    {
        Types.Market storage market = _validateWithdrawCollateral(underlying, amount, supplier, receiver);

        Types.Indexes256 memory indexes = _updateIndexes(underlying);
        uint256 poolSupplyIndex = indexes.supply.poolIndex;
        amount = Math.min(_getUserCollateralBalanceFromIndex(underlying, supplier, poolSupplyIndex), amount);

        if (amount == 0) revert Errors.CollateralIsZero();

        _executeWithdrawCollateral(underlying, amount, supplier, receiver, poolSupplyIndex);

        // The following check requires accounting to have been performed.
        if (_getUserHealthFactor(supplier) < Constants.DEFAULT_LIQUIDATION_MAX_HF) {
            revert Errors.UnauthorizedWithdraw();
        }

        _pool.withdrawFromPool(underlying, market.aToken, amount);

        ERC20(underlying).safeTransfer(receiver, amount);

        return amount;
    }

    /// @notice Implements the liquidation logic.
    /// @param underlyingBorrowed The address of the underlying borrowed to repay.
    /// @param underlyingCollateral The address of the underlying collateral to seize.
    /// @param amount The amount of `underlyingBorrowed` to repay.
    /// @param borrower The address of the borrower to liquidate.
    /// @param liquidator The address that will liquidate the borrower.
    /// @return The `underlyingBorrowed` amount repaid (in underlying) and the `underlyingCollateral` amount seized (in underlying).
    function liquidateLogic(
        address underlyingBorrowed,
        address underlyingCollateral,
        uint256 amount,
        address borrower,
        address liquidator
    ) external returns (uint256, uint256) {
        _validateLiquidate(underlyingBorrowed, underlyingCollateral, borrower);

        Types.Indexes256 memory borrowIndexes = _updateIndexes(underlyingBorrowed);
        Types.Indexes256 memory collateralIndexes = _updateIndexes(underlyingCollateral);

        Types.LiquidateVars memory vars;
        vars.closeFactor = _authorizeLiquidate(underlyingBorrowed, borrower);

        amount = Math.min(
            _getUserBorrowBalanceFromIndexes(underlyingBorrowed, borrower, borrowIndexes).percentMul(vars.closeFactor), // Max liquidatable debt.
            amount
        );

        // If the check is done later, it is ambiguous whether debt is truly zero or whether there's not enough collateral to cover for 1 dust of debt.
        if (amount == 0) revert Errors.DebtIsZero();

        (amount, vars.seized) = _calculateAmountToSeize(
            underlyingBorrowed, underlyingCollateral, amount, borrower, collateralIndexes.supply.poolIndex
        );

        if (vars.seized == 0) revert Errors.CollateralIsZero();
        if (amount == 0) revert Errors.DebtIsZero(); // `amount` could still be zero because there's not enough collateral to cover for 1 dust of debt.

        ERC20Permit2(underlyingBorrowed).transferFrom2(liquidator, address(this), amount);

        Types.SupplyRepayVars memory repayVars =
            _executeRepay(underlyingBorrowed, amount, liquidator, borrower, 0, borrowIndexes);
        _executeWithdrawCollateral(
            underlyingCollateral, vars.seized, borrower, liquidator, collateralIndexes.supply.poolIndex
        );

        _pool.repayToPool(underlyingBorrowed, _market[underlyingBorrowed].variableDebtToken, repayVars.toRepay);
        _pool.supplyToPool(underlyingBorrowed, repayVars.toSupply, borrowIndexes.supply.poolIndex);
        _pool.withdrawFromPool(underlyingCollateral, _market[underlyingCollateral].aToken, vars.seized);

        ERC20(underlyingCollateral).safeTransfer(liquidator, vars.seized);

        emit Events.Liquidated(liquidator, borrower, underlyingBorrowed, amount, underlyingCollateral, vars.seized);

        return (amount, vars.seized);
    }
}

File 2 of 57 : IPool.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IPoolAddressesProvider} from './IPoolAddressesProvider.sol';
import {DataTypes} from '../protocol/libraries/types/DataTypes.sol';

/**
 * @title IPool
 * @author Aave
 * @notice Defines the basic interface for an Aave Pool.
 */
interface IPool {
  /**
   * @dev Emitted on mintUnbacked()
   * @param reserve The address of the underlying asset of the reserve
   * @param user The address initiating the supply
   * @param onBehalfOf The beneficiary of the supplied assets, receiving the aTokens
   * @param amount The amount of supplied assets
   * @param referralCode The referral code used
   */
  event MintUnbacked(
    address indexed reserve,
    address user,
    address indexed onBehalfOf,
    uint256 amount,
    uint16 indexed referralCode
  );

  /**
   * @dev Emitted on backUnbacked()
   * @param reserve The address of the underlying asset of the reserve
   * @param backer The address paying for the backing
   * @param amount The amount added as backing
   * @param fee The amount paid in fees
   */
  event BackUnbacked(address indexed reserve, address indexed backer, uint256 amount, uint256 fee);

  /**
   * @dev Emitted on supply()
   * @param reserve The address of the underlying asset of the reserve
   * @param user The address initiating the supply
   * @param onBehalfOf The beneficiary of the supply, receiving the aTokens
   * @param amount The amount supplied
   * @param referralCode The referral code used
   */
  event Supply(
    address indexed reserve,
    address user,
    address indexed onBehalfOf,
    uint256 amount,
    uint16 indexed referralCode
  );

  /**
   * @dev Emitted on withdraw()
   * @param reserve The address of the underlying asset being withdrawn
   * @param user The address initiating the withdrawal, owner of aTokens
   * @param to The address that will receive the underlying
   * @param amount The amount to be withdrawn
   */
  event Withdraw(address indexed reserve, address indexed user, address indexed to, uint256 amount);

  /**
   * @dev Emitted on borrow() and flashLoan() when debt needs to be opened
   * @param reserve The address of the underlying asset being borrowed
   * @param user The address of the user initiating the borrow(), receiving the funds on borrow() or just
   * initiator of the transaction on flashLoan()
   * @param onBehalfOf The address that will be getting the debt
   * @param amount The amount borrowed out
   * @param interestRateMode The rate mode: 1 for Stable, 2 for Variable
   * @param borrowRate The numeric rate at which the user has borrowed, expressed in ray
   * @param referralCode The referral code used
   */
  event Borrow(
    address indexed reserve,
    address user,
    address indexed onBehalfOf,
    uint256 amount,
    DataTypes.InterestRateMode interestRateMode,
    uint256 borrowRate,
    uint16 indexed referralCode
  );

  /**
   * @dev Emitted on repay()
   * @param reserve The address of the underlying asset of the reserve
   * @param user The beneficiary of the repayment, getting his debt reduced
   * @param repayer The address of the user initiating the repay(), providing the funds
   * @param amount The amount repaid
   * @param useATokens True if the repayment is done using aTokens, `false` if done with underlying asset directly
   */
  event Repay(
    address indexed reserve,
    address indexed user,
    address indexed repayer,
    uint256 amount,
    bool useATokens
  );

  /**
   * @dev Emitted on swapBorrowRateMode()
   * @param reserve The address of the underlying asset of the reserve
   * @param user The address of the user swapping his rate mode
   * @param interestRateMode The current interest rate mode of the position being swapped: 1 for Stable, 2 for Variable
   */
  event SwapBorrowRateMode(
    address indexed reserve,
    address indexed user,
    DataTypes.InterestRateMode interestRateMode
  );

  /**
   * @dev Emitted on borrow(), repay() and liquidationCall() when using isolated assets
   * @param asset The address of the underlying asset of the reserve
   * @param totalDebt The total isolation mode debt for the reserve
   */
  event IsolationModeTotalDebtUpdated(address indexed asset, uint256 totalDebt);

  /**
   * @dev Emitted when the user selects a certain asset category for eMode
   * @param user The address of the user
   * @param categoryId The category id
   */
  event UserEModeSet(address indexed user, uint8 categoryId);

  /**
   * @dev Emitted on setUserUseReserveAsCollateral()
   * @param reserve The address of the underlying asset of the reserve
   * @param user The address of the user enabling the usage as collateral
   */
  event ReserveUsedAsCollateralEnabled(address indexed reserve, address indexed user);

  /**
   * @dev Emitted on setUserUseReserveAsCollateral()
   * @param reserve The address of the underlying asset of the reserve
   * @param user The address of the user enabling the usage as collateral
   */
  event ReserveUsedAsCollateralDisabled(address indexed reserve, address indexed user);

  /**
   * @dev Emitted on rebalanceStableBorrowRate()
   * @param reserve The address of the underlying asset of the reserve
   * @param user The address of the user for which the rebalance has been executed
   */
  event RebalanceStableBorrowRate(address indexed reserve, address indexed user);

  /**
   * @dev Emitted on flashLoan()
   * @param target The address of the flash loan receiver contract
   * @param initiator The address initiating the flash loan
   * @param asset The address of the asset being flash borrowed
   * @param amount The amount flash borrowed
   * @param interestRateMode The flashloan mode: 0 for regular flashloan, 1 for Stable debt, 2 for Variable debt
   * @param premium The fee flash borrowed
   * @param referralCode The referral code used
   */
  event FlashLoan(
    address indexed target,
    address initiator,
    address indexed asset,
    uint256 amount,
    DataTypes.InterestRateMode interestRateMode,
    uint256 premium,
    uint16 indexed referralCode
  );

  /**
   * @dev Emitted when a borrower is liquidated.
   * @param collateralAsset The address of the underlying asset used as collateral, to receive as result of the liquidation
   * @param debtAsset The address of the underlying borrowed asset to be repaid with the liquidation
   * @param user The address of the borrower getting liquidated
   * @param debtToCover The debt amount of borrowed `asset` the liquidator wants to cover
   * @param liquidatedCollateralAmount The amount of collateral received by the liquidator
   * @param liquidator The address of the liquidator
   * @param receiveAToken True if the liquidators wants to receive the collateral aTokens, `false` if he wants
   * to receive the underlying collateral asset directly
   */
  event LiquidationCall(
    address indexed collateralAsset,
    address indexed debtAsset,
    address indexed user,
    uint256 debtToCover,
    uint256 liquidatedCollateralAmount,
    address liquidator,
    bool receiveAToken
  );

  /**
   * @dev Emitted when the state of a reserve is updated.
   * @param reserve The address of the underlying asset of the reserve
   * @param liquidityRate The next liquidity rate
   * @param stableBorrowRate The next stable borrow rate
   * @param variableBorrowRate The next variable borrow rate
   * @param liquidityIndex The next liquidity index
   * @param variableBorrowIndex The next variable borrow index
   */
  event ReserveDataUpdated(
    address indexed reserve,
    uint256 liquidityRate,
    uint256 stableBorrowRate,
    uint256 variableBorrowRate,
    uint256 liquidityIndex,
    uint256 variableBorrowIndex
  );

  /**
   * @dev Emitted when the protocol treasury receives minted aTokens from the accrued interest.
   * @param reserve The address of the reserve
   * @param amountMinted The amount minted to the treasury
   */
  event MintedToTreasury(address indexed reserve, uint256 amountMinted);

  /**
   * @notice Mints an `amount` of aTokens to the `onBehalfOf`
   * @param asset The address of the underlying asset to mint
   * @param amount The amount to mint
   * @param onBehalfOf The address that will receive the aTokens
   * @param referralCode Code used to register the integrator originating the operation, for potential rewards.
   *   0 if the action is executed directly by the user, without any middle-man
   */
  function mintUnbacked(
    address asset,
    uint256 amount,
    address onBehalfOf,
    uint16 referralCode
  ) external;

  /**
   * @notice Back the current unbacked underlying with `amount` and pay `fee`.
   * @param asset The address of the underlying asset to back
   * @param amount The amount to back
   * @param fee The amount paid in fees
   * @return The backed amount
   */
  function backUnbacked(
    address asset,
    uint256 amount,
    uint256 fee
  ) external returns (uint256);

  /**
   * @notice Supplies an `amount` of underlying asset into the reserve, receiving in return overlying aTokens.
   * - E.g. User supplies 100 USDC and gets in return 100 aUSDC
   * @param asset The address of the underlying asset to supply
   * @param amount The amount to be supplied
   * @param onBehalfOf The address that will receive the aTokens, same as msg.sender if the user
   *   wants to receive them on his own wallet, or a different address if the beneficiary of aTokens
   *   is a different wallet
   * @param referralCode Code used to register the integrator originating the operation, for potential rewards.
   *   0 if the action is executed directly by the user, without any middle-man
   */
  function supply(
    address asset,
    uint256 amount,
    address onBehalfOf,
    uint16 referralCode
  ) external;

  /**
   * @notice Supply with transfer approval of asset to be supplied done via permit function
   * see: https://eips.ethereum.org/EIPS/eip-2612 and https://eips.ethereum.org/EIPS/eip-713
   * @param asset The address of the underlying asset to supply
   * @param amount The amount to be supplied
   * @param onBehalfOf The address that will receive the aTokens, same as msg.sender if the user
   *   wants to receive them on his own wallet, or a different address if the beneficiary of aTokens
   *   is a different wallet
   * @param deadline The deadline timestamp that the permit is valid
   * @param referralCode Code used to register the integrator originating the operation, for potential rewards.
   *   0 if the action is executed directly by the user, without any middle-man
   * @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
   */
  function supplyWithPermit(
    address asset,
    uint256 amount,
    address onBehalfOf,
    uint16 referralCode,
    uint256 deadline,
    uint8 permitV,
    bytes32 permitR,
    bytes32 permitS
  ) external;

  /**
   * @notice Withdraws an `amount` of underlying asset from the reserve, burning the equivalent aTokens owned
   * E.g. User has 100 aUSDC, calls withdraw() and receives 100 USDC, burning the 100 aUSDC
   * @param asset The address of the underlying asset to withdraw
   * @param amount The underlying amount to be withdrawn
   *   - Send the value type(uint256).max in order to withdraw the whole aToken balance
   * @param to The address that will receive the underlying, same as msg.sender if the user
   *   wants to receive it on his own wallet, or a different address if the beneficiary is a
   *   different wallet
   * @return The final amount withdrawn
   */
  function withdraw(
    address asset,
    uint256 amount,
    address to
  ) external returns (uint256);

  /**
   * @notice Allows users to borrow a specific `amount` of the reserve underlying asset, provided that the borrower
   * already supplied enough collateral, or he was given enough allowance by a credit delegator on the
   * corresponding debt token (StableDebtToken or VariableDebtToken)
   * - E.g. User borrows 100 USDC passing as `onBehalfOf` his own address, receiving the 100 USDC in his wallet
   *   and 100 stable/variable debt tokens, depending on the `interestRateMode`
   * @param asset The address of the underlying asset to borrow
   * @param amount The amount to be borrowed
   * @param interestRateMode The interest rate mode at which the user wants to borrow: 1 for Stable, 2 for Variable
   * @param referralCode The code used to register the integrator originating the operation, for potential rewards.
   *   0 if the action is executed directly by the user, without any middle-man
   * @param onBehalfOf The address of the user who will receive the debt. Should be the address of the borrower itself
   * calling the function if he wants to borrow against his own collateral, or the address of the credit delegator
   * if he has been given credit delegation allowance
   */
  function borrow(
    address asset,
    uint256 amount,
    uint256 interestRateMode,
    uint16 referralCode,
    address onBehalfOf
  ) external;

  /**
   * @notice Repays a borrowed `amount` on a specific reserve, burning the equivalent debt tokens owned
   * - E.g. User repays 100 USDC, burning 100 variable/stable debt tokens of the `onBehalfOf` address
   * @param asset The address of the borrowed underlying asset previously borrowed
   * @param amount The amount to repay
   * - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
   * @param interestRateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
   * @param onBehalfOf The address of the user who will get his debt reduced/removed. Should be the address of the
   * user calling the function if he wants to reduce/remove his own debt, or the address of any other
   * other borrower whose debt should be removed
   * @return The final amount repaid
   */
  function repay(
    address asset,
    uint256 amount,
    uint256 interestRateMode,
    address onBehalfOf
  ) external returns (uint256);

  /**
   * @notice Repay with transfer approval of asset to be repaid done via permit function
   * see: https://eips.ethereum.org/EIPS/eip-2612 and https://eips.ethereum.org/EIPS/eip-713
   * @param asset The address of the borrowed underlying asset previously borrowed
   * @param amount The amount to repay
   * - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
   * @param interestRateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
   * @param onBehalfOf Address of the user who will get his debt reduced/removed. Should be the address of the
   * user calling the function if he wants to reduce/remove his own debt, or the address of any other
   * other borrower whose debt should be removed
   * @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 The final amount repaid
   */
  function repayWithPermit(
    address asset,
    uint256 amount,
    uint256 interestRateMode,
    address onBehalfOf,
    uint256 deadline,
    uint8 permitV,
    bytes32 permitR,
    bytes32 permitS
  ) external returns (uint256);

  /**
   * @notice Repays a borrowed `amount` on a specific reserve using the reserve aTokens, burning the
   * equivalent debt tokens
   * - E.g. User repays 100 USDC using 100 aUSDC, burning 100 variable/stable debt tokens
   * @dev  Passing uint256.max as amount will clean up any residual aToken dust balance, if the user aToken
   * balance is not enough to cover the whole debt
   * @param asset The address of the borrowed underlying asset previously borrowed
   * @param amount The amount to repay
   * - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
   * @param interestRateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
   * @return The final amount repaid
   */
  function repayWithATokens(
    address asset,
    uint256 amount,
    uint256 interestRateMode
  ) external returns (uint256);

  /**
   * @notice Allows a borrower to swap his debt between stable and variable mode, or vice versa
   * @param asset The address of the underlying asset borrowed
   * @param interestRateMode The current interest rate mode of the position being swapped: 1 for Stable, 2 for Variable
   */
  function swapBorrowRateMode(address asset, uint256 interestRateMode) external;

  /**
   * @notice Rebalances the stable interest rate of a user to the current stable rate defined on the reserve.
   * - Users can be rebalanced if the following conditions are satisfied:
   *     1. Usage ratio is above 95%
   *     2. the current supply APY is below REBALANCE_UP_THRESHOLD * maxVariableBorrowRate, which means that too
   *        much has been borrowed at a stable rate and suppliers are not earning enough
   * @param asset The address of the underlying asset borrowed
   * @param user The address of the user to be rebalanced
   */
  function rebalanceStableBorrowRate(address asset, address user) external;

  /**
   * @notice Allows suppliers to enable/disable a specific supplied asset as collateral
   * @param asset The address of the underlying asset supplied
   * @param useAsCollateral True if the user wants to use the supply as collateral, false otherwise
   */
  function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external;

  /**
   * @notice Function to liquidate a non-healthy position collateral-wise, with Health Factor below 1
   * - The caller (liquidator) covers `debtToCover` amount of debt of the user getting liquidated, and receives
   *   a proportionally amount of the `collateralAsset` plus a bonus to cover market risk
   * @param collateralAsset The address of the underlying asset used as collateral, to receive as result of the liquidation
   * @param debtAsset The address of the underlying borrowed asset to be repaid with the liquidation
   * @param user The address of the borrower getting liquidated
   * @param debtToCover The debt amount of borrowed `asset` the liquidator wants to cover
   * @param receiveAToken True if the liquidators wants to receive the collateral aTokens, `false` if he wants
   * to receive the underlying collateral asset directly
   */
  function liquidationCall(
    address collateralAsset,
    address debtAsset,
    address user,
    uint256 debtToCover,
    bool receiveAToken
  ) external;

  /**
   * @notice Allows smartcontracts to access the liquidity of the pool within one transaction,
   * as long as the amount taken plus a fee is returned.
   * @dev IMPORTANT There are security concerns for developers of flashloan receiver contracts that must be kept
   * into consideration. For further details please visit https://docs.aave.com/developers/
   * @param receiverAddress The address of the contract receiving the funds, implementing IFlashLoanReceiver interface
   * @param assets The addresses of the assets being flash-borrowed
   * @param amounts The amounts of the assets being flash-borrowed
   * @param interestRateModes Types of the debt to open if the flash loan is not returned:
   *   0 -> Don't open any debt, just revert if funds can't be transferred from the receiver
   *   1 -> Open debt at stable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
   *   2 -> Open debt at variable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
   * @param onBehalfOf The address  that will receive the debt in the case of using on `modes` 1 or 2
   * @param params Variadic packed params to pass to the receiver as extra information
   * @param referralCode The code used to register the integrator originating the operation, for potential rewards.
   *   0 if the action is executed directly by the user, without any middle-man
   */
  function flashLoan(
    address receiverAddress,
    address[] calldata assets,
    uint256[] calldata amounts,
    uint256[] calldata interestRateModes,
    address onBehalfOf,
    bytes calldata params,
    uint16 referralCode
  ) external;

  /**
   * @notice Allows smartcontracts to access the liquidity of the pool within one transaction,
   * as long as the amount taken plus a fee is returned.
   * @dev IMPORTANT There are security concerns for developers of flashloan receiver contracts that must be kept
   * into consideration. For further details please visit https://docs.aave.com/developers/
   * @param receiverAddress The address of the contract receiving the funds, implementing IFlashLoanSimpleReceiver interface
   * @param asset The address of the asset being flash-borrowed
   * @param amount The amount of the asset being flash-borrowed
   * @param params Variadic packed params to pass to the receiver as extra information
   * @param referralCode The code used to register the integrator originating the operation, for potential rewards.
   *   0 if the action is executed directly by the user, without any middle-man
   */
  function flashLoanSimple(
    address receiverAddress,
    address asset,
    uint256 amount,
    bytes calldata params,
    uint16 referralCode
  ) external;

  /**
   * @notice Returns the user account data across all the reserves
   * @param user The address of the user
   * @return totalCollateralBase The total collateral of the user in the base currency used by the price feed
   * @return totalDebtBase The total debt of the user in the base currency used by the price feed
   * @return availableBorrowsBase The borrowing power left of the user in the base currency used by the price feed
   * @return currentLiquidationThreshold The liquidation threshold of the user
   * @return ltv The loan to value of The user
   * @return healthFactor The current health factor of the user
   */
  function getUserAccountData(address user)
    external
    view
    returns (
      uint256 totalCollateralBase,
      uint256 totalDebtBase,
      uint256 availableBorrowsBase,
      uint256 currentLiquidationThreshold,
      uint256 ltv,
      uint256 healthFactor
    );

  /**
   * @notice Initializes a reserve, activating it, assigning an aToken and debt tokens and an
   * interest rate strategy
   * @dev Only callable by the PoolConfigurator contract
   * @param asset The address of the underlying asset of the reserve
   * @param aTokenAddress The address of the aToken that will be assigned to the reserve
   * @param stableDebtAddress The address of the StableDebtToken that will be assigned to the reserve
   * @param variableDebtAddress The address of the VariableDebtToken that will be assigned to the reserve
   * @param interestRateStrategyAddress The address of the interest rate strategy contract
   */
  function initReserve(
    address asset,
    address aTokenAddress,
    address stableDebtAddress,
    address variableDebtAddress,
    address interestRateStrategyAddress
  ) external;

  /**
   * @notice Drop a reserve
   * @dev Only callable by the PoolConfigurator contract
   * @param asset The address of the underlying asset of the reserve
   */
  function dropReserve(address asset) external;

  /**
   * @notice Updates the address of the interest rate strategy contract
   * @dev Only callable by the PoolConfigurator contract
   * @param asset The address of the underlying asset of the reserve
   * @param rateStrategyAddress The address of the interest rate strategy contract
   */
  function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress)
    external;

  /**
   * @notice Sets the configuration bitmap of the reserve as a whole
   * @dev Only callable by the PoolConfigurator contract
   * @param asset The address of the underlying asset of the reserve
   * @param configuration The new configuration bitmap
   */
  function setConfiguration(address asset, DataTypes.ReserveConfigurationMap calldata configuration)
    external;

  /**
   * @notice Returns the configuration of the reserve
   * @param asset The address of the underlying asset of the reserve
   * @return The configuration of the reserve
   */
  function getConfiguration(address asset)
    external
    view
    returns (DataTypes.ReserveConfigurationMap memory);

  /**
   * @notice Returns the configuration of the user across all the reserves
   * @param user The user address
   * @return The configuration of the user
   */
  function getUserConfiguration(address user)
    external
    view
    returns (DataTypes.UserConfigurationMap memory);

  /**
   * @notice Returns the normalized income of the reserve
   * @param asset The address of the underlying asset of the reserve
   * @return The reserve's normalized income
   */
  function getReserveNormalizedIncome(address asset) external view returns (uint256);

  /**
   * @notice Returns the normalized variable debt per unit of asset
   * @dev WARNING: This function is intended to be used primarily by the protocol itself to get a
   * "dynamic" variable index based on time, current stored index and virtual rate at the current
   * moment (approx. a borrower would get if opening a position). This means that is always used in
   * combination with variable debt supply/balances.
   * If using this function externally, consider that is possible to have an increasing normalized
   * variable debt that is not equivalent to how the variable debt index would be updated in storage
   * (e.g. only updates with non-zero variable debt supply)
   * @param asset The address of the underlying asset of the reserve
   * @return The reserve normalized variable debt
   */
  function getReserveNormalizedVariableDebt(address asset) external view returns (uint256);

  /**
   * @notice Returns the state and configuration of the reserve
   * @param asset The address of the underlying asset of the reserve
   * @return The state and configuration data of the reserve
   */
  function getReserveData(address asset) external view returns (DataTypes.ReserveData memory);

  /**
   * @notice Validates and finalizes an aToken transfer
   * @dev Only callable by the overlying aToken of the `asset`
   * @param asset The address of the underlying asset of the aToken
   * @param from The user from which the aTokens are transferred
   * @param to The user receiving the aTokens
   * @param amount The amount being transferred/withdrawn
   * @param balanceFromBefore The aToken balance of the `from` user before the transfer
   * @param balanceToBefore The aToken balance of the `to` user before the transfer
   */
  function finalizeTransfer(
    address asset,
    address from,
    address to,
    uint256 amount,
    uint256 balanceFromBefore,
    uint256 balanceToBefore
  ) external;

  /**
   * @notice Returns the list of the underlying assets of all the initialized reserves
   * @dev It does not include dropped reserves
   * @return The addresses of the underlying assets of the initialized reserves
   */
  function getReservesList() external view returns (address[] memory);

  /**
   * @notice Returns the address of the underlying asset of a reserve by the reserve id as stored in the DataTypes.ReserveData struct
   * @param id The id of the reserve as stored in the DataTypes.ReserveData struct
   * @return The address of the reserve associated with id
   */
  function getReserveAddressById(uint16 id) external view returns (address);

  /**
   * @notice Returns the PoolAddressesProvider connected to this contract
   * @return The address of the PoolAddressesProvider
   */
  function ADDRESSES_PROVIDER() external view returns (IPoolAddressesProvider);

  /**
   * @notice Updates the protocol fee on the bridging
   * @param bridgeProtocolFee The part of the premium sent to the protocol treasury
   */
  function updateBridgeProtocolFee(uint256 bridgeProtocolFee) external;

  /**
   * @notice Updates flash loan premiums. Flash loan premium consists of two parts:
   * - A part is sent to aToken holders as extra, one time accumulated interest
   * - A part is collected by the protocol treasury
   * @dev The total premium is calculated on the total borrowed amount
   * @dev The premium to protocol is calculated on the total premium, being a percentage of `flashLoanPremiumTotal`
   * @dev Only callable by the PoolConfigurator contract
   * @param flashLoanPremiumTotal The total premium, expressed in bps
   * @param flashLoanPremiumToProtocol The part of the premium sent to the protocol treasury, expressed in bps
   */
  function updateFlashloanPremiums(
    uint128 flashLoanPremiumTotal,
    uint128 flashLoanPremiumToProtocol
  ) external;

  /**
   * @notice Configures a new category for the eMode.
   * @dev In eMode, the protocol allows very high borrowing power to borrow assets of the same category.
   * The category 0 is reserved as it's the default for volatile assets
   * @param id The id of the category
   * @param config The configuration of the category
   */
  function configureEModeCategory(uint8 id, DataTypes.EModeCategory memory config) external;

  /**
   * @notice Returns the data of an eMode category
   * @param id The id of the category
   * @return The configuration data of the category
   */
  function getEModeCategoryData(uint8 id) external view returns (DataTypes.EModeCategory memory);

  /**
   * @notice Allows a user to use the protocol in eMode
   * @param categoryId The id of the category
   */
  function setUserEMode(uint8 categoryId) external;

  /**
   * @notice Returns the eMode the user is using
   * @param user The address of the user
   * @return The eMode id
   */
  function getUserEMode(address user) external view returns (uint256);

  /**
   * @notice Resets the isolation mode total debt of the given asset to zero
   * @dev It requires the given asset has zero debt ceiling
   * @param asset The address of the underlying asset to reset the isolationModeTotalDebt
   */
  function resetIsolationModeTotalDebt(address asset) external;

  /**
   * @notice Returns the percentage of available liquidity that can be borrowed at once at stable rate
   * @return The percentage of available liquidity to borrow, expressed in bps
   */
  function MAX_STABLE_RATE_BORROW_SIZE_PERCENT() external view returns (uint256);

  /**
   * @notice Returns the total fee on flash loans
   * @return The total fee on flashloans
   */
  function FLASHLOAN_PREMIUM_TOTAL() external view returns (uint128);

  /**
   * @notice Returns the part of the bridge fees sent to protocol
   * @return The bridge fee sent to the protocol treasury
   */
  function BRIDGE_PROTOCOL_FEE() external view returns (uint256);

  /**
   * @notice Returns the part of the flashloan fees sent to protocol
   * @return The flashloan fee sent to the protocol treasury
   */
  function FLASHLOAN_PREMIUM_TO_PROTOCOL() external view returns (uint128);

  /**
   * @notice Returns the maximum number of reserves supported to be listed in this Pool
   * @return The maximum number of reserves supported
   */
  function MAX_NUMBER_RESERVES() external view returns (uint16);

  /**
   * @notice Mints the assets accrued through the reserve factor to the treasury in the form of aTokens
   * @param assets The list of reserves for which the minting needs to be executed
   */
  function mintToTreasury(address[] calldata assets) external;

  /**
   * @notice Rescue and transfer tokens locked in this contract
   * @param token The address of the token
   * @param to The address of the recipient
   * @param amount The amount of token to transfer
   */
  function rescueTokens(
    address token,
    address to,
    uint256 amount
  ) external;

  /**
   * @notice Supplies an `amount` of underlying asset into the reserve, receiving in return overlying aTokens.
   * - E.g. User supplies 100 USDC and gets in return 100 aUSDC
   * @dev Deprecated: Use the `supply` function instead
   * @param asset The address of the underlying asset to supply
   * @param amount The amount to be supplied
   * @param onBehalfOf The address that will receive the aTokens, same as msg.sender if the user
   *   wants to receive them on his own wallet, or a different address if the beneficiary of aTokens
   *   is a different wallet
   * @param referralCode Code used to register the integrator originating the operation, for potential rewards.
   *   0 if the action is executed directly by the user, without any middle-man
   */
  function deposit(
    address asset,
    uint256 amount,
    address onBehalfOf,
    uint16 referralCode
  ) external;
}

File 3 of 57 : IPositionsManager.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.5.0;

interface IPositionsManager {
    function supplyLogic(address underlying, uint256 amount, address from, address onBehalf, uint256 maxIterations)
        external
        returns (uint256 supplied);

    function supplyCollateralLogic(address underlying, uint256 amount, address from, address onBehalf)
        external
        returns (uint256 supplied);

    function borrowLogic(address underlying, uint256 amount, address borrower, address receiver, uint256 maxIterations)
        external
        returns (uint256 borrowed);

    function repayLogic(address underlying, uint256 amount, address repayer, address onBehalf)
        external
        returns (uint256 repaid);

    function withdrawLogic(
        address underlying,
        uint256 amount,
        address supplier,
        address receiver,
        uint256 maxIterations
    ) external returns (uint256 withdrawn);
    function withdrawCollateralLogic(address underlying, uint256 amount, address supplier, address receiver)
        external
        returns (uint256 withdrawn);

    function liquidateLogic(
        address underlyingBorrowed,
        address underlyingCollateral,
        uint256 amount,
        address borrower,
        address liquidator
    ) external returns (uint256 liquidated, uint256 seized);
}

File 4 of 57 : Types.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IAaveOracle} from "@aave-v3-core/interfaces/IAaveOracle.sol";

import {DataTypes} from "@aave-v3-core/protocol/libraries/types/DataTypes.sol";

import {LogarithmicBuckets} from "@morpho-data-structures/LogarithmicBuckets.sol";

/// @title Types
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library exposing all Types used in Morpho.
library Types {
    /* ENUMS */

    /// @notice Enumeration of the different position types in the protocol.
    enum Position {
        POOL_SUPPLIER,
        P2P_SUPPLIER,
        POOL_BORROWER,
        P2P_BORROWER
    }

    /* NESTED STRUCTS */

    /// @notice Contains the market side delta data.
    struct MarketSideDelta {
        uint256 scaledDelta; // The delta amount in pool unit.
        uint256 scaledP2PTotal; // The total peer-to-peer amount in peer-to-peer unit.
    }

    /// @notice Contains the delta data for both `supply` and `borrow`.
    struct Deltas {
        MarketSideDelta supply; // The `MarketSideDelta` related to the supply side.
        MarketSideDelta borrow; // The `MarketSideDelta` related to the borrow side.
    }

    /// @notice Contains the market side indexes.
    struct MarketSideIndexes {
        uint128 poolIndex; // The pool index (in ray).
        uint128 p2pIndex; // The peer-to-peer index (in ray).
    }

    /// @notice Contains the indexes for both `supply` and `borrow`.
    struct Indexes {
        MarketSideIndexes supply; // The `MarketSideIndexes` related to the supply side.
        MarketSideIndexes borrow; // The `MarketSideIndexes` related to the borrow side.
    }

    /// @notice Contains the different pauses statuses possible in Morpho.
    struct PauseStatuses {
        bool isP2PDisabled;
        bool isSupplyPaused;
        bool isSupplyCollateralPaused;
        bool isBorrowPaused;
        bool isWithdrawPaused;
        bool isWithdrawCollateralPaused;
        bool isRepayPaused;
        bool isLiquidateCollateralPaused;
        bool isLiquidateBorrowPaused;
        bool isDeprecated;
    }

    /* STORAGE STRUCTS */

    /// @notice Contains the market data that is stored in storage.
    /// @dev This market struct is able to be passed into memory.
    struct Market {
        // SLOT 0-1
        Indexes indexes;
        // SLOT 2-5
        Deltas deltas; // 1024 bits
        // SLOT 6
        address underlying; // 160 bits
        PauseStatuses pauseStatuses; // 80 bits
        bool isCollateral; // 8 bits
        // SLOT 7
        address variableDebtToken; // 160 bits
        uint32 lastUpdateTimestamp; // 32 bits
        uint16 reserveFactor; // 16 bits
        uint16 p2pIndexCursor; // 16 bits
        // SLOT 8
        address aToken; // 160 bits
        // SLOT 9
        address stableDebtToken; // 160 bits
        // SLOT 10
        uint256 idleSupply; // 256 bits
    }

    /// @notice Contains storage-only dynamic arrays and mappings.
    struct MarketBalances {
        LogarithmicBuckets.Buckets poolSuppliers; // In pool unit.
        LogarithmicBuckets.Buckets p2pSuppliers; // In peer-to-peer unit.
        LogarithmicBuckets.Buckets poolBorrowers; // In pool unit.
        LogarithmicBuckets.Buckets p2pBorrowers; // In peer-to-peer unit.
        mapping(address => uint256) collateral; // In pool unit.
    }

    /// @notice Contains the minimum number of iterations for a `repay` or a `withdraw`.
    struct Iterations {
        uint128 repay;
        uint128 withdraw;
    }

    /* STACK AND RETURN STRUCTS */

    /// @notice Contains the data related to the liquidity of a user.
    struct LiquidityData {
        uint256 borrowable; // The maximum debt value allowed to borrow (in base currency).
        uint256 maxDebt; // The maximum debt value allowed before being liquidatable (in base currency).
        uint256 debt; // The debt value (in base currency).
    }

    /// @notice The paramaters used to compute the new peer-to-peer indexes.
    struct IndexesParams {
        MarketSideIndexes256 lastSupplyIndexes; // The last supply indexes stored (in ray).
        MarketSideIndexes256 lastBorrowIndexes; // The last borrow indexes stored (in ray).
        uint256 poolSupplyIndex; // The current pool supply index (in ray).
        uint256 poolBorrowIndex; // The current pool borrow index (in ray).
        uint256 reserveFactor; // The reserve factor percentage (10 000 = 100%).
        uint256 p2pIndexCursor; // The peer-to-peer index cursor (10 000 = 100%).
        Deltas deltas; // The deltas and peer-to-peer amounts.
        uint256 proportionIdle; // The amount of proportion idle (in underlying).
    }

    /// @notice Contains the data related to growth factors as part of the peer-to-peer indexes computation.
    struct GrowthFactors {
        uint256 poolSupplyGrowthFactor; // The pool's supply index growth factor (in ray).
        uint256 p2pSupplyGrowthFactor; // Peer-to-peer supply index growth factor (in ray).
        uint256 poolBorrowGrowthFactor; // The pool's borrow index growth factor (in ray).
        uint256 p2pBorrowGrowthFactor; // Peer-to-peer borrow index growth factor (in ray).
    }

    /// @notice Contains the market side indexes as uint256 instead of uint128.
    struct MarketSideIndexes256 {
        uint256 poolIndex; // The pool index (in ray).
        uint256 p2pIndex; // The peer-to-peer index (in ray).
    }

    /// @notice Contains the indexes as uint256 instead of uint128.
    struct Indexes256 {
        MarketSideIndexes256 supply; // The `MarketSideIndexes` related to the supply as uint256.
        MarketSideIndexes256 borrow; // The `MarketSideIndexes` related to the borrow as uint256.
    }

    /// @notice Contains the `v`, `r` and `s` parameters of an ECDSA signature.
    struct Signature {
        uint8 v;
        bytes32 r;
        bytes32 s;
    }

    /// @notice Variables used in the matching engine.
    struct MatchingEngineVars {
        address underlying; // The underlying asset address.
        MarketSideIndexes256 indexes; // The indexes of the market side.
        uint256 amount; // The amount to promote or demote (in underlying).
        uint256 maxIterations; // The maximum number of iterations allowed.
        bool borrow; // Whether the process happens on the borrow side or not.
        function (address, address, uint256, uint256, bool) returns (uint256, uint256) updateDS; // This function will be used to update the data-structure.
        bool demoting; // True for demote, False for promote.
        function(uint256, uint256, MarketSideIndexes256 memory, uint256)
            pure returns (uint256, uint256, uint256) step; // This function will be used to decide whether to use the algorithm for promoting or for demoting.
    }

    /// @notice Variables used in the liquidity computation process of a `user`.
    /// @dev Used to avoid stack too deep.
    struct LiquidityVars {
        address user; // The user address.
        IAaveOracle oracle; // The oracle used by Aave.
        DataTypes.EModeCategory eModeCategory; // The data related to the eMode category (could be empty if not in any e-mode).
    }

    /// @notice Variables used during a borrow or withdraw.
    /// @dev Used to avoid stack too deep.
    struct BorrowWithdrawVars {
        uint256 onPool; // The working scaled balance on pool of the user.
        uint256 inP2P; // The working scaled balance in peer-to-peer of the user.
        uint256 toWithdraw; // The amount to withdraw from the pool (in underlying).
        uint256 toBorrow; // The amount to borrow on the pool (in underlying).
    }

    /// @notice Variables used during a supply or repay.
    /// @dev Used to avoid stack too deep.
    struct SupplyRepayVars {
        uint256 onPool; // The working scaled balance on pool of the user.
        uint256 inP2P; // The working scaled balance in peer-to-peer of the user.
        uint256 toSupply; // The amount to supply on the pool (in underlying).
        uint256 toRepay; // The amount to repay on the pool (in underlying).
    }

    /// @notice Variables used during a liquidate.
    /// @dev Used to avoid stack too deep.
    struct LiquidateVars {
        uint256 closeFactor; // The close factor used during the liquidation process.
        uint256 seized; // The amount of collateral to be seized (in underlying).
    }

    /// @notice Variables used to compute the amount to seize during a liquidation.
    /// @dev Used to avoid stack too deep.
    struct AmountToSeizeVars {
        uint256 liquidationBonus; // The liquidation bonus used during the liquidation process.
        uint256 borrowedTokenUnit; // The borrowed token unit.
        uint256 collateralTokenUnit; // The collateral token unit.
        uint256 borrowedPrice; // The borrowed token price (in base currency).
        uint256 collateralPrice; // The collateral token price (in base currency).
    }
}

File 5 of 57 : Errors.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

/// @title Errors
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library exposing errors used in Morpho.
library Errors {
    /// @notice Thrown when interacting with a market that is not created.
    error MarketNotCreated();

    /// @notice Thrown when creating a market that is already created.
    error MarketAlreadyCreated();

    /// @notice Thrown when creating a market that is not listed on Aave.
    error MarketIsNotListedOnAave();

    /// @notice Thrown when the market is in a siloed borrowing mode.
    error SiloedBorrowMarket();

    /// @notice Thrown when the market liquidation threshold is too low to be created.
    error MarketLtTooLow();

    /// @notice Thrown when the address used is the zero address.
    error AddressIsZero();

    /// @notice Thrown when the amount used is zero.
    error AmountIsZero();

    /// @notice Thrown when the user's debt is zero.
    error DebtIsZero();

    /// @notice Thrown when the user's supply is zero.
    error SupplyIsZero();

    /// @notice Thrown when the user's collateral is zero.
    error CollateralIsZero();

    /// @notice Thrown when the manager is not approved by the delegator.
    error PermissionDenied();

    /// @notice Thrown when supply is paused for the asset.
    error SupplyIsPaused();

    /// @notice Thrown when supply collateral is paused for the asset.
    error SupplyCollateralIsPaused();

    /// @notice Thrown when borrow is paused for the asset.
    error BorrowIsPaused();

    /// @notice Thrown when repay is paused for the asset.
    error RepayIsPaused();

    /// @notice Thrown when withdraw is paused for the asset.
    error WithdrawIsPaused();

    /// @notice Thrown when withdraw collateral is paused for the asset.
    error WithdrawCollateralIsPaused();

    /// @notice Thrown when liquidate is paused for the collateral asset.
    error LiquidateCollateralIsPaused();

    /// @notice Thrown when liquidate is paused for the borrow asset
    error LiquidateBorrowIsPaused();

    /// @notice Thrown when claim rewards is paused.
    error ClaimRewardsPaused();

    /// @notice Thrown when unpausing the borrow of a market that is deprecated.
    error MarketIsDeprecated();

    /// @notice Thrown when deprecating a market that is not paused.
    error BorrowNotPaused();

    /// @notice Thrown when the market is not enabled on Aave.
    error BorrowNotEnabled();

    /// @notice Thrown when the oracle sentinel is set and disables borrowing.
    error SentinelBorrowNotEnabled();

    /// @notice Thrown when borrowing an asset that is not in Morpho's e-mode category.
    error InconsistentEMode();

    /// @notice Thrown when a borrow would leave the user undercollateralized.
    error UnauthorizedBorrow();

    /// @notice Thrown when a withdraw would leave the user undercollateralized.
    error UnauthorizedWithdraw();

    /// @notice Thrown when the liquidatation is not authorized because of a collateralization ratio too high.
    error UnauthorizedLiquidate();

    /// @notice Thrown when the oracle sentinel is set and disables liquidating.
    error SentinelLiquidateNotEnabled();

    /// @notice Thrown when (un)setting a market as collateral on Morpho while it is not a collateral on Aave.
    error AssetNotCollateralOnPool();

    /// @notice Thrown when supplying an asset as collateral while it is not a collateral on Morpho.
    error AssetNotCollateralOnMorpho();

    /// @notice Thrown when (un)setting a market as collateral on Aave while it is a collateral on Morpho.
    error AssetIsCollateralOnMorpho();

    /// @notice Thrown when setting a market as collateral on Aave while the market is not created on Morpho.
    error SetAsCollateralOnPoolButMarketNotCreated();

    /// @notice Thrown when the value exceeds the maximum basis points value (100% = 10000).
    error ExceedsMaxBasisPoints();

    /// @notice Thrown when the s part of the ECDSA signature is invalid.
    error InvalidValueS();

    /// @notice Thrown when the v part of the ECDSA signature is invalid.
    error InvalidValueV();

    /// @notice Thrown when the signatory of the ECDSA signature is invalid.
    error InvalidSignatory();

    /// @notice Thrown when the nonce is invalid.
    error InvalidNonce();

    /// @notice Thrown when the signature is expired
    error SignatureExpired();

    /// @notice Thrown when the borrow cap on Aave is exceeded.
    error ExceedsBorrowCap();
}

File 6 of 57 : Events.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

/// @title Events
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library exposing events used in Morpho.
library Events {
    /// @notice Emitted when a supply happens.
    /// @param from The address of the user supplying the funds.
    /// @param onBehalf The address of the user on behalf of which the position is created.
    /// @param underlying The address of the underlying asset supplied.
    /// @param amount The amount of `underlying` asset supplied.
    /// @param scaledOnPool The scaled supply balance on pool of `onBehalf` after the supply.
    /// @param scaledInP2P The scaled supply balance in peer-to-peer of `onBehalf` after the supply.
    event Supplied(
        address indexed from,
        address indexed onBehalf,
        address indexed underlying,
        uint256 amount,
        uint256 scaledOnPool,
        uint256 scaledInP2P
    );

    /// @notice Emitted when a supply collateral happens.
    /// @param from The address of the user supplying the funds.
    /// @param onBehalf The address of the user on behalf of which the position is created.
    /// @param underlying The address of the underlying asset supplied.
    /// @param amount The amount of `underlying` asset supplied.
    /// @param scaledBalance The scaled collateral balance of `onBehalf` after the supply.
    event CollateralSupplied(
        address indexed from,
        address indexed onBehalf,
        address indexed underlying,
        uint256 amount,
        uint256 scaledBalance
    );

    /// @notice Emitted when a borrow happens.
    /// @param caller The address of the caller.
    /// @param onBehalf The address of the user on behalf of which the position is created.
    /// @param receiver The address of the user receiving the funds.
    /// @param underlying The address of the underlying asset borrowed.
    /// @param amount The amount of `underlying` asset borrowed.
    /// @param scaledOnPool The scaled borrow balance on pool of `onBehalf` after the borrow.
    /// @param scaledInP2P The scaled borrow balance in peer-to-peer of `onBehalf` after the borrow.
    event Borrowed(
        address caller,
        address indexed onBehalf,
        address indexed receiver,
        address indexed underlying,
        uint256 amount,
        uint256 scaledOnPool,
        uint256 scaledInP2P
    );

    /// @notice Emitted when a repay happens.
    /// @param repayer The address of the user repaying the debt.
    /// @param onBehalf The address of the user on behalf of which the position is modified.
    /// @param underlying The address of the underlying asset repaid.
    /// @param amount The amount of `underlying` asset repaid.
    /// @param scaledOnPool The scaled borrow balance on pool of `onBehalf` after the repay.
    /// @param scaledInP2P The scaled borrow balance in peer-to-peer of `onBehalf` after the repay.
    event Repaid(
        address indexed repayer,
        address indexed onBehalf,
        address indexed underlying,
        uint256 amount,
        uint256 scaledOnPool,
        uint256 scaledInP2P
    );

    /// @notice Emitted when a withdraw happens.
    /// @param caller The address of the caller.
    /// @param onBehalf The address of the user on behalf of which the position is modified.
    /// @param receiver The address of the user receiving the funds.
    /// @param underlying The address of the underlying asset withdrawn.
    /// @param amount The amount of `underlying` asset withdrawn.
    /// @param scaledOnPool The scaled supply balance on pool of `onBehalf` after the withdraw.
    /// @param scaledInP2P The scaled supply balance in peer-to-peer of `onBehalf` after the withdraw.
    event Withdrawn(
        address caller,
        address indexed onBehalf,
        address indexed receiver,
        address indexed underlying,
        uint256 amount,
        uint256 scaledOnPool,
        uint256 scaledInP2P
    );

    /// @notice Emitted when a withdraw collateral happens.
    /// @param caller The address of the caller.
    /// @param onBehalf The address of the user on behalf of which the position is modified.
    /// @param receiver The address of the user receiving the funds.
    /// @param underlying The address of the underlying asset withdrawn.
    /// @param amount The amount of `underlying` asset withdrawn.
    /// @param scaledBalance The scaled collateral balance of `onBehalf` after the withdraw.
    event CollateralWithdrawn(
        address caller,
        address indexed onBehalf,
        address indexed receiver,
        address indexed underlying,
        uint256 amount,
        uint256 scaledBalance
    );

    /// @notice Emitted when a liquidate happens.
    /// @param liquidator The address of the liquidator.
    /// @param borrower The address of the borrower that was liquidated.
    /// @param underlyingBorrowed The address of the underlying asset borrowed being repaid.
    /// @param amountLiquidated The amount of `underlyingBorrowed` repaid.
    /// @param underlyingCollateral The address of the collateral underlying seized.
    /// @param amountSeized The amount of `underlyingCollateral` seized.
    event Liquidated(
        address indexed liquidator,
        address indexed borrower,
        address indexed underlyingBorrowed,
        uint256 amountLiquidated,
        address underlyingCollateral,
        uint256 amountSeized
    );

    /// @notice Emitted when a `manager` is approved or unapproved to act on behalf of a `delegator`.
    event ManagerApproval(address indexed delegator, address indexed manager, bool isAllowed);

    /// @notice Emitted when a supply position is updated.
    /// @param user The address of the user.
    /// @param underlying The address of the underlying asset.
    /// @param scaledOnPool The scaled supply balance on pool of `user` after the update.
    /// @param scaledInP2P The scaled supply balance in peer-to-peer of `user` after the update.
    event SupplyPositionUpdated(
        address indexed user, address indexed underlying, uint256 scaledOnPool, uint256 scaledInP2P
    );

    /// @notice Emitted when a borrow position is updated.
    /// @param user The address of the user.
    /// @param underlying The address of the underlying asset.
    /// @param scaledOnPool The scaled borrow balance on pool of `user` after the update.
    /// @param scaledInP2P The scaled borrow balance in peer-to-peer of `user` after the update.
    event BorrowPositionUpdated(
        address indexed user, address indexed underlying, uint256 scaledOnPool, uint256 scaledInP2P
    );

    /// @notice Emitted when a peer-to-peer supply delta is updated.
    /// @param underlying The address of the underlying asset.
    /// @param scaledDelta The scaled supply delta of `underlying` asset.
    event P2PSupplyDeltaUpdated(address indexed underlying, uint256 scaledDelta);

    /// @notice Emitted when a peer-to-peer borrow delta is updated.
    /// @param underlying The address of the underlying asset.
    /// @param scaledDelta The scaled borrow delta of `underlying` asset.
    event P2PBorrowDeltaUpdated(address indexed underlying, uint256 scaledDelta);

    /// @notice Emitted when the peer-to-peer total amounts are updated.
    /// @param underlying The address of the underlying asset.
    /// @param scaledTotalSupplyP2P The scaled total supply of `underlying` asset in peer-to-peer.
    /// @param scaledTotalBorrowP2P The scaled total borrow of `underlying` asset in peer-to-peer.
    event P2PTotalsUpdated(address indexed underlying, uint256 scaledTotalSupplyP2P, uint256 scaledTotalBorrowP2P);

    /// @notice Emitted when a rewards are claimed.
    /// @param claimer The address of the user claiming the rewards.
    /// @param onBehalf The address of the user on behalf of which the rewards are claimed.
    /// @param rewardToken The address of the reward token claimed.
    /// @param amountClaimed The amount of `rewardToken` claimed.
    event RewardsClaimed(
        address indexed claimer, address indexed onBehalf, address indexed rewardToken, uint256 amountClaimed
    );

    /// @notice Emitted when the collateral status of the `underlying` market is set to `isCollateral`.
    event IsCollateralSet(address indexed underlying, bool isCollateral);

    /// @notice Emitted when the claim rewards status is set to `isPaused`.
    event IsClaimRewardsPausedSet(bool isPaused);

    /// @notice Emitted when the supply pause status of the `underlying` market is set to `isPaused`.
    event IsSupplyPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when the supply collateral pause status of the `underlying` market is set to `isPaused`.
    event IsSupplyCollateralPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when the borrow pause status of the `underlying` market is set to `isPaused`.
    event IsBorrowPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when the withdraw pause status of the `underlying` market is set to `isPaused`.
    event IsWithdrawPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when the withdraw collateral pause status of the `underlying` market is set to `isPaused`.
    event IsWithdrawCollateralPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when the repay pause status of the `underlying` market is set to `isPaused`.
    event IsRepayPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when the liquidate collateral pause status of the `underlying` market is set to `isPaused`.
    event IsLiquidateCollateralPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when the liquidate borrow pause status of the `underlying` market is set to `isPaused`.
    event IsLiquidateBorrowPausedSet(address indexed underlying, bool isPaused);

    /// @notice Emitted when an `_increaseP2PDeltas` is triggered.
    /// @param underlying The address of the underlying asset.
    /// @param amount The amount of the increases in `underlying` asset.
    event P2PDeltasIncreased(address indexed underlying, uint256 amount);

    /// @notice Emitted when a new market is created.
    /// @param underlying The address of the underlying asset of the new market.
    event MarketCreated(address indexed underlying);

    /// @notice Emitted when the default iterations are set.
    /// @param repay The default number of repay iterations.
    /// @param withdraw The default number of withdraw iterations.
    event DefaultIterationsSet(uint128 repay, uint128 withdraw);

    /// @notice Emitted when the positions manager is set.
    /// @param positionsManager The address of the positions manager.
    event PositionsManagerSet(address indexed positionsManager);

    /// @notice Emitted when the rewards manager is set.
    /// @param rewardsManager The address of the rewards manager.
    event RewardsManagerSet(address indexed rewardsManager);

    /// @notice Emitted when the treasury vault is set.
    /// @param treasuryVault The address of the treasury vault.
    event TreasuryVaultSet(address indexed treasuryVault);

    /// @notice Emitted when the reserve factor is set.
    /// @param underlying The address of the underlying asset.
    /// @param reserveFactor The reserve factor for this `underlying` asset.
    event ReserveFactorSet(address indexed underlying, uint16 reserveFactor);

    /// @notice Emitted when the peer-to-peer index cursor is set.
    /// @param underlying The address of the underlying asset.
    /// @param p2pIndexCursor The peer-to-peer index cursor for this `underlying` asset.
    event P2PIndexCursorSet(address indexed underlying, uint16 p2pIndexCursor);

    /// @notice Emitted when the peer-to-peer disabled status is set.
    /// @param underlying The address of the underlying asset.
    /// @param isP2PDisabled The peer-to-peer disabled status for this `underlying` asset.
    event IsP2PDisabledSet(address indexed underlying, bool isP2PDisabled);

    /// @notice Emitted when the deprecation status is set.
    /// @param underlying The address of the underlying asset.
    /// @param isDeprecated The deprecation status for this `underlying` asset.
    event IsDeprecatedSet(address indexed underlying, bool isDeprecated);

    /// @notice Emitted when the indexes are updated.
    /// @param underlying The address of the underlying asset.
    /// @param poolSupplyIndex The new pool supply index.
    /// @param p2pSupplyIndex The new peer-to-peer supply index.
    /// @param poolBorrowIndex The new pool borrow index.
    /// @param p2pBorrowIndex The new peer-to-peer borrow index.
    event IndexesUpdated(
        address indexed underlying,
        uint256 poolSupplyIndex,
        uint256 p2pSupplyIndex,
        uint256 poolBorrowIndex,
        uint256 p2pBorrowIndex
    );

    /// @notice Emitted when the idle supply is updated.
    /// @param underlying The address of the underlying asset.
    /// @param idleSupply The new idle supply.
    event IdleSupplyUpdated(address indexed underlying, uint256 idleSupply);

    /// @notice Emitted when the reserve fee is claimed.
    /// @param underlying The address of the underlying asset.
    /// @param claimed The amount of the claimed reserve fee.
    event ReserveFeeClaimed(address indexed underlying, uint256 claimed);

    /// @notice Emitted when a user nonce is incremented.
    /// @param caller The address of the caller.
    /// @param signatory The address of the signatory.
    /// @param usedNonce The used nonce.
    event UserNonceIncremented(address indexed caller, address indexed signatory, uint256 usedNonce);
}

File 7 of 57 : PoolLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IAToken} from "../interfaces/aave/IAToken.sol";
import {IPool} from "@aave-v3-core/interfaces/IPool.sol";
import {IVariableDebtToken} from "@aave-v3-core/interfaces/IVariableDebtToken.sol";

import {Constants} from "./Constants.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";

/// @title PoolLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library used to ease pool interactions.
library PoolLib {
    using WadRayMath for uint256;

    /// @notice Supplies `amount` of `underlying` to `pool`.
    /// @dev The pool supply `index` must be passed as a parameter to skip the supply on pool
    ///      if it were to revert due to the amount being too small.
    function supplyToPool(IPool pool, address underlying, uint256 amount, uint256 index) internal {
        if (amount.rayDiv(index) == 0) return;

        pool.supply(underlying, amount, address(this), Constants.NO_REFERRAL_CODE);
    }

    /// @notice Borrows `amount` of `underlying`from `pool`.
    function borrowFromPool(IPool pool, address underlying, uint256 amount) internal {
        if (amount == 0) return;

        pool.borrow(underlying, amount, Constants.VARIABLE_INTEREST_MODE, Constants.NO_REFERRAL_CODE, address(this));
    }

    /// @notice Repays `amount` of `underlying` to `pool`.
    /// @dev If the debt has been fully repaid already, the function will return early.
    function repayToPool(IPool pool, address underlying, address variableDebtToken, uint256 amount) internal {
        if (amount == 0 || IVariableDebtToken(variableDebtToken).scaledBalanceOf(address(this)) == 0) return;

        pool.repay(underlying, amount, Constants.VARIABLE_INTEREST_MODE, address(this)); // Reverts if debt is 0.
    }

    /// @notice Withdraws `amount` of `underlying` from `pool`.
    /// @dev If the amount is greater than the balance of the aToken, the function will withdraw the maximum possible.
    function withdrawFromPool(IPool pool, address underlying, address aToken, uint256 amount) internal {
        if (amount == 0) return;

        // Withdraw only what is possible. The remaining dust is taken from the contract balance.
        amount = Math.min(IAToken(aToken).balanceOf(address(this)), amount);
        pool.withdraw(underlying, amount, address(this));
    }

    /// @notice Returns the current pool indexes for `underlying` on the `pool`.
    /// @return poolSupplyIndex The current supply index of the pool (in ray).
    /// @return poolBorrowIndex The current borrow index of the pool (in ray).
    function getCurrentPoolIndexes(IPool pool, address underlying)
        internal
        view
        returns (uint256 poolSupplyIndex, uint256 poolBorrowIndex)
    {
        poolSupplyIndex = pool.getReserveNormalizedIncome(underlying);
        poolBorrowIndex = pool.getReserveNormalizedVariableDebt(underlying);
    }
}

File 8 of 57 : Constants.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

/// @title Constants
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library exposing constants used in Morpho.
library Constants {
    /// @dev The referral code used for Aave.
    uint8 internal constant NO_REFERRAL_CODE = 0;

    /// @dev The variable interest rate mode of Aave.
    uint8 internal constant VARIABLE_INTEREST_MODE = 2;

    /// @dev The threshold under which the balance is swept to 0.
    uint256 internal constant DUST_THRESHOLD = 1;

    /// @dev A lower bound on the liquidation threshold values of all the listed assets.
    uint256 internal constant LT_LOWER_BOUND = 10_00;

    /// @dev The maximum close factor used during liquidations (100%).
    uint256 internal constant MAX_CLOSE_FACTOR = PercentageMath.PERCENTAGE_FACTOR;

    /// @dev The default close factor used during liquidations (50%).
    uint256 internal constant DEFAULT_CLOSE_FACTOR = PercentageMath.HALF_PERCENTAGE_FACTOR;

    /// @dev Health factor below which the positions can be liquidated.
    uint256 internal constant DEFAULT_LIQUIDATION_MAX_HF = WadRayMath.WAD;

    /// @dev Health factor below which the positions can be liquidated, whether or not the price oracle sentinel allows the liquidation.
    uint256 internal constant DEFAULT_LIQUIDATION_MIN_HF = 0.95e18;

    /// @dev The prefix used for EIP-712 signature.
    string internal constant EIP712_MSG_PREFIX = "\x19\x01";

    /// @dev The name used for EIP-712 signature.
    string internal constant EIP712_NAME = "Morpho-AaveV3";

    /// @dev The version used for EIP-712 signature.
    string internal constant EIP712_VERSION = "0";

    /// @dev The domain typehash used for the EIP-712 signature.
    bytes32 internal constant EIP712_DOMAIN_TYPEHASH =
        keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");

    /// @dev The typehash for approveManagerWithSig Authorization used for the EIP-712 signature.
    bytes32 internal constant EIP712_AUTHORIZATION_TYPEHASH =
        keccak256("Authorization(address delegator,address manager,bool isAllowed,uint256 nonce,uint256 deadline)");

    /// @dev The highest valid value for s in an ECDSA signature pair (0 < s < secp256k1n ÷ 2 + 1).
    uint256 internal constant MAX_VALID_ECDSA_S = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0;
}

File 9 of 57 : MarketBalanceLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {Types} from "./Types.sol";

import {LogarithmicBuckets} from "@morpho-data-structures/LogarithmicBuckets.sol";

/// @title MarketBalanceLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library used to ease market balance reads.
library MarketBalanceLib {
    /// @notice Returns the scaled pool supply balance of `user` given the `marketBalances` of a specific market.
    function scaledPoolSupplyBalance(Types.MarketBalances storage marketBalances, address user)
        internal
        view
        returns (uint256)
    {
        return marketBalances.poolSuppliers.valueOf[user];
    }

    /// @notice Returns the scaled peer-to-peer supply balance of `user` given the `marketBalances` of a specific market.
    function scaledP2PSupplyBalance(Types.MarketBalances storage marketBalances, address user)
        internal
        view
        returns (uint256)
    {
        return marketBalances.p2pSuppliers.valueOf[user];
    }

    /// @notice Returns the scaled pool borrow balance of `user` given the `marketBalances` of a specific market.
    function scaledPoolBorrowBalance(Types.MarketBalances storage marketBalances, address user)
        internal
        view
        returns (uint256)
    {
        return marketBalances.poolBorrowers.valueOf[user];
    }

    /// @notice Returns the scaled peer-to-peer borrow balance of `user` given the `marketBalances` of a specific market.
    function scaledP2PBorrowBalance(Types.MarketBalances storage marketBalances, address user)
        internal
        view
        returns (uint256)
    {
        return marketBalances.p2pBorrowers.valueOf[user];
    }

    /// @notice Returns the scaled collateral balance of `user` given the `marketBalances` of a specific market.
    function scaledCollateralBalance(Types.MarketBalances storage marketBalances, address user)
        internal
        view
        returns (uint256)
    {
        return marketBalances.collateral[user];
    }
}

File 10 of 57 : Math.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

library Math {
    function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
        assembly {
            z := xor(x, mul(xor(x, y), lt(y, x)))
        }
    }

    function max(uint256 x, uint256 y) internal pure returns (uint256 z) {
        assembly {
            z := xor(x, mul(xor(x, y), gt(y, x)))
        }
    }

    /// @dev Returns max(x - y, 0).
    function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
        assembly {
            z := mul(gt(x, y), sub(x, y))
        }
    }

    /// @dev Returns x / y rounded up (x / y + boolAsInt(x % y > 0)).
    function divUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // Division by 0 if
        //    y = 0
        assembly {
            if iszero(y) { revert(0, 0) }

            z := add(gt(mod(x, y), 0), div(x, y))
        }
    }

    /// @dev Returns the floor of log2(x) and returns 0 on input 0.
    /// @dev This computation makes use of De Bruijn sequences, their usage for computing log2 is referenced here: http://supertech.csail.mit.edu/papers/debruijn.pdf
    function log2(uint256 x) internal pure returns (uint256 y) {
        assembly {
            // Use the highest set bit of x to set each of its lower bits
            x := or(x, shr(1, x))
            x := or(x, shr(2, x))
            x := or(x, shr(4, x))
            x := or(x, shr(8, x))
            x := or(x, shr(16, x))
            x := or(x, shr(32, x))
            x := or(x, shr(64, x))
            x := or(x, shr(128, x))
            // Take only the highest bit of x
            x := xor(x, shr(1, x))

            // Hash table associating the first 256 powers of 2 to their log
            // Let x be a power of 2, its hash is obtained by taking the first byte of x * deBruijnSeq
            let m := mload(0x40)
            mstore(m, 0x0001020903110a19042112290b311a3905412245134d2a550c5d32651b6d3a75)
            mstore(add(m, 0x20), 0x06264262237d468514804e8d2b95569d0d495ea533a966b11c886eb93bc176c9)
            mstore(add(m, 0x40), 0x071727374353637324837e9b47af86c7155181ad4fd18ed32c9096db57d59ee3)
            mstore(add(m, 0x60), 0x0e2e4a6a5f92a6be3498aae067ddb2eb1d5989b56fd7baf33ca0c2ee77e5caf7)
            mstore(add(m, 0x80), 0xff0810182028303840444c545c646c7425617c847f8c949c48a4a8b087b8c0c8)
            mstore(add(m, 0xa0), 0x16365272829aaec650acd0d28fdad4e22d6991bd97dfdcea58b4d6f29fede4f6)
            mstore(add(m, 0xc0), 0xfe0f1f2f3f4b5b6b607b8b93a3a7b7bf357199c5abcfd9e168bcdee9b3f1ecf5)
            mstore(add(m, 0xe0), 0xfd1e3e5a7a8aa2b670c4ced8bbe8f0f4fc3d79a1c3cde7effb78cce6facbf9f8)
            mstore(0x40, add(m, 0x100)) // Update the free memory pointer

            // This De Bruijn sequence begins with the byte 0, which is important to make shifting work like a rotation on the first byte
            let deBruijnSeq := 0x00818283848586878898a8b8c8d8e8f929395969799a9b9d9e9faaeb6bedeeff
            let key := shr(248, mul(x, deBruijnSeq)) // With the multiplication it works also for 0
            y := shr(248, mload(add(m, key))) // Look in the table and take the first byte
        }
    }
}

File 11 of 57 : PercentageMath.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

/// @title PercentageMath.
/// @author Morpho Labs.
/// @custom:contact [email protected]
/// @notice Optimized version of Aave V3 math library PercentageMath to conduct percentage manipulations: https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/math/PercentageMath.sol
library PercentageMath {
    ///	CONSTANTS ///

    // Only direct number constants and references to such constants are supported by inline assembly.
    uint256 internal constant PERCENTAGE_FACTOR = 100_00;
    uint256 internal constant HALF_PERCENTAGE_FACTOR = 50_00;
    uint256 internal constant PERCENTAGE_FACTOR_MINUS_ONE = 100_00 - 1;
    uint256 internal constant MAX_UINT256 = 2 ** 256 - 1;
    uint256 internal constant MAX_UINT256_MINUS_HALF_PERCENTAGE_FACTOR = 2 ** 256 - 1 - 50_00;
    uint256 internal constant MAX_UINT256_MINUS_PERCENTAGE_FACTOR_MINUS_ONE = 2 ** 256 - 1 - (100_00 - 1);

    /// INTERNAL ///

    /// @notice Executes the bps-based percentage addition (x * (1 + p)), rounded half up.
    /// @param x The value to which to add the percentage.
    /// @param percentage The percentage of the value to add (in bps).
    /// @return y The result of the addition.
    function percentAdd(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // 1. Overflow if
        //        PERCENTAGE_FACTOR + percentage > type(uint256).max
        //    <=> percentage > type(uint256).max - PERCENTAGE_FACTOR
        // 2. Overflow if
        //        x * (PERCENTAGE_FACTOR + percentage) + HALF_PERCENTAGE_FACTOR > type(uint256).max
        //    <=> x > (type(uint256).max - HALF_PERCENTAGE_FACTOR) / (PERCENTAGE_FACTOR + percentage)
        assembly {
            y := add(PERCENTAGE_FACTOR, percentage) // Temporary assignment to save gas.

            if or(
                gt(percentage, sub(MAX_UINT256, PERCENTAGE_FACTOR)),
                gt(x, div(MAX_UINT256_MINUS_HALF_PERCENTAGE_FACTOR, y))
            ) { revert(0, 0) }

            y := div(add(mul(x, y), HALF_PERCENTAGE_FACTOR), PERCENTAGE_FACTOR)
        }
    }

    /// @notice Executes the bps-based percentage subtraction (x * (1 - p)), rounded half up.
    /// @param x The value to which to subtract the percentage.
    /// @param percentage The percentage of the value to subtract (in bps).
    /// @return y The result of the subtraction.
    function percentSub(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // 1. Underflow if
        //        percentage > PERCENTAGE_FACTOR
        // 2. Overflow if
        //        x * (PERCENTAGE_FACTOR - percentage) + HALF_PERCENTAGE_FACTOR > type(uint256).max
        //    <=> (PERCENTAGE_FACTOR - percentage) > 0 and x > (type(uint256).max - HALF_PERCENTAGE_FACTOR) / (PERCENTAGE_FACTOR - percentage)
        assembly {
            y := sub(PERCENTAGE_FACTOR, percentage) // Temporary assignment to save gas.

            if or(gt(percentage, PERCENTAGE_FACTOR), mul(y, gt(x, div(MAX_UINT256_MINUS_HALF_PERCENTAGE_FACTOR, y)))) {
                revert(0, 0)
            }

            y := div(add(mul(x, y), HALF_PERCENTAGE_FACTOR), PERCENTAGE_FACTOR)
        }
    }

    /// @notice Executes the bps-based multiplication (x * p), rounded half up.
    /// @param x The value to multiply by the percentage.
    /// @param percentage The percentage of the value to multiply (in bps).
    /// @return y The result of the multiplication.
    function percentMul(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // Overflow if
        //     x * percentage + HALF_PERCENTAGE_FACTOR > type(uint256).max
        // <=> percentage > 0 and x > (type(uint256).max - HALF_PERCENTAGE_FACTOR) / percentage
        assembly {
            if mul(percentage, gt(x, div(MAX_UINT256_MINUS_HALF_PERCENTAGE_FACTOR, percentage))) { revert(0, 0) }

            y := div(add(mul(x, percentage), HALF_PERCENTAGE_FACTOR), PERCENTAGE_FACTOR)
        }
    }

    /// @notice Executes the bps-based multiplication (x * p), rounded down.
    /// @param x The value to multiply by the percentage.
    /// @param percentage The percentage of the value to multiply.
    /// @return y The result of the multiplication.
    function percentMulDown(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // Overflow if
        //     x * percentage > type(uint256).max
        // <=> percentage > 0 and x > type(uint256).max / percentage
        assembly {
            if mul(percentage, gt(x, div(MAX_UINT256, percentage))) { revert(0, 0) }

            y := div(mul(x, percentage), PERCENTAGE_FACTOR)
        }
    }

    /// @notice Executes the bps-based multiplication (x * p), rounded up.
    /// @param x The value to multiply by the percentage.
    /// @param percentage The percentage of the value to multiply.
    /// @return y The result of the multiplication.
    function percentMulUp(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // Overflow if
        //     x * percentage + PERCENTAGE_FACTOR_MINUS_ONE > type(uint256).max
        // <=> percentage > 0 and x > (type(uint256).max - PERCENTAGE_FACTOR_MINUS_ONE) / percentage
        assembly {
            if mul(percentage, gt(x, div(MAX_UINT256_MINUS_PERCENTAGE_FACTOR_MINUS_ONE, percentage))) { revert(0, 0) }

            y := div(add(mul(x, percentage), PERCENTAGE_FACTOR_MINUS_ONE), PERCENTAGE_FACTOR)
        }
    }

    /// @notice Executes the bps-based division (x / p), rounded half up.
    /// @param x The value to divide by the percentage.
    /// @param percentage The percentage of the value to divide (in bps).
    /// @return y The result of the division.
    function percentDiv(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // 1. Division by 0 if
        //        percentage == 0
        // 2. Overflow if
        //        x * PERCENTAGE_FACTOR + percentage / 2 > type(uint256).max
        //    <=> x > (type(uint256).max - percentage / 2) / PERCENTAGE_FACTOR
        assembly {
            y := div(percentage, 2) // Temporary assignment to save gas.

            if iszero(mul(percentage, iszero(gt(x, div(sub(MAX_UINT256, y), PERCENTAGE_FACTOR))))) { revert(0, 0) }

            y := div(add(mul(PERCENTAGE_FACTOR, x), y), percentage)
        }
    }

    /// @notice Executes the bps-based division (x / p), rounded down.
    /// @param x The value to divide by the percentage.
    /// @param percentage The percentage of the value to divide.
    /// @return y The result of the division.
    function percentDivDown(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // 1. Division by 0 if
        //        percentage == 0
        // 2. Overflow if
        //        x * PERCENTAGE_FACTOR > type(uint256).max
        //    <=> x > type(uint256).max / PERCENTAGE_FACTOR
        assembly {
            if iszero(mul(percentage, lt(x, add(div(MAX_UINT256, PERCENTAGE_FACTOR), 1)))) { revert(0, 0) }

            y := div(mul(PERCENTAGE_FACTOR, x), percentage)
        }
    }

    /// @notice Executes the bps-based division (x / p), rounded up.
    /// @param x The value to divide by the percentage.
    /// @param percentage The percentage of the value to divide.
    /// @return y The result of the division.
    function percentDivUp(uint256 x, uint256 percentage) internal pure returns (uint256 y) {
        // 1. Division by 0 if
        //        percentage == 0
        // 2. Overflow if
        //        x * PERCENTAGE_FACTOR + (percentage - 1) > type(uint256).max
        //    <=> x > (type(uint256).max - (percentage - 1)) / PERCENTAGE_FACTOR
        assembly {
            y := sub(percentage, 1) // Temporary assignment to save gas.

            if iszero(mul(percentage, iszero(gt(x, div(sub(MAX_UINT256, y), PERCENTAGE_FACTOR))))) { revert(0, 0) }

            y := div(add(mul(PERCENTAGE_FACTOR, x), y), percentage)
        }
    }

    /// @notice Executes the bps-based weighted average (x * (1 - p) + y * p), rounded half up.
    /// @param x The first value, with a weight of 1 - percentage.
    /// @param y The second value, with a weight of percentage.
    /// @param percentage The weight of y, and complement of the weight of x (in bps).
    /// @return z The result of the bps-based weighted average.
    function weightedAvg(uint256 x, uint256 y, uint256 percentage) internal pure returns (uint256 z) {
        // 1. Underflow if
        //        percentage > PERCENTAGE_FACTOR
        // 2. Overflow if
        //        y * percentage + HALF_PERCENTAGE_FACTOR > type(uint256).max
        //    <=> percentage > 0 and y > (type(uint256).max - HALF_PERCENTAGE_FACTOR) / percentage
        // 3. Overflow if
        //        x * (PERCENTAGE_FACTOR - percentage) + y * percentage + HALF_PERCENTAGE_FACTOR > type(uint256).max
        //    <=> x * (PERCENTAGE_FACTOR - percentage) > type(uint256).max - HALF_PERCENTAGE_FACTOR - y * percentage
        //    <=> PERCENTAGE_FACTOR > percentage and x > (type(uint256).max - HALF_PERCENTAGE_FACTOR - y * percentage) / (PERCENTAGE_FACTOR - percentage)
        assembly {
            z := sub(PERCENTAGE_FACTOR, percentage) // Temporary assignment to save gas.

            if or(
                gt(percentage, PERCENTAGE_FACTOR),
                or(
                    mul(percentage, gt(y, div(MAX_UINT256_MINUS_HALF_PERCENTAGE_FACTOR, percentage))),
                    mul(z, gt(x, div(sub(MAX_UINT256_MINUS_HALF_PERCENTAGE_FACTOR, mul(y, percentage)), z)))
                )
            ) { revert(0, 0) }

            z := div(add(add(mul(x, z), mul(y, percentage)), HALF_PERCENTAGE_FACTOR), PERCENTAGE_FACTOR)
        }
    }
}

File 12 of 57 : SafeTransferLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

import {ERC20} from "../tokens/ERC20.sol";

/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @author Modified from Gnosis (https://github.com/gnosis/gp-v2-contracts/blob/main/src/contracts/libraries/GPv2SafeERC20.sol)
/// @dev Use with caution! Some functions in this library knowingly create dirty bits at the destination of the free memory pointer.
library SafeTransferLib {
    /*///////////////////////////////////////////////////////////////
                            ETH OPERATIONS
    //////////////////////////////////////////////////////////////*/

    function safeTransferETH(address to, uint256 amount) internal {
        bool callStatus;

        assembly {
            // Transfer the ETH and store if it succeeded or not.
            callStatus := call(gas(), to, amount, 0, 0, 0, 0)
        }

        require(callStatus, "ETH_TRANSFER_FAILED");
    }

    /*///////////////////////////////////////////////////////////////
                           ERC20 OPERATIONS
    //////////////////////////////////////////////////////////////*/

    function safeTransferFrom(
        ERC20 token,
        address from,
        address to,
        uint256 amount
    ) internal {
        bool callStatus;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata to memory piece by piece:
            mstore(freeMemoryPointer, 0x23b872dd00000000000000000000000000000000000000000000000000000000) // Begin with the function selector.
            mstore(add(freeMemoryPointer, 4), and(from, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "from" argument.
            mstore(add(freeMemoryPointer, 36), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "to" argument.
            mstore(add(freeMemoryPointer, 68), amount) // Finally append the "amount" argument. No mask as it's a full 32 byte value.

            // Call the token and store if it succeeded or not.
            // We use 100 because the calldata length is 4 + 32 * 3.
            callStatus := call(gas(), token, 0, freeMemoryPointer, 100, 0, 0)
        }

        require(didLastOptionalReturnCallSucceed(callStatus), "TRANSFER_FROM_FAILED");
    }

    function safeTransfer(
        ERC20 token,
        address to,
        uint256 amount
    ) internal {
        bool callStatus;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata to memory piece by piece:
            mstore(freeMemoryPointer, 0xa9059cbb00000000000000000000000000000000000000000000000000000000) // Begin with the function selector.
            mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "to" argument.
            mstore(add(freeMemoryPointer, 36), amount) // Finally append the "amount" argument. No mask as it's a full 32 byte value.

            // Call the token and store if it succeeded or not.
            // We use 68 because the calldata length is 4 + 32 * 2.
            callStatus := call(gas(), token, 0, freeMemoryPointer, 68, 0, 0)
        }

        require(didLastOptionalReturnCallSucceed(callStatus), "TRANSFER_FAILED");
    }

    function safeApprove(
        ERC20 token,
        address to,
        uint256 amount
    ) internal {
        bool callStatus;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata to memory piece by piece:
            mstore(freeMemoryPointer, 0x095ea7b300000000000000000000000000000000000000000000000000000000) // Begin with the function selector.
            mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "to" argument.
            mstore(add(freeMemoryPointer, 36), amount) // Finally append the "amount" argument. No mask as it's a full 32 byte value.

            // Call the token and store if it succeeded or not.
            // We use 68 because the calldata length is 4 + 32 * 2.
            callStatus := call(gas(), token, 0, freeMemoryPointer, 68, 0, 0)
        }

        require(didLastOptionalReturnCallSucceed(callStatus), "APPROVE_FAILED");
    }

    /*///////////////////////////////////////////////////////////////
                         INTERNAL HELPER LOGIC
    //////////////////////////////////////////////////////////////*/

    function didLastOptionalReturnCallSucceed(bool callStatus) private pure returns (bool success) {
        assembly {
            // Get how many bytes the call returned.
            let returnDataSize := returndatasize()

            // If the call reverted:
            if iszero(callStatus) {
                // Copy the revert message into memory.
                returndatacopy(0, 0, returnDataSize)

                // Revert with the same message.
                revert(0, returnDataSize)
            }

            switch returnDataSize
            case 32 {
                // Copy the return data into memory.
                returndatacopy(0, 0, returnDataSize)

                // Set success to whether it returned true.
                success := iszero(iszero(mload(0)))
            }
            case 0 {
                // There was no return data.
                success := 1
            }
            default {
                // It returned some malformed input.
                success := 0
            }
        }
    }
}

File 13 of 57 : Permit2Lib.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import {ERC20} from "solmate/src/tokens/ERC20.sol";

import {IDAIPermit} from "../interfaces/IDAIPermit.sol";
import {IAllowanceTransfer} from "../interfaces/IAllowanceTransfer.sol";
import {SafeCast160} from "./SafeCast160.sol";

/// @title Permit2Lib
/// @notice Enables efficient transfers and EIP-2612/DAI
/// permits for any token by falling back to Permit2.
library Permit2Lib {
    using SafeCast160 for uint256;
    /*//////////////////////////////////////////////////////////////
                                CONSTANTS
    //////////////////////////////////////////////////////////////*/

    /// @dev The unique EIP-712 domain domain separator for the DAI token contract.
    bytes32 internal constant DAI_DOMAIN_SEPARATOR = 0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;

    /// @dev The address for the WETH9 contract on Ethereum mainnet, encoded as a bytes32.
    bytes32 internal constant WETH9_ADDRESS = 0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2;

    /// @dev The address of the Permit2 contract the library will use.
    IAllowanceTransfer internal constant PERMIT2 =
        IAllowanceTransfer(address(0x000000000022D473030F116dDEE9F6B43aC78BA3));

    /// @notice Transfer a given amount of tokens from one user to another.
    /// @param token The token to transfer.
    /// @param from The user to transfer from.
    /// @param to The user to transfer to.
    /// @param amount The amount to transfer.
    function transferFrom2(ERC20 token, address from, address to, uint256 amount) internal {
        // Generate calldata for a standard transferFrom call.
        bytes memory inputData = abi.encodeCall(ERC20.transferFrom, (from, to, amount));

        bool success; // Call the token contract as normal, capturing whether it succeeded.
        assembly {
            success :=
                and(
                    // Set success to whether the call reverted, if not we check it either
                    // returned exactly 1 (can't just be non-zero data), or had no return data.
                    or(eq(mload(0), 1), iszero(returndatasize())),
                    // Counterintuitively, this call() must be positioned after the or() in the
                    // surrounding and() because and() evaluates its arguments from right to left.
                    // We use 0 and 32 to copy up to 32 bytes of return data into the first slot of scratch space.
                    call(gas(), token, 0, add(inputData, 32), mload(inputData), 0, 32)
                )
        }

        // We'll fall back to using Permit2 if calling transferFrom on the token directly reverted.
        if (!success) PERMIT2.transferFrom(from, to, amount.toUint160(), address(token));
    }

    /*//////////////////////////////////////////////////////////////
                              PERMIT LOGIC
    //////////////////////////////////////////////////////////////*/

    /// @notice Permit a user to spend a given amount of
    /// another user's tokens via native EIP-2612 permit if possible, falling
    /// back to Permit2 if native permit fails or is not implemented on the token.
    /// @param token The token to permit spending.
    /// @param owner The user to permit spending from.
    /// @param spender The user to permit spending to.
    /// @param amount The amount to permit spending.
    /// @param deadline  The timestamp after which the signature is no longer valid.
    /// @param v Must produce valid secp256k1 signature from the owner along with r and s.
    /// @param r Must produce valid secp256k1 signature from the owner along with v and s.
    /// @param s Must produce valid secp256k1 signature from the owner along with r and v.
    function permit2(
        ERC20 token,
        address owner,
        address spender,
        uint256 amount,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        // Generate calldata for a call to DOMAIN_SEPARATOR on the token.
        bytes memory inputData = abi.encodeWithSelector(ERC20.DOMAIN_SEPARATOR.selector);

        bool success; // Call the token contract as normal, capturing whether it succeeded.
        bytes32 domainSeparator; // If the call succeeded, we'll capture the return value here.

        assembly {
            // If the token is WETH9, we know it doesn't have a DOMAIN_SEPARATOR, and we'll skip this step.
            // We make sure to mask the token address as its higher order bits aren't guaranteed to be clean.
            if iszero(eq(and(token, 0xffffffffffffffffffffffffffffffffffffffff), WETH9_ADDRESS)) {
                success :=
                    and(
                        // Should resolve false if its not 32 bytes or its first word is 0.
                        and(iszero(iszero(mload(0))), eq(returndatasize(), 32)),
                        // We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
                        // Counterintuitively, this call must be positioned second to the and() call in the
                        // surrounding and() call or else returndatasize() will be zero during the computation.
                        // We send a maximum of 5000 gas to prevent tokens with fallbacks from using a ton of gas.
                        // which should be plenty to allow tokens to fetch their DOMAIN_SEPARATOR from storage, etc.
                        staticcall(5000, token, add(inputData, 32), mload(inputData), 0, 32)
                    )

                domainSeparator := mload(0) // Copy the return value into the domainSeparator variable.
            }
        }

        // If the call to DOMAIN_SEPARATOR succeeded, try using permit on the token.
        if (success) {
            // We'll use DAI's special permit if it's DOMAIN_SEPARATOR matches,
            // otherwise we'll just encode a call to the standard permit function.
            inputData = domainSeparator == DAI_DOMAIN_SEPARATOR
                ? abi.encodeCall(IDAIPermit.permit, (owner, spender, token.nonces(owner), deadline, true, v, r, s))
                : abi.encodeCall(ERC20.permit, (owner, spender, amount, deadline, v, r, s));

            assembly {
                success := call(gas(), token, 0, add(inputData, 32), mload(inputData), 0, 0)
            }
        }

        if (!success) {
            // If the initial DOMAIN_SEPARATOR call on the token failed or a
            // subsequent call to permit failed, fall back to using Permit2.
            simplePermit2(token, owner, spender, amount, deadline, v, r, s);
        }
    }

    /// @notice Simple unlimited permit on the Permit2 contract.
    /// @param token The token to permit spending.
    /// @param owner The user to permit spending from.
    /// @param spender The user to permit spending to.
    /// @param amount The amount to permit spending.
    /// @param deadline  The timestamp after which the signature is no longer valid.
    /// @param v Must produce valid secp256k1 signature from the owner along with r and s.
    /// @param r Must produce valid secp256k1 signature from the owner along with v and s.
    /// @param s Must produce valid secp256k1 signature from the owner along with r and v.
    function simplePermit2(
        ERC20 token,
        address owner,
        address spender,
        uint256 amount,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        (,, uint48 nonce) = PERMIT2.allowance(owner, address(token), spender);

        PERMIT2.permit(
            owner,
            IAllowanceTransfer.PermitSingle({
                details: IAllowanceTransfer.PermitDetails({
                    token: address(token),
                    amount: amount.toUint160(),
                    // Use an unlimited expiration because it most
                    // closely mimics how a standard approval works.
                    expiration: type(uint48).max,
                    nonce: nonce
                }),
                spender: spender,
                sigDeadline: deadline
            }),
            bytes.concat(r, s, bytes1(v))
        );
    }
}

File 14 of 57 : EnumerableSet.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.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.
 *
 * ```
 * 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;
    }
}

File 15 of 57 : MorphoStorage.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IRewardsManager} from "./interfaces/IRewardsManager.sol";
import {IPool, IPoolAddressesProvider} from "@aave-v3-core/interfaces/IPool.sol";

import {Types} from "./libraries/Types.sol";
import {Constants} from "./libraries/Constants.sol";

import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {Initializable} from "@openzeppelin-upgradeable/proxy/utils/Initializable.sol";
import {Ownable2StepUpgradeable} from "@openzeppelin-upgradeable/access/Ownable2StepUpgradeable.sol";

/// @title MorphoStorage
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice The storage shared by Morpho's contracts.
abstract contract MorphoStorage is Initializable, Ownable2StepUpgradeable {
    /* STORAGE */

    /// @dev The address of Aave's pool.
    IPool internal _pool;

    /// @dev The address of the pool addresses provider.
    IPoolAddressesProvider internal _addressesProvider;

    /// @dev The e-mode category of the deployed Morpho.
    uint8 internal _eModeCategoryId;

    /// @dev The list of created markets.
    address[] internal _marketsCreated;

    /// @dev The markets data.
    mapping(address => Types.Market) internal _market;

    /// @dev The markets balances data.
    mapping(address => Types.MarketBalances) internal _marketBalances;

    /// @dev The collateral markets entered by users.
    mapping(address => EnumerableSet.AddressSet) internal _userCollaterals;

    /// @dev The borrow markets entered by users.
    mapping(address => EnumerableSet.AddressSet) internal _userBorrows;

    /// @dev Users allowances to manage other users' accounts. delegator => manager => isManagedBy
    mapping(address => mapping(address => bool)) internal _isManagedBy;

    /// @dev The nonce of users. Used to prevent replay attacks with EIP-712 signatures.
    mapping(address => uint256) internal _userNonce;

    /// @dev The default number of iterations to use in the matching process.
    Types.Iterations internal _defaultIterations;

    /// @dev The address of the positions manager on which calls are delegated to.
    address internal _positionsManager;

    /// @dev The address of the rewards manager to track pool rewards for users.
    IRewardsManager internal _rewardsManager;

    /// @dev The address of the treasury vault, recipient of the reserve fee.
    address internal _treasuryVault;

    /// @dev Whether claiming rewards is paused or not.
    bool internal _isClaimRewardsPaused;

    /* CONSTRUCTOR */

    /// @notice Contract constructor.
    /// @dev The implementation contract disables initialization upon deployment to avoid being hijacked.
    constructor() {
        _disableInitializers();
    }
}

File 16 of 57 : PositionsManagerInternal.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IAaveOracle} from "@aave-v3-core/interfaces/IAaveOracle.sol";
import {IPriceOracleSentinel} from "@aave-v3-core/interfaces/IPriceOracleSentinel.sol";

import {Types} from "./libraries/Types.sol";
import {Events} from "./libraries/Events.sol";
import {Errors} from "./libraries/Errors.sol";
import {Constants} from "./libraries/Constants.sol";
import {MarketLib} from "./libraries/MarketLib.sol";
import {DeltasLib} from "./libraries/DeltasLib.sol";
import {MarketSideDeltaLib} from "./libraries/MarketSideDeltaLib.sol";
import {MarketBalanceLib} from "./libraries/MarketBalanceLib.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

import {LogarithmicBuckets} from "@morpho-data-structures/LogarithmicBuckets.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {DataTypes} from "@aave-v3-core/protocol/libraries/types/DataTypes.sol";
import {UserConfiguration} from "@aave-v3-core/protocol/libraries/configuration/UserConfiguration.sol";
import {ReserveConfiguration} from "@aave-v3-core/protocol/libraries/configuration/ReserveConfiguration.sol";

import {ERC20} from "@solmate/tokens/ERC20.sol";

import {MatchingEngine} from "./MatchingEngine.sol";

/// @title PositionsManagerInternal
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Abstract contract defining `PositionsManager`'s internal functions.
abstract contract PositionsManagerInternal is MatchingEngine {
    using MarketLib for Types.Market;
    using DeltasLib for Types.Deltas;
    using MarketBalanceLib for Types.MarketBalances;
    using MarketSideDeltaLib for Types.MarketSideDelta;

    using Math for uint256;
    using WadRayMath for uint256;
    using PercentageMath for uint256;

    using EnumerableSet for EnumerableSet.AddressSet;
    using LogarithmicBuckets for LogarithmicBuckets.Buckets;

    using UserConfiguration for DataTypes.UserConfigurationMap;
    using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

    /// @dev Validates the manager's permission.
    function _validatePermission(address delegator, address manager) internal view {
        if (!(delegator == manager || _isManagedBy[delegator][manager])) revert Errors.PermissionDenied();
    }

    /// @dev Validates the input.
    function _validateInput(address underlying, uint256 amount, address user)
        internal
        view
        returns (Types.Market storage market)
    {
        if (user == address(0)) revert Errors.AddressIsZero();
        if (amount == 0) revert Errors.AmountIsZero();

        market = _market[underlying];
        if (!market.isCreated()) revert Errors.MarketNotCreated();
    }

    /// @dev Validates the manager's permission and the input.
    function _validateManagerInput(address underlying, uint256 amount, address onBehalf, address receiver)
        internal
        view
        returns (Types.Market storage market)
    {
        if (receiver == address(0)) revert Errors.AddressIsZero();

        market = _validateInput(underlying, amount, onBehalf);

        _validatePermission(onBehalf, msg.sender);
    }

    /// @dev Validates a supply action.
    function _validateSupply(address underlying, uint256 amount, address user)
        internal
        view
        returns (Types.Market storage market)
    {
        market = _validateInput(underlying, amount, user);
        if (market.isSupplyPaused()) revert Errors.SupplyIsPaused();
    }

    /// @dev Validates a supply collateral action.
    function _validateSupplyCollateral(address underlying, uint256 amount, address user) internal view {
        Types.Market storage market = _validateInput(underlying, amount, user);
        if (market.isSupplyCollateralPaused()) revert Errors.SupplyCollateralIsPaused();
        if (!market.isCollateral) revert Errors.AssetNotCollateralOnMorpho();
    }

    /// @dev Validates a borrow action.
    function _validateBorrow(address underlying, uint256 amount, address borrower, address receiver)
        internal
        view
        returns (Types.Market storage market)
    {
        market = _validateManagerInput(underlying, amount, borrower, receiver);
        if (market.isBorrowPaused()) revert Errors.BorrowIsPaused();
    }

    /// @dev Authorizes a borrow action.
    function _authorizeBorrow(address underlying, uint256 amount, Types.Indexes256 memory indexes) internal view {
        DataTypes.ReserveConfigurationMap memory config = _pool.getConfiguration(underlying);
        if (!config.getBorrowingEnabled()) revert Errors.BorrowNotEnabled();

        address priceOracleSentinel = _addressesProvider.getPriceOracleSentinel();
        if (priceOracleSentinel != address(0) && !IPriceOracleSentinel(priceOracleSentinel).isBorrowAllowed()) {
            revert Errors.SentinelBorrowNotEnabled();
        }

        if (_eModeCategoryId != 0 && _eModeCategoryId != config.getEModeCategory()) {
            revert Errors.InconsistentEMode();
        }

        if (config.getBorrowCap() != 0) {
            Types.Market storage market = _market[underlying];

            uint256 trueP2PBorrow = market.trueP2PBorrow(indexes);
            uint256 borrowCap = config.getBorrowCap() * (10 ** config.getDecimals());
            uint256 poolDebt =
                ERC20(market.variableDebtToken).totalSupply() + ERC20(market.stableDebtToken).totalSupply();

            if (amount + trueP2PBorrow + poolDebt > borrowCap) revert Errors.ExceedsBorrowCap();
        }
    }

    /// @dev Validates a repay action.
    function _validateRepay(address underlying, uint256 amount, address user)
        internal
        view
        returns (Types.Market storage market)
    {
        market = _validateInput(underlying, amount, user);
        if (market.isRepayPaused()) revert Errors.RepayIsPaused();
    }

    /// @dev Validates a withdraw action.
    function _validateWithdraw(address underlying, uint256 amount, address supplier, address receiver)
        internal
        view
        returns (Types.Market storage market)
    {
        market = _validateManagerInput(underlying, amount, supplier, receiver);
        if (market.isWithdrawPaused()) revert Errors.WithdrawIsPaused();
    }

    /// @dev Validates a withdraw collateral action.
    function _validateWithdrawCollateral(address underlying, uint256 amount, address supplier, address receiver)
        internal
        view
        returns (Types.Market storage market)
    {
        market = _validateManagerInput(underlying, amount, supplier, receiver);
        if (market.isWithdrawCollateralPaused()) revert Errors.WithdrawCollateralIsPaused();
    }

    /// @dev Validates a liquidate action.
    function _validateLiquidate(address underlyingBorrowed, address underlyingCollateral, address borrower)
        internal
        view
    {
        Types.Market storage borrowMarket = _market[underlyingBorrowed];
        Types.Market storage collateralMarket = _market[underlyingCollateral];

        if (borrower == address(0)) revert Errors.AddressIsZero();

        if (!borrowMarket.isCreated() || !collateralMarket.isCreated()) revert Errors.MarketNotCreated();
        if (collateralMarket.isLiquidateCollateralPaused()) revert Errors.LiquidateCollateralIsPaused();
        if (borrowMarket.isLiquidateBorrowPaused()) revert Errors.LiquidateBorrowIsPaused();
    }

    /// @dev Authorizes a liquidate action.
    function _authorizeLiquidate(address underlyingBorrowed, address borrower) internal view returns (uint256) {
        if (_market[underlyingBorrowed].isDeprecated()) return Constants.MAX_CLOSE_FACTOR; // Allow liquidation of the whole debt.

        uint256 healthFactor = _getUserHealthFactor(borrower);
        if (healthFactor >= Constants.DEFAULT_LIQUIDATION_MAX_HF) {
            revert Errors.UnauthorizedLiquidate();
        }

        if (healthFactor >= Constants.DEFAULT_LIQUIDATION_MIN_HF) {
            address priceOracleSentinel = _addressesProvider.getPriceOracleSentinel();

            if (priceOracleSentinel != address(0) && !IPriceOracleSentinel(priceOracleSentinel).isLiquidationAllowed())
            {
                revert Errors.SentinelLiquidateNotEnabled();
            }
        }

        if (healthFactor > Constants.DEFAULT_LIQUIDATION_MIN_HF) return Constants.DEFAULT_CLOSE_FACTOR;

        return Constants.MAX_CLOSE_FACTOR;
    }

    /// @dev Performs the accounting of a supply action.
    function _accountSupply(
        address underlying,
        uint256 amount,
        address onBehalf,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.SupplyRepayVars memory vars) {
        Types.Market storage market = _market[underlying];
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];
        vars.onPool = marketBalances.scaledPoolSupplyBalance(onBehalf);
        vars.inP2P = marketBalances.scaledP2PSupplyBalance(onBehalf);

        /* Peer-to-peer supply */

        if (!market.isP2PDisabled()) {
            // Decrease the peer-to-peer borrow delta.
            (amount, vars.toRepay) =
                market.deltas.borrow.decreaseDelta(underlying, amount, indexes.borrow.poolIndex, true);

            // Promote pool borrowers.
            uint256 promoted;
            (amount, promoted,) = _promoteRoutine(underlying, amount, maxIterations, _promoteBorrowers);
            vars.toRepay += promoted;

            // Update the peer-to-peer totals.
            vars.inP2P += market.deltas.increaseP2P(underlying, promoted, vars.toRepay, indexes, true);
        }

        /* Pool supply */

        // Supply on pool.
        (vars.toSupply, vars.onPool) = _addToPool(amount, vars.onPool, indexes.supply.poolIndex);

        (vars.onPool, vars.inP2P) = _updateSupplierInDS(underlying, onBehalf, vars.onPool, vars.inP2P, false);
    }

    /// @dev Performs the accounting of a borrow action.
    ///      Note: the borrower's set of borrowed market is updated in `_updateBorrowerInDS`.
    function _accountBorrow(
        address underlying,
        uint256 amount,
        address borrower,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.BorrowWithdrawVars memory vars) {
        Types.Market storage market = _market[underlying];
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];
        vars.onPool = marketBalances.scaledPoolBorrowBalance(borrower);
        vars.inP2P = marketBalances.scaledP2PBorrowBalance(borrower);

        /* Peer-to-peer borrow */

        if (!market.isP2PDisabled()) {
            // Decrease the peer-to-peer idle supply.
            uint256 matchedIdle;
            (amount, matchedIdle) = market.decreaseIdle(underlying, amount);

            // Decrease the peer-to-peer supply delta.
            (amount, vars.toWithdraw) =
                market.deltas.supply.decreaseDelta(underlying, amount, indexes.supply.poolIndex, false);

            // Promote pool suppliers.
            uint256 promoted;
            (amount, promoted,) = _promoteRoutine(underlying, amount, maxIterations, _promoteSuppliers);
            vars.toWithdraw += promoted;

            // Update the peer-to-peer totals.
            vars.inP2P += market.deltas.increaseP2P(underlying, promoted, vars.toWithdraw + matchedIdle, indexes, false);
        }

        /* Pool borrow */

        // Borrow on pool.
        (vars.toBorrow, vars.onPool) = _addToPool(amount, vars.onPool, indexes.borrow.poolIndex);

        (vars.onPool, vars.inP2P) = _updateBorrowerInDS(underlying, borrower, vars.onPool, vars.inP2P, false);
    }

    /// @dev Performs the accounting of a repay action.
    ///      Note: the borrower's set of borrowed market is updated in `_updateBorrowerInDS`.
    function _accountRepay(
        address underlying,
        uint256 amount,
        address onBehalf,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.SupplyRepayVars memory vars) {
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];
        vars.onPool = marketBalances.scaledPoolBorrowBalance(onBehalf);
        vars.inP2P = marketBalances.scaledP2PBorrowBalance(onBehalf);

        /* Pool repay */

        // Repay borrow on pool.
        (amount, vars.toRepay, vars.onPool) = _subFromPool(amount, vars.onPool, indexes.borrow.poolIndex);

        // Repay borrow peer-to-peer.
        vars.inP2P = vars.inP2P.zeroFloorSub(amount.rayDivUp(indexes.borrow.p2pIndex)); // In peer-to-peer borrow unit.

        (vars.onPool, vars.inP2P) = _updateBorrowerInDS(underlying, onBehalf, vars.onPool, vars.inP2P, false);

        // Returning early requires having updated the borrower in the data structure, which in turn requires having updated `inP2P`.
        if (amount == 0) return vars;

        Types.Market storage market = _market[underlying];

        // Decrease the peer-to-peer borrow delta.
        uint256 matchedBorrowDelta;
        (amount, matchedBorrowDelta) =
            market.deltas.borrow.decreaseDelta(underlying, amount, indexes.borrow.poolIndex, true);
        vars.toRepay += matchedBorrowDelta;

        // Updates the P2P total for the repay fee calculation. Events are emitted in the decreaseP2P step.
        market.deltas.borrow.scaledP2PTotal =
            market.deltas.borrow.scaledP2PTotal.zeroFloorSub(matchedBorrowDelta.rayDiv(indexes.borrow.p2pIndex));

        // Repay the fee.
        amount = market.repayFee(amount, indexes);

        /* Transfer repay */

        if (!market.isP2PDisabled()) {
            // Promote pool borrowers.
            uint256 promoted;
            (amount, promoted, maxIterations) = _promoteRoutine(underlying, amount, maxIterations, _promoteBorrowers);
            vars.toRepay += promoted;
        }

        /* Breaking repay */

        // Handle the supply cap.
        uint256 idleSupplyIncrease;
        (vars.toSupply, idleSupplyIncrease) =
            market.increaseIdle(underlying, amount, _pool.getReserveData(underlying), indexes);

        // Demote peer-to-peer suppliers.
        uint256 demoted = _demoteSuppliers(underlying, vars.toSupply, maxIterations);

        // Increase the peer-to-peer supply delta.
        market.deltas.supply.increaseDelta(underlying, vars.toSupply - demoted, indexes.supply, false);

        // Update the peer-to-peer totals.
        market.deltas.decreaseP2P(underlying, demoted, vars.toSupply + idleSupplyIncrease, indexes, false);
    }

    /// @dev Performs the accounting of a withdraw action.
    function _accountWithdraw(
        address underlying,
        uint256 amount,
        address supplier,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.BorrowWithdrawVars memory vars) {
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];
        vars.onPool = marketBalances.scaledPoolSupplyBalance(supplier);
        vars.inP2P = marketBalances.scaledP2PSupplyBalance(supplier);

        /* Pool withdraw */

        // Withdraw supply on pool.
        (amount, vars.toWithdraw, vars.onPool) = _subFromPool(amount, vars.onPool, indexes.supply.poolIndex);

        Types.Market storage market = _market[underlying];

        // Withdraw supply peer-to-peer.
        vars.inP2P = vars.inP2P.zeroFloorSub(amount.rayDivUp(indexes.supply.p2pIndex)); // In peer-to-peer supply unit.

        (vars.onPool, vars.inP2P) = _updateSupplierInDS(underlying, supplier, vars.onPool, vars.inP2P, false);

        // Returning early requires having updated the supplier in the data structure, which in turn requires having updated `inP2P`.
        if (amount == 0) return vars;

        // Decrease the peer-to-peer idle supply.
        uint256 matchedIdle;
        (amount, matchedIdle) = market.decreaseIdle(underlying, amount);

        // Decrease the peer-to-peer supply delta.
        uint256 toWithdrawStep;
        (amount, toWithdrawStep) =
            market.deltas.supply.decreaseDelta(underlying, amount, indexes.supply.poolIndex, false);
        vars.toWithdraw += toWithdrawStep;
        uint256 p2pTotalSupplyDecrease = toWithdrawStep + matchedIdle;

        /* Transfer withdraw */

        if (!market.isP2PDisabled()) {
            // Promote pool suppliers.
            (vars.toBorrow, toWithdrawStep, maxIterations) =
                _promoteRoutine(underlying, amount, maxIterations, _promoteSuppliers);
            vars.toWithdraw += toWithdrawStep;
        } else {
            vars.toBorrow = amount;
        }

        /* Breaking withdraw */

        // Demote peer-to-peer borrowers.
        uint256 demoted = _demoteBorrowers(underlying, vars.toBorrow, maxIterations);

        // Increase the peer-to-peer borrow delta.
        market.deltas.borrow.increaseDelta(underlying, vars.toBorrow - demoted, indexes.borrow, true);

        // Update the peer-to-peer totals.
        market.deltas.decreaseP2P(underlying, demoted, vars.toBorrow + p2pTotalSupplyDecrease, indexes, true);
    }

    /// @dev Performs the accounting of a supply action.
    function _accountSupplyCollateral(address underlying, uint256 amount, address onBehalf, uint256 poolSupplyIndex)
        internal
        returns (uint256 collateralBalance)
    {
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];

        collateralBalance = marketBalances.collateral[onBehalf];

        _updateRewards(onBehalf, _market[underlying].aToken, collateralBalance);

        collateralBalance += amount.rayDivDown(poolSupplyIndex);

        marketBalances.collateral[onBehalf] = collateralBalance;

        _userCollaterals[onBehalf].add(underlying);
    }

    /// @dev Performs the accounting of a withdraw collateral action.
    function _accountWithdrawCollateral(address underlying, uint256 amount, address onBehalf, uint256 poolSupplyIndex)
        internal
        returns (uint256 collateralBalance)
    {
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];

        collateralBalance = marketBalances.collateral[onBehalf];

        _updateRewards(onBehalf, _market[underlying].aToken, collateralBalance);

        collateralBalance = collateralBalance.zeroFloorSub(amount.rayDivUp(poolSupplyIndex));

        marketBalances.collateral[onBehalf] = collateralBalance;

        if (collateralBalance == 0) _userCollaterals[onBehalf].remove(underlying);
    }

    /// @dev Executes a supply action.
    function _executeSupply(
        address underlying,
        uint256 amount,
        address from,
        address onBehalf,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.SupplyRepayVars memory vars) {
        vars = _accountSupply(underlying, amount, onBehalf, maxIterations, indexes);

        emit Events.Supplied(from, onBehalf, underlying, amount, vars.onPool, vars.inP2P);
    }

    /// @dev Executes a borrow action.
    function _executeBorrow(
        address underlying,
        uint256 amount,
        address onBehalf,
        address receiver,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.BorrowWithdrawVars memory vars) {
        vars = _accountBorrow(underlying, amount, onBehalf, maxIterations, indexes);

        emit Events.Borrowed(msg.sender, onBehalf, receiver, underlying, amount, vars.onPool, vars.inP2P);
    }

    /// @dev Executes a repay action.
    function _executeRepay(
        address underlying,
        uint256 amount,
        address repayer,
        address onBehalf,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.SupplyRepayVars memory vars) {
        vars = _accountRepay(underlying, amount, onBehalf, maxIterations, indexes);

        emit Events.Repaid(repayer, onBehalf, underlying, amount, vars.onPool, vars.inP2P);
    }

    /// @dev Executes a withdraw action.
    function _executeWithdraw(
        address underlying,
        uint256 amount,
        address onBehalf,
        address receiver,
        uint256 maxIterations,
        Types.Indexes256 memory indexes
    ) internal returns (Types.BorrowWithdrawVars memory vars) {
        vars = _accountWithdraw(underlying, amount, onBehalf, maxIterations, indexes);

        emit Events.Withdrawn(msg.sender, onBehalf, receiver, underlying, amount, vars.onPool, vars.inP2P);
    }

    /// @dev Executes a supply collateral action.
    function _executeSupplyCollateral(
        address underlying,
        uint256 amount,
        address from,
        address onBehalf,
        uint256 poolSupplyIndex
    ) internal {
        uint256 collateralBalance = _accountSupplyCollateral(underlying, amount, onBehalf, poolSupplyIndex);

        emit Events.CollateralSupplied(from, onBehalf, underlying, amount, collateralBalance);
    }

    /// @dev Executes a withdraw collateral action.
    function _executeWithdrawCollateral(
        address underlying,
        uint256 amount,
        address onBehalf,
        address receiver,
        uint256 poolSupplyIndex
    ) internal {
        uint256 collateralBalance = _accountWithdrawCollateral(underlying, amount, onBehalf, poolSupplyIndex);

        emit Events.CollateralWithdrawn(msg.sender, onBehalf, receiver, underlying, amount, collateralBalance);
    }

    /// @notice Given variables from a market side, calculates the amount to supply/borrow and a new on pool amount.
    /// @param amount The amount to supply/borrow.
    /// @param onPool The current user's scaled on pool balance.
    /// @param poolIndex The current pool index.
    /// @return The amount to supply/borrow and the new on pool amount.
    function _addToPool(uint256 amount, uint256 onPool, uint256 poolIndex) internal pure returns (uint256, uint256) {
        if (amount == 0) return (0, onPool);

        return (
            amount,
            onPool + amount.rayDivDown(poolIndex) // In scaled balance.
        );
    }

    /// @notice Given variables from a market side, calculates the amount to repay/withdraw, the amount left to process, and a new on pool amount.
    /// @param amount The amount to repay/withdraw.
    /// @param onPool The current user's scaled on pool balance.
    /// @param poolIndex The current pool index.
    /// @return The amount left to process, the amount to repay/withdraw, and the new on pool amount.
    function _subFromPool(uint256 amount, uint256 onPool, uint256 poolIndex)
        internal
        pure
        returns (uint256, uint256, uint256)
    {
        if (onPool == 0) return (amount, 0, onPool);

        uint256 toProcess = Math.min(onPool.rayMul(poolIndex), amount);

        return (
            amount - toProcess,
            toProcess,
            onPool.zeroFloorSub(toProcess.rayDivUp(poolIndex)) // In scaled balance.
        );
    }

    /// @notice Given variables from a market side, promotes users and calculates the amount to repay/withdraw from promote,
    ///         the amount left to process, and the number of iterations left.
    /// @param underlying The underlying address.
    /// @param amount The amount to supply/borrow.
    /// @param maxIterations The maximum number of iterations to run.
    /// @param promote The promote function.
    /// @return The amount left to process, the amount to repay/withdraw from promote, and the number of iterations left.
    function _promoteRoutine(
        address underlying,
        uint256 amount,
        uint256 maxIterations,
        function(address, uint256, uint256) returns (uint256, uint256) promote
    ) internal returns (uint256, uint256, uint256) {
        if (amount == 0) return (amount, 0, maxIterations);

        (uint256 promoted, uint256 iterationsDone) = promote(underlying, amount, maxIterations); // In underlying.

        return (amount - promoted, promoted, maxIterations - iterationsDone);
    }

    /// @dev Calculates the amount to seize during a liquidation process.
    /// @param underlyingBorrowed The address of the underlying borrowed asset.
    /// @param underlyingCollateral The address of the underlying collateral asset.
    /// @param maxToRepay The maximum amount of `underlyingBorrowed` to repay.
    /// @param borrower The address of the borrower being liquidated.
    /// @param poolSupplyIndex The current pool supply index of the `underlyingCollateral` market.
    /// @return amountToRepay The amount of `underlyingBorrowed` to repay.
    /// @return amountToSeize The amount of `underlyingCollateral` to seize.
    function _calculateAmountToSeize(
        address underlyingBorrowed,
        address underlyingCollateral,
        uint256 maxToRepay,
        address borrower,
        uint256 poolSupplyIndex
    ) internal view returns (uint256 amountToRepay, uint256 amountToSeize) {
        Types.AmountToSeizeVars memory vars;

        DataTypes.EModeCategory memory eModeCategory;
        if (_eModeCategoryId != 0) eModeCategory = _pool.getEModeCategoryData(_eModeCategoryId);

        bool collateralIsInEMode;
        IAaveOracle oracle = IAaveOracle(_addressesProvider.getPriceOracle());
        DataTypes.ReserveConfigurationMap memory borrowedConfig = _pool.getConfiguration(underlyingBorrowed);
        DataTypes.ReserveConfigurationMap memory collateralConfig = _pool.getConfiguration(underlyingCollateral);

        (, vars.borrowedPrice, vars.borrowedTokenUnit) =
            _assetData(underlyingBorrowed, oracle, borrowedConfig, eModeCategory.priceSource);
        (collateralIsInEMode, vars.collateralPrice, vars.collateralTokenUnit) =
            _assetData(underlyingCollateral, oracle, collateralConfig, eModeCategory.priceSource);

        vars.liquidationBonus =
            collateralIsInEMode ? eModeCategory.liquidationBonus : collateralConfig.getLiquidationBonus();

        amountToRepay = maxToRepay;
        amountToSeize = (
            (amountToRepay * vars.borrowedPrice * vars.collateralTokenUnit)
                / (vars.borrowedTokenUnit * vars.collateralPrice)
        ).percentMul(vars.liquidationBonus);

        uint256 collateralBalance = _getUserCollateralBalanceFromIndex(underlyingCollateral, borrower, poolSupplyIndex);

        if (amountToSeize > collateralBalance) {
            amountToSeize = collateralBalance;
            amountToRepay = (
                (collateralBalance * vars.collateralPrice * vars.borrowedTokenUnit)
                    / (vars.borrowedPrice * vars.collateralTokenUnit)
            ).percentDiv(vars.liquidationBonus);
        }
    }
}

File 17 of 57 : IPoolAddressesProvider.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

/**
 * @title IPoolAddressesProvider
 * @author Aave
 * @notice Defines the basic interface for a Pool Addresses Provider.
 */
interface IPoolAddressesProvider {
  /**
   * @dev Emitted when the market identifier is updated.
   * @param oldMarketId The old id of the market
   * @param newMarketId The new id of the market
   */
  event MarketIdSet(string indexed oldMarketId, string indexed newMarketId);

  /**
   * @dev Emitted when the pool is updated.
   * @param oldAddress The old address of the Pool
   * @param newAddress The new address of the Pool
   */
  event PoolUpdated(address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when the pool configurator is updated.
   * @param oldAddress The old address of the PoolConfigurator
   * @param newAddress The new address of the PoolConfigurator
   */
  event PoolConfiguratorUpdated(address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when the price oracle is updated.
   * @param oldAddress The old address of the PriceOracle
   * @param newAddress The new address of the PriceOracle
   */
  event PriceOracleUpdated(address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when the ACL manager is updated.
   * @param oldAddress The old address of the ACLManager
   * @param newAddress The new address of the ACLManager
   */
  event ACLManagerUpdated(address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when the ACL admin is updated.
   * @param oldAddress The old address of the ACLAdmin
   * @param newAddress The new address of the ACLAdmin
   */
  event ACLAdminUpdated(address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when the price oracle sentinel is updated.
   * @param oldAddress The old address of the PriceOracleSentinel
   * @param newAddress The new address of the PriceOracleSentinel
   */
  event PriceOracleSentinelUpdated(address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when the pool data provider is updated.
   * @param oldAddress The old address of the PoolDataProvider
   * @param newAddress The new address of the PoolDataProvider
   */
  event PoolDataProviderUpdated(address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when a new proxy is created.
   * @param id The identifier of the proxy
   * @param proxyAddress The address of the created proxy contract
   * @param implementationAddress The address of the implementation contract
   */
  event ProxyCreated(
    bytes32 indexed id,
    address indexed proxyAddress,
    address indexed implementationAddress
  );

  /**
   * @dev Emitted when a new non-proxied contract address is registered.
   * @param id The identifier of the contract
   * @param oldAddress The address of the old contract
   * @param newAddress The address of the new contract
   */
  event AddressSet(bytes32 indexed id, address indexed oldAddress, address indexed newAddress);

  /**
   * @dev Emitted when the implementation of the proxy registered with id is updated
   * @param id The identifier of the contract
   * @param proxyAddress The address of the proxy contract
   * @param oldImplementationAddress The address of the old implementation contract
   * @param newImplementationAddress The address of the new implementation contract
   */
  event AddressSetAsProxy(
    bytes32 indexed id,
    address indexed proxyAddress,
    address oldImplementationAddress,
    address indexed newImplementationAddress
  );

  /**
   * @notice Returns the id of the Aave market to which this contract points to.
   * @return The market id
   */
  function getMarketId() external view returns (string memory);

  /**
   * @notice Associates an id with a specific PoolAddressesProvider.
   * @dev This can be used to create an onchain registry of PoolAddressesProviders to
   * identify and validate multiple Aave markets.
   * @param newMarketId The market id
   */
  function setMarketId(string calldata newMarketId) external;

  /**
   * @notice Returns an address by its identifier.
   * @dev The returned address might be an EOA or a contract, potentially proxied
   * @dev It returns ZERO if there is no registered address with the given id
   * @param id The id
   * @return The address of the registered for the specified id
   */
  function getAddress(bytes32 id) external view returns (address);

  /**
   * @notice General function to update the implementation of a proxy registered with
   * certain `id`. If there is no proxy registered, it will instantiate one and
   * set as implementation the `newImplementationAddress`.
   * @dev IMPORTANT Use this function carefully, only for ids that don't have an explicit
   * setter function, in order to avoid unexpected consequences
   * @param id The id
   * @param newImplementationAddress The address of the new implementation
   */
  function setAddressAsProxy(bytes32 id, address newImplementationAddress) external;

  /**
   * @notice Sets an address for an id replacing the address saved in the addresses map.
   * @dev IMPORTANT Use this function carefully, as it will do a hard replacement
   * @param id The id
   * @param newAddress The address to set
   */
  function setAddress(bytes32 id, address newAddress) external;

  /**
   * @notice Returns the address of the Pool proxy.
   * @return The Pool proxy address
   */
  function getPool() external view returns (address);

  /**
   * @notice Updates the implementation of the Pool, or creates a proxy
   * setting the new `pool` implementation when the function is called for the first time.
   * @param newPoolImpl The new Pool implementation
   */
  function setPoolImpl(address newPoolImpl) external;

  /**
   * @notice Returns the address of the PoolConfigurator proxy.
   * @return The PoolConfigurator proxy address
   */
  function getPoolConfigurator() external view returns (address);

  /**
   * @notice Updates the implementation of the PoolConfigurator, or creates a proxy
   * setting the new `PoolConfigurator` implementation when the function is called for the first time.
   * @param newPoolConfiguratorImpl The new PoolConfigurator implementation
   */
  function setPoolConfiguratorImpl(address newPoolConfiguratorImpl) external;

  /**
   * @notice Returns the address of the price oracle.
   * @return The address of the PriceOracle
   */
  function getPriceOracle() external view returns (address);

  /**
   * @notice Updates the address of the price oracle.
   * @param newPriceOracle The address of the new PriceOracle
   */
  function setPriceOracle(address newPriceOracle) external;

  /**
   * @notice Returns the address of the ACL manager.
   * @return The address of the ACLManager
   */
  function getACLManager() external view returns (address);

  /**
   * @notice Updates the address of the ACL manager.
   * @param newAclManager The address of the new ACLManager
   */
  function setACLManager(address newAclManager) external;

  /**
   * @notice Returns the address of the ACL admin.
   * @return The address of the ACL admin
   */
  function getACLAdmin() external view returns (address);

  /**
   * @notice Updates the address of the ACL admin.
   * @param newAclAdmin The address of the new ACL admin
   */
  function setACLAdmin(address newAclAdmin) external;

  /**
   * @notice Returns the address of the price oracle sentinel.
   * @return The address of the PriceOracleSentinel
   */
  function getPriceOracleSentinel() external view returns (address);

  /**
   * @notice Updates the address of the price oracle sentinel.
   * @param newPriceOracleSentinel The address of the new PriceOracleSentinel
   */
  function setPriceOracleSentinel(address newPriceOracleSentinel) external;

  /**
   * @notice Returns the address of the data provider.
   * @return The address of the DataProvider
   */
  function getPoolDataProvider() external view returns (address);

  /**
   * @notice Updates the address of the data provider.
   * @param newDataProvider The address of the new DataProvider
   */
  function setPoolDataProvider(address newDataProvider) external;
}

File 18 of 57 : DataTypes.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

library DataTypes {
  struct ReserveData {
    //stores the reserve configuration
    ReserveConfigurationMap configuration;
    //the liquidity index. Expressed in ray
    uint128 liquidityIndex;
    //the current supply rate. Expressed in ray
    uint128 currentLiquidityRate;
    //variable borrow index. Expressed in ray
    uint128 variableBorrowIndex;
    //the current variable borrow rate. Expressed in ray
    uint128 currentVariableBorrowRate;
    //the current stable borrow rate. Expressed in ray
    uint128 currentStableBorrowRate;
    //timestamp of last update
    uint40 lastUpdateTimestamp;
    //the id of the reserve. Represents the position in the list of the active reserves
    uint16 id;
    //aToken address
    address aTokenAddress;
    //stableDebtToken address
    address stableDebtTokenAddress;
    //variableDebtToken address
    address variableDebtTokenAddress;
    //address of the interest rate strategy
    address interestRateStrategyAddress;
    //the current treasury balance, scaled
    uint128 accruedToTreasury;
    //the outstanding unbacked aTokens minted through the bridging feature
    uint128 unbacked;
    //the outstanding debt borrowed against this asset in isolation mode
    uint128 isolationModeTotalDebt;
  }

  struct ReserveConfigurationMap {
    //bit 0-15: LTV
    //bit 16-31: Liq. threshold
    //bit 32-47: Liq. bonus
    //bit 48-55: Decimals
    //bit 56: reserve is active
    //bit 57: reserve is frozen
    //bit 58: borrowing is enabled
    //bit 59: stable rate borrowing enabled
    //bit 60: asset is paused
    //bit 61: borrowing in isolation mode is enabled
    //bit 62-63: reserved
    //bit 64-79: reserve factor
    //bit 80-115 borrow cap in whole tokens, borrowCap == 0 => no cap
    //bit 116-151 supply cap in whole tokens, supplyCap == 0 => no cap
    //bit 152-167 liquidation protocol fee
    //bit 168-175 eMode category
    //bit 176-211 unbacked mint cap in whole tokens, unbackedMintCap == 0 => minting disabled
    //bit 212-251 debt ceiling for isolation mode with (ReserveConfiguration::DEBT_CEILING_DECIMALS) decimals
    //bit 252-255 unused

    uint256 data;
  }

  struct UserConfigurationMap {
    /**
     * @dev Bitmap of the users collaterals and borrows. It is divided in pairs of bits, one pair per asset.
     * The first bit indicates if an asset is used as collateral by the user, the second whether an
     * asset is borrowed by the user.
     */
    uint256 data;
  }

  struct EModeCategory {
    // each eMode category has a custom ltv and liquidation threshold
    uint16 ltv;
    uint16 liquidationThreshold;
    uint16 liquidationBonus;
    // each eMode category may or may not have a custom oracle to override the individual assets price oracles
    address priceSource;
    string label;
  }

  enum InterestRateMode {
    NONE,
    STABLE,
    VARIABLE
  }

  struct ReserveCache {
    uint256 currScaledVariableDebt;
    uint256 nextScaledVariableDebt;
    uint256 currPrincipalStableDebt;
    uint256 currAvgStableBorrowRate;
    uint256 currTotalStableDebt;
    uint256 nextAvgStableBorrowRate;
    uint256 nextTotalStableDebt;
    uint256 currLiquidityIndex;
    uint256 nextLiquidityIndex;
    uint256 currVariableBorrowIndex;
    uint256 nextVariableBorrowIndex;
    uint256 currLiquidityRate;
    uint256 currVariableBorrowRate;
    uint256 reserveFactor;
    ReserveConfigurationMap reserveConfiguration;
    address aTokenAddress;
    address stableDebtTokenAddress;
    address variableDebtTokenAddress;
    uint40 reserveLastUpdateTimestamp;
    uint40 stableDebtLastUpdateTimestamp;
  }

  struct ExecuteLiquidationCallParams {
    uint256 reservesCount;
    uint256 debtToCover;
    address collateralAsset;
    address debtAsset;
    address user;
    bool receiveAToken;
    address priceOracle;
    uint8 userEModeCategory;
    address priceOracleSentinel;
  }

  struct ExecuteSupplyParams {
    address asset;
    uint256 amount;
    address onBehalfOf;
    uint16 referralCode;
  }

  struct ExecuteBorrowParams {
    address asset;
    address user;
    address onBehalfOf;
    uint256 amount;
    InterestRateMode interestRateMode;
    uint16 referralCode;
    bool releaseUnderlying;
    uint256 maxStableRateBorrowSizePercent;
    uint256 reservesCount;
    address oracle;
    uint8 userEModeCategory;
    address priceOracleSentinel;
  }

  struct ExecuteRepayParams {
    address asset;
    uint256 amount;
    InterestRateMode interestRateMode;
    address onBehalfOf;
    bool useATokens;
  }

  struct ExecuteWithdrawParams {
    address asset;
    uint256 amount;
    address to;
    uint256 reservesCount;
    address oracle;
    uint8 userEModeCategory;
  }

  struct ExecuteSetUserEModeParams {
    uint256 reservesCount;
    address oracle;
    uint8 categoryId;
  }

  struct FinalizeTransferParams {
    address asset;
    address from;
    address to;
    uint256 amount;
    uint256 balanceFromBefore;
    uint256 balanceToBefore;
    uint256 reservesCount;
    address oracle;
    uint8 fromEModeCategory;
  }

  struct FlashloanParams {
    address receiverAddress;
    address[] assets;
    uint256[] amounts;
    uint256[] interestRateModes;
    address onBehalfOf;
    bytes params;
    uint16 referralCode;
    uint256 flashLoanPremiumToProtocol;
    uint256 flashLoanPremiumTotal;
    uint256 maxStableRateBorrowSizePercent;
    uint256 reservesCount;
    address addressesProvider;
    uint8 userEModeCategory;
    bool isAuthorizedFlashBorrower;
  }

  struct FlashloanSimpleParams {
    address receiverAddress;
    address asset;
    uint256 amount;
    bytes params;
    uint16 referralCode;
    uint256 flashLoanPremiumToProtocol;
    uint256 flashLoanPremiumTotal;
  }

  struct FlashLoanRepaymentParams {
    uint256 amount;
    uint256 totalPremium;
    uint256 flashLoanPremiumToProtocol;
    address asset;
    address receiverAddress;
    uint16 referralCode;
  }

  struct CalculateUserAccountDataParams {
    UserConfigurationMap userConfig;
    uint256 reservesCount;
    address user;
    address oracle;
    uint8 userEModeCategory;
  }

  struct ValidateBorrowParams {
    ReserveCache reserveCache;
    UserConfigurationMap userConfig;
    address asset;
    address userAddress;
    uint256 amount;
    InterestRateMode interestRateMode;
    uint256 maxStableLoanPercent;
    uint256 reservesCount;
    address oracle;
    uint8 userEModeCategory;
    address priceOracleSentinel;
    bool isolationModeActive;
    address isolationModeCollateralAddress;
    uint256 isolationModeDebtCeiling;
  }

  struct ValidateLiquidationCallParams {
    ReserveCache debtReserveCache;
    uint256 totalDebt;
    uint256 healthFactor;
    address priceOracleSentinel;
  }

  struct CalculateInterestRatesParams {
    uint256 unbacked;
    uint256 liquidityAdded;
    uint256 liquidityTaken;
    uint256 totalStableDebt;
    uint256 totalVariableDebt;
    uint256 averageStableBorrowRate;
    uint256 reserveFactor;
    address reserve;
    address aToken;
  }

  struct InitReserveParams {
    address asset;
    address aTokenAddress;
    address stableDebtAddress;
    address variableDebtAddress;
    address interestRateStrategyAddress;
    uint16 reservesCount;
    uint16 maxNumberReserves;
  }
}

File 19 of 57 : IAaveOracle.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IPriceOracleGetter} from './IPriceOracleGetter.sol';
import {IPoolAddressesProvider} from './IPoolAddressesProvider.sol';

/**
 * @title IAaveOracle
 * @author Aave
 * @notice Defines the basic interface for the Aave Oracle
 */
interface IAaveOracle is IPriceOracleGetter {
  /**
   * @dev Emitted after the base currency is set
   * @param baseCurrency The base currency of used for price quotes
   * @param baseCurrencyUnit The unit of the base currency
   */
  event BaseCurrencySet(address indexed baseCurrency, uint256 baseCurrencyUnit);

  /**
   * @dev Emitted after the price source of an asset is updated
   * @param asset The address of the asset
   * @param source The price source of the asset
   */
  event AssetSourceUpdated(address indexed asset, address indexed source);

  /**
   * @dev Emitted after the address of fallback oracle is updated
   * @param fallbackOracle The address of the fallback oracle
   */
  event FallbackOracleUpdated(address indexed fallbackOracle);

  /**
   * @notice Returns the PoolAddressesProvider
   * @return The address of the PoolAddressesProvider contract
   */
  function ADDRESSES_PROVIDER() external view returns (IPoolAddressesProvider);

  /**
   * @notice Sets or replaces price sources of assets
   * @param assets The addresses of the assets
   * @param sources The addresses of the price sources
   */
  function setAssetSources(address[] calldata assets, address[] calldata sources) external;

  /**
   * @notice Sets the fallback oracle
   * @param fallbackOracle The address of the fallback oracle
   */
  function setFallbackOracle(address fallbackOracle) external;

  /**
   * @notice Returns a list of prices from a list of assets addresses
   * @param assets The list of assets addresses
   * @return The prices of the given assets
   */
  function getAssetsPrices(address[] calldata assets) external view returns (uint256[] memory);

  /**
   * @notice Returns the address of the source for an asset address
   * @param asset The address of the asset
   * @return The address of the source
   */
  function getSourceOfAsset(address asset) external view returns (address);

  /**
   * @notice Returns the address of the fallback oracle
   * @return The address of the fallback oracle
   */
  function getFallbackOracle() external view returns (address);
}

File 20 of 57 : LogarithmicBuckets.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

import "./BucketDLL.sol";

/// @title LogarithmicBuckets
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice The logarithmic buckets data-structure.
library LogarithmicBuckets {
    using BucketDLL for BucketDLL.List;

    struct Buckets {
        mapping(uint256 => BucketDLL.List) buckets;
        mapping(address => uint256) valueOf;
        uint256 bucketsMask;
    }

    /* ERRORS */

    /// @notice Thrown when the address is zero at insertion.
    error ZeroAddress();

    /// @notice Thrown when 0 value is inserted.
    error ZeroValue();

    /* INTERNAL */

    /// @notice Updates an account in the `buckets`.
    /// @param buckets The buckets to update.
    /// @param id The address of the account.
    /// @param newValue The new value of the account.
    /// @param head Indicates whether to insert the new values at the head or at the tail of the buckets list.
    function update(
        Buckets storage buckets,
        address id,
        uint256 newValue,
        bool head
    ) internal {
        if (id == address(0)) revert ZeroAddress();
        uint256 value = buckets.valueOf[id];
        buckets.valueOf[id] = newValue;

        if (value == 0) {
            if (newValue == 0) revert ZeroValue();
            // `highestSetBit` is used to compute the bucket associated with `newValue`.
            _insert(buckets, id, highestSetBit(newValue), head);
            return;
        }

        // `highestSetBit` is used to compute the bucket associated with `value`.
        uint256 currentBucket = highestSetBit(value);
        if (newValue == 0) {
            _remove(buckets, id, currentBucket);
            return;
        }

        // `highestSetBit` is used to compute the bucket associated with `newValue`.
        uint256 newBucket = highestSetBit(newValue);
        if (newBucket != currentBucket) {
            _remove(buckets, id, currentBucket);
            _insert(buckets, id, newBucket, head);
        }
    }

    /// @notice Returns the address in `buckets` that is a candidate for matching the value `value`.
    /// @param buckets The buckets to get the head.
    /// @param value The value to match.
    /// @return The address of the head.
    function getMatch(Buckets storage buckets, uint256 value) internal view returns (address) {
        uint256 bucketsMask = buckets.bucketsMask;
        if (bucketsMask == 0) return address(0);

        uint256 next = nextBucket(value, bucketsMask);
        if (next != 0) return buckets.buckets[next].getNext(address(0));

        // `highestSetBit` is used to compute the highest non-empty bucket.
        // Knowing that `next` == 0, it is also the highest previous non-empty bucket.
        uint256 prev = highestSetBit(bucketsMask);
        return buckets.buckets[prev].getNext(address(0));
    }

    /* PRIVATE */

    /// @notice Removes an account in the `buckets`.
    /// @dev Does not update the value.
    /// @param buckets The buckets to modify.
    /// @param id The address of the account to remove.
    /// @param bucket The mask of the bucket where to remove.
    function _remove(
        Buckets storage buckets,
        address id,
        uint256 bucket
    ) private {
        if (buckets.buckets[bucket].remove(id)) buckets.bucketsMask &= ~bucket;
    }

    /// @notice Inserts an account in the `buckets`.
    /// @dev Expects that `id` != 0.
    /// @dev Does not update the value.
    /// @param buckets The buckets to modify.
    /// @param id The address of the account to update.
    /// @param bucket The mask of the bucket where to insert.
    /// @param head Whether to insert at the head or at the tail of the list.
    function _insert(
        Buckets storage buckets,
        address id,
        uint256 bucket,
        bool head
    ) private {
        if (buckets.buckets[bucket].insert(id, head)) buckets.bucketsMask |= bucket;
    }

    /* PURE HELPERS */

    /// @notice Returns the highest set bit.
    /// @dev Used to compute the bucket associated to a given `value`.
    /// @dev Used to compute the highest non empty bucket given the `bucketsMask`.
    function highestSetBit(uint256 value) internal pure returns (uint256) {
        uint256 lowerMask = setLowerBits(value);
        return lowerMask ^ (lowerMask >> 1);
    }

    /// @notice Sets all the bits lower than (or equal to) the highest bit in the input.
    /// @dev This is the same as rounding the input the nearest upper value of the form `2 ** n - 1`.
    function setLowerBits(uint256 x) internal pure returns (uint256 y) {
        assembly {
            x := or(x, shr(1, x))
            x := or(x, shr(2, x))
            x := or(x, shr(4, x))
            x := or(x, shr(8, x))
            x := or(x, shr(16, x))
            x := or(x, shr(32, x))
            x := or(x, shr(64, x))
            y := or(x, shr(128, x))
        }
    }

    /// @notice Returns the lowest non-empty bucket containing larger values.
    /// @dev The bucket returned is the lowest that is in `bucketsMask` and not in `lowerMask`.
    function nextBucket(uint256 value, uint256 bucketsMask) internal pure returns (uint256 bucket) {
        uint256 lowerMask = setLowerBits(value);
        assembly {
            let higherBucketsMask := and(not(lowerMask), bucketsMask)
            bucket := and(higherBucketsMask, add(not(higherBucketsMask), 1))
        }
    }
}

File 21 of 57 : IAToken.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
import {IScaledBalanceToken} from "@aave-v3-core/interfaces/IScaledBalanceToken.sol";
import {IInitializableAToken} from "@aave-v3-core/interfaces/IInitializableAToken.sol";

/**
 * @title IAToken
 * @author Aave
 * @notice Defines the basic interface for an AToken.
 * @dev Aave's IAToken inherit IERC20 from OZ, using pragma 0.8.10. This interface is copied to enable compilation using a different pragma.
 */
interface IAToken is IERC20, IScaledBalanceToken, IInitializableAToken {
    /**
     * @dev Emitted during the transfer action
     * @param from The user whose tokens are being transferred
     * @param to The recipient
     * @param value The scaled amount being transferred
     * @param index The next liquidity index of the reserve
     */
    event BalanceTransfer(address indexed from, address indexed to, uint256 value, uint256 index);

    /**
     * @notice Mints `amount` aTokens to `user`
     * @param caller The address performing the mint
     * @param onBehalfOf The address of the user that will receive the minted aTokens
     * @param amount The amount of tokens getting minted
     * @param index The next liquidity index of the reserve
     * @return `true` if the the previous balance of the user was 0
     */
    function mint(address caller, address onBehalfOf, uint256 amount, uint256 index) external returns (bool);

    /**
     * @notice Burns aTokens from `user` and sends the equivalent amount of underlying to `receiverOfUnderlying`
     * @dev In some instances, the mint event could be emitted from a burn transaction
     * if the amount to burn is less than the interest that the user accrued
     * @param from The address from which the aTokens will be burned
     * @param receiverOfUnderlying The address that will receive the underlying
     * @param amount The amount being burned
     * @param index The next liquidity index of the reserve
     */
    function burn(address from, address receiverOfUnderlying, uint256 amount, uint256 index) external;

    /**
     * @notice Mints aTokens to the reserve treasury
     * @param amount The amount of tokens getting minted
     * @param index The next liquidity index of the reserve
     */
    function mintToTreasury(uint256 amount, uint256 index) external;

    /**
     * @notice Transfers aTokens in the event of a borrow being liquidated, in case the liquidators reclaims the aToken
     * @param from The address getting liquidated, current owner of the aTokens
     * @param to The recipient
     * @param value The amount of tokens getting transferred
     */
    function transferOnLiquidation(address from, address to, uint256 value) external;

    /**
     * @notice Transfers the underlying asset to `target`.
     * @dev Used by the Pool to transfer assets in borrow(), withdraw() and flashLoan()
     * @param target The recipient of the underlying
     * @param amount The amount getting transferred
     */
    function transferUnderlyingTo(address target, uint256 amount) external;

    /**
     * @notice Handles the underlying received by the aToken after the transfer has been completed.
     * @dev The default implementation is empty as with standard ERC20 tokens, nothing needs to be done after the
     * transfer is concluded. However in the future there may be aTokens that allow for example to stake the underlying
     * to receive LM rewards. In that case, `handleRepayment()` would perform the staking of the underlying asset.
     * @param user The user executing the repayment
     * @param onBehalfOf The address of the user who will get his debt reduced/removed
     * @param amount The amount getting repaid
     */
    function handleRepayment(address user, address onBehalfOf, uint256 amount) external;

    /**
     * @notice Allow passing a signed message to approve spending
     * @dev implements the permit function as for
     * https://github.com/ethereum/EIPs/blob/8a34d644aacf0f9f8f00815307fd7dd5da07655f/EIPS/eip-2612.md
     * @param owner The owner of the funds
     * @param spender The spender
     * @param value The amount
     * @param deadline The deadline timestamp, type(uint256).max for max deadline
     * @param v Signature param
     * @param s Signature param
     * @param r Signature param
     */
    function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
        external;

    /**
     * @notice Returns the address of the underlying asset of this aToken (E.g. WETH for aWETH)
     * @return The address of the underlying asset
     */
    function UNDERLYING_ASSET_ADDRESS() external view returns (address);

    /**
     * @notice Returns the address of the Aave treasury, receiving the fees on this aToken.
     * @return Address of the Aave treasury
     */
    function RESERVE_TREASURY_ADDRESS() external view returns (address);

    /**
     * @notice Get the domain separator for the token
     * @dev Return cached value if chainId matches cache, otherwise recomputes separator
     * @return The domain separator of the token at current chain
     */
    function DOMAIN_SEPARATOR() external view returns (bytes32);

    /**
     * @notice Returns the nonce for owner.
     * @param owner The address of the owner
     * @return The nonce of the owner
     */
    function nonces(address owner) external view returns (uint256);

    /**
     * @notice Rescue and transfer tokens locked in this contract
     * @param token The address of the token
     * @param to The address of the recipient
     * @param amount The amount of token to transfer
     */
    function rescueTokens(address token, address to, uint256 amount) external;
}

File 22 of 57 : IVariableDebtToken.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IScaledBalanceToken} from './IScaledBalanceToken.sol';
import {IInitializableDebtToken} from './IInitializableDebtToken.sol';

/**
 * @title IVariableDebtToken
 * @author Aave
 * @notice Defines the basic interface for a variable debt token.
 */
interface IVariableDebtToken is IScaledBalanceToken, IInitializableDebtToken {
  /**
   * @notice Mints debt token to the `onBehalfOf` address
   * @param user The address receiving the borrowed underlying, being the delegatee in case
   * of credit delegate, or same as `onBehalfOf` otherwise
   * @param onBehalfOf The address receiving the debt tokens
   * @param amount The amount of debt being minted
   * @param index The variable debt index of the reserve
   * @return True if the previous balance of the user is 0, false otherwise
   * @return The scaled total debt of the reserve
   */
  function mint(
    address user,
    address onBehalfOf,
    uint256 amount,
    uint256 index
  ) external returns (bool, uint256);

  /**
   * @notice Burns user variable debt
   * @dev In some instances, a burn transaction will emit a mint event
   * if the amount to burn is less than the interest that the user accrued
   * @param from The address from which the debt will be burned
   * @param amount The amount getting burned
   * @param index The variable debt index of the reserve
   * @return The scaled total debt of the reserve
   */
  function burn(
    address from,
    uint256 amount,
    uint256 index
  ) external returns (uint256);

  /**
   * @notice Returns the address of the underlying asset of this debtToken (E.g. WETH for variableDebtWETH)
   * @return The address of the underlying asset
   */
  function UNDERLYING_ASSET_ADDRESS() external view returns (address);
}

File 23 of 57 : WadRayMath.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

/// @title WadRayMath.
/// @author Morpho Labs.
/// @custom:contact [email protected]
/// @notice Optimized version of Aave V3 math library WadRayMath to conduct wad and ray manipulations: https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/math/WadRayMath.sol
library WadRayMath {
    /// CONSTANTS ///

    // Only direct number constants and references to such constants are supported by inline assembly.
    uint256 internal constant WAD = 1e18;
    uint256 internal constant HALF_WAD = 0.5e18;
    uint256 internal constant WAD_MINUS_ONE = 1e18 - 1;
    uint256 internal constant RAY = 1e27;
    uint256 internal constant HALF_RAY = 0.5e27;
    uint256 internal constant RAY_MINUS_ONE = 1e27 - 1;
    uint256 internal constant RAY_WAD_RATIO = 1e9;
    uint256 internal constant HALF_RAY_WAD_RATIO = 0.5e9;
    uint256 internal constant MAX_UINT256 = 2 ** 256 - 1;
    uint256 internal constant MAX_UINT256_MINUS_HALF_WAD = 2 ** 256 - 1 - 0.5e18;
    uint256 internal constant MAX_UINT256_MINUS_HALF_RAY = 2 ** 256 - 1 - 0.5e27;
    uint256 internal constant MAX_UINT256_MINUS_WAD_MINUS_ONE = 2 ** 256 - 1 - (1e18 - 1);
    uint256 internal constant MAX_UINT256_MINUS_RAY_MINUS_ONE = 2 ** 256 - 1 - (1e27 - 1);

    /// INTERNAL ///

    /// @dev Executes the wad-based multiplication of 2 numbers, rounded half up.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x * y, in wad.
    function wadMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // Overflow if
        //     x * y + HALF_WAD > type(uint256).max
        // <=> x * y > type(uint256).max - HALF_WAD
        // <=> y > 0 and x > (type(uint256).max - HALF_WAD) / y
        assembly {
            if mul(y, gt(x, div(MAX_UINT256_MINUS_HALF_WAD, y))) { revert(0, 0) }

            z := div(add(mul(x, y), HALF_WAD), WAD)
        }
    }

    /// @dev Executes the wad-based multiplication of 2 numbers, rounded down.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x * y, in wad.
    function wadMulDown(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // Overflow if
        //     x * y > type(uint256).max
        // <=> y > 0 and x > type(uint256).max / y
        assembly {
            if mul(y, gt(x, div(MAX_UINT256, y))) { revert(0, 0) }

            z := div(mul(x, y), WAD)
        }
    }

    /// @dev Executes the wad-based multiplication of 2 numbers, rounded up.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x * y, in wad.
    function wadMulUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // Overflow if
        //     x * y + WAD_MINUS_ONE > type(uint256).max
        // <=> x * y > type(uint256).max - WAD_MINUS_ONE
        // <=> y > 0 and x > (type(uint256).max - WAD_MINUS_ONE) / y
        assembly {
            if mul(y, gt(x, div(MAX_UINT256_MINUS_WAD_MINUS_ONE, y))) { revert(0, 0) }

            z := div(add(mul(x, y), WAD_MINUS_ONE), WAD)
        }
    }

    /// @dev Executes wad-based division of 2 numbers, rounded half up.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x / y, in wad.
    function wadDiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // 1. Division by 0 if
        //        y == 0
        // 2. Overflow if
        //        x * WAD + y / 2 > type(uint256).max
        //    <=> x * WAD > type(uint256).max - y / 2
        //    <=> x > (type(uint256).max - y / 2) / WAD
        assembly {
            z := div(y, 2) // Temporary assignment to save gas.

            if iszero(mul(y, iszero(gt(x, div(sub(MAX_UINT256, z), WAD))))) { revert(0, 0) }

            z := div(add(mul(WAD, x), z), y)
        }
    }

    /// @dev Executes wad-based division of 2 numbers, rounded down.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x / y, in wad.
    function wadDivDown(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // 1. Division by 0 if
        //        y == 0
        // 2. Overflow if
        //        x * WAD > type(uint256).max
        //    <=> x > type(uint256).max / WAD
        assembly {
            if iszero(mul(y, lt(x, add(div(MAX_UINT256, WAD), 1)))) { revert(0, 0) }

            z := div(mul(WAD, x), y)
        }
    }

    /// @dev Executes wad-based division of 2 numbers, rounded up.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x / y, in wad.
    function wadDivUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // 1. Division by 0 if
        //        y == 0
        // 2. Overflow if
        //        x * WAD + (y - 1) > type(uint256).max
        //    <=> x * WAD > type(uint256).max - (y - 1)
        //    <=> x > (type(uint256).max - (y - 1)) / WAD
        assembly {
            z := sub(y, 1) // Temporary assignment to save gas.

            if iszero(mul(y, iszero(gt(x, div(sub(MAX_UINT256, z), WAD))))) { revert(0, 0) }

            z := div(add(mul(WAD, x), z), y)
        }
    }

    /// @dev Executes the ray-based multiplication of 2 numbers, rounded half up.
    /// @param x Ray.
    /// @param y Ray.
    /// @return z The result of x * y, in ray.
    function rayMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // Overflow if
        //     x * y + HALF_RAY > type(uint256).max
        // <=> x * y > type(uint256).max - HALF_RAY
        // <=> y > 0 and x > (type(uint256).max - HALF_RAY) / y
        assembly {
            if mul(y, gt(x, div(MAX_UINT256_MINUS_HALF_RAY, y))) { revert(0, 0) }

            z := div(add(mul(x, y), HALF_RAY), RAY)
        }
    }

    /// @dev Executes the ray-based multiplication of 2 numbers, rounded down.
    /// @param x Ray.
    /// @param y Ray.
    /// @return z The result of x * y, in ray.
    function rayMulDown(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // Overflow if
        //     x * y > type(uint256).max
        // <=> y > 0 and x > type(uint256).max / y
        assembly {
            if mul(y, gt(x, div(MAX_UINT256, y))) { revert(0, 0) }

            z := div(mul(x, y), RAY)
        }
    }

    /// @dev Executes the ray-based multiplication of 2 numbers, rounded up.
    /// @param x Ray.
    /// @param y Wad.
    /// @return z The result of x * y, in ray.
    function rayMulUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // Overflow if
        //     x * y + RAY_MINUS_ONE > type(uint256).max
        // <=> x * y > type(uint256).max - RAY_MINUS_ONE
        // <=> y > 0 and x > (type(uint256).max - RAY_MINUS_ONE) / y
        assembly {
            if mul(y, gt(x, div(MAX_UINT256_MINUS_RAY_MINUS_ONE, y))) { revert(0, 0) }

            z := div(add(mul(x, y), RAY_MINUS_ONE), RAY)
        }
    }

    /// @dev Executes the ray-based division of 2 numbers, rounded half up.
    /// @param x Ray.
    /// @param y Ray.
    /// @return z The result of x / y, in ray.
    function rayDiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // 1. Division by 0 if
        //        y == 0
        // 2. Overflow if
        //        x * RAY + y / 2 > type(uint256).max
        //    <=> x * RAY > type(uint256).max - y / 2
        //    <=> x > (type(uint256).max - y / 2) / RAY
        assembly {
            z := div(y, 2) // Temporary assignment to save gas.

            if iszero(mul(y, iszero(gt(x, div(sub(MAX_UINT256, z), RAY))))) { revert(0, 0) }

            z := div(add(mul(RAY, x), z), y)
        }
    }

    /// @dev Executes the ray-based multiplication of 2 numbers, rounded down.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x / y, in ray.
    function rayDivDown(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // 1. Division by 0 if
        //        y == 0
        // 2. Overflow if
        //        x * RAY > type(uint256).max
        //    <=> x > type(uint256).max / RAY
        assembly {
            if iszero(mul(y, lt(x, add(div(MAX_UINT256, RAY), 1)))) { revert(0, 0) }

            z := div(mul(RAY, x), y)
        }
    }

    /// @dev Executes the ray-based multiplication of 2 numbers, rounded up.
    /// @param x Wad.
    /// @param y Wad.
    /// @return z The result of x / y, in ray.
    function rayDivUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // 1. Division by 0 if
        //        y == 0
        // 2. Overflow if
        //        x * RAY + (y - 1) > type(uint256).max
        //    <=> x * RAY > type(uint256).max - (y - 1)
        //    <=> x > (type(uint256).max - (y - 1)) / RAY
        assembly {
            z := sub(y, 1) // Temporary assignment to save gas.

            if iszero(mul(y, iszero(gt(x, div(sub(MAX_UINT256, z), RAY))))) { revert(0, 0) }

            z := div(add(mul(RAY, x), z), y)
        }
    }

    /// @dev Converts ray down to wad.
    /// @param x Ray.
    /// @return y = x converted to wad, rounded half up to the nearest wad.
    function rayToWad(uint256 x) internal pure returns (uint256 y) {
        assembly {
            // If x % RAY_WAD_RATIO >= HALF_RAY_WAD_RATIO, round up.
            y := add(div(x, RAY_WAD_RATIO), iszero(lt(mod(x, RAY_WAD_RATIO), HALF_RAY_WAD_RATIO)))
        }
    }

    /// @dev Converts wad up to ray.
    /// @param x Wad.
    /// @return y = x converted in ray.
    function wadToRay(uint256 x) internal pure returns (uint256 y) {
        // Overflow if
        //     x * RAY_WAD_RATIO > type(uint256).max
        // <=> x > type(uint256).max / RAY_WAD_RATIO
        assembly {
            if gt(x, div(MAX_UINT256, RAY_WAD_RATIO)) { revert(0, 0) }

            y := mul(x, RAY_WAD_RATIO)
        }
    }

    /// @notice Computes the wad-based weighted average (x * (1 - weight) + y * weight), rounded half up.
    /// @param x The first value, with a weight of 1 - weight.
    /// @param y The second value, with a weight of weight.
    /// @param weight The weight of y, and complement of the weight of x (in wad).
    /// @return z The result of the wad-based weighted average.
    function wadWeightedAvg(uint256 x, uint256 y, uint256 weight) internal pure returns (uint256 z) {
        // 1. Underflow if
        //        weight > WAD
        // 2. Overflow if
        //        y * weight + HALF_WAD > type(uint256).max
        //    <=> weight > 0 and y > (type(uint256).max - HALF_WAD) / weight
        // 3. Overflow if
        //        x * (WAD - weight) + y * weight + HALF_WAD > type(uint256).max
        //    <=> x * (WAD - weight) > type(uint256).max - HALF_WAD - y * weight
        //    <=> WAD > weight and x > (type(uint256).max - HALF_WAD - y * weight) / (WAD - weight)
        assembly {
            z := sub(WAD, weight) // Temporary assignment to save gas.

            if or(
                gt(weight, WAD),
                or(
                    mul(weight, gt(y, div(MAX_UINT256_MINUS_HALF_WAD, weight))),
                    mul(z, gt(x, div(sub(MAX_UINT256_MINUS_HALF_WAD, mul(y, weight)), z)))
                )
            ) { revert(0, 0) }

            z := div(add(add(mul(x, z), mul(y, weight)), HALF_WAD), WAD)
        }
    }

    /// @notice Computes the ray-based weighted average (x * (1 - weight) + y * weight), rounded half up.
    /// @param x The first value, with a weight of 1 - weight.
    /// @param y The second value, with a weight of weight.
    /// @param weight The weight of y, and complement of the weight of x (in ray).
    /// @return z The result of the ray-based weighted average.
    function rayWeightedAvg(uint256 x, uint256 y, uint256 weight) internal pure returns (uint256 z) {
        // 1. Underflow if
        //        weight > RAY
        // 2. Overflow if
        //        y * weight + HALF_RAY > type(uint256).max
        //    <=> weight > 0 and y > (type(uint256).max - HALF_RAY) / weight
        // 3. Overflow if
        //        x * (RAY - weight) + y * weight + HALF_RAY > type(uint256).max
        //    <=> x * (RAY - weight) > type(uint256).max - HALF_RAY - y * weight
        //    <=> RAY > weight and x > (type(uint256).max - HALF_RAY - y * weight) / (RAY - weight)
        assembly {
            z := sub(RAY, weight) // Temporary assignment to save gas.

            if or(
                gt(weight, RAY),
                or(
                    mul(weight, gt(y, div(MAX_UINT256_MINUS_HALF_RAY, weight))),
                    mul(z, gt(x, div(sub(MAX_UINT256_MINUS_HALF_RAY, mul(y, weight)), z)))
                )
            ) { revert(0, 0) }

            z := div(add(add(mul(x, z), mul(y, weight)), HALF_RAY), RAY)
        }
    }
}

File 24 of 57 : ERC20.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
    /*///////////////////////////////////////////////////////////////
                                  EVENTS
    //////////////////////////////////////////////////////////////*/

    event Transfer(address indexed from, address indexed to, uint256 amount);

    event Approval(address indexed owner, address indexed spender, uint256 amount);

    /*///////////////////////////////////////////////////////////////
                             METADATA STORAGE
    //////////////////////////////////////////////////////////////*/

    string public name;

    string public symbol;

    uint8 public immutable decimals;

    /*///////////////////////////////////////////////////////////////
                              ERC20 STORAGE
    //////////////////////////////////////////////////////////////*/

    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    mapping(address => mapping(address => uint256)) public allowance;

    /*///////////////////////////////////////////////////////////////
                             EIP-2612 STORAGE
    //////////////////////////////////////////////////////////////*/

    bytes32 public constant PERMIT_TYPEHASH =
        keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

    uint256 internal immutable INITIAL_CHAIN_ID;

    bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;

    mapping(address => uint256) public nonces;

    /*///////////////////////////////////////////////////////////////
                               CONSTRUCTOR
    //////////////////////////////////////////////////////////////*/

    constructor(
        string memory _name,
        string memory _symbol,
        uint8 _decimals
    ) {
        name = _name;
        symbol = _symbol;
        decimals = _decimals;

        INITIAL_CHAIN_ID = block.chainid;
        INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
    }

    /*///////////////////////////////////////////////////////////////
                              ERC20 LOGIC
    //////////////////////////////////////////////////////////////*/

    function approve(address spender, uint256 amount) public virtual returns (bool) {
        allowance[msg.sender][spender] = amount;

        emit Approval(msg.sender, spender, amount);

        return true;
    }

    function transfer(address to, uint256 amount) public virtual returns (bool) {
        balanceOf[msg.sender] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(msg.sender, to, amount);

        return true;
    }

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual returns (bool) {
        uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.

        if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;

        balanceOf[from] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(from, to, amount);

        return true;
    }

    /*///////////////////////////////////////////////////////////////
                              EIP-2612 LOGIC
    //////////////////////////////////////////////////////////////*/

    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) public virtual {
        require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

        // Unchecked because the only math done is incrementing
        // the owner's nonce which cannot realistically overflow.
        unchecked {
            bytes32 digest = keccak256(
                abi.encodePacked(
                    "\x19\x01",
                    DOMAIN_SEPARATOR(),
                    keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
                )
            );

            address recoveredAddress = ecrecover(digest, v, r, s);

            require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");

            allowance[recoveredAddress][spender] = value;
        }

        emit Approval(owner, spender, value);
    }

    function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
        return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
    }

    function computeDomainSeparator() internal view virtual returns (bytes32) {
        return
            keccak256(
                abi.encode(
                    keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                    keccak256(bytes(name)),
                    keccak256("1"),
                    block.chainid,
                    address(this)
                )
            );
    }

    /*///////////////////////////////////////////////////////////////
                       INTERNAL MINT/BURN LOGIC
    //////////////////////////////////////////////////////////////*/

    function _mint(address to, uint256 amount) internal virtual {
        totalSupply += amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(address(0), to, amount);
    }

    function _burn(address from, uint256 amount) internal virtual {
        balanceOf[from] -= amount;

        // Cannot underflow because a user's balance
        // will never be larger than the total supply.
        unchecked {
            totalSupply -= amount;
        }

        emit Transfer(from, address(0), amount);
    }
}

File 25 of 57 : ERC20.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
    /*//////////////////////////////////////////////////////////////
                                 EVENTS
    //////////////////////////////////////////////////////////////*/

    event Transfer(address indexed from, address indexed to, uint256 amount);

    event Approval(address indexed owner, address indexed spender, uint256 amount);

    /*//////////////////////////////////////////////////////////////
                            METADATA STORAGE
    //////////////////////////////////////////////////////////////*/

    string public name;

    string public symbol;

    uint8 public immutable decimals;

    /*//////////////////////////////////////////////////////////////
                              ERC20 STORAGE
    //////////////////////////////////////////////////////////////*/

    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    mapping(address => mapping(address => uint256)) public allowance;

    /*//////////////////////////////////////////////////////////////
                            EIP-2612 STORAGE
    //////////////////////////////////////////////////////////////*/

    uint256 internal immutable INITIAL_CHAIN_ID;

    bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;

    mapping(address => uint256) public nonces;

    /*//////////////////////////////////////////////////////////////
                               CONSTRUCTOR
    //////////////////////////////////////////////////////////////*/

    constructor(
        string memory _name,
        string memory _symbol,
        uint8 _decimals
    ) {
        name = _name;
        symbol = _symbol;
        decimals = _decimals;

        INITIAL_CHAIN_ID = block.chainid;
        INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
    }

    /*//////////////////////////////////////////////////////////////
                               ERC20 LOGIC
    //////////////////////////////////////////////////////////////*/

    function approve(address spender, uint256 amount) public virtual returns (bool) {
        allowance[msg.sender][spender] = amount;

        emit Approval(msg.sender, spender, amount);

        return true;
    }

    function transfer(address to, uint256 amount) public virtual returns (bool) {
        balanceOf[msg.sender] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(msg.sender, to, amount);

        return true;
    }

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual returns (bool) {
        uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.

        if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;

        balanceOf[from] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(from, to, amount);

        return true;
    }

    /*//////////////////////////////////////////////////////////////
                             EIP-2612 LOGIC
    //////////////////////////////////////////////////////////////*/

    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) public virtual {
        require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

        // Unchecked because the only math done is incrementing
        // the owner's nonce which cannot realistically overflow.
        unchecked {
            address recoveredAddress = ecrecover(
                keccak256(
                    abi.encodePacked(
                        "\x19\x01",
                        DOMAIN_SEPARATOR(),
                        keccak256(
                            abi.encode(
                                keccak256(
                                    "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
                                ),
                                owner,
                                spender,
                                value,
                                nonces[owner]++,
                                deadline
                            )
                        )
                    )
                ),
                v,
                r,
                s
            );

            require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");

            allowance[recoveredAddress][spender] = value;
        }

        emit Approval(owner, spender, value);
    }

    function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
        return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
    }

    function computeDomainSeparator() internal view virtual returns (bytes32) {
        return
            keccak256(
                abi.encode(
                    keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                    keccak256(bytes(name)),
                    keccak256("1"),
                    block.chainid,
                    address(this)
                )
            );
    }

    /*//////////////////////////////////////////////////////////////
                        INTERNAL MINT/BURN LOGIC
    //////////////////////////////////////////////////////////////*/

    function _mint(address to, uint256 amount) internal virtual {
        totalSupply += amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(address(0), to, amount);
    }

    function _burn(address from, uint256 amount) internal virtual {
        balanceOf[from] -= amount;

        // Cannot underflow because a user's balance
        // will never be larger than the total supply.
        unchecked {
            totalSupply -= amount;
        }

        emit Transfer(from, address(0), amount);
    }
}

File 26 of 57 : IDAIPermit.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

interface IDAIPermit {
    /// @param holder The address of the token owner.
    /// @param spender The address of the token spender.
    /// @param nonce The owner's nonce, increases at each call to permit.
    /// @param expiry The timestamp at which the permit is no longer valid.
    /// @param allowed Boolean that sets approval amount, true for type(uint256).max and false for 0.
    /// @param v Must produce valid secp256k1 signature from the owner along with r and s.
    /// @param r Must produce valid secp256k1 signature from the owner along with v and s.
    /// @param s Must produce valid secp256k1 signature from the owner along with r and v.
    function permit(
        address holder,
        address spender,
        uint256 nonce,
        uint256 expiry,
        bool allowed,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;
}

File 27 of 57 : IAllowanceTransfer.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

/// @title AllowanceTransfer
/// @notice Handles ERC20 token permissions through signature based allowance setting and ERC20 token transfers by checking allowed amounts
/// @dev Requires user's token approval on the Permit2 contract
interface IAllowanceTransfer {
    /// @notice Thrown when an allowance on a token has expired.
    /// @param deadline The timestamp at which the allowed amount is no longer valid
    error AllowanceExpired(uint256 deadline);

    /// @notice Thrown when an allowance on a token has been depleted.
    /// @param amount The maximum amount allowed
    error InsufficientAllowance(uint256 amount);

    /// @notice Thrown when too many nonces are invalidated.
    error ExcessiveInvalidation();

    /// @notice Emits an event when the owner successfully invalidates an ordered nonce.
    event NonceInvalidation(
        address indexed owner, address indexed token, address indexed spender, uint48 newNonce, uint48 oldNonce
    );

    /// @notice Emits an event when the owner successfully sets permissions on a token for the spender.
    event Approval(
        address indexed owner, address indexed token, address indexed spender, uint160 amount, uint48 expiration
    );

    /// @notice Emits an event when the owner successfully sets permissions using a permit signature on a token for the spender.
    event Permit(
        address indexed owner,
        address indexed token,
        address indexed spender,
        uint160 amount,
        uint48 expiration,
        uint48 nonce
    );

    /// @notice Emits an event when the owner sets the allowance back to 0 with the lockdown function.
    event Lockdown(address indexed owner, address token, address spender);

    /// @notice The permit data for a token
    struct PermitDetails {
        // ERC20 token address
        address token;
        // the maximum amount allowed to spend
        uint160 amount;
        // timestamp at which a spender's token allowances become invalid
        uint48 expiration;
        // an incrementing value indexed per owner,token,and spender for each signature
        uint48 nonce;
    }

    /// @notice The permit message signed for a single token allownce
    struct PermitSingle {
        // the permit data for a single token alownce
        PermitDetails details;
        // address permissioned on the allowed tokens
        address spender;
        // deadline on the permit signature
        uint256 sigDeadline;
    }

    /// @notice The permit message signed for multiple token allowances
    struct PermitBatch {
        // the permit data for multiple token allowances
        PermitDetails[] details;
        // address permissioned on the allowed tokens
        address spender;
        // deadline on the permit signature
        uint256 sigDeadline;
    }

    /// @notice The saved permissions
    /// @dev This info is saved per owner, per token, per spender and all signed over in the permit message
    /// @dev Setting amount to type(uint160).max sets an unlimited approval
    struct PackedAllowance {
        // amount allowed
        uint160 amount;
        // permission expiry
        uint48 expiration;
        // an incrementing value indexed per owner,token,and spender for each signature
        uint48 nonce;
    }

    /// @notice A token spender pair.
    struct TokenSpenderPair {
        // the token the spender is approved
        address token;
        // the spender address
        address spender;
    }

    /// @notice Details for a token transfer.
    struct AllowanceTransferDetails {
        // the owner of the token
        address from;
        // the recipient of the token
        address to;
        // the amount of the token
        uint160 amount;
        // the token to be transferred
        address token;
    }

    /// @notice A mapping from owner address to token address to spender address to PackedAllowance struct, which contains details and conditions of the approval.
    /// @notice The mapping is indexed in the above order see: allowance[ownerAddress][tokenAddress][spenderAddress]
    /// @dev The packed slot holds the allowed amount, expiration at which the allowed amount is no longer valid, and current nonce thats updated on any signature based approvals.
    function allowance(address, address, address) external view returns (uint160, uint48, uint48);

    /// @notice Approves the spender to use up to amount of the specified token up until the expiration
    /// @param token The token to approve
    /// @param spender The spender address to approve
    /// @param amount The approved amount of the token
    /// @param expiration The timestamp at which the approval is no longer valid
    /// @dev The packed allowance also holds a nonce, which will stay unchanged in approve
    /// @dev Setting amount to type(uint160).max sets an unlimited approval
    function approve(address token, address spender, uint160 amount, uint48 expiration) external;

    /// @notice Permit a spender to a given amount of the owners token via the owner's EIP-712 signature
    /// @dev May fail if the owner's nonce was invalidated in-flight by invalidateNonce
    /// @param owner The owner of the tokens being approved
    /// @param permitSingle Data signed over by the owner specifying the terms of approval
    /// @param signature The owner's signature over the permit data
    function permit(address owner, PermitSingle memory permitSingle, bytes calldata signature) external;

    /// @notice Permit a spender to the signed amounts of the owners tokens via the owner's EIP-712 signature
    /// @dev May fail if the owner's nonce was invalidated in-flight by invalidateNonce
    /// @param owner The owner of the tokens being approved
    /// @param permitBatch Data signed over by the owner specifying the terms of approval
    /// @param signature The owner's signature over the permit data
    function permit(address owner, PermitBatch memory permitBatch, bytes calldata signature) external;

    /// @notice Transfer approved tokens from one address to another
    /// @param from The address to transfer from
    /// @param to The address of the recipient
    /// @param amount The amount of the token to transfer
    /// @param token The token address to transfer
    /// @dev Requires the from address to have approved at least the desired amount
    /// of tokens to msg.sender.
    function transferFrom(address from, address to, uint160 amount, address token) external;

    /// @notice Transfer approved tokens in a batch
    /// @param transferDetails Array of owners, recipients, amounts, and tokens for the transfers
    /// @dev Requires the from addresses to have approved at least the desired amount
    /// of tokens to msg.sender.
    function transferFrom(AllowanceTransferDetails[] calldata transferDetails) external;

    /// @notice Enables performing a "lockdown" of the sender's Permit2 identity
    /// by batch revoking approvals
    /// @param approvals Array of approvals to revoke.
    function lockdown(TokenSpenderPair[] calldata approvals) external;

    /// @notice Invalidate nonces for a given (token, spender) pair
    /// @param token The token to invalidate nonces for
    /// @param spender The spender to invalidate nonces for
    /// @param newNonce The new nonce to set. Invalidates all nonces less than it.
    /// @dev Can't invalidate more than 2**16 nonces per transaction.
    function invalidateNonces(address token, address spender, uint48 newNonce) external;
}

File 28 of 57 : SafeCast160.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

library SafeCast160 {
    /// @notice Thrown when a valude greater than type(uint160).max is cast to uint160
    error UnsafeCast();

    /// @notice Safely casts uint256 to uint160
    /// @param value The uint256 to be cast
    function toUint160(uint256 value) internal pure returns (uint160) {
        if (value > type(uint160).max) revert UnsafeCast();
        return uint160(value);
    }
}

File 29 of 57 : IRewardsManager.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.5.0;

interface IRewardsManager {
    function MORPHO() external view returns (address);
    function REWARDS_CONTROLLER() external view returns (address);

    function getRewardData(address asset, address reward)
        external
        view
        returns (uint256 startingIndex, uint256 index, uint256 lastUpdateTimestamp);
    function getUserData(address asset, address reward, address user)
        external
        view
        returns (uint256 index, uint256 accrued);

    function getAllUserRewards(address[] calldata assets, address user)
        external
        view
        returns (address[] memory rewardsList, uint256[] memory unclaimedAmounts);
    function getUserRewards(address[] calldata assets, address user, address reward) external view returns (uint256);
    function getUserAccruedRewards(address[] calldata assets, address user, address reward)
        external
        view
        returns (uint256 totalAccrued);
    function getUserAssetIndex(address user, address asset, address reward) external view returns (uint256);
    function getAssetIndex(address asset, address reward) external view returns (uint256 assetIndex);

    function claimRewards(address[] calldata assets, address user)
        external
        returns (address[] memory rewardsList, uint256[] memory claimedAmounts);
    function updateUserRewards(address user, address asset, uint256 userBalance) external;
}

File 30 of 57 : Initializable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.1) (proxy/utils/Initializable.sol)

pragma solidity ^0.8.2;

import "../../utils/AddressUpgradeable.sol";

/**
 * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
 * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
 * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
 * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
 *
 * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
 * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
 * case an upgrade adds a module that needs to be initialized.
 *
 * For example:
 *
 * [.hljs-theme-light.nopadding]
 * ```
 * contract MyToken is ERC20Upgradeable {
 *     function initialize() initializer public {
 *         __ERC20_init("MyToken", "MTK");
 *     }
 * }
 * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
 *     function initializeV2() reinitializer(2) public {
 *         __ERC20Permit_init("MyToken");
 *     }
 * }
 * ```
 *
 * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
 * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
 *
 * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
 * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
 *
 * [CAUTION]
 * ====
 * Avoid leaving a contract uninitialized.
 *
 * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
 * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
 * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
 *
 * [.hljs-theme-light.nopadding]
 * ```
 * /// @custom:oz-upgrades-unsafe-allow constructor
 * constructor() {
 *     _disableInitializers();
 * }
 * ```
 * ====
 */
abstract contract Initializable {
    /**
     * @dev Indicates that the contract has been initialized.
     * @custom:oz-retyped-from bool
     */
    uint8 private _initialized;

    /**
     * @dev Indicates that the contract is in the process of being initialized.
     */
    bool private _initializing;

    /**
     * @dev Triggered when the contract has been initialized or reinitialized.
     */
    event Initialized(uint8 version);

    /**
     * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
     * `onlyInitializing` functions can be used to initialize parent contracts.
     *
     * Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
     * constructor.
     *
     * Emits an {Initialized} event.
     */
    modifier initializer() {
        bool isTopLevelCall = !_initializing;
        require(
            (isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
            "Initializable: contract is already initialized"
        );
        _initialized = 1;
        if (isTopLevelCall) {
            _initializing = true;
        }
        _;
        if (isTopLevelCall) {
            _initializing = false;
            emit Initialized(1);
        }
    }

    /**
     * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
     * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
     * used to initialize parent contracts.
     *
     * A reinitializer may be used after the original initialization step. This is essential to configure modules that
     * are added through upgrades and that require initialization.
     *
     * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
     * cannot be nested. If one is invoked in the context of another, execution will revert.
     *
     * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
     * a contract, executing them in the right order is up to the developer or operator.
     *
     * WARNING: setting the version to 255 will prevent any future reinitialization.
     *
     * Emits an {Initialized} event.
     */
    modifier reinitializer(uint8 version) {
        require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
        _initialized = version;
        _initializing = true;
        _;
        _initializing = false;
        emit Initialized(version);
    }

    /**
     * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
     * {initializer} and {reinitializer} modifiers, directly or indirectly.
     */
    modifier onlyInitializing() {
        require(_initializing, "Initializable: contract is not initializing");
        _;
    }

    /**
     * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
     * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
     * to any version. It is recommended to use this to lock implementation contracts that are designed to be called
     * through proxies.
     *
     * Emits an {Initialized} event the first time it is successfully executed.
     */
    function _disableInitializers() internal virtual {
        require(!_initializing, "Initializable: contract is initializing");
        if (_initialized < type(uint8).max) {
            _initialized = type(uint8).max;
            emit Initialized(type(uint8).max);
        }
    }

    /**
     * @dev Returns the highest version that has been initialized. See {reinitializer}.
     */
    function _getInitializedVersion() internal view returns (uint8) {
        return _initialized;
    }

    /**
     * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
     */
    function _isInitializing() internal view returns (bool) {
        return _initializing;
    }
}

File 31 of 57 : Ownable2StepUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (access/Ownable2Step.sol)

pragma solidity ^0.8.0;

import "./OwnableUpgradeable.sol";
import "../proxy/utils/Initializable.sol";

/**
 * @dev Contract module which provides 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} and {acceptOwnership}.
 *
 * This module is used through inheritance. It will make available all functions
 * from parent (Ownable).
 */
abstract contract Ownable2StepUpgradeable is Initializable, OwnableUpgradeable {
    function __Ownable2Step_init() internal onlyInitializing {
        __Ownable_init_unchained();
    }

    function __Ownable2Step_init_unchained() internal onlyInitializing {
    }
    address private _pendingOwner;

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

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

    /**
     * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        _pendingOwner = newOwner;
        emit OwnershipTransferStarted(owner(), newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual override {
        delete _pendingOwner;
        super._transferOwnership(newOwner);
    }

    /**
     * @dev The new owner accepts the ownership transfer.
     */
    function acceptOwnership() external {
        address sender = _msgSender();
        require(pendingOwner() == sender, "Ownable2Step: caller is not the new owner");
        _transferOwnership(sender);
    }

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[49] private __gap;
}

File 32 of 57 : IPriceOracleSentinel.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IPoolAddressesProvider} from './IPoolAddressesProvider.sol';

/**
 * @title IPriceOracleSentinel
 * @author Aave
 * @notice Defines the basic interface for the PriceOracleSentinel
 */
interface IPriceOracleSentinel {
  /**
   * @dev Emitted after the sequencer oracle is updated
   * @param newSequencerOracle The new sequencer oracle
   */
  event SequencerOracleUpdated(address newSequencerOracle);

  /**
   * @dev Emitted after the grace period is updated
   * @param newGracePeriod The new grace period value
   */
  event GracePeriodUpdated(uint256 newGracePeriod);

  /**
   * @notice Returns the PoolAddressesProvider
   * @return The address of the PoolAddressesProvider contract
   */
  function ADDRESSES_PROVIDER() external view returns (IPoolAddressesProvider);

  /**
   * @notice Returns true if the `borrow` operation is allowed.
   * @dev Operation not allowed when PriceOracle is down or grace period not passed.
   * @return True if the `borrow` operation is allowed, false otherwise.
   */
  function isBorrowAllowed() external view returns (bool);

  /**
   * @notice Returns true if the `liquidation` operation is allowed.
   * @dev Operation not allowed when PriceOracle is down or grace period not passed.
   * @return True if the `liquidation` operation is allowed, false otherwise.
   */
  function isLiquidationAllowed() external view returns (bool);

  /**
   * @notice Updates the address of the sequencer oracle
   * @param newSequencerOracle The address of the new Sequencer Oracle to use
   */
  function setSequencerOracle(address newSequencerOracle) external;

  /**
   * @notice Updates the duration of the grace period
   * @param newGracePeriod The value of the new grace period duration
   */
  function setGracePeriod(uint256 newGracePeriod) external;

  /**
   * @notice Returns the SequencerOracle
   * @return The address of the sequencer oracle contract
   */
  function getSequencerOracle() external view returns (address);

  /**
   * @notice Returns the grace period
   * @return The duration of the grace period
   */
  function getGracePeriod() external view returns (uint256);
}

File 33 of 57 : MarketLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IAToken} from "../interfaces/aave/IAToken.sol";
import {IPool} from "@aave-v3-core/interfaces/IPool.sol";

import {Types} from "./Types.sol";
import {Events} from "./Events.sol";
import {Errors} from "./Errors.sol";
import {ReserveDataLib} from "./ReserveDataLib.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";

import {DataTypes} from "@aave-v3-core/protocol/libraries/types/DataTypes.sol";
import {ReserveConfiguration} from "@aave-v3-core/protocol/libraries/configuration/ReserveConfiguration.sol";

/// @title MarketLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library used to ease market reads and writes.
library MarketLib {
    using Math for uint256;
    using SafeCast for uint256;
    using WadRayMath for uint256;
    using MarketLib for Types.Market;
    using ReserveDataLib for DataTypes.ReserveData;
    using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

    /// @notice Returns whether the `market` is created or not.
    function isCreated(Types.Market storage market) internal view returns (bool) {
        return market.aToken != address(0);
    }

    /// @notice Returns whether supply is paused on `market` or not.
    function isSupplyPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isSupplyPaused;
    }

    /// @notice Returns whether supply collateral is paused on `market` or not.
    function isSupplyCollateralPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isSupplyCollateralPaused;
    }

    /// @notice Returns whether borrow is paused on `market` or not.
    function isBorrowPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isBorrowPaused;
    }

    /// @notice Returns whether repay is paused on `market` or not.
    function isRepayPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isRepayPaused;
    }

    /// @notice Returns whether withdraw is paused on `market` or not.
    function isWithdrawPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isWithdrawPaused;
    }

    /// @notice Returns whether withdraw collateral is paused on `market` or not.
    function isWithdrawCollateralPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isWithdrawCollateralPaused;
    }

    /// @notice Returns whether liquidate collateral is paused on `market` or not.
    function isLiquidateCollateralPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isLiquidateCollateralPaused;
    }

    /// @notice Returns whether liquidate borrow is paused on `market` or not.
    function isLiquidateBorrowPaused(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isLiquidateBorrowPaused;
    }

    /// @notice Returns whether the `market` is deprecated or not.
    function isDeprecated(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isDeprecated;
    }

    /// @notice Returns whether the peer-to-peer is disabled on `market` or not.
    function isP2PDisabled(Types.Market storage market) internal view returns (bool) {
        return market.pauseStatuses.isP2PDisabled;
    }

    /// @notice Sets the `market` as `isCollateral` on Morpho.
    function setAssetIsCollateral(Types.Market storage market, bool isCollateral) internal {
        market.isCollateral = isCollateral;

        emit Events.IsCollateralSet(market.underlying, isCollateral);
    }

    /// @notice Sets the `market` supply pause status as `isPaused` on Morpho.
    function setIsSupplyPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isSupplyPaused = isPaused;

        emit Events.IsSupplyPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` supply collateral pause status as `isPaused` on Morpho.
    function setIsSupplyCollateralPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isSupplyCollateralPaused = isPaused;

        emit Events.IsSupplyCollateralPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` borrow pause status as `isPaused` on Morpho.
    function setIsBorrowPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isBorrowPaused = isPaused;

        emit Events.IsBorrowPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` repay pause status as `isPaused` on Morpho.
    function setIsRepayPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isRepayPaused = isPaused;

        emit Events.IsRepayPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` withdraw pause status as `isPaused` on Morpho.
    function setIsWithdrawPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isWithdrawPaused = isPaused;

        emit Events.IsWithdrawPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` withdraw collateral pause status as `isPaused` on Morpho.
    function setIsWithdrawCollateralPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isWithdrawCollateralPaused = isPaused;

        emit Events.IsWithdrawCollateralPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` liquidate collateral pause status as `isPaused` on Morpho.
    function setIsLiquidateCollateralPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isLiquidateCollateralPaused = isPaused;

        emit Events.IsLiquidateCollateralPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` liquidate borrow pause status as `isPaused` on Morpho.
    function setIsLiquidateBorrowPaused(Types.Market storage market, bool isPaused) internal {
        market.pauseStatuses.isLiquidateBorrowPaused = isPaused;

        emit Events.IsLiquidateBorrowPausedSet(market.underlying, isPaused);
    }

    /// @notice Sets the `market` as `deprecated` on Morpho.
    function setIsDeprecated(Types.Market storage market, bool deprecated) internal {
        market.pauseStatuses.isDeprecated = deprecated;

        emit Events.IsDeprecatedSet(market.underlying, deprecated);
    }

    /// @notice Sets the `market` peer-to-peer as `p2pDisabled` on Morpho.
    function setIsP2PDisabled(Types.Market storage market, bool p2pDisabled) internal {
        market.pauseStatuses.isP2PDisabled = p2pDisabled;

        emit Events.IsP2PDisabledSet(market.underlying, p2pDisabled);
    }

    /// @notice Sets the `market` peer-to-peer reserve factor to `reserveFactor`.
    function setReserveFactor(Types.Market storage market, uint16 reserveFactor) internal {
        if (reserveFactor > PercentageMath.PERCENTAGE_FACTOR) revert Errors.ExceedsMaxBasisPoints();
        market.reserveFactor = reserveFactor;

        emit Events.ReserveFactorSet(market.underlying, reserveFactor);
    }

    /// @notice Sets the `market` peer-to-peer index cursor to `p2pIndexCursor`.
    function setP2PIndexCursor(Types.Market storage market, uint16 p2pIndexCursor) internal {
        if (p2pIndexCursor > PercentageMath.PERCENTAGE_FACTOR) revert Errors.ExceedsMaxBasisPoints();
        market.p2pIndexCursor = p2pIndexCursor;

        emit Events.P2PIndexCursorSet(market.underlying, p2pIndexCursor);
    }

    /// @notice Sets the `market` indexes to `indexes`.
    function setIndexes(Types.Market storage market, Types.Indexes256 memory indexes) internal {
        market.indexes.supply.poolIndex = indexes.supply.poolIndex.toUint128();
        market.indexes.supply.p2pIndex = indexes.supply.p2pIndex.toUint128();
        market.indexes.borrow.poolIndex = indexes.borrow.poolIndex.toUint128();
        market.indexes.borrow.p2pIndex = indexes.borrow.p2pIndex.toUint128();
        market.lastUpdateTimestamp = uint32(block.timestamp);
        emit Events.IndexesUpdated(
            market.underlying,
            indexes.supply.poolIndex,
            indexes.supply.p2pIndex,
            indexes.borrow.poolIndex,
            indexes.borrow.p2pIndex
        );
    }

    /// @notice Returns the supply indexes of `market`.
    function getSupplyIndexes(Types.Market storage market)
        internal
        view
        returns (Types.MarketSideIndexes256 memory supplyIndexes)
    {
        supplyIndexes.poolIndex = uint256(market.indexes.supply.poolIndex);
        supplyIndexes.p2pIndex = uint256(market.indexes.supply.p2pIndex);
    }

    /// @notice Returns the borrow indexes of `market`.
    function getBorrowIndexes(Types.Market storage market)
        internal
        view
        returns (Types.MarketSideIndexes256 memory borrowIndexes)
    {
        borrowIndexes.poolIndex = uint256(market.indexes.borrow.poolIndex);
        borrowIndexes.p2pIndex = uint256(market.indexes.borrow.p2pIndex);
    }

    /// @notice Returns the indexes of `market`.
    function getIndexes(Types.Market storage market) internal view returns (Types.Indexes256 memory indexes) {
        indexes.supply = getSupplyIndexes(market);
        indexes.borrow = getBorrowIndexes(market);
    }

    /// @notice Returns the proportion of idle supply in `market` over the total peer-to-peer amount in supply.
    function getProportionIdle(Types.Market storage market) internal view returns (uint256) {
        uint256 idleSupply = market.idleSupply;
        if (idleSupply == 0) return 0;

        uint256 totalP2PSupplied = market.deltas.supply.scaledP2PTotal.rayMul(market.indexes.supply.p2pIndex);
        if (totalP2PSupplied == 0) return 0;

        // We take the minimum to handle the case where the proportion is rounded to greater than 1.
        return Math.min(idleSupply.rayDivUp(totalP2PSupplied), WadRayMath.RAY);
    }

    /// @notice Increases the idle supply if the supply cap is reached in a breaking repay, and returns a new toSupply amount.
    /// @param market The market storage.
    /// @param underlying The underlying address.
    /// @param amount The amount to repay. (by supplying on pool)
    /// @param reserve The reserve data for the market.
    /// @return The amount to supply to stay below the supply cap and the amount the idle supply was increased by.
    function increaseIdle(
        Types.Market storage market,
        address underlying,
        uint256 amount,
        DataTypes.ReserveData memory reserve,
        Types.Indexes256 memory indexes
    ) internal returns (uint256, uint256) {
        uint256 supplyCap = reserve.configuration.getSupplyCap() * (10 ** reserve.configuration.getDecimals());
        if (supplyCap == 0) return (amount, 0);

        uint256 suppliable = supplyCap.zeroFloorSub(
            (IAToken(market.aToken).scaledTotalSupply() + reserve.getAccruedToTreasury(indexes)).rayMul(
                indexes.supply.poolIndex
            )
        );
        if (amount <= suppliable) return (amount, 0);

        uint256 idleSupplyIncrease = amount - suppliable;
        uint256 newIdleSupply = market.idleSupply + idleSupplyIncrease;

        market.idleSupply = newIdleSupply;

        emit Events.IdleSupplyUpdated(underlying, newIdleSupply);

        return (suppliable, idleSupplyIncrease);
    }

    /// @notice Decreases the idle supply.
    /// @param market The market storage.
    /// @param underlying The underlying address.
    /// @param amount The amount to borrow.
    /// @return The amount left to process and the processed amount.
    function decreaseIdle(Types.Market storage market, address underlying, uint256 amount)
        internal
        returns (uint256, uint256)
    {
        if (amount == 0) return (0, 0);

        uint256 idleSupply = market.idleSupply;
        if (idleSupply == 0) return (amount, 0);

        uint256 matchedIdle = Math.min(idleSupply, amount); // In underlying.
        uint256 newIdleSupply = idleSupply.zeroFloorSub(matchedIdle);
        market.idleSupply = newIdleSupply;

        emit Events.IdleSupplyUpdated(underlying, newIdleSupply);

        return (amount - matchedIdle, matchedIdle);
    }

    /// @notice Calculates the total quantity of underlyings truly supplied peer-to-peer on the given market.
    /// @param indexes The current indexes.
    /// @return The total peer-to-peer supply (total peer-to-peer supply - supply delta - idle supply).
    function trueP2PSupply(Types.Market storage market, Types.Indexes256 memory indexes)
        internal
        view
        returns (uint256)
    {
        Types.MarketSideDelta storage supplyDelta = market.deltas.supply;
        return supplyDelta.scaledP2PTotal.rayMul(indexes.supply.p2pIndex).zeroFloorSub(
            supplyDelta.scaledDelta.rayMul(indexes.supply.poolIndex)
        ).zeroFloorSub(market.idleSupply);
    }

    /// @notice Calculates the total quantity of underlyings truly borrowed peer-to-peer on the given market.
    /// @param indexes The current indexes.
    /// @return The total peer-to-peer borrow (total peer-to-peer borrow - borrow delta).
    function trueP2PBorrow(Types.Market storage market, Types.Indexes256 memory indexes)
        internal
        view
        returns (uint256)
    {
        Types.MarketSideDelta storage borrowDelta = market.deltas.borrow;
        return borrowDelta.scaledP2PTotal.rayMul(indexes.borrow.p2pIndex).zeroFloorSub(
            borrowDelta.scaledDelta.rayMul(indexes.borrow.poolIndex)
        );
    }

    /// @notice Calculates & deducts the reserve fee to repay from the given amount, updating the total peer-to-peer amount.
    /// @param amount The amount to repay (in underlying).
    /// @param indexes The current indexes.
    /// @return The new amount left to process (in underlying).
    function repayFee(Types.Market storage market, uint256 amount, Types.Indexes256 memory indexes)
        internal
        returns (uint256)
    {
        if (amount == 0) return 0;

        Types.Deltas storage deltas = market.deltas;
        uint256 feeToRepay = Math.min(amount, market.trueP2PBorrow(indexes).zeroFloorSub(market.trueP2PSupply(indexes)));

        if (feeToRepay == 0) return amount;

        deltas.borrow.scaledP2PTotal =
            deltas.borrow.scaledP2PTotal.zeroFloorSub(feeToRepay.rayDivDown(indexes.borrow.p2pIndex)); // P2PTotalsUpdated emitted in `decreaseP2P`.

        return amount - feeToRepay;
    }
}

File 34 of 57 : DeltasLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {Types} from "./Types.sol";
import {Events} from "./Events.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";

/// @title DeltasLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library used to ease delta reads and writes.
library DeltasLib {
    using Math for uint256;
    using WadRayMath for uint256;

    /// @notice Increases the peer-to-peer amounts following a promotion.
    /// @param deltas The market deltas to update.
    /// @param underlying The underlying address.
    /// @param promoted The amount to increase the promoted side peer-to-peer total (in underlying). Must be lower than or equal to amount.
    /// @param amount The amount to increase the opposite side peer-to-peer total (in underlying).
    /// @param indexes The current indexes.
    /// @param borrowSide True if this follows borrower promotions. False for supplier promotions.
    /// @return p2pBalanceIncrease The scaled balance amount in peer-to-peer to increase.
    function increaseP2P(
        Types.Deltas storage deltas,
        address underlying,
        uint256 promoted,
        uint256 amount,
        Types.Indexes256 memory indexes,
        bool borrowSide
    ) internal returns (uint256 p2pBalanceIncrease) {
        if (amount == 0) return 0; // promoted == 0 is not checked since promoted <= amount.

        Types.MarketSideDelta storage promotedDelta = borrowSide ? deltas.borrow : deltas.supply;
        Types.MarketSideDelta storage counterDelta = borrowSide ? deltas.supply : deltas.borrow;
        Types.MarketSideIndexes256 memory promotedIndexes = borrowSide ? indexes.borrow : indexes.supply;
        Types.MarketSideIndexes256 memory counterIndexes = borrowSide ? indexes.supply : indexes.borrow;

        p2pBalanceIncrease = amount.rayDiv(counterIndexes.p2pIndex);
        promotedDelta.scaledP2PTotal += promoted.rayDiv(promotedIndexes.p2pIndex);
        counterDelta.scaledP2PTotal += p2pBalanceIncrease;

        emit Events.P2PTotalsUpdated(underlying, deltas.supply.scaledP2PTotal, deltas.borrow.scaledP2PTotal);
    }

    /// @notice Decreases the peer-to-peer amounts following a demotion.
    /// @param deltas The market deltas to update.
    /// @param underlying The underlying address.
    /// @param demoted The amount to decrease the demoted side peer-to-peer total (in underlying). Must be lower than or equal to amount.
    /// @param amount The amount to decrease the opposite side peer-to-peer total (in underlying).
    /// @param indexes The current indexes.
    /// @param borrowSide True if this follows borrower demotions. False for supplier demotions.
    function decreaseP2P(
        Types.Deltas storage deltas,
        address underlying,
        uint256 demoted,
        uint256 amount,
        Types.Indexes256 memory indexes,
        bool borrowSide
    ) internal {
        if (amount == 0) return; // demoted == 0 is not checked since demoted <= amount.

        Types.MarketSideDelta storage demotedDelta = borrowSide ? deltas.borrow : deltas.supply;
        Types.MarketSideDelta storage counterDelta = borrowSide ? deltas.supply : deltas.borrow;
        Types.MarketSideIndexes256 memory demotedIndexes = borrowSide ? indexes.borrow : indexes.supply;
        Types.MarketSideIndexes256 memory counterIndexes = borrowSide ? indexes.supply : indexes.borrow;

        demotedDelta.scaledP2PTotal = demotedDelta.scaledP2PTotal.zeroFloorSub(demoted.rayDiv(demotedIndexes.p2pIndex));
        counterDelta.scaledP2PTotal = counterDelta.scaledP2PTotal.zeroFloorSub(amount.rayDiv(counterIndexes.p2pIndex));

        emit Events.P2PTotalsUpdated(underlying, deltas.supply.scaledP2PTotal, deltas.borrow.scaledP2PTotal);
    }
}

File 35 of 57 : MarketSideDeltaLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {Types} from "./Types.sol";
import {Events} from "./Events.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";

/// @title MarketSideDeltaLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library used to ease increase or decrease deltas.
library MarketSideDeltaLib {
    using Math for uint256;
    using WadRayMath for uint256;

    /// @notice Given variables from a market side, updates the market side delta according to the demoted amount.
    /// @param delta The market deltas to update.
    /// @param underlying The underlying address.
    /// @param amount The amount to supply/borrow (in underlying).
    /// @param indexes The current indexes.
    /// @param borrowSide Whether the market side is borrow.
    function increaseDelta(
        Types.MarketSideDelta storage delta,
        address underlying,
        uint256 amount,
        Types.MarketSideIndexes256 memory indexes,
        bool borrowSide
    ) internal {
        if (amount == 0) return;

        uint256 newScaledDelta = delta.scaledDelta + amount.rayDiv(indexes.poolIndex);

        delta.scaledDelta = newScaledDelta;

        if (borrowSide) emit Events.P2PBorrowDeltaUpdated(underlying, newScaledDelta);
        else emit Events.P2PSupplyDeltaUpdated(underlying, newScaledDelta);
    }

    /// @notice Given variables from a market side, matches the delta and calculates the amount to supply/borrow from delta.
    ///         Updates the market side delta accordingly.
    /// @param delta The market deltas to update.
    /// @param underlying The underlying address.
    /// @param amount The amount to supply/borrow (in underlying).
    /// @param poolIndex The current pool index.
    /// @param borrowSide Whether the market side is borrow.
    /// @return The amount left to process (in underlying) and the amount to repay/withdraw (in underlying).
    function decreaseDelta(
        Types.MarketSideDelta storage delta,
        address underlying,
        uint256 amount,
        uint256 poolIndex,
        bool borrowSide
    ) internal returns (uint256, uint256) {
        if (amount == 0) return (0, 0);

        uint256 scaledDelta = delta.scaledDelta;
        if (scaledDelta == 0) return (amount, 0);

        uint256 decreased = Math.min(scaledDelta.rayMulUp(poolIndex), amount); // In underlying.
        uint256 newScaledDelta = scaledDelta.zeroFloorSub(decreased.rayDivDown(poolIndex));

        delta.scaledDelta = newScaledDelta;

        if (borrowSide) emit Events.P2PBorrowDeltaUpdated(underlying, newScaledDelta);
        else emit Events.P2PSupplyDeltaUpdated(underlying, newScaledDelta);

        return (amount - decreased, decreased);
    }
}

File 36 of 57 : UserConfiguration.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {Errors} from '../helpers/Errors.sol';
import {DataTypes} from '../types/DataTypes.sol';
import {ReserveConfiguration} from './ReserveConfiguration.sol';

/**
 * @title UserConfiguration library
 * @author Aave
 * @notice Implements the bitmap logic to handle the user configuration
 */
library UserConfiguration {
  using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

  uint256 internal constant BORROWING_MASK =
    0x5555555555555555555555555555555555555555555555555555555555555555;
  uint256 internal constant COLLATERAL_MASK =
    0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;

  /**
   * @notice Sets if the user is borrowing the reserve identified by reserveIndex
   * @param self The configuration object
   * @param reserveIndex The index of the reserve in the bitmap
   * @param borrowing True if the user is borrowing the reserve, false otherwise
   */
  function setBorrowing(
    DataTypes.UserConfigurationMap storage self,
    uint256 reserveIndex,
    bool borrowing
  ) internal {
    unchecked {
      require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.INVALID_RESERVE_INDEX);
      uint256 bit = 1 << (reserveIndex << 1);
      if (borrowing) {
        self.data |= bit;
      } else {
        self.data &= ~bit;
      }
    }
  }

  /**
   * @notice Sets if the user is using as collateral the reserve identified by reserveIndex
   * @param self The configuration object
   * @param reserveIndex The index of the reserve in the bitmap
   * @param usingAsCollateral True if the user is using the reserve as collateral, false otherwise
   */
  function setUsingAsCollateral(
    DataTypes.UserConfigurationMap storage self,
    uint256 reserveIndex,
    bool usingAsCollateral
  ) internal {
    unchecked {
      require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.INVALID_RESERVE_INDEX);
      uint256 bit = 1 << ((reserveIndex << 1) + 1);
      if (usingAsCollateral) {
        self.data |= bit;
      } else {
        self.data &= ~bit;
      }
    }
  }

  /**
   * @notice Returns if a user has been using the reserve for borrowing or as collateral
   * @param self The configuration object
   * @param reserveIndex The index of the reserve in the bitmap
   * @return True if the user has been using a reserve for borrowing or as collateral, false otherwise
   */
  function isUsingAsCollateralOrBorrowing(
    DataTypes.UserConfigurationMap memory self,
    uint256 reserveIndex
  ) internal pure returns (bool) {
    unchecked {
      require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.INVALID_RESERVE_INDEX);
      return (self.data >> (reserveIndex << 1)) & 3 != 0;
    }
  }

  /**
   * @notice Validate a user has been using the reserve for borrowing
   * @param self The configuration object
   * @param reserveIndex The index of the reserve in the bitmap
   * @return True if the user has been using a reserve for borrowing, false otherwise
   */
  function isBorrowing(DataTypes.UserConfigurationMap memory self, uint256 reserveIndex)
    internal
    pure
    returns (bool)
  {
    unchecked {
      require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.INVALID_RESERVE_INDEX);
      return (self.data >> (reserveIndex << 1)) & 1 != 0;
    }
  }

  /**
   * @notice Validate a user has been using the reserve as collateral
   * @param self The configuration object
   * @param reserveIndex The index of the reserve in the bitmap
   * @return True if the user has been using a reserve as collateral, false otherwise
   */
  function isUsingAsCollateral(DataTypes.UserConfigurationMap memory self, uint256 reserveIndex)
    internal
    pure
    returns (bool)
  {
    unchecked {
      require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.INVALID_RESERVE_INDEX);
      return (self.data >> ((reserveIndex << 1) + 1)) & 1 != 0;
    }
  }

  /**
   * @notice Checks if a user has been supplying only one reserve as collateral
   * @dev this uses a simple trick - if a number is a power of two (only one bit set) then n & (n - 1) == 0
   * @param self The configuration object
   * @return True if the user has been supplying as collateral one reserve, false otherwise
   */
  function isUsingAsCollateralOne(DataTypes.UserConfigurationMap memory self)
    internal
    pure
    returns (bool)
  {
    uint256 collateralData = self.data & COLLATERAL_MASK;
    return collateralData != 0 && (collateralData & (collateralData - 1) == 0);
  }

  /**
   * @notice Checks if a user has been supplying any reserve as collateral
   * @param self The configuration object
   * @return True if the user has been supplying as collateral any reserve, false otherwise
   */
  function isUsingAsCollateralAny(DataTypes.UserConfigurationMap memory self)
    internal
    pure
    returns (bool)
  {
    return self.data & COLLATERAL_MASK != 0;
  }

  /**
   * @notice Checks if a user has been borrowing only one asset
   * @dev this uses a simple trick - if a number is a power of two (only one bit set) then n & (n - 1) == 0
   * @param self The configuration object
   * @return True if the user has been supplying as collateral one reserve, false otherwise
   */
  function isBorrowingOne(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) {
    uint256 borrowingData = self.data & BORROWING_MASK;
    return borrowingData != 0 && (borrowingData & (borrowingData - 1) == 0);
  }

  /**
   * @notice Checks if a user has been borrowing from any reserve
   * @param self The configuration object
   * @return True if the user has been borrowing any reserve, false otherwise
   */
  function isBorrowingAny(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) {
    return self.data & BORROWING_MASK != 0;
  }

  /**
   * @notice Checks if a user has not been using any reserve for borrowing or supply
   * @param self The configuration object
   * @return True if the user has not been borrowing or supplying any reserve, false otherwise
   */
  function isEmpty(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) {
    return self.data == 0;
  }

  /**
   * @notice Returns the Isolation Mode state of the user
   * @param self The configuration object
   * @param reservesData The state of all the reserves
   * @param reservesList The addresses of all the active reserves
   * @return True if the user is in isolation mode, false otherwise
   * @return The address of the only asset used as collateral
   * @return The debt ceiling of the reserve
   */
  function getIsolationModeState(
    DataTypes.UserConfigurationMap memory self,
    mapping(address => DataTypes.ReserveData) storage reservesData,
    mapping(uint256 => address) storage reservesList
  )
    internal
    view
    returns (
      bool,
      address,
      uint256
    )
  {
    if (isUsingAsCollateralOne(self)) {
      uint256 assetId = _getFirstAssetIdByMask(self, COLLATERAL_MASK);

      address assetAddress = reservesList[assetId];
      uint256 ceiling = reservesData[assetAddress].configuration.getDebtCeiling();
      if (ceiling != 0) {
        return (true, assetAddress, ceiling);
      }
    }
    return (false, address(0), 0);
  }

  /**
   * @notice Returns the siloed borrowing state for the user
   * @param self The configuration object
   * @param reservesData The data of all the reserves
   * @param reservesList The reserve list
   * @return True if the user has borrowed a siloed asset, false otherwise
   * @return The address of the only borrowed asset
   */
  function getSiloedBorrowingState(
    DataTypes.UserConfigurationMap memory self,
    mapping(address => DataTypes.ReserveData) storage reservesData,
    mapping(uint256 => address) storage reservesList
  ) internal view returns (bool, address) {
    if (isBorrowingOne(self)) {
      uint256 assetId = _getFirstAssetIdByMask(self, BORROWING_MASK);
      address assetAddress = reservesList[assetId];
      if (reservesData[assetAddress].configuration.getSiloedBorrowing()) {
        return (true, assetAddress);
      }
    }

    return (false, address(0));
  }

  /**
   * @notice Returns the address of the first asset flagged in the bitmap given the corresponding bitmask
   * @param self The configuration object
   * @return The index of the first asset flagged in the bitmap once the corresponding mask is applied
   */
  function _getFirstAssetIdByMask(DataTypes.UserConfigurationMap memory self, uint256 mask)
    internal
    pure
    returns (uint256)
  {
    unchecked {
      uint256 bitmapData = self.data & mask;
      uint256 firstAssetPosition = bitmapData & ~(bitmapData - 1);
      uint256 id;

      while ((firstAssetPosition >>= 2) != 0) {
        id += 1;
      }
      return id;
    }
  }
}

File 37 of 57 : ReserveConfiguration.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {Errors} from '../helpers/Errors.sol';
import {DataTypes} from '../types/DataTypes.sol';

/**
 * @title ReserveConfiguration library
 * @author Aave
 * @notice Implements the bitmap logic to handle the reserve configuration
 */
library ReserveConfiguration {
  uint256 internal constant LTV_MASK =                       0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000; // prettier-ignore
  uint256 internal constant LIQUIDATION_THRESHOLD_MASK =     0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFF; // prettier-ignore
  uint256 internal constant LIQUIDATION_BONUS_MASK =         0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFFFFFF; // prettier-ignore
  uint256 internal constant DECIMALS_MASK =                  0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant ACTIVE_MASK =                    0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant FROZEN_MASK =                    0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant BORROWING_MASK =                 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant STABLE_BORROWING_MASK =          0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant PAUSED_MASK =                    0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant BORROWABLE_IN_ISOLATION_MASK =   0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant SILOED_BORROWING_MASK =          0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant FLASHLOAN_ENABLED_MASK =         0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant RESERVE_FACTOR_MASK =            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant BORROW_CAP_MASK =                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000FFFFFFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant SUPPLY_CAP_MASK =                0xFFFFFFFFFFFFFFFFFFFFFFFFFF000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant LIQUIDATION_PROTOCOL_FEE_MASK =  0xFFFFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant EMODE_CATEGORY_MASK =            0xFFFFFFFFFFFFFFFFFFFF00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant UNBACKED_MINT_CAP_MASK =         0xFFFFFFFFFFF000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; // prettier-ignore
  uint256 internal constant DEBT_CEILING_MASK =              0xF0000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; // prettier-ignore

  /// @dev For the LTV, the start bit is 0 (up to 15), hence no bitshifting is needed
  uint256 internal constant LIQUIDATION_THRESHOLD_START_BIT_POSITION = 16;
  uint256 internal constant LIQUIDATION_BONUS_START_BIT_POSITION = 32;
  uint256 internal constant RESERVE_DECIMALS_START_BIT_POSITION = 48;
  uint256 internal constant IS_ACTIVE_START_BIT_POSITION = 56;
  uint256 internal constant IS_FROZEN_START_BIT_POSITION = 57;
  uint256 internal constant BORROWING_ENABLED_START_BIT_POSITION = 58;
  uint256 internal constant STABLE_BORROWING_ENABLED_START_BIT_POSITION = 59;
  uint256 internal constant IS_PAUSED_START_BIT_POSITION = 60;
  uint256 internal constant BORROWABLE_IN_ISOLATION_START_BIT_POSITION = 61;
  uint256 internal constant SILOED_BORROWING_START_BIT_POSITION = 62;
  uint256 internal constant FLASHLOAN_ENABLED_START_BIT_POSITION = 63;
  uint256 internal constant RESERVE_FACTOR_START_BIT_POSITION = 64;
  uint256 internal constant BORROW_CAP_START_BIT_POSITION = 80;
  uint256 internal constant SUPPLY_CAP_START_BIT_POSITION = 116;
  uint256 internal constant LIQUIDATION_PROTOCOL_FEE_START_BIT_POSITION = 152;
  uint256 internal constant EMODE_CATEGORY_START_BIT_POSITION = 168;
  uint256 internal constant UNBACKED_MINT_CAP_START_BIT_POSITION = 176;
  uint256 internal constant DEBT_CEILING_START_BIT_POSITION = 212;

  uint256 internal constant MAX_VALID_LTV = 65535;
  uint256 internal constant MAX_VALID_LIQUIDATION_THRESHOLD = 65535;
  uint256 internal constant MAX_VALID_LIQUIDATION_BONUS = 65535;
  uint256 internal constant MAX_VALID_DECIMALS = 255;
  uint256 internal constant MAX_VALID_RESERVE_FACTOR = 65535;
  uint256 internal constant MAX_VALID_BORROW_CAP = 68719476735;
  uint256 internal constant MAX_VALID_SUPPLY_CAP = 68719476735;
  uint256 internal constant MAX_VALID_LIQUIDATION_PROTOCOL_FEE = 65535;
  uint256 internal constant MAX_VALID_EMODE_CATEGORY = 255;
  uint256 internal constant MAX_VALID_UNBACKED_MINT_CAP = 68719476735;
  uint256 internal constant MAX_VALID_DEBT_CEILING = 1099511627775;

  uint256 public constant DEBT_CEILING_DECIMALS = 2;
  uint16 public constant MAX_RESERVES_COUNT = 128;

  /**
   * @notice Sets the Loan to Value of the reserve
   * @param self The reserve configuration
   * @param ltv The new ltv
   */
  function setLtv(DataTypes.ReserveConfigurationMap memory self, uint256 ltv) internal pure {
    require(ltv <= MAX_VALID_LTV, Errors.INVALID_LTV);

    self.data = (self.data & LTV_MASK) | ltv;
  }

  /**
   * @notice Gets the Loan to Value of the reserve
   * @param self The reserve configuration
   * @return The loan to value
   */
  function getLtv(DataTypes.ReserveConfigurationMap memory self) internal pure returns (uint256) {
    return self.data & ~LTV_MASK;
  }

  /**
   * @notice Sets the liquidation threshold of the reserve
   * @param self The reserve configuration
   * @param threshold The new liquidation threshold
   */
  function setLiquidationThreshold(DataTypes.ReserveConfigurationMap memory self, uint256 threshold)
    internal
    pure
  {
    require(threshold <= MAX_VALID_LIQUIDATION_THRESHOLD, Errors.INVALID_LIQ_THRESHOLD);

    self.data =
      (self.data & LIQUIDATION_THRESHOLD_MASK) |
      (threshold << LIQUIDATION_THRESHOLD_START_BIT_POSITION);
  }

  /**
   * @notice Gets the liquidation threshold of the reserve
   * @param self The reserve configuration
   * @return The liquidation threshold
   */
  function getLiquidationThreshold(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~LIQUIDATION_THRESHOLD_MASK) >> LIQUIDATION_THRESHOLD_START_BIT_POSITION;
  }

  /**
   * @notice Sets the liquidation bonus of the reserve
   * @param self The reserve configuration
   * @param bonus The new liquidation bonus
   */
  function setLiquidationBonus(DataTypes.ReserveConfigurationMap memory self, uint256 bonus)
    internal
    pure
  {
    require(bonus <= MAX_VALID_LIQUIDATION_BONUS, Errors.INVALID_LIQ_BONUS);

    self.data =
      (self.data & LIQUIDATION_BONUS_MASK) |
      (bonus << LIQUIDATION_BONUS_START_BIT_POSITION);
  }

  /**
   * @notice Gets the liquidation bonus of the reserve
   * @param self The reserve configuration
   * @return The liquidation bonus
   */
  function getLiquidationBonus(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~LIQUIDATION_BONUS_MASK) >> LIQUIDATION_BONUS_START_BIT_POSITION;
  }

  /**
   * @notice Sets the decimals of the underlying asset of the reserve
   * @param self The reserve configuration
   * @param decimals The decimals
   */
  function setDecimals(DataTypes.ReserveConfigurationMap memory self, uint256 decimals)
    internal
    pure
  {
    require(decimals <= MAX_VALID_DECIMALS, Errors.INVALID_DECIMALS);

    self.data = (self.data & DECIMALS_MASK) | (decimals << RESERVE_DECIMALS_START_BIT_POSITION);
  }

  /**
   * @notice Gets the decimals of the underlying asset of the reserve
   * @param self The reserve configuration
   * @return The decimals of the asset
   */
  function getDecimals(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~DECIMALS_MASK) >> RESERVE_DECIMALS_START_BIT_POSITION;
  }

  /**
   * @notice Sets the active state of the reserve
   * @param self The reserve configuration
   * @param active The active state
   */
  function setActive(DataTypes.ReserveConfigurationMap memory self, bool active) internal pure {
    self.data =
      (self.data & ACTIVE_MASK) |
      (uint256(active ? 1 : 0) << IS_ACTIVE_START_BIT_POSITION);
  }

  /**
   * @notice Gets the active state of the reserve
   * @param self The reserve configuration
   * @return The active state
   */
  function getActive(DataTypes.ReserveConfigurationMap memory self) internal pure returns (bool) {
    return (self.data & ~ACTIVE_MASK) != 0;
  }

  /**
   * @notice Sets the frozen state of the reserve
   * @param self The reserve configuration
   * @param frozen The frozen state
   */
  function setFrozen(DataTypes.ReserveConfigurationMap memory self, bool frozen) internal pure {
    self.data =
      (self.data & FROZEN_MASK) |
      (uint256(frozen ? 1 : 0) << IS_FROZEN_START_BIT_POSITION);
  }

  /**
   * @notice Gets the frozen state of the reserve
   * @param self The reserve configuration
   * @return The frozen state
   */
  function getFrozen(DataTypes.ReserveConfigurationMap memory self) internal pure returns (bool) {
    return (self.data & ~FROZEN_MASK) != 0;
  }

  /**
   * @notice Sets the paused state of the reserve
   * @param self The reserve configuration
   * @param paused The paused state
   */
  function setPaused(DataTypes.ReserveConfigurationMap memory self, bool paused) internal pure {
    self.data =
      (self.data & PAUSED_MASK) |
      (uint256(paused ? 1 : 0) << IS_PAUSED_START_BIT_POSITION);
  }

  /**
   * @notice Gets the paused state of the reserve
   * @param self The reserve configuration
   * @return The paused state
   */
  function getPaused(DataTypes.ReserveConfigurationMap memory self) internal pure returns (bool) {
    return (self.data & ~PAUSED_MASK) != 0;
  }

  /**
   * @notice Sets the borrowable in isolation flag for the reserve.
   * @dev When this flag is set to true, the asset will be borrowable against isolated collaterals and the borrowed
   * amount will be accumulated in the isolated collateral's total debt exposure.
   * @dev Only assets of the same family (eg USD stablecoins) should be borrowable in isolation mode to keep
   * consistency in the debt ceiling calculations.
   * @param self The reserve configuration
   * @param borrowable True if the asset is borrowable
   */
  function setBorrowableInIsolation(DataTypes.ReserveConfigurationMap memory self, bool borrowable)
    internal
    pure
  {
    self.data =
      (self.data & BORROWABLE_IN_ISOLATION_MASK) |
      (uint256(borrowable ? 1 : 0) << BORROWABLE_IN_ISOLATION_START_BIT_POSITION);
  }

  /**
   * @notice Gets the borrowable in isolation flag for the reserve.
   * @dev If the returned flag is true, the asset is borrowable against isolated collateral. Assets borrowed with
   * isolated collateral is accounted for in the isolated collateral's total debt exposure.
   * @dev Only assets of the same family (eg USD stablecoins) should be borrowable in isolation mode to keep
   * consistency in the debt ceiling calculations.
   * @param self The reserve configuration
   * @return The borrowable in isolation flag
   */
  function getBorrowableInIsolation(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (bool)
  {
    return (self.data & ~BORROWABLE_IN_ISOLATION_MASK) != 0;
  }

  /**
   * @notice Sets the siloed borrowing flag for the reserve.
   * @dev When this flag is set to true, users borrowing this asset will not be allowed to borrow any other asset.
   * @param self The reserve configuration
   * @param siloed True if the asset is siloed
   */
  function setSiloedBorrowing(DataTypes.ReserveConfigurationMap memory self, bool siloed)
    internal
    pure
  {
    self.data =
      (self.data & SILOED_BORROWING_MASK) |
      (uint256(siloed ? 1 : 0) << SILOED_BORROWING_START_BIT_POSITION);
  }

  /**
   * @notice Gets the siloed borrowing flag for the reserve.
   * @dev When this flag is set to true, users borrowing this asset will not be allowed to borrow any other asset.
   * @param self The reserve configuration
   * @return The siloed borrowing flag
   */
  function getSiloedBorrowing(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (bool)
  {
    return (self.data & ~SILOED_BORROWING_MASK) != 0;
  }

  /**
   * @notice Enables or disables borrowing on the reserve
   * @param self The reserve configuration
   * @param enabled True if the borrowing needs to be enabled, false otherwise
   */
  function setBorrowingEnabled(DataTypes.ReserveConfigurationMap memory self, bool enabled)
    internal
    pure
  {
    self.data =
      (self.data & BORROWING_MASK) |
      (uint256(enabled ? 1 : 0) << BORROWING_ENABLED_START_BIT_POSITION);
  }

  /**
   * @notice Gets the borrowing state of the reserve
   * @param self The reserve configuration
   * @return The borrowing state
   */
  function getBorrowingEnabled(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (bool)
  {
    return (self.data & ~BORROWING_MASK) != 0;
  }

  /**
   * @notice Enables or disables stable rate borrowing on the reserve
   * @param self The reserve configuration
   * @param enabled True if the stable rate borrowing needs to be enabled, false otherwise
   */
  function setStableRateBorrowingEnabled(
    DataTypes.ReserveConfigurationMap memory self,
    bool enabled
  ) internal pure {
    self.data =
      (self.data & STABLE_BORROWING_MASK) |
      (uint256(enabled ? 1 : 0) << STABLE_BORROWING_ENABLED_START_BIT_POSITION);
  }

  /**
   * @notice Gets the stable rate borrowing state of the reserve
   * @param self The reserve configuration
   * @return The stable rate borrowing state
   */
  function getStableRateBorrowingEnabled(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (bool)
  {
    return (self.data & ~STABLE_BORROWING_MASK) != 0;
  }

  /**
   * @notice Sets the reserve factor of the reserve
   * @param self The reserve configuration
   * @param reserveFactor The reserve factor
   */
  function setReserveFactor(DataTypes.ReserveConfigurationMap memory self, uint256 reserveFactor)
    internal
    pure
  {
    require(reserveFactor <= MAX_VALID_RESERVE_FACTOR, Errors.INVALID_RESERVE_FACTOR);

    self.data =
      (self.data & RESERVE_FACTOR_MASK) |
      (reserveFactor << RESERVE_FACTOR_START_BIT_POSITION);
  }

  /**
   * @notice Gets the reserve factor of the reserve
   * @param self The reserve configuration
   * @return The reserve factor
   */
  function getReserveFactor(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~RESERVE_FACTOR_MASK) >> RESERVE_FACTOR_START_BIT_POSITION;
  }

  /**
   * @notice Sets the borrow cap of the reserve
   * @param self The reserve configuration
   * @param borrowCap The borrow cap
   */
  function setBorrowCap(DataTypes.ReserveConfigurationMap memory self, uint256 borrowCap)
    internal
    pure
  {
    require(borrowCap <= MAX_VALID_BORROW_CAP, Errors.INVALID_BORROW_CAP);

    self.data = (self.data & BORROW_CAP_MASK) | (borrowCap << BORROW_CAP_START_BIT_POSITION);
  }

  /**
   * @notice Gets the borrow cap of the reserve
   * @param self The reserve configuration
   * @return The borrow cap
   */
  function getBorrowCap(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~BORROW_CAP_MASK) >> BORROW_CAP_START_BIT_POSITION;
  }

  /**
   * @notice Sets the supply cap of the reserve
   * @param self The reserve configuration
   * @param supplyCap The supply cap
   */
  function setSupplyCap(DataTypes.ReserveConfigurationMap memory self, uint256 supplyCap)
    internal
    pure
  {
    require(supplyCap <= MAX_VALID_SUPPLY_CAP, Errors.INVALID_SUPPLY_CAP);

    self.data = (self.data & SUPPLY_CAP_MASK) | (supplyCap << SUPPLY_CAP_START_BIT_POSITION);
  }

  /**
   * @notice Gets the supply cap of the reserve
   * @param self The reserve configuration
   * @return The supply cap
   */
  function getSupplyCap(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~SUPPLY_CAP_MASK) >> SUPPLY_CAP_START_BIT_POSITION;
  }

  /**
   * @notice Sets the debt ceiling in isolation mode for the asset
   * @param self The reserve configuration
   * @param ceiling The maximum debt ceiling for the asset
   */
  function setDebtCeiling(DataTypes.ReserveConfigurationMap memory self, uint256 ceiling)
    internal
    pure
  {
    require(ceiling <= MAX_VALID_DEBT_CEILING, Errors.INVALID_DEBT_CEILING);

    self.data = (self.data & DEBT_CEILING_MASK) | (ceiling << DEBT_CEILING_START_BIT_POSITION);
  }

  /**
   * @notice Gets the debt ceiling for the asset if the asset is in isolation mode
   * @param self The reserve configuration
   * @return The debt ceiling (0 = isolation mode disabled)
   */
  function getDebtCeiling(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~DEBT_CEILING_MASK) >> DEBT_CEILING_START_BIT_POSITION;
  }

  /**
   * @notice Sets the liquidation protocol fee of the reserve
   * @param self The reserve configuration
   * @param liquidationProtocolFee The liquidation protocol fee
   */
  function setLiquidationProtocolFee(
    DataTypes.ReserveConfigurationMap memory self,
    uint256 liquidationProtocolFee
  ) internal pure {
    require(
      liquidationProtocolFee <= MAX_VALID_LIQUIDATION_PROTOCOL_FEE,
      Errors.INVALID_LIQUIDATION_PROTOCOL_FEE
    );

    self.data =
      (self.data & LIQUIDATION_PROTOCOL_FEE_MASK) |
      (liquidationProtocolFee << LIQUIDATION_PROTOCOL_FEE_START_BIT_POSITION);
  }

  /**
   * @dev Gets the liquidation protocol fee
   * @param self The reserve configuration
   * @return The liquidation protocol fee
   */
  function getLiquidationProtocolFee(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return
      (self.data & ~LIQUIDATION_PROTOCOL_FEE_MASK) >> LIQUIDATION_PROTOCOL_FEE_START_BIT_POSITION;
  }

  /**
   * @notice Sets the unbacked mint cap of the reserve
   * @param self The reserve configuration
   * @param unbackedMintCap The unbacked mint cap
   */
  function setUnbackedMintCap(
    DataTypes.ReserveConfigurationMap memory self,
    uint256 unbackedMintCap
  ) internal pure {
    require(unbackedMintCap <= MAX_VALID_UNBACKED_MINT_CAP, Errors.INVALID_UNBACKED_MINT_CAP);

    self.data =
      (self.data & UNBACKED_MINT_CAP_MASK) |
      (unbackedMintCap << UNBACKED_MINT_CAP_START_BIT_POSITION);
  }

  /**
   * @dev Gets the unbacked mint cap of the reserve
   * @param self The reserve configuration
   * @return The unbacked mint cap
   */
  function getUnbackedMintCap(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~UNBACKED_MINT_CAP_MASK) >> UNBACKED_MINT_CAP_START_BIT_POSITION;
  }

  /**
   * @notice Sets the eMode asset category
   * @param self The reserve configuration
   * @param category The asset category when the user selects the eMode
   */
  function setEModeCategory(DataTypes.ReserveConfigurationMap memory self, uint256 category)
    internal
    pure
  {
    require(category <= MAX_VALID_EMODE_CATEGORY, Errors.INVALID_EMODE_CATEGORY);

    self.data = (self.data & EMODE_CATEGORY_MASK) | (category << EMODE_CATEGORY_START_BIT_POSITION);
  }

  /**
   * @dev Gets the eMode asset category
   * @param self The reserve configuration
   * @return The eMode category for the asset
   */
  function getEModeCategory(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256)
  {
    return (self.data & ~EMODE_CATEGORY_MASK) >> EMODE_CATEGORY_START_BIT_POSITION;
  }

  /**
   * @notice Sets the flashloanable flag for the reserve
   * @param self The reserve configuration
   * @param flashLoanEnabled True if the asset is flashloanable, false otherwise
   */
  function setFlashLoanEnabled(DataTypes.ReserveConfigurationMap memory self, bool flashLoanEnabled)
    internal
    pure
  {
    self.data =
      (self.data & FLASHLOAN_ENABLED_MASK) |
      (uint256(flashLoanEnabled ? 1 : 0) << FLASHLOAN_ENABLED_START_BIT_POSITION);
  }

  /**
   * @notice Gets the flashloanable flag for the reserve
   * @param self The reserve configuration
   * @return The flashloanable flag
   */
  function getFlashLoanEnabled(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (bool)
  {
    return (self.data & ~FLASHLOAN_ENABLED_MASK) != 0;
  }

  /**
   * @notice Gets the configuration flags of the reserve
   * @param self The reserve configuration
   * @return The state flag representing active
   * @return The state flag representing frozen
   * @return The state flag representing borrowing enabled
   * @return The state flag representing stableRateBorrowing enabled
   * @return The state flag representing paused
   */
  function getFlags(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (
      bool,
      bool,
      bool,
      bool,
      bool
    )
  {
    uint256 dataLocal = self.data;

    return (
      (dataLocal & ~ACTIVE_MASK) != 0,
      (dataLocal & ~FROZEN_MASK) != 0,
      (dataLocal & ~BORROWING_MASK) != 0,
      (dataLocal & ~STABLE_BORROWING_MASK) != 0,
      (dataLocal & ~PAUSED_MASK) != 0
    );
  }

  /**
   * @notice Gets the configuration parameters of the reserve from storage
   * @param self The reserve configuration
   * @return The state param representing ltv
   * @return The state param representing liquidation threshold
   * @return The state param representing liquidation bonus
   * @return The state param representing reserve decimals
   * @return The state param representing reserve factor
   * @return The state param representing eMode category
   */
  function getParams(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (
      uint256,
      uint256,
      uint256,
      uint256,
      uint256,
      uint256
    )
  {
    uint256 dataLocal = self.data;

    return (
      dataLocal & ~LTV_MASK,
      (dataLocal & ~LIQUIDATION_THRESHOLD_MASK) >> LIQUIDATION_THRESHOLD_START_BIT_POSITION,
      (dataLocal & ~LIQUIDATION_BONUS_MASK) >> LIQUIDATION_BONUS_START_BIT_POSITION,
      (dataLocal & ~DECIMALS_MASK) >> RESERVE_DECIMALS_START_BIT_POSITION,
      (dataLocal & ~RESERVE_FACTOR_MASK) >> RESERVE_FACTOR_START_BIT_POSITION,
      (dataLocal & ~EMODE_CATEGORY_MASK) >> EMODE_CATEGORY_START_BIT_POSITION
    );
  }

  /**
   * @notice Gets the caps parameters of the reserve from storage
   * @param self The reserve configuration
   * @return The state param representing borrow cap
   * @return The state param representing supply cap.
   */
  function getCaps(DataTypes.ReserveConfigurationMap memory self)
    internal
    pure
    returns (uint256, uint256)
  {
    uint256 dataLocal = self.data;

    return (
      (dataLocal & ~BORROW_CAP_MASK) >> BORROW_CAP_START_BIT_POSITION,
      (dataLocal & ~SUPPLY_CAP_MASK) >> SUPPLY_CAP_START_BIT_POSITION
    );
  }
}

File 38 of 57 : MatchingEngine.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {Types} from "./libraries/Types.sol";
import {Events} from "./libraries/Events.sol";

import {MarketLib} from "./libraries/MarketLib.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";

import {LogarithmicBuckets} from "@morpho-data-structures/LogarithmicBuckets.sol";

import {MorphoInternal} from "./MorphoInternal.sol";

/// @title MatchingEngine
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Abstract contract with functions to promote or demote users to/from peer-to-peer.
abstract contract MatchingEngine is MorphoInternal {
    using MarketLib for Types.Market;

    using Math for uint256;
    using WadRayMath for uint256;

    using LogarithmicBuckets for LogarithmicBuckets.Buckets;

    /// @dev Promotes suppliers on the `underlying` market.
    /// @param underlying The address of the underlying market on which to promote suppliers.
    /// @param amount The amount of `underlying` to promote.
    /// @param maxIterations The maximum number of iterations allowed during the matching process.
    function _promoteSuppliers(address underlying, uint256 amount, uint256 maxIterations)
        internal
        returns (uint256, uint256)
    {
        return _promoteOrDemote(
            _marketBalances[underlying].poolSuppliers,
            _marketBalances[underlying].p2pSuppliers,
            Types.MatchingEngineVars({
                underlying: underlying,
                indexes: _market[underlying].getSupplyIndexes(),
                amount: amount,
                maxIterations: maxIterations,
                borrow: false,
                updateDS: _updateSupplierInDS,
                demoting: false,
                step: _promote
            })
        );
    }

    /// @dev Promotes borrowers on the `underlying` market.
    /// @param underlying The address of the underlying market on which to promote borrowers.
    /// @param amount The amount of `underlying` to promote.
    /// @param maxIterations The maximum number of iterations allowed during the matching process.
    function _promoteBorrowers(address underlying, uint256 amount, uint256 maxIterations)
        internal
        returns (uint256, uint256)
    {
        return _promoteOrDemote(
            _marketBalances[underlying].poolBorrowers,
            _marketBalances[underlying].p2pBorrowers,
            Types.MatchingEngineVars({
                underlying: underlying,
                indexes: _market[underlying].getBorrowIndexes(),
                amount: amount,
                maxIterations: maxIterations,
                borrow: true,
                updateDS: _updateBorrowerInDS,
                demoting: false,
                step: _promote
            })
        );
    }

    /// @dev Demotes suppliers on the `underlying` market.
    /// @param underlying The address of the underlying market on which to demote suppliers.
    /// @param amount The amount of `underlying` to demote.
    /// @param maxIterations The maximum number of iterations allowed during the matching process.
    function _demoteSuppliers(address underlying, uint256 amount, uint256 maxIterations)
        internal
        returns (uint256 demoted)
    {
        (demoted,) = _promoteOrDemote(
            _marketBalances[underlying].poolSuppliers,
            _marketBalances[underlying].p2pSuppliers,
            Types.MatchingEngineVars({
                underlying: underlying,
                indexes: _market[underlying].getSupplyIndexes(),
                amount: amount,
                maxIterations: maxIterations,
                borrow: false,
                updateDS: _updateSupplierInDS,
                demoting: true,
                step: _demote
            })
        );
    }

    /// @dev Demotes borrowers on the `underlying` market.
    /// @param underlying The address of the underlying market on which to demote borrowers.
    /// @param amount The amount of `underlying` to demote.
    /// @param maxIterations The maximum number of iterations allowed during the matching process.
    function _demoteBorrowers(address underlying, uint256 amount, uint256 maxIterations)
        internal
        returns (uint256 demoted)
    {
        (demoted,) = _promoteOrDemote(
            _marketBalances[underlying].poolBorrowers,
            _marketBalances[underlying].p2pBorrowers,
            Types.MatchingEngineVars({
                underlying: underlying,
                indexes: _market[underlying].getBorrowIndexes(),
                amount: amount,
                maxIterations: maxIterations,
                borrow: true,
                updateDS: _updateBorrowerInDS,
                demoting: true,
                step: _demote
            })
        );
    }

    /// @dev Promotes or demotes users.
    /// @param poolBuckets The pool buckets.
    /// @param poolBuckets The peer-to-peer buckets.
    /// @param vars The required matching engine variables to perform the matching process.
    function _promoteOrDemote(
        LogarithmicBuckets.Buckets storage poolBuckets,
        LogarithmicBuckets.Buckets storage p2pBuckets,
        Types.MatchingEngineVars memory vars
    ) internal returns (uint256 processed, uint256 iterationsDone) {
        if (vars.maxIterations == 0) return (0, 0);

        uint256 remaining = vars.amount;
        LogarithmicBuckets.Buckets storage workingBuckets = vars.demoting ? p2pBuckets : poolBuckets;

        for (; iterationsDone < vars.maxIterations && remaining != 0; ++iterationsDone) {
            address firstUser = workingBuckets.getMatch(remaining);
            if (firstUser == address(0)) break;

            uint256 onPool;
            uint256 inP2P;

            (onPool, inP2P, remaining) =
                vars.step(poolBuckets.valueOf[firstUser], p2pBuckets.valueOf[firstUser], vars.indexes, remaining);

            (onPool, inP2P) = vars.updateDS(vars.underlying, firstUser, onPool, inP2P, vars.demoting);

            if (vars.borrow) emit Events.BorrowPositionUpdated(firstUser, vars.underlying, onPool, inP2P);
            else emit Events.SupplyPositionUpdated(firstUser, vars.underlying, onPool, inP2P);
        }

        // Safe unchecked because vars.amount >= remaining.
        unchecked {
            processed = vars.amount - remaining;
        }
    }

    /// @dev Promotes a given amount in peer-to-peer.
    /// @param poolBalance The scaled balance of the user on the pool.
    /// @param p2pBalance The scaled balance of the user in peer-to-peer.
    /// @param indexes The indexes of the market.
    /// @param remaining The remaining amount to promote.
    function _promote(
        uint256 poolBalance,
        uint256 p2pBalance,
        Types.MarketSideIndexes256 memory indexes,
        uint256 remaining
    ) internal pure returns (uint256 newPoolBalance, uint256 newP2PBalance, uint256 newRemaining) {
        uint256 toProcess = Math.min(poolBalance.rayMul(indexes.poolIndex), remaining);

        newRemaining = remaining - toProcess;
        newPoolBalance = poolBalance - toProcess.rayDiv(indexes.poolIndex);
        newP2PBalance = p2pBalance + toProcess.rayDiv(indexes.p2pIndex);
    }

    /// @dev Demotes a given amount in peer-to-peer.
    /// @param poolBalance The scaled balance of the user on the pool.
    /// @param p2pBalance The scaled balance of the user in peer-to-peer.
    /// @param indexes The indexes of the market.
    /// @param remaining The remaining amount to demote.
    function _demote(
        uint256 poolBalance,
        uint256 p2pBalance,
        Types.MarketSideIndexes256 memory indexes,
        uint256 remaining
    ) internal pure returns (uint256 newPoolBalance, uint256 newP2PBalance, uint256 newRemaining) {
        uint256 toProcess = Math.min(p2pBalance.rayMul(indexes.p2pIndex), remaining);

        newRemaining = remaining - toProcess;
        newPoolBalance = poolBalance + toProcess.rayDiv(indexes.poolIndex);
        newP2PBalance = p2pBalance - toProcess.rayDiv(indexes.p2pIndex);
    }
}

File 39 of 57 : IPriceOracleGetter.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

/**
 * @title IPriceOracleGetter
 * @author Aave
 * @notice Interface for the Aave price oracle.
 */
interface IPriceOracleGetter {
  /**
   * @notice Returns the base currency address
   * @dev Address 0x0 is reserved for USD as base currency.
   * @return Returns the base currency address.
   */
  function BASE_CURRENCY() external view returns (address);

  /**
   * @notice Returns the base currency unit
   * @dev 1 ether for ETH, 1e8 for USD.
   * @return Returns the base currency unit.
   */
  function BASE_CURRENCY_UNIT() external view returns (uint256);

  /**
   * @notice Returns the asset price in the base currency
   * @param asset The address of the asset
   * @return The price of the asset
   */
  function getAssetPrice(address asset) external view returns (uint256);
}

File 40 of 57 : BucketDLL.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

/// @title BucketDLL
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice The doubly linked list used in logarithmic buckets.
library BucketDLL {
    /* STRUCTS */

    struct Account {
        address prev;
        address next;
    }

    struct List {
        mapping(address => Account) accounts;
    }

    /* INTERNAL */

    /// @notice Returns the next id address from the current `id`.
    /// @dev Pass the address 0 to get the head of the list.
    /// @param list The list to search in.
    /// @param id The address of the current account.
    /// @return The address of the next account.
    function getNext(List storage list, address id) internal view returns (address) {
        return list.accounts[id].next;
    }

    /// @notice Removes an account of the `list`.
    /// @dev This function should not be called with `id` equal to address 0.
    /// @dev This function should not be called with an `_id` that is not in the list.
    /// @param list The list to search in.
    /// @param id The address of the account.
    /// @return Whether the bucket is empty after removal.
    function remove(List storage list, address id) internal returns (bool) {
        Account memory account = list.accounts[id];
        address prev = account.prev;
        address next = account.next;

        list.accounts[prev].next = next;
        list.accounts[next].prev = prev;

        delete list.accounts[id];

        return (prev == address(0) && next == address(0));
    }

    /// @notice Inserts an account in the `list`.
    /// @dev This function should not be called with `id` equal to address 0.
    /// @dev This function should not be called with an `id` that is already in the list.
    /// @param list The list to search in.
    /// @param id The address of the account.
    /// @param atHead Tells whether to insert at the head or at the tail of the list.
    /// @return Whether the bucket was empty before insertion.
    function insert(
        List storage list,
        address id,
        bool atHead
    ) internal returns (bool) {
        if (atHead) {
            address head = list.accounts[address(0)].next;
            list.accounts[address(0)].next = id;
            list.accounts[head].prev = id;
            list.accounts[id].next = head;
            return head == address(0);
        } else {
            address tail = list.accounts[address(0)].prev;
            list.accounts[address(0)].prev = id;
            list.accounts[tail].next = id;
            list.accounts[id].prev = tail;
            return tail == address(0);
        }
    }
}

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

pragma solidity ^0.8.0;

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

File 42 of 57 : IScaledBalanceToken.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

/**
 * @title IScaledBalanceToken
 * @author Aave
 * @notice Defines the basic interface for a scaled-balance token.
 */
interface IScaledBalanceToken {
  /**
   * @dev Emitted after the mint action
   * @param caller The address performing the mint
   * @param onBehalfOf The address of the user that will receive the minted tokens
   * @param value The scaled-up amount being minted (based on user entered amount and balance increase from interest)
   * @param balanceIncrease The increase in scaled-up balance since the last action of 'onBehalfOf'
   * @param index The next liquidity index of the reserve
   */
  event Mint(
    address indexed caller,
    address indexed onBehalfOf,
    uint256 value,
    uint256 balanceIncrease,
    uint256 index
  );

  /**
   * @dev Emitted after the burn action
   * @dev If the burn function does not involve a transfer of the underlying asset, the target defaults to zero address
   * @param from The address from which the tokens will be burned
   * @param target The address that will receive the underlying, if any
   * @param value The scaled-up amount being burned (user entered amount - balance increase from interest)
   * @param balanceIncrease The increase in scaled-up balance since the last action of 'from'
   * @param index The next liquidity index of the reserve
   */
  event Burn(
    address indexed from,
    address indexed target,
    uint256 value,
    uint256 balanceIncrease,
    uint256 index
  );

  /**
   * @notice Returns the scaled balance of the user.
   * @dev The scaled balance is the sum of all the updated stored balance divided by the reserve's liquidity index
   * at the moment of the update
   * @param user The user whose balance is calculated
   * @return The scaled balance of the user
   */
  function scaledBalanceOf(address user) external view returns (uint256);

  /**
   * @notice Returns the scaled balance of the user and the scaled total supply.
   * @param user The address of the user
   * @return The scaled balance of the user
   * @return The scaled total supply
   */
  function getScaledUserBalanceAndSupply(address user) external view returns (uint256, uint256);

  /**
   * @notice Returns the scaled total supply of the scaled balance token. Represents sum(debt/index)
   * @return The scaled total supply
   */
  function scaledTotalSupply() external view returns (uint256);

  /**
   * @notice Returns last index interest was accrued to the user's balance
   * @param user The address of the user
   * @return The last index interest was accrued to the user's balance, expressed in ray
   */
  function getPreviousIndex(address user) external view returns (uint256);
}

File 43 of 57 : IInitializableAToken.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IAaveIncentivesController} from './IAaveIncentivesController.sol';
import {IPool} from './IPool.sol';

/**
 * @title IInitializableAToken
 * @author Aave
 * @notice Interface for the initialize function on AToken
 */
interface IInitializableAToken {
  /**
   * @dev Emitted when an aToken is initialized
   * @param underlyingAsset The address of the underlying asset
   * @param pool The address of the associated pool
   * @param treasury The address of the treasury
   * @param incentivesController The address of the incentives controller for this aToken
   * @param aTokenDecimals The decimals of the underlying
   * @param aTokenName The name of the aToken
   * @param aTokenSymbol The symbol of the aToken
   * @param params A set of encoded parameters for additional initialization
   */
  event Initialized(
    address indexed underlyingAsset,
    address indexed pool,
    address treasury,
    address incentivesController,
    uint8 aTokenDecimals,
    string aTokenName,
    string aTokenSymbol,
    bytes params
  );

  /**
   * @notice Initializes the aToken
   * @param pool The pool contract that is initializing this contract
   * @param treasury The address of the Aave treasury, receiving the fees on this aToken
   * @param underlyingAsset The address of the underlying asset of this aToken (E.g. WETH for aWETH)
   * @param incentivesController The smart contract managing potential incentives distribution
   * @param aTokenDecimals The decimals of the aToken, same as the underlying asset's
   * @param aTokenName The name of the aToken
   * @param aTokenSymbol The symbol of the aToken
   * @param params A set of encoded parameters for additional initialization
   */
  function initialize(
    IPool pool,
    address treasury,
    address underlyingAsset,
    IAaveIncentivesController incentivesController,
    uint8 aTokenDecimals,
    string calldata aTokenName,
    string calldata aTokenSymbol,
    bytes calldata params
  ) external;
}

File 44 of 57 : IInitializableDebtToken.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IAaveIncentivesController} from './IAaveIncentivesController.sol';
import {IPool} from './IPool.sol';

/**
 * @title IInitializableDebtToken
 * @author Aave
 * @notice Interface for the initialize function common between debt tokens
 */
interface IInitializableDebtToken {
  /**
   * @dev Emitted when a debt token is initialized
   * @param underlyingAsset The address of the underlying asset
   * @param pool The address of the associated pool
   * @param incentivesController The address of the incentives controller for this aToken
   * @param debtTokenDecimals The decimals of the debt token
   * @param debtTokenName The name of the debt token
   * @param debtTokenSymbol The symbol of the debt token
   * @param params A set of encoded parameters for additional initialization
   */
  event Initialized(
    address indexed underlyingAsset,
    address indexed pool,
    address incentivesController,
    uint8 debtTokenDecimals,
    string debtTokenName,
    string debtTokenSymbol,
    bytes params
  );

  /**
   * @notice Initializes the debt token.
   * @param pool The pool contract that is initializing this contract
   * @param underlyingAsset The address of the underlying asset of this aToken (E.g. WETH for aWETH)
   * @param incentivesController The smart contract managing potential incentives distribution
   * @param debtTokenDecimals The decimals of the debtToken, same as the underlying asset's
   * @param debtTokenName The name of the token
   * @param debtTokenSymbol The symbol of the token
   * @param params A set of encoded parameters for additional initialization
   */
  function initialize(
    IPool pool,
    address underlyingAsset,
    IAaveIncentivesController incentivesController,
    uint8 debtTokenDecimals,
    string memory debtTokenName,
    string memory debtTokenSymbol,
    bytes calldata params
  ) external;
}

File 45 of 57 : AddressUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library AddressUpgradeable {
    /**
     * @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
     * ====
     *
     * [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://diligence.consensys.net/posts/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.5.11/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 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);
        }
    }
}

File 46 of 57 : OwnableUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/ContextUpgradeable.sol";
import "../proxy/utils/Initializable.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 OwnableUpgradeable is Initializable, ContextUpgradeable {
    address private _owner;

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

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    function __Ownable_init() internal onlyInitializing {
        __Ownable_init_unchained();
    }

    function __Ownable_init_unchained() internal onlyInitializing {
        _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 anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing 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);
    }

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[49] private __gap;
}

File 47 of 57 : ReserveDataLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IStableDebtToken} from "@aave-v3-core/interfaces/IStableDebtToken.sol";
import {IVariableDebtToken} from "@aave-v3-core/interfaces/IVariableDebtToken.sol";

import {Types} from "./Types.sol";

import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

import {MathUtils} from "@aave-v3-core/protocol/libraries/math/MathUtils.sol";
import {DataTypes} from "@aave-v3-core/protocol/libraries/types/DataTypes.sol";
import {ReserveConfiguration} from "@aave-v3-core/protocol/libraries/configuration/ReserveConfiguration.sol";

/// @title ReserveDataLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library used to ease AaveV3's reserve-related calculations.
library ReserveDataLib {
    using WadRayMath for uint256;
    using PercentageMath for uint256;

    using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

    /// @notice Calculates the scaled quantity of reserve dedicated to AaveV3's treasury, taking into account interests accrued.
    /// @dev Reference: https://github.com/aave/aave-v3-core/blob/a00f28e3ad7c0e4a369d8e06e0ac9fd0acabcab7/contracts/protocol/libraries/logic/ReserveLogic.sol#L230-L282.
    /// @param reserve The reserve data of a given market.
    /// @param indexes The updated pool & peer-to-peer indexes of the associated market.
    /// @return The reserve's dedicated treasury, in pool unit.
    function getAccruedToTreasury(DataTypes.ReserveData memory reserve, Types.Indexes256 memory indexes)
        internal
        view
        returns (uint256)
    {
        uint256 reserveFactor = reserve.configuration.getReserveFactor();
        if (reserveFactor == 0) return reserve.accruedToTreasury;

        (
            uint256 currPrincipalStableDebt,
            uint256 currTotalStableDebt,
            uint256 currAvgStableBorrowRate,
            uint40 stableDebtLastUpdateTimestamp
        ) = IStableDebtToken(reserve.stableDebtTokenAddress).getSupplyData();
        uint256 scaledTotalVariableDebt = IVariableDebtToken(reserve.variableDebtTokenAddress).scaledTotalSupply();

        uint256 currTotalVariableDebt = scaledTotalVariableDebt.rayMul(indexes.borrow.poolIndex);
        uint256 prevTotalVariableDebt = scaledTotalVariableDebt.rayMul(reserve.variableBorrowIndex);
        uint256 prevTotalStableDebt = currPrincipalStableDebt.rayMul(
            MathUtils.calculateCompoundedInterest(
                currAvgStableBorrowRate, stableDebtLastUpdateTimestamp, reserve.lastUpdateTimestamp
            )
        );

        uint256 accruedTotalDebt =
            currTotalVariableDebt + currTotalStableDebt - prevTotalVariableDebt - prevTotalStableDebt;
        if (accruedTotalDebt == 0) return reserve.accruedToTreasury;

        uint256 newAccruedToTreasury = accruedTotalDebt.percentMul(reserveFactor).rayDiv(indexes.supply.poolIndex);

        return reserve.accruedToTreasury + newAccruedToTreasury;
    }
}

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

pragma solidity ^0.8.0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

File 49 of 57 : Errors.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

/**
 * @title Errors library
 * @author Aave
 * @notice Defines the error messages emitted by the different contracts of the Aave protocol
 */
library Errors {
  string public constant CALLER_NOT_POOL_ADMIN = '1'; // 'The caller of the function is not a pool admin'
  string public constant CALLER_NOT_EMERGENCY_ADMIN = '2'; // 'The caller of the function is not an emergency admin'
  string public constant CALLER_NOT_POOL_OR_EMERGENCY_ADMIN = '3'; // 'The caller of the function is not a pool or emergency admin'
  string public constant CALLER_NOT_RISK_OR_POOL_ADMIN = '4'; // 'The caller of the function is not a risk or pool admin'
  string public constant CALLER_NOT_ASSET_LISTING_OR_POOL_ADMIN = '5'; // 'The caller of the function is not an asset listing or pool admin'
  string public constant CALLER_NOT_BRIDGE = '6'; // 'The caller of the function is not a bridge'
  string public constant ADDRESSES_PROVIDER_NOT_REGISTERED = '7'; // 'Pool addresses provider is not registered'
  string public constant INVALID_ADDRESSES_PROVIDER_ID = '8'; // 'Invalid id for the pool addresses provider'
  string public constant NOT_CONTRACT = '9'; // 'Address is not a contract'
  string public constant CALLER_NOT_POOL_CONFIGURATOR = '10'; // 'The caller of the function is not the pool configurator'
  string public constant CALLER_NOT_ATOKEN = '11'; // 'The caller of the function is not an AToken'
  string public constant INVALID_ADDRESSES_PROVIDER = '12'; // 'The address of the pool addresses provider is invalid'
  string public constant INVALID_FLASHLOAN_EXECUTOR_RETURN = '13'; // 'Invalid return value of the flashloan executor function'
  string public constant RESERVE_ALREADY_ADDED = '14'; // 'Reserve has already been added to reserve list'
  string public constant NO_MORE_RESERVES_ALLOWED = '15'; // 'Maximum amount of reserves in the pool reached'
  string public constant EMODE_CATEGORY_RESERVED = '16'; // 'Zero eMode category is reserved for volatile heterogeneous assets'
  string public constant INVALID_EMODE_CATEGORY_ASSIGNMENT = '17'; // 'Invalid eMode category assignment to asset'
  string public constant RESERVE_LIQUIDITY_NOT_ZERO = '18'; // 'The liquidity of the reserve needs to be 0'
  string public constant FLASHLOAN_PREMIUM_INVALID = '19'; // 'Invalid flashloan premium'
  string public constant INVALID_RESERVE_PARAMS = '20'; // 'Invalid risk parameters for the reserve'
  string public constant INVALID_EMODE_CATEGORY_PARAMS = '21'; // 'Invalid risk parameters for the eMode category'
  string public constant BRIDGE_PROTOCOL_FEE_INVALID = '22'; // 'Invalid bridge protocol fee'
  string public constant CALLER_MUST_BE_POOL = '23'; // 'The caller of this function must be a pool'
  string public constant INVALID_MINT_AMOUNT = '24'; // 'Invalid amount to mint'
  string public constant INVALID_BURN_AMOUNT = '25'; // 'Invalid amount to burn'
  string public constant INVALID_AMOUNT = '26'; // 'Amount must be greater than 0'
  string public constant RESERVE_INACTIVE = '27'; // 'Action requires an active reserve'
  string public constant RESERVE_FROZEN = '28'; // 'Action cannot be performed because the reserve is frozen'
  string public constant RESERVE_PAUSED = '29'; // 'Action cannot be performed because the reserve is paused'
  string public constant BORROWING_NOT_ENABLED = '30'; // 'Borrowing is not enabled'
  string public constant STABLE_BORROWING_NOT_ENABLED = '31'; // 'Stable borrowing is not enabled'
  string public constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '32'; // 'User cannot withdraw more than the available balance'
  string public constant INVALID_INTEREST_RATE_MODE_SELECTED = '33'; // 'Invalid interest rate mode selected'
  string public constant COLLATERAL_BALANCE_IS_ZERO = '34'; // 'The collateral balance is 0'
  string public constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '35'; // 'Health factor is lesser than the liquidation threshold'
  string public constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '36'; // 'There is not enough collateral to cover a new borrow'
  string public constant COLLATERAL_SAME_AS_BORROWING_CURRENCY = '37'; // 'Collateral is (mostly) the same currency that is being borrowed'
  string public constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '38'; // 'The requested amount is greater than the max loan size in stable rate mode'
  string public constant NO_DEBT_OF_SELECTED_TYPE = '39'; // 'For repayment of a specific type of debt, the user needs to have debt that type'
  string public constant NO_EXPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '40'; // 'To repay on behalf of a user an explicit amount to repay is needed'
  string public constant NO_OUTSTANDING_STABLE_DEBT = '41'; // 'User does not have outstanding stable rate debt on this reserve'
  string public constant NO_OUTSTANDING_VARIABLE_DEBT = '42'; // 'User does not have outstanding variable rate debt on this reserve'
  string public constant UNDERLYING_BALANCE_ZERO = '43'; // 'The underlying balance needs to be greater than 0'
  string public constant INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET = '44'; // 'Interest rate rebalance conditions were not met'
  string public constant HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '45'; // 'Health factor is not below the threshold'
  string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '46'; // 'The collateral chosen cannot be liquidated'
  string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '47'; // 'User did not borrow the specified currency'
  string public constant INCONSISTENT_FLASHLOAN_PARAMS = '49'; // 'Inconsistent flashloan parameters'
  string public constant BORROW_CAP_EXCEEDED = '50'; // 'Borrow cap is exceeded'
  string public constant SUPPLY_CAP_EXCEEDED = '51'; // 'Supply cap is exceeded'
  string public constant UNBACKED_MINT_CAP_EXCEEDED = '52'; // 'Unbacked mint cap is exceeded'
  string public constant DEBT_CEILING_EXCEEDED = '53'; // 'Debt ceiling is exceeded'
  string public constant UNDERLYING_CLAIMABLE_RIGHTS_NOT_ZERO = '54'; // 'Claimable rights over underlying not zero (aToken supply or accruedToTreasury)'
  string public constant STABLE_DEBT_NOT_ZERO = '55'; // 'Stable debt supply is not zero'
  string public constant VARIABLE_DEBT_SUPPLY_NOT_ZERO = '56'; // 'Variable debt supply is not zero'
  string public constant LTV_VALIDATION_FAILED = '57'; // 'Ltv validation failed'
  string public constant INCONSISTENT_EMODE_CATEGORY = '58'; // 'Inconsistent eMode category'
  string public constant PRICE_ORACLE_SENTINEL_CHECK_FAILED = '59'; // 'Price oracle sentinel validation failed'
  string public constant ASSET_NOT_BORROWABLE_IN_ISOLATION = '60'; // 'Asset is not borrowable in isolation mode'
  string public constant RESERVE_ALREADY_INITIALIZED = '61'; // 'Reserve has already been initialized'
  string public constant USER_IN_ISOLATION_MODE = '62'; // 'User is in isolation mode'
  string public constant INVALID_LTV = '63'; // 'Invalid ltv parameter for the reserve'
  string public constant INVALID_LIQ_THRESHOLD = '64'; // 'Invalid liquidity threshold parameter for the reserve'
  string public constant INVALID_LIQ_BONUS = '65'; // 'Invalid liquidity bonus parameter for the reserve'
  string public constant INVALID_DECIMALS = '66'; // 'Invalid decimals parameter of the underlying asset of the reserve'
  string public constant INVALID_RESERVE_FACTOR = '67'; // 'Invalid reserve factor parameter for the reserve'
  string public constant INVALID_BORROW_CAP = '68'; // 'Invalid borrow cap for the reserve'
  string public constant INVALID_SUPPLY_CAP = '69'; // 'Invalid supply cap for the reserve'
  string public constant INVALID_LIQUIDATION_PROTOCOL_FEE = '70'; // 'Invalid liquidation protocol fee for the reserve'
  string public constant INVALID_EMODE_CATEGORY = '71'; // 'Invalid eMode category for the reserve'
  string public constant INVALID_UNBACKED_MINT_CAP = '72'; // 'Invalid unbacked mint cap for the reserve'
  string public constant INVALID_DEBT_CEILING = '73'; // 'Invalid debt ceiling for the reserve
  string public constant INVALID_RESERVE_INDEX = '74'; // 'Invalid reserve index'
  string public constant ACL_ADMIN_CANNOT_BE_ZERO = '75'; // 'ACL admin cannot be set to the zero address'
  string public constant INCONSISTENT_PARAMS_LENGTH = '76'; // 'Array parameters that should be equal length are not'
  string public constant ZERO_ADDRESS_NOT_VALID = '77'; // 'Zero address not valid'
  string public constant INVALID_EXPIRATION = '78'; // 'Invalid expiration'
  string public constant INVALID_SIGNATURE = '79'; // 'Invalid signature'
  string public constant OPERATION_NOT_SUPPORTED = '80'; // 'Operation not supported'
  string public constant DEBT_CEILING_NOT_ZERO = '81'; // 'Debt ceiling is not zero'
  string public constant ASSET_NOT_LISTED = '82'; // 'Asset is not listed'
  string public constant INVALID_OPTIMAL_USAGE_RATIO = '83'; // 'Invalid optimal usage ratio'
  string public constant INVALID_OPTIMAL_STABLE_TO_TOTAL_DEBT_RATIO = '84'; // 'Invalid optimal stable to total debt ratio'
  string public constant UNDERLYING_CANNOT_BE_RESCUED = '85'; // 'The underlying asset cannot be rescued'
  string public constant ADDRESSES_PROVIDER_ALREADY_ADDED = '86'; // 'Reserve has already been added to reserve list'
  string public constant POOL_ADDRESSES_DO_NOT_MATCH = '87'; // 'The token implementation pool address and the pool address provided by the initializing pool do not match'
  string public constant STABLE_BORROWING_ENABLED = '88'; // 'Stable borrowing is enabled'
  string public constant SILOED_BORROWING_VIOLATION = '89'; // 'User is trying to borrow multiple assets including a siloed one'
  string public constant RESERVE_DEBT_NOT_ZERO = '90'; // the total debt of the reserve needs to be 0
  string public constant FLASHLOAN_DISABLED = '91'; // FlashLoaning for this asset is disabled
}

File 50 of 57 : MorphoInternal.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {IPool} from "@aave-v3-core/interfaces/IPool.sol";
import {IRewardsManager} from "./interfaces/IRewardsManager.sol";
import {IAaveOracle} from "@aave-v3-core/interfaces/IAaveOracle.sol";

import {Types} from "./libraries/Types.sol";
import {Events} from "./libraries/Events.sol";
import {Errors} from "./libraries/Errors.sol";
import {PoolLib} from "./libraries/PoolLib.sol";
import {MarketLib} from "./libraries/MarketLib.sol";
import {DeltasLib} from "./libraries/DeltasLib.sol";
import {Constants} from "./libraries/Constants.sol";
import {MarketBalanceLib} from "./libraries/MarketBalanceLib.sol";
import {InterestRatesLib} from "./libraries/InterestRatesLib.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

import {ERC20, SafeTransferLib} from "@solmate/utils/SafeTransferLib.sol";

import {LogarithmicBuckets} from "@morpho-data-structures/LogarithmicBuckets.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {DataTypes} from "@aave-v3-core/protocol/libraries/types/DataTypes.sol";
import {UserConfiguration} from "@aave-v3-core/protocol/libraries/configuration/UserConfiguration.sol";
import {ReserveConfiguration} from "@aave-v3-core/protocol/libraries/configuration/ReserveConfiguration.sol";

import {MorphoStorage} from "./MorphoStorage.sol";

/// @title MorphoInternal
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Abstract contract exposing `Morpho`'s internal functions.
abstract contract MorphoInternal is MorphoStorage {
    using PoolLib for IPool;
    using MarketLib for Types.Market;
    using DeltasLib for Types.Deltas;
    using MarketBalanceLib for Types.MarketBalances;

    using Math for uint256;
    using WadRayMath for uint256;
    using PercentageMath for uint256;

    using SafeTransferLib for ERC20;

    using EnumerableSet for EnumerableSet.AddressSet;
    using LogarithmicBuckets for LogarithmicBuckets.Buckets;

    using UserConfiguration for DataTypes.UserConfigurationMap;
    using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

    /* INTERNAL */

    /// @dev Dynamically computed to use the root proxy address in a delegate call.
    function _domainSeparator() internal view returns (bytes32) {
        return keccak256(
            abi.encode(
                Constants.EIP712_DOMAIN_TYPEHASH,
                keccak256(bytes(Constants.EIP712_NAME)),
                keccak256(bytes(Constants.EIP712_VERSION)),
                block.chainid,
                address(this)
            )
        );
    }

    /// @dev Creates a new market for the `underlying` token with a given `reserveFactor` (in bps) and a given `p2pIndexCursor` (in bps).
    function _createMarket(address underlying, uint16 reserveFactor, uint16 p2pIndexCursor) internal {
        if (underlying == address(0)) revert Errors.AddressIsZero();

        DataTypes.ReserveData memory reserve = _pool.getReserveData(underlying);
        if (!reserve.configuration.getActive()) revert Errors.MarketIsNotListedOnAave();
        if (reserve.configuration.getSiloedBorrowing()) revert Errors.SiloedBorrowMarket();

        // `liquidationThreshold < Constants.LT_LOWER_BOUND` checks that the asset's LT is not too low to be listed as a collateral on Morpho.
        // If the LT is 0, the check is skipped to be able to create markets for non-collateral assets.
        // In the case where the "standard" LT is below the `LT_LOWER_BOUND` while the eMode LT is higher, the check will still revert for safety.
        uint256 liquidationThreshold = reserve.configuration.getLiquidationThreshold();
        if (liquidationThreshold > 0 && liquidationThreshold < Constants.LT_LOWER_BOUND) {
            revert Errors.MarketLtTooLow();
        }

        Types.Market storage market = _market[underlying];

        if (market.isCreated()) revert Errors.MarketAlreadyCreated();

        market.underlying = underlying;
        market.aToken = reserve.aTokenAddress;
        market.variableDebtToken = reserve.variableDebtTokenAddress;
        market.stableDebtToken = reserve.stableDebtTokenAddress;

        _marketsCreated.push(underlying);

        emit Events.MarketCreated(underlying);

        Types.Indexes256 memory indexes;
        (indexes.supply.poolIndex, indexes.borrow.poolIndex) = _pool.getCurrentPoolIndexes(underlying);
        indexes.supply.p2pIndex = WadRayMath.RAY;
        indexes.borrow.p2pIndex = WadRayMath.RAY;

        market.setIndexes(indexes);
        market.setReserveFactor(reserveFactor);
        market.setP2PIndexCursor(p2pIndexCursor);

        ERC20(underlying).safeApprove(address(_pool), type(uint256).max);
    }

    /// @dev Claims the fee for the `underlyings` and send it to the `_treasuryVault`.
    ///      Claiming on a market where there are some rewards might steal users' rewards.
    function _claimToTreasury(address[] calldata underlyings, uint256[] calldata amounts) internal {
        address treasuryVault = _treasuryVault;
        if (treasuryVault == address(0)) revert Errors.AddressIsZero();

        for (uint256 i; i < underlyings.length; ++i) {
            address underlying = underlyings[i];
            Types.Market storage market = _market[underlying];

            if (!market.isCreated()) continue;

            uint256 claimable = ERC20(underlying).balanceOf(address(this)) - market.idleSupply;
            uint256 claimed = Math.min(amounts[i], claimable);

            if (claimed == 0) continue;

            ERC20(underlying).safeTransfer(treasuryVault, claimed);
            emit Events.ReserveFeeClaimed(underlying, claimed);
        }
    }

    /// @dev Increases the peer-to-peer delta of `amount` on the `underlying` market.
    /// @dev Note that this can fail if the amount is too big. In this case, consider splitting in multiple calls/txs.
    function _increaseP2PDeltas(address underlying, uint256 amount) internal {
        Types.Indexes256 memory indexes = _updateIndexes(underlying);

        Types.Market storage market = _market[underlying];
        Types.Deltas storage deltas = market.deltas;
        uint256 poolSupplyIndex = indexes.supply.poolIndex;
        uint256 poolBorrowIndex = indexes.borrow.poolIndex;

        amount = Math.min(amount, Math.min(market.trueP2PSupply(indexes), market.trueP2PBorrow(indexes)));
        if (amount == 0) revert Errors.AmountIsZero();

        uint256 newSupplyDelta = deltas.supply.scaledDelta + amount.rayDiv(poolSupplyIndex);
        uint256 newBorrowDelta = deltas.borrow.scaledDelta + amount.rayDiv(poolBorrowIndex);

        deltas.supply.scaledDelta = newSupplyDelta;
        deltas.borrow.scaledDelta = newBorrowDelta;
        emit Events.P2PSupplyDeltaUpdated(underlying, newSupplyDelta);
        emit Events.P2PBorrowDeltaUpdated(underlying, newBorrowDelta);

        _pool.borrowFromPool(underlying, amount);
        _pool.supplyToPool(underlying, amount, poolSupplyIndex);

        emit Events.P2PDeltasIncreased(underlying, amount);
    }

    /// @dev Returns the hash of the EIP712 typed data.
    function _hashEIP712TypedData(bytes32 structHash) internal view returns (bytes32) {
        return keccak256(abi.encodePacked(Constants.EIP712_MSG_PREFIX, _domainSeparator(), structHash));
    }

    /// @dev Approves a `manager` to borrow/withdraw on behalf of the `delegator`.
    /// @param delegator The address of the delegator.
    /// @param manager The address of the manager.
    /// @param isAllowed Whether `manager` is allowed to manage `delegator`'s position or not.
    function _approveManager(address delegator, address manager, bool isAllowed) internal {
        _isManagedBy[delegator][manager] = isAllowed;
        emit Events.ManagerApproval(delegator, manager, isAllowed);
    }

    /// @dev Returns the total supply balance of `user` on the `underlying` market given `indexes` (in underlying).
    function _getUserSupplyBalanceFromIndexes(address underlying, address user, Types.Indexes256 memory indexes)
        internal
        view
        returns (uint256)
    {
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];

        return marketBalances.scaledPoolSupplyBalance(user).rayMulDown(indexes.supply.poolIndex)
            + marketBalances.scaledP2PSupplyBalance(user).rayMulDown(indexes.supply.p2pIndex);
    }

    /// @dev Returns the total borrow balance of `user` on the `underlying` market given `indexes` (in underlying).
    function _getUserBorrowBalanceFromIndexes(address underlying, address user, Types.Indexes256 memory indexes)
        internal
        view
        returns (uint256)
    {
        Types.MarketBalances storage marketBalances = _marketBalances[underlying];

        return marketBalances.scaledPoolBorrowBalance(user).rayMulUp(indexes.borrow.poolIndex)
            + marketBalances.scaledP2PBorrowBalance(user).rayMulUp(indexes.borrow.p2pIndex);
    }

    /// @dev Returns the collateral balance of `user` on the `underlying` market a `poolSupplyIndex` (in underlying).
    function _getUserCollateralBalanceFromIndex(address underlying, address user, uint256 poolSupplyIndex)
        internal
        view
        returns (uint256)
    {
        return _marketBalances[underlying].scaledCollateralBalance(user).rayMulDown(poolSupplyIndex);
    }

    /// @dev Returns the buckets of a particular side of a market.
    /// @param underlying The address of the underlying asset.
    /// @param position The side of the market.
    function _getBuckets(address underlying, Types.Position position)
        internal
        view
        returns (LogarithmicBuckets.Buckets storage)
    {
        if (position == Types.Position.POOL_SUPPLIER) {
            return _marketBalances[underlying].poolSuppliers;
        } else if (position == Types.Position.P2P_SUPPLIER) {
            return _marketBalances[underlying].p2pSuppliers;
        } else if (position == Types.Position.POOL_BORROWER) {
            return _marketBalances[underlying].poolBorrowers;
        } else {
            return _marketBalances[underlying].p2pBorrowers;
        }
    }

    /// @notice Returns the liquidity data about the position of `user`.
    /// @param user The address of the user to get the liquidity data for.
    /// @return liquidityData The liquidity data of the user.
    function _liquidityData(address user) internal view returns (Types.LiquidityData memory liquidityData) {
        Types.LiquidityVars memory vars;

        if (_eModeCategoryId != 0) vars.eModeCategory = _pool.getEModeCategoryData(_eModeCategoryId);
        vars.oracle = IAaveOracle(_addressesProvider.getPriceOracle());
        vars.user = user;

        (liquidityData.borrowable, liquidityData.maxDebt) = _totalCollateralData(vars);
        liquidityData.debt = _totalDebt(vars);
    }

    /// @dev Returns the collateral data for a given set of inputs.
    /// @dev The total collateral data is computed looping through all user's collateral assets.
    /// @param vars The liquidity variables.
    /// @return borrowable The total borrowable amount of `vars.user`.
    /// @return maxDebt The total maximum debt of `vars.user`.
    function _totalCollateralData(Types.LiquidityVars memory vars)
        internal
        view
        returns (uint256 borrowable, uint256 maxDebt)
    {
        address[] memory userCollaterals = _userCollaterals[vars.user].values();

        for (uint256 i; i < userCollaterals.length; ++i) {
            (uint256 borrowableSingle, uint256 maxDebtSingle) = _collateralData(userCollaterals[i], vars);

            borrowable += borrowableSingle;
            maxDebt += maxDebtSingle;
        }
    }

    /// @dev Returns the debt data for a given set of inputs.
    /// @dev The total debt data is computed iterating through all user's borrow assets.
    /// @param vars The liquidity variables.
    /// @return debt The total debt of `vars.user`.
    function _totalDebt(Types.LiquidityVars memory vars) internal view returns (uint256 debt) {
        address[] memory userBorrows = _userBorrows[vars.user].values();

        for (uint256 i; i < userBorrows.length; ++i) {
            debt += _debt(userBorrows[i], vars);
        }
    }

    /// @dev Returns the collateral data for a given set of inputs.
    /// @param underlying The address of the underlying collateral asset.
    /// @param vars The liquidity variables.
    /// @return borrowable The borrowable amount of `vars.user` on the `underlying` market.
    /// @return maxDebt The maximum debt of `vars.user` on the `underlying` market.
    function _collateralData(address underlying, Types.LiquidityVars memory vars)
        internal
        view
        returns (uint256 borrowable, uint256 maxDebt)
    {
        if (!_market[underlying].isCollateral) return (0, 0);

        (uint256 underlyingPrice, uint256 ltv, uint256 liquidationThreshold, uint256 underlyingUnit) =
            _assetLiquidityData(underlying, vars);

        Types.Indexes256 memory indexes = _computeIndexes(underlying);
        uint256 rawCollateral = (
            (_getUserCollateralBalanceFromIndex(underlying, vars.user, indexes.supply.poolIndex)) * underlyingPrice
        ) / underlyingUnit;

        // Morpho has a slightly different method of health factor calculation from the underlying pool.
        // This method is used to account for a potential rounding error in calculateUserAccountData,
        // see https://github.com/aave/aave-v3-core/blob/94e571f3a7465201881a59555314cd550ccfda57/contracts/protocol/libraries/logic/GenericLogic.sol#L64-L196
        // To resolve this, Morpho reduces the collateral value by a small amount.
        uint256 collateral = ((Constants.LT_LOWER_BOUND - 1) * rawCollateral) / Constants.LT_LOWER_BOUND;

        borrowable = collateral.percentMulDown(ltv);
        maxDebt = collateral.percentMulDown(liquidationThreshold);
    }

    /// @dev Returns the debt value for a given set of inputs.
    /// @param underlying The address of the underlying asset to borrow.
    /// @param vars The liquidity variables.
    /// @return debtValue The debt value of `vars.user` on the `underlying` market.
    function _debt(address underlying, Types.LiquidityVars memory vars) internal view returns (uint256 debtValue) {
        DataTypes.ReserveConfigurationMap memory config = _pool.getConfiguration(underlying);
        (, uint256 underlyingPrice, uint256 underlyingUnit) =
            _assetData(underlying, vars.oracle, config, vars.eModeCategory.priceSource);

        Types.Indexes256 memory indexes = _computeIndexes(underlying);
        debtValue =
            (_getUserBorrowBalanceFromIndexes(underlying, vars.user, indexes) * underlyingPrice).divUp(underlyingUnit);
    }

    /// @dev Returns the liquidity data for a given set of inputs.
    /// @param underlying The address of the underlying asset.
    /// @param vars The liquidity variables.
    /// @return underlyingPrice The price of the underlying asset (in base currency).
    /// @return ltv The loan to value of the underlying asset.
    /// @return liquidationThreshold The liquidation threshold of the underlying asset.
    /// @return underlyingUnit The token unit of the underlying asset.
    function _assetLiquidityData(address underlying, Types.LiquidityVars memory vars)
        internal
        view
        returns (uint256 underlyingPrice, uint256 ltv, uint256 liquidationThreshold, uint256 underlyingUnit)
    {
        DataTypes.ReserveConfigurationMap memory config = _pool.getConfiguration(underlying);

        bool isInEMode;
        (isInEMode, underlyingPrice, underlyingUnit) =
            _assetData(underlying, vars.oracle, config, vars.eModeCategory.priceSource);

        // If the LTV is 0 on Aave V3, the asset cannot be used as collateral to borrow upon a breaking withdraw.
        // In response, Morpho disables the asset as collateral and sets its liquidation threshold
        // to 0 and the governance should warn users to repay their debt.
        if (config.getLtv() == 0) return (underlyingPrice, 0, 0, underlyingUnit);

        if (isInEMode) {
            ltv = vars.eModeCategory.ltv;
            liquidationThreshold = vars.eModeCategory.liquidationThreshold;
        } else {
            ltv = config.getLtv();
            liquidationThreshold = config.getLiquidationThreshold();
        }
    }

    /// @dev Prompts the rewards manager (if set) to accrue a user's rewards.
    /// @param user The address of the user to accrue rewards for.
    /// @param poolToken The address of the pool token related to this market (aToken or variable debt token address).
    /// @param formerOnPool The former scaled balance on pool of the `user`.
    function _updateRewards(address user, address poolToken, uint256 formerOnPool) internal {
        IRewardsManager rewardsManager = _rewardsManager;
        if (address(rewardsManager) != address(0)) {
            rewardsManager.updateUserRewards(user, poolToken, formerOnPool);
        }
    }

    /// @dev Updates a `user`'s position in the data structure.
    /// @param poolToken The address of the pool token related to this market (aToken or variable debt token address).
    /// @param user The address of the user to update.
    /// @param poolBuckets The pool buckets.
    /// @param p2pBuckets The peer-to-peer buckets.
    /// @param onPool The new scaled balance on pool of the `user`.
    /// @param inP2P The new scaled balance in peer-to-peer of the `user`.
    /// @param demoting Whether the update is happening during a demoting process or not.
    /// @return The actual new scaled balance on pool and in peer-to-peer of the `user` after accounting for dust.
    function _updateInDS(
        address poolToken,
        address user,
        LogarithmicBuckets.Buckets storage poolBuckets,
        LogarithmicBuckets.Buckets storage p2pBuckets,
        uint256 onPool,
        uint256 inP2P,
        bool demoting
    ) internal returns (uint256, uint256) {
        if (onPool <= Constants.DUST_THRESHOLD) onPool = 0;
        if (inP2P <= Constants.DUST_THRESHOLD) inP2P = 0;

        uint256 formerOnPool = poolBuckets.valueOf[user];
        uint256 formerInP2P = p2pBuckets.valueOf[user];

        if (onPool != formerOnPool) {
            _updateRewards(user, poolToken, formerOnPool);
            poolBuckets.update(user, onPool, demoting);
        }

        if (inP2P != formerInP2P) p2pBuckets.update(user, inP2P, true);
        return (onPool, inP2P);
    }

    /// @dev Updates a `user`'s supply position in the data structure.
    /// @param underlying The address of the underlying asset.
    /// @param user The address of the user to update.
    /// @param onPool The new scaled balance on pool of the `user`.
    /// @param inP2P The new scaled balance in peer-to-peer of the `user`.
    /// @param demoting Whether the update is happening during a demoting process or not.
    /// @return The actual new scaled balance on pool and in peer-to-peer of the `user` after accounting for dust.
    function _updateSupplierInDS(address underlying, address user, uint256 onPool, uint256 inP2P, bool demoting)
        internal
        returns (uint256, uint256)
    {
        return _updateInDS(
            _market[underlying].aToken,
            user,
            _marketBalances[underlying].poolSuppliers,
            _marketBalances[underlying].p2pSuppliers,
            onPool,
            inP2P,
            demoting
        );
        // No need to update the user's list of supplied assets,
        // as it cannot be used as collateral and thus there's no need to iterate over it.
    }

    /// @dev Updates a `user`'s borrow position in the data structure.
    /// @param underlying The address of the underlying asset.
    /// @param user The address of the user to update.
    /// @param onPool The new scaled balance on pool of the `user`.
    /// @param inP2P The new scaled balance in peer-to-peer of the `user`.
    /// @param demoting Whether the update is happening during a demoting process or not.
    /// @return The actual new scaled balance on pool and in peer-to-peer of the `user` after accounting for dust.
    function _updateBorrowerInDS(address underlying, address user, uint256 onPool, uint256 inP2P, bool demoting)
        internal
        returns (uint256, uint256)
    {
        (onPool, inP2P) = _updateInDS(
            _market[underlying].variableDebtToken,
            user,
            _marketBalances[underlying].poolBorrowers,
            _marketBalances[underlying].p2pBorrowers,
            onPool,
            inP2P,
            demoting
        );
        if (onPool == 0 && inP2P == 0) _userBorrows[user].remove(underlying);
        else _userBorrows[user].add(underlying);
        return (onPool, inP2P);
    }

    /// @dev Sets globally the pause status to `isPaused` on the `underlying` market.
    function _setPauseStatus(address underlying, bool isPaused) internal {
        Types.Market storage market = _market[underlying];

        market.setIsSupplyPaused(isPaused);
        market.setIsSupplyCollateralPaused(isPaused);
        market.setIsRepayPaused(isPaused);
        market.setIsWithdrawPaused(isPaused);
        market.setIsWithdrawCollateralPaused(isPaused);
        market.setIsLiquidateCollateralPaused(isPaused);
        market.setIsLiquidateBorrowPaused(isPaused);
        if (!market.isDeprecated()) market.setIsBorrowPaused(isPaused);
    }

    /// @dev Updates the indexes of the `underlying` market and returns them.
    function _updateIndexes(address underlying) internal returns (Types.Indexes256 memory indexes) {
        indexes = _computeIndexes(underlying);

        _market[underlying].setIndexes(indexes);
    }

    /// @dev Computes the updated indexes of the `underlying` market (if not already updated) and returns them.
    function _computeIndexes(address underlying) internal view returns (Types.Indexes256 memory indexes) {
        Types.Market storage market = _market[underlying];
        Types.Indexes256 memory lastIndexes = market.getIndexes();

        (indexes.supply.poolIndex, indexes.borrow.poolIndex) = _pool.getCurrentPoolIndexes(underlying);

        (indexes.supply.p2pIndex, indexes.borrow.p2pIndex) = InterestRatesLib.computeP2PIndexes(
            Types.IndexesParams({
                lastSupplyIndexes: lastIndexes.supply,
                lastBorrowIndexes: lastIndexes.borrow,
                poolSupplyIndex: indexes.supply.poolIndex,
                poolBorrowIndex: indexes.borrow.poolIndex,
                reserveFactor: market.reserveFactor,
                p2pIndexCursor: market.p2pIndexCursor,
                deltas: market.deltas,
                proportionIdle: market.getProportionIdle()
            })
        );
    }

    /// @dev Returns the `user`'s health factor.
    function _getUserHealthFactor(address user) internal view returns (uint256) {
        Types.LiquidityData memory liquidityData = _liquidityData(user);

        return liquidityData.debt > 0 ? liquidityData.maxDebt.wadDiv(liquidityData.debt) : type(uint256).max;
    }

    /// @dev Returns data relative to the given asset and its configuration, according to a given oracle.
    /// @return Whether the given asset is part of Morpho's e-mode category.
    /// @return The asset's price or the price of the given e-mode price source if the asset is in the e-mode category, according to the given oracle.
    /// @return The asset's unit.
    function _assetData(
        address asset,
        IAaveOracle oracle,
        DataTypes.ReserveConfigurationMap memory config,
        address priceSource
    ) internal view returns (bool, uint256, uint256) {
        uint256 assetUnit;
        unchecked {
            assetUnit = 10 ** config.getDecimals();
        }

        bool isInEMode = _isInEModeCategory(config);
        if (isInEMode && priceSource != address(0)) {
            uint256 eModePrice = oracle.getAssetPrice(priceSource);

            if (eModePrice != 0) return (isInEMode, eModePrice, assetUnit);
        }

        return (isInEMode, oracle.getAssetPrice(asset), assetUnit);
    }

    /// @dev Returns whether Morpho is in an e-mode category and the given asset configuration is in the same e-mode category.
    function _isInEModeCategory(DataTypes.ReserveConfigurationMap memory config) internal view returns (bool) {
        return _eModeCategoryId != 0 && config.getEModeCategory() == _eModeCategoryId;
    }
}

File 51 of 57 : IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

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

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

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

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

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

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

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

File 52 of 57 : IAaveIncentivesController.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

/**
 * @title IAaveIncentivesController
 * @author Aave
 * @notice Defines the basic interface for an Aave Incentives Controller.
 * @dev It only contains one single function, needed as a hook on aToken and debtToken transfers.
 */
interface IAaveIncentivesController {
  /**
   * @dev Called by the corresponding asset on transfer hook in order to update the rewards distribution.
   * @dev The units of `totalSupply` and `userBalance` should be the same.
   * @param user The address of the user whose asset balance has changed
   * @param totalSupply The total supply of the asset prior to user balance change
   * @param userBalance The previous user balance prior to balance change
   */
  function handleAction(
    address user,
    uint256 totalSupply,
    uint256 userBalance
  ) external;
}

File 53 of 57 : ContextUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;
import "../proxy/utils/Initializable.sol";

/**
 * @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 ContextUpgradeable is Initializable {
    function __Context_init() internal onlyInitializing {
    }

    function __Context_init_unchained() internal onlyInitializing {
    }
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

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

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[50] private __gap;
}

File 54 of 57 : IStableDebtToken.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IInitializableDebtToken} from './IInitializableDebtToken.sol';

/**
 * @title IStableDebtToken
 * @author Aave
 * @notice Defines the interface for the stable debt token
 * @dev It does not inherit from IERC20 to save in code size
 */
interface IStableDebtToken is IInitializableDebtToken {
  /**
   * @dev Emitted when new stable debt is minted
   * @param user The address of the user who triggered the minting
   * @param onBehalfOf The recipient of stable debt tokens
   * @param amount The amount minted (user entered amount + balance increase from interest)
   * @param currentBalance The balance of the user based on the previous balance and balance increase from interest
   * @param balanceIncrease The increase in balance since the last action of the user 'onBehalfOf'
   * @param newRate The rate of the debt after the minting
   * @param avgStableRate The next average stable rate after the minting
   * @param newTotalSupply The next total supply of the stable debt token after the action
   */
  event Mint(
    address indexed user,
    address indexed onBehalfOf,
    uint256 amount,
    uint256 currentBalance,
    uint256 balanceIncrease,
    uint256 newRate,
    uint256 avgStableRate,
    uint256 newTotalSupply
  );

  /**
   * @dev Emitted when new stable debt is burned
   * @param from The address from which the debt will be burned
   * @param amount The amount being burned (user entered amount - balance increase from interest)
   * @param currentBalance The balance of the user based on the previous balance and balance increase from interest
   * @param balanceIncrease The increase in balance since the last action of 'from'
   * @param avgStableRate The next average stable rate after the burning
   * @param newTotalSupply The next total supply of the stable debt token after the action
   */
  event Burn(
    address indexed from,
    uint256 amount,
    uint256 currentBalance,
    uint256 balanceIncrease,
    uint256 avgStableRate,
    uint256 newTotalSupply
  );

  /**
   * @notice Mints debt token to the `onBehalfOf` address.
   * @dev The resulting rate is the weighted average between the rate of the new debt
   * and the rate of the previous debt
   * @param user The address receiving the borrowed underlying, being the delegatee in case
   * of credit delegate, or same as `onBehalfOf` otherwise
   * @param onBehalfOf The address receiving the debt tokens
   * @param amount The amount of debt tokens to mint
   * @param rate The rate of the debt being minted
   * @return True if it is the first borrow, false otherwise
   * @return The total stable debt
   * @return The average stable borrow rate
   */
  function mint(
    address user,
    address onBehalfOf,
    uint256 amount,
    uint256 rate
  )
    external
    returns (
      bool,
      uint256,
      uint256
    );

  /**
   * @notice Burns debt of `user`
   * @dev The resulting rate is the weighted average between the rate of the new debt
   * and the rate of the previous debt
   * @dev In some instances, a burn transaction will emit a mint event
   * if the amount to burn is less than the interest the user earned
   * @param from The address from which the debt will be burned
   * @param amount The amount of debt tokens getting burned
   * @return The total stable debt
   * @return The average stable borrow rate
   */
  function burn(address from, uint256 amount) external returns (uint256, uint256);

  /**
   * @notice Returns the average rate of all the stable rate loans.
   * @return The average stable rate
   */
  function getAverageStableRate() external view returns (uint256);

  /**
   * @notice Returns the stable rate of the user debt
   * @param user The address of the user
   * @return The stable rate of the user
   */
  function getUserStableRate(address user) external view returns (uint256);

  /**
   * @notice Returns the timestamp of the last update of the user
   * @param user The address of the user
   * @return The timestamp
   */
  function getUserLastUpdated(address user) external view returns (uint40);

  /**
   * @notice Returns the principal, the total supply, the average stable rate and the timestamp for the last update
   * @return The principal
   * @return The total supply
   * @return The average stable rate
   * @return The timestamp of the last update
   */
  function getSupplyData()
    external
    view
    returns (
      uint256,
      uint256,
      uint256,
      uint40
    );

  /**
   * @notice Returns the timestamp of the last update of the total supply
   * @return The timestamp
   */
  function getTotalSupplyLastUpdated() external view returns (uint40);

  /**
   * @notice Returns the total supply and the average stable rate
   * @return The total supply
   * @return The average rate
   */
  function getTotalSupplyAndAvgRate() external view returns (uint256, uint256);

  /**
   * @notice Returns the principal debt balance of the user
   * @return The debt balance of the user since the last burn/mint action
   */
  function principalBalanceOf(address user) external view returns (uint256);

  /**
   * @notice Returns the address of the underlying asset of this stableDebtToken (E.g. WETH for stableDebtWETH)
   * @return The address of the underlying asset
   */
  function UNDERLYING_ASSET_ADDRESS() external view returns (address);
}

File 55 of 57 : MathUtils.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {WadRayMath} from './WadRayMath.sol';

/**
 * @title MathUtils library
 * @author Aave
 * @notice Provides functions to perform linear and compounded interest calculations
 */
library MathUtils {
  using WadRayMath for uint256;

  /// @dev Ignoring leap years
  uint256 internal constant SECONDS_PER_YEAR = 365 days;

  /**
   * @dev Function to calculate the interest accumulated using a linear interest rate formula
   * @param rate The interest rate, in ray
   * @param lastUpdateTimestamp The timestamp of the last update of the interest
   * @return The interest rate linearly accumulated during the timeDelta, in ray
   */
  function calculateLinearInterest(uint256 rate, uint40 lastUpdateTimestamp)
    internal
    view
    returns (uint256)
  {
    //solium-disable-next-line
    uint256 result = rate * (block.timestamp - uint256(lastUpdateTimestamp));
    unchecked {
      result = result / SECONDS_PER_YEAR;
    }

    return WadRayMath.RAY + result;
  }

  /**
   * @dev Function to calculate the interest using a compounded interest rate formula
   * To avoid expensive exponentiation, the calculation is performed using a binomial approximation:
   *
   *  (1+x)^n = 1+n*x+[n/2*(n-1)]*x^2+[n/6*(n-1)*(n-2)*x^3...
   *
   * The approximation slightly underpays liquidity providers and undercharges borrowers, with the advantage of great
   * gas cost reductions. The whitepaper contains reference to the approximation and a table showing the margin of
   * error per different time periods
   *
   * @param rate The interest rate, in ray
   * @param lastUpdateTimestamp The timestamp of the last update of the interest
   * @return The interest rate compounded during the timeDelta, in ray
   */
  function calculateCompoundedInterest(
    uint256 rate,
    uint40 lastUpdateTimestamp,
    uint256 currentTimestamp
  ) internal pure returns (uint256) {
    //solium-disable-next-line
    uint256 exp = currentTimestamp - uint256(lastUpdateTimestamp);

    if (exp == 0) {
      return WadRayMath.RAY;
    }

    uint256 expMinusOne;
    uint256 expMinusTwo;
    uint256 basePowerTwo;
    uint256 basePowerThree;
    unchecked {
      expMinusOne = exp - 1;

      expMinusTwo = exp > 2 ? exp - 2 : 0;

      basePowerTwo = rate.rayMul(rate) / (SECONDS_PER_YEAR * SECONDS_PER_YEAR);
      basePowerThree = basePowerTwo.rayMul(rate) / SECONDS_PER_YEAR;
    }

    uint256 secondTerm = exp * expMinusOne * basePowerTwo;
    unchecked {
      secondTerm /= 2;
    }
    uint256 thirdTerm = exp * expMinusOne * expMinusTwo * basePowerThree;
    unchecked {
      thirdTerm /= 6;
    }

    return WadRayMath.RAY + (rate * exp) / SECONDS_PER_YEAR + secondTerm + thirdTerm;
  }

  /**
   * @dev Calculates the compounded interest between the timestamp of the last update and the current block timestamp
   * @param rate The interest rate (in ray)
   * @param lastUpdateTimestamp The timestamp from which the interest accumulation needs to be calculated
   * @return The interest rate compounded between lastUpdateTimestamp and current block timestamp, in ray
   */
  function calculateCompoundedInterest(uint256 rate, uint40 lastUpdateTimestamp)
    internal
    view
    returns (uint256)
  {
    return calculateCompoundedInterest(rate, lastUpdateTimestamp, block.timestamp);
  }
}

File 56 of 57 : InterestRatesLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

import {Types} from "./Types.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

/// @title InterestRatesLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library helping to compute the new peer-to-peer indexes.
library InterestRatesLib {
    using WadRayMath for uint256;
    using PercentageMath for uint256;

    /// @notice Computes and returns the new peer-to-peer indexes of a market given its parameters.
    /// @return newP2PSupplyIndex The peer-to-peer supply index.
    /// @return newP2PBorrowIndex The peer-to-peer borrow index.
    function computeP2PIndexes(Types.IndexesParams memory params)
        internal
        pure
        returns (uint256 newP2PSupplyIndex, uint256 newP2PBorrowIndex)
    {
        Types.GrowthFactors memory growthFactors = computeGrowthFactors(
            params.poolSupplyIndex,
            params.poolBorrowIndex,
            params.lastSupplyIndexes.poolIndex,
            params.lastBorrowIndexes.poolIndex,
            params.p2pIndexCursor,
            params.reserveFactor
        );
        newP2PSupplyIndex = computeP2PIndex(
            growthFactors.poolSupplyGrowthFactor,
            growthFactors.p2pSupplyGrowthFactor,
            params.lastSupplyIndexes,
            params.deltas.supply.scaledDelta,
            params.deltas.supply.scaledP2PTotal,
            params.proportionIdle
        );
        newP2PBorrowIndex = computeP2PIndex(
            growthFactors.poolBorrowGrowthFactor,
            growthFactors.p2pBorrowGrowthFactor,
            params.lastBorrowIndexes,
            params.deltas.borrow.scaledDelta,
            params.deltas.borrow.scaledP2PTotal,
            0
        );
    }

    /// @notice Computes and returns the new growth factors associated to a given pool's supply/borrow index & Morpho's peer-to-peer index.
    /// @param newPoolSupplyIndex The pool's current supply index.
    /// @param newPoolBorrowIndex The pool's current borrow index.
    /// @param lastPoolSupplyIndex The pool's last supply index.
    /// @param lastPoolBorrowIndex The pool's last borrow index.
    /// @param p2pIndexCursor The peer-to-peer index cursor for the given market.
    /// @param reserveFactor The reserve factor of the given market.
    /// @return growthFactors The market's indexes growth factors (in ray).
    function computeGrowthFactors(
        uint256 newPoolSupplyIndex,
        uint256 newPoolBorrowIndex,
        uint256 lastPoolSupplyIndex,
        uint256 lastPoolBorrowIndex,
        uint256 p2pIndexCursor,
        uint256 reserveFactor
    ) internal pure returns (Types.GrowthFactors memory growthFactors) {
        growthFactors.poolSupplyGrowthFactor = newPoolSupplyIndex.rayDiv(lastPoolSupplyIndex);
        growthFactors.poolBorrowGrowthFactor = newPoolBorrowIndex.rayDiv(lastPoolBorrowIndex);

        if (growthFactors.poolSupplyGrowthFactor <= growthFactors.poolBorrowGrowthFactor) {
            uint256 p2pGrowthFactor = PercentageMath.weightedAvg(
                growthFactors.poolSupplyGrowthFactor, growthFactors.poolBorrowGrowthFactor, p2pIndexCursor
            );

            growthFactors.p2pSupplyGrowthFactor =
                p2pGrowthFactor - (p2pGrowthFactor - growthFactors.poolSupplyGrowthFactor).percentMul(reserveFactor);
            growthFactors.p2pBorrowGrowthFactor =
                p2pGrowthFactor + (growthFactors.poolBorrowGrowthFactor - p2pGrowthFactor).percentMul(reserveFactor);
        } else {
            // The case poolSupplyGrowthFactor > poolBorrowGrowthFactor happens because someone has done a flashloan on Aave:
            // the peer-to-peer growth factors are set to the pool borrow growth factor.
            growthFactors.p2pSupplyGrowthFactor = growthFactors.poolBorrowGrowthFactor;
            growthFactors.p2pBorrowGrowthFactor = growthFactors.poolBorrowGrowthFactor;
        }
    }

    /// @notice Computes and returns the new peer-to-peer index of a market given its parameters.
    /// @param poolGrowthFactor The pool growth factor.
    /// @param p2pGrowthFactor The peer-to-peer growth factor.
    /// @param lastIndexes The last pool & peer-to-peer indexes.
    /// @param scaledDelta The last scaled peer-to-peer delta (pool unit).
    /// @param scaledP2PTotal The last scaled total peer-to-peer amount (P2P unit).
    /// @return newP2PIndex The updated peer-to-peer index (in ray).
    function computeP2PIndex(
        uint256 poolGrowthFactor,
        uint256 p2pGrowthFactor,
        Types.MarketSideIndexes256 memory lastIndexes,
        uint256 scaledDelta,
        uint256 scaledP2PTotal,
        uint256 proportionIdle
    ) internal pure returns (uint256) {
        if (scaledP2PTotal == 0 || (scaledDelta == 0 && proportionIdle == 0)) {
            return lastIndexes.p2pIndex.rayMul(p2pGrowthFactor);
        }

        uint256 proportionDelta = Math.min(
            scaledDelta.rayMul(lastIndexes.poolIndex).rayDivUp(scaledP2PTotal.rayMul(lastIndexes.p2pIndex)),
            WadRayMath.RAY - proportionIdle // To avoid proportionDelta + proportionIdle > 1 with rounding errors.
        ); // In ray.

        // Equivalent to:
        // lastP2PIndex * (
        // p2pGrowthFactor * (1 - proportionDelta - proportionIdle) +
        // poolGrowthFactor * proportionDelta +
        // idleGrowthFactor * proportionIdle)
        // Notice that the idleGrowthFactor is always equal to 1 (no interests accumulated).
        return lastIndexes.p2pIndex.rayMul(
            p2pGrowthFactor.rayMul(WadRayMath.RAY - proportionDelta - proportionIdle)
                + poolGrowthFactor.rayMul(proportionDelta) + proportionIdle
        );
    }
}

File 57 of 57 : WadRayMath.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

/**
 * @title WadRayMath library
 * @author Aave
 * @notice Provides functions to perform calculations with Wad and Ray units
 * @dev Provides mul and div function for wads (decimal numbers with 18 digits of precision) and rays (decimal numbers
 * with 27 digits of precision)
 * @dev Operations are rounded. If a value is >=.5, will be rounded up, otherwise rounded down.
 */
library WadRayMath {
  // HALF_WAD and HALF_RAY expressed with extended notation as constant with operations are not supported in Yul assembly
  uint256 internal constant WAD = 1e18;
  uint256 internal constant HALF_WAD = 0.5e18;

  uint256 internal constant RAY = 1e27;
  uint256 internal constant HALF_RAY = 0.5e27;

  uint256 internal constant WAD_RAY_RATIO = 1e9;

  /**
   * @dev Multiplies two wad, rounding half up to the nearest wad
   * @dev assembly optimized for improved gas savings, see https://twitter.com/transmissions11/status/1451131036377571328
   * @param a Wad
   * @param b Wad
   * @return c = a*b, in wad
   */
  function wadMul(uint256 a, uint256 b) internal pure returns (uint256 c) {
    // to avoid overflow, a <= (type(uint256).max - HALF_WAD) / b
    assembly {
      if iszero(or(iszero(b), iszero(gt(a, div(sub(not(0), HALF_WAD), b))))) {
        revert(0, 0)
      }

      c := div(add(mul(a, b), HALF_WAD), WAD)
    }
  }

  /**
   * @dev Divides two wad, rounding half up to the nearest wad
   * @dev assembly optimized for improved gas savings, see https://twitter.com/transmissions11/status/1451131036377571328
   * @param a Wad
   * @param b Wad
   * @return c = a/b, in wad
   */
  function wadDiv(uint256 a, uint256 b) internal pure returns (uint256 c) {
    // to avoid overflow, a <= (type(uint256).max - halfB) / WAD
    assembly {
      if or(iszero(b), iszero(iszero(gt(a, div(sub(not(0), div(b, 2)), WAD))))) {
        revert(0, 0)
      }

      c := div(add(mul(a, WAD), div(b, 2)), b)
    }
  }

  /**
   * @notice Multiplies two ray, rounding half up to the nearest ray
   * @dev assembly optimized for improved gas savings, see https://twitter.com/transmissions11/status/1451131036377571328
   * @param a Ray
   * @param b Ray
   * @return c = a raymul b
   */
  function rayMul(uint256 a, uint256 b) internal pure returns (uint256 c) {
    // to avoid overflow, a <= (type(uint256).max - HALF_RAY) / b
    assembly {
      if iszero(or(iszero(b), iszero(gt(a, div(sub(not(0), HALF_RAY), b))))) {
        revert(0, 0)
      }

      c := div(add(mul(a, b), HALF_RAY), RAY)
    }
  }

  /**
   * @notice Divides two ray, rounding half up to the nearest ray
   * @dev assembly optimized for improved gas savings, see https://twitter.com/transmissions11/status/1451131036377571328
   * @param a Ray
   * @param b Ray
   * @return c = a raydiv b
   */
  function rayDiv(uint256 a, uint256 b) internal pure returns (uint256 c) {
    // to avoid overflow, a <= (type(uint256).max - halfB) / RAY
    assembly {
      if or(iszero(b), iszero(iszero(gt(a, div(sub(not(0), div(b, 2)), RAY))))) {
        revert(0, 0)
      }

      c := div(add(mul(a, RAY), div(b, 2)), b)
    }
  }

  /**
   * @dev Casts ray down to wad
   * @dev assembly optimized for improved gas savings, see https://twitter.com/transmissions11/status/1451131036377571328
   * @param a Ray
   * @return b = a converted to wad, rounded half up to the nearest wad
   */
  function rayToWad(uint256 a) internal pure returns (uint256 b) {
    assembly {
      b := div(a, WAD_RAY_RATIO)
      let remainder := mod(a, WAD_RAY_RATIO)
      if iszero(lt(remainder, div(WAD_RAY_RATIO, 2))) {
        b := add(b, 1)
      }
    }
  }

  /**
   * @dev Converts wad up to ray
   * @dev assembly optimized for improved gas savings, see https://twitter.com/transmissions11/status/1451131036377571328
   * @param a Wad
   * @return b = a converted in ray
   */
  function wadToRay(uint256 a) internal pure returns (uint256 b) {
    // to avoid overflow, b/WAD_RAY_RATIO == a
    assembly {
      b := mul(a, WAD_RAY_RATIO)

      if iszero(eq(div(b, WAD_RAY_RATIO), a)) {
        revert(0, 0)
      }
    }
  }
}

Settings
{
  "remappings": [
    "@aave-v3-core/=lib/aave-v3-core/contracts/",
    "@aave-v3-periphery/=lib/aave-v3-periphery/contracts/",
    "@aave/core-v3/=lib/morpho-utils/lib/aave-v3-core/",
    "@ds-test/=lib/forge-std/lib/ds-test/src/",
    "@forge-std/=lib/forge-std/src/",
    "@morpho-data-structures/=lib/morpho-data-structures/src/",
    "@morpho-utils/=lib/morpho-utils/src/",
    "@openzeppelin-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "@openzeppelin/=lib/openzeppelin-contracts/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "@permit2/=lib/permit2/src/",
    "@solmate/=lib/solmate/src/",
    "aave-v3-core/=lib/aave-v3-core/",
    "aave-v3-periphery/=lib/aave-v3-periphery/contracts/",
    "config/=config/",
    "ds-test/=lib/forge-std/lib/ds-test/src/",
    "forge-gas-snapshot/=lib/permit2/lib/forge-gas-snapshot/src/",
    "forge-std/=lib/forge-std/src/",
    "morpho-data-structures/=lib/morpho-data-structures/",
    "morpho-utils/=lib/morpho-utils/src/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "permit2/=lib/permit2/",
    "solmate/=lib/permit2/lib/solmate/",
    "src/=src/",
    "test/=test/",
    "weird-erc20/=lib/solmate/lib/weird-erc20/src/"
  ],
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "ipfs",
    "appendCBOR": true
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "abi"
      ]
    }
  },
  "evmVersion": "paris",
  "libraries": {}
}

Contract Security Audit

Contract ABI

[{"inputs":[],"name":"AddressIsZero","type":"error"},{"inputs":[],"name":"AmountIsZero","type":"error"},{"inputs":[],"name":"AssetNotCollateralOnMorpho","type":"error"},{"inputs":[],"name":"BorrowIsPaused","type":"error"},{"inputs":[],"name":"BorrowNotEnabled","type":"error"},{"inputs":[],"name":"CollateralIsZero","type":"error"},{"inputs":[],"name":"DebtIsZero","type":"error"},{"inputs":[],"name":"ExceedsBorrowCap","type":"error"},{"inputs":[],"name":"InconsistentEMode","type":"error"},{"inputs":[],"name":"LiquidateBorrowIsPaused","type":"error"},{"inputs":[],"name":"LiquidateCollateralIsPaused","type":"error"},{"inputs":[],"name":"MarketNotCreated","type":"error"},{"inputs":[],"name":"PermissionDenied","type":"error"},{"inputs":[],"name":"RepayIsPaused","type":"error"},{"inputs":[],"name":"SentinelBorrowNotEnabled","type":"error"},{"inputs":[],"name":"SentinelLiquidateNotEnabled","type":"error"},{"inputs":[],"name":"SupplyCollateralIsPaused","type":"error"},{"inputs":[],"name":"SupplyIsPaused","type":"error"},{"inputs":[],"name":"SupplyIsZero","type":"error"},{"inputs":[],"name":"UnauthorizedBorrow","type":"error"},{"inputs":[],"name":"UnauthorizedLiquidate","type":"error"},{"inputs":[],"name":"UnauthorizedWithdraw","type":"error"},{"inputs":[],"name":"UnsafeCast","type":"error"},{"inputs":[],"name":"WithdrawCollateralIsPaused","type":"error"},{"inputs":[],"name":"WithdrawIsPaused","type":"error"},{"inputs":[],"name":"ZeroAddress","type":"error"},{"inputs":[],"name":"ZeroValue","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint8","name":"version","type":"uint8"}],"name":"Initialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"maxIterations","type":"uint256"}],"name":"borrowLogic","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"underlyingBorrowed","type":"address"},{"internalType":"address","name":"underlyingCollateral","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"address","name":"liquidator","type":"address"}],"name":"liquidateLogic","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"repayer","type":"address"},{"internalType":"address","name":"onBehalf","type":"address"}],"name":"repayLogic","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"onBehalf","type":"address"}],"name":"supplyCollateralLogic","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"onBehalf","type":"address"},{"internalType":"uint256","name":"maxIterations","type":"uint256"}],"name":"supplyLogic","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"supplier","type":"address"},{"internalType":"address","name":"receiver","type":"address"}],"name":"withdrawCollateralLogic","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"supplier","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"maxIterations","type":"uint256"}],"name":"withdrawLogic","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]

60808060405234620000c6576000549060ff8260081c1662000074575060ff8082161062000038575b6040516152809081620000cc8239f35b60ff90811916176000557f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb3847402498602060405160ff8152a13862000028565b62461bcd60e51b815260206004820152602760248201527f496e697469616c697a61626c653a20636f6e747261637420697320696e697469604482015266616c697a696e6760c81b6064820152608490fd5b600080fdfe6080604052600436101561001257600080fd5b60003560e01c80633ee5baf1146100d7578063402aec6d146100d25780636a35a80d146100cd578063715018a6146100c857806379ba5097146100c35780638da5cb5b146100be578063b6f2bf1c146100b9578063dc076372146100b4578063e30c3978146100af578063ea9a3409146100aa578063f2fde38b146100a55763ff3dfd75146100a057600080fd5b610832565b6107c1565b61074d565b610724565b610626565b610562565b610539565b610470565b610405565b61030a565b610228565b61012b565b6001600160a01b038116036100ed57565b600080fd5b60809060031901126100ed5760043561010a816100dc565b906024359060443561011b816100dc565b90606435610128816100dc565b90565b346100ed57610139366100f2565b9291610146848285611eeb565b9061016b61015385610cff565b9161015f83888861171c565b90818110908218021890565b9384156102165761021295610202946101e7916101c29185916101998a30846001600160a01b038a16610b60565b6101ba6101ae60a0546001600160801b031690565b6001600160801b031690565b918a8761315e565b609754600895860154606083015192961c6001600160a01b0390811691859116614b4b565b6097546040906001600160a01b031693015191515192614a7c565b6040519081529081906020820190565b0390f35b604051631744e6c560e31b8152600490fd5b346100ed57610236366100f2565b6102438184869496611aee565b60ff600782015460101c166102bb576008015460ff16156102a95782826102946102129661027361020296610cff565b946102898530846001600160a01b038816610b60565b8551519185856138e6565b6097546001600160a01b031692515192614a7c565b604051632697ed7760e21b8152600490fd5b6040516366d3f10d60e11b8152600490fd5b60a09060031901126100ed576004356102e5816100dc565b90602435906044356102f6816100dc565b90606435610303816100dc565b9060843590565b346100ed57610318366102cd565b919061032681838688611b5a565b9260ff600785015460181c166103e85761035b6103619161034688610cff565b9061035282898b611beb565b8486898b612f5c565b926111ba565b60408101519051106103d657610212946103c885936103ae610202966103a2600961039360975460018060a01b031690565b9201546001600160a01b031690565b85604085015192614c26565b60975483906060906001600160a01b031692015191614aed565b6001600160a01b0316610f93565b60405163629a8bfd60e11b8152600490fd5b60405163d8010e4b60e01b8152600490fd5b60009103126100ed57565b346100ed5760008060031936011261046d5761041f610a3a565b606580546001600160a01b031990811690915560338054918216905581906001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b80fd5b346100ed5760008060031936011261046d576065546001600160a01b0333818316036104e2576bffffffffffffffffffffffff60a01b8092166065556033549133908316176033553391167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08380a380f35b60405162461bcd60e51b815260206004820152602960248201527f4f776e61626c6532537465703a2063616c6c6572206973206e6f7420746865206044820152683732bb9037bbb732b960b91b6064820152608490fd5b346100ed5760003660031901126100ed576033546040516001600160a01b039091168152602090f35b346100ed57610570366102cd565b9061057d81848688611b5a565b9460ff600787015460201c16610614576105a561059982610cff565b9561015f878785611a36565b94851561060257610212966103ae6105e76103c89361020298876105df8c9a6105d36101ae60a05460801c90565b90818111908218021890565b918a8961366d565b6097549092906103a2906009906001600160a01b0316610393565b6040516306bf8d8160e51b8152600490fd5b60405163140b571960e11b8152600490fd5b346100ed57610634366100f2565b929061064284828585611f1a565b9061069b61064f84610cff565b51519461015f86610696856106768960018060a01b0316600052609b602052604060002090565b6001600160a01b039091166000908152600c909101602052604090205490565b6115e7565b93841561071257816106bc6106c19288670de0b6b3a7640000958989613a01565b611a90565b106107005761021293826103c8858094610202966106fa60096106eb60975460018060a01b031690565b9301546001600160a01b031690565b91614c26565b6040516323a7cebf60e21b8152600490fd5b604051635a78c58160e11b8152600490fd5b346100ed5760003660031901126100ed576065546040516001600160a01b039091168152602090f35b346100ed5761075b366102cd565b919390610769818584611aee565b9160ff600784015460081c166107af576101e76101c2610212976102029661079085610cff565b9586926107a88b30836001600160a01b038b16610b60565b8a8761216b565b6040516334ff8fc960e21b8152600490fd5b346100ed5760203660031901126100ed576004356107de816100dc565b6107e6610a3a565b606580546001600160a01b0319166001600160a01b039283169081179091556033549091167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e22700600080a3005b346100ed5760a03660031901126100ed5760043561084f816100dc565b6024359061085c826100dc565b60643591610869836100dc565b608435610875816100dc565b610880848385611f4a565b61088983610cff565b9161089381610cff565b9361089c610cbf565b956108a78183611fda565b87526108c66108b786838561171c565b61015f604435918a5190611ad3565b958615610216576108df60209783835151918787613d89565b979098019680885215610712578715610216576001600160a01b038381169690939061090d8a30898b610b60565b61091a8185898d866130f1565b9284888b519251519161092d938a613a01565b6097546001600160a01b03166001600160a01b0383166000908152609a602052604090208390600890810154901c6001600160a01b031660608601519161097393614b4b565b6097546001600160a01b031692604001519051519161099193614a7c565b6097546001600160a01b03166001600160a01b0384166000908152609a602052604090208490600901546001600160a01b03168851916109d093614c26565b85516109df9085858516610f93565b8551604080518981526001600160a01b0395909516602086015284015281169216907fc2c75a73164c2efcbb9f74bfa511cd0866489d90687831a7217b3dbeeb69708890606090a45160408051928352602083019190915290f35b6033546001600160a01b03163303610a4e57565b606460405162461bcd60e51b815260206004820152602060248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152fd5b634e487b7160e01b600052604160045260246000fd5b67ffffffffffffffff8111610abc57604052565b610a92565b6040810190811067ffffffffffffffff821117610abc57604052565b6060810190811067ffffffffffffffff821117610abc57604052565b60a0810190811067ffffffffffffffff821117610abc57604052565b610100810190811067ffffffffffffffff821117610abc57604052565b90601f8019910116810190811067ffffffffffffffff821117610abc57604052565b6040513d6000823e3d90fd5b6040516323b872dd60e01b60208083019182526001600160a01b038581166024850152861660448401526064808401889052835293959094939091610bc39190610bab608482610b32565b6000968791519082875af185516001143d1517161590565b610bcf575b5050505050565b610bd890610c69565b916e22d473030f116ddee9f6b43ac78ba391823b15610c6557604051631b63c28b60e11b81526001600160a01b03968716600482015291861660248301529285166044820152919093166064820152918290608490829084905af18015610c6057610c47575b80808080610bc8565b80610c54610c5a92610aa8565b806103fa565b38610c3e565b610b54565b8480fd5b6001600160a01b0390818111610c7d571690565b60405163c4bd89a960e01b8152600490fd5b60405190610c9c82610b15565b565b604051906101e0820182811067ffffffffffffffff821117610abc57604052565b60405190610ccc82610ac1565b60006020838281520152565b60405190610ce582610ac1565b81610cee610cbf565b81526020610cfa610cbf565b910152565b90610d08610cd8565b50610d1282610ec2565b9160018060a01b038091166000527fb7f1c1a7c27b63c53c9c4700bfc54d905ec2ef2b451c24e6426a7cc86fed7ed7602091609a83526040600020610d77610d5b875151614550565b82546001600160801b0319166001600160801b03909116178255565b610da7610d878588510151614550565b82546001600160801b031660809190911b6001600160801b031916178255565b610e78610e40600686890193610e12610dc1865151614550565b610de5600184019182906001600160801b03166001600160801b0319825416179055565b610df28a88510151614550565b81546001600160801b031660809190911b6001600160801b031916179055565b60088101805463ffffffff60a81b19164260a81b63ffffffff60a81b1617905501546001600160a01b031690565b918751958087519701519151908151910151916040519586951697859094939260609260808301968352602083015260408201520152565b0390a2565b90604051610e8a81610ac1565b602060018294805484520154910152565b90604051610ea881610ac1565b6020610cfa60028395610eba81610e7d565b855201610e7d565b90610ecb610cd8565b91610f83610eeb8260018060a01b0316600052609a602052604060002090565b91610f0c610ef8846145fe565b6097549092906001600160a01b0316614d00565b93908651602088019586515252610f7460026020845194015192885151875151600883015491610f3b84614622565b96610f44610c8f565b98895260208901526040880152606087015261ffff90818160c81c16608088015260d81c1660a086015201610e9b565b60c083015260e08201526142d1565b6020808694939451019251015252565b6044926040519263a9059cbb60e01b845260018060a01b03166004840152602483015260009283928380935af13d9015611029578060201461101b5715611013575b15610fdc57565b60405162461bcd60e51b815260206004820152600f60248201526e1514905394d1915497d19052531151608a1b6044820152606490fd5b506001610fd5565b50602081803e511515610fd5565b908181803efd5b6040519061103d82610add565b60006040838281528260208201520152565b6040519061105c82610af9565b60606080836000815260006020820152600060408201526000838201520152565b6040519061108a82610add565b8160008152600060208201526040610cfa61104f565b519061ffff821682036100ed57565b5190610c9c826100dc565b90602080838303126100ed57825167ffffffffffffffff938482116100ed57019260a0848403126100ed57604051936110f285610af9565b6110fb816110a0565b85526111088382016110a0565b83860152611118604082016110a0565b6040860152606081015161112b816100dc565b60608601526080810151908282116100ed570183601f820112156100ed578051918211610abc5760405193611169601f8401601f1916850186610b32565b8285528383830101116100ed5760005b8281106111925750509060009183010152608082015290565b8181018401518582018501528301611179565b908160209103126100ed5751610128816100dc565b906111c3611030565b916111cc61107d565b6098549160ff8360a01c1680611282575b5060405190631f94a27560e31b825260208260048160018060a01b038098165afa918215610c605761124d946112389361122b92600091611254575b50166001600160a01b03166020850152565b6001600160a01b03168252565b61124181611371565b6020860152845261142c565b6040830152565b611275915060203d811161127b575b61126d8183610b32565b8101906111a5565b38611219565b503d611263565b6097546112cb916000916112a6906001600160a01b03165b6001600160a01b031690565b6040518080958194636c6f6ae160e01b83526004830191909160ff6020820193169052565b03915afa908115610c60576000916112ea575b506040830152386111dd565b611305913d8091833e6112fd8183610b32565b8101906110ba565b386112de565b634e487b7160e01b600052601160045260246000fd5b60001981146113305760010190565b61130b565b634e487b7160e01b600052603260045260246000fd5b805182101561135f5760209160051b010190565b611335565b9190820180921161133057565b90600080928360018060a01b038251168152602090609c825260408120604051928381835491828152019284528184209184905b8282106114155750505050826113bc910383610b32565b925b815184101561140d57611400611406916113fa6113f4866113ef6113e28a8961134b565b516001600160a01b031690565b611513565b92611364565b97611364565b93611321565b92946113be565b949392505050565b8354855293840193600193840193909101906113a5565b90600091600060018060a01b038251168152602090609d825260408120916040518093849183825491828152019185528385209385905b8282106114b95750505061147992500383610b32565b935b81518510156114b3576114a76114ad916114a18561149c6113e28a8861134b565b61164a565b90611364565b94611321565b9361147b565b93505050565b855484526001958601958895509381019390910190611463565b8181029291811591840414171561133057565b81156114f0570490565b634e487b7160e01b600052601260045260246000fd5b9190820391821161133057565b6001600160a01b038082166000818152609a60205260408120600801549095949060ff16156115c8576115a092611596611569979361159084600c604061155d61159b988c6117d7565b9b9299919f909d610ec2565b945116935151958152609b60205220019060018060a01b0316600052602052604060002090565b546115e7565b6114d3565b6114e6565b906103e79180830292830403611330576115c26103e8610128930494856115d0565b936115d0565b505050508190565b908060001904821181026100ed5761271091020490565b908060001904821181026100ed57676765c793fa10079d601b1b91020490565b91908260209103126100ed576040516020810181811067ffffffffffffffff821117610abc5760405291518252565b906020828203126100ed5761012891611607565b60975460405163c44b11f760e01b81526001600160a01b03808416600483015290939290916020908590602490829086165afa8015610c60576116b36116d79361159692610128976000916116dc575b50602087015160408801516060015183169216856118fb565b959150926116d16116c382610ec2565b92516001600160a01b031690565b9061171c565b61170a565b6116fd915060203d8111611703575b6116f58183610b32565b810190611636565b3861169a565b503d6116eb565b9080156100ed57808204910615150190565b91611793919260018060a01b0316600052609b6020526020611788604060002094600a61176f846117628460078b019060018060a01b0316600052602052604060002090565b549601958651519061179f565b96019060018060a01b0316600052602052604060002090565b54915101519061179f565b81018091116113305790565b816b033b2e3c9fd0803ce7ffffff1904811182026100ed57676765c793fa10079d601b1b91026b033b2e3c9fd0803ce7ffffff010490565b6097549092906117ef906001600160a01b031661129a565b60405163c44b11f760e01b81526001600160a01b03851660048201529390602090859060249082905afa938415610c60576000946118c8575b50602083015161185c919085906040906001600160a01b031695018051606001519095906001600160a01b03165b926118fb565b809492918096519261ffff928385169283156118b6575050156118a95750505051916118a6611895602061189c611895875161ffff1690565b61ffff1690565b95015161ffff1690565b91565b90919492509360101c1691565b98506000975087969095509350505050565b61185c9194506118e59060203d8111611703576116f58183610b32565b9390611828565b908160209103126100ed575190565b9391929061191360ff855160301c16600a0a94611a12565b918280611a00575b611991575b5060405163b3596f0760e01b81526001600160a01b0395861660048201529460209186916024918391165afa938415610c6057600094611961575b50929190565b61198391945060203d811161198a575b61197b8183610b32565b8101906118ec565b923861195b565b503d611971565b60405163b3596f0760e01b81526001600160a01b039091166004820152602081806024810103816001600160a01b0386165afa908115610c60576000916119e2575b50801561192057945050929190565b6119fa915060203d811161198a5761197b8183610b32565b386119d3565b506001600160a01b038116151561191b565b60ff60985460a01c168015159182611a2957505090565b5160a81c60ff1614919050565b91611793919260018060a01b0316600052609b6020526020611a85604060002094600461176f611a7b8360018a019060018060a01b0316600052602052604060002090565b54865151906115e7565b5491510151906115e7565b611a99906111ba565b6040810151908115611acb57602001518160011c90670de0b6b3a7640000808319048211158402156100ed5702010490565b505060001990565b816113881904811182026100ed576127109102611388010490565b6001600160a01b039392909190841615611b485715611b36578216600052609a60205260406000209160098301541615611b2457565b6040516396e1352960e01b8152600490fd5b6040516310eb483f60e21b8152600490fd5b60405163867915ab60e01b8152600490fd5b9193926001600160a01b039290831615611b4857611b79918591611aee565b9216338114908115611ba0575b5015611b8e57565b604051630782484160e21b8152600490fd5b6000908152609e60209081526040808320338452909152902060ff9150541638611b86565b908160209103126100ed575180151581036100ed5790565b604d811161133057600a0a90565b609754611c00906001600160a01b031661129a565b6040805163c44b11f760e01b81526001600160a01b0384166004808301919091529591949093919290916020918290869060249082905afa948515610c6057600095611ecc575b5084516704000000000000001615611ebc576098548651635eb88d3d60e01b81526001600160a01b039084818b818587165afa908115610c6057600091611e9f575b501688848215159283611e43575b505050611e335760a01c60ff168015159081611e21575b50611e1157640fffffffff80865160501c16611ccf575b5050505050505050565b611cf8611cf1611d149360018060a01b0316600052609a602052604060002090565b958661482d565b955190611d0a60ff8360301c16611bdd565b9160501c166114d3565b600884810154919591611d339161129a91901c6001600160a01b031681565b9186519481868a816318160ddd60e01b978882525afa958615610c6057600096611de1575b50600a01548190611d739061129a906001600160a01b031681565b938989518096819382525afa8015610c6057611da595611da0946113f493600093611dc2575b5050611364565b611364565b11611db557808080808080611cc5565b5163e4c1095960e01b8152fd5b611dd9929350803d1061198a5761197b8183610b32565b903880611d99565b8291965061129a61129a600a611e06611d7394863d881161198a5761197b8183610b32565b999450505050611d58565b8551630179481360e21b81528790fd5b905060ff865160a81c16141538611cae565b8651634a6eaf1b60e11b81528890fd5b8a516349aa2e8160e01b815293509091839182905afa908115610c6057600091611e72575b5015888438611c97565b611e929150843d8611611e98575b611e8a8183610b32565b810190611bc5565b38611e68565b503d611e80565b611eb69150853d871161127b5761126d8183610b32565b38611c89565b855163769dfb1760e11b81528790fd5b611ee4919550823d8411611703576116f58183610b32565b9338611c47565b90611ef69291611aee565b9060ff600783015460301c16611f0857565b6040516308df075f60e01b8152600490fd5b90611f26939291611b5a565b9060ff600783015460281c16611f3857565b604051634deb62b160e01b8152600490fd5b6001600160a01b039081166000908152609a6020526040808220938316825290209192811615611b48576009830154811615908115611fcb575b50611b24576007015460381c60ff16611fb9576007015460401c60ff16611fa757565b60405163201817cf60e01b8152600490fd5b60405163087c64e760e31b8152600490fd5b60098301541615905038611f84565b6001600160a01b03166000908152609a60205260409020612002906007015460481c60ff1690565b61212e5761200f90611a90565b670de0b6b3a764000081101561211c57670d2f13f7789f00009081811015612043575b1161203d5761271090565b61138890565b609854612058906001600160a01b031661129a565b6040518091635eb88d3d60e01b825281600460209485935afa908115610c60576000916120ff575b506001600160a01b03168015159190826120ae575b50501561203257604051630bb3ca3360e01b8152600490fd5b604051633d2e907560e11b81529192508290829060049082905afa918215610c60576000926120e2575b5050153880612095565b6120f89250803d10611e9857611e8a8183610b32565b38806120d8565b6121169150823d841161127b5761126d8183610b32565b38612080565b60405163b4f0a90360e01b8152600490fd5b5061271090565b604051906080820182811067ffffffffffffffff821117610abc5760405260006060838281528260208201528260408201520152565b959493866122a361226c6122597f11adb3570ba55fd255b1f04252ca0071ae6639c86d4fd69e7c1bf1688afb493f959697986121a5612135565b5087908a6121b1612135565b9d8e9861222a6121d38360018060a01b0316600052609a602052604060002090565b9360046121f28560018060a01b0316600052609b602052604060002090565b6001600160a01b038316600090815260018201602052604090208e905b549052019060018060a01b0316600052602052604060002090565b5499602081019a8b528c61224560ff60078701541615151590565b6122a8575b50925050505190515191613af9565b90818c5260408c01528451908985612648565b93849052808a526040805196875260208701919091528501929092526001600160a01b039081169581169416929081906060820190565b0390a4565b849650836122de87946060946122d16122fd996002966122f69a600460208c01515193016149a3565b9690910195865283613c1c565b509290986122ed848651611364565b809552016140b8565b8851611364565b87523886818f8c61224a565b92919260009260608501948551156124cd576040938482019384519060c08401986123348a51151590565b156124c3578391929890985b89518210806124ba575b156124ab5761235981846124db565b6001600160a01b038181169290919083156124995792869594926123f68f98848f9561245c988e938e6123c96123d69460e0880151936123af8260018093019060018060a01b0316600052602052604060002090565b5493019060018060a01b0316600052602052604060002090565b54906020870151926150e1565b60a085015194519d51909d9193916001600160a01b031690151594615193565b908b6124056080820151151590565b1561246457517f91f5b273b6b78b55e25fb4e5f27a0e7eeccf8a71e105b7676e5c56ec64179bf093612454916001600160a01b03165b1695519283928360209093929193604081019481520152565b0390a3611321565b909192612340565b517f82784f5c4393a9d1646b5c21c845e4366c3160cf42b957716877bbefeb45705c93612454916001600160a01b031661243b565b9a505050509750505050509350510391565b97509750505050509350510391565b5080151561234a565b8192989098612340565b509350505050600090600090565b9060028201549081156125ac5761251882918060011c178060021c178060041c178060081c178060101c178060201c178060401c178060801c1790565b191660018119011690816125855761255691508060011c178060021c178060041c178060081c178060101c178060201c178060401c178060801c1790565b8060011c18600052602052604060002060008052602052610128600160406000200160018060a01b0390541690565b5060005260205260406000206000805260205260018060a01b036001604060002001541690565b505050600090565b6125e3908060011c178060021c178060041c178060081c178060101c178060201c178060401c178060801c1790565b8060011c1890565b816b019d971e4fe8401e740000001904811182026100ed57676765c793fa10079d601b1b91026b019d971e4fe8401e74000000010490565b8160011c90676765c793fa10079d601b1b808319048211158402156100ed5702010490565b9061268193929160018060a01b03809116600052609a6020526009604060002001541690609b6020526040600020906003820192612685565b9091565b90919594956001851115612723575b600187111561271a575b6001600160a01b03831660009081526001828101602090815260408084205492880190915290912054889493909287918591908484036126fd575b505050505082036126ec575b5050509190565b6126f5926127c8565b3883816126e5565b6127109461270b9184612c96565b6128a9565b38858382806126d9565b6000965061269e565b60009450612694565b9190929394969560018611156127bf575b60018811156127b6575b6001600160a01b0384166000908152600183810160209081526040808420549289019091529091205489959490939192918891869190858403612798575b50505050505082036126ec575050509190565b6127ab956127a69184612c96565b612938565b388086848280612785565b60009750612747565b6000955061273d565b906001600160a01b03811615612897576001820183612816836127fd81859060018060a01b0316600052602052604060002090565b54939060018060a01b0316600052602052604060002090565b55801561286c57612826906125b4565b92801561286157612836906125b4565b91838303612845575b50505050565b6128536128589483836129e2565b612afa565b3880808061283f565b50610c9c92916129e2565b5082156128855761287f610c9c936125b4565b91612afa565b604051637c946ed760e01b8152600490fd5b60405163d92e233d60e01b8152600490fd5b906001600160a01b038116156128975760018201836128de836127fd81859060018060a01b0316600052602052604060002090565b55801561291f576128ee906125b4565b928015612861576128fe906125b4565b9183830361290c5750505050565b61291a6128589483836129e2565b612b88565b50821561288557612932610c9c936125b4565b91612b88565b919291906001600160a01b03811615612897576001820184612970836127fd81859060018060a01b0316600052602052604060002090565b5580156129c957612980906125b4565b9380156129bc57612990906125b4565b9184830361299f575050505050565b6129ad6129b29583836129e2565b612bef565b3880808080610bc8565b5090610c9c9392506129e2565b508315612885576129dc610c9c946125b4565b91612bef565b906020908360005282825260006001612acf6040832093612a1581869060018060a01b0316600052602052604060002090565b94604051612a2281610ac1565b848060a01b038581895416988984520154169788910152612a7a8785612a5a89859060018060a01b0316600052602052604060002090565b0180546001600160a01b0319166001600160a01b03909216919091179055565b6001600160a01b0387166000908152602082905260409020612ab89087905b80546001600160a01b0319166001600160a01b03909216919091179055565b9060018060a01b0316600052602052604060002090565b8281550155159081612af1575b50612ae5575050565b60020190198154169055565b90501538612adc565b908260005281602052612b74604060002060008052806020526001612a5a848260406000200193612b51848060a01b038654169788969060018060a01b03166bffffffffffffffffffffffff60a01b825416179055565b6001600160a01b0385166000908152602082905260409020612ab8908390612a99565b15612b7d575050565b600201908154179055565b6000838152602082815260408083208380529182905290912080546001600160a01b038581166001600160a01b03198316179092559293921691612b74918391612a99915b612ab8826001612a5a87859060018060a01b0316600052602052604060002090565b9192612c079184600052836020526040600020612c0f565b612b7d575050565b909115612c5b576000808052602082905260409020600190810180546001600160a01b038581166001600160a01b03198316179092551692612c5792849291612a5a91612b51565b1590565b600080805260208290526040902080546001600160a01b038481166001600160a01b03198316179092551691612c57918391612a9991612bcd565b60a254919290916001600160a01b031680612cb15750505050565b803b156100ed5760405163236608ef60e21b81526001600160a01b03938416600482015293909216602484015260448301526000908290606490829084905af18015610c6057612d04575b80808061283f565b612d0d90610aa8565b38612cfc565b6001600160a01b039081166000818152609a60209081526040808320600890810154609b90935292209497969295612d5e959394909360098101936006909101928a92911c16612685565b939091821580612dbb575b15612d9757612d8d612d929260018060a01b0316600052609d602052604060002090565b612ea7565b509190565b612db6612d929260018060a01b0316600052609d602052604060002090565b612df9565b508415612d69565b805482101561135f5760005260206000200190600090565b91612df59183549060031b91821b91600019901b19161790565b9055565b60018101908260005281602052604060002054156000146125ac57805468010000000000000000811015610abc57612e52612e3b826001879401855584612dc3565b819391549060031b91821b91600019901b19161790565b90555491600052602052604060002055600190565b8054908115612e915760001991820191612e818383612dc3565b909182549160031b1b1916905555565b634e487b7160e01b600052603160045260246000fd5b6001810191806000528260205260406000205492831515600014612f53576000199284840190858211611330578054948501948511611330576000958583612efb94612f0a9803612f10575b505050612e67565b90600052602052604060002090565b55600190565b612f3a612f3491612f24612f4a9487612dc3565b90549060031b1c92839187612dc3565b90612ddb565b8590600052602052604060002090565b55388080612ef3565b50505050600090565b937ff99275e3db7a3400181f0bd088002bba02b833be9187bccc88fbbc79fb52f2f191929394969596612f8d612135565b506122a361304a61303786612fa0612135565b9b8c9686612fc08160018060a01b0316600052609a602052604060002090565b916130048d600a612fe38560018060a01b0316600052609b602052604060002090565b6001600160a01b038316600090815260078201602052604090208e9061220f565b5499602081019a8b528c61301f60ff60078701541615151590565b613086575b5091505060209150519101515191613af9565b90818c5260608c01528451908885612d13565b93849052808a5260408051338152602081019790975286015260608501929092526001600160a01b039081169581169416929081906080820190565b60409650836130df6130c8889588956130bb6130ab6002976122f69b6130e59d6147aa565b979098019789515190878a614a38565b9c9091019b8c5284613cb9565b509390996130d7858251611364565b809152611364565b9261414e565b87523886818f8c613024565b94936131297f7b417e520d2b905fc5a1689d29d329358dd55efc60ed115aa165b0a2b64232c691613120612135565b508584896132d7565b80516020808301516040805196875291860192909252840152956001600160a01b0390811694811693169180606081016122a3565b9594613129907f7b417e520d2b905fc5a1689d29d329358dd55efc60ed115aa165b0a2b64232c692939495613191612135565b5086858a613506565b51906001600160801b03821682036100ed57565b519064ffffffffff821682036100ed57565b6101e0818303126100ed576131dd6131d6610c9e565b9282611607565b82526131eb6020820161319a565b60208301526131fc6040820161319a565b604083015261320d6060820161319a565b606083015261321e6080820161319a565b608083015261322f60a0820161319a565b60a083015261324060c082016131ae565b60c083015261325160e082016110a0565b60e08301526101006132648183016110af565b908301526101206132768183016110af565b908301526101406132888183016110af565b9083015261016061329a8183016110af565b908301526101806132ac81830161319a565b908301526101a06132be81830161319a565b908301526132d06101c080920161319a565b9082015290565b9392909160006132e5612135565b9561333e83600a6133088460018060a01b0316600052609b602052604060002090565b6001600160a01b038316600090815260078201602052604090205b548b52019060018060a01b0316600052602052604060002090565b546020880190815261339061335d895197602088019889515191613b21565b90818c9893985260608c0152613386845161337d60208c5101518a6135c8565b80820391110290565b9182855285612d13565b90915287528215610bc8576001600160a01b0381166000908152609a6020526040902094613401856133ce6002890196845151908660048c016149a3565b6133f960608d01956133e1838851611364565b875261337d60058d0193602085549351015190612623565b905588614858565b600787015490919060ff16156134e2575b50609754613428906001600160a01b031661129a565b6040516335ea6a7560e01b81526001600160a01b0384166004820152966101e0918290899060249082905afa978815610c6057610c9c986134ad948661347d948b9461348e976000956134b3575b505061466f565b91909460408b0195808752856135f0565b936134a761349d868351611506565b88519086896148f1565b51611364565b926141d2565b6134d3929550803d106134db575b6134cb8183610b32565b8101906131c0565b923880613476565b503d6134c1565b92506134f16134fe9183613b66565b8551909592939190611364565b905238613412565b9493919092613513612135565b9561353683600a6133088460018060a01b0316600052609b602052604060002090565b546020880190815261355561335d895197602088019889515191613b21565b90915287528215610bc8576001600160a01b0381166000908152609a6020526040902094613593856133ce6002890196845151908660048c016149a3565b600787015490919060ff16156135b95750609754613428906001600160a01b031661129a565b926134f1906134fe9284613c1c565b676765c793fa10079d601b1b600019918183850119048111158402156100ed57028201010490565b6001600160a01b03166000818152609b60209081526040808320609a90925290912061366994929391929190613625906145b9565b6040519461363286610b15565b855260208501526040840152606083015260006080830152600560a0830152600160c0830152600660e08301526003810190612309565b5090565b95946136a9907f6a9c828ef646db99cc7a20bbfb02fdf8f7dcc183400a28daab4968e47b9a21e0929394956136a0612135565b5085858a6136ea565b80516020808301516040805133815292830196909652948101919091526060810193909352956001600160a01b0390811694811693169180608081016122a3565b9391949290946136f8612135565b9461375161373d83600461371e8560018060a01b0316600052609b602052604060002090565b6001600160a01b03831660009081526001820160205260409020613323565b549760208801988952875186515191613b21565b8899929952604088015261379c61377a8360018060a01b0316600052609a602052604060002090565b9361378f835161337d60208a5101518d6135c8565b9081845289519085612648565b9091528652861561385c5761384a6137ee876137be610c9c98999a85876147aa565b9290966137d460028801988a515190888b614a38565b92909460408501936137e7818651611364565b8552611364565b600788015490949060ff166138505761380d6138199260609289613cb9565b95909201528251611364565b90525b6134a761382f60608c0192835187613865565b9561383b878451611506565b86600460208c0151930161494a565b92614252565b9150916060015261381c565b50505050915090565b6001600160a01b03166000818152609b60209081526040808320609a9092529091206136699492939192919061389f906145da565b6145da565b604051946138ac86610b15565b855260208501526040840152606083015260016080830152600260a0830152600160c0830152600660e08301526006600982019101612309565b6001600160a01b039081166000818152609b602090815260408083208589168452600c0191829052909120919694959394939161394390549488600052609a60205261393d86866009604060002001541686612c96565b866139d1565b840180941161133057837f4d1fc6dc36972a1eeab2351fae829d06c827d7ee429880dbf762ec00b805fb2f9461398e846122a3949060018060a01b0316600052602052604060002090565b556001600160a01b0383166000908152609c602052604090206139b2908990612df9565b5083604051948594169716958360209093929193604081019481520152565b744f3a68dbc8f03f243baf513267aa9a3ee524f8e02981108202156100ed57676765c793fa10079d601b1b020490565b826060917fb49f4cffa4b6674963440a1fb6cb419c233a9341280f44d8543571eca130657793949560018060a01b0392838093169889600052609b602052613ab0600c60406000200191613a90613a6a85859060018060a01b0316600052602052604060002090565b54918d600052609a602052613a8a83896009604060002001541688612c96565b8b6135c8565b8082039111029283929060018060a01b0316600052602052604060002090565b558015613ad0575b604051963388526020880152604087015216941692a4565b6001600160a01b0388166000908152609c60205260409020613af3908a90612ea7565b50613ab8565b9092918115613b1957613b0c90826139d1565b8301809311611330579190565b505060009190565b9192908015613b5d57613b3484826125eb565b80841084821802189384840393841161133057613b5190856135c8565b92939280820391110290565b91925060009190565b9190918215613c13576001600160a01b0381166000818152609b60205260409020613bf992906006810190613bb79061389a906009905b019360018060a01b0316600052609a602052604060002090565b60405193613bc485610b15565b845260208401528560408401526000606084015260016080840152600260a0840152600060c0840152600360e0840152612309565b919092838103908111611330578261133057929160000390565b50600090600090565b9290928315613cb2576001600160a01b0381166000818152609b60205260409020613c9892906006810190613c579061389a90600990613b9d565b60405193613c6485610b15565b8452602084015286604084015285606084015260016080840152600260a0840152600060c0840152600360e0840152612309565b818592950391821161133057830392831161133057929190565b5060009190565b929091928215613d56576001600160a01b03166000818152609b60209081526040808320609a909252909120613d3a929190613cf4906145b9565b60405192613d0184610b15565b8352602083015284604083015285606083015260006080830152600560a0830152600060c0830152600360e08301526003810190612309565b81849592950393841161133057613d5091611506565b91929190565b509091600091565b60405190613d6b82610af9565b60006080838281528260208201528260408201528260608201520152565b9194929093613d96613d5e565b91613d9f61104f565b916098549160ff8360a01c1680614048575b50604051631f94a27560e31b81526020936001600160a01b03919085908290600490829086165afa908115610c605760009161402b575b5060975491169490613e02906001600160a01b031661129a565b60405163c44b11f760e01b8082526001600160a01b038a166004830152989196919086816024818b5afa978815610c6057613e699a8d9260009a614006575b506040519081526001600160a01b039092166004830152909987918b91829081906024820190565b03915afa988915610c6057600099613fd3575b50829189613ea2613eca936060613ec3999897019a846118568d5160018060a01b031690565b8a8d0190815260608d019182529a51909a986001600160a01b039091169150565b918d6118fb565b60408a0190815260808a01918252909990969115613fc557505060400151613ef59061ffff16611895565b8652898551613f03916114d3565b8451613f0e916114d3565b83518851613f1b916114d3565b613f24916114e6565b8651613f2f91611ad3565b6001600160a01b03998a166000908152609b6020908152604080832094909c168252600c9093019092529890205490613f67916115e7565b808811613f77575b505050505050565b613fb9969850613fb194959750613fab9291613f9982613fa1939a51906114d3565b9051906114d3565b92519051906114d3565b906114e6565b90519061409c565b91388080808080613f6f565b61ffff925051901c16613ef5565b613ea2995091613eca91613ff9613ec397969594893d8b11611703576116f58183610b32565b9a50915091929394613e7c565b899392919a5061402290843d8611611703576116f58183610b32565b99909192613e41565b6140429150853d871161127b5761126d8183610b32565b38613de8565b60975491945061406891600091906112a6906001600160a01b031661129a565b03915afa908115610c6057600091614083575b509238613db1565b614096913d8091833e6112fd8183610b32565b3861407b565b8160011c90612710808319048211158402156100ed5702010490565b9190929493948015614144579060206140dd6140e69382808a01519951015190612623565b96015190612623565b9060038101908154928301809311611330578260019255019081549285840180941161133057918390556040805193845260208401919091526001600160a01b039091169160008051602061522b8339815191529181908101610e78565b5060009450505050565b929493949190918015614144579060206140dd6141749382808a519a0151015190612623565b9160018101908154938401809411611330578360039255019081549085820180921161133057918190556040805193845260208401919091526001600160a01b039091169160008051602061522b8339815191529181908101610e78565b9291938215610bc857614225600360008051602061522b83398151915295602084519401519361420d600183019960208b5493015190612623565b80820391110280985501936020855493015190612623565b808211910302918290556040805194855260208501929092526001600160a01b0316929081908101610e78565b92938415610bc8576142a4600160008051602061522b83398151915295602084015193519361428c60038301976020895493015190612623565b80820391110280965501956020875493015190612623565b808211910302938490556040805194855260208501929092526001600160a01b0316929081908101610e78565b9061012860408301516143016060850151918551519260208701938451519060a08901519260808a01519461434c565b9061432b825160208401519680519760c08201988951519160e060208451940151940151946144a1565b94602060606040850151940151925191510151916020835193015193614402565b95949261436661436f929593614360612135565b98612623565b94858852612623565b604086018181529381106143f157508451918351612710938385039385818402936113881990838887840304861189029204108302179111176100ed576143ea956143df6143d9856143d4986113886143d4976114a19a880201010498899586611506565b611ad3565b83611506565b60208a015251611506565b6060830152565b925050508060208401526060830152565b9291909184158015614490575b61447e57906144326020614428614438948451906125eb565b92015180966125eb565b906135c8565b90676765c793fa10079d601b1b918083108382180218908183039283116113305761446c92614466916125eb565b926125eb565b810180911161133057610128916125eb565b506101289350602091925001516125eb565b508015801561440f5750600161440f565b9392949190948315801561453f575b61452b579061443260206144c96144d4948451906125eb565b9201948551906125eb565b93676765c793fa10079d601b1b9482860386811161133057818110908218021892519383860395808711611330578385019003958611611330576114a16145259461446661012898611da0956125eb565b906125eb565b5090506101289392506020915001516125eb565b50801580156144b0575082156144b0565b6001600160801b0390818111614564571690565b60405162461bcd60e51b815260206004820152602760248201527f53616665436173743a2076616c756520646f65736e27742066697420696e20316044820152663238206269747360c81b6064820152608490fd5b906145c2610cbf565b91546001600160801b038116835260801c6020830152565b9060016145e5610cbf565b9201546001600160801b038116835260801c6020830152565b9061461b61460a610cd8565b92614614816145b9565b84526145da565b6020830152565b600b81015490811561466857806003614642920154905460801c906125eb565b801561466857614651916135c8565b676765c793fa10079d601b1b818110908218021890565b5050600090565b94939290919261469a825151640fffffffff61469060ff8360301c16611bdd565b9160741c166114d3565b90811561479d576009870154600491906020906146c19061129a906001600160a01b031681565b60405163b1bf962d60e01b815293849182905afa918215610c60576147059461337d93836146fc9360009261477b575b506114a19192614de2565b905151906125eb565b91828111156147715761476a600b61473e857fd37f8c5b1028a8745d109e601653cbf41563b7e941e8affa1a9c4a7a38abd97194611506565b96019261474c878554611364565b938490556040519384526001600160a01b0316929081906020820190565b0390a29190565b9350600092915050565b6114a192506147979060203d811161198a5761197b8183610b32565b916146f1565b5050505090915090600090565b92919092811561482157600b0180549384156148165760207fd37f8c5b1028a8745d109e601653cbf41563b7e941e8affa1a9c4a7a38abd97191868510858818028718968780820391110280945560405193845260018060a01b031692a2828103908111611330579190565b505090915090600090565b50509050600090600090565b9061337d90600461484d60206005860154930151926020840151906125eb565b9301549051906125eb565b919080156125ac576148be6148b1614870848661482d565b60038601546148976148898751926020840151906125eb565b9160028901549051906125eb565b808203911102600b87015480820391110280820391110290565b8290818110908218021890565b9182156148ea576148e3600561012895019161337d60208085549301510151866139d1565b9055611506565b5091505090565b9091801561283f578154935161490691612623565b8301809311611330578290556040519182526001600160a01b0316907f1cf8705a784a46d32023f3694b5e8149137d563085a870fde2f54a6cc5c59df790602090a2565b9091801561283f578154935161495f91612623565b8301809311611330578290556040519182526001600160a01b0316907f8113f59ef078158acce9021327489b70d6ab15d0c107c36455c3505248648df690602090a2565b909392938215614a2b5781548015614a1f577f8113f59ef078158acce9021327489b70d6ab15d0c107c36455c3505248648df691816149f66149e78960209561179f565b808810888218021898896139d1565b808211910302938490556040519384526001600160a01b031692a2828103908111611330579190565b50505090915090600090565b5050509050600090600090565b909392938215614a2b5781548015614a1f577f1cf8705a784a46d32023f3694b5e8149137d563085a870fde2f54a6cc5c59df791816149f66149e78960209561179f565b909192614a899084612623565b15614ae8576001600160a01b03908116803b156100ed5760009283608492604051968795869463617ba03760e01b865216600485015260248401523060448401528160648401525af18015610c6057614adf5750565b610c9c90610aa8565b505050565b8215614ae8576001600160a01b03908116803b156100ed576000928360a492604051968795869463a415bcad60e01b86521660048501526024840152600260448401528160648401523060848401525af18015610c6057614adf5750565b92918215908115614bc7575b50614ae85760405163573ade8160e01b81526001600160a01b039182166004820152602481019290925260026044830152306064830152909160209183916084918391600091165af18015610c6057614bad5750565b614bc49060203d811161198a5761197b8183610b32565b50565b604051630ed1279f60e11b81523060048201529150602090829060249082906001600160a01b03165afa908115610c6057600091614c08575b501538614b57565b614c20915060203d811161198a5761197b8183610b32565b38614c00565b9092801561283f576040516370a0823160e01b81523060048201526020946001600160a01b039491939186908590602490829089165afa938415610c6057614cba9587956000928391614ce3575b50604051631a4ca37b60e21b81526001600160a01b0390941660048501528086189581119590950290941860248301523060448301529094859384929183906064820190565b0393165af18015610c6057614ccd575050565b81614bc492903d1061198a5761197b8183610b32565b614cfa9150873d891161198a5761197b8183610b32565b38614c74565b60405163d15e005360e01b81526001600160a01b0383811660048301529190911692916020918281602481885afa918215610c6057614d72928492600091614d9f575b5060405163386497fd60e01b81526001600160a01b039092166004830152959092839190829081906024820190565b03915afa918215610c6057600092614d8957505090565b6101289250803d1061198a5761197b8183610b32565b614db69150833d851161198a5761197b8183610b32565b38614d43565b91908260809103126100ed578151916020810151916101286060604084015193016131ae565b90614df4825161ffff905160401c1690565b8015614fb557610120830151600490608090614e1a9061129a906001600160a01b031681565b604051630f2ee86760e31b815292839182905afa8015610c605760009081808192614f7d575b6004929394506020614e6261129a61129a6101408c015160018060a01b031690565b60405163b1bf962d60e01b815294859182905afa928315610c6057600093614f5d575b50602087015151614e9690846125eb565b926060890151614eac906001600160801b031690565b6001600160801b0316614ebe916125eb565b9360c0890151614ed29064ffffffffff1690565b64ffffffffff1690614ee392614fce565b614eec916125eb565b92614ef691611364565b90614f0091611506565b90614f0a91611506565b8015614f4357610180614f346101289594614f2b611da0956101ae95611ad3565b90515190612623565b9301516001600160801b031690565b5050506101800151610128906001600160801b03166101ae565b614f7691935060203d811161198a5761197b8183610b32565b9138614e85565b50505050614fa360049160803d8111614fae575b614f9b8183610b32565b810190614dbc565b919350839290614e40565b503d614f91565b50506101800151610128906001600160801b03166101ae565b9064ffffffffff1680830392808411611330571461507f576000198201916000600282111561507557506150516001198201915b600661504a66038882915c40006150198780615090565b046115966301e1338096611596615040896150348c87615090565b04946115968d8a6114d3565b60011c9a876114d3565b04936114d3565b04676765c793fa10079d601b1b9081018091116113305761012892611da091611364565b6150519091615002565b5050676765c793fa10079d601b1b90565b816b019d971e4fe8401e740000001904811115821517156100ed57676765c793fa10079d601b1b91026b019d971e4fe8401e74000000010490565b634e487b7160e01b600052605160045260246000fd5b949290919480600314615145576006036150cb5760208101906151058251876125eb565b80861086821802189081860395861161133057615123905182612623565b830180931161133057615137915190612623565b840393841161133057929190565b506151518151836125eb565b80851085821802188085039485116113305761516e825182612623565b830392831161133057602061518592015190612623565b840180941161133057929190565b95949195939093806005146151ee576002036150cb576001600160a01b039384166000818152609a60209081526040808320600890810154609b90935292209296612d5e969360098101936006909101928b92911c1661272c565b5091909294612681949560018060a01b03809116600052609a6020526009604060002001541690609b602052604060002090600382019261272c56fe29c7258ad2a828aee0fb295826bf2a731d38a5ae377f284addeb97838d657c2da26469706673582212201137663bcc6b00c2b83e4e7ecd3f254cf570a713c77ffa3cc4811f6624cc627b64736f6c63430008130033

Deployed Bytecode

0x6080604052600436101561001257600080fd5b60003560e01c80633ee5baf1146100d7578063402aec6d146100d25780636a35a80d146100cd578063715018a6146100c857806379ba5097146100c35780638da5cb5b146100be578063b6f2bf1c146100b9578063dc076372146100b4578063e30c3978146100af578063ea9a3409146100aa578063f2fde38b146100a55763ff3dfd75146100a057600080fd5b610832565b6107c1565b61074d565b610724565b610626565b610562565b610539565b610470565b610405565b61030a565b610228565b61012b565b6001600160a01b038116036100ed57565b600080fd5b60809060031901126100ed5760043561010a816100dc565b906024359060443561011b816100dc565b90606435610128816100dc565b90565b346100ed57610139366100f2565b9291610146848285611eeb565b9061016b61015385610cff565b9161015f83888861171c565b90818110908218021890565b9384156102165761021295610202946101e7916101c29185916101998a30846001600160a01b038a16610b60565b6101ba6101ae60a0546001600160801b031690565b6001600160801b031690565b918a8761315e565b609754600895860154606083015192961c6001600160a01b0390811691859116614b4b565b6097546040906001600160a01b031693015191515192614a7c565b6040519081529081906020820190565b0390f35b604051631744e6c560e31b8152600490fd5b346100ed57610236366100f2565b6102438184869496611aee565b60ff600782015460101c166102bb576008015460ff16156102a95782826102946102129661027361020296610cff565b946102898530846001600160a01b038816610b60565b8551519185856138e6565b6097546001600160a01b031692515192614a7c565b604051632697ed7760e21b8152600490fd5b6040516366d3f10d60e11b8152600490fd5b60a09060031901126100ed576004356102e5816100dc565b90602435906044356102f6816100dc565b90606435610303816100dc565b9060843590565b346100ed57610318366102cd565b919061032681838688611b5a565b9260ff600785015460181c166103e85761035b6103619161034688610cff565b9061035282898b611beb565b8486898b612f5c565b926111ba565b60408101519051106103d657610212946103c885936103ae610202966103a2600961039360975460018060a01b031690565b9201546001600160a01b031690565b85604085015192614c26565b60975483906060906001600160a01b031692015191614aed565b6001600160a01b0316610f93565b60405163629a8bfd60e11b8152600490fd5b60405163d8010e4b60e01b8152600490fd5b60009103126100ed57565b346100ed5760008060031936011261046d5761041f610a3a565b606580546001600160a01b031990811690915560338054918216905581906001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b80fd5b346100ed5760008060031936011261046d576065546001600160a01b0333818316036104e2576bffffffffffffffffffffffff60a01b8092166065556033549133908316176033553391167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08380a380f35b60405162461bcd60e51b815260206004820152602960248201527f4f776e61626c6532537465703a2063616c6c6572206973206e6f7420746865206044820152683732bb9037bbb732b960b91b6064820152608490fd5b346100ed5760003660031901126100ed576033546040516001600160a01b039091168152602090f35b346100ed57610570366102cd565b9061057d81848688611b5a565b9460ff600787015460201c16610614576105a561059982610cff565b9561015f878785611a36565b94851561060257610212966103ae6105e76103c89361020298876105df8c9a6105d36101ae60a05460801c90565b90818111908218021890565b918a8961366d565b6097549092906103a2906009906001600160a01b0316610393565b6040516306bf8d8160e51b8152600490fd5b60405163140b571960e11b8152600490fd5b346100ed57610634366100f2565b929061064284828585611f1a565b9061069b61064f84610cff565b51519461015f86610696856106768960018060a01b0316600052609b602052604060002090565b6001600160a01b039091166000908152600c909101602052604090205490565b6115e7565b93841561071257816106bc6106c19288670de0b6b3a7640000958989613a01565b611a90565b106107005761021293826103c8858094610202966106fa60096106eb60975460018060a01b031690565b9301546001600160a01b031690565b91614c26565b6040516323a7cebf60e21b8152600490fd5b604051635a78c58160e11b8152600490fd5b346100ed5760003660031901126100ed576065546040516001600160a01b039091168152602090f35b346100ed5761075b366102cd565b919390610769818584611aee565b9160ff600784015460081c166107af576101e76101c2610212976102029661079085610cff565b9586926107a88b30836001600160a01b038b16610b60565b8a8761216b565b6040516334ff8fc960e21b8152600490fd5b346100ed5760203660031901126100ed576004356107de816100dc565b6107e6610a3a565b606580546001600160a01b0319166001600160a01b039283169081179091556033549091167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e22700600080a3005b346100ed5760a03660031901126100ed5760043561084f816100dc565b6024359061085c826100dc565b60643591610869836100dc565b608435610875816100dc565b610880848385611f4a565b61088983610cff565b9161089381610cff565b9361089c610cbf565b956108a78183611fda565b87526108c66108b786838561171c565b61015f604435918a5190611ad3565b958615610216576108df60209783835151918787613d89565b979098019680885215610712578715610216576001600160a01b038381169690939061090d8a30898b610b60565b61091a8185898d866130f1565b9284888b519251519161092d938a613a01565b6097546001600160a01b03166001600160a01b0383166000908152609a602052604090208390600890810154901c6001600160a01b031660608601519161097393614b4b565b6097546001600160a01b031692604001519051519161099193614a7c565b6097546001600160a01b03166001600160a01b0384166000908152609a602052604090208490600901546001600160a01b03168851916109d093614c26565b85516109df9085858516610f93565b8551604080518981526001600160a01b0395909516602086015284015281169216907fc2c75a73164c2efcbb9f74bfa511cd0866489d90687831a7217b3dbeeb69708890606090a45160408051928352602083019190915290f35b6033546001600160a01b03163303610a4e57565b606460405162461bcd60e51b815260206004820152602060248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152fd5b634e487b7160e01b600052604160045260246000fd5b67ffffffffffffffff8111610abc57604052565b610a92565b6040810190811067ffffffffffffffff821117610abc57604052565b6060810190811067ffffffffffffffff821117610abc57604052565b60a0810190811067ffffffffffffffff821117610abc57604052565b610100810190811067ffffffffffffffff821117610abc57604052565b90601f8019910116810190811067ffffffffffffffff821117610abc57604052565b6040513d6000823e3d90fd5b6040516323b872dd60e01b60208083019182526001600160a01b038581166024850152861660448401526064808401889052835293959094939091610bc39190610bab608482610b32565b6000968791519082875af185516001143d1517161590565b610bcf575b5050505050565b610bd890610c69565b916e22d473030f116ddee9f6b43ac78ba391823b15610c6557604051631b63c28b60e11b81526001600160a01b03968716600482015291861660248301529285166044820152919093166064820152918290608490829084905af18015610c6057610c47575b80808080610bc8565b80610c54610c5a92610aa8565b806103fa565b38610c3e565b610b54565b8480fd5b6001600160a01b0390818111610c7d571690565b60405163c4bd89a960e01b8152600490fd5b60405190610c9c82610b15565b565b604051906101e0820182811067ffffffffffffffff821117610abc57604052565b60405190610ccc82610ac1565b60006020838281520152565b60405190610ce582610ac1565b81610cee610cbf565b81526020610cfa610cbf565b910152565b90610d08610cd8565b50610d1282610ec2565b9160018060a01b038091166000527fb7f1c1a7c27b63c53c9c4700bfc54d905ec2ef2b451c24e6426a7cc86fed7ed7602091609a83526040600020610d77610d5b875151614550565b82546001600160801b0319166001600160801b03909116178255565b610da7610d878588510151614550565b82546001600160801b031660809190911b6001600160801b031916178255565b610e78610e40600686890193610e12610dc1865151614550565b610de5600184019182906001600160801b03166001600160801b0319825416179055565b610df28a88510151614550565b81546001600160801b031660809190911b6001600160801b031916179055565b60088101805463ffffffff60a81b19164260a81b63ffffffff60a81b1617905501546001600160a01b031690565b918751958087519701519151908151910151916040519586951697859094939260609260808301968352602083015260408201520152565b0390a2565b90604051610e8a81610ac1565b602060018294805484520154910152565b90604051610ea881610ac1565b6020610cfa60028395610eba81610e7d565b855201610e7d565b90610ecb610cd8565b91610f83610eeb8260018060a01b0316600052609a602052604060002090565b91610f0c610ef8846145fe565b6097549092906001600160a01b0316614d00565b93908651602088019586515252610f7460026020845194015192885151875151600883015491610f3b84614622565b96610f44610c8f565b98895260208901526040880152606087015261ffff90818160c81c16608088015260d81c1660a086015201610e9b565b60c083015260e08201526142d1565b6020808694939451019251015252565b6044926040519263a9059cbb60e01b845260018060a01b03166004840152602483015260009283928380935af13d9015611029578060201461101b5715611013575b15610fdc57565b60405162461bcd60e51b815260206004820152600f60248201526e1514905394d1915497d19052531151608a1b6044820152606490fd5b506001610fd5565b50602081803e511515610fd5565b908181803efd5b6040519061103d82610add565b60006040838281528260208201520152565b6040519061105c82610af9565b60606080836000815260006020820152600060408201526000838201520152565b6040519061108a82610add565b8160008152600060208201526040610cfa61104f565b519061ffff821682036100ed57565b5190610c9c826100dc565b90602080838303126100ed57825167ffffffffffffffff938482116100ed57019260a0848403126100ed57604051936110f285610af9565b6110fb816110a0565b85526111088382016110a0565b83860152611118604082016110a0565b6040860152606081015161112b816100dc565b60608601526080810151908282116100ed570183601f820112156100ed578051918211610abc5760405193611169601f8401601f1916850186610b32565b8285528383830101116100ed5760005b8281106111925750509060009183010152608082015290565b8181018401518582018501528301611179565b908160209103126100ed5751610128816100dc565b906111c3611030565b916111cc61107d565b6098549160ff8360a01c1680611282575b5060405190631f94a27560e31b825260208260048160018060a01b038098165afa918215610c605761124d946112389361122b92600091611254575b50166001600160a01b03166020850152565b6001600160a01b03168252565b61124181611371565b6020860152845261142c565b6040830152565b611275915060203d811161127b575b61126d8183610b32565b8101906111a5565b38611219565b503d611263565b6097546112cb916000916112a6906001600160a01b03165b6001600160a01b031690565b6040518080958194636c6f6ae160e01b83526004830191909160ff6020820193169052565b03915afa908115610c60576000916112ea575b506040830152386111dd565b611305913d8091833e6112fd8183610b32565b8101906110ba565b386112de565b634e487b7160e01b600052601160045260246000fd5b60001981146113305760010190565b61130b565b634e487b7160e01b600052603260045260246000fd5b805182101561135f5760209160051b010190565b611335565b9190820180921161133057565b90600080928360018060a01b038251168152602090609c825260408120604051928381835491828152019284528184209184905b8282106114155750505050826113bc910383610b32565b925b815184101561140d57611400611406916113fa6113f4866113ef6113e28a8961134b565b516001600160a01b031690565b611513565b92611364565b97611364565b93611321565b92946113be565b949392505050565b8354855293840193600193840193909101906113a5565b90600091600060018060a01b038251168152602090609d825260408120916040518093849183825491828152019185528385209385905b8282106114b95750505061147992500383610b32565b935b81518510156114b3576114a76114ad916114a18561149c6113e28a8861134b565b61164a565b90611364565b94611321565b9361147b565b93505050565b855484526001958601958895509381019390910190611463565b8181029291811591840414171561133057565b81156114f0570490565b634e487b7160e01b600052601260045260246000fd5b9190820391821161133057565b6001600160a01b038082166000818152609a60205260408120600801549095949060ff16156115c8576115a092611596611569979361159084600c604061155d61159b988c6117d7565b9b9299919f909d610ec2565b945116935151958152609b60205220019060018060a01b0316600052602052604060002090565b546115e7565b6114d3565b6114e6565b906103e79180830292830403611330576115c26103e8610128930494856115d0565b936115d0565b505050508190565b908060001904821181026100ed5761271091020490565b908060001904821181026100ed57676765c793fa10079d601b1b91020490565b91908260209103126100ed576040516020810181811067ffffffffffffffff821117610abc5760405291518252565b906020828203126100ed5761012891611607565b60975460405163c44b11f760e01b81526001600160a01b03808416600483015290939290916020908590602490829086165afa8015610c60576116b36116d79361159692610128976000916116dc575b50602087015160408801516060015183169216856118fb565b959150926116d16116c382610ec2565b92516001600160a01b031690565b9061171c565b61170a565b6116fd915060203d8111611703575b6116f58183610b32565b810190611636565b3861169a565b503d6116eb565b9080156100ed57808204910615150190565b91611793919260018060a01b0316600052609b6020526020611788604060002094600a61176f846117628460078b019060018060a01b0316600052602052604060002090565b549601958651519061179f565b96019060018060a01b0316600052602052604060002090565b54915101519061179f565b81018091116113305790565b816b033b2e3c9fd0803ce7ffffff1904811182026100ed57676765c793fa10079d601b1b91026b033b2e3c9fd0803ce7ffffff010490565b6097549092906117ef906001600160a01b031661129a565b60405163c44b11f760e01b81526001600160a01b03851660048201529390602090859060249082905afa938415610c60576000946118c8575b50602083015161185c919085906040906001600160a01b031695018051606001519095906001600160a01b03165b926118fb565b809492918096519261ffff928385169283156118b6575050156118a95750505051916118a6611895602061189c611895875161ffff1690565b61ffff1690565b95015161ffff1690565b91565b90919492509360101c1691565b98506000975087969095509350505050565b61185c9194506118e59060203d8111611703576116f58183610b32565b9390611828565b908160209103126100ed575190565b9391929061191360ff855160301c16600a0a94611a12565b918280611a00575b611991575b5060405163b3596f0760e01b81526001600160a01b0395861660048201529460209186916024918391165afa938415610c6057600094611961575b50929190565b61198391945060203d811161198a575b61197b8183610b32565b8101906118ec565b923861195b565b503d611971565b60405163b3596f0760e01b81526001600160a01b039091166004820152602081806024810103816001600160a01b0386165afa908115610c60576000916119e2575b50801561192057945050929190565b6119fa915060203d811161198a5761197b8183610b32565b386119d3565b506001600160a01b038116151561191b565b60ff60985460a01c168015159182611a2957505090565b5160a81c60ff1614919050565b91611793919260018060a01b0316600052609b6020526020611a85604060002094600461176f611a7b8360018a019060018060a01b0316600052602052604060002090565b54865151906115e7565b5491510151906115e7565b611a99906111ba565b6040810151908115611acb57602001518160011c90670de0b6b3a7640000808319048211158402156100ed5702010490565b505060001990565b816113881904811182026100ed576127109102611388010490565b6001600160a01b039392909190841615611b485715611b36578216600052609a60205260406000209160098301541615611b2457565b6040516396e1352960e01b8152600490fd5b6040516310eb483f60e21b8152600490fd5b60405163867915ab60e01b8152600490fd5b9193926001600160a01b039290831615611b4857611b79918591611aee565b9216338114908115611ba0575b5015611b8e57565b604051630782484160e21b8152600490fd5b6000908152609e60209081526040808320338452909152902060ff9150541638611b86565b908160209103126100ed575180151581036100ed5790565b604d811161133057600a0a90565b609754611c00906001600160a01b031661129a565b6040805163c44b11f760e01b81526001600160a01b0384166004808301919091529591949093919290916020918290869060249082905afa948515610c6057600095611ecc575b5084516704000000000000001615611ebc576098548651635eb88d3d60e01b81526001600160a01b039084818b818587165afa908115610c6057600091611e9f575b501688848215159283611e43575b505050611e335760a01c60ff168015159081611e21575b50611e1157640fffffffff80865160501c16611ccf575b5050505050505050565b611cf8611cf1611d149360018060a01b0316600052609a602052604060002090565b958661482d565b955190611d0a60ff8360301c16611bdd565b9160501c166114d3565b600884810154919591611d339161129a91901c6001600160a01b031681565b9186519481868a816318160ddd60e01b978882525afa958615610c6057600096611de1575b50600a01548190611d739061129a906001600160a01b031681565b938989518096819382525afa8015610c6057611da595611da0946113f493600093611dc2575b5050611364565b611364565b11611db557808080808080611cc5565b5163e4c1095960e01b8152fd5b611dd9929350803d1061198a5761197b8183610b32565b903880611d99565b8291965061129a61129a600a611e06611d7394863d881161198a5761197b8183610b32565b999450505050611d58565b8551630179481360e21b81528790fd5b905060ff865160a81c16141538611cae565b8651634a6eaf1b60e11b81528890fd5b8a516349aa2e8160e01b815293509091839182905afa908115610c6057600091611e72575b5015888438611c97565b611e929150843d8611611e98575b611e8a8183610b32565b810190611bc5565b38611e68565b503d611e80565b611eb69150853d871161127b5761126d8183610b32565b38611c89565b855163769dfb1760e11b81528790fd5b611ee4919550823d8411611703576116f58183610b32565b9338611c47565b90611ef69291611aee565b9060ff600783015460301c16611f0857565b6040516308df075f60e01b8152600490fd5b90611f26939291611b5a565b9060ff600783015460281c16611f3857565b604051634deb62b160e01b8152600490fd5b6001600160a01b039081166000908152609a6020526040808220938316825290209192811615611b48576009830154811615908115611fcb575b50611b24576007015460381c60ff16611fb9576007015460401c60ff16611fa757565b60405163201817cf60e01b8152600490fd5b60405163087c64e760e31b8152600490fd5b60098301541615905038611f84565b6001600160a01b03166000908152609a60205260409020612002906007015460481c60ff1690565b61212e5761200f90611a90565b670de0b6b3a764000081101561211c57670d2f13f7789f00009081811015612043575b1161203d5761271090565b61138890565b609854612058906001600160a01b031661129a565b6040518091635eb88d3d60e01b825281600460209485935afa908115610c60576000916120ff575b506001600160a01b03168015159190826120ae575b50501561203257604051630bb3ca3360e01b8152600490fd5b604051633d2e907560e11b81529192508290829060049082905afa918215610c60576000926120e2575b5050153880612095565b6120f89250803d10611e9857611e8a8183610b32565b38806120d8565b6121169150823d841161127b5761126d8183610b32565b38612080565b60405163b4f0a90360e01b8152600490fd5b5061271090565b604051906080820182811067ffffffffffffffff821117610abc5760405260006060838281528260208201528260408201520152565b959493866122a361226c6122597f11adb3570ba55fd255b1f04252ca0071ae6639c86d4fd69e7c1bf1688afb493f959697986121a5612135565b5087908a6121b1612135565b9d8e9861222a6121d38360018060a01b0316600052609a602052604060002090565b9360046121f28560018060a01b0316600052609b602052604060002090565b6001600160a01b038316600090815260018201602052604090208e905b549052019060018060a01b0316600052602052604060002090565b5499602081019a8b528c61224560ff60078701541615151590565b6122a8575b50925050505190515191613af9565b90818c5260408c01528451908985612648565b93849052808a526040805196875260208701919091528501929092526001600160a01b039081169581169416929081906060820190565b0390a4565b849650836122de87946060946122d16122fd996002966122f69a600460208c01515193016149a3565b9690910195865283613c1c565b509290986122ed848651611364565b809552016140b8565b8851611364565b87523886818f8c61224a565b92919260009260608501948551156124cd576040938482019384519060c08401986123348a51151590565b156124c3578391929890985b89518210806124ba575b156124ab5761235981846124db565b6001600160a01b038181169290919083156124995792869594926123f68f98848f9561245c988e938e6123c96123d69460e0880151936123af8260018093019060018060a01b0316600052602052604060002090565b5493019060018060a01b0316600052602052604060002090565b54906020870151926150e1565b60a085015194519d51909d9193916001600160a01b031690151594615193565b908b6124056080820151151590565b1561246457517f91f5b273b6b78b55e25fb4e5f27a0e7eeccf8a71e105b7676e5c56ec64179bf093612454916001600160a01b03165b1695519283928360209093929193604081019481520152565b0390a3611321565b909192612340565b517f82784f5c4393a9d1646b5c21c845e4366c3160cf42b957716877bbefeb45705c93612454916001600160a01b031661243b565b9a505050509750505050509350510391565b97509750505050509350510391565b5080151561234a565b8192989098612340565b509350505050600090600090565b9060028201549081156125ac5761251882918060011c178060021c178060041c178060081c178060101c178060201c178060401c178060801c1790565b191660018119011690816125855761255691508060011c178060021c178060041c178060081c178060101c178060201c178060401c178060801c1790565b8060011c18600052602052604060002060008052602052610128600160406000200160018060a01b0390541690565b5060005260205260406000206000805260205260018060a01b036001604060002001541690565b505050600090565b6125e3908060011c178060021c178060041c178060081c178060101c178060201c178060401c178060801c1790565b8060011c1890565b816b019d971e4fe8401e740000001904811182026100ed57676765c793fa10079d601b1b91026b019d971e4fe8401e74000000010490565b8160011c90676765c793fa10079d601b1b808319048211158402156100ed5702010490565b9061268193929160018060a01b03809116600052609a6020526009604060002001541690609b6020526040600020906003820192612685565b9091565b90919594956001851115612723575b600187111561271a575b6001600160a01b03831660009081526001828101602090815260408084205492880190915290912054889493909287918591908484036126fd575b505050505082036126ec575b5050509190565b6126f5926127c8565b3883816126e5565b6127109461270b9184612c96565b6128a9565b38858382806126d9565b6000965061269e565b60009450612694565b9190929394969560018611156127bf575b60018811156127b6575b6001600160a01b0384166000908152600183810160209081526040808420549289019091529091205489959490939192918891869190858403612798575b50505050505082036126ec575050509190565b6127ab956127a69184612c96565b612938565b388086848280612785565b60009750612747565b6000955061273d565b906001600160a01b03811615612897576001820183612816836127fd81859060018060a01b0316600052602052604060002090565b54939060018060a01b0316600052602052604060002090565b55801561286c57612826906125b4565b92801561286157612836906125b4565b91838303612845575b50505050565b6128536128589483836129e2565b612afa565b3880808061283f565b50610c9c92916129e2565b5082156128855761287f610c9c936125b4565b91612afa565b604051637c946ed760e01b8152600490fd5b60405163d92e233d60e01b8152600490fd5b906001600160a01b038116156128975760018201836128de836127fd81859060018060a01b0316600052602052604060002090565b55801561291f576128ee906125b4565b928015612861576128fe906125b4565b9183830361290c5750505050565b61291a6128589483836129e2565b612b88565b50821561288557612932610c9c936125b4565b91612b88565b919291906001600160a01b03811615612897576001820184612970836127fd81859060018060a01b0316600052602052604060002090565b5580156129c957612980906125b4565b9380156129bc57612990906125b4565b9184830361299f575050505050565b6129ad6129b29583836129e2565b612bef565b3880808080610bc8565b5090610c9c9392506129e2565b508315612885576129dc610c9c946125b4565b91612bef565b906020908360005282825260006001612acf6040832093612a1581869060018060a01b0316600052602052604060002090565b94604051612a2281610ac1565b848060a01b038581895416988984520154169788910152612a7a8785612a5a89859060018060a01b0316600052602052604060002090565b0180546001600160a01b0319166001600160a01b03909216919091179055565b6001600160a01b0387166000908152602082905260409020612ab89087905b80546001600160a01b0319166001600160a01b03909216919091179055565b9060018060a01b0316600052602052604060002090565b8281550155159081612af1575b50612ae5575050565b60020190198154169055565b90501538612adc565b908260005281602052612b74604060002060008052806020526001612a5a848260406000200193612b51848060a01b038654169788969060018060a01b03166bffffffffffffffffffffffff60a01b825416179055565b6001600160a01b0385166000908152602082905260409020612ab8908390612a99565b15612b7d575050565b600201908154179055565b6000838152602082815260408083208380529182905290912080546001600160a01b038581166001600160a01b03198316179092559293921691612b74918391612a99915b612ab8826001612a5a87859060018060a01b0316600052602052604060002090565b9192612c079184600052836020526040600020612c0f565b612b7d575050565b909115612c5b576000808052602082905260409020600190810180546001600160a01b038581166001600160a01b03198316179092551692612c5792849291612a5a91612b51565b1590565b600080805260208290526040902080546001600160a01b038481166001600160a01b03198316179092551691612c57918391612a9991612bcd565b60a254919290916001600160a01b031680612cb15750505050565b803b156100ed5760405163236608ef60e21b81526001600160a01b03938416600482015293909216602484015260448301526000908290606490829084905af18015610c6057612d04575b80808061283f565b612d0d90610aa8565b38612cfc565b6001600160a01b039081166000818152609a60209081526040808320600890810154609b90935292209497969295612d5e959394909360098101936006909101928a92911c16612685565b939091821580612dbb575b15612d9757612d8d612d929260018060a01b0316600052609d602052604060002090565b612ea7565b509190565b612db6612d929260018060a01b0316600052609d602052604060002090565b612df9565b508415612d69565b805482101561135f5760005260206000200190600090565b91612df59183549060031b91821b91600019901b19161790565b9055565b60018101908260005281602052604060002054156000146125ac57805468010000000000000000811015610abc57612e52612e3b826001879401855584612dc3565b819391549060031b91821b91600019901b19161790565b90555491600052602052604060002055600190565b8054908115612e915760001991820191612e818383612dc3565b909182549160031b1b1916905555565b634e487b7160e01b600052603160045260246000fd5b6001810191806000528260205260406000205492831515600014612f53576000199284840190858211611330578054948501948511611330576000958583612efb94612f0a9803612f10575b505050612e67565b90600052602052604060002090565b55600190565b612f3a612f3491612f24612f4a9487612dc3565b90549060031b1c92839187612dc3565b90612ddb565b8590600052602052604060002090565b55388080612ef3565b50505050600090565b937ff99275e3db7a3400181f0bd088002bba02b833be9187bccc88fbbc79fb52f2f191929394969596612f8d612135565b506122a361304a61303786612fa0612135565b9b8c9686612fc08160018060a01b0316600052609a602052604060002090565b916130048d600a612fe38560018060a01b0316600052609b602052604060002090565b6001600160a01b038316600090815260078201602052604090208e9061220f565b5499602081019a8b528c61301f60ff60078701541615151590565b613086575b5091505060209150519101515191613af9565b90818c5260608c01528451908885612d13565b93849052808a5260408051338152602081019790975286015260608501929092526001600160a01b039081169581169416929081906080820190565b60409650836130df6130c8889588956130bb6130ab6002976122f69b6130e59d6147aa565b979098019789515190878a614a38565b9c9091019b8c5284613cb9565b509390996130d7858251611364565b809152611364565b9261414e565b87523886818f8c613024565b94936131297f7b417e520d2b905fc5a1689d29d329358dd55efc60ed115aa165b0a2b64232c691613120612135565b508584896132d7565b80516020808301516040805196875291860192909252840152956001600160a01b0390811694811693169180606081016122a3565b9594613129907f7b417e520d2b905fc5a1689d29d329358dd55efc60ed115aa165b0a2b64232c692939495613191612135565b5086858a613506565b51906001600160801b03821682036100ed57565b519064ffffffffff821682036100ed57565b6101e0818303126100ed576131dd6131d6610c9e565b9282611607565b82526131eb6020820161319a565b60208301526131fc6040820161319a565b604083015261320d6060820161319a565b606083015261321e6080820161319a565b608083015261322f60a0820161319a565b60a083015261324060c082016131ae565b60c083015261325160e082016110a0565b60e08301526101006132648183016110af565b908301526101206132768183016110af565b908301526101406132888183016110af565b9083015261016061329a8183016110af565b908301526101806132ac81830161319a565b908301526101a06132be81830161319a565b908301526132d06101c080920161319a565b9082015290565b9392909160006132e5612135565b9561333e83600a6133088460018060a01b0316600052609b602052604060002090565b6001600160a01b038316600090815260078201602052604090205b548b52019060018060a01b0316600052602052604060002090565b546020880190815261339061335d895197602088019889515191613b21565b90818c9893985260608c0152613386845161337d60208c5101518a6135c8565b80820391110290565b9182855285612d13565b90915287528215610bc8576001600160a01b0381166000908152609a6020526040902094613401856133ce6002890196845151908660048c016149a3565b6133f960608d01956133e1838851611364565b875261337d60058d0193602085549351015190612623565b905588614858565b600787015490919060ff16156134e2575b50609754613428906001600160a01b031661129a565b6040516335ea6a7560e01b81526001600160a01b0384166004820152966101e0918290899060249082905afa978815610c6057610c9c986134ad948661347d948b9461348e976000956134b3575b505061466f565b91909460408b0195808752856135f0565b936134a761349d868351611506565b88519086896148f1565b51611364565b926141d2565b6134d3929550803d106134db575b6134cb8183610b32565b8101906131c0565b923880613476565b503d6134c1565b92506134f16134fe9183613b66565b8551909592939190611364565b905238613412565b9493919092613513612135565b9561353683600a6133088460018060a01b0316600052609b602052604060002090565b546020880190815261355561335d895197602088019889515191613b21565b90915287528215610bc8576001600160a01b0381166000908152609a6020526040902094613593856133ce6002890196845151908660048c016149a3565b600787015490919060ff16156135b95750609754613428906001600160a01b031661129a565b926134f1906134fe9284613c1c565b676765c793fa10079d601b1b600019918183850119048111158402156100ed57028201010490565b6001600160a01b03166000818152609b60209081526040808320609a90925290912061366994929391929190613625906145b9565b6040519461363286610b15565b855260208501526040840152606083015260006080830152600560a0830152600160c0830152600660e08301526003810190612309565b5090565b95946136a9907f6a9c828ef646db99cc7a20bbfb02fdf8f7dcc183400a28daab4968e47b9a21e0929394956136a0612135565b5085858a6136ea565b80516020808301516040805133815292830196909652948101919091526060810193909352956001600160a01b0390811694811693169180608081016122a3565b9391949290946136f8612135565b9461375161373d83600461371e8560018060a01b0316600052609b602052604060002090565b6001600160a01b03831660009081526001820160205260409020613323565b549760208801988952875186515191613b21565b8899929952604088015261379c61377a8360018060a01b0316600052609a602052604060002090565b9361378f835161337d60208a5101518d6135c8565b9081845289519085612648565b9091528652861561385c5761384a6137ee876137be610c9c98999a85876147aa565b9290966137d460028801988a515190888b614a38565b92909460408501936137e7818651611364565b8552611364565b600788015490949060ff166138505761380d6138199260609289613cb9565b95909201528251611364565b90525b6134a761382f60608c0192835187613865565b9561383b878451611506565b86600460208c0151930161494a565b92614252565b9150916060015261381c565b50505050915090565b6001600160a01b03166000818152609b60209081526040808320609a9092529091206136699492939192919061389f906145da565b6145da565b604051946138ac86610b15565b855260208501526040840152606083015260016080830152600260a0830152600160c0830152600660e08301526006600982019101612309565b6001600160a01b039081166000818152609b602090815260408083208589168452600c0191829052909120919694959394939161394390549488600052609a60205261393d86866009604060002001541686612c96565b866139d1565b840180941161133057837f4d1fc6dc36972a1eeab2351fae829d06c827d7ee429880dbf762ec00b805fb2f9461398e846122a3949060018060a01b0316600052602052604060002090565b556001600160a01b0383166000908152609c602052604090206139b2908990612df9565b5083604051948594169716958360209093929193604081019481520152565b744f3a68dbc8f03f243baf513267aa9a3ee524f8e02981108202156100ed57676765c793fa10079d601b1b020490565b826060917fb49f4cffa4b6674963440a1fb6cb419c233a9341280f44d8543571eca130657793949560018060a01b0392838093169889600052609b602052613ab0600c60406000200191613a90613a6a85859060018060a01b0316600052602052604060002090565b54918d600052609a602052613a8a83896009604060002001541688612c96565b8b6135c8565b8082039111029283929060018060a01b0316600052602052604060002090565b558015613ad0575b604051963388526020880152604087015216941692a4565b6001600160a01b0388166000908152609c60205260409020613af3908a90612ea7565b50613ab8565b9092918115613b1957613b0c90826139d1565b8301809311611330579190565b505060009190565b9192908015613b5d57613b3484826125eb565b80841084821802189384840393841161133057613b5190856135c8565b92939280820391110290565b91925060009190565b9190918215613c13576001600160a01b0381166000818152609b60205260409020613bf992906006810190613bb79061389a906009905b019360018060a01b0316600052609a602052604060002090565b60405193613bc485610b15565b845260208401528560408401526000606084015260016080840152600260a0840152600060c0840152600360e0840152612309565b919092838103908111611330578261133057929160000390565b50600090600090565b9290928315613cb2576001600160a01b0381166000818152609b60205260409020613c9892906006810190613c579061389a90600990613b9d565b60405193613c6485610b15565b8452602084015286604084015285606084015260016080840152600260a0840152600060c0840152600360e0840152612309565b818592950391821161133057830392831161133057929190565b5060009190565b929091928215613d56576001600160a01b03166000818152609b60209081526040808320609a909252909120613d3a929190613cf4906145b9565b60405192613d0184610b15565b8352602083015284604083015285606083015260006080830152600560a0830152600060c0830152600360e08301526003810190612309565b81849592950393841161133057613d5091611506565b91929190565b509091600091565b60405190613d6b82610af9565b60006080838281528260208201528260408201528260608201520152565b9194929093613d96613d5e565b91613d9f61104f565b916098549160ff8360a01c1680614048575b50604051631f94a27560e31b81526020936001600160a01b03919085908290600490829086165afa908115610c605760009161402b575b5060975491169490613e02906001600160a01b031661129a565b60405163c44b11f760e01b8082526001600160a01b038a166004830152989196919086816024818b5afa978815610c6057613e699a8d9260009a614006575b506040519081526001600160a01b039092166004830152909987918b91829081906024820190565b03915afa988915610c6057600099613fd3575b50829189613ea2613eca936060613ec3999897019a846118568d5160018060a01b031690565b8a8d0190815260608d019182529a51909a986001600160a01b039091169150565b918d6118fb565b60408a0190815260808a01918252909990969115613fc557505060400151613ef59061ffff16611895565b8652898551613f03916114d3565b8451613f0e916114d3565b83518851613f1b916114d3565b613f24916114e6565b8651613f2f91611ad3565b6001600160a01b03998a166000908152609b6020908152604080832094909c168252600c9093019092529890205490613f67916115e7565b808811613f77575b505050505050565b613fb9969850613fb194959750613fab9291613f9982613fa1939a51906114d3565b9051906114d3565b92519051906114d3565b906114e6565b90519061409c565b91388080808080613f6f565b61ffff925051901c16613ef5565b613ea2995091613eca91613ff9613ec397969594893d8b11611703576116f58183610b32565b9a50915091929394613e7c565b899392919a5061402290843d8611611703576116f58183610b32565b99909192613e41565b6140429150853d871161127b5761126d8183610b32565b38613de8565b60975491945061406891600091906112a6906001600160a01b031661129a565b03915afa908115610c6057600091614083575b509238613db1565b614096913d8091833e6112fd8183610b32565b3861407b565b8160011c90612710808319048211158402156100ed5702010490565b9190929493948015614144579060206140dd6140e69382808a01519951015190612623565b96015190612623565b9060038101908154928301809311611330578260019255019081549285840180941161133057918390556040805193845260208401919091526001600160a01b039091169160008051602061522b8339815191529181908101610e78565b5060009450505050565b929493949190918015614144579060206140dd6141749382808a519a0151015190612623565b9160018101908154938401809411611330578360039255019081549085820180921161133057918190556040805193845260208401919091526001600160a01b039091169160008051602061522b8339815191529181908101610e78565b9291938215610bc857614225600360008051602061522b83398151915295602084519401519361420d600183019960208b5493015190612623565b80820391110280985501936020855493015190612623565b808211910302918290556040805194855260208501929092526001600160a01b0316929081908101610e78565b92938415610bc8576142a4600160008051602061522b83398151915295602084015193519361428c60038301976020895493015190612623565b80820391110280965501956020875493015190612623565b808211910302938490556040805194855260208501929092526001600160a01b0316929081908101610e78565b9061012860408301516143016060850151918551519260208701938451519060a08901519260808a01519461434c565b9061432b825160208401519680519760c08201988951519160e060208451940151940151946144a1565b94602060606040850151940151925191510151916020835193015193614402565b95949261436661436f929593614360612135565b98612623565b94858852612623565b604086018181529381106143f157508451918351612710938385039385818402936113881990838887840304861189029204108302179111176100ed576143ea956143df6143d9856143d4986113886143d4976114a19a880201010498899586611506565b611ad3565b83611506565b60208a015251611506565b6060830152565b925050508060208401526060830152565b9291909184158015614490575b61447e57906144326020614428614438948451906125eb565b92015180966125eb565b906135c8565b90676765c793fa10079d601b1b918083108382180218908183039283116113305761446c92614466916125eb565b926125eb565b810180911161133057610128916125eb565b506101289350602091925001516125eb565b508015801561440f5750600161440f565b9392949190948315801561453f575b61452b579061443260206144c96144d4948451906125eb565b9201948551906125eb565b93676765c793fa10079d601b1b9482860386811161133057818110908218021892519383860395808711611330578385019003958611611330576114a16145259461446661012898611da0956125eb565b906125eb565b5090506101289392506020915001516125eb565b50801580156144b0575082156144b0565b6001600160801b0390818111614564571690565b60405162461bcd60e51b815260206004820152602760248201527f53616665436173743a2076616c756520646f65736e27742066697420696e20316044820152663238206269747360c81b6064820152608490fd5b906145c2610cbf565b91546001600160801b038116835260801c6020830152565b9060016145e5610cbf565b9201546001600160801b038116835260801c6020830152565b9061461b61460a610cd8565b92614614816145b9565b84526145da565b6020830152565b600b81015490811561466857806003614642920154905460801c906125eb565b801561466857614651916135c8565b676765c793fa10079d601b1b818110908218021890565b5050600090565b94939290919261469a825151640fffffffff61469060ff8360301c16611bdd565b9160741c166114d3565b90811561479d576009870154600491906020906146c19061129a906001600160a01b031681565b60405163b1bf962d60e01b815293849182905afa918215610c60576147059461337d93836146fc9360009261477b575b506114a19192614de2565b905151906125eb565b91828111156147715761476a600b61473e857fd37f8c5b1028a8745d109e601653cbf41563b7e941e8affa1a9c4a7a38abd97194611506565b96019261474c878554611364565b938490556040519384526001600160a01b0316929081906020820190565b0390a29190565b9350600092915050565b6114a192506147979060203d811161198a5761197b8183610b32565b916146f1565b5050505090915090600090565b92919092811561482157600b0180549384156148165760207fd37f8c5b1028a8745d109e601653cbf41563b7e941e8affa1a9c4a7a38abd97191868510858818028718968780820391110280945560405193845260018060a01b031692a2828103908111611330579190565b505090915090600090565b50509050600090600090565b9061337d90600461484d60206005860154930151926020840151906125eb565b9301549051906125eb565b919080156125ac576148be6148b1614870848661482d565b60038601546148976148898751926020840151906125eb565b9160028901549051906125eb565b808203911102600b87015480820391110280820391110290565b8290818110908218021890565b9182156148ea576148e3600561012895019161337d60208085549301510151866139d1565b9055611506565b5091505090565b9091801561283f578154935161490691612623565b8301809311611330578290556040519182526001600160a01b0316907f1cf8705a784a46d32023f3694b5e8149137d563085a870fde2f54a6cc5c59df790602090a2565b9091801561283f578154935161495f91612623565b8301809311611330578290556040519182526001600160a01b0316907f8113f59ef078158acce9021327489b70d6ab15d0c107c36455c3505248648df690602090a2565b909392938215614a2b5781548015614a1f577f8113f59ef078158acce9021327489b70d6ab15d0c107c36455c3505248648df691816149f66149e78960209561179f565b808810888218021898896139d1565b808211910302938490556040519384526001600160a01b031692a2828103908111611330579190565b50505090915090600090565b5050509050600090600090565b909392938215614a2b5781548015614a1f577f1cf8705a784a46d32023f3694b5e8149137d563085a870fde2f54a6cc5c59df791816149f66149e78960209561179f565b909192614a899084612623565b15614ae8576001600160a01b03908116803b156100ed5760009283608492604051968795869463617ba03760e01b865216600485015260248401523060448401528160648401525af18015610c6057614adf5750565b610c9c90610aa8565b505050565b8215614ae8576001600160a01b03908116803b156100ed576000928360a492604051968795869463a415bcad60e01b86521660048501526024840152600260448401528160648401523060848401525af18015610c6057614adf5750565b92918215908115614bc7575b50614ae85760405163573ade8160e01b81526001600160a01b039182166004820152602481019290925260026044830152306064830152909160209183916084918391600091165af18015610c6057614bad5750565b614bc49060203d811161198a5761197b8183610b32565b50565b604051630ed1279f60e11b81523060048201529150602090829060249082906001600160a01b03165afa908115610c6057600091614c08575b501538614b57565b614c20915060203d811161198a5761197b8183610b32565b38614c00565b9092801561283f576040516370a0823160e01b81523060048201526020946001600160a01b039491939186908590602490829089165afa938415610c6057614cba9587956000928391614ce3575b50604051631a4ca37b60e21b81526001600160a01b0390941660048501528086189581119590950290941860248301523060448301529094859384929183906064820190565b0393165af18015610c6057614ccd575050565b81614bc492903d1061198a5761197b8183610b32565b614cfa9150873d891161198a5761197b8183610b32565b38614c74565b60405163d15e005360e01b81526001600160a01b0383811660048301529190911692916020918281602481885afa918215610c6057614d72928492600091614d9f575b5060405163386497fd60e01b81526001600160a01b039092166004830152959092839190829081906024820190565b03915afa918215610c6057600092614d8957505090565b6101289250803d1061198a5761197b8183610b32565b614db69150833d851161198a5761197b8183610b32565b38614d43565b91908260809103126100ed578151916020810151916101286060604084015193016131ae565b90614df4825161ffff905160401c1690565b8015614fb557610120830151600490608090614e1a9061129a906001600160a01b031681565b604051630f2ee86760e31b815292839182905afa8015610c605760009081808192614f7d575b6004929394506020614e6261129a61129a6101408c015160018060a01b031690565b60405163b1bf962d60e01b815294859182905afa928315610c6057600093614f5d575b50602087015151614e9690846125eb565b926060890151614eac906001600160801b031690565b6001600160801b0316614ebe916125eb565b9360c0890151614ed29064ffffffffff1690565b64ffffffffff1690614ee392614fce565b614eec916125eb565b92614ef691611364565b90614f0091611506565b90614f0a91611506565b8015614f4357610180614f346101289594614f2b611da0956101ae95611ad3565b90515190612623565b9301516001600160801b031690565b5050506101800151610128906001600160801b03166101ae565b614f7691935060203d811161198a5761197b8183610b32565b9138614e85565b50505050614fa360049160803d8111614fae575b614f9b8183610b32565b810190614dbc565b919350839290614e40565b503d614f91565b50506101800151610128906001600160801b03166101ae565b9064ffffffffff1680830392808411611330571461507f576000198201916000600282111561507557506150516001198201915b600661504a66038882915c40006150198780615090565b046115966301e1338096611596615040896150348c87615090565b04946115968d8a6114d3565b60011c9a876114d3565b04936114d3565b04676765c793fa10079d601b1b9081018091116113305761012892611da091611364565b6150519091615002565b5050676765c793fa10079d601b1b90565b816b019d971e4fe8401e740000001904811115821517156100ed57676765c793fa10079d601b1b91026b019d971e4fe8401e74000000010490565b634e487b7160e01b600052605160045260246000fd5b949290919480600314615145576006036150cb5760208101906151058251876125eb565b80861086821802189081860395861161133057615123905182612623565b830180931161133057615137915190612623565b840393841161133057929190565b506151518151836125eb565b80851085821802188085039485116113305761516e825182612623565b830392831161133057602061518592015190612623565b840180941161133057929190565b95949195939093806005146151ee576002036150cb576001600160a01b039384166000818152609a60209081526040808320600890810154609b90935292209296612d5e969360098101936006909101928b92911c1661272c565b5091909294612681949560018060a01b03809116600052609a6020526009604060002001541690609b602052604060002090600382019261272c56fe29c7258ad2a828aee0fb295826bf2a731d38a5ae377f284addeb97838d657c2da26469706673582212201137663bcc6b00c2b83e4e7ecd3f254cf570a713c77ffa3cc4811f6624cc627b64736f6c63430008130033

Deployed Bytecode Sourcemap

1190:11066:41:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;;:::i;:::-;-1:-1:-1;;;;;1190:11066:41;;;;;:::o;:::-;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;:::i;:::-;;:::o;:::-;;;;;;;:::i;:::-;5797:44;;;;;;;:::i;:::-;5886:26;5931:81;5886:26;;;:::i;:::-;5940:63;;;;;;:::i;:::-;86:155:19;165:70;;;;;;;;86:155;;5931:81:41;6027:11;;;6023:43;;1190:11066;;6429:24;;6360:12;;6206:87;;6133:4;;6140:6;6133:4;;;-1:-1:-1;;;;;1190:11066:41;;6140:6;:::i;:::-;6206:87;1190:11066;;;-1:-1:-1;;;;;1190:11066:41;;;;-1:-1:-1;;;;;1190:11066:41;;;6206:87;;;;;:::i;:::-;6304:5;1190:11066;6334:24;;;;1190:11066;6360:12;;;1190:11066;;;;-1:-1:-1;;;;;1190:11066:41;;;;6360:12;;1190:11066;6360:12;:::i;:::-;6304:5;1190:11066;6414:13;;-1:-1:-1;;;;;1190:11066:41;6414:13;;1190:11066;6429:14;;1190:11066;6429:24;;:::i;:::-;6414:13;1190:11066;;;;;;;;;;;;;;;;6023:43;1190:11066;;-1:-1:-1;;;6047:19:41;;1190:11066;;6047:19;1190:11066;;;;;;;:::i;:::-;3843:40:42;;;;;;;:::i;:::-;1190:11066:41;1819:20:52;;;1190:11066:41;;;;3893:79:42;;3987:19;;1190:11066:41;;;3986:20:42;3982:68;;3485:26:41;;3661:24;1190:11066;3485:26;;3736:24;3485:26;;:::i;:::-;3575:4;3582:6;3575:4;;1190:11066;-1:-1:-1;;;;;1190:11066:41;;3582:6;:::i;:::-;3661:14;;1190:11066;3661:24;;;;:::i;:::-;3697:5;1190:11066;-1:-1:-1;;;;;1190:11066:41;3736:14;;1190:11066;3736:24;;:::i;3982:68:42:-;1190:11066:41;;-1:-1:-1;;;4015:35:42;;1190:11066:41;;4015:35:42;3893:79;1190:11066:41;;-1:-1:-1;;;3939:33:42;;1190:11066:41;;3939:33:42;1190:11066:41;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;:::i;:::-;;;;;:::o;:::-;;;;;;;:::i;:::-;4299:61:42;;;;;;;;:::i;:::-;2045:20:52;1190:11066:41;2045:20:52;;;1190:11066:41;;;;4370:59:42;;4722:78:41;4922:24;4578:26;;;;:::i;:::-;4652:7;;;;;;:::i;:::-;4722:78;;;;;:::i;:::-;4922:24;;:::i;:::-;4960:11;;;1190:11066;;;-1:-1:-1;4956:71:41;;1190:11066;;5147:13;1190:11066;;5088:15;5213:6;1190:11066;;5073:13;1190:11066;5038:5;1190:11066;;;;;;;;;;5073:13;;1190:11066;-1:-1:-1;;;;;1190:11066:41;;;;5088:15;4960:11;5088:15;;1190:11066;5088:15;;:::i;:::-;5038:5;1190:11066;;;5147:13;;-1:-1:-1;;;;;1190:11066:41;5147:13;;1190:11066;5147:13;;:::i;:::-;-1:-1:-1;;;;;1190:11066:41;5213:6;:::i;4956:71::-;4960:11;1190:11066;-1:-1:-1;;;5000:27:41;;1190:11066;;5000:27;4370:59:42;1190:11066:41;;-1:-1:-1;;;4406:23:42;;1190:11066:41;;4406:23:42;1190:11066:41;;;;;;;:::o;:::-;;;;;;;;;;;;;1303:62:23;;:::i;:::-;1859:20:22;1190:11066:41;;-1:-1:-1;;;;;;1190:11066:41;;;;;;2765:6:23;1190:11066:41;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;2813:40:23;1190:11066:41;;2813:40:23;1190:11066:41;;;;;;;;;;;;;;;;;;1202:13:22;1190:11066:41;-1:-1:-1;;;;;929:10:26;1190:11066:41;;;2103:24:22;1190:11066:41;;;;;;;;1202:13:22;1190:11066:41;2765:6:23;1190:11066:41;929:10:26;;1190:11066:41;;;;2765:6:23;1190:11066:41;929:10:26;1190:11066:41;;2813:40:23;;;;1190:11066:41;;;;;-1:-1:-1;;;1190:11066:41;;;;;;;;;;;;;;;;;-1:-1:-1;;;1190:11066:41;;;;;;;;;;;;;-1:-1:-1;;1190:11066:41;;;;1513:6:23;1190:11066:41;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;;;:::i;:::-;6261:61:42;;;;;;;:::i;:::-;2478:20:52;1190:11066:41;2478:20:52;;;1190:11066:41;;;;6332:63:42;;7351:81:41;7306:26;;;:::i;:::-;7360:63;;;;;;:::i;7351:81::-;7447:11;;;7443:45;;1190:11066;;7740:15;7538:141;7799:13;1190:11066;7865:6;1190:11066;;7608:52;1190:11066;;7608:52;1190:11066;7617:18;1190:11066;;;;;7608:52;247:155:19;326:70;;;;;;;;247:155;;7608:52:41;7538:141;;;;:::i;:::-;7690:5;1190:11066;;;;;;7725:13;;-1:-1:-1;;;;;1190:11066:41;;;7443:45;1190:11066;;-1:-1:-1;;;7467:21:41;;1190:11066;;7467:21;6332:63:42;1190:11066:41;;-1:-1:-1;;;6370:25:42;;1190:11066:41;;6370:25:42;1190:11066:41;;;;;;;:::i;:::-;8506:67;;;;;;;;:::i;:::-;8618:26;8723:91;8618:26;;;:::i;:::-;8680:14;1190:11066;9412:27:39;:85;:27;:57;:27;;;1190:11066:41;;;;;;;;9412:15:39;1190:11066:41;;;;;;;9412:27:39;-1:-1:-1;;;;;1190:11066:41;;;;;;;2012:25:51;;;;1190:11066:41;;;;;;1843:207:51;;9412:57:39;:85;:::i;8723:91:41:-;8829:11;;;8825:49;;8952:15;;9058:30;8952:15;;539:4:21;8952:15:41;;;;:::i;:::-;9058:30;:::i;:::-;:69;9054:136;;1190:11066;;;9250:6;1190:11066;;;9309:6;1190:11066;;9235:13;1190:11066;9200:5;1190:11066;;;;;;;;;;9235:13;;1190:11066;-1:-1:-1;;;;;1190:11066:41;;;;9250:6;;:::i;9054:136::-;1190:11066;;-1:-1:-1;;;9150:29:41;;1190:11066;;9150:29;8825:49;1190:11066;;-1:-1:-1;;;8849:25:41;;1190:11066;;8849:25;1190:11066;;;;;;-1:-1:-1;;1190:11066:41;;;;1202:13:22;1190:11066:41;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;;;:::i;:::-;3531:40:42;;;;;;;;:::i;:::-;1582:20:52;1190:11066:41;1582:20:52;;;1190:11066:41;;;;3581:59:42;;2622:12:41;2481:74;1190:11066;2330:26;2691:24;2330:26;;;;:::i;:::-;2420:4;;;2427:6;2420:4;;;-1:-1:-1;;;;;1190:11066:41;;2427:6;:::i;:::-;2481:74;;;:::i;3581:59:42:-;1190:11066:41;;-1:-1:-1;;;3617:23:42;;1190:11066:41;;3617:23:42;1190:11066:41;;;;;;-1:-1:-1;;1190:11066:41;;;;;;;;;:::i;:::-;1303:62:23;;:::i;:::-;1504:24:22;1190:11066:41;;-1:-1:-1;;;;;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;1513:6:23;1190:11066:41;;;;1543:43:22;-1:-1:-1;;1543:43:22;1190:11066:41;;;;;;;-1:-1:-1;;1190:11066:41;;;;;;;;;:::i;:::-;;;;;;;:::i;:::-;;;;;;;:::i;:::-;;;;;;:::i;:::-;10200:8;;;;;:::i;:::-;10260:34;;;:::i;:::-;10348:36;;;;:::i;:::-;1190:11066;;;:::i;:::-;10455:49;;;;;:::i;:::-;1190:11066;;10524:184;10546:77;;;;;:::i;:::-;:106;1190:11066;;;;;10546:106;;:::i;10524:184::-;10875:11;;;10871:43;;10949:141;1190:11066;11046:24;;;;1190:11066;10949:141;;;;:::i;:::-;10934:11;;;;1190:11066;;;;11105:16;11101:54;;11169:11;;11165:43;;-1:-1:-1;;;;;1190:11066:41;;;;;;;11392:6;11385:4;;11392:6;1190:11066;11392:6;:::i;:::-;11463:81;;;;;;;:::i;:::-;1190:11066;;;;;11651:24;;1190:11066;11651:34;;;;;:::i;:::-;11706:5;1190:11066;-1:-1:-1;;;;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;11744:7;1190:11066;;;;;11744:27;;:45;;;;1190:11066;;;-1:-1:-1;;;;;1190:11066:41;;11791:17;;1190:11066;11791:17;;;;:::i;:::-;11706:5;1190:11066;-1:-1:-1;;;;;1190:11066:41;11858:18;1190:11066;11858:18;1190:11066;11878:20;;1190:11066;11878:30;;;;:::i;:::-;11706:5;1190:11066;-1:-1:-1;;;;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;11744:7;1190:11066;;;;;11964:29;;:36;;1190:11066;-1:-1:-1;;;;;1190:11066:41;;;12002:11;;;;:::i;:::-;1190:11066;;12078:11;;1190:11066;;;;12078:11;:::i;:::-;1190:11066;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;;;;12106:102;;1190:11066;;12106:102;1190:11066;;;;;;;;;;;;;;;;1599:130:23;1513:6;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;929:10:26;1662:23:23;1190:11066:41;;1599:130:23:o;1190:11066:41:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;:::i;:::-;;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;:::o;1201:71:34:-;1190:11066:41;;1201:71:34;;;;;;;1535:1272;1190:11066:41;;-1:-1:-1;;;1720:54:34;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;1720:54:34;;;1190:11066:41;;;;;;;;;;;;;;1720:54:34;;1190:11066:41;;1720:54:34;;1190:11066:41;;;2724:8:34;;1720:54;;1190:11066:41;;1720:54:34;:::i;:::-;-1:-1:-1;1877:733:34;;;;;;;;;;;;;;;;;2724:8;;1190:11066:41;2724:8:34;2720:80;;1535:1272;;;;;;:::o;2720:80::-;2765:18;;;:::i;:::-;1228:42;;2734:66;;;;;;1190:11066:41;;-1:-1:-1;;;2734:66:34;;-1:-1:-1;;;;;1190:11066:41;;;1720:54:34;2734:66;;1190:11066:41;;;;1201:71:34;;;1190:11066:41;;;;1201:71:34;;;1190:11066:41;;;;;1201:71:34;;;1190:11066:41;;;;1201:71:34;;1190:11066:41;;;;2734:66:34;;;;;;;;2720:80;;;;;;;2734:66;;;;;;:::i;:::-;;;:::i;:::-;;;;;;:::i;:::-;1190:11066:41;;;288:164:35;-1:-1:-1;;;;;1190:11066:41;368:25:35;;;364:50;;1190:11066:41;288:164:35;:::o;364:50::-;1190:11066:41;;-1:-1:-1;;;402:12:35;;;;;1190:11066:41;;;;;;;:::i;:::-;:::o;:::-;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;:::i;:::-;-1:-1:-1;1190:11066:41;;;;;;;:::o;:::-;;;;;;;:::i;:::-;;;;:::i;:::-;;;;;;:::i;:::-;;;;:::o;21721:199:39:-;;1190:11066:41;;:::i;:::-;;21836:27:39;;;:::i;:::-;1190:11066:41;;;;;;;;;-1:-1:-1;1190:11066:41;8481:212:52;1190:11066:41;;21874:7:39;1190:11066:41;;;-1:-1:-1;1190:11066:41;8098:70:52;8132:36;:14;;1190:11066:41;8132:36:52;:::i;:::-;1190:11066:41;;-1:-1:-1;;;;;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;;8098:70:52;8178:68;8211:35;:14;;;:23;1190:11066:41;8211:35:52;:::i;:::-;1190:11066:41;;-1:-1:-1;;;;;1190:11066:41;;;;;;-1:-1:-1;;;;;;1190:11066:41;;;;;8178:68:52;8481:212;1190:11066:41;8516:17:52;8290:14;;;;8336:68;8290:36;:14;;1190:11066:41;8290:36:52;:::i;:::-;8256:70;1190:11066:41;8256:21:52;;:70;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;;;8256:70:52;8369:35;:14;;;:23;1190:11066:41;8369:35:52;:::i;:::-;1190:11066:41;;-1:-1:-1;;;;;1190:11066:41;;;;;;-1:-1:-1;;;;;;1190:11066:41;;;;;8336:68:52;8414:26;;;1190:11066:41;;-1:-1:-1;;;;1190:11066:41;8450:15:52;1190:11066:41;;-1:-1:-1;;;1190:11066:41;;;;8516:17:52;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;8547:14:52;;;1190:11066:41;;;;8585:23:52;;1190:11066:41;8622:14:52;;1190:11066:41;;;8660:23:52;;1190:11066:41;;;;;;;;8481:212:52;;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;8481:212:52;;;;21721:199:39:o;1190:11066:41:-;;;;;;;:::i;:::-;;;;;;;;;;;;;;:::o;:::-;;;;;;;:::i;:::-;;;;;;;;;:::i;:::-;;;;;:::i;22038:931:39:-;;1190:11066:41;;:::i;:::-;22179:19:39;22434:528;22179:19;;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;;22179:19:39;22246;22331:39;22246:19;;;:::i;:::-;22331:5;1190:11066:41;;;;-1:-1:-1;;;;;1190:11066:41;22331:39:39;:::i;:::-;22277:14;;;;22303;;;;;;1190:11066:41;;;22864:13:39;22303:14;22539:18;;22594;;;22647:14;;;1190:11066:41;22706:14:39;;1190:11066:41;22763:20:39;;;1190:11066:41;22911:26:39;;;;:::i;:::-;1190:11066:41;;;:::i;:::-;;;;22303:14:39;22482:470;;1190:11066:41;22482:470:39;;;1190:11066:41;22482:470:39;;;1190:11066:41;;;;;;;;22482:470:39;;;1190:11066:41;;;;22482:470:39;;;1190:11066:41;22864:13:39;1190:11066:41;:::i;:::-;22482:470:39;;;1190:11066:41;22482:470:39;;;1190:11066:41;22434:528:39;:::i;:::-;22303:14;22382;;;;;;:23;22407:14;;:23;1190:11066:41;;22038:931:39:o;2503:1043:37:-;2641:816;2503:1043;2641:816;;;;;;;;1190:11066:41;;;;;2641:816:37;;;;;;;;;-1:-1:-1;2641:816:37;;;;;;;;4891:930;;;;;;;;;;-1:-1:-1;;;4891:930:37;1190:11066:41;;;2503:1043:37:o;1190:11066:41:-;2641:816:37;1190:11066:41;-1:-1:-1;;;1190:11066:41;;;2641:816:37;1190:11066:41;;;;2641:816:37;1190:11066:41;;;-1:-1:-1;;;2641:816:37;1190:11066:41;;;;;;4891:930:37;;1190:11066:41;4891:930:37;;;;;;;;;;;;;;;;;;;;1190:11066:41;;;;;;;:::i;:::-;-1:-1:-1;1190:11066:41;;;;;;;;;;;;:::o;:::-;;;;;;;:::i;:::-;;;;-1:-1:-1;1190:11066:41;;-1:-1:-1;1190:11066:41;;;;-1:-1:-1;1190:11066:41;;;;-1:-1:-1;1190:11066:41;;;;;;:::o;:::-;;;;;;;:::i;:::-;;-1:-1:-1;1190:11066:41;;-1:-1:-1;1190:11066:41;;;;;;;:::i;:::-;;;;;;;;;;:::o;:::-;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;:::i;:::-;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;1190:11066:41;;;;;:::i;:::-;;;;;;;;;;;;-1:-1:-1;1190:11066:41;;;;;;;;;-1:-1:-1;1190:11066:41;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;10518:488:39:-;;1190:11066:41;;:::i;:::-;;;;:::i;:::-;10677:16:39;1190:11066:41;;;;;;;10677:21:39;10673:92;;10518:488;1190:11066:41;;;1201:71:34;;;;10801:35:39;;;1190:11066:41;10801:35:39;1190:11066:41;;;;;;;;;10801:35:39;;;;;;;10983:16;10801:35;10847:16;10801:35;10775:62;10801:35;-1:-1:-1;10801:35:39;;;10518:488;-1:-1:-1;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;10801:35:39;10775:11;;1190:11066:41;;10775:62:39;-1:-1:-1;;;;;1190:11066:41;;;;10847:16:39;10926:26;;;:::i;:::-;10801:35;10901:21;;1190:11066:41;;;10983:16:39;:::i;:::-;1190:11066:41;10962:18:39;;1190:11066:41;10518:488:39:o;10801:35::-;;;;;;;;;;;;;;;:::i;:::-;;;;;:::i;:::-;;;;;;;;;10673:92;10721:5;1190:11066:41;10721:44:39;;-1:-1:-1;;10721:26:39;;-1:-1:-1;;;;;1190:11066:41;;-1:-1:-1;;;;;1190:11066:41;;;10721:26:39;1190:11066:41;;1201:71:34;;;;;;;;10721:44:39;;;;;1190:11066:41;;;;;;;;;;;;10721:44:39;;;;;;;;;;-1:-1:-1;10721:44:39;;;10673:92;10700:18;1190:11066:41;10700:18:39;;:65;10673:92;;;10721:44;;;;;;;;;;;;:::i;:::-;;;;;:::i;:::-;;;;1190:11066:41;;;;;;;;;;;;;-1:-1:-1;;1190:11066:41;;;;;;;:::o;:::-;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;:::i;:::-;;;;;;;;;;:::o;11356:499:39:-;;1190:11066:41;;11486:15:39;;1190:11066:41;;;;;;;;;;;;11552:16:39;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;11599:250:39;11643:3;1190:11066:41;;11615:26:39;;;;;11814:24;11643:3;11730:18;11770:30;11714:41;11730:18;;;;;;:::i;:::-;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;11730:18:39;11714:41;:::i;:::-;11770:30;;:::i;:::-;11814:24;;:::i;:::-;11643:3;;:::i;:::-;11604:9;;;;11615:26;;;;;;;11356:499::o;1190:11066:41:-;;;;;;;;;;;;;;;;;;;;12109:285:39;;1190:11066:41;12185:12:39;1190:11066:41;;;;;;;;;;;;;12240:12:39;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;12283:105:39;12323:3;1190:11066:41;;12299:22:39;;;;;12342:35;12323:3;12356:14;12350:27;12356:14;;;;;;:::i;:::-;12350:27;:::i;:::-;12342:35;;:::i;:::-;12323:3;;:::i;:::-;12288:9;;;12299:22;;;;;12109:285::o;1190:11066:41:-;;;;;;;;;;;;-1:-1:-1;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;:::o;:::-;;;;;;;;;;;;822:5:46;;;;;;;;;;:::o;12763:1317:39:-;-1:-1:-1;;;;;1190:11066:41;;;-1:-1:-1;1190:11066:41;;;12944:7:39;1190:11066:41;;;;;12944:32:39;;1190:11066:41;-1:-1:-1;;12763:1317:39;;1190:11066:41;;12943:33:39;12939:52;;13252:144;13109:37;9412:85;13191:27;13109:37;;2012:31:51;13109:37:39;2012:25:51;1190:11066:41;13109:37:39;13266:103;13109:37;;;:::i;:::-;13191:27;;;;;;;;:::i;:::-;1190:11066:41;;;13325:14:39;;1190:11066:41;;;;9412:15:39;1190:11066:41;;;2012:25:51;1190:11066:41;;;;;;;;;;;;;;;;2012:31:51;1190:11066:41;9412:85:39;:::i;:::-;13266:103;:::i;:::-;13252:144;:::i;:::-;822:5:46;;1190:11066:41;;;;;;;;;;13976:30:39;822:5:46;14026:47:39;1190:11066:41;;13976:30:39;;;:::i;:::-;14026:47;;:::i;12939:52::-;12978:13;;;;;;:::o;4279:413:20:-;;1190:11066:41;;;4520:166:20;;;;;;;;;;;4279:413;:::o;5908:332:21:-;;1190:11066:41;;;6109:125:21;;;;;;;-1:-1:-1;;;6109:125:21;;;5908:332;:::o;1190:11066:41:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;;;;:::i;14351:574:39:-;14521:5;1190:11066:41;;;-1:-1:-1;;;14521:34:39;;-1:-1:-1;;;;;1190:11066:41;;;14521:34:39;;;1190:11066:41;;;14351:574:39;1190:11066:41;;14521:34:39;;1190:11066:41;;;;;;;;14521:34:39;;;;;;14631:75;14813:82;14521:34;14813:64;14521:34;14812:106;14521:34;-1:-1:-1;14521:34:39;;;14351:574;-1:-1:-1;14521:34:39;14654:11;;1190:11066:41;;14675:18:39;;;:30;;1190:11066:41;;;;;14631:75:39;;:::i;:::-;14751:27;;;;1190:11066:41;14751:27:39;;;:::i;:::-;1190:11066:41;;-1:-1:-1;;;;;1190:11066:41;;;;14813:64:39;;:::i;:82::-;14812:106;:::i;14521:34::-;;;;;;;;;;;;;;;:::i;:::-;;;;;:::i;:::-;;;;;;;;;676:248:19;;805:113;;;;;;;;;;;;676:248;:::o;8660:448:39:-;;9024:77;8660:448;;1190:11066:41;;;;;;-1:-1:-1;1190:11066:41;8885:15:39;1190:11066:41;;;1676:41:51;1190:11066:41;-1:-1:-1;1190:11066:41;1331:36:51;1676:35;8930:79:39;1331:36:51;:42;:36;;;;1190:11066:41;;;;;;;;;;;;;;;;1331:42:51;1190:11066:41;8984:14:39;;;;;1190:11066:41;8930:79:39;;:::i;:::-;1676:35:51;;1190:11066:41;;;;;;;;;;;;;;;;1676:41:51;1190:11066:41;9077:14:39;;:23;1190:11066:41;9024:77:39;;:::i;:::-;1190:11066:41;;;;;;;8660:448:39;:::o;6414:461:21:-;6704:165;;;;;;;;;;-1:-1:-1;;;6704:165:21;;;;;;6414:461::o;15414:1140:39:-;15699:5;1190:11066:41;15414:1140:39;;;15699:22;;-1:-1:-1;;;;;1190:11066:41;;;15699:22:39;1190:11066:41;;-1:-1:-1;;;15699:34:39;;-1:-1:-1;;;;;1190:11066:41;;15699:34:39;;;1190:11066:41;;;15699:34:39;;1190:11066:41;;;;;;15699:34:39;;;;;;;-1:-1:-1;15699:34:39;;;15414:1140;-1:-1:-1;15699:34:39;15850:11;;1190:11066:41;15827:75:39;;15850:11;;;1190:11066:41;;-1:-1:-1;;;;;1190:11066:41;15871:18:39;;;;:30;;1190:11066:41;15871:18:39;;;-1:-1:-1;;;;;1190:11066:41;15871:30:39;15827:75;;:::i;:::-;15768:134;;;;;1190:11066:41;;5884:9:11;;5872:21;;;;16204:20:39;;;16200:72;;-1:-1:-1;;16283:265:39;;;16318:18;;;;1190:11066:41;16354:62:39;1190:11066:41;15699:34:39;16312:28;1190:11066:41;;;;;;;;;;;;16312:28:39;16377:39;;1190:11066:41;;;;;16354:62:39;16283:265;15414:1140::o;16283:265::-;16447:21;;;;;3298:2:11;;;;16283:265:39;15414:1140::o;16200:72::-;16226:46;-1:-1:-1;;;;;;16226:46:39;;-1:-1:-1;16226:46:39;-1:-1:-1;;;;16226:46:39:o;15699:34::-;15827:75;15699:34;;;;;;;;;;;;;;;:::i;:::-;;;;;1190:11066:41;;;;;;;;;;;:::o;23665:658:39:-;;;;;24012:26;3298:2:11;1190:11066:41;;3439:2:11;3298;;23948::39;1190:11066:41;24012:26:39;;:::i;:::-;24052:38;;;;;23665:658;24048:200;;23665:658;-1:-1:-1;1190:11066:41;;-1:-1:-1;;;24277:27:39;;-1:-1:-1;;;;;1190:11066:41;;;24277:27:39;;;1190:11066:41;;24277:27:39;;1190:11066:41;;;;;;;24277:27:39;;;;;;;-1:-1:-1;24277:27:39;;;23665:658;24258:58;;;23665:658;:::o;24277:27::-;;;;;;;;;;;;;;;;:::i;:::-;;;;;:::i;:::-;;;;;;;;;;24048:200;1190:11066:41;;-1:-1:-1;;;24127:33:39;;-1:-1:-1;;;;;1190:11066:41;;;24127:33:39;;;1190:11066:41;24127:33:39;1190:11066:41;;;;;24127:33:39;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;24127:33:39;;;;;;;-1:-1:-1;24127:33:39;;;24048:200;24179:15;;24175:62;24048:200;24175:62;24196:41;;;;;;:::o;24127:33::-;;;;;;;;;;;;;;:::i;:::-;;;;24052:38;-1:-1:-1;;;;;;1190:11066:41;;24065:25:39;;24052:38;;24456:200;1190:11066:41;24579:16:39;1190:11066:41;;;;24579:21:39;;;:70;;;;24572:77;;24456:200;:::o;24579:70::-;1190:11066:41;4339:3:11;3298:2;1190:11066:41;3298:2:11;24604:45:39;;24456:200;-1:-1:-1;24456:200:39:o;8086:452::-;;8452:79;8086:452;;1190:11066:41;;;;;;-1:-1:-1;1190:11066:41;8311:15:39;1190:11066:41;;;994:41:51;1190:11066:41;-1:-1:-1;1190:11066:41;649:36:51;994:35;8356:81:39;649:42:51;:36;1190:11066:41;649:36:51;;1190:11066:41;;;;;;;;;;;;;;;;649:42:51;1190:11066:41;8412:14:39;;1190:11066:41;8356:81:39;;:::i;994:41:51:-;1190:11066:41;8507:14:39;;:23;1190:11066:41;8452:79:39;;:::i;23024:267::-;23153:20;23024:267;23153:20;:::i;:::-;23191:18;;;1190:11066:41;;23191:22:39;;;;23216:21;;1190:11066:41;3584:224:21;;;;;;;;;;;;;;;;;;;;23024:267:39;:::o;23191:93::-;1190:11066:41;;;;23024:267:39;:::o;3500:519:20:-;3789:224;;;;;;;;;;;;;;;;;3500:519::o;2484:385:42:-;-1:-1:-1;;;;;1190:11066:41;2484:385:42;;;;1190:11066:41;;2652:18:42;2648:53;;2715:11;2711:45;;1190:11066:41;;2668:1:42;1190:11066:41;2776:7:42;1190:11066:41;;;2668:1:42;1190:11066:41;1374:13:52;;;;1190:11066:41;;1374:27:52;2805:57:42;;2484:385::o;2805:57::-;1190:11066:41;;-1:-1:-1;;;2837:25:42;;;;;2711:45;1190:11066:41;;-1:-1:-1;;;2735:21:42;;;;;2648:53;1190:11066:41;;-1:-1:-1;;;2679:22:42;;;;;2938:373;;;;-1:-1:-1;;;;;1190:11066:41;2938:373:42;1190:11066:41;;3135:22:42;3131:57;;3208:44;;;;;:::i;:::-;1190:11066:41;;3293:10:42;2346:20;;:56;;;;;2938:373;2344:59;;2340:97;;2938:373::o;2340:97::-;1190:11066:41;;-1:-1:-1;;;2412:25:42;;;;;2346:56;3155:1;1190:11066:41;;;2370:12:42;1190:11066:41;;;;;;;;3293:10:42;1190:11066:41;;;;;;;;;-1:-1:-1;1190:11066:41;;2346:56:42;;;1190:11066:41;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;:::o;4483:1200:42:-;4652:5;1190:11066:41;4652:22:42;;-1:-1:-1;;;;;1190:11066:41;;;4652:22:42;1190:11066:41;;;-1:-1:-1;;;4652:34:42;;-1:-1:-1;;;;;1190:11066:41;;4652:34:42;;;;1190:11066:41;;;;4652:34:42;1190:11066:41;;;;4652:34:42;;1190:11066:41;;4652:34:42;;;;1190:11066:41;;;;;;4652:34:42;;;;;;;-1:-1:-1;4652:34:42;;;4483:1200;-1:-1:-1;1190:11066:41;;13634:15:11;13622:27;13621:34;4696:67:42;;4804:18;1190:11066:41;;;-1:-1:-1;;;4804:43:42;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;4804:43:42;;;;;;;-1:-1:-1;4804:43:42;;;4483:1200;1190:11066:41;;4861:33:42;;;;;:97;;;;4483:1200;4857:168;;;;;1190:11066:41;;;;5039:21:42;;;;;:70;;4483:1200;5035:134;;;3298:2:11;1190:11066:41;;;4127:2:11;3298;;5179:498:42;;4483:1200;;;;;;;;;:::o;5179:498::-;5313:29;5255:19;5376:52;5255:19;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;;5255:19:42;5313:29;;;:::i;:::-;1190:11066:41;;3298:2:11;5401:26:42;1190:11066:41;3298:2:11;3439;3298;;5401:26:42;:::i;:::-;3298:2:11;4127;3298;;5376:52:42;:::i;:::-;5483:24;;;;1190:11066:41;5483:24:42;;;5477:43;;:31;;1190:11066:41;;-1:-1:-1;;;;;1190:11066:41;5477:31:42;1190:11066:41;5477:43:42;1190:11066:41;;;1201:71:34;;;;;;;;5477:45:42;;;;;;;;;;;-1:-1:-1;5477:45:42;;;5179:498;-1:-1:-1;5531:22:42;;1190:11066:41;5531:22:42;;5525:41;;:29;;-1:-1:-1;;;;;1190:11066:41;5525:29:42;1190:11066:41;5525:41:42;1190:11066:41;;;;5525:43:42;;;;;;;;;;;;5587:33;5525:43;5587:22;5525:43;5477:91;5525:43;-1:-1:-1;5525:43:42;;;5179:498;5477:91;;;:::i;5587:22::-;:33;:::i;:::-;:45;5583:83;;5179:498;;;;;;;;5583:83;1190:11066:41;-1:-1:-1;;;5641:25:42;;;5525:43;;;;;;;-1:-1:-1;5525:43:42;;;;;;:::i;:::-;;;;;;5477:45;;;;;5525:29;1190:11066:41;5531:22:42;5477:45;5525:41;5477:45;;;;;;;;;;;:::i;:::-;;;;;;;;;5035:134;1190:11066:41;;-1:-1:-1;;;5132:26:42;;1190:11066:41;;5132:26:42;5039:70;1190:11066:41;;;;;4339:3:11;3298:2;;5064:45:42;;5039:70;;;4857:168;1190:11066:41;;-1:-1:-1;;;4981:33:42;;1190:11066:41;;4981:33:42;4861:97;1190:11066:41;;-1:-1:-1;;;4899:59:42;;1190:11066:41;-1:-1:-1;1190:11066:41;;;;;;4899:59:42;;;;;;;-1:-1:-1;4899:59:42;;;4861:97;4898:60;;4861:97;;;;;4899:59;;;;;;;;;;;;;;;:::i;:::-;;;;;:::i;:::-;;;;;;;;;4804:43;;;;;;;;;;;;;;:::i;:::-;;;;4696:67;1190:11066:41;;-1:-1:-1;;;4738:25:42;;1190:11066:41;;4738:25:42;4652:34;;;;;;;;;;;;;;;:::i;:::-;;;;;5728:287;;5901:40;5728:287;;5901:40;:::i;:::-;2259:20:52;1190:11066:41;2259:20:52;;;1190:11066:41;;;;5951:57:42;;5728:287::o;5951:57::-;1190:11066:41;;-1:-1:-1;;;5986:22:42;;;;;6461:369;;6669:61;6461:369;;;6669:61;:::i;:::-;2721:20:52;1190:11066:41;2721:20:52;;;1190:11066:41;;;;6740:83:42;;6461:369::o;6740:83::-;1190:11066:41;;-1:-1:-1;;;6788:35:42;;;;;6879:670;-1:-1:-1;;;;;1190:11066:41;;;;;;;11744:7;1190:11066;;;;;;;;;;;;;;;;;7184:22:42;7180:57;;1374:13:52;;;1190:11066:41;;;1374:27:52;7252:58:42;;;;;6879:670;7248:96;;;2976:20:52;;1190:11066:41;;;;;7354:95:42;;2976:20:52;3224;1190:11066:41;;;;;7459:83:42;;6879:670::o;7459:83::-;1190:11066:41;;-1:-1:-1;;;7510:32:42;;;;;7354:95;1190:11066:41;;-1:-1:-1;;;7413:36:42;;;;;7252:58;1374:13:52;;;1190:11066:41;;1374:27:52;;-1:-1:-1;7252:58:42;;;7599:963;-1:-1:-1;;;;;1190:11066:41;;;;;11744:7;1190:11066;;;;;7720:42:42;;3445:20:52;;1190:11066:41;;;;;;3348:137:52;7720:42:42;7716:81;;7871:30;;;:::i;:::-;539:4:21;7915:52:42;;;7911:120;;1492:7:46;8045:52:42;;;;;8041:366;;7599:963;8421:51;8417:94;;568:6:20;7599:963:42;:::o;8417:94::-;631:5:20;8474:37:42;:::o;8041:366::-;8143:18;1190:11066:41;8143:41:42;;-1:-1:-1;;;;;1190:11066:41;;;8143:41:42;1190:11066:41;;1201:71:34;;;;;8143:43:42;;;;;;;;;;;;;;;-1:-1:-1;8143:43:42;;;8041:366;-1:-1:-1;;;;;;1190:11066:41;8205:33:42;;;;;;:102;;8041:366;8201:196;;;8041:366;8201:196;1190:11066:41;;-1:-1:-1;;;8346:36:42;;8143:43;;8346:36;8205:102;1190:11066:41;;-1:-1:-1;;;8243:64:42;;1190:11066:41;;-1:-1:-1;1190:11066:41;;;;8143:43:42;;1190:11066:41;;8243:64:42;;;;;;;-1:-1:-1;8243:64:42;;;8205:102;8242:65;;;8205:102;;;;8243:64;;;;;;-1:-1:-1;8243:64:42;;;;;;:::i;:::-;;;;;8143:43;;;;;;;;;;;;;;:::i;:::-;;;;7911:120;1190:11066:41;;-1:-1:-1;;;7990:30:42;;;;;7716:81;7764:33;568:6:20;7764:33:42;:::o;1190:11066:41:-;;;;;;;;;;;;;;;;;;-1:-1:-1;1190:11066:41;;;;;;;;;;;;;;;;;:::o;18735:439:42:-;;;;;19091:76;9980:73;9884:57;19091:76;18735:439;;;;1190:11066:41;;:::i;:::-;;19007:68:42;1190:11066:41;;;;:::i;:::-;8898:19:42;;;994:41:51;8898:19:42;;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;;8898:19:42;8973:27;994:35:51;8973:27:42;;1190:11066:41;;;;;;;;9412:15:39;1190:11066:41;;;;;;;8973:27:42;-1:-1:-1;;;;;1190:11066:41;;;;;;649:36:51;;;1190:11066:41;;;;;649:36:51;;:42;1190:11066:41;;;994:35:51;1190:11066:41;;;;;;;;;;;;;;;;994:41:51;1190:11066:41;9082:10:42;;;;1190:11066:41;;;3670:20:52;9192:23:42;1190:11066:41;3670:20:52;;;1190:11066:41;;9192:23:42;1190:11066:41;;;;9192:23:42;9188:601;;18735:439;1190:11066:41;;;;;;9916:14:42;;1190:11066:41;9884:57:42;;:::i;:::-;1190:11066:41;;;;9854:13:42;;;1190:11066:41;;;9980:73:42;;;;:::i;:::-;1190:11066:41;;;;;;;9854:13:42;1190:11066:41;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;;;;;19091:76:42;;;;18735:439::o;9188:601::-;9382:14;;;;9519:69;9382:14;;9295:12;9382:14;9327:86;9688:90;9382:14;9327:13;9382:14;9702:76;9382:14;994:35:51;9082:10:42;9382:14;;;1190:11066:41;9327:20:42;;:86;:::i;:::-;9295:12;;;;1190:11066:41;;;9519:69:42;;:::i;:::-;9497:91;;;1190:11066:41;9602:24:42;1190:11066:41;;;9602:24:42;:::i;:::-;1190:11066:41;;;9327:13:42;9702:76;:::i;:::-;1190:11066:41;;9688:90:42;:::i;:::-;1190:11066:41;;9188:601:42;;;;;;;5101:1330:38;;;;1190:11066:41;5365:18:38;;;;1190:11066:41;;;5365:23:38;5361:42;;5434:11;;;;;1190:11066:41;;;5507:13:38;;;;1190:11066:41;;;;;;;;;5507:40:38;;;;;;;;;;1190:11066:41;;5565:35:38;;:53;;;5620:16;5565:53;;;5672:34;;;;:::i;:::-;-1:-1:-1;;;;;1190:11066:41;;;;;;;5724:23:38;;5720:34;;5870:9;;;;;6000:71;5870:9;;;;;5620:16;5870:9;;;;5912:29;5870:97;:9;;;;1190:11066:41;;5880:30:38;1190:11066:41;;5880:19:38;;;1190:11066:41;;;;;;;;;;;;;;;;5880:30:38;1190:11066:41;5912:18:38;;1190:11066:41;;;;;;;;;;;;;;;;5912:29:38;1190:11066:41;5943:12:38;;;;;5870:97;;:::i;:::-;1190:11066:41;6000:13:38;;1190:11066:41;;;;;5825:142:38;;;;;-1:-1:-1;;;;;1190:11066:41;;;;6000:71:38;;:::i;:::-;6090:11;;1190:11066:41;6090:11:38;;;1190:11066:41;;;;;;6090:11:38;;;1190:11066:41;6108:71:38;;;;-1:-1:-1;;;;;1190:11066:41;;;;;6108:71:38;;;;1190:11066:41;;;;;;;;;;;;;;;6108:71:38;;;;5620:16;:::i;:::-;5558:727;;;;;6086:188;1190:11066:41;6203:71:38;;;;-1:-1:-1;;;;;1190:11066:41;;;5720:34:38;5749:5;;;;;;;;;;;;;1190:11066:41;;5101:1330:38;:::o;5565:53::-;;;;;;;;;;;1190:11066:41;;5101:1330:38;:::o;5565:53::-;5604:14;;;;5565:53;;5507:40;;;;;;;;5361:42;5390:13;;;;;;1190:11066:41;5390:13:38;1190:11066:41;5390:13:38;:::o;2289:599:18:-;;2411:19;;;1190:11066:41;2444:16:18;;;2440:39;;5244:19;;;4636:297;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4559:380;;5244:19;5273:167;;;;;;;2549:9;;2545:63;;4291:19;;;4636:297;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4559:380;;4291:19;3298:2:11;5273:167:18;3298:2:11;4327:28:18;-1:-1:-1;1190:11066:41;;;;-1:-1:-1;1190:11066:41;-1:-1:-1;1190:11066:41;;;;793:22:17;5273:167:18;1190:11066:41;-1:-1:-1;1190:11066:41;793:22:17;1190:11066:41;;;;;;;;;;2545:63:18;1190:11066:41;-1:-1:-1;1190:11066:41;;;;-1:-1:-1;1190:11066:41;-1:-1:-1;1190:11066:41;;;;;;;;;5273:167:18;1190:11066:41;-1:-1:-1;1190:11066:41;793:22:17;1190:11066:41;;2560:48:18;:::o;2440:39::-;2462:17;;;-1:-1:-1;2462:17:18;:::o;4191:171::-;4291:19;4191:171;4636:297;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4559:380;;4291:19;3298:2:11;4353:1:18;3298:2:11;4327:28:18;4191:171;:::o;5298:434:21:-;5571:155;;;;;;;;;;-1:-1:-1;;;5571:155:21;;;;;;5298:434::o;7048:558::-;7376:224;;;;-1:-1:-1;;;7376:224:21;;;;;;;;;;;;;;;7048:558;:::o;19228:594:39:-;;19410:249;19228:594;;;1190:11066:41;;;;;;;;9010:11:42;1190:11066:41;19435:7:39;1190:11066:41;;19435:26:39;1190:11066:41;9010:11:42;1190:11066:41;19435:26:39;1190:11066:41;;;19493:15:39;1190:11066:41;;;9010:11:42;1190:11066:41;19548:40:39;;;;19410:249;;:::i;:::-;19403:256;;19228:594::o;17883:802::-;;;;;;682:1:46;18190:34:39;;;18186:50;;17883:802;682:1:46;18250:33:39;;;18246:48;;17883:802;-1:-1:-1;;;;;1190:11066:41;;;;;;682:1:46;18328:19:39;;;1190:11066:41;;;;;;;;;18385:18:39;;;1190:11066:41;;;;;;;18328:19:39;;1190:11066:41;;;18328:19:39;;1190:11066:41;;18328:19:39;18424:22;;;18420:154;;17883:802;18588:20;;;;;;;18584:62;;17883:802;18656:22;;;;17883:802;:::o;18584:62::-;18641:4;;;:::i;:::-;18584:62;;;;;18420:154;18554:8;18494:12;;;;;:::i;:::-;18554:8;:::i;:::-;18420:154;;;;;;;18246:48;9010:11:42;;-1:-1:-1;18246:48:39;;18186:50;9010:11:42;;-1:-1:-1;18186:50:39;;17883:802;;;;;;;;682:1:46;18190:34:39;;;18186:50;;17883:802;682:1:46;18250:33:39;;;18246:48;;17883:802;-1:-1:-1;;;;;1190:11066:41;;;;;;682:1:46;18328:19:39;;;1190:11066:41;;;;;;;;;18385:18:39;;;1190:11066:41;;;;;;;18328:19:39;;1190:11066:41;;;;;18328:19:39;;;1190:11066:41;;18328:19:39;18424:22;;;18420:154;;17883:802;18588:20;;;;;;;;18584:62;;18656:22;;;;17883:802;:::o;18420:154::-;18554:8;18494:12;;;;;:::i;:::-;18554:8;:::i;:::-;18420:154;;;;;;;;18246:48;18293:1;;-1:-1:-1;18246:48:39;;18186:50;18235:1;;-1:-1:-1;18186:50:39;;977:1071:18;;-1:-1:-1;;;;;1190:11066:41;;1120:16:18;1116:42;;1190:11066:41;1184:15:18;;:19;1213;1184;;;;1190:11066:41;;;;;;;;;;;;;;;;1184:19:18;1190:11066:41;1213:19:18;1190:11066:41;;;;;;;;;;;;;;;;1213:19:18;1190:11066:41;1258:10:18;;1254:252;;1622:20;;;:::i;:::-;1656:13;;;1652:99;;1866:23;;;:::i;:::-;1903:26;;;;1899:143;;977:1071;;;;;:::o;1899:143::-;1966:13;2026:4;1966:13;;;;:::i;:::-;2026:4;:::i;:::-;1899:143;;;;;;1652:99;1706:13;;;;;:::i;1254:252::-;1288:13;;;1284:37;;1445:23;1470:4;1445:23;;:::i;:::-;1470:4;;:::i;1284:37::-;1190:11066:41;;-1:-1:-1;;;1310:11:18;;;;;1116:42;1190:11066:41;;-1:-1:-1;;;1145:13:18;;;;;977:1071;;-1:-1:-1;;;;;1190:11066:41;;1120:16:18;1116:42;;1190:11066:41;1184:15:18;;:19;1213;1184;;;;1190:11066:41;;;;;;;;;;;;;;;;1213:19:18;1190:11066:41;1258:10:18;;1254:252;;1622:20;;;:::i;:::-;1656:13;;;1652:99;;1866:23;;;:::i;:::-;1903:26;;;;1899:143;;977:1071;;;;:::o;1899:143::-;1966:13;2026:4;1966:13;;;;:::i;:::-;2026:4;:::i;1254:252::-;1288:13;;;1284:37;;1445:23;1470:4;1445:23;;:::i;:::-;1470:4;;:::i;977:1071::-;;;;;-1:-1:-1;;;;;1190:11066:41;;1120:16:18;1116:42;;1190:11066:41;1184:15:18;;:19;1213;1184;;;;1190:11066:41;;;;;;;;;;;;;;;;1213:19:18;1190:11066:41;1258:10:18;;1254:252;;1622:20;;;:::i;:::-;1656:13;;;1652:99;;1866:23;;;:::i;:::-;1903:26;;;;1899:143;;977:1071;;;;;:::o;1899:143::-;1966:13;2026:4;1966:13;;;;:::i;:::-;2026:4;:::i;:::-;1899:143;;;;;;;1652:99;1706:13;;;;;;;:::i;1254:252::-;1288:13;;;1284:37;;1445:23;1470:4;1445:23;;:::i;:::-;1470:4;;:::i;3170:195::-;;1190:11066:41;3170:195:18;1190:11066:41;-1:-1:-1;1190:11066:41;;;;-1:-1:-1;1190:11066:41;1489:17:17;1190:11066:41;;;1297:17:17;;;;1190:11066:41;;;;;;;;;;;;;;;;1297:17:17;1190:11066:41;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;1399:31:17;:19;;;;;1190:11066:41;;;;;;;;;;;;;;;;1399:19:17;:24;1190:11066:41;;-1:-1:-1;;;;;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;1399:31:17;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;1440:31:17;;:19;;;1190:11066:41;;-1:-1:-1;;;;;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;1440:31:17;1190:11066:41;;;;;;;;;;;;;;;;1489:17:17;1190:11066:41;;;;;1525:18:17;:40;;;;3170:195:18;3288:70;;;3170:195;;:::o;3288:70::-;3328:19;;3351:7;;1190:11066:41;;3328:30:18;1190:11066:41;;3170:195:18:o;1525:40:17:-;1547:18;;;1525:40;;;3743:219:18;;1190:11066:41;-1:-1:-1;1190:11066:41;;;;2336:29:17;1190:11066:41;-1:-1:-1;1190:11066:41;-1:-1:-1;1190:11066:41;;;;;682:1:46;2336:17:17;1190:11066:41;;;-1:-1:-1;1190:11066:41;2200:30:17;1190:11066:41;2244:35:17;1190:11066:41;;;;;;;;2244:35:17;;;1190:11066:41;;;;;;;;;;;;;;;;;2244:35:17;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;2293:29:17;;:19;;;1190:11066:41;2336:29:17;2386:18;3880:75:18;;3743:219;;:::o;3880:75::-;3926:19;;1190:11066:41;;;3926:29:18;1190:11066:41;;3743:219:18:o;:::-;9010:11:42;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;-1:-1:-1;;;;;;1190:11066:41;;;;;;;;;;;2586:29:17;;1190:11066:41;;2586:17:17;;2494:35;2543:29;:19;1190:11066:41;2543:19:17;;;1190:11066:41;;;;;;;;;;;;;;;;3743:219:18;;;3884:40;3743:219;1190:11066:41;-1:-1:-1;1190:11066:41;;;;;-1:-1:-1;1190:11066:41;3884:40:18;:::i;:::-;3880:75;;3743:219;;:::o;2035:636:17:-;;;2159:506;;;-1:-1:-1;1190:11066:41;;;;;;;;;;2200:30:17;;;;1190:11066:41;;-1:-1:-1;;;;;1190:11066:41;;;-1:-1:-1;;;;;;1190:11066:41;;;;;;;;2336:29:17;;1190:11066:41;;2200:30:17;2336:17;;2244:35;1190:11066:41;2336:29:17;2386:18;2379:25;:::o;2159:506::-;-1:-1:-1;1190:11066:41;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;-1:-1:-1;;;;;;1190:11066:41;;;;;;;;2586:29:17;;1190:11066:41;;2586:17:17;;2494:35;1190:11066:41;16901:293:39;17032:15;1190:11066:41;16901:293:39;;;;-1:-1:-1;;;;;1190:11066:41;;17057:131:39;;16901:293;;;;:::o;17057:131::-;17114:63;;;;;1190:11066:41;;-1:-1:-1;;;17114:63:39;;-1:-1:-1;;;;;1190:11066:41;;;17114:63:39;;;1190:11066:41;;;;;;;;;;;;;-1:-1:-1;;1190:11066:41;;;;;;-1:-1:-1;;17114:63:39;;;;;;;;17057:131;;;;;;17114:63;;;;:::i;:::-;;;;20365:619;-1:-1:-1;;;;;1190:11066:41;;;10605:11:42;1190:11066:41;;;20583:7:39;1190:11066:41;;;;;;;;20583:37:39;;;;1190:11066:41;20652:15:39;1190:11066:41;;;;;20365:619:39;;;1190:11066:41;;20558:260:39;;20365:619;;1190:11066:41;;20707:40:39;;;;20652:41;;;;;20365:619;;1190:11066:41;;;20558:260:39;:::i;:::-;20832:11;;;;;:25;;;20365:619;20828:117;;;20859:18;8711:53:30;20859:18:39;1190:11066:41;;;;;;;;20911:12:39;1190:11066:41;;;;;;;20859:18:39;8711:53:30;:::i;:::-;;20955:22:39;20365:619;:::o;20828:117::-;20911:18;8390:50:30;20911:18:39;1190:11066:41;;;;;;;;20911:12:39;1190:11066:41;;;;;;;20911:18:39;8390:50:30;:::i;20832:25:39:-;20847:10;;;20832:25;;1190:11066:41;;;;;;;;-1:-1:-1;1190:11066:41;;-1:-1:-1;1190:11066:41;;;-1:-1:-1;1190:11066:41;:::o;:::-;;;;;;;;;;;;;;;;;;;;;;;;;:::o;2206:404:30:-;4343:12;;;1190:11066:41;;-1:-1:-1;1190:11066:41;;;;;-1:-1:-1;1190:11066:41;;4343:24:30;2285:319;1190:11066:41;;;;;;;;;;;;;;4343:12:30;1190:11066:41;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;1190:11066:41;;;;-1:-1:-1;1190:11066:41;;4343:12:30;2539:11;:::o;1190:11066:41:-;;;;;;;;-1:-1:-1;;1190:11066:41;;;;;;;;:::i;:::-;;;;;;;;;;;;;;:::o;:::-;;;;;;;;;;;;2778:1388:30;2981:12;;;1190:11066:41;;-1:-1:-1;1190:11066:41;;;;;-1:-1:-1;1190:11066:41;;3015:15:30;;;;3011:1149;3015:15;;;-1:-1:-1;;1190:11066:41;822:5:46;;;;;;;;;1190:11066:41;;822:5:46;;;;;;;;-1:-1:-1;3497:26:30;;;3969:15;3497:26;4061:19;3497:26;;3493:398;;3011:1149;3969:15;;;;:::i;:::-;1190:11066:41;;;;;;;;;;4061:19:30;1190:11066:41;2981:12:30;4095:11;:::o;3493:398::-;3685:38;:26;3563:22;;3797:23;3563:22;;;:::i;:::-;1190:11066:41;;;;;3298:2:11;3685:26:30;;;;;:::i;:::-;:38;;:::i;:::-;3797:23;1190:11066:41;;;;;;;;;;3797:23:30;1190:11066:41;3493:398:30;;;;;3011:1149;4137:12;;;;-1:-1:-1;4137:12:30;:::o;19219:462:42:-;;19582:92;19219:462;;;;;;;1190:11066:41;;:::i;:::-;;19582:92:42;11765:73;11669:57;19498:68;1190:11066:41;;:::i;:::-;10493:19:42;;;;;;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;;10493:19:42;10568:27;1676:41:51;10568:27:42;1676:35:51;10568:27:42;;1190:11066:41;;;;;;;;9412:15:39;1190:11066:41;;;;;;;10568:27:42;-1:-1:-1;;;;;1190:11066:41;;;;;;1331:36:51;;;1190:11066:41;;;;;1331:36:51;;:42;1190:11066:41;1676:41:51;1190:11066:41;10677:10:42;;;;1190:11066:41;;;3670:20:52;10787:23:42;1190:11066:41;1331:36:51;3670:20:52;;1190:11066:41;;10787:23:42;1190:11066:41;;;;10787:23:42;10783:791;;19219:462;1190:11066:41;;;;10677:10:42;1190:11066:41;;;11701:14:42;;;1190:11066:41;11669:57:42;;:::i;:::-;1190:11066:41;;;;11639:13:42;;;1190:11066:41;;;11765:73:42;;;;:::i;:::-;1190:11066:41;;;;;;;;;;19598:10:42;1190:11066:41;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;;;;;10783:791:42;11055:15;10937:39;;;11517:29;11283:69;10937:39;;;;11090:87;10937:39;11090:13;10937:39;11469:94;10937:39;11455:108;10937:39;;:::i;:::-;11090:13;;;;11145:14;;;1190:11066:41;11090:87:42;;;;:::i;:::-;11055:15;;;;1190:11066:41;;;11283:69:42;;:::i;:::-;11261:91;;;1190:11066:41;11366:27:42;1190:11066:41;;;11366:27:42;:::i;:::-;1190:11066:41;;;11517:29:42;:::i;:::-;11469:94;;:::i;11455:108::-;1190:11066:41;;10783:791:42;;;;;;;19725:441;;;19999:67;20082:77;19725:441;1190:11066:41;;:::i;:::-;;19999:67:42;;;;:::i;:::-;1190:11066:41;;20148:10:42;;;;1190:11066:41;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;20082:77:42;1190:11066:41;19725:441:42;;;19999:67;19725:441;20082:77;19725:441;;;;1190:11066:41;;:::i;:::-;;19999:67:42;;;;:::i;1190:11066:41:-;;;-1:-1:-1;;;;;1190:11066:41;;;;;;:::o;:::-;;;;;;;;;;:::o;:::-;;;;;;;;;;;:::i;:::-;;;;:::i;:::-;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;;;;:::i;:::-;;;;;;;;;;;:::i;:::-;;;;;;;;;;;:::i;:::-;;;;;;;;;;;:::i;:::-;;;;;;;;;;;:::i;:::-;;;;;;;;;;;:::i;:::-;;;;;;;;;;;:::i;:::-;;;;;;;;;;;:::i;:::-;;;;;;:::o;12001:2739:42:-;;;;;-1:-1:-1;1190:11066:41;;:::i;:::-;12289:27:42;1676:41:51;12289:27:42;1676:35:51;12289:27:42;;1190:11066:41;;;;;;;;9412:15:39;1190:11066:41;;;;;;;12289:27:42;-1:-1:-1;;;;;1190:11066:41;;;;;;1331:36:51;;;1190:11066:41;;;;;1331:42:51;1190:11066:41;;;1676:35:51;1190:11066:41;;;;;;;;;;;;;;;;1676:41:51;1190:11066:41;12398:10:42;;;1190:11066:41;;;12823:73:42;12566:59;1190:11066:41;;12600:14:42;12398:10;12600:14;;;;;1190:11066:41;12566:59:42;;:::i;:::-;1190:11066:41;;;;;;;12537:12:42;;;1190:11066:41;12687:65:42;1190:11066:41;;12711:40:42;12398:10;12727:14;;:23;1190:11066:41;12711:40:42;;:::i;:::-;532:62:19;;;;;;444:156;;12687:65:42;1190:11066:41;;;;12823:73:42;;:::i;:::-;1190:11066:41;;;;;13045:11:42;;13041:28;;-1:-1:-1;;;;;1190:11066:41;;;;;;11744:7;1190:11066;;;;;13270:13:42;13715:32;13270:13;:86;:13;;;13325:14;;;1190:11066:41;13270:20:42;;;;;:86;:::i;:::-;13569:100;12537:12;13366;;1190:11066:41;13366:34:42;1190:11066:41;;;13366:34:42;:::i;:::-;1190:11066:41;;13618:50:42;13569:35;;;1190:11066:41;12398:10:42;1190:11066:41;;13644:14:42;;:23;1190:11066:41;13618:50:42;;:::i;13569:100::-;1190:11066:41;;13715:32:42;;:::i;:::-;1331:36:51;3670:20:52;;1190:11066:41;3670:20:52;;;1190:11066:41;;13792:23:42;13788:266;;12001:2739;-1:-1:-1;14254:5:42;1190:11066:41;14254:20:42;;-1:-1:-1;;;;;1190:11066:41;;;14254:20:42;1190:11066:41;;-1:-1:-1;;;14254:32:42;;-1:-1:-1;;;;;1190:11066:41;;13270:20:42;14254:32;;1190:11066:41;;14254:32:42;;;;1190:11066:41;;;;;;14254:32:42;;;;;;;14727:5;14254:32;14682:34;14254:32;;14214:82;14254:32;;;14367:58;14254:32;-1:-1:-1;14254:32:42;;;12001:2739;14214:82;;;:::i;:::-;14165:13;;;1190:11066:41;14165:13:42;;1190:11066:41;;;;14367:58:42;;:::i;:::-;1190:11066:41;14575:5:42;14534:23;1190:11066:41;;;14534:23:42;:::i;:::-;14559:14;;14575:5;;;;:::i;:::-;1190:11066:41;14682:34:42;:::i;:::-;14727:5;;:::i;14254:32::-;;;;;;;-1:-1:-1;14254:32:42;;;;;;;:::i;:::-;;;;;:::i;:::-;;;;;;;;;;;13788:266;13936:69;;;14019:24;13936:69;;;:::i;:::-;1190:11066:41;;13900:105:42;;;;;;14019:24;:::i;:::-;1190:11066:41;;13788:266:42;;;12001:2739;;;;;;1190:11066:41;;:::i;:::-;12289:27:42;1676:41:51;12289:27:42;1676:35:51;12289:27:42;;1190:11066:41;;;;;;;;9412:15:39;1190:11066:41;;;;;;;1676:41:51;1190:11066:41;12398:10:42;;;1190:11066:41;;;12823:73:42;12566:59;1190:11066:41;;12600:14:42;12398:10;12600:14;;;;;1190:11066:41;12566:59:42;;:::i;12823:73::-;1190:11066:41;;;;;13045:11:42;;13041:28;;-1:-1:-1;;;;;1190:11066:41;;;;;;11744:7;1190:11066;;;;;13270:13:42;13715:32;13270:13;:86;:13;;;13325:14;;;1190:11066:41;13270:20:42;;;;;:86;:::i;13715:32::-;1331:36:51;3670:20:52;;1190:11066:41;3670:20:52;;;1190:11066:41;;13792:23:42;13788:266;;-1:-1:-1;14254:5:42;1190:11066:41;14254:20:42;;-1:-1:-1;;;;;1190:11066:41;;;13788:266:42;13936:69;;;14019:24;13936:69;;;:::i;8365:566:21:-;-1:-1:-1;;;1190:11066:41;;8701:224:21;;;;;;;;;;;;;;;;;;;;;8365:566::o;3183:683:38:-;-1:-1:-1;;;;;1190:11066:41;-1:-1:-1;1190:11066:41;;;3376:15:38;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;3346:513:38;;1190:11066:41;;;;3183:683:38;1190:11066:41;3577:38:38;;;:::i;:::-;1190:11066:41;;;;;;:::i;:::-;;;;3485:364:38;;1190:11066:41;;3485:364:38;;1190:11066:41;3485:364:38;;;1190:11066:41;-1:-1:-1;3485:364:38;;;1190:11066:41;3752:19:38;1190:11066:41;3485:364:38;;1190:11066:41;;3485:364:38;;;1190:11066:41;3827:7:38;3485:364;;;1190:11066:41;3431:40:38;;;3346:513;;:::i;:::-;3333:526;3183:683;:::o;20213:467:42:-;;;20494:70;20213:467;20580:93;20213:467;;;;1190:11066:41;;:::i;:::-;;20494:70:42;;;;:::i;:::-;1190:11066:41;;20662:10:42;;;;1190:11066:41;;;;20597:10:42;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;;;;20580:93:42;1190:11066:41;14805:2482:42;;;;;;;1190:11066:41;;:::i;:::-;15099:27:42;15385:59;994:41:51;15099:27:42;994:35:51;15099:27:42;;1190:11066:41;;;;;;;;9412:15:39;1190:11066:41;;;;;;;15099:27:42;-1:-1:-1;;;;;1190:11066:41;;;;;;649:36:51;;;1190:11066:41;;;;;649:42:51;1190:11066:41;994:41:51;1190:11066:41;15208:10:42;;;;1190:11066:41;;;;;15419:14:42;;1190:11066:41;15385:59:42;;:::i;:::-;1190:11066:41;;;;;15353:15:42;;;1190:11066:41;15705:73:42;15485:19;;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;;15485:19:42;1190:11066:41;15569:65:42;1190:11066:41;;15593:40:42;15208:10;15609:14;;:23;1190:11066:41;15593:40:42;;:::i;15569:65::-;1190:11066:41;;;;;;15705:73:42;;;:::i;:::-;1190:11066:41;;;;;15927:11:42;;15923:28;;17226:38;16410:28;16065:39;;17275:4;16065:39;;;;;;:::i;:::-;16237:13;;;:87;:13;;;16292:14;;;1190:11066:41;16237:87:42;;;;:::i;:::-;16334:15;;;15353;16334;;1190:11066:41;16334:33:42;1190:11066:41;;;16334:33:42;:::i;:::-;1190:11066:41;;16410:28:42;:::i;:::-;3670:20:52;;;1190:11066:41;3670:20:52;;;1190:11066:41;;;;16629:69:42;16712:33;16629:69;16565:13;16629:69;;;:::i;:::-;16565:13;;;;1190:11066:41;;;16712:33:42;:::i;:::-;1190:11066:41;;16482:327:42;17120:4;16912:58;16941:13;;;1190:11066:41;;;16912:58:42;;:::i;:::-;1190:11066:41;17079:23:42;1190:11066:41;;;17079:23:42;:::i;:::-;17104:14;994:35:51;15208:10:42;17104:14;;;17032:20;;17120:4;:::i;17226:38::-;17275:4;;:::i;16482:327::-;16776:13;;;;;1190:11066:41;16482:327:42;;15923:28;15940:11;;;;;;;:::o;4183:682:38:-;-1:-1:-1;;;;;1190:11066:41;-1:-1:-1;1190:11066:41;;;4376:15:38;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;4346:512:38;;1190:11066:41;;;;4183:682:38;1190:11066:41;4577:38:38;;;:::i;:19::-;:38;:::i;:::-;1190:11066:41;;;;;;:::i;:::-;;;;4485:363:38;;1190:11066:41;;4485:363:38;;1190:11066:41;4485:363:38;;;1190:11066:41;;4485:363:38;;;1190:11066:41;4751:19:38;1190:11066:41;4485:363:38;;1190:11066:41;;4485:363:38;;;1190:11066:41;4376:41:38;4485:363;;;1190:11066:41;4376:41:38;4431:40;;;4376:41;;4346:512;:::i;20736:394:42:-;-1:-1:-1;;;;;1190:11066:41;;;-1:-1:-1;1190:11066:41;;;17584:15:42;1190:11066:41;;;;;;;;;;;;;17642:25:42;;1190:11066:41;;;;;;;;;20736:394:42;;;;;;17791:34;;1190:11066:41;;;-1:-1:-1;1190:11066:41;17713:7:42;1190:11066:41;;17741:17:42;1190:11066:41;;17713:26:42;1190:11066:41;-1:-1:-1;1190:11066:41;17713:26:42;1190:11066:41;;17741:17:42;;:::i;:::-;17791:34;;:::i;:::-;1190:11066:41;;;;;;;17836:35:42;21043:80;17836:35;;;21043:80;17836:35;1190:11066:41;;;;;;;;;;;;;;;;17836:35:42;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;17902:16:42;1190:11066:41;;;;;8390:50:30;;17902:26:42;;8390:50:30;:::i;:::-;;1190:11066:41;;;;;;;;;21043:80:42;;1190:11066:41;;;;;;;;;;;;;;;7782:409:21;8042:143;;;;;;;;-1:-1:-1;;;8042:143:21;;7782:409;:::o;21188:419:42:-;;1190:11066:41;21188:419:42;21503:97;21188:419;;;1190:11066:41;;;;;;;;;;;;-1:-1:-1;1190:11066:41;18263:15:42;1190:11066:41;;18544:35:42;18321:25;1190:11066:41;-1:-1:-1;1190:11066:41;18321:25:42;:35;18500:32;18321:35;;;1190:11066:41;;;;;;;;;;;;;;;;18321:35:42;1190:11066:41;;;-1:-1:-1;1190:11066:41;18392:7:42;1190:11066:41;;18420:17:42;1190:11066:41;;18392:26:42;1190:11066:41;-1:-1:-1;1190:11066:41;18392:26:42;1190:11066:41;;18420:17:42;;:::i;:::-;18500:32;;:::i;:::-;532:62:19;;;;;;18544:35:42;;;1190:11066:41;;;;;;;;;;;;;;;;18544:35:42;1190:11066:41;18614:22:42;;18610:73;;21188:419;1190:11066:41;;21530:10:42;;1190:11066:41;;;;;;;;;;;;;21503:97:42;;21188:419::o;18610:73::-;-1:-1:-1;;;;;1190:11066:41;;;;;;17902:16:42;1190:11066:41;;;;;8711:53:30;;18638:26:42;;8711:53:30;:::i;:::-;;18610:73:42;;21967:285;;;;22093:11;;22089:35;;22185:28;;;;:::i;:::-;1190:11066:41;;;;;;;22135:110:42;21967:285;:::o;22089:35::-;22106:18;;22103:1;22106:18;;:::o;22673:453::-;;;;22838:11;;22834:43;;22917:24;;;;:::i;:::-;165:70:19;;;;;;;;822:5:46;;;;;;;;;23057:29:42;;;;:::i;:::-;22961:158;;532:62:19;;;;;;;22673:453:42;:::o;22834:43::-;22851:26;;-1:-1:-1;22848:1:42;;22851:26;:::o;23674:501::-;;;;23927:11;;23923:50;;-1:-1:-1;;;;;1190:11066:41;;-1:-1:-1;1190:11066:41;;;2375:15:38;1190:11066:41;;;;;2345:514:38;;1190:11066:41;2375:41:38;;;;2576:38;;:19;;2430:40;;:27;:40;2576:19;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;;2576:38:38;1190:11066:41;;;;;;:::i;:::-;;;;2484:365:38;;1190:11066:41;2484:365:38;1190:11066:41;2484:365:38;;1190:11066:41;-1:-1:-1;2484:365:38;;;1190:11066:41;;2484:365:38;;;1190:11066:41;2750:19:38;1190:11066:41;2484:365:38;;1190:11066:41;-1:-1:-1;2484:365:38;;;1190:11066:41;2826:8:38;2484:365;;;1190:11066:41;2345:514:38;:::i;:::-;822:5:46;;;;;;;;;;;;;;24100:68:42;822:5:46;-1:-1:-1;822:5:46;23674:501:42;:::o;23923:50::-;23940:33;-1:-1:-1;23940:33:42;-1:-1:-1;23940:33:42;:::o;23674:501::-;;;;23927:11;;23923:50;;-1:-1:-1;;;;;1190:11066:41;;-1:-1:-1;1190:11066:41;;;2375:15:38;1190:11066:41;;;;;2345:514:38;;1190:11066:41;2375:41:38;;;;2576:38;;:19;;2430:40;;:27;1190:11066:41;2576:38:38;1190:11066:41;;;;;;:::i;:::-;;;;2484:365:38;;1190:11066:41;2484:365:38;1190:11066:41;2484:365:38;;1190:11066:41;2484:365:38;;;;1190:11066:41;;2484:365:38;;;1190:11066:41;2750:19:38;1190:11066:41;2484:365:38;;1190:11066:41;-1:-1:-1;2484:365:38;;;1190:11066:41;2826:8:38;2484:365;;;1190:11066:41;2345:514:38;:::i;:::-;822:5:46;;;;;;;;;;;;;;;;;24100:68:42;;23674:501;:::o;23923:50::-;23940:33;23937:1;23940:33;;:::o;23674:501::-;;;;;23927:11;;23923:50;;-1:-1:-1;;;;;1190:11066:41;23937:1:42;1190:11066:41;;;1374:15:38;1190:11066:41;;;;;;;;11744:7;1190:11066;;;;;;1344:515:38;;1190:11066:41;;1575:38:38;;3577;:::i;1575:::-;1190:11066:41;;;;;;:::i;:::-;;;;1483:366:38;;1190:11066:41;1483:366:38;1190:11066:41;1483:366:38;;1190:11066:41;1483:366:38;;;;1190:11066:41;23937:1:42;1483:366:38;;;1190:11066:41;1750:19:38;1190:11066:41;1483:366:38;;1190:11066:41;23937:1:42;1483:366:38;;;1190:11066:41;1429:40:38;1483:366;;;1190:11066:41;1429:40:38;;;1344:515;;:::i;:::-;822:5:46;;;;;;;;;;;24137:30:42;;;:::i;:::-;24100:68;;;23674:501;:::o;23923:50::-;-1:-1:-1;23940:33:42;;23937:1;;23940:33::o;1190:11066:41:-;;;;;;;:::i;:::-;-1:-1:-1;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;:::o;24819:1995:42:-;;;;;;1190:11066:41;;:::i;:::-;25144:44:42;1190:11066:41;;:::i;:::-;;25202:16:42;1190:11066:41;;;;;;;25202:21:42;25198:87;;24819:1995;-1:-1:-1;1190:11066:41;;-1:-1:-1;;;25363:35:42;;;;-1:-1:-1;;;;;1190:11066:41;;25363:35:42;;1190:11066:41;;25363:35:42;;1190:11066:41;;;;25363:35:42;;;;;;;-1:-1:-1;25363:35:42;;;24819:1995;-1:-1:-1;25467:5:42;1190:11066:41;;;;;25467:22:42;;-1:-1:-1;;;;;1190:11066:41;;;25467:22:42;1190:11066:41;;-1:-1:-1;;;25467:42:42;;;-1:-1:-1;;;;;1190:11066:41;;25363:35:42;25467:42;;1190:11066:41;1201:71:34;1190:11066:41;;;;25467:42:42;1190:11066:41;;;;25467:42:42;;;;;;;25579:44;25467:42;;;-1:-1:-1;25467:42:42;;;24819:1995;-1:-1:-1;1190:11066:41;;25579:44:42;;;-1:-1:-1;;;;;1190:11066:41;;;25363:35:42;25579:44;;1190:11066:41;;;;;;;;;;;;;;;;25579:44:42;;;;;;;;;;-1:-1:-1;25579:44:42;;;24819:1995;25750:25;;;;25695:81;25870:85;25750:25;;1190:11066:41;25750:25:42;;;;1190:11066:41;;;;;;;;;;;;;25695:81:42;25657:22;;;1190:11066:41;;;25750:25:42;25637:18;;1190:11066:41;;;;;25637:18:42;;25657:22;-1:-1:-1;;;;;1190:11066:41;;;;-1:-1:-1;1190:11066:41;;25870:85:42;;;:::i;:::-;1190:11066:41;25830:24:42;;1190:11066:41;;;25808:20:42;;;1190:11066:41;;;25808:20:42;;25830:24;;25808:20;26002:93;;;-1:-1:-1;;1190:11066:41;26024:30:42;1190:11066:41;26002:93:42;;1190:11066:41;;;;26002:93:42;1190:11066:41;;26106:26:42;1190:11066:41;;26173:34:42;;;:::i;:::-;1190:11066:41;;26173:61:42;;;:::i;:::-;1190:11066:41;;;;26255:45:42;;;:::i;:::-;26172:129;;;:::i;:::-;1190:11066:41;;26158:187:42;;;:::i;:::-;-1:-1:-1;;;;;1190:11066:41;;;;;;;9412:15:39;1190:11066:41;;;;;;;;;;;;;;2012:25:51;;;;1190:11066:41;;;;;;;9412:85:39;;;;:::i;:::-;26482:33:42;;;26478:330;;26002:93;24819:1995;;;;;;:::o;26478:330::-;26594:203;26531:33;;;26612:137;26531:33;;;;26703:45;26531:33;;26613:40;26531:33;26613:65;26531:33;1190:11066:41;;26613:40:42;;:::i;:::-;1190:11066:41;;26613:65:42;;:::i;:::-;1190:11066:41;;;;26703:45:42;;:::i;:::-;26612:137;;:::i;:::-;1190:11066:41;;26594:203:42;;:::i;:::-;26478:330;;;;;;;;;26002:93;3298:2:11;1190:11066:41;;;3298:2:11;;;26002:93:42;;25579:44;25695:81;25579:44;;;25870:85;25579:44;;1190:11066:41;25579:44:42;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;25467:42;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;25363:35;;;;;;;;;;;;;;:::i;:::-;;;;25198:87;25241:5;1190:11066:41;;;-1:-1:-1;25241:44:42;;-1:-1:-1;;1190:11066:41;25241:26:42;;-1:-1:-1;;;;;1190:11066:41;;;25241:44:42;;;;;;;;;;-1:-1:-1;25241:44:42;;;25198:87;25225:60;25198:87;;;;25241:44;;;;;;;;;;;;:::i;:::-;;;;5747:627:20;6089:279;;;;;;;;;;;;;;;;;;;;5747:627;:::o;1130:1084:47:-;;;;;;;1398:11;;1394:25;;1748:14;;1916:38;1996:41;1748:14;;;;;;1853;;1930:23;1190:11066:41;1916:38:47;;:::i;:::-;2012:24;;1190:11066:41;1996:41:47;;:::i;:::-;1964:28;;;;1190:11066:41;;;;;;;;;;;;649:36:51;1190:11066:41;;2047:27:47;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;-1:-1:-1;;;;;;;;;;;2112:95:47;1190:11066:41;;;;2112:95:47;1190:11066:41;1394:25:47;-1:-1:-1;1408:1:47;;-1:-1:-1;;;;1411:8:47:o;1130:1084::-;;;;;;;;1398:11;;1394:25;;1765:14;1870;1916:38;1996:41;1765:14;;;;;1870;;;1930:23;1190:11066:41;1916:38:47;;:::i;1996:41::-;1964:28;;;;1190:11066:41;;;;;;;;;;;;2047:27:47;1190:11066:41;;2047:27:47;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;-1:-1:-1;;;;;;;;;;;2112:95:47;1190:11066:41;;;;2112:95:47;1190:11066:41;2772:1070:47;;;;3002:11;;2998:24;;3685:38;3644:27;-1:-1:-1;;;;;;;;;;;3363:14:47;3468;3363;;3468;;;3523:27;3564:39;3523:27;;;1190:11066:41;3468:14:47;1190:11066:41;;3579:23:47;;1190:11066:41;3564:39:47;;:::i;:::-;532:62:19;;;;;;1190:11066:41;;;3644:27:47;1190:11066:41;3468:14:47;1190:11066:41;;3699:23:47;;1190:11066:41;3685:38:47;;:::i;:::-;532:62:19;;;;;;1190:11066:41;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;3740:95:47;1190:11066:41;2772:1070:47;;;3002:11;;2998:24;;3685:38;649:36:51;-1:-1:-1;;;;;;;;;;;3346:14:47;;;;;3451;;3523:27;3564:39;3523:27;;;1190:11066:41;3346:14:47;1190:11066:41;;3579:23:47;;1190:11066:41;3564:39:47;;:::i;:::-;532:62:19;;;;;;1190:11066:41;;;3644:27:47;1190:11066:41;3346:14:47;1190:11066:41;;3699:23:47;;1190:11066:41;3685:38:47;;:::i;:::-;532:62:19;;;;;;1190:11066:41;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;3740:95:47;1190:11066:41;781:1118:50;;1620:272;1035:22;;;1190:11066:41;1001:267:50;1071:22;;;1190:11066:41;1107:24:50;;;1190:11066:41;1155:24:50;;;;;;;1190:11066:41;1203:21:50;;;;1190:11066:41;1238:20:50;;;;1190:11066:41;1001:267:50;;:::i;:::-;1190:11066:41;1298:292:50;1190:11066:41;;1155:24:50;1377:35;;1190:11066:41;1426:24:50;;;1464:13;;;;;;;:20;1190:11066:41;1559:21:50;1155:24;1190:11066:41;;1510:35:50;;1190:11066:41;1559:21:50;;1190:11066:41;1298:292:50;;:::i;:::-;1649:36;1155:24;1071:22;1035;1649:36;;1190:11066:41;1699:35:50;;1190:11066:41;1748:24:50;;1786:13;;:20;;1190:11066:41;1155:24:50;1190:11066:41;;1832:35:50;;1190:11066:41;1620:272:50;;:::i;2536:1543::-;;;;2895:46;2990;2536:1543;;;1190:11066:41;;:::i;:::-;2895:46:50;;:::i;:::-;1190:11066:41;;;;2990:46:50;:::i;:::-;2951:36;;;1190:11066:41;;;2951:36:50;3051:76;-1:-1:-1;3051:76:50;;1190:11066:41;;;;;;9182:567:20;;;;;;;;;;3789:224;;;9182:567;;;;;;;;;;;;;-1:-1:-1;9182:567:20;;;;;;;;3554:100:50;9182:567:20;3386:100:50;3404:82;9182:567:20;3405:54:50;9182:567:20;3789:224;3573:54:50;9182:567:20;3572:82:50;9182:567:20;;;;;;3405:54:50;;;;;:::i;:::-;3404:82;:::i;:::-;3386:100;;:::i;:::-;3332:35;;;1190:11066:41;;3573:54:50;:::i;3554:100::-;3500:35;;;1190:11066:41;2536:1543:50:o;3047:1026::-;3900:35;;;;;;;;1190:11066:41;3988:35:50;;;1190:11066:41;2536:1543:50:o;4595:1260::-;;;;;4887:19;;:64;;;;4595:1260;4883:146;;1190:11066:41;5138:43:50;5160:20;5087:41;:95;1190:11066:41;;;5087:41:50;;:::i;:::-;5160:20;;1190:11066:41;5138:43:50;;;:::i;:::-;5087:95;;:::i;:::-;686:4:21;-1:-1:-1;;;165:70:19;;;;;;;;;822:5:46;;;;;;;;;5781:40:50;5689:73;;;;:::i;:::-;5781:40;;:::i;:::-;1190:11066:41;;;;;;;5648:200:50;;;:::i;4883:146::-;4974:20;:44;:20;;;;;;;1190:11066:41;4974:44:50;:::i;4887:64::-;4911:16;;;:39;;4887:64;4911:39;;4931:19;4887:64;;4595:1260;;;;;;;4887:19;;:64;;;;4595:1260;4883:146;;1190:11066:41;5138:43:50;5160:20;5087:41;:95;1190:11066:41;;;5087:41:50;;:::i;:::-;5160:20;;1190:11066:41;;;5138:43:50;;:::i;5087:95::-;686:4:21;-1:-1:-1;;;822:5:46;;;;;;;;;165:70:19;;;;;;;;1190:11066:41;;822:5:46;;;;;;;;;;;;;;;;;;;;5781:40:50;5689:149;:73;;5648:200;5689:73;:132;:73;;:::i;:149::-;5648:200;;:::i;4883:146::-;4974:20;;;:44;:20;;;;;;;1190:11066:41;4974:44:50;:::i;4887:64::-;4911:16;;;:39;;4887:64;4911:39;4931:19;;;4887:64;;9088:192:29;-1:-1:-1;;;;;9172:26:29;;;;1190:11066:41;;;9088:192:29;:::o;1190:11066:41:-;;;-1:-1:-1;;;1190:11066:41;;;;;;;;;;;;;;;;;-1:-1:-1;;;1190:11066:41;;;;;;;8762:312:52;;1190:11066:41;;:::i;:::-;;;-1:-1:-1;;;;;1190:11066:41;;;;;;9003:22:52;;;1190:11066:41;8762:312:52:o;9136:::-;;9335:21;1190:11066:41;;:::i;:::-;9335:21:52;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;;;;9377:22:52;;;1190:11066:41;9136:312:52:o;9503:214::-;;9686:24;1190:11066:41;;:::i;:::-;9635:24:52;;;;:::i;:::-;9618:41;;9686:24;:::i;:::-;9669:14;;;:41;9503:214::o;9835:521::-;9954:17;;;1190:11066:41;9985:15:52;;;9981:29;;10048:35;;:74;:35;;1190:11066:41;;;;;10048:74:52;;:::i;:::-;10136:21;;10132:35;;10295:37;;;:::i;:::-;-1:-1:-1;;;165:70:19;;;;;;;;9835:521:52;:::o;10132:35::-;10159:8;;-1:-1:-1;10159:8:52;:::o;10818:978::-;;;;;;;11088:82;:21;;1190:11066:41;3298:2:11;11128:41:52;3298:2:11;;3439;3298;;11128:41:52;:::i;:::-;3298:2:11;4191:3;3298:2;;11088:82:52;:::i;:::-;11184:14;;;11180:38;;11295:13;;;1190:11066:41;11287:42:52;;11295:13;11287:42;;:40;;:22;;-1:-1:-1;;;;;1190:11066:41;11287:22:52;1190:11066:41;11287:40:52;1190:11066:41;;-1:-1:-1;;;11287:42:52;;1190:11066:41;;;;;11287:42:52;;;;;;;11250:193;11287:42;11286:147;11287:42;;:82;:42;11088:21;11287:42;;;10818:978;11332:37;;;;;:::i;11287:82::-;11395:14;;1190:11066:41;11286:147:52;;:::i;11250:193::-;11457:20;;;;;11453:44;;11688:51;11590:17;11537:19;;11688:51;11537:19;;:::i;:::-;11590:17;;1190:11066:41;11590:38:52;1190:11066:41;;;11590:38:52;:::i;:::-;1190:11066:41;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;;;;;;;;11688:51:52;;;;11750:39;10818:978;:::o;11453:44::-;11479:18;-1:-1:-1;11088:21:52;;11479:18;-1:-1:-1;;11479:18:52:o;11287:42::-;11332:37;11287:42;;;;;;;;;;;;;;:::i;:::-;;;;11180:38;11200:18;;;;;;;;11088:21;11200:18;:::o;12050:600::-;;;;;12207:11;;12203:30;;12265:17;;1190:11066:41;;12296:15:52;;;12292:39;;1190:11066:41;12539:51:52;165:70:19;;;;;;;;;;532:62;;;;;;;;1190:11066:41;;;;;;;;;;;;;;12539:51:52;;822:5:46;;;;;;;;12601:42:52;12050:600;:::o;12292:39::-;12313:18;;;;;;12217:1;12313:18;:::o;12203:30::-;12220:13;;;;12217:1;12220:13;12217:1;12220:13;:::o;13590:394::-;;13911:56;13590:394;13789:20;13826:58;13860:14;13826:26;;;1190:11066:41;13860:14:52;;;:23;:14;:23;;1190:11066:41;13826:58:52;;:::i;:::-;13789:20;;1190:11066:41;;;13911:56:52;;:::i;14282:632::-;;;14439:11;;14435:25;;14545:91;14562:73;:29;;;;:::i;:::-;13150:26;;;1190:11066:41;13235:56:52;13150:58;13184:14;;:23;;;;1190:11066:41;13150:58:52;;:::i;:::-;14501:13;;;;1190:11066:41;;;13235:56:52;;:::i;:::-;532:62:19;;;;;;13315:17:52;;;1190:11066:41;532:62:19;;;;;;;;;;;;444:156;;14562:73:52;14545:91;86:155:19;165:70;;;;;;;;86:155;;14545:91:52;14651:15;;;14647:34;;14735:89;:28;14888:19;14735:28;;1190:11066:41;14777:46:52;13184:23;1190:11066:41;;;14799:14:52;;;:23;1190:11066:41;14777:46:52;;:::i;14735:89::-;1190:11066:41;;14888:19:52;:::i;14647:34::-;14668:13;;;;;:::o;893:548:53:-;;;1118:11;;1114:24;;1190:11066:41;;;;1193:32:53;;;:::i;:::-;1190:11066:41;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;1378:56:53;;1190:11066:41;;1378:56:53;893:548::o;:::-;;;1118:11;;1114:24;;1190:11066:41;;;;1193:32:53;;;:::i;:::-;1190:11066:41;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;1302:56:53;;1190:11066:41;;1302:56:53;893:548::o;2017:809::-;;;;;2245:11;;2241:30;;1190:11066:41;;2335:16:53;;2331:40;;2638:56;2411:31;;2529;2411;;1190:11066:41;2411:31:53;;:::i;:::-;165:70:19;;;;;;;;2529:31:53;;;:::i;:::-;532:62:19;;;;;;1190:11066:41;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;2638:56:53;822:5:46;;;;;;;;2781:38:53;2017:809;:::o;2331:40::-;2353:18;;;;;;;2255:1;2353:18;:::o;2241:30::-;2258:13;;;;;2255:1;2258:13;2255:1;2258:13;:::o;2017:809::-;;;;;2245:11;;2241:30;;1190:11066:41;;2335:16:53;;2331:40;;2714:56;2411:31;;2529;2411;;1190:11066:41;2411:31:53;;:::i;840:233:54:-;;;;948:20;840:233;948:20;;:::i;:::-;:25;944:38;;-1:-1:-1;;;;;1190:11066:41;;;992:74:54;;;;;972:1;1190:11066:41;;454:1:46;1190:11066:41;;;1201:71:34;;;;;;;;992:74:54;;1190:11066:41;992:74:54;;;1190:11066:41;454:1:46;;;1190:11066:41;1032:4:54;454:1:46;;;1190:11066:41;454:1:46;;;;;992:74:54;;;;;;;;840:233;:::o;992:74::-;;;;:::i;944:38::-;975:7;;;:::o;1140:240::-;1235:11;;1231:24;;-1:-1:-1;;;;;1190:11066:41;;;1265:108:54;;;;;1245:1;1190:11066:41;;565:1:46;1190:11066:41;;;1201:71:34;;;;;;;;1265:108:54;;1190:11066:41;1265:108:54;;;1190:11066:41;565:1:46;;;1190:11066:41;565:1:46;;;;1190:11066:41;565:1:46;;;;454;1367:4:54;565:1:46;;;1190:11066:41;1265:108:54;;;;;;;;1140:240;:::o;1533:337::-;;;1652:11;;:88;;;;;1533:337;1648:101;;;1190:11066:41;;-1:-1:-1;;;1759:79:54;;-1:-1:-1;;;;;1190:11066:41;;;1759:79:54;;;1190:11066:41;;;;;;;;;;;;;1832:4:54;1190:11066:41;;;;;;1759:79:54;;1190:11066:41;;;;;;-1:-1:-1;;1190:11066:41;1759:79:54;;;;;;;;1533:337;:::o;1759:79::-;;;;;;;;;;;;;:::i;:::-;;1533:337::o;1652:88::-;1190:11066:41;;-1:-1:-1;;;1667:68:54;;1729:4;1667:68;;;1190:11066:41;;-1:-1:-1;1667:68:54;;1190:11066:41;;;;;;-1:-1:-1;;;;;1190:11066:41;1667:68:54;;;;;;;1662:1;1667:68;;;1652:88;1667:73;;1652:88;;;1667:68;;;;;;;;;;;;;;:::i;:::-;;;;2059:373;;;2172:11;;2168:24;;1190:11066:41;;-1:-1:-1;;;2318:40:54;;2352:4;2318:40;;;1190:11066:41;2318:40:54;;-1:-1:-1;;;;;1190:11066:41;;;;2318:40:54;;1190:11066:41;;;;;;;;2318:40:54;;;;;;;2377:48;2318:40;;;2182:1;2318:40;;;;;2059:373;-1:-1:-1;1190:11066:41;;-1:-1:-1;;;2377:48:54;;-1:-1:-1;;;;;1190:11066:41;;;2318:40:54;2377:48;;1190:11066:41;165:70:19;;;;;-1:-1:-1;165:70:19;;;;;;;1190:11066:41;;;;2352:4:54;1190:11066:41;;;;;;;;;;-1:-1:-1;1190:11066:41;;;;;;;2377:48:54;;1190:11066:41;;2377:48:54;;;;;;;;2059:373;;:::o;2377:48::-;;;;;;-1:-1:-1;2377:48:54;;;;;;:::i;2318:40::-;;;;;;;;;;;;;;:::i;:::-;;;;2677:319;1190:11066:41;;-1:-1:-1;;;2869:43:54;;-1:-1:-1;;;;;1190:11066:41;;;2869:43:54;;;1190:11066:41;;;;;;;2869:43:54;;;1190:11066:41;;;;2869:43:54;;;;;;;2940:49;2869:43;;;-1:-1:-1;2869:43:54;;;2677:319;-1:-1:-1;1190:11066:41;;-1:-1:-1;;;2940:49:54;;-1:-1:-1;;;;;1190:11066:41;;;2869:43:54;2940:49;;1190:11066:41;;;;;;;;;;;;;;;;2940:49:54;;;;;;;;;;-1:-1:-1;2940:49:54;;;2922:67;;2677:319;:::o;2940:49::-;;;;;;-1:-1:-1;2940:49:54;;;;;;:::i;2869:43::-;;;;;;;;;;;;;;:::i;:::-;;;;1190:11066:41;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;1518:1530:55:-;;1713:40;:21;;3298:2:11;15139:208;1190:11066:41;4063:2:11;3298;;15139:208;;1713:40:55;1767:18;;1763:56;;2041:30;;;1190:11066:41;2024:64:55;;;;:62;;:48;;-1:-1:-1;;;;;1190:11066:41;2024:48:55;1190:11066:41;2024:62:55;1190:11066:41;;-1:-1:-1;;;2024:64:55;;1190:11066:41;;;;;2024:64:55;;;;;;1713:21;;;;;2024:64;;;1518:1530;2024:64;2151:32;;;;2132:72;:70;:52;1190:11066:41;2151:32:55;;;1190:11066:41;;;;;;;;;2132:70:55;1190:11066:41;;-1:-1:-1;;;2132:72:55;;1190:11066:41;;;;;2132:72:55;;;;;;;1713:21;2132:72;;;1518:1530;2278:14;2132:72;2278:14;;;1190:11066:41;2247:56:55;;;;:::i;:::-;2376:27;;;;1190:11066:41;;;-1:-1:-1;;;;;1190:11066:41;;;;-1:-1:-1;;;;;1190:11066:41;2345:59:55;;;:::i;:::-;2599:27;;;;1190:11066:41;;;;;;;;;;2488:152:55;;;;:::i;:::-;2444:206;;;:::i;:::-;2700:43;;;;:::i;:::-;:67;;;;:::i;:::-;:89;;;;:::i;:::-;2803:21;;2799:59;;2993:25;2900:75;2993:48;2900:42;;;2993:48;2900:42;1190:11066:41;2900:42:55;;:::i;:::-;2950:14;;1190:11066:41;2900:75:55;;:::i;:::-;2993:25;;1190:11066:41;-1:-1:-1;;;;;1190:11066:41;;;2799:59:55;-1:-1:-1;;;2833:25:55;;1190:11066:41;2826:32:55;;-1:-1:-1;;;;;1190:11066:41;;;2132:72:55;;;;;;;;;;;;;;;:::i;:::-;;;;;2024:64;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;:::i;:::-;;;-1:-1:-1;2024:64:55;;;;;;;;;;1763:56;-1:-1:-1;;1794:25:55;;1190:11066:41;1787:32:55;;-1:-1:-1;;;;;1190:11066:41;;;1782:972:14;;1190:11066:41;;822:5:46;;;;;;;;;2044:8:14;2040:50;;-1:-1:-1;;1190:11066:41;;;;2268:1:14;2262:7;;2268:1;;;1190:11066:41;2694:10:14;1190:11066:41;;;;2262:21:14;;2655:1;2570:48;376:8;2307:17;;;;:::i;:::-;1190:11066:41;2570:31:14;376:8;2389:25;2570:17;2467:32;2389:25;;;;;:::i;:::-;1190:11066:41;2467:17:14;;;;;:::i;:32::-;1190:11066:41;;2570:17:14;;;:::i;:48::-;1190:11066:41;2694:10:14;;:::i;:::-;1190:11066:41;-1:-1:-1;;;1190:11066:41;;;;;;;;2676:73:14;:61;;;;:::i;2262:21::-;2694:10;2262:21;;;;2040:50;2062:21;;-1:-1:-1;;;2062:21:14;:::o;2253:319:15:-;5571:155:21;;;2397:171:15;;;;;;;;;;-1:-1:-1;;;2397:171:15;;5571:155:21;2397:171:15;;;2253:319::o;1190:11066:41:-;;;;;;;;;;;;;;;;;;;;;;;;;;;7904:16:38;;;1190:11066:41;7886:35:38;1190:11066:41;;7886:35:38;;:::i;:::-;165:70:19;;;;;;;;822:5:46;;;;;;;;;8021:35:38;1190:11066:41;;8021:35:38;;:::i;:::-;1190:11066:41;;;;;;;8095:34:38;1190:11066:41;;8095:34:38;;:::i;:::-;822:5:46;;;;;;;1190:11066:41;;;:::o;:::-;;7034:37:38;1190:11066:41;;7034:37:38;;:::i;:::-;165:70:19;;;;;;;;822:5:46;;;;;;;;7171:35:38;1190:11066:41;;7171:35:38;;:::i;:::-;822:5:46;;;;;;;7262:16:38;7245:34;7262:16;;1190:11066:41;7245:34:38;;:::i;:::-;1190:11066:41;;;;;;;;;;:::o;:::-;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;1190:11066:41;;;-1:-1:-1;1190:11066:41;;;20583:7:39;1190:11066:41;;;;;;;;20583:37:39;;;;1190:11066:41;20652:15:39;1190:11066:41;;;;;;;20558:260:39;;;20707:40;;;;20652:41;;;;;1190:11066:41;;;;;20558:260:39;:::i;1190:11066:41:-;;;;;;19410:249:39;1190:11066:41;;;;;;;;;;-1:-1:-1;1190:11066:41;19435:7:39;1190:11066:41;;19435:26:39;1190:11066:41;-1:-1:-1;1190:11066:41;19435:26:39;1190:11066:41;;;19493:15:39;1190:11066:41;;;-1:-1:-1;1190:11066:41;19548:40:39;;;;19410:249;;:::i

Swarm Source

ipfs://1137663bcc6b00c2b83e4e7ecd3f254cf570a713c77ffa3cc4811f6624cc627b

Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
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  ]

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.