ETH Price: $2,861.91 (-2.70%)

Contract

0x2f38Ccbf21A1757cad08a1dEdD9b049cF4C364Ac

Overview

ETH Balance

0 ETH

ETH Value

$0.00

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Block
From
To

There are no matching entries

> 10 Internal Transactions found.

Latest 25 internal transactions (View All)

Parent Transaction Hash Block From To
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860672023-03-21 10:16:201041 days ago1679393780
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720860562023-03-21 10:16:181041 days ago1679393778
0x2f38Ccbf...cF4C364Ac
0 ETH
720857522023-03-21 10:15:011041 days ago1679393701
0x2f38Ccbf...cF4C364Ac
0 ETH
720857522023-03-21 10:15:011041 days ago1679393701
0x2f38Ccbf...cF4C364Ac
0 ETH
720857522023-03-21 10:15:011041 days ago1679393701
0x2f38Ccbf...cF4C364Ac
0 ETH
720847762023-03-21 10:11:001041 days ago1679393460
0x2f38Ccbf...cF4C364Ac
0 ETH
720847762023-03-21 10:11:001041 days ago1679393460
0x2f38Ccbf...cF4C364Ac
0 ETH
720847762023-03-21 10:11:001041 days ago1679393460
0x2f38Ccbf...cF4C364Ac
0 ETH
720847762023-03-21 10:11:001041 days ago1679393460
0x2f38Ccbf...cF4C364Ac
0 ETH
720847762023-03-21 10:11:001041 days ago1679393460
0x2f38Ccbf...cF4C364Ac
0 ETH
720847762023-03-21 10:11:001041 days ago1679393460
0x2f38Ccbf...cF4C364Ac
0 ETH
View All Internal Transactions

Cross-Chain Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
DnGmxJuniorVault

Compiler Version
v0.8.18+commit.87f61d96

Optimization Enabled:
Yes with 256 runs

Other Settings:
default evmVersion
File 1 of 83 : DnGmxJuniorVault.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import { IAToken } from '@aave/core-v3/contracts/interfaces/IAToken.sol';
import { IPool } from '@aave/core-v3/contracts/interfaces/IPool.sol';
import { IPoolAddressesProvider } from '@aave/core-v3/contracts/interfaces/IPoolAddressesProvider.sol';
import { IPriceOracle } from '@aave/core-v3/contracts/interfaces/IPriceOracle.sol';
import { IRewardsController } from '@aave/periphery-v3/contracts/rewards/interfaces/IRewardsController.sol';
import { WadRayMath } from '@aave/core-v3/contracts/protocol/libraries/math/WadRayMath.sol';

import { IERC20 } from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import { IERC20Metadata } from '@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol';
import { OwnableUpgradeable } from '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import { PausableUpgradeable } from '@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol';
import { SafeERC20 } from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';

import { FixedPointMathLib } from '@rari-capital/solmate/src/utils/FixedPointMathLib.sol';

import { FullMath } from '@uniswap/v3-core/contracts/libraries/FullMath.sol';
import { ISwapRouter } from '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';

import { IBalancerVault } from '../interfaces/balancer/IBalancerVault.sol';
import { IDnGmxSeniorVault } from '../interfaces/IDnGmxSeniorVault.sol';
import { IDnGmxJuniorVault, IERC4626 } from '../interfaces/IDnGmxJuniorVault.sol';
import { IDebtToken } from '../interfaces/IDebtToken.sol';
import { IGlpManager } from '../interfaces/gmx/IGlpManager.sol';
import { ISglpExtended } from '../interfaces/gmx/ISglpExtended.sol';
import { IRewardRouterV2 } from '../interfaces/gmx/IRewardRouterV2.sol';
import { IRewardTracker } from '../interfaces/gmx/IRewardTracker.sol';
import { IVault } from '../interfaces/gmx/IVault.sol';
import { IVester } from '../interfaces/gmx/IVester.sol';
import { IDnGmxTraderHedgeStrategy } from '../interfaces/IDnGmxTraderHedgeStrategy.sol';

import { DnGmxJuniorVaultManager } from '../libraries/DnGmxJuniorVaultManager.sol';
import { SafeCast } from '../libraries/SafeCast.sol';

import { ERC4626Upgradeable } from '../ERC4626/ERC4626Upgradeable.sol';

/**
 * @title Delta Neutral GMX Junior Tranche contract
 * @notice Implements the handling of junior tranche which maintains hedges for btc and eth
 * basis the target weights on GMX
 * @notice It is upgradable contract (via TransparentUpgradeableProxy proxy owned by ProxyAdmin)
 * @author RageTrade
 **/
contract DnGmxJuniorVault is IDnGmxJuniorVault, ERC4626Upgradeable, OwnableUpgradeable, PausableUpgradeable {
    using SafeCast for uint256;
    using FullMath for uint256;
    using WadRayMath for uint256;
    using SafeERC20 for IERC20Metadata;
    using FixedPointMathLib for uint256;

    using DnGmxJuniorVaultManager for DnGmxJuniorVaultManager.State;

    uint256 internal constant MAX_BPS = 10_000;
    uint256 internal constant PRICE_PRECISION = 1e30;

    DnGmxJuniorVaultManager.State internal state;

    // these gaps are added to allow adding new variables without shifting down inheritance chain
    uint256[50] private __gaps;

    modifier onlyKeeper() {
        if (msg.sender != state.keeper) revert OnlyKeeperAllowed(msg.sender, state.keeper);
        _;
    }

    modifier whenFlashloaned() {
        if (!state.hasFlashloaned) revert FlashloanNotInitiated();
        _;
    }

    modifier onlyBalancerVault() {
        if (msg.sender != address(state.balancerVault)) revert NotBalancerVault();
        _;
    }

    /* ##################################################################
                                SYSTEM FUNCTIONS
    ################################################################## */

    /// @notice initializer
    /// @param _name name of vault share token
    /// @param _symbol symbol of vault share token
    /// @param _swapRouter uniswap swap router address
    /// @param _rewardRouter gmx reward router address
    /// @param _tokens addresses of tokens used
    /// @param _poolAddressesProvider add
    function initialize(
        string calldata _name,
        string calldata _symbol,
        address _swapRouter,
        address _rewardRouter,
        address _mintBurnRewardRouter,
        DnGmxJuniorVaultManager.Tokens calldata _tokens,
        IPoolAddressesProvider _poolAddressesProvider
    ) external initializer {
        __Ownable_init();
        __Pausable_init();
        __ERC4626Upgradeable_init(address(_tokens.sGlp), _name, _symbol);

        state.weth = _tokens.weth;
        state.wbtc = _tokens.wbtc;
        state.usdc = _tokens.usdc;

        state.swapRouter = ISwapRouter(_swapRouter);
        state.rewardRouter = IRewardRouterV2(_rewardRouter);
        state.mintBurnRewardRouter = IRewardRouterV2(_mintBurnRewardRouter);

        state.poolAddressProvider = _poolAddressesProvider;

        state.glp = IERC20Metadata(ISglpExtended(asset).glp());
        state.glpManager = IGlpManager(IRewardRouterV2(_mintBurnRewardRouter).glpManager());
        state.fsGlp = IERC20(ISglpExtended(asset).stakedGlpTracker());

        state.gmxVault = IVault(state.glpManager.vault());

        state.pool = IPool(state.poolAddressProvider.getPool());
        state.oracle = IPriceOracle(state.poolAddressProvider.getPriceOracle());

        state.aUsdc = IAToken(state.pool.getReserveData(address(state.usdc)).aTokenAddress);

        state.vWbtc = IDebtToken(state.pool.getReserveData(address(state.wbtc)).variableDebtTokenAddress);
        state.vWeth = IDebtToken(state.pool.getReserveData(address(state.weth)).variableDebtTokenAddress);
    }

    /* ##################################################################
                                ADMIN FUNCTIONS
    ################################################################## */

    /// @notice grants allowances for tokens to relevant external contracts
    /// @dev to be called once the vault is deployed
    function grantAllowances() external onlyOwner {
        address aavePool = address(state.pool);
        address swapRouter = address(state.swapRouter);

        // allowance to aave pool for wbtc for repay and supply
        state.wbtc.approve(aavePool, type(uint256).max);
        // allowance to uniswap swap router for wbtc for swap
        state.wbtc.approve(swapRouter, type(uint256).max);

        // allowance to aave pool for weth for repay and supply
        state.weth.approve(aavePool, type(uint256).max);
        // allowance to uniswap swap router for weth for swap
        state.weth.approve(swapRouter, type(uint256).max);
        // allowance to batching manager for weth
        state.weth.approve(address(state.glpManager), type(uint256).max);

        // allowance to aave pool for usdc for supply
        state.usdc.approve(aavePool, type(uint256).max);
        // allowance to swap router for usdc for swap
        state.usdc.approve(address(swapRouter), type(uint256).max);
        // allowance to batching manager for usdc deposits when rebalancing profits
        state.usdc.approve(address(state.glpManager), type(uint256).max);

        // allowance to aave pool for aUSDC transfers to senior tranche
        state.aUsdc.approve(address(state.dnGmxSeniorVault), type(uint256).max);

        // allowance for sGLP to glpManager
        IERC20Metadata(asset).approve(address(state.glpManager), type(uint256).max);

        emit AllowancesGranted();
    }

    /// @notice set admin paramters
    /// @param newKeeper keeper address
    /// @param dnGmxSeniorVault senior vault address
    /// @param newDepositCap deposit cap
    /// @param withdrawFeeBps fees bps on withdrawals and redeems
    function setAdminParams(
        address newKeeper,
        address dnGmxSeniorVault,
        uint256 newDepositCap,
        uint16 withdrawFeeBps,
        uint24 feeTierWethWbtcPool
    ) external onlyOwner {
        if (withdrawFeeBps > MAX_BPS) revert InvalidWithdrawFeeBps();

        state.keeper = newKeeper;
        state.depositCap = newDepositCap;
        state.withdrawFeeBps = withdrawFeeBps;
        state.feeTierWethWbtcPool = feeTierWethWbtcPool;

        state.dnGmxSeniorVault = IDnGmxSeniorVault(dnGmxSeniorVault);

        emit AdminParamsUpdated(newKeeper, dnGmxSeniorVault, newDepositCap, address(0), withdrawFeeBps);
    }

    /// @notice set thresholds
    /// @param slippageThresholdSwapBtcBps (BPS) slippage threshold on btc swaps
    /// @param slippageThresholdSwapEthBps (BPS) slippage threshold on eth swaps
    /// @param slippageThresholdGmxBps (BPS) slippage threshold on sGlp mint and redeem
    /// @param usdcConversionThreshold (usdc amount) threshold amount for conversion of usdc into sGlp
    /// @param wethConversionThreshold (weth amount) threshold amount for weth fees to be compounded into sGlp
    /// @param hedgeUsdcAmountThreshold (usdc amount) threshold amount below which ETH/BTC hedges are not executed
    /// @param partialBtcHedgeUsdcAmountThreshold (usdc amount) threshold amount above which BTC hedge is not fully taken (gets executed in blocks over multiple rebalances)
    /// @param partialEthHedgeUsdcAmountThreshold (usdc amount) threshold amount above which ETH hedge is not fully taken (gets executed in blocks over multiple rebalances)
    function setThresholds(
        uint16 slippageThresholdSwapBtcBps,
        uint16 slippageThresholdSwapEthBps,
        uint16 slippageThresholdGmxBps,
        uint128 usdcConversionThreshold,
        uint128 wethConversionThreshold,
        uint128 hedgeUsdcAmountThreshold,
        uint128 partialBtcHedgeUsdcAmountThreshold,
        uint128 partialEthHedgeUsdcAmountThreshold
    ) external onlyOwner {
        if (slippageThresholdSwapBtcBps > MAX_BPS) revert InvalidSlippageThresholdSwapBtc();
        if (slippageThresholdSwapEthBps > MAX_BPS) revert InvalidSlippageThresholdSwapEth();
        if (slippageThresholdGmxBps > MAX_BPS) revert InvalidSlippageThresholdGmx();

        state.slippageThresholdSwapBtcBps = slippageThresholdSwapBtcBps;
        state.slippageThresholdSwapEthBps = slippageThresholdSwapEthBps;
        state.slippageThresholdGmxBps = slippageThresholdGmxBps;
        state.usdcConversionThreshold = usdcConversionThreshold;
        state.wethConversionThreshold = wethConversionThreshold;
        state.hedgeUsdcAmountThreshold = hedgeUsdcAmountThreshold;
        state.partialBtcHedgeUsdcAmountThreshold = partialBtcHedgeUsdcAmountThreshold;
        state.partialEthHedgeUsdcAmountThreshold = partialEthHedgeUsdcAmountThreshold;

        emit ThresholdsUpdated(
            slippageThresholdSwapBtcBps,
            slippageThresholdSwapEthBps,
            slippageThresholdGmxBps,
            usdcConversionThreshold,
            wethConversionThreshold,
            hedgeUsdcAmountThreshold,
            partialBtcHedgeUsdcAmountThreshold,
            partialEthHedgeUsdcAmountThreshold
        );
    }

    /// @notice set thresholds
    /// @param rebalanceProfitUsdcAmountThreshold (BPS) slippage threshold on btc swaps
    /// @param dnGmxTraderHedgeStrategy (BPS) slippage threshold on btc swaps
    function setParamsV1(
        uint128 rebalanceProfitUsdcAmountThreshold,
        IDnGmxTraderHedgeStrategy dnGmxTraderHedgeStrategy
    ) external onlyOwner {
        state.rebalanceProfitUsdcAmountThreshold = rebalanceProfitUsdcAmountThreshold;
        state.dnGmxTraderHedgeStrategy = dnGmxTraderHedgeStrategy;

        emit ParamsV1Updated(rebalanceProfitUsdcAmountThreshold, dnGmxTraderHedgeStrategy);
    }

    /// @notice set rebalance paramters
    /// @param rebalanceTimeThreshold (seconds) minimum time difference required between two rebalance calls
    /// @dev a partial rebalance (rebalance where partial hedge gets taken) does not count.
    /// @dev setHedgeParams should already have been called.
    /// @param rebalanceDeltaThresholdBps (BPS) threshold difference between optimal and current token hedges for triggering a rebalance
    /// @param rebalanceHfThresholdBps (BPS) threshold amount of health factor on AAVE below which a rebalance is triggered
    function setRebalanceParams(
        uint32 rebalanceTimeThreshold,
        uint16 rebalanceDeltaThresholdBps,
        uint16 rebalanceHfThresholdBps
    ) external onlyOwner {
        if (rebalanceTimeThreshold > 3 days) revert InvalidRebalanceTimeThreshold();
        if (rebalanceDeltaThresholdBps > MAX_BPS) revert InvalidRebalanceDeltaThresholdBps();
        if (rebalanceHfThresholdBps < MAX_BPS || rebalanceHfThresholdBps > state.targetHealthFactor)
            revert InvalidRebalanceHfThresholdBps();

        state.rebalanceTimeThreshold = rebalanceTimeThreshold;
        state.rebalanceDeltaThresholdBps = rebalanceDeltaThresholdBps;
        state.rebalanceHfThresholdBps = rebalanceHfThresholdBps;

        emit RebalanceParamsUpdated(rebalanceTimeThreshold, rebalanceDeltaThresholdBps, rebalanceHfThresholdBps);
    }

    /// @notice set hedge parameters
    /// @param vault balancer vault for ETH and BTC flashloans
    /// @param swapRouter uniswap swap router for swapping ETH/BTC to USDC and viceversa
    /// @param targetHealthFactor health factor to target on AAVE after every rebalance
    /// @param aaveRewardsController AAVE rewards controller for handling additional reward distribution on AAVE
    function setHedgeParams(
        IBalancerVault vault,
        ISwapRouter swapRouter,
        uint256 targetHealthFactor,
        IRewardsController aaveRewardsController
    ) external onlyOwner {
        if (targetHealthFactor > 20_000) revert InvalidTargetHealthFactor();

        state.balancerVault = vault;
        state.swapRouter = swapRouter;
        state.targetHealthFactor = targetHealthFactor;
        state.aaveRewardsController = aaveRewardsController;

        // update aave pool and oracle if their addresses have updated
        IPoolAddressesProvider poolAddressProvider = state.poolAddressProvider;
        IPool pool = IPool(poolAddressProvider.getPool());
        state.pool = pool;
        IPriceOracle oracle = IPriceOracle(poolAddressProvider.getPriceOracle());
        state.oracle = oracle;

        emit HedgeParamsUpdated(vault, swapRouter, targetHealthFactor, aaveRewardsController, pool, oracle);
    }

    /// @notice set GMX parameters
    /// @param _glpManager GMX glp manager
    function setGmxParams(IGlpManager _glpManager) external onlyOwner {
        state.glpManager = _glpManager;
    }

    function setDirectConversion(bool _useDirectConversionBurn, bool _useDirectConversionMint) external onlyOwner {
        state.useDirectConversionBurn = _useDirectConversionBurn;
        state.useDirectConversionMint = _useDirectConversionMint;
    }

    /// @notice pause deposit, mint, withdraw and redeem
    function pause() external onlyOwner {
        _pause();
    }

    /// @notice unpause deposit, mint, withdraw and redeem
    function unpause() external onlyOwner {
        _unpause();
    }

    /// @notice sets feeBps and feeRecipient
    /// @param _feeBps the part of eth rewards earned from GMX earned deducted as protocol fees
    /// @param _feeRecipient recipient address for protocol fees and protocol esGmx
    function setFeeParams(uint16 _feeBps, address _feeRecipient) external onlyOwner {
        if (state.feeRecipient != _feeRecipient) {
            state.feeRecipient = _feeRecipient;
        } else revert InvalidFeeRecipient();

        if (_feeBps > 3000) revert InvalidFeeBps();
        state.feeBps = _feeBps;

        emit FeeParamsUpdated(_feeBps, _feeRecipient);
    }

    /// @notice withdraw accumulated WETH fees
    function withdrawFees() external {
        uint256 amount = state.protocolFee;
        state.protocolFee = 0;
        state.weth.transfer(state.feeRecipient, amount);
        emit FeesWithdrawn(amount);
    }

    /// @notice unstakes and vest protocol esGmx to convert it to Gmx
    function unstakeAndVestEsGmx() external onlyOwner {
        // unstakes the protocol esGMX and starts vesting it
        // this encumbers some glp deposits
        // can stop vesting to enable glp withdraws
        uint256 amount = state.protocolEsGmx;

        state.rewardRouter.unstakeEsGmx(amount);
        IVester(state.rewardRouter.glpVester()).deposit(amount);

        emit EsGmxVested(amount);

        state.protocolEsGmx = 0;
    }

    /// @notice claims vested gmx tokens (i.e. stops vesting esGmx so that the relevant glp amount is unlocked)
    /// @dev when esGmx is vested some GlP tokens are locked on a pro-rata basis, in case that leads to issue in withdrawal this function can be called
    function stopVestAndStakeEsGmx() external onlyOwner {
        // stops vesting and stakes the remaining esGMX
        // this enables glp withdraws
        IVester(state.rewardRouter.glpVester()).withdraw();
        uint256 esGmxWithdrawn = IERC20(state.rewardRouter.esGmx()).balanceOf(address(this));
        state.rewardRouter.stakeEsGmx(esGmxWithdrawn);

        emit EsGmxStaked(esGmxWithdrawn);

        state.protocolEsGmx += esGmxWithdrawn;
    }

    /// @notice claims vested gmx tokens to feeRecipient
    /// @dev vested esGmx gets converted to GMX every second, so whatever amount is vested gets claimed
    function claimVestedGmx() external onlyOwner {
        // stops vesting and stakes the remaining esGMX
        // this can be used in case glp withdraws are hampered
        uint256 gmxClaimed = IVester(state.rewardRouter.glpVester()).claim();

        emit GmxClaimed(gmxClaimed);

        //Transfer all of the gmx received to fee recipient
        IERC20Metadata(state.rewardRouter.gmx()).safeTransfer(state.feeRecipient, gmxClaimed);
    }

    function rebalanceProfit() external onlyOwner {
        (uint256 currentBtc, uint256 currentEth) = state.getCurrentBorrows();
        uint256 totalCurrentBorrowValue = state.getBorrowValue(currentBtc, currentEth); // = total position value of current btc and eth position

        // rebalance profit
        state.rebalanceProfit(totalCurrentBorrowValue);
    }

    function harvestFees() external {
        state.harvestFees();
    }

    /* ##################################################################
                                KEEPER FUNCTIONS
    ################################################################## */
    /// @notice checks if the rebalance can be run (3 thresholds - time, hedge deviation and AAVE HF )
    function isValidRebalance() public view returns (bool) {
        return
            state.isValidRebalanceHF() ||
            state.isValidRebalanceTime() ||
            state.isValidRebalanceDeviation() ||
            state.isValidRebalanceDueToChangeInHedges();
    }

    /* solhint-disable not-rely-on-time */
    /// @notice harvests glp rewards & rebalances the hedge positions, profits on AAVE and Gmx.
    /// @notice run only if valid rebalance is true
    function rebalance() external onlyKeeper {
        if (!isValidRebalance()) revert InvalidRebalance();

        emit Rebalanced();

        (state.btcPoolAmount, state.ethPoolAmount) = state.getPoolAmounts();

        (int128 currentBtcTraderOIHedge, int128 currentEthTraderOIHedge) = state.getTraderOIHedgeAmounts();
        state.btcTraderOIHedge = currentBtcTraderOIHedge;
        state.ethTraderOIHedge = currentEthTraderOIHedge;

        (uint256 currentBtc, uint256 currentEth) = state.getCurrentBorrows();
        uint256 totalCurrentBorrowValue = state.getBorrowValue(currentBtc, currentEth); // = total position value of current btc and eth position

        _emitVaultState(0);

        // harvest fees
        state.harvestFees();

        // rebalance profit
        state.rebalanceProfit(totalCurrentBorrowValue);

        // calculate current btc and eth positions in GLP
        // get the position value and calculate the collateral needed to borrow that
        // transfer collateral from LB vault to DN vault
        bool isPartialHedge = state.rebalanceHedge(currentBtc, currentEth, state.totalGlp(false), true);

        if (isPartialHedge) {
            state.lastRebalanceTS = 0; // if partial hedge is happening due to delta threshold breach, next rebalance should still go through
            if (!paused()) _pause(); // pause the contracts to prevent users from taking the slippage for updating overall hedges instead of incremental hedges
        } else {
            state.lastRebalanceTS = uint48(block.timestamp); // once partial hedge is completed the lastRebalanceTS gets updated
            if (paused()) _unpause(); // since hedges are optimal now, vault can be unpaused to handle deposits and withdraws
        }

        (currentBtc, currentEth) = state.getCurrentBorrows();

        _emitVaultState(1);
    }

    /* ##################################################################
                                USER FUNCTIONS
    ################################################################## */
    /// @notice deposits sGlp token and returns vault shares
    /// @param amount amount of sGlp (asset) tokens to deposit
    /// @param to receiver address for share allocation
    /// @return shares amount of shares allocated for deposit
    function deposit(
        uint256 amount,
        address to
    ) public virtual override(IERC4626, ERC4626Upgradeable) whenNotPaused returns (uint256 shares) {
        _rebalanceBeforeShareAllocation();
        (, uint slippage) = _previewDeposit(amount);
        emit AssetSlippage(to, slippage);
        shares = super.deposit(amount, to);
        _emitVaultState(1);
    }

    /// @notice mints "shares" amount of vault shares and pull relevant amount of sGlp tokens
    /// @param shares amount of vault shares to mint
    /// @param to receiver address for share allocation
    /// @return amount amount of sGlp tokens required for given number of shares
    function mint(
        uint256 shares,
        address to
    ) public virtual override(IERC4626, ERC4626Upgradeable) whenNotPaused returns (uint256 amount) {
        _rebalanceBeforeShareAllocation();
        (, uint slippage) = _previewMint(shares);
        emit AssetSlippage(to, slippage);
        amount = super.mint(shares, to);
        _emitVaultState(1);
    }

    ///@notice withdraws "assets" amount of sGlp tokens and burns relevant amount of vault shares
    ///@notice deducts some assets for the remaining shareholders to cover the cost of opening and closing of hedge
    ///@param assets amount of assets to withdraw
    ///@param receiver receiver address for the assets
    ///@param owner owner address of the shares to be burnt
    ///@return shares number of shares burnt
    function withdraw(
        uint256 assets,
        address receiver,
        address owner
    ) public override(IERC4626, ERC4626Upgradeable) whenNotPaused returns (uint256 shares) {
        _rebalanceBeforeShareAllocation();
        (, uint slippage) = _previewWithdraw(assets);
        emit AssetSlippage(owner, slippage);
        shares = super.withdraw(assets, receiver, owner);
        _emitVaultState(1);
    }

    ///@notice burns "shares" amount of vault shares and withdraws relevant amount of sGlp tokens
    ///@notice deducts some assets for the remaining shareholders to cover the cost of opening and closing of hedge
    ///@param shares amount of shares to redeem
    ///@param receiver receiver address for the assets
    ///@param owner owner address of the shares to be burnt
    ///@return assets number of assets sent
    function redeem(
        uint256 shares,
        address receiver,
        address owner
    ) public override(IERC4626, ERC4626Upgradeable) whenNotPaused returns (uint256 assets) {
        _rebalanceBeforeShareAllocation();
        (, uint slippage) = _previewRedeem(shares);
        emit AssetSlippage(owner, slippage);
        assets = super.redeem(shares, receiver, owner);
        _emitVaultState(1);
    }

    /* ##################################################################
                            FLASHLOAN RECEIVER
    ################################################################## */

    ///@notice flashloan receiver for balance vault
    ///@notice receives flashloaned tokens(WETH or WBTC or USDC) from balancer, swaps on uniswap and borrows/repays on AAVE
    ///@dev only allows balancer vault to call this
    ///@dev only runs when _hasFlashloaned is set to true (prevents someone else from initiating flashloan to vault)
    ///@param tokens list of tokens flashloaned
    ///@param amounts amounts of token flashloans in same order
    ///@param feeAmounts amounts of fee/premium charged for flashloan
    ///@param userData data passed to balancer for flashloan (includes token amounts, token usdc value and swap direction)
    function receiveFlashLoan(
        IERC20[] memory tokens,
        uint256[] memory amounts,
        uint256[] memory feeAmounts,
        bytes memory userData
    ) external onlyBalancerVault whenFlashloaned {
        state.receiveFlashLoan(tokens, amounts, feeAmounts, userData);
    }

    /* ##################################################################
                                VIEW FUNCTIONS
    ################################################################## */

    ///@notice gives total asset tokens available in vault
    ///@dev some unhedged part of glp might be converted to USDC (its value in GLP is added to total glp assets)
    function totalAssets() public view override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        return state.totalAssets();
    }

    ///@notice returns price of glp token
    ///@param maximize specifies aum used is minimum(false) or maximum(true)
    ///@return price of glp token in PRICE_PRECISION
    function getPrice(bool maximize) public view returns (uint256) {
        uint256 aum = state.glpManager.getAum(maximize);
        uint256 totalSupply = state.glp.totalSupply();

        return aum.mulDivDown(PRICE_PRECISION, totalSupply * 1e24);
    }

    ///@notice returns price of glp token
    ///@return price of glp token in X128
    function getPriceX128() public view returns (uint256) {
        uint256 aum = state.glpManager.getAum(false);
        uint256 totalSupply = state.glp.totalSupply();

        return aum.mulDiv(1 << 128, totalSupply * 1e24);
    }

    ///@notice returns the minimum market value of "assetAmount" of asset (sGlp) tokens
    ///@dev uses minimum price i.e. minimum AUM of glp tokens
    ///@param assetAmount amount of sGlp tokens
    ///@return marketValue of given amount of glp assets
    function getMarketValue(uint256 assetAmount) public view returns (uint256 marketValue) {
        marketValue = assetAmount.mulDivDown(state.getGlpPriceInUsdc(false), PRICE_PRECISION);
    }

    ///@notice returns vault market value (USD terms & 6 decimals) basis glp and usdc tokens in vault
    ///@dev Part 1. adds value of glp tokens basis minimum glp aum from gmx
    ///@dev Part 2. adds value of junior vault usdc deposit in AAVE (swap outputs + unhedged GLP)
    ///@dev Part 3. subtracts value of WETH & WBTC borrows from AAVE
    ///@return vaultMarketValue : market value of vault assets
    function getVaultMarketValue() public view returns (int256 vaultMarketValue) {
        (uint256 currentBtc, uint256 currentEth) = state.getCurrentBorrows();
        uint256 totalCurrentBorrowValue = state.getBorrowValue(currentBtc, currentEth);
        uint256 glpBalance = state.fsGlp.balanceOf(address(this));
        vaultMarketValue = ((getMarketValue(glpBalance).toInt256() +
            state.dnUsdcDeposited +
            state.unhedgedGlpInUsdc.toInt256()) - totalCurrentBorrowValue.toInt256());
    }

    /// @notice returns total amount of usdc borrowed from senior vault
    /// @dev all aUSDC yield from AAVE goes to the senior vault
    /// @dev deducts junior vault usdc (swapped + unhedged glp) from overall balance
    /// @return usdcAmount borrowed from senior tranche
    function getUsdcBorrowed() public view returns (uint256 usdcAmount) {
        return
            uint256(
                state.aUsdc.balanceOf(address(this)).toInt256() -
                    state.dnUsdcDeposited -
                    state.unhedgedGlpInUsdc.toInt256()
            );
    }

    /// @notice returns maximum amount of shares that a user can deposit
    /// @return maximum asset amount
    function maxDeposit(address) public view override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        uint256 _depositCap = state.depositCap;
        uint256 _totalAssets = state.totalAssets(true);
        return _depositCap > _totalAssets ? _depositCap - _totalAssets : 0;
    }

    /// @notice returns maximum amount of shares that can be minted for a given user
    /// @param receiver address of the user
    /// @return maximum share amount
    function maxMint(address receiver) public view override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        return convertToShares(maxDeposit(receiver));
    }

    /// @notice converts asset amount to share amount
    /// @param assets asset amount to convert to shares
    /// @return share amount corresponding to given asset amount
    function convertToShares(uint256 assets) public view override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

        return supply == 0 ? assets : assets.mulDivDown(supply, state.totalAssets(true));
    }

    /// @notice converts share amount to asset amount
    /// @param shares asset amount to convert to assets
    /// @return asset amount corresponding to given share amount
    function convertToAssets(uint256 shares) public view override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

        return supply == 0 ? shares : shares.mulDivDown(state.totalAssets(false), supply);
    }

    /// @notice preview function for using assets to mint shares
    /// @param assets number of assets to be deposited
    /// @return shares that would be minted to the user
    function previewDeposit(
        uint256 assets
    ) public view virtual override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        (uint shares, ) = _previewDeposit(assets);
        return shares;
    }

    /// @notice preview function for using assets to mint shares
    /// @param assets number of assets to be deposited
    /// @return shares that would be minted to the user
    /// @return slippage assets adjustment due to slippage
    function _previewDeposit(uint256 assets) internal view returns (uint256, uint256) {
        uint256 netAssets = state.getSlippageAdjustedAssets({ assets: assets, isDeposit: true });
        return (convertToShares(netAssets), assets - netAssets);
    }

    /// @notice preview function for minting of shares
    /// @param shares number of shares to mint
    /// @return assets that would be taken from the user
    function previewMint(uint256 shares) public view virtual override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        (uint assets, ) = _previewMint(shares);
        return assets;
    }

    /// @notice preview function for minting of shares
    /// @param shares number of shares to mint
    /// @return assets that would be taken from the user
    /// @return slippage assets adjustment due to slippage
    function _previewMint(uint256 shares) internal view virtual returns (uint256, uint256) {
        uint256 supply = totalSupply();

        if (supply == 0) return (shares, 0);

        uint256 assets = convertToAssets(shares);
        uint256 netAssets = state.getSlippageAdjustedAssets({ assets: assets, isDeposit: true });

        uint256 slippageInAssetTerms = assets - netAssets;

        return (assets + slippageInAssetTerms, slippageInAssetTerms);
    }

    /// @notice preview function for withdrawal of assets
    /// @param assets that would be given to the user
    /// @return shares that would be burnt
    function previewWithdraw(
        uint256 assets
    ) public view virtual override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        (uint _assets, ) = _previewWithdraw(assets);
        return _assets;
    }

    /// @notice preview function for withdrawal of assets
    /// @param assets that would be given to the user
    /// @return shares that would be burnt
    /// @return slippage assets adjustment due to slippage
    function _previewWithdraw(uint256 assets) internal view virtual returns (uint256, uint256) {
        uint256 supply = totalSupply();

        if (supply == 0) return (assets, 0);

        uint256 netAssets = state.getSlippageAdjustedAssets({ assets: assets, isDeposit: false });

        return (
            netAssets.mulDivUp(supply * MAX_BPS, state.totalAssets(false) * (MAX_BPS - state.withdrawFeeBps)),
            assets - netAssets
        );
    }

    /// @notice preview function for redeeming shares
    /// @param shares that would be taken from the user
    /// @return assets that user would get
    function previewRedeem(
        uint256 shares
    ) public view virtual override(IERC4626, ERC4626Upgradeable) returns (uint256) {
        (uint assets, ) = _previewRedeem(shares);
        return assets;
    }

    /// @notice preview function for redeeming shares
    /// @param shares that would be taken from the user
    /// @return assets that user would get
    /// @return slippage assets adjustment due to slippage
    function _previewRedeem(uint256 shares) internal view virtual returns (uint256, uint256) {
        uint256 supply = totalSupply();

        if (supply == 0) return (shares, 0);

        uint256 assets = convertToAssets(shares);
        uint256 netAssets = state.getSlippageAdjustedAssets({ assets: assets, isDeposit: false });

        return (netAssets.mulDivDown(MAX_BPS - state.withdrawFeeBps, MAX_BPS), assets - netAssets);
    }

    /// @notice returns deposit cap in terms of asset tokens
    function depositCap() external view returns (uint256) {
        return state.depositCap;
    }

    /// @notice returns current borrows for BTC and ETH respectively
    /// @return currentBtcBorrow amount of btc borrowed from AAVE
    /// @return currentEthBorrow amount of eth borrowed from AAVE
    function getCurrentBorrows() external view returns (uint256 currentBtcBorrow, uint256 currentEthBorrow) {
        return state.getCurrentBorrows();
    }

    /// @notice returns optimal borrows for BTC and ETH respectively basis glpDeposited amount
    /// @param glpDeposited amount of glp for which optimal borrow needs to be calculated
    /// @return optimalBtcBorrow optimal amount of btc borrowed from AAVE
    /// @return optimalEthBorrow optimal amount of eth borrowed from AAVE
    function getOptimalBorrows(
        uint256 glpDeposited,
        bool withUpdatedPoolAmounts
    ) external view returns (uint256 optimalBtcBorrow, uint256 optimalEthBorrow) {
        return state.getOptimalBorrows(glpDeposited, withUpdatedPoolAmounts);
    }

    /// @notice returns junior vault share of usdc deposited to AAVE
    function dnUsdcDeposited() external view returns (int256) {
        return state.dnUsdcDeposited;
    }

    function getAdminParams()
        external
        view
        returns (
            address keeper,
            IDnGmxSeniorVault dnGmxSeniorVault,
            uint256 depositCap_,
            uint16 withdrawFeeBps,
            uint24 feeTierWethWbtcPool
        )
    {
        return (
            state.keeper,
            state.dnGmxSeniorVault,
            state.depositCap,
            state.withdrawFeeBps,
            state.feeTierWethWbtcPool
        );
    }

    function getThresholds()
        external
        view
        returns (
            uint16 slippageThresholdSwapBtcBps,
            uint16 slippageThresholdSwapEthBps,
            uint16 slippageThresholdGmxBps,
            uint128 usdcConversionThreshold,
            uint128 wethConversionThreshold,
            uint128 hedgeUsdcAmountThreshold,
            uint128 partialBtcHedgeUsdcAmountThreshold,
            uint128 partialEthHedgeUsdcAmountThreshold
        )
    {
        return (
            state.slippageThresholdSwapBtcBps,
            state.slippageThresholdSwapEthBps,
            state.slippageThresholdGmxBps,
            state.usdcConversionThreshold,
            state.wethConversionThreshold,
            state.hedgeUsdcAmountThreshold,
            state.partialBtcHedgeUsdcAmountThreshold,
            state.partialEthHedgeUsdcAmountThreshold
        );
    }

    function getRebalanceParams()
        external
        view
        returns (uint32 rebalanceTimeThreshold, uint16 rebalanceDeltaThresholdBps, uint16 rebalanceHfThresholdBps)
    {
        return (state.rebalanceTimeThreshold, state.rebalanceDeltaThresholdBps, state.rebalanceHfThresholdBps);
    }

    function getHedgeParams()
        external
        view
        returns (
            IBalancerVault balancerVault,
            ISwapRouter swapRouter,
            uint256 targetHealthFactor,
            IRewardsController aaveRewardsController
        )
    {
        return (state.balancerVault, state.swapRouter, state.targetHealthFactor, state.aaveRewardsController);
    }

    /* ##################################################################
                            INTERNAL FUNCTIONS
    ################################################################## */

    function _emitVaultState(uint256 eventType) internal {
        state.emitVaultState(eventType);
    }

    /*
        DEPOSIT/WITHDRAW HELPERS
    */

    /// @notice harvests fees and rebalances profits before deposits and withdrawals
    /// @dev called first on any deposit/withdrawals
    function _rebalanceBeforeShareAllocation() internal {
        if (state.btcPoolAmount == 0)
            state.btcPoolAmount = (state.gmxVault.poolAmounts(address(state.wbtc))).toUint128();
        if (state.ethPoolAmount == 0)
            state.ethPoolAmount = (state.gmxVault.poolAmounts(address(state.weth))).toUint128();

        (uint256 currentBtc, uint256 currentEth) = state.getCurrentBorrows();
        uint256 totalCurrentBorrowValue = state.getBorrowValue(currentBtc, currentEth); // = total position value of current btc and eth position

        _emitVaultState(0);

        // harvest fees
        state.harvestFees();

        // rebalance profit
        state.rebalanceProfit(totalCurrentBorrowValue);
    }

    function beforeWithdraw(uint256 assets, uint256, address) internal override {
        (uint256 currentBtc, uint256 currentEth) = state.getCurrentBorrows();

        //rebalance of hedge based on assets after withdraw (before withdraw assets - withdrawn assets)
        state.rebalanceHedge(currentBtc, currentEth, state.totalGlp(false) - assets, false);
    }

    function afterDeposit(uint256, uint256, address) internal override {
        if (totalAssets() > state.depositCap) revert DepositCapExceeded();
        (uint256 currentBtc, uint256 currentEth) = state.getCurrentBorrows();

        //rebalance of hedge based on assets after deposit (after deposit assets)
        state.rebalanceHedge(currentBtc, currentEth, state.totalGlp(false), false);
    }
}

// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
  /**
   * @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 `recipient`.
   *
   * Returns a boolean value indicating whether the operation succeeded.
   *
   * Emits a {Transfer} event.
   */
  function transfer(address recipient, 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 `sender` to `recipient` 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 sender,
    address recipient,
    uint256 amount
  ) external returns (bool);

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

File 3 of 83 : 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;
}

// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IERC20} from '../dependencies/openzeppelin/contracts/IERC20.sol';
import {IScaledBalanceToken} from './IScaledBalanceToken.sol';
import {IInitializableAToken} from './IInitializableAToken.sol';

/**
 * @title IAToken
 * @author Aave
 * @notice Defines the basic interface for an AToken.
 */
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 5 of 83 : 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 6 of 83 : 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 7 of 83 : 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;
}

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

// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

/**
 * @title IPriceOracle
 * @author Aave
 * @notice Defines the basic interface for a Price oracle.
 */
interface IPriceOracle {
  /**
   * @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);

  /**
   * @notice Set the price of the asset
   * @param asset The address of the asset
   * @param price The price of the asset
   */
  function setAssetPrice(address asset, uint256 price) external;
}

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

// 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 12 of 83 : 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 13 of 83 : 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
}

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

File 15 of 83 : 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;
  }
}

// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;

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

  function latestAnswer() external view returns (int256);

  function latestTimestamp() external view returns (uint256);

  function latestRound() external view returns (uint256);

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

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

  event AnswerUpdated(int256 indexed current, uint256 indexed roundId, uint256 timestamp);
  event NewRound(uint256 indexed roundId, address indexed startedBy);
}

// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;

import {IRewardsDistributor} from './IRewardsDistributor.sol';
import {ITransferStrategyBase} from './ITransferStrategyBase.sol';
import {IEACAggregatorProxy} from '../../misc/interfaces/IEACAggregatorProxy.sol';
import {RewardsDataTypes} from '../libraries/RewardsDataTypes.sol';

/**
 * @title IRewardsController
 * @author Aave
 * @notice Defines the basic interface for a Rewards Controller.
 */
interface IRewardsController is IRewardsDistributor {
  /**
   * @dev Emitted when a new address is whitelisted as claimer of rewards on behalf of a user
   * @param user The address of the user
   * @param claimer The address of the claimer
   */
  event ClaimerSet(address indexed user, address indexed claimer);

  /**
   * @dev Emitted when rewards are claimed
   * @param user The address of the user rewards has been claimed on behalf of
   * @param reward The address of the token reward is claimed
   * @param to The address of the receiver of the rewards
   * @param claimer The address of the claimer
   * @param amount The amount of rewards claimed
   */
  event RewardsClaimed(
    address indexed user,
    address indexed reward,
    address indexed to,
    address claimer,
    uint256 amount
  );

  /**
   * @dev Emitted when a transfer strategy is installed for the reward distribution
   * @param reward The address of the token reward
   * @param transferStrategy The address of TransferStrategy contract
   */
  event TransferStrategyInstalled(address indexed reward, address indexed transferStrategy);

  /**
   * @dev Emitted when the reward oracle is updated
   * @param reward The address of the token reward
   * @param rewardOracle The address of oracle
   */
  event RewardOracleUpdated(address indexed reward, address indexed rewardOracle);

  /**
   * @dev Whitelists an address to claim the rewards on behalf of another address
   * @param user The address of the user
   * @param claimer The address of the claimer
   */
  function setClaimer(address user, address claimer) external;

  /**
   * @dev Sets a TransferStrategy logic contract that determines the logic of the rewards transfer
   * @param reward The address of the reward token
   * @param transferStrategy The address of the TransferStrategy logic contract
   */
  function setTransferStrategy(address reward, ITransferStrategyBase transferStrategy) external;

  /**
   * @dev Sets an Aave Oracle contract to enforce rewards with a source of value.
   * @notice At the moment of reward configuration, the Incentives Controller performs
   * a check to see if the reward asset oracle is compatible with IEACAggregator proxy.
   * This check is enforced for integrators to be able to show incentives at
   * the current Aave UI without the need to setup an external price registry
   * @param reward The address of the reward to set the price aggregator
   * @param rewardOracle The address of price aggregator that follows IEACAggregatorProxy interface
   */
  function setRewardOracle(address reward, IEACAggregatorProxy rewardOracle) external;

  /**
   * @dev Get the price aggregator oracle address
   * @param reward The address of the reward
   * @return The price oracle of the reward
   */
  function getRewardOracle(address reward) external view returns (address);

  /**
   * @dev Returns the whitelisted claimer for a certain address (0x0 if not set)
   * @param user The address of the user
   * @return The claimer address
   */
  function getClaimer(address user) external view returns (address);

  /**
   * @dev Returns the Transfer Strategy implementation contract address being used for a reward address
   * @param reward The address of the reward
   * @return The address of the TransferStrategy contract
   */
  function getTransferStrategy(address reward) external view returns (address);

  /**
   * @dev Configure assets to incentivize with an emission of rewards per second until the end of distribution.
   * @param config The assets configuration input, the list of structs contains the following fields:
   *   uint104 emissionPerSecond: The emission per second following rewards unit decimals.
   *   uint256 totalSupply: The total supply of the asset to incentivize
   *   uint40 distributionEnd: The end of the distribution of the incentives for an asset
   *   address asset: The asset address to incentivize
   *   address reward: The reward token address
   *   ITransferStrategy transferStrategy: The TransferStrategy address with the install hook and claim logic.
   *   IEACAggregatorProxy rewardOracle: The Price Oracle of a reward to visualize the incentives at the UI Frontend.
   *                                     Must follow Chainlink Aggregator IEACAggregatorProxy interface to be compatible.
   */
  function configureAssets(RewardsDataTypes.RewardsConfigInput[] memory config) external;

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

  /**
   * @dev Claims reward for a user to the desired address, on all the assets of the pool, accumulating the pending rewards
   * @param assets List of assets to check eligible distributions before claiming rewards
   * @param amount The amount of rewards to claim
   * @param to The address that will be receiving the rewards
   * @param reward The address of the reward token
   * @return The amount of rewards claimed
   **/
  function claimRewards(
    address[] calldata assets,
    uint256 amount,
    address to,
    address reward
  ) external returns (uint256);

  /**
   * @dev Claims reward for a user on behalf, on all the assets of the pool, accumulating the pending rewards. The
   * caller must be whitelisted via "allowClaimOnBehalf" function by the RewardsAdmin role manager
   * @param assets The list of assets to check eligible distributions before claiming rewards
   * @param amount The amount of rewards to claim
   * @param user The address to check and claim rewards
   * @param to The address that will be receiving the rewards
   * @param reward The address of the reward token
   * @return The amount of rewards claimed
   **/
  function claimRewardsOnBehalf(
    address[] calldata assets,
    uint256 amount,
    address user,
    address to,
    address reward
  ) external returns (uint256);

  /**
   * @dev Claims reward for msg.sender, on all the assets of the pool, accumulating the pending rewards
   * @param assets The list of assets to check eligible distributions before claiming rewards
   * @param amount The amount of rewards to claim
   * @param reward The address of the reward token
   * @return The amount of rewards claimed
   **/
  function claimRewardsToSelf(
    address[] calldata assets,
    uint256 amount,
    address reward
  ) external returns (uint256);

  /**
   * @dev Claims all rewards for a user to the desired address, on all the assets of the pool, accumulating the pending rewards
   * @param assets The list of assets to check eligible distributions before claiming rewards
   * @param to The address that will be receiving the rewards
   * @return rewardsList List of addresses of the reward tokens
   * @return claimedAmounts List that contains the claimed amount per reward, following same order as "rewardList"
   **/
  function claimAllRewards(address[] calldata assets, address to)
    external
    returns (address[] memory rewardsList, uint256[] memory claimedAmounts);

  /**
   * @dev Claims all rewards for a user on behalf, on all the assets of the pool, accumulating the pending rewards. The caller must
   * be whitelisted via "allowClaimOnBehalf" function by the RewardsAdmin role manager
   * @param assets The list of assets to check eligible distributions before claiming rewards
   * @param user The address to check and claim rewards
   * @param to The address that will be receiving the rewards
   * @return rewardsList List of addresses of the reward tokens
   * @return claimedAmounts List that contains the claimed amount per reward, following same order as "rewardsList"
   **/
  function claimAllRewardsOnBehalf(
    address[] calldata assets,
    address user,
    address to
  ) external returns (address[] memory rewardsList, uint256[] memory claimedAmounts);

  /**
   * @dev Claims all reward for msg.sender, on all the assets of the pool, accumulating the pending rewards
   * @param assets The list of assets to check eligible distributions before claiming rewards
   * @return rewardsList List of addresses of the reward tokens
   * @return claimedAmounts List that contains the claimed amount per reward, following same order as "rewardsList"
   **/
  function claimAllRewardsToSelf(address[] calldata assets)
    external
    returns (address[] memory rewardsList, uint256[] memory claimedAmounts);
}

// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;

/**
 * @title IRewardsDistributor
 * @author Aave
 * @notice Defines the basic interface for a Rewards Distributor.
 */
interface IRewardsDistributor {
  /**
   * @dev Emitted when the configuration of the rewards of an asset is updated.
   * @param asset The address of the incentivized asset
   * @param reward The address of the reward token
   * @param oldEmission The old emissions per second value of the reward distribution
   * @param newEmission The new emissions per second value of the reward distribution
   * @param oldDistributionEnd The old end timestamp of the reward distribution
   * @param newDistributionEnd The new end timestamp of the reward distribution
   * @param assetIndex The index of the asset distribution
   */
  event AssetConfigUpdated(
    address indexed asset,
    address indexed reward,
    uint256 oldEmission,
    uint256 newEmission,
    uint256 oldDistributionEnd,
    uint256 newDistributionEnd,
    uint256 assetIndex
  );

  /**
   * @dev Emitted when rewards of an asset are accrued on behalf of a user.
   * @param asset The address of the incentivized asset
   * @param reward The address of the reward token
   * @param user The address of the user that rewards are accrued on behalf of
   * @param assetIndex The index of the asset distribution
   * @param userIndex The index of the asset distribution on behalf of the user
   * @param rewardsAccrued The amount of rewards accrued
   */
  event Accrued(
    address indexed asset,
    address indexed reward,
    address indexed user,
    uint256 assetIndex,
    uint256 userIndex,
    uint256 rewardsAccrued
  );

  /**
   * @dev Sets the end date for the distribution
   * @param asset The asset to incentivize
   * @param reward The reward token that incentives the asset
   * @param newDistributionEnd The end date of the incentivization, in unix time format
   **/
  function setDistributionEnd(
    address asset,
    address reward,
    uint32 newDistributionEnd
  ) external;

  /**
   * @dev Sets the emission per second of a set of reward distributions
   * @param asset The asset is being incentivized
   * @param rewards List of reward addresses are being distributed
   * @param newEmissionsPerSecond List of new reward emissions per second
   */
  function setEmissionPerSecond(
    address asset,
    address[] calldata rewards,
    uint88[] calldata newEmissionsPerSecond
  ) external;

  /**
   * @dev Gets the end date for the distribution
   * @param asset The incentivized asset
   * @param reward The reward token of the incentivized asset
   * @return The timestamp with the end of the distribution, in unix time format
   **/
  function getDistributionEnd(address asset, address reward) external view returns (uint256);

  /**
   * @dev Returns the index of a user on a reward distribution
   * @param user Address of the user
   * @param asset The incentivized asset
   * @param reward The reward token of the incentivized asset
   * @return The current user asset index, not including new distributions
   **/
  function getUserAssetIndex(
    address user,
    address asset,
    address reward
  ) external view returns (uint256);

  /**
   * @dev Returns the configuration of the distribution reward for a certain asset
   * @param asset The incentivized asset
   * @param reward The reward token of the incentivized asset
   * @return The index of the asset distribution
   * @return The emission per second of the reward distribution
   * @return The timestamp of the last update of the index
   * @return The timestamp of the distribution end
   **/
  function getRewardsData(address asset, address reward)
    external
    view
    returns (
      uint256,
      uint256,
      uint256,
      uint256
    );

  /**
   * @dev Calculates the next value of an specific distribution index, with validations.
   * @param asset The incentivized asset
   * @param reward The reward token of the incentivized asset
   * @return The old index of the asset distribution
   * @return The new index of the asset distribution
   **/
  function getAssetIndex(address asset, address reward) external view returns (uint256, uint256);

  /**
   * @dev Returns the list of available reward token addresses of an incentivized asset
   * @param asset The incentivized asset
   * @return List of rewards addresses of the input asset
   **/
  function getRewardsByAsset(address asset) external view returns (address[] memory);

  /**
   * @dev Returns the list of available reward addresses
   * @return List of rewards supported in this contract
   **/
  function getRewardsList() external view returns (address[] memory);

  /**
   * @dev Returns the accrued rewards balance of a user, not including virtually accrued rewards since last distribution.
   * @param user The address of the user
   * @param reward The address of the reward token
   * @return Unclaimed rewards, not including new distributions
   **/
  function getUserAccruedRewards(address user, address reward) external view returns (uint256);

  /**
   * @dev Returns a single rewards balance of a user, including virtually accrued and unrealized claimable rewards.
   * @param assets List of incentivized assets to check eligible distributions
   * @param user The address of the user
   * @param reward The address of the reward token
   * @return The rewards amount
   **/
  function getUserRewards(
    address[] calldata assets,
    address user,
    address reward
  ) external view returns (uint256);

  /**
   * @dev Returns a list all rewards of a user, including already accrued and unrealized claimable rewards
   * @param assets List of incentivized assets to check eligible distributions
   * @param user The address of the user
   * @return The list of reward addresses
   * @return The list of unclaimed amount of rewards
   **/
  function getAllUserRewards(address[] calldata assets, address user)
    external
    view
    returns (address[] memory, uint256[] memory);

  /**
   * @dev Returns the decimals of an asset to calculate the distribution delta
   * @param asset The address to retrieve decimals
   * @return The decimals of an underlying asset
   */
  function getAssetDecimals(address asset) external view returns (uint8);

  /**
   * @dev Returns the address of the emission manager
   * @return The address of the EmissionManager
   */
  function EMISSION_MANAGER() external view returns (address);

  /**
   * @dev Returns the address of the emission manager.
   * Deprecated: This getter is maintained for compatibility purposes. Use the `EMISSION_MANAGER()` function instead.
   * @return The address of the EmissionManager
   */
  function getEmissionManager() external view returns (address);
}

// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;

interface ITransferStrategyBase {
  event EmergencyWithdrawal(
    address indexed caller,
    address indexed token,
    address indexed to,
    uint256 amount
  );

  /**
   * @dev Perform custom transfer logic via delegate call from source contract to a TransferStrategy implementation
   * @param to Account to transfer rewards
   * @param reward Address of the reward token
   * @param amount Amount to transfer to the "to" address parameter
   * @return Returns true bool if transfer logic succeeds
   */
  function performTransfer(
    address to,
    address reward,
    uint256 amount
  ) external returns (bool);

  /**
   * @return Returns the address of the Incentives Controller
   */
  function getIncentivesController() external view returns (address);

  /**
   * @return Returns the address of the Rewards admin
   */
  function getRewardsAdmin() external view returns (address);

  /**
   * @dev Perform an emergency token withdrawal only callable by the Rewards admin
   * @param token Address of the token to withdraw funds from this contract
   * @param to Address of the recipient of the withdrawal
   * @param amount Amount of the withdrawal
   */
  function emergencyWithdrawal(
    address token,
    address to,
    uint256 amount
  ) external;
}

File 20 of 83 : RewardsDataTypes.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;

import {ITransferStrategyBase} from '../interfaces/ITransferStrategyBase.sol';
import {IEACAggregatorProxy} from '../../misc/interfaces/IEACAggregatorProxy.sol';

library RewardsDataTypes {
  struct RewardsConfigInput {
    uint88 emissionPerSecond;
    uint256 totalSupply;
    uint32 distributionEnd;
    address asset;
    address reward;
    ITransferStrategyBase transferStrategy;
    IEACAggregatorProxy rewardOracle;
  }

  struct UserAssetBalance {
    address asset;
    uint256 userBalance;
    uint256 totalSupply;
  }

  struct UserData {
    // Liquidity index of the reward distribution for the user
    uint104 index;
    // Amount of accrued rewards for the user since last user index update
    uint128 accrued;
  }

  struct RewardData {
    // Liquidity index of the reward distribution
    uint104 index;
    // Amount of reward tokens distributed per second
    uint88 emissionPerSecond;
    // Timestamp of the last reward index update
    uint32 lastUpdateTimestamp;
    // The end of the distribution of rewards (in seconds)
    uint32 distributionEnd;
    // Map of user addresses and their rewards data (userAddress => userData)
    mapping(address => UserData) usersData;
  }

  struct AssetData {
    // Map of reward token addresses and their data (rewardTokenAddress => rewardData)
    mapping(address => RewardData) rewards;
    // List of reward token addresses for the asset
    mapping(uint128 => address) availableRewards;
    // Count of reward tokens for the asset
    uint128 availableRewardsCount;
    // Number of decimals of the asset
    uint8 decimals;
  }
}

// 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 22 of 83 : Initializable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (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. Equivalent to `reinitializer(1)`.
     */
    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.
     *
     * `initializer` is equivalent to `reinitializer(1)`, so 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.
     *
     * 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.
     */
    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.
     */
    function _disableInitializers() internal virtual {
        require(!_initializing, "Initializable: contract is initializing");
        if (_initialized < type(uint8).max) {
            _initialized = type(uint8).max;
            emit Initialized(type(uint8).max);
        }
    }
}

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

pragma solidity ^0.8.0;

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

/**
 * @dev Contract module which allows children to implement an emergency stop
 * mechanism that can be triggered by an authorized account.
 *
 * This module is used through inheritance. It will make available the
 * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
 * the functions of your contract. Note that they will not be pausable by
 * simply including this module, only once the modifiers are put in place.
 */
abstract contract PausableUpgradeable is Initializable, ContextUpgradeable {
    /**
     * @dev Emitted when the pause is triggered by `account`.
     */
    event Paused(address account);

    /**
     * @dev Emitted when the pause is lifted by `account`.
     */
    event Unpaused(address account);

    bool private _paused;

    /**
     * @dev Initializes the contract in unpaused state.
     */
    function __Pausable_init() internal onlyInitializing {
        __Pausable_init_unchained();
    }

    function __Pausable_init_unchained() internal onlyInitializing {
        _paused = false;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is not paused.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    modifier whenNotPaused() {
        _requireNotPaused();
        _;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is paused.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    modifier whenPaused() {
        _requirePaused();
        _;
    }

    /**
     * @dev Returns true if the contract is paused, and false otherwise.
     */
    function paused() public view virtual returns (bool) {
        return _paused;
    }

    /**
     * @dev Throws if the contract is paused.
     */
    function _requireNotPaused() internal view virtual {
        require(!paused(), "Pausable: paused");
    }

    /**
     * @dev Throws if the contract is not paused.
     */
    function _requirePaused() internal view virtual {
        require(paused(), "Pausable: not paused");
    }

    /**
     * @dev Triggers stopped state.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    function _pause() internal virtual whenNotPaused {
        _paused = true;
        emit Paused(_msgSender());
    }

    /**
     * @dev Returns to normal state.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    function _unpause() internal virtual whenPaused {
        _paused = false;
        emit Unpaused(_msgSender());
    }

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

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

pragma solidity ^0.8.0;

import "./IERC20Upgradeable.sol";
import "./extensions/IERC20MetadataUpgradeable.sol";
import "../../utils/ContextUpgradeable.sol";
import "../../proxy/utils/Initializable.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20PresetMinterPauser}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * We have followed general OpenZeppelin Contracts guidelines: functions revert
 * instead returning `false` on failure. This behavior is nonetheless
 * conventional and does not conflict with the expectations of ERC20
 * applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
contract ERC20Upgradeable is Initializable, ContextUpgradeable, IERC20Upgradeable, IERC20MetadataUpgradeable {
    mapping(address => uint256) private _balances;

    mapping(address => mapping(address => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;

    /**
     * @dev Sets the values for {name} and {symbol}.
     *
     * The default value of {decimals} is 18. To select a different value for
     * {decimals} you should overload it.
     *
     * All two of these values are immutable: they can only be set once during
     * construction.
     */
    function __ERC20_init(string memory name_, string memory symbol_) internal onlyInitializing {
        __ERC20_init_unchained(name_, symbol_);
    }

    function __ERC20_init_unchained(string memory name_, string memory symbol_) internal onlyInitializing {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5.05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the value {ERC20} uses, unless this function is
     * overridden;
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view virtual override returns (uint8) {
        return 18;
    }

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address to, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     *
     * Requirements:
     *
     * - `from` and `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     * - the caller must have allowance for ``from``'s tokens of at least
     * `amount`.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual override returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, amount);
        _transfer(from, to, amount);
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, allowance(owner, spender) + addedValue);
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        address owner = _msgSender();
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(owner, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    /**
     * @dev Moves `amount` of tokens from `from` to `to`.
     *
     * This internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     */
    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
        }
        _balances[to] += amount;

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply += amount;
        _balances[account] += amount;
        emit Transfer(address(0), account, amount);

        _afterTokenTransfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance - amount;
        }
        _totalSupply -= amount;

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    /**
     * @dev Updates `owner` s allowance for `spender` based on spent `amount`.
     *
     * Does not update the allowance amount in case of infinite allowance.
     * Revert if not enough allowance is available.
     *
     * Might emit an {Approval} event.
     */
    function _spendAllowance(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "ERC20: insufficient allowance");
            unchecked {
                _approve(owner, spender, currentAllowance - amount);
            }
        }
    }

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}

    /**
     * @dev Hook that is called after any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * has been transferred to `to`.
     * - when `from` is zero, `amount` tokens have been minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}

    /**
     * @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[45] private __gap;
}

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

pragma solidity ^0.8.0;

import "../IERC20Upgradeable.sol";

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

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

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

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

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20Upgradeable {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.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 functionCall(target, data, "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");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(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) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason 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 {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts 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;
}

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

pragma solidity ^0.8.0;

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

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

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

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

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

pragma solidity ^0.8.0;

import "../token/ERC20/extensions/IERC20Metadata.sol";

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

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

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

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

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

pragma solidity ^0.8.0;

import "../IERC20.sol";

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

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

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

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

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

pragma solidity ^0.8.0;

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

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

    function safeTransfer(
        IERC20 token,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

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

    function safeIncreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender) + value;
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    function safeDecreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
            uint256 newAllowance = oldAllowance - value;
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
        }
    }

    function safePermit(
        IERC20Permit token,
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        uint256 nonceBefore = token.nonces(owner);
        token.permit(owner, spender, value, deadline, v, r, s);
        uint256 nonceAfter = token.nonces(owner);
        require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
    }

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

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

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

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     *
     * [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 functionCall(target, data, "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");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(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) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

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

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason 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 {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Arithmetic library with operations for fixed-point numbers.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/utils/FixedPointMathLib.sol)
/// @author Inspired by USM (https://github.com/usmfum/USM/blob/master/contracts/WadMath.sol)
library FixedPointMathLib {
    /*//////////////////////////////////////////////////////////////
                    SIMPLIFIED FIXED POINT OPERATIONS
    //////////////////////////////////////////////////////////////*/

    uint256 internal constant WAD = 1e18; // The scalar of ETH and most ERC20s.

    function mulWadDown(uint256 x, uint256 y) internal pure returns (uint256) {
        return mulDivDown(x, y, WAD); // Equivalent to (x * y) / WAD rounded down.
    }

    function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256) {
        return mulDivUp(x, y, WAD); // Equivalent to (x * y) / WAD rounded up.
    }

    function divWadDown(uint256 x, uint256 y) internal pure returns (uint256) {
        return mulDivDown(x, WAD, y); // Equivalent to (x * WAD) / y rounded down.
    }

    function divWadUp(uint256 x, uint256 y) internal pure returns (uint256) {
        return mulDivUp(x, WAD, y); // Equivalent to (x * WAD) / y rounded up.
    }

    /*//////////////////////////////////////////////////////////////
                    LOW LEVEL FIXED POINT OPERATIONS
    //////////////////////////////////////////////////////////////*/

    function mulDivDown(
        uint256 x,
        uint256 y,
        uint256 denominator
    ) internal pure returns (uint256 z) {
        assembly {
            // Store x * y in z for now.
            z := mul(x, y)

            // Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y))
            if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) {
                revert(0, 0)
            }

            // Divide z by the denominator.
            z := div(z, denominator)
        }
    }

    function mulDivUp(
        uint256 x,
        uint256 y,
        uint256 denominator
    ) internal pure returns (uint256 z) {
        assembly {
            // Store x * y in z for now.
            z := mul(x, y)

            // Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y))
            if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) {
                revert(0, 0)
            }

            // First, divide z - 1 by the denominator and add 1.
            // We allow z - 1 to underflow if z is 0, because we multiply the
            // end result by 0 if z is zero, ensuring we return 0 if z is zero.
            z := mul(iszero(iszero(z)), add(div(sub(z, 1), denominator), 1))
        }
    }

    function rpow(
        uint256 x,
        uint256 n,
        uint256 scalar
    ) internal pure returns (uint256 z) {
        assembly {
            switch x
            case 0 {
                switch n
                case 0 {
                    // 0 ** 0 = 1
                    z := scalar
                }
                default {
                    // 0 ** n = 0
                    z := 0
                }
            }
            default {
                switch mod(n, 2)
                case 0 {
                    // If n is even, store scalar in z for now.
                    z := scalar
                }
                default {
                    // If n is odd, store x in z for now.
                    z := x
                }

                // Shifting right by 1 is like dividing by 2.
                let half := shr(1, scalar)

                for {
                    // Shift n right by 1 before looping to halve it.
                    n := shr(1, n)
                } n {
                    // Shift n right by 1 each iteration to halve it.
                    n := shr(1, n)
                } {
                    // Revert immediately if x ** 2 would overflow.
                    // Equivalent to iszero(eq(div(xx, x), x)) here.
                    if shr(128, x) {
                        revert(0, 0)
                    }

                    // Store x squared.
                    let xx := mul(x, x)

                    // Round to the nearest number.
                    let xxRound := add(xx, half)

                    // Revert if xx + half overflowed.
                    if lt(xxRound, xx) {
                        revert(0, 0)
                    }

                    // Set x to scaled xxRound.
                    x := div(xxRound, scalar)

                    // If n is even:
                    if mod(n, 2) {
                        // Compute z * x.
                        let zx := mul(z, x)

                        // If z * x overflowed:
                        if iszero(eq(div(zx, x), z)) {
                            // Revert if x is non-zero.
                            if iszero(iszero(x)) {
                                revert(0, 0)
                            }
                        }

                        // Round to the nearest number.
                        let zxRound := add(zx, half)

                        // Revert if zx + half overflowed.
                        if lt(zxRound, zx) {
                            revert(0, 0)
                        }

                        // Return properly scaled zxRound.
                        z := div(zxRound, scalar)
                    }
                }
            }
        }
    }

    /*//////////////////////////////////////////////////////////////
                        GENERAL NUMBER UTILITIES
    //////////////////////////////////////////////////////////////*/

    function sqrt(uint256 x) internal pure returns (uint256 z) {
        assembly {
            // Start off with z at 1.
            z := 1

            // Used below to help find a nearby power of 2.
            let y := x

            // Find the lowest power of 2 that is at least sqrt(x).
            if iszero(lt(y, 0x100000000000000000000000000000000)) {
                y := shr(128, y) // Like dividing by 2 ** 128.
                z := shl(64, z) // Like multiplying by 2 ** 64.
            }
            if iszero(lt(y, 0x10000000000000000)) {
                y := shr(64, y) // Like dividing by 2 ** 64.
                z := shl(32, z) // Like multiplying by 2 ** 32.
            }
            if iszero(lt(y, 0x100000000)) {
                y := shr(32, y) // Like dividing by 2 ** 32.
                z := shl(16, z) // Like multiplying by 2 ** 16.
            }
            if iszero(lt(y, 0x10000)) {
                y := shr(16, y) // Like dividing by 2 ** 16.
                z := shl(8, z) // Like multiplying by 2 ** 8.
            }
            if iszero(lt(y, 0x100)) {
                y := shr(8, y) // Like dividing by 2 ** 8.
                z := shl(4, z) // Like multiplying by 2 ** 4.
            }
            if iszero(lt(y, 0x10)) {
                y := shr(4, y) // Like dividing by 2 ** 4.
                z := shl(2, z) // Like multiplying by 2 ** 2.
            }
            if iszero(lt(y, 0x8)) {
                // Equivalent to 2 ** z.
                z := shl(1, z)
            }

            // Shifting right by 1 is like dividing by 2.
            z := shr(1, add(z, div(x, z)))
            z := shr(1, add(z, div(x, z)))
            z := shr(1, add(z, div(x, z)))
            z := shr(1, add(z, div(x, z)))
            z := shr(1, add(z, div(x, z)))
            z := shr(1, add(z, div(x, z)))
            z := shr(1, add(z, div(x, z)))

            // Compute a rounded down version of z.
            let zRoundDown := div(x, z)

            // If zRoundDown is smaller, use it.
            if lt(zRoundDown, z) {
                z := zRoundDown
            }
        }
    }
}

File 38 of 83 : IUniswapV3SwapCallback.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Callback for IUniswapV3PoolActions#swap
/// @notice Any contract that calls IUniswapV3PoolActions#swap must implement this interface
interface IUniswapV3SwapCallback {
    /// @notice Called to `msg.sender` after executing a swap via IUniswapV3Pool#swap.
    /// @dev In the implementation you must pay the pool tokens owed for the swap.
    /// The caller of this method must be checked to be a UniswapV3Pool deployed by the canonical UniswapV3Factory.
    /// amount0Delta and amount1Delta can both be 0 if no tokens were swapped.
    /// @param amount0Delta The amount of token0 that was sent (negative) or must be received (positive) by the pool by
    /// the end of the swap. If positive, the callback must send that amount of token0 to the pool.
    /// @param amount1Delta The amount of token1 that was sent (negative) or must be received (positive) by the pool by
    /// the end of the swap. If positive, the callback must send that amount of token1 to the pool.
    /// @param data Any data passed through by the caller via the IUniswapV3PoolActions#swap call
    function uniswapV3SwapCallback(
        int256 amount0Delta,
        int256 amount1Delta,
        bytes calldata data
    ) external;
}

File 39 of 83 : IUniswapV3Pool.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

import {IUniswapV3PoolImmutables} from './pool/IUniswapV3PoolImmutables.sol';
import {IUniswapV3PoolState} from './pool/IUniswapV3PoolState.sol';
import {IUniswapV3PoolDerivedState} from './pool/IUniswapV3PoolDerivedState.sol';
import {IUniswapV3PoolActions} from './pool/IUniswapV3PoolActions.sol';
import {IUniswapV3PoolOwnerActions} from './pool/IUniswapV3PoolOwnerActions.sol';
import {IUniswapV3PoolErrors} from './pool/IUniswapV3PoolErrors.sol';
import {IUniswapV3PoolEvents} from './pool/IUniswapV3PoolEvents.sol';

/// @title The interface for a Uniswap V3 Pool
/// @notice A Uniswap pool facilitates swapping and automated market making between any two assets that strictly conform
/// to the ERC20 specification
/// @dev The pool interface is broken up into many smaller pieces
interface IUniswapV3Pool is
    IUniswapV3PoolImmutables,
    IUniswapV3PoolState,
    IUniswapV3PoolDerivedState,
    IUniswapV3PoolActions,
    IUniswapV3PoolOwnerActions,
    IUniswapV3PoolErrors,
    IUniswapV3PoolEvents
{

}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Permissionless pool actions
/// @notice Contains pool methods that can be called by anyone
interface IUniswapV3PoolActions {
    /// @notice Sets the initial price for the pool
    /// @dev Price is represented as a sqrt(amountToken1/amountToken0) Q64.96 value
    /// @param sqrtPriceX96 the initial sqrt price of the pool as a Q64.96
    function initialize(uint160 sqrtPriceX96) external;

    /// @notice Adds liquidity for the given recipient/tickLower/tickUpper position
    /// @dev The caller of this method receives a callback in the form of IUniswapV3MintCallback#uniswapV3MintCallback
    /// in which they must pay any token0 or token1 owed for the liquidity. The amount of token0/token1 due depends
    /// on tickLower, tickUpper, the amount of liquidity, and the current price.
    /// @param recipient The address for which the liquidity will be created
    /// @param tickLower The lower tick of the position in which to add liquidity
    /// @param tickUpper The upper tick of the position in which to add liquidity
    /// @param amount The amount of liquidity to mint
    /// @param data Any data that should be passed through to the callback
    /// @return amount0 The amount of token0 that was paid to mint the given amount of liquidity. Matches the value in the callback
    /// @return amount1 The amount of token1 that was paid to mint the given amount of liquidity. Matches the value in the callback
    function mint(
        address recipient,
        int24 tickLower,
        int24 tickUpper,
        uint128 amount,
        bytes calldata data
    ) external returns (uint256 amount0, uint256 amount1);

    /// @notice Collects tokens owed to a position
    /// @dev Does not recompute fees earned, which must be done either via mint or burn of any amount of liquidity.
    /// Collect must be called by the position owner. To withdraw only token0 or only token1, amount0Requested or
    /// amount1Requested may be set to zero. To withdraw all tokens owed, caller may pass any value greater than the
    /// actual tokens owed, e.g. type(uint128).max. Tokens owed may be from accumulated swap fees or burned liquidity.
    /// @param recipient The address which should receive the fees collected
    /// @param tickLower The lower tick of the position for which to collect fees
    /// @param tickUpper The upper tick of the position for which to collect fees
    /// @param amount0Requested How much token0 should be withdrawn from the fees owed
    /// @param amount1Requested How much token1 should be withdrawn from the fees owed
    /// @return amount0 The amount of fees collected in token0
    /// @return amount1 The amount of fees collected in token1
    function collect(
        address recipient,
        int24 tickLower,
        int24 tickUpper,
        uint128 amount0Requested,
        uint128 amount1Requested
    ) external returns (uint128 amount0, uint128 amount1);

    /// @notice Burn liquidity from the sender and account tokens owed for the liquidity to the position
    /// @dev Can be used to trigger a recalculation of fees owed to a position by calling with an amount of 0
    /// @dev Fees must be collected separately via a call to #collect
    /// @param tickLower The lower tick of the position for which to burn liquidity
    /// @param tickUpper The upper tick of the position for which to burn liquidity
    /// @param amount How much liquidity to burn
    /// @return amount0 The amount of token0 sent to the recipient
    /// @return amount1 The amount of token1 sent to the recipient
    function burn(
        int24 tickLower,
        int24 tickUpper,
        uint128 amount
    ) external returns (uint256 amount0, uint256 amount1);

    /// @notice Swap token0 for token1, or token1 for token0
    /// @dev The caller of this method receives a callback in the form of IUniswapV3SwapCallback#uniswapV3SwapCallback
    /// @param recipient The address to receive the output of the swap
    /// @param zeroForOne The direction of the swap, true for token0 to token1, false for token1 to token0
    /// @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
    /// @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this
    /// value after the swap. If one for zero, the price cannot be greater than this value after the swap
    /// @param data Any data to be passed through to the callback
    /// @return amount0 The delta of the balance of token0 of the pool, exact when negative, minimum when positive
    /// @return amount1 The delta of the balance of token1 of the pool, exact when negative, minimum when positive
    function swap(
        address recipient,
        bool zeroForOne,
        int256 amountSpecified,
        uint160 sqrtPriceLimitX96,
        bytes calldata data
    ) external returns (int256 amount0, int256 amount1);

    /// @notice Receive token0 and/or token1 and pay it back, plus a fee, in the callback
    /// @dev The caller of this method receives a callback in the form of IUniswapV3FlashCallback#uniswapV3FlashCallback
    /// @dev Can be used to donate underlying tokens pro-rata to currently in-range liquidity providers by calling
    /// with 0 amount{0,1} and sending the donation amount(s) from the callback
    /// @param recipient The address which will receive the token0 and token1 amounts
    /// @param amount0 The amount of token0 to send
    /// @param amount1 The amount of token1 to send
    /// @param data Any data to be passed through to the callback
    function flash(
        address recipient,
        uint256 amount0,
        uint256 amount1,
        bytes calldata data
    ) external;

    /// @notice Increase the maximum number of price and liquidity observations that this pool will store
    /// @dev This method is no-op if the pool already has an observationCardinalityNext greater than or equal to
    /// the input observationCardinalityNext.
    /// @param observationCardinalityNext The desired minimum number of observations for the pool to store
    function increaseObservationCardinalityNext(uint16 observationCardinalityNext) external;
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Pool state that is not stored
/// @notice Contains view functions to provide information about the pool that is computed rather than stored on the
/// blockchain. The functions here may have variable gas costs.
interface IUniswapV3PoolDerivedState {
    /// @notice Returns the cumulative tick and liquidity as of each timestamp `secondsAgo` from the current block timestamp
    /// @dev To get a time weighted average tick or liquidity-in-range, you must call this with two values, one representing
    /// the beginning of the period and another for the end of the period. E.g., to get the last hour time-weighted average tick,
    /// you must call it with secondsAgos = [3600, 0].
    /// @dev The time weighted average tick represents the geometric time weighted average price of the pool, in
    /// log base sqrt(1.0001) of token1 / token0. The TickMath library can be used to go from a tick value to a ratio.
    /// @param secondsAgos From how long ago each cumulative tick and liquidity value should be returned
    /// @return tickCumulatives Cumulative tick values as of each `secondsAgos` from the current block timestamp
    /// @return secondsPerLiquidityCumulativeX128s Cumulative seconds per liquidity-in-range value as of each `secondsAgos` from the current block
    /// timestamp
    function observe(uint32[] calldata secondsAgos)
        external
        view
        returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);

    /// @notice Returns a snapshot of the tick cumulative, seconds per liquidity and seconds inside a tick range
    /// @dev Snapshots must only be compared to other snapshots, taken over a period for which a position existed.
    /// I.e., snapshots cannot be compared if a position is not held for the entire period between when the first
    /// snapshot is taken and the second snapshot is taken.
    /// @param tickLower The lower tick of the range
    /// @param tickUpper The upper tick of the range
    /// @return tickCumulativeInside The snapshot of the tick accumulator for the range
    /// @return secondsPerLiquidityInsideX128 The snapshot of seconds per liquidity for the range
    /// @return secondsInside The snapshot of seconds per liquidity for the range
    function snapshotCumulativesInside(int24 tickLower, int24 tickUpper)
        external
        view
        returns (
            int56 tickCumulativeInside,
            uint160 secondsPerLiquidityInsideX128,
            uint32 secondsInside
        );
}

File 42 of 83 : IUniswapV3PoolErrors.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Errors emitted by a pool
/// @notice Contains all events emitted by the pool
interface IUniswapV3PoolErrors {
    error LOK();
    error TLU();
    error TLM();
    error TUM();
    error AI();
    error M0();
    error M1();
    error AS();
    error IIA();
    error L();
    error F0();
    error F1();
}

File 43 of 83 : IUniswapV3PoolEvents.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Events emitted by a pool
/// @notice Contains all events emitted by the pool
interface IUniswapV3PoolEvents {
    /// @notice Emitted exactly once by a pool when #initialize is first called on the pool
    /// @dev Mint/Burn/Swap cannot be emitted by the pool before Initialize
    /// @param sqrtPriceX96 The initial sqrt price of the pool, as a Q64.96
    /// @param tick The initial tick of the pool, i.e. log base 1.0001 of the starting price of the pool
    event Initialize(uint160 sqrtPriceX96, int24 tick);

    /// @notice Emitted when liquidity is minted for a given position
    /// @param sender The address that minted the liquidity
    /// @param owner The owner of the position and recipient of any minted liquidity
    /// @param tickLower The lower tick of the position
    /// @param tickUpper The upper tick of the position
    /// @param amount The amount of liquidity minted to the position range
    /// @param amount0 How much token0 was required for the minted liquidity
    /// @param amount1 How much token1 was required for the minted liquidity
    event Mint(
        address sender,
        address indexed owner,
        int24 indexed tickLower,
        int24 indexed tickUpper,
        uint128 amount,
        uint256 amount0,
        uint256 amount1
    );

    /// @notice Emitted when fees are collected by the owner of a position
    /// @dev Collect events may be emitted with zero amount0 and amount1 when the caller chooses not to collect fees
    /// @param owner The owner of the position for which fees are collected
    /// @param tickLower The lower tick of the position
    /// @param tickUpper The upper tick of the position
    /// @param amount0 The amount of token0 fees collected
    /// @param amount1 The amount of token1 fees collected
    event Collect(
        address indexed owner,
        address recipient,
        int24 indexed tickLower,
        int24 indexed tickUpper,
        uint128 amount0,
        uint128 amount1
    );

    /// @notice Emitted when a position's liquidity is removed
    /// @dev Does not withdraw any fees earned by the liquidity position, which must be withdrawn via #collect
    /// @param owner The owner of the position for which liquidity is removed
    /// @param tickLower The lower tick of the position
    /// @param tickUpper The upper tick of the position
    /// @param amount The amount of liquidity to remove
    /// @param amount0 The amount of token0 withdrawn
    /// @param amount1 The amount of token1 withdrawn
    event Burn(
        address indexed owner,
        int24 indexed tickLower,
        int24 indexed tickUpper,
        uint128 amount,
        uint256 amount0,
        uint256 amount1
    );

    /// @notice Emitted by the pool for any swaps between token0 and token1
    /// @param sender The address that initiated the swap call, and that received the callback
    /// @param recipient The address that received the output of the swap
    /// @param amount0 The delta of the token0 balance of the pool
    /// @param amount1 The delta of the token1 balance of the pool
    /// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
    /// @param liquidity The liquidity of the pool after the swap
    /// @param tick The log base 1.0001 of price of the pool after the swap
    event Swap(
        address indexed sender,
        address indexed recipient,
        int256 amount0,
        int256 amount1,
        uint160 sqrtPriceX96,
        uint128 liquidity,
        int24 tick
    );

    /// @notice Emitted by the pool for any flashes of token0/token1
    /// @param sender The address that initiated the swap call, and that received the callback
    /// @param recipient The address that received the tokens from flash
    /// @param amount0 The amount of token0 that was flashed
    /// @param amount1 The amount of token1 that was flashed
    /// @param paid0 The amount of token0 paid for the flash, which can exceed the amount0 plus the fee
    /// @param paid1 The amount of token1 paid for the flash, which can exceed the amount1 plus the fee
    event Flash(
        address indexed sender,
        address indexed recipient,
        uint256 amount0,
        uint256 amount1,
        uint256 paid0,
        uint256 paid1
    );

    /// @notice Emitted by the pool for increases to the number of observations that can be stored
    /// @dev observationCardinalityNext is not the observation cardinality until an observation is written at the index
    /// just before a mint/swap/burn.
    /// @param observationCardinalityNextOld The previous value of the next observation cardinality
    /// @param observationCardinalityNextNew The updated value of the next observation cardinality
    event IncreaseObservationCardinalityNext(
        uint16 observationCardinalityNextOld,
        uint16 observationCardinalityNextNew
    );

    /// @notice Emitted when the protocol fee is changed by the pool
    /// @param feeProtocol0Old The previous value of the token0 protocol fee
    /// @param feeProtocol1Old The previous value of the token1 protocol fee
    /// @param feeProtocol0New The updated value of the token0 protocol fee
    /// @param feeProtocol1New The updated value of the token1 protocol fee
    event SetFeeProtocol(uint8 feeProtocol0Old, uint8 feeProtocol1Old, uint8 feeProtocol0New, uint8 feeProtocol1New);

    /// @notice Emitted when the collected protocol fees are withdrawn by the factory owner
    /// @param sender The address that collects the protocol fees
    /// @param recipient The address that receives the collected protocol fees
    /// @param amount0 The amount of token0 protocol fees that is withdrawn
    /// @param amount0 The amount of token1 protocol fees that is withdrawn
    event CollectProtocol(address indexed sender, address indexed recipient, uint128 amount0, uint128 amount1);
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Pool state that never changes
/// @notice These parameters are fixed for a pool forever, i.e., the methods will always return the same values
interface IUniswapV3PoolImmutables {
    /// @notice The contract that deployed the pool, which must adhere to the IUniswapV3Factory interface
    /// @return The contract address
    function factory() external view returns (address);

    /// @notice The first of the two tokens of the pool, sorted by address
    /// @return The token contract address
    function token0() external view returns (address);

    /// @notice The second of the two tokens of the pool, sorted by address
    /// @return The token contract address
    function token1() external view returns (address);

    /// @notice The pool's fee in hundredths of a bip, i.e. 1e-6
    /// @return The fee
    function fee() external view returns (uint24);

    /// @notice The pool tick spacing
    /// @dev Ticks can only be used at multiples of this value, minimum of 1 and always positive
    /// e.g.: a tickSpacing of 3 means ticks can be initialized every 3rd tick, i.e., ..., -6, -3, 0, 3, 6, ...
    /// This value is an int24 to avoid casting even though it is always positive.
    /// @return The tick spacing
    function tickSpacing() external view returns (int24);

    /// @notice The maximum amount of position liquidity that can use any tick in the range
    /// @dev This parameter is enforced per tick to prevent liquidity from overflowing a uint128 at any point, and
    /// also prevents out-of-range liquidity from being used to prevent adding in-range liquidity to a pool
    /// @return The max amount of liquidity per tick
    function maxLiquidityPerTick() external view returns (uint128);
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Permissioned pool actions
/// @notice Contains pool methods that may only be called by the factory owner
interface IUniswapV3PoolOwnerActions {
    /// @notice Set the denominator of the protocol's % share of the fees
    /// @param feeProtocol0 new protocol fee for token0 of the pool
    /// @param feeProtocol1 new protocol fee for token1 of the pool
    function setFeeProtocol(uint8 feeProtocol0, uint8 feeProtocol1) external;

    /// @notice Collect the protocol fee accrued to the pool
    /// @param recipient The address to which collected protocol fees should be sent
    /// @param amount0Requested The maximum amount of token0 to send, can be 0 to collect fees in only token1
    /// @param amount1Requested The maximum amount of token1 to send, can be 0 to collect fees in only token0
    /// @return amount0 The protocol fee collected in token0
    /// @return amount1 The protocol fee collected in token1
    function collectProtocol(
        address recipient,
        uint128 amount0Requested,
        uint128 amount1Requested
    ) external returns (uint128 amount0, uint128 amount1);
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Pool state that can change
/// @notice These methods compose the pool's state, and can change with any frequency including multiple times
/// per transaction
interface IUniswapV3PoolState {
    /// @notice The 0th storage slot in the pool stores many values, and is exposed as a single method to save gas
    /// when accessed externally.
    /// @return sqrtPriceX96 The current price of the pool as a sqrt(token1/token0) Q64.96 value
    /// @return tick The current tick of the pool, i.e. according to the last tick transition that was run.
    /// This value may not always be equal to SqrtTickMath.getTickAtSqrtRatio(sqrtPriceX96) if the price is on a tick
    /// boundary.
    /// @return observationIndex The index of the last oracle observation that was written,
    /// @return observationCardinality The current maximum number of observations stored in the pool,
    /// @return observationCardinalityNext The next maximum number of observations, to be updated when the observation.
    /// @return feeProtocol The protocol fee for both tokens of the pool.
    /// Encoded as two 4 bit values, where the protocol fee of token1 is shifted 4 bits and the protocol fee of token0
    /// is the lower 4 bits. Used as the denominator of a fraction of the swap fee, e.g. 4 means 1/4th of the swap fee.
    /// unlocked Whether the pool is currently locked to reentrancy
    function slot0()
        external
        view
        returns (
            uint160 sqrtPriceX96,
            int24 tick,
            uint16 observationIndex,
            uint16 observationCardinality,
            uint16 observationCardinalityNext,
            uint8 feeProtocol,
            bool unlocked
        );

    /// @notice The fee growth as a Q128.128 fees of token0 collected per unit of liquidity for the entire life of the pool
    /// @dev This value can overflow the uint256
    function feeGrowthGlobal0X128() external view returns (uint256);

    /// @notice The fee growth as a Q128.128 fees of token1 collected per unit of liquidity for the entire life of the pool
    /// @dev This value can overflow the uint256
    function feeGrowthGlobal1X128() external view returns (uint256);

    /// @notice The amounts of token0 and token1 that are owed to the protocol
    /// @dev Protocol fees will never exceed uint128 max in either token
    function protocolFees() external view returns (uint128 token0, uint128 token1);

    /// @notice The currently in range liquidity available to the pool
    /// @dev This value has no relationship to the total liquidity across all ticks
    /// @return The liquidity at the current price of the pool
    function liquidity() external view returns (uint128);

    /// @notice Look up information about a specific tick in the pool
    /// @param tick The tick to look up
    /// @return liquidityGross the total amount of position liquidity that uses the pool either as tick lower or
    /// tick upper
    /// @return liquidityNet how much liquidity changes when the pool price crosses the tick,
    /// @return feeGrowthOutside0X128 the fee growth on the other side of the tick from the current tick in token0,
    /// @return feeGrowthOutside1X128 the fee growth on the other side of the tick from the current tick in token1,
    /// @return tickCumulativeOutside the cumulative tick value on the other side of the tick from the current tick
    /// @return secondsPerLiquidityOutsideX128 the seconds spent per liquidity on the other side of the tick from the current tick,
    /// @return secondsOutside the seconds spent on the other side of the tick from the current tick,
    /// @return initialized Set to true if the tick is initialized, i.e. liquidityGross is greater than 0, otherwise equal to false.
    /// Outside values can only be used if the tick is initialized, i.e. if liquidityGross is greater than 0.
    /// In addition, these values are only relative and must be used only in comparison to previous snapshots for
    /// a specific position.
    function ticks(int24 tick)
        external
        view
        returns (
            uint128 liquidityGross,
            int128 liquidityNet,
            uint256 feeGrowthOutside0X128,
            uint256 feeGrowthOutside1X128,
            int56 tickCumulativeOutside,
            uint160 secondsPerLiquidityOutsideX128,
            uint32 secondsOutside,
            bool initialized
        );

    /// @notice Returns 256 packed tick initialized boolean values. See TickBitmap for more information
    function tickBitmap(int16 wordPosition) external view returns (uint256);

    /// @notice Returns the information about a position by the position's key
    /// @param key The position's key is a hash of a preimage composed by the owner, tickLower and tickUpper
    /// @return liquidity The amount of liquidity in the position,
    /// @return feeGrowthInside0LastX128 fee growth of token0 inside the tick range as of the last mint/burn/poke,
    /// @return feeGrowthInside1LastX128 fee growth of token1 inside the tick range as of the last mint/burn/poke,
    /// @return tokensOwed0 the computed amount of token0 owed to the position as of the last mint/burn/poke,
    /// @return tokensOwed1 the computed amount of token1 owed to the position as of the last mint/burn/poke
    function positions(bytes32 key)
        external
        view
        returns (
            uint128 liquidity,
            uint256 feeGrowthInside0LastX128,
            uint256 feeGrowthInside1LastX128,
            uint128 tokensOwed0,
            uint128 tokensOwed1
        );

    /// @notice Returns data about a specific observation index
    /// @param index The element of the observations array to fetch
    /// @dev You most likely want to use #observe() instead of this method to get an observation as of some amount of time
    /// ago, rather than at a specific index in the array.
    /// @return blockTimestamp The timestamp of the observation,
    /// @return tickCumulative the tick multiplied by seconds elapsed for the life of the pool as of the observation timestamp,
    /// @return secondsPerLiquidityCumulativeX128 the seconds per in range liquidity for the life of the pool as of the observation timestamp,
    /// @return initialized whether the observation has been initialized and the values are safe to use
    function observations(uint256 index)
        external
        view
        returns (
            uint32 blockTimestamp,
            int56 tickCumulative,
            uint160 secondsPerLiquidityCumulativeX128,
            bool initialized
        );
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

/// @title BitMath
/// @dev This library provides functionality for computing bit properties of an unsigned integer
library BitMath {
    /// @notice Returns the index of the most significant bit of the number,
    ///     where the least significant bit is at index 0 and the most significant bit is at index 255
    /// @dev The function satisfies the property:
    ///     x >= 2**mostSignificantBit(x) and x < 2**(mostSignificantBit(x)+1)
    /// @param x the value for which to compute the most significant bit, must be greater than 0
    /// @return r the index of the most significant bit
    function mostSignificantBit(uint256 x) internal pure returns (uint8 r) {
        require(x > 0);

        unchecked {
            if (x >= 0x100000000000000000000000000000000) {
                x >>= 128;
                r += 128;
            }
            if (x >= 0x10000000000000000) {
                x >>= 64;
                r += 64;
            }
            if (x >= 0x100000000) {
                x >>= 32;
                r += 32;
            }
            if (x >= 0x10000) {
                x >>= 16;
                r += 16;
            }
            if (x >= 0x100) {
                x >>= 8;
                r += 8;
            }
            if (x >= 0x10) {
                x >>= 4;
                r += 4;
            }
            if (x >= 0x4) {
                x >>= 2;
                r += 2;
            }
            if (x >= 0x2) r += 1;
        }
    }

    /// @notice Returns the index of the least significant bit of the number,
    ///     where the least significant bit is at index 0 and the most significant bit is at index 255
    /// @dev The function satisfies the property:
    ///     (x & 2**leastSignificantBit(x)) != 0 and (x & (2**(leastSignificantBit(x)) - 1)) == 0)
    /// @param x the value for which to compute the least significant bit, must be greater than 0
    /// @return r the index of the least significant bit
    function leastSignificantBit(uint256 x) internal pure returns (uint8 r) {
        require(x > 0);

        unchecked {
            r = 255;
            if (x & type(uint128).max > 0) {
                r -= 128;
            } else {
                x >>= 128;
            }
            if (x & type(uint64).max > 0) {
                r -= 64;
            } else {
                x >>= 64;
            }
            if (x & type(uint32).max > 0) {
                r -= 32;
            } else {
                x >>= 32;
            }
            if (x & type(uint16).max > 0) {
                r -= 16;
            } else {
                x >>= 16;
            }
            if (x & type(uint8).max > 0) {
                r -= 8;
            } else {
                x >>= 8;
            }
            if (x & 0xf > 0) {
                r -= 4;
            } else {
                x >>= 4;
            }
            if (x & 0x3 > 0) {
                r -= 2;
            } else {
                x >>= 2;
            }
            if (x & 0x1 > 0) r -= 1;
        }
    }
}

File 48 of 83 : FixedPoint96.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.4.0;

/// @title FixedPoint96
/// @notice A library for handling binary fixed point numbers, see https://en.wikipedia.org/wiki/Q_(number_format)
/// @dev Used in SqrtPriceMath.sol
library FixedPoint96 {
    uint8 internal constant RESOLUTION = 96;
    uint256 internal constant Q96 = 0x1000000000000000000000000;
}

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

/// @title Contains 512-bit math functions
/// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision
/// @dev Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits
library FullMath {
    /// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
    /// @param a The multiplicand
    /// @param b The multiplier
    /// @param denominator The divisor
    /// @return result The 256-bit result
    /// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv
    function mulDiv(
        uint256 a,
        uint256 b,
        uint256 denominator
    ) internal pure returns (uint256 result) {
        unchecked {
            // 512-bit multiply [prod1 prod0] = a * b
            // Compute the product mod 2**256 and mod 2**256 - 1
            // then use the Chinese Remainder Theorem to reconstruct
            // the 512 bit result. The result is stored in two 256
            // variables such that product = prod1 * 2**256 + prod0
            uint256 prod0; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(a, b, not(0))
                prod0 := mul(a, b)
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

            // Handle non-overflow cases, 256 by 256 division
            if (prod1 == 0) {
                require(denominator > 0);
                assembly {
                    result := div(prod0, denominator)
                }
                return result;
            }

            // Make sure the result is less than 2**256.
            // Also prevents denominator == 0
            require(denominator > prod1);

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

            // Make division exact by subtracting the remainder from [prod1 prod0]
            // Compute remainder using mulmod
            uint256 remainder;
            assembly {
                remainder := mulmod(a, b, denominator)
            }
            // Subtract 256 bit number from 512 bit number
            assembly {
                prod1 := sub(prod1, gt(remainder, prod0))
                prod0 := sub(prod0, remainder)
            }

            // Factor powers of two out of denominator
            // Compute largest power of two divisor of denominator.
            // Always >= 1.
            uint256 twos = (0 - denominator) & denominator;
            // Divide denominator by power of two
            assembly {
                denominator := div(denominator, twos)
            }

            // Divide [prod1 prod0] by the factors of two
            assembly {
                prod0 := div(prod0, twos)
            }
            // Shift in bits from prod1 into prod0. For this we need
            // to flip `twos` such that it is 2**256 / twos.
            // If twos is zero, then it becomes one
            assembly {
                twos := add(div(sub(0, twos), twos), 1)
            }
            prod0 |= prod1 * twos;

            // Invert denominator mod 2**256
            // Now that denominator is an odd number, it has an inverse
            // modulo 2**256 such that denominator * inv = 1 mod 2**256.
            // Compute the inverse by starting with a seed that is correct
            // correct for four bits. That is, denominator * inv = 1 mod 2**4
            uint256 inv = (3 * denominator) ^ 2;
            // Now use Newton-Raphson iteration to improve the precision.
            // Thanks to Hensel's lifting lemma, this also works in modular
            // arithmetic, doubling the correct bits in each step.
            inv *= 2 - denominator * inv; // inverse mod 2**8
            inv *= 2 - denominator * inv; // inverse mod 2**16
            inv *= 2 - denominator * inv; // inverse mod 2**32
            inv *= 2 - denominator * inv; // inverse mod 2**64
            inv *= 2 - denominator * inv; // inverse mod 2**128
            inv *= 2 - denominator * inv; // inverse mod 2**256

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

    /// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
    /// @param a The multiplicand
    /// @param b The multiplier
    /// @param denominator The divisor
    /// @return result The 256-bit result
    function mulDivRoundingUp(
        uint256 a,
        uint256 b,
        uint256 denominator
    ) internal pure returns (uint256 result) {
        unchecked {
            result = mulDiv(a, b, denominator);
            if (mulmod(a, b, denominator) > 0) {
                require(result < type(uint256).max);
                result++;
            }
        }
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Safe casting methods
/// @notice Contains methods for safely casting between types
library SafeCast {
    /// @notice Cast a uint256 to a uint160, revert on overflow
    /// @param y The uint256 to be downcasted
    /// @return z The downcasted integer, now type uint160
    function toUint160(uint256 y) internal pure returns (uint160 z) {
        require((z = uint160(y)) == y);
    }

    /// @notice Cast a int256 to a int128, revert on overflow or underflow
    /// @param y The int256 to be downcasted
    /// @return z The downcasted integer, now type int128
    function toInt128(int256 y) internal pure returns (int128 z) {
        require((z = int128(y)) == y);
    }

    /// @notice Cast a uint256 to a int256, revert on overflow
    /// @param y The uint256 to be casted
    /// @return z The casted integer, now type int256
    function toInt256(uint256 y) internal pure returns (int256 z) {
        require(y < 2**255);
        z = int256(y);
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {SwapMath} from './SwapMath.sol';
import {SafeCast} from './SafeCast.sol';
import {TickMath} from './TickMath.sol';
import {TickBitmap} from './TickBitmap.sol';
import {BitMath} from './BitMath.sol';

import {IUniswapV3Pool} from '../interfaces/IUniswapV3Pool.sol';

/// @title Library for simulating swaps.
/// @notice By fully replicating the swap logic, we can make a static call to get a quote.
library Simulate {
    using SafeCast for uint256;

    struct Cache {
        // price at the beginning of the swap
        uint160 sqrtPriceX96Start;
        // tick at the beginning of the swap
        int24 tickStart;
        // liquidity at the beginning of the swap
        uint128 liquidityStart;
        // the lp fee of the pool
        uint24 fee;
        // the tick spacing of the pool
        int24 tickSpacing;
    }

    struct State {
        // the amount remaining to be swapped in/out of the input/output asset
        int256 amountSpecifiedRemaining;
        // the amount already swapped out/in of the output/input asset
        int256 amountCalculated;
        // current sqrt(price)
        uint160 sqrtPriceX96;
        // the tick associated with the current price
        int24 tick;
        // the current liquidity in range
        uint128 liquidity;
    }

    // copied from UniswapV3Pool to avoid pragma issues associated with importing it
    struct StepComputations {
        // the price at the beginning of the step
        uint160 sqrtPriceStartX96;
        // the next tick to swap to from the current tick in the swap direction
        int24 tickNext;
        // whether tickNext is initialized or not
        bool initialized;
        // sqrt(price) for the next tick (1/0)
        uint160 sqrtPriceNextX96;
        // how much is being swapped in in this step
        uint256 amountIn;
        // how much is being swapped out
        uint256 amountOut;
        // how much fee is being paid in
        uint256 feeAmount;
    }

    function simulateSwap(
        IUniswapV3Pool pool,
        bool zeroForOne,
        int256 amountSpecified,
        uint160 sqrtPriceLimitX96
    ) internal view returns (int256 amount0, int256 amount1) {
        require(amountSpecified != 0, 'AS');

        (uint160 sqrtPriceX96, int24 tick, , , , , ) = pool.slot0();

        require(
            zeroForOne
                ? sqrtPriceLimitX96 < sqrtPriceX96 && sqrtPriceLimitX96 > TickMath.MIN_SQRT_RATIO
                : sqrtPriceLimitX96 > sqrtPriceX96 && sqrtPriceLimitX96 < TickMath.MAX_SQRT_RATIO,
            'SPL'
        );

        Cache memory cache = Cache({
            sqrtPriceX96Start: sqrtPriceX96,
            tickStart: tick,
            liquidityStart: pool.liquidity(),
            fee: pool.fee(),
            tickSpacing: pool.tickSpacing()
        });

        bool exactInput = amountSpecified > 0;

        State memory state = State({
            amountSpecifiedRemaining: amountSpecified,
            amountCalculated: 0,
            sqrtPriceX96: cache.sqrtPriceX96Start,
            tick: cache.tickStart,
            liquidity: cache.liquidityStart
        });

        while (state.amountSpecifiedRemaining != 0 && state.sqrtPriceX96 != sqrtPriceLimitX96) {
            StepComputations memory step;

            step.sqrtPriceStartX96 = state.sqrtPriceX96;

            (step.tickNext, step.initialized) = nextInitializedTickWithinOneWord(
                pool.tickBitmap,
                state.tick,
                cache.tickSpacing,
                zeroForOne
            );

            if (step.tickNext < TickMath.MIN_TICK) {
                step.tickNext = TickMath.MIN_TICK;
            } else if (step.tickNext > TickMath.MAX_TICK) {
                step.tickNext = TickMath.MAX_TICK;
            }

            step.sqrtPriceNextX96 = TickMath.getSqrtRatioAtTick(step.tickNext);

            (state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount) = SwapMath.computeSwapStep(
                state.sqrtPriceX96,
                (zeroForOne ? step.sqrtPriceNextX96 < sqrtPriceLimitX96 : step.sqrtPriceNextX96 > sqrtPriceLimitX96)
                    ? sqrtPriceLimitX96
                    : step.sqrtPriceNextX96,
                state.liquidity,
                state.amountSpecifiedRemaining,
                cache.fee
            );

            if (exactInput) {
                unchecked {
                    state.amountSpecifiedRemaining -= (step.amountIn + step.feeAmount).toInt256();
                }
                state.amountCalculated -= step.amountOut.toInt256();
            } else {
                unchecked {
                    state.amountSpecifiedRemaining += step.amountOut.toInt256();
                }
                state.amountCalculated += (step.amountIn + step.feeAmount).toInt256();
            }

            if (state.sqrtPriceX96 == step.sqrtPriceNextX96) {
                if (step.initialized) {
                    (, int128 liquidityNet, , , , , , ) = pool.ticks(step.tickNext);
                    unchecked {
                        if (zeroForOne) liquidityNet = -liquidityNet;
                    }

                    state.liquidity = liquidityNet < 0
                        ? state.liquidity - uint128(-liquidityNet)
                        : state.liquidity + uint128(liquidityNet);
                }

                unchecked {
                    state.tick = zeroForOne ? step.tickNext - 1 : step.tickNext;
                }
            } else if (state.sqrtPriceX96 != step.sqrtPriceStartX96) {
                // recompute unless we're on a lower tick boundary (i.e. already transitioned ticks), and haven't moved
                state.tick = TickMath.getTickAtSqrtRatio(state.sqrtPriceX96);
            }
        }

        (amount0, amount1) = zeroForOne == exactInput
            ? (amountSpecified - state.amountSpecifiedRemaining, state.amountCalculated)
            : (state.amountCalculated, amountSpecified - state.amountSpecifiedRemaining);
    }

    // This function replicates TickBitmap, but accepts a function pointer argument.
    // It's private because it's messy, and shouldn't be re-used.
    function nextInitializedTickWithinOneWord(
        function(int16) external view returns (uint256) self,
        int24 tick,
        int24 tickSpacing,
        bool lte
    ) private view returns (int24 next, bool initialized) {
        unchecked {
            int24 compressed = tick / tickSpacing;
            if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity

            if (lte) {
                (int16 wordPos, uint8 bitPos) = TickBitmap.position(compressed);
                // all the 1s at or to the right of the current bitPos
                uint256 mask = (1 << bitPos) - 1 + (1 << bitPos);
                uint256 masked = self(wordPos) & mask;

                // if there are no initialized ticks to the right of or at the current tick, return rightmost in the word
                initialized = masked != 0;
                // overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
                next = initialized
                    ? (compressed - int24(uint24(bitPos - BitMath.mostSignificantBit(masked)))) * tickSpacing
                    : (compressed - int24(uint24(bitPos))) * tickSpacing;
            } else {
                // start from the word of the next tick, since the current tick state doesn't matter
                (int16 wordPos, uint8 bitPos) = TickBitmap.position(compressed + 1);
                // all the 1s at or to the left of the bitPos
                uint256 mask = ~((1 << bitPos) - 1);
                uint256 masked = self(wordPos) & mask;

                // if there are no initialized ticks to the left of the current tick, return leftmost in the word
                initialized = masked != 0;
                // overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
                next = initialized
                    ? (compressed + 1 + int24(uint24(BitMath.leastSignificantBit(masked) - bitPos))) * tickSpacing
                    : (compressed + 1 + int24(uint24(type(uint8).max - bitPos))) * tickSpacing;
            }
        }
    }
}

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

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

import {FullMath} from './FullMath.sol';
import {UnsafeMath} from './UnsafeMath.sol';
import {FixedPoint96} from './FixedPoint96.sol';

/// @title Functions based on Q64.96 sqrt price and liquidity
/// @notice Contains the math that uses square root of price as a Q64.96 and liquidity to compute deltas
library SqrtPriceMath {
    using SafeCast for uint256;

    /// @notice Gets the next sqrt price given a delta of token0
    /// @dev Always rounds up, because in the exact output case (increasing price) we need to move the price at least
    /// far enough to get the desired output amount, and in the exact input case (decreasing price) we need to move the
    /// price less in order to not send too much output.
    /// The most precise formula for this is liquidity * sqrtPX96 / (liquidity +- amount * sqrtPX96),
    /// if this is impossible because of overflow, we calculate liquidity / (liquidity / sqrtPX96 +- amount).
    /// @param sqrtPX96 The starting price, i.e. before accounting for the token0 delta
    /// @param liquidity The amount of usable liquidity
    /// @param amount How much of token0 to add or remove from virtual reserves
    /// @param add Whether to add or remove the amount of token0
    /// @return The price after adding or removing amount, depending on add
    function getNextSqrtPriceFromAmount0RoundingUp(
        uint160 sqrtPX96,
        uint128 liquidity,
        uint256 amount,
        bool add
    ) internal pure returns (uint160) {
        // we short circuit amount == 0 because the result is otherwise not guaranteed to equal the input price
        if (amount == 0) return sqrtPX96;
        uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;

        if (add) {
            unchecked {
                uint256 product;
                if ((product = amount * sqrtPX96) / amount == sqrtPX96) {
                    uint256 denominator = numerator1 + product;
                    if (denominator >= numerator1)
                        // always fits in 160 bits
                        return uint160(FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator));
                }
            }
            // denominator is checked for overflow
            return uint160(UnsafeMath.divRoundingUp(numerator1, (numerator1 / sqrtPX96) + amount));
        } else {
            unchecked {
                uint256 product;
                // if the product overflows, we know the denominator underflows
                // in addition, we must check that the denominator does not underflow
                require((product = amount * sqrtPX96) / amount == sqrtPX96 && numerator1 > product);
                uint256 denominator = numerator1 - product;
                return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator).toUint160();
            }
        }
    }

    /// @notice Gets the next sqrt price given a delta of token1
    /// @dev Always rounds down, because in the exact output case (decreasing price) we need to move the price at least
    /// far enough to get the desired output amount, and in the exact input case (increasing price) we need to move the
    /// price less in order to not send too much output.
    /// The formula we compute is within <1 wei of the lossless version: sqrtPX96 +- amount / liquidity
    /// @param sqrtPX96 The starting price, i.e., before accounting for the token1 delta
    /// @param liquidity The amount of usable liquidity
    /// @param amount How much of token1 to add, or remove, from virtual reserves
    /// @param add Whether to add, or remove, the amount of token1
    /// @return The price after adding or removing `amount`
    function getNextSqrtPriceFromAmount1RoundingDown(
        uint160 sqrtPX96,
        uint128 liquidity,
        uint256 amount,
        bool add
    ) internal pure returns (uint160) {
        // if we're adding (subtracting), rounding down requires rounding the quotient down (up)
        // in both cases, avoid a mulDiv for most inputs
        if (add) {
            uint256 quotient = (
                amount <= type(uint160).max
                    ? (amount << FixedPoint96.RESOLUTION) / liquidity
                    : FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity)
            );

            return (uint256(sqrtPX96) + quotient).toUint160();
        } else {
            uint256 quotient = (
                amount <= type(uint160).max
                    ? UnsafeMath.divRoundingUp(amount << FixedPoint96.RESOLUTION, liquidity)
                    : FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity)
            );

            require(sqrtPX96 > quotient);
            // always fits 160 bits
            unchecked {
                return uint160(sqrtPX96 - quotient);
            }
        }
    }

    /// @notice Gets the next sqrt price given an input amount of token0 or token1
    /// @dev Throws if price or liquidity are 0, or if the next price is out of bounds
    /// @param sqrtPX96 The starting price, i.e., before accounting for the input amount
    /// @param liquidity The amount of usable liquidity
    /// @param amountIn How much of token0, or token1, is being swapped in
    /// @param zeroForOne Whether the amount in is token0 or token1
    /// @return sqrtQX96 The price after adding the input amount to token0 or token1
    function getNextSqrtPriceFromInput(
        uint160 sqrtPX96,
        uint128 liquidity,
        uint256 amountIn,
        bool zeroForOne
    ) internal pure returns (uint160 sqrtQX96) {
        require(sqrtPX96 > 0);
        require(liquidity > 0);

        // round to make sure that we don't pass the target price
        return
            zeroForOne
                ? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
                : getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true);
    }

    /// @notice Gets the next sqrt price given an output amount of token0 or token1
    /// @dev Throws if price or liquidity are 0 or the next price is out of bounds
    /// @param sqrtPX96 The starting price before accounting for the output amount
    /// @param liquidity The amount of usable liquidity
    /// @param amountOut How much of token0, or token1, is being swapped out
    /// @param zeroForOne Whether the amount out is token0 or token1
    /// @return sqrtQX96 The price after removing the output amount of token0 or token1
    function getNextSqrtPriceFromOutput(
        uint160 sqrtPX96,
        uint128 liquidity,
        uint256 amountOut,
        bool zeroForOne
    ) internal pure returns (uint160 sqrtQX96) {
        require(sqrtPX96 > 0);
        require(liquidity > 0);

        // round to make sure that we pass the target price
        return
            zeroForOne
                ? getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
                : getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false);
    }

    /// @notice Gets the amount0 delta between two prices
    /// @dev Calculates liquidity / sqrt(lower) - liquidity / sqrt(upper),
    /// i.e. liquidity * (sqrt(upper) - sqrt(lower)) / (sqrt(upper) * sqrt(lower))
    /// @param sqrtRatioAX96 A sqrt price
    /// @param sqrtRatioBX96 Another sqrt price
    /// @param liquidity The amount of usable liquidity
    /// @param roundUp Whether to round the amount up or down
    /// @return amount0 Amount of token0 required to cover a position of size liquidity between the two passed prices
    function getAmount0Delta(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity,
        bool roundUp
    ) internal pure returns (uint256 amount0) {
        unchecked {
            if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

            uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
            uint256 numerator2 = sqrtRatioBX96 - sqrtRatioAX96;

            require(sqrtRatioAX96 > 0);

            return
                roundUp
                    ? UnsafeMath.divRoundingUp(
                        FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96),
                        sqrtRatioAX96
                    )
                    : FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96) / sqrtRatioAX96;
        }
    }

    /// @notice Gets the amount1 delta between two prices
    /// @dev Calculates liquidity * (sqrt(upper) - sqrt(lower))
    /// @param sqrtRatioAX96 A sqrt price
    /// @param sqrtRatioBX96 Another sqrt price
    /// @param liquidity The amount of usable liquidity
    /// @param roundUp Whether to round the amount up, or down
    /// @return amount1 Amount of token1 required to cover a position of size liquidity between the two passed prices
    function getAmount1Delta(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity,
        bool roundUp
    ) internal pure returns (uint256 amount1) {
        unchecked {
            if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

            return
                roundUp
                    ? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96)
                    : FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
        }
    }

    /// @notice Helper that gets signed token0 delta
    /// @param sqrtRatioAX96 A sqrt price
    /// @param sqrtRatioBX96 Another sqrt price
    /// @param liquidity The change in liquidity for which to compute the amount0 delta
    /// @return amount0 Amount of token0 corresponding to the passed liquidityDelta between the two prices
    function getAmount0Delta(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        int128 liquidity
    ) internal pure returns (int256 amount0) {
        unchecked {
            return
                liquidity < 0
                    ? -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
                    : getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
        }
    }

    /// @notice Helper that gets signed token1 delta
    /// @param sqrtRatioAX96 A sqrt price
    /// @param sqrtRatioBX96 Another sqrt price
    /// @param liquidity The change in liquidity for which to compute the amount1 delta
    /// @return amount1 Amount of token1 corresponding to the passed liquidityDelta between the two prices
    function getAmount1Delta(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        int128 liquidity
    ) internal pure returns (int256 amount1) {
        unchecked {
            return
                liquidity < 0
                    ? -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
                    : getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
        }
    }
}

File 53 of 83 : SwapMath.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {FullMath} from './FullMath.sol';
import {SqrtPriceMath} from './SqrtPriceMath.sol';

/// @title Computes the result of a swap within ticks
/// @notice Contains methods for computing the result of a swap within a single tick price range, i.e., a single tick.
library SwapMath {
    /// @notice Computes the result of swapping some amount in, or amount out, given the parameters of the swap
    /// @dev The fee, plus the amount in, will never exceed the amount remaining if the swap's `amountSpecified` is positive
    /// @param sqrtRatioCurrentX96 The current sqrt price of the pool
    /// @param sqrtRatioTargetX96 The price that cannot be exceeded, from which the direction of the swap is inferred
    /// @param liquidity The usable liquidity
    /// @param amountRemaining How much input or output amount is remaining to be swapped in/out
    /// @param feePips The fee taken from the input amount, expressed in hundredths of a bip
    /// @return sqrtRatioNextX96 The price after swapping the amount in/out, not to exceed the price target
    /// @return amountIn The amount to be swapped in, of either token0 or token1, based on the direction of the swap
    /// @return amountOut The amount to be received, of either token0 or token1, based on the direction of the swap
    /// @return feeAmount The amount of input that will be taken as a fee
    function computeSwapStep(
        uint160 sqrtRatioCurrentX96,
        uint160 sqrtRatioTargetX96,
        uint128 liquidity,
        int256 amountRemaining,
        uint24 feePips
    )
        internal
        pure
        returns (
            uint160 sqrtRatioNextX96,
            uint256 amountIn,
            uint256 amountOut,
            uint256 feeAmount
        )
    {
        unchecked {
            bool zeroForOne = sqrtRatioCurrentX96 >= sqrtRatioTargetX96;
            bool exactIn = amountRemaining >= 0;

            if (exactIn) {
                uint256 amountRemainingLessFee = FullMath.mulDiv(uint256(amountRemaining), 1e6 - feePips, 1e6);
                amountIn = zeroForOne
                    ? SqrtPriceMath.getAmount0Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true)
                    : SqrtPriceMath.getAmount1Delta(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, true);
                if (amountRemainingLessFee >= amountIn) sqrtRatioNextX96 = sqrtRatioTargetX96;
                else
                    sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromInput(
                        sqrtRatioCurrentX96,
                        liquidity,
                        amountRemainingLessFee,
                        zeroForOne
                    );
            } else {
                amountOut = zeroForOne
                    ? SqrtPriceMath.getAmount1Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false)
                    : SqrtPriceMath.getAmount0Delta(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, false);
                if (uint256(-amountRemaining) >= amountOut) sqrtRatioNextX96 = sqrtRatioTargetX96;
                else
                    sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromOutput(
                        sqrtRatioCurrentX96,
                        liquidity,
                        uint256(-amountRemaining),
                        zeroForOne
                    );
            }

            bool max = sqrtRatioTargetX96 == sqrtRatioNextX96;

            // get the input/output amounts
            if (zeroForOne) {
                amountIn = max && exactIn
                    ? amountIn
                    : SqrtPriceMath.getAmount0Delta(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, true);
                amountOut = max && !exactIn
                    ? amountOut
                    : SqrtPriceMath.getAmount1Delta(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, false);
            } else {
                amountIn = max && exactIn
                    ? amountIn
                    : SqrtPriceMath.getAmount1Delta(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, true);
                amountOut = max && !exactIn
                    ? amountOut
                    : SqrtPriceMath.getAmount0Delta(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, false);
            }

            // cap the output amount to not exceed the remaining output amount
            if (!exactIn && amountOut > uint256(-amountRemaining)) {
                amountOut = uint256(-amountRemaining);
            }

            if (exactIn && sqrtRatioNextX96 != sqrtRatioTargetX96) {
                // we didn't reach the target, so take the remainder of the maximum input as fee
                feeAmount = uint256(amountRemaining) - amountIn;
            } else {
                feeAmount = FullMath.mulDivRoundingUp(amountIn, feePips, 1e6 - feePips);
            }
        }
    }
}

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

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

/// @title Packed tick initialized state library
/// @notice Stores a packed mapping of tick index to its initialized state
/// @dev The mapping uses int16 for keys since ticks are represented as int24 and there are 256 (2^8) values per word.
library TickBitmap {
    /// @notice Computes the position in the mapping where the initialized bit for a tick lives
    /// @param tick The tick for which to compute the position
    /// @return wordPos The key in the mapping containing the word in which the bit is stored
    /// @return bitPos The bit position in the word where the flag is stored
    function position(int24 tick) internal pure returns (int16 wordPos, uint8 bitPos) {
        unchecked {
            wordPos = int16(tick >> 8);
            bitPos = uint8(int8(tick % 256));
        }
    }

    /// @notice Flips the initialized state for a given tick from false to true, or vice versa
    /// @param self The mapping in which to flip the tick
    /// @param tick The tick to flip
    /// @param tickSpacing The spacing between usable ticks
    function flipTick(
        mapping(int16 => uint256) storage self,
        int24 tick,
        int24 tickSpacing
    ) internal {
        unchecked {
            require(tick % tickSpacing == 0); // ensure that the tick is spaced
            (int16 wordPos, uint8 bitPos) = position(tick / tickSpacing);
            uint256 mask = 1 << bitPos;
            self[wordPos] ^= mask;
        }
    }

    /// @notice Returns the next initialized tick contained in the same word (or adjacent word) as the tick that is either
    /// to the left (less than or equal to) or right (greater than) of the given tick
    /// @param self The mapping in which to compute the next initialized tick
    /// @param tick The starting tick
    /// @param tickSpacing The spacing between usable ticks
    /// @param lte Whether to search for the next initialized tick to the left (less than or equal to the starting tick)
    /// @return next The next initialized or uninitialized tick up to 256 ticks away from the current tick
    /// @return initialized Whether the next tick is initialized, as the function only searches within up to 256 ticks
    function nextInitializedTickWithinOneWord(
        mapping(int16 => uint256) storage self,
        int24 tick,
        int24 tickSpacing,
        bool lte
    ) internal view returns (int24 next, bool initialized) {
        unchecked {
            int24 compressed = tick / tickSpacing;
            if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity

            if (lte) {
                (int16 wordPos, uint8 bitPos) = position(compressed);
                // all the 1s at or to the right of the current bitPos
                uint256 mask = (1 << bitPos) - 1 + (1 << bitPos);
                uint256 masked = self[wordPos] & mask;

                // if there are no initialized ticks to the right of or at the current tick, return rightmost in the word
                initialized = masked != 0;
                // overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
                next = initialized
                    ? (compressed - int24(uint24(bitPos - BitMath.mostSignificantBit(masked)))) * tickSpacing
                    : (compressed - int24(uint24(bitPos))) * tickSpacing;
            } else {
                // start from the word of the next tick, since the current tick state doesn't matter
                (int16 wordPos, uint8 bitPos) = position(compressed + 1);
                // all the 1s at or to the left of the bitPos
                uint256 mask = ~((1 << bitPos) - 1);
                uint256 masked = self[wordPos] & mask;

                // if there are no initialized ticks to the left of the current tick, return leftmost in the word
                initialized = masked != 0;
                // overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
                next = initialized
                    ? (compressed + 1 + int24(uint24(BitMath.leastSignificantBit(masked) - bitPos))) * tickSpacing
                    : (compressed + 1 + int24(uint24(type(uint8).max - bitPos))) * tickSpacing;
            }
        }
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

/// @title Math library for computing sqrt prices from ticks and vice versa
/// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports
/// prices between 2**-128 and 2**128
library TickMath {
    error T();
    error R();

    /// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
    int24 internal constant MIN_TICK = -887272;
    /// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
    int24 internal constant MAX_TICK = -MIN_TICK;

    /// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
    uint160 internal constant MIN_SQRT_RATIO = 4295128739;
    /// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
    uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;

    /// @notice Calculates sqrt(1.0001^tick) * 2^96
    /// @dev Throws if |tick| > max tick
    /// @param tick The input tick for the above formula
    /// @return sqrtPriceX96 A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)
    /// at the given tick
    function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96) {
        unchecked {
            uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick));
            if (absTick > uint256(int256(MAX_TICK))) revert T();

            uint256 ratio = absTick & 0x1 != 0
                ? 0xfffcb933bd6fad37aa2d162d1a594001
                : 0x100000000000000000000000000000000;
            if (absTick & 0x2 != 0) ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128;
            if (absTick & 0x4 != 0) ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128;
            if (absTick & 0x8 != 0) ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128;
            if (absTick & 0x10 != 0) ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128;
            if (absTick & 0x20 != 0) ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128;
            if (absTick & 0x40 != 0) ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128;
            if (absTick & 0x80 != 0) ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128;
            if (absTick & 0x100 != 0) ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128;
            if (absTick & 0x200 != 0) ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128;
            if (absTick & 0x400 != 0) ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128;
            if (absTick & 0x800 != 0) ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128;
            if (absTick & 0x1000 != 0) ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
            if (absTick & 0x2000 != 0) ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128;
            if (absTick & 0x4000 != 0) ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128;
            if (absTick & 0x8000 != 0) ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128;
            if (absTick & 0x10000 != 0) ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128;
            if (absTick & 0x20000 != 0) ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128;
            if (absTick & 0x40000 != 0) ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128;
            if (absTick & 0x80000 != 0) ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128;

            if (tick > 0) ratio = type(uint256).max / ratio;

            // this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
            // we then downcast because we know the result always fits within 160 bits due to our tick input constraint
            // we round up in the division so getTickAtSqrtRatio of the output price is always consistent
            sqrtPriceX96 = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
        }
    }

    /// @notice Calculates the greatest tick value such that getRatioAtTick(tick) <= ratio
    /// @dev Throws in case sqrtPriceX96 < MIN_SQRT_RATIO, as MIN_SQRT_RATIO is the lowest value getRatioAtTick may
    /// ever return.
    /// @param sqrtPriceX96 The sqrt ratio for which to compute the tick as a Q64.96
    /// @return tick The greatest tick for which the ratio is less than or equal to the input ratio
    function getTickAtSqrtRatio(uint160 sqrtPriceX96) internal pure returns (int24 tick) {
        unchecked {
            // second inequality must be < because the price can never reach the price at the max tick
            if (!(sqrtPriceX96 >= MIN_SQRT_RATIO && sqrtPriceX96 < MAX_SQRT_RATIO)) revert R();
            uint256 ratio = uint256(sqrtPriceX96) << 32;

            uint256 r = ratio;
            uint256 msb = 0;

            assembly {
                let f := shl(7, gt(r, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))
                msb := or(msb, f)
                r := shr(f, r)
            }
            assembly {
                let f := shl(6, gt(r, 0xFFFFFFFFFFFFFFFF))
                msb := or(msb, f)
                r := shr(f, r)
            }
            assembly {
                let f := shl(5, gt(r, 0xFFFFFFFF))
                msb := or(msb, f)
                r := shr(f, r)
            }
            assembly {
                let f := shl(4, gt(r, 0xFFFF))
                msb := or(msb, f)
                r := shr(f, r)
            }
            assembly {
                let f := shl(3, gt(r, 0xFF))
                msb := or(msb, f)
                r := shr(f, r)
            }
            assembly {
                let f := shl(2, gt(r, 0xF))
                msb := or(msb, f)
                r := shr(f, r)
            }
            assembly {
                let f := shl(1, gt(r, 0x3))
                msb := or(msb, f)
                r := shr(f, r)
            }
            assembly {
                let f := gt(r, 0x1)
                msb := or(msb, f)
            }

            if (msb >= 128) r = ratio >> (msb - 127);
            else r = ratio << (127 - msb);

            int256 log_2 = (int256(msb) - 128) << 64;

            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(63, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(62, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(61, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(60, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(59, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(58, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(57, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(56, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(55, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(54, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(53, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(52, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(51, f))
                r := shr(f, r)
            }
            assembly {
                r := shr(127, mul(r, r))
                let f := shr(128, r)
                log_2 := or(log_2, shl(50, f))
            }

            int256 log_sqrt10001 = log_2 * 255738958999603826347141; // 128.128 number

            int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128);
            int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128);

            tick = tickLow == tickHi ? tickLow : getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
        }
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Math functions that do not check inputs or outputs
/// @notice Contains methods that perform common math functions but do not do any overflow or underflow checks
library UnsafeMath {
    /// @notice Returns ceil(x / y)
    /// @dev division by 0 has unspecified behavior, and must be checked externally
    /// @param x The dividend
    /// @param y The divisor
    /// @return z The quotient, ceil(x / y)
    function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
        assembly {
            z := add(div(x, y), gt(mod(x, y), 0))
        }
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.7.5;
pragma abicoder v2;

import '@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol';

/// @title Router token swapping functionality
/// @notice Functions for swapping tokens via Uniswap V3
interface ISwapRouter is IUniswapV3SwapCallback {
    struct ExactInputSingleParams {
        address tokenIn;
        address tokenOut;
        uint24 fee;
        address recipient;
        uint256 deadline;
        uint256 amountIn;
        uint256 amountOutMinimum;
        uint160 sqrtPriceLimitX96;
    }

    /// @notice Swaps `amountIn` of one token for as much as possible of another token
    /// @param params The parameters necessary for the swap, encoded as `ExactInputSingleParams` in calldata
    /// @return amountOut The amount of the received token
    function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);

    struct ExactInputParams {
        bytes path;
        address recipient;
        uint256 deadline;
        uint256 amountIn;
        uint256 amountOutMinimum;
    }

    /// @notice Swaps `amountIn` of one token for as much as possible of another along the specified path
    /// @param params The parameters necessary for the multi-hop swap, encoded as `ExactInputParams` in calldata
    /// @return amountOut The amount of the received token
    function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);

    struct ExactOutputSingleParams {
        address tokenIn;
        address tokenOut;
        uint24 fee;
        address recipient;
        uint256 deadline;
        uint256 amountOut;
        uint256 amountInMaximum;
        uint160 sqrtPriceLimitX96;
    }

    /// @notice Swaps as little as possible of one token for `amountOut` of another token
    /// @param params The parameters necessary for the swap, encoded as `ExactOutputSingleParams` in calldata
    /// @return amountIn The amount of the input token
    function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);

    struct ExactOutputParams {
        bytes path;
        address recipient;
        uint256 deadline;
        uint256 amountOut;
        uint256 amountInMaximum;
    }

    /// @notice Swaps as little as possible of one token for `amountOut` of another along the specified path (reversed)
    /// @param params The parameters necessary for the multi-hop swap, encoded as `ExactOutputParams` in calldata
    /// @return amountIn The amount of the input token
    function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
}

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * @title Solidity Bytes Arrays Utils
 * @author Gonçalo Sá <[email protected]>
 *
 * @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
 *      The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
 */
pragma solidity >=0.8.0 <0.9.0;

library BytesLib {
    function slice(
        bytes memory _bytes,
        uint256 _start,
        uint256 _length
    ) internal pure returns (bytes memory) {
        require(_length + 31 >= _length, 'slice_overflow');
        require(_bytes.length >= _start + _length, 'slice_outOfBounds');

        bytes memory tempBytes;

        assembly {
            switch iszero(_length)
            case 0 {
                // Get a location of some free memory and store it in tempBytes as
                // Solidity does for memory variables.
                tempBytes := mload(0x40)

                // The first word of the slice result is potentially a partial
                // word read from the original array. To read it, we calculate
                // the length of that partial word and start copying that many
                // bytes into the array. The first word we copy will start with
                // data we don't care about, but the last `lengthmod` bytes will
                // land at the beginning of the contents of the new array. When
                // we're done copying, we overwrite the full first word with
                // the actual length of the slice.
                let lengthmod := and(_length, 31)

                // The multiplication in the next line is necessary
                // because when slicing multiples of 32 bytes (lengthmod == 0)
                // the following copy loop was copying the origin's length
                // and then ending prematurely not copying everything it should.
                let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
                let end := add(mc, _length)

                for {
                    // The multiplication in the next line has the same exact purpose
                    // as the one above.
                    let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
                } lt(mc, end) {
                    mc := add(mc, 0x20)
                    cc := add(cc, 0x20)
                } {
                    mstore(mc, mload(cc))
                }

                mstore(tempBytes, _length)

                //update free-memory pointer
                //allocating the array padded to 32 bytes like the compiler does now
                mstore(0x40, and(add(mc, 31), not(31)))
            }
            //if we want a zero-length slice let's just return a zero-length array
            default {
                tempBytes := mload(0x40)
                //zero out the 32 bytes slice we are about to return
                //we need to do it because Solidity does not garbage collect
                mstore(tempBytes, 0)

                mstore(0x40, add(tempBytes, 0x20))
            }
        }

        return tempBytes;
    }

    function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) {
        require(_bytes.length >= _start + 20, 'toAddress_outOfBounds');
        address tempAddress;

        assembly {
            tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000)
        }

        return tempAddress;
    }

    function toUint24(bytes memory _bytes, uint256 _start) internal pure returns (uint24) {
        require(_start + 3 >= _start, 'toUint24_overflow');
        require(_bytes.length >= _start + 3, 'toUint24_outOfBounds');
        uint24 tempUint;

        assembly {
            tempUint := mload(add(add(_bytes, 0x3), _start))
        }

        return tempUint;
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
import './PoolAddress.sol';

/// @notice Provides validation for callbacks from Uniswap V3 Pools
library CallbackValidation {
    /// @notice Returns the address of a valid Uniswap V3 Pool
    /// @param factory The contract address of the Uniswap V3 factory
    /// @param tokenA The contract address of either token0 or token1
    /// @param tokenB The contract address of the other token
    /// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
    /// @return pool The V3 pool contract address
    function verifyCallback(
        address factory,
        address tokenA,
        address tokenB,
        uint24 fee
    ) internal view returns (IUniswapV3Pool pool) {
        return verifyCallback(factory, PoolAddress.getPoolKey(tokenA, tokenB, fee));
    }

    /// @notice Returns the address of a valid Uniswap V3 Pool
    /// @param factory The contract address of the Uniswap V3 factory
    /// @param poolKey The identifying key of the V3 pool
    /// @return pool The V3 pool contract address
    function verifyCallback(address factory, PoolAddress.PoolKey memory poolKey)
        internal
        view
        returns (IUniswapV3Pool pool)
    {
        pool = IUniswapV3Pool(PoolAddress.computeAddress(factory, poolKey));
        require(msg.sender == address(pool));
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.6.0;

import './BytesLib.sol';

/// @title Functions for manipulating path data for multihop swaps
library Path {
    using BytesLib for bytes;

    /// @dev The length of the bytes encoded address
    uint256 private constant ADDR_SIZE = 20;
    /// @dev The length of the bytes encoded fee
    uint256 private constant FEE_SIZE = 3;

    /// @dev The offset of a single token address and pool fee
    uint256 private constant NEXT_OFFSET = ADDR_SIZE + FEE_SIZE;
    /// @dev The offset of an encoded pool key
    uint256 private constant POP_OFFSET = NEXT_OFFSET + ADDR_SIZE;
    /// @dev The minimum length of an encoding that contains 2 or more pools
    uint256 private constant MULTIPLE_POOLS_MIN_LENGTH = POP_OFFSET + NEXT_OFFSET;

    /// @notice Returns true iff the path contains two or more pools
    /// @param path The encoded swap path
    /// @return True if path contains two or more pools, otherwise false
    function hasMultiplePools(bytes memory path) internal pure returns (bool) {
        return path.length >= MULTIPLE_POOLS_MIN_LENGTH;
    }

    /// @notice Returns the number of pools in the path
    /// @param path The encoded swap path
    /// @return The number of pools in the path
    function numPools(bytes memory path) internal pure returns (uint256) {
        // Ignore the first token address. From then on every fee and token offset indicates a pool.
        return ((path.length - ADDR_SIZE) / NEXT_OFFSET);
    }

    /// @notice Decodes the first pool in path
    /// @param path The bytes encoded swap path
    /// @return tokenA The first token of the given pool
    /// @return tokenB The second token of the given pool
    /// @return fee The fee level of the pool
    function decodeFirstPool(bytes memory path)
        internal
        pure
        returns (
            address tokenA,
            address tokenB,
            uint24 fee
        )
    {
        tokenA = path.toAddress(0);
        fee = path.toUint24(ADDR_SIZE);
        tokenB = path.toAddress(NEXT_OFFSET);
    }

    /// @notice Gets the segment corresponding to the first pool in the path
    /// @param path The bytes encoded swap path
    /// @return The segment containing all data necessary to target the first pool in the path
    function getFirstPool(bytes memory path) internal pure returns (bytes memory) {
        return path.slice(0, POP_OFFSET);
    }

    /// @notice Skips a token + fee element from the buffer and returns the remainder
    /// @param path The swap path
    /// @return The remaining token + fee elements in the path
    function skipToken(bytes memory path) internal pure returns (bytes memory) {
        return path.slice(NEXT_OFFSET, path.length - NEXT_OFFSET);
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee
library PoolAddress {
    bytes32 internal constant POOL_INIT_CODE_HASH = 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;

    /// @notice The identifying key of the pool
    struct PoolKey {
        address token0;
        address token1;
        uint24 fee;
    }

    /// @notice Returns PoolKey: the ordered tokens with the matched fee levels
    /// @param tokenA The first token of a pool, unsorted
    /// @param tokenB The second token of a pool, unsorted
    /// @param fee The fee level of the pool
    /// @return Poolkey The pool details with ordered token0 and token1 assignments
    function getPoolKey(
        address tokenA,
        address tokenB,
        uint24 fee
    ) internal pure returns (PoolKey memory) {
        if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA);
        return PoolKey({token0: tokenA, token1: tokenB, fee: fee});
    }

    /// @notice Deterministically computes the pool address given the factory and PoolKey
    /// @param factory The Uniswap V3 factory contract address
    /// @param key The PoolKey
    /// @return pool The contract address of the V3 pool
    function computeAddress(address factory, PoolKey memory key) internal pure returns (address pool) {
        require(key.token0 < key.token1);
        pool = address(
            uint160(
                uint256(
                    keccak256(
                        abi.encodePacked(
                            hex'ff',
                            factory,
                            keccak256(abi.encode(key.token0, key.token1, key.fee)),
                            POOL_INIT_CODE_HASH
                        )
                    )
                )
            )
        );
    }
}

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

import { SafeERC20 } from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import { IERC20Metadata } from '@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol';
import { ERC20Upgradeable } from '@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol';

import { FixedPointMathLib } from '@rari-capital/solmate/src/utils/FixedPointMathLib.sol';

import { IERC4626 } from '../interfaces/IERC4626.sol';

/// @notice Minimal ERC4626 tokenized Vault implementation.
/// @author Copied and modified from Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/mixins/ERC4626.sol)
abstract contract ERC4626Upgradeable is IERC4626, ERC20Upgradeable {
    using SafeERC20 for IERC20Metadata;
    using FixedPointMathLib for uint256;

    /*//////////////////////////////////////////////////////////////
                               STATE
    //////////////////////////////////////////////////////////////*/

    /**
     * @dev Returns the address of the underlying token used for the Vault for accounting, depositing, and withdrawing.
     *
     * - MUST be an ERC-20 token contract.
     * - MUST NOT revert.
     */
    address public asset;

    // these gaps are added to allow adding new variables without shifting down inheritance chain
    uint256[50] private __gaps;

    /* solhint-disable func-name-mixedcase */
    function __ERC4626Upgradeable_init(address _asset, string memory _name, string memory _symbol) internal {
        __ERC20_init(_name, _symbol);
        asset = _asset;
    }

    /*//////////////////////////////////////////////////////////////
                        DEPOSIT/WITHDRAWAL LOGIC
    //////////////////////////////////////////////////////////////*/

    /**
     * @dev Mints shares Vault shares to receiver by depositing exactly amount of underlying tokens.
     *
     * - MUST emit the Deposit event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   deposit execution, and are accounted for during deposit.
     * - MUST revert if all of assets cannot be deposited (due to deposit limit being reached, slippage, the user not
     *   approving enough underlying tokens to the Vault contract, etc).
     *
     * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
     */
    function deposit(uint256 assets, address receiver) public virtual returns (uint256 shares) {
        // Check for rounding error since we round down in previewDeposit.
        require((shares = previewDeposit(assets)) != 0, 'ZERO_SHARES');

        // Need to transfer before minting or ERC777s could reenter.
        IERC20Metadata(asset).safeTransferFrom(msg.sender, address(this), assets);

        _mint(receiver, shares);

        emit Deposit(msg.sender, receiver, assets, shares);

        afterDeposit(assets, shares, receiver);
    }

    /**
     * @dev Mints exactly shares Vault shares to receiver by depositing amount of underlying tokens.
     *
     * - MUST emit the Deposit event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the mint
     *   execution, and are accounted for during mint.
     * - MUST revert if all of shares cannot be minted (due to deposit limit being reached, slippage, the user not
     *   approving enough underlying tokens to the Vault contract, etc).
     *
     * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
     */
    function mint(uint256 shares, address receiver) public virtual returns (uint256 assets) {
        assets = previewMint(shares); // No need to check for rounding error, previewMint rounds up.

        // Need to transfer before minting or ERC777s could reenter.
        IERC20Metadata(asset).safeTransferFrom(msg.sender, address(this), assets);

        _mint(receiver, shares);

        emit Deposit(msg.sender, receiver, assets, shares);

        afterDeposit(assets, shares, receiver);
    }

    /**
     * @dev Burns shares from owner and sends exactly assets of underlying tokens to receiver.
     *
     * - MUST emit the Withdraw event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   withdraw execution, and are accounted for during withdraw.
     * - MUST revert if all of assets cannot be withdrawn (due to withdrawal limit being reached, slippage, the owner
     *   not having enough shares, etc).
     *
     * Note that some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
     * Those methods should be performed separately.
     */
    function withdraw(uint256 assets, address receiver, address owner) public virtual returns (uint256 shares) {
        shares = previewWithdraw(assets); // No need to check for rounding error, previewWithdraw rounds up.

        if (msg.sender != owner) {
            uint256 allowed = allowance(owner, msg.sender); // Saves gas for limited approvals.

            if (allowed != type(uint256).max) _approve(owner, msg.sender, allowed - shares);
        }

        beforeWithdraw(assets, shares, receiver);

        _burn(owner, shares);

        emit Withdraw(msg.sender, receiver, owner, assets, shares);

        IERC20Metadata(asset).safeTransfer(receiver, assets);
    }

    /**
     * @dev Burns exactly shares from owner and sends assets of underlying tokens to receiver.
     *
     * - MUST emit the Withdraw event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   redeem execution, and are accounted for during redeem.
     * - MUST revert if all of shares cannot be redeemed (due to withdrawal limit being reached, slippage, the owner
     *   not having enough shares, etc).
     *
     * NOTE: some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
     * Those methods should be performed separately.
     */
    function redeem(uint256 shares, address receiver, address owner) public virtual returns (uint256 assets) {
        if (msg.sender != owner) {
            uint256 allowed = allowance(owner, msg.sender); // Saves gas for limited approvals.

            if (allowed != type(uint256).max) _approve(owner, msg.sender, allowed - shares);
        }

        // Check for rounding error since we round down in previewRedeem.
        require((assets = previewRedeem(shares)) != 0, 'ZERO_ASSETS');

        beforeWithdraw(assets, shares, receiver);

        _burn(owner, shares);

        emit Withdraw(msg.sender, receiver, owner, assets, shares);

        IERC20Metadata(asset).safeTransfer(receiver, assets);
    }

    /*//////////////////////////////////////////////////////////////
                            ACCOUNTING LOGIC
    //////////////////////////////////////////////////////////////*/

    /**
     * @dev Returns the total amount of the underlying asset that is “managed” by Vault.
     *
     * - SHOULD include any compounding that occurs from yield.
     * - MUST be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT revert.
     */
    function totalAssets() public view virtual returns (uint256);

    /**
     * @dev Returns the amount of shares that the Vault would exchange for the amount of assets provided, in an ideal
     * scenario where all the conditions are met.
     *
     * - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT show any variations depending on the caller.
     * - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
     * - MUST NOT revert.
     *
     * NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
     * “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
     * from.
     */
    function convertToShares(uint256 assets) public view virtual returns (uint256) {
        uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

        return supply == 0 ? assets : assets.mulDivDown(supply, totalAssets());
    }

    /**
     * @dev Returns the amount of assets that the Vault would exchange for the amount of shares provided, in an ideal
     * scenario where all the conditions are met.
     *
     * - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT show any variations depending on the caller.
     * - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
     * - MUST NOT revert.
     *
     * NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
     * “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
     * from.
     */
    function convertToAssets(uint256 shares) public view virtual returns (uint256) {
        uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

        return supply == 0 ? shares : shares.mulDivDown(totalAssets(), supply);
    }

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their deposit at the current block, given
     * current on-chain conditions.
     *
     * - MUST return as close to and no more than the exact amount of Vault shares that would be minted in a deposit
     *   call in the same transaction. I.e. deposit should return the same or more shares as previewDeposit if called
     *   in the same transaction.
     * - MUST NOT account for deposit limits like those returned from maxDeposit and should always act as though the
     *   deposit would be accepted, regardless if the user has enough tokens approved, etc.
     * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToShares and previewDeposit SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by depositing.
     */
    function previewDeposit(uint256 assets) public view virtual returns (uint256) {
        return convertToShares(assets);
    }

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their mint at the current block, given
     * current on-chain conditions.
     *
     * - MUST return as close to and no fewer than the exact amount of assets that would be deposited in a mint call
     *   in the same transaction. I.e. mint should return the same or fewer assets as previewMint if called in the
     *   same transaction.
     * - MUST NOT account for mint limits like those returned from maxMint and should always act as though the mint
     *   would be accepted, regardless if the user has enough tokens approved, etc.
     * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToAssets and previewMint SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by minting.
     */
    function previewMint(uint256 shares) public view virtual returns (uint256) {
        uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

        return supply == 0 ? shares : shares.mulDivUp(totalAssets(), supply);
    }

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal at the current block,
     * given current on-chain conditions.
     *
     * - MUST return as close to and no fewer than the exact amount of Vault shares that would be burned in a withdraw
     *   call in the same transaction. I.e. withdraw should return the same or fewer shares as previewWithdraw if
     *   called
     *   in the same transaction.
     * - MUST NOT account for withdrawal limits like those returned from maxWithdraw and should always act as though
     *   the withdrawal would be accepted, regardless if the user has enough shares, etc.
     * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToShares and previewWithdraw SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by depositing.
     */
    function previewWithdraw(uint256 assets) public view virtual returns (uint256) {
        uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

        return supply == 0 ? assets : assets.mulDivUp(supply, totalAssets());
    }

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their redeemption at the current block,
     * given current on-chain conditions.
     *
     * - MUST return as close to and no more than the exact amount of assets that would be withdrawn in a redeem call
     *   in the same transaction. I.e. redeem should return the same or more assets as previewRedeem if called in the
     *   same transaction.
     * - MUST NOT account for redemption limits like those returned from maxRedeem and should always act as though the
     *   redemption would be accepted, regardless if the user has enough shares, etc.
     * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToAssets and previewRedeem SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by redeeming.
     */
    function previewRedeem(uint256 shares) public view virtual returns (uint256) {
        return convertToAssets(shares);
    }

    /*//////////////////////////////////////////////////////////////
                     DEPOSIT/WITHDRAWAL LIMIT LOGIC
    //////////////////////////////////////////////////////////////*/

    /**
     * @dev Returns the maximum amount of the underlying asset that can be deposited into the Vault for the receiver,
     * through a deposit call.
     *
     * - MUST return a limited value if receiver is subject to some deposit limit.
     * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of assets that may be deposited.
     * - MUST NOT revert.
     */
    function maxDeposit(address) public view virtual returns (uint256) {
        return type(uint256).max;
    }

    /**
     * @dev Returns the maximum amount of the Vault shares that can be minted for the receiver, through a mint call.
     * - MUST return a limited value if receiver is subject to some mint limit.
     * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of shares that may be minted.
     * - MUST NOT revert.
     */
    function maxMint(address) public view virtual returns (uint256) {
        return type(uint256).max;
    }

    /**
     * @dev Returns the maximum amount of the underlying asset that can be withdrawn from the owner balance in the
     * Vault, through a withdraw call.
     *
     * - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
     * - MUST NOT revert.
     */
    function maxWithdraw(address owner) public view virtual returns (uint256) {
        return convertToAssets(balanceOf(owner));
    }

    /**
     * @dev Returns the maximum amount of Vault shares that can be redeemed from the owner balance in the Vault,
     * through a redeem call.
     *
     * - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
     * - MUST return balanceOf(owner) if owner is not subject to any withdrawal limit or timelock.
     * - MUST NOT revert.
     */
    function maxRedeem(address owner) public view virtual returns (uint256) {
        return balanceOf(owner);
    }

    /*//////////////////////////////////////////////////////////////
                          INTERNAL HOOKS LOGIC
    //////////////////////////////////////////////////////////////*/

    /* solhint-disable no-empty-blocks */
    function beforeWithdraw(uint256 assets, uint256 shares, address receiver) internal virtual {}

    /* solhint-disable no-empty-blocks */
    function afterDeposit(uint256 assets, uint256 shares, address receiver) internal virtual {}
}

File 63 of 83 : IBalancerVault.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IBalancerVault {
    event FlashLoan(address indexed recipient, address indexed token, uint256 amount, uint256 feeAmount);

    /**
     * @dev Performs a 'flash loan', sending tokens to `recipient`, executing the `receiveFlashLoan` hook on it,
     * and then reverting unless the tokens plus a proportional protocol fee have been returned.
     *
     * The `tokens` and `amounts` arrays must have the same length, and each entry in these indicates the loan amount
     * for each token contract. `tokens` must be sorted in ascending order.
     *
     * The 'userData' field is ignored by the Vault, and forwarded as-is to `recipient` as part of the
     * `receiveFlashLoan` call.
     *
     * Emits `FlashLoan` events.
     */
    function flashLoan(
        address recipient,
        address[] memory tokens,
        uint256[] memory amounts,
        bytes memory userData
    ) external;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IGlpManager {
    function glp() external view returns (address);

    function gov() external view returns (address);

    function cooldownDuration() external returns (uint256);

    function lastAddedAt(address _account) external returns (uint256);

    function setCooldownDuration(uint256 _cooldownDuration) external;

    function addLiquidity(
        address _token,
        uint256 _amount,
        uint256 _minUsdg,
        uint256 _minGlp
    ) external returns (uint256);

    function addLiquidityForAccount(
        address _fundingAccount,
        address _account,
        address _token,
        uint256 _amount,
        uint256 _minUsdg,
        uint256 _minGlp
    ) external returns (uint256);

    function removeLiquidity(
        address _tokenOut,
        uint256 _glpAmount,
        uint256 _minOut,
        address _receiver
    ) external returns (uint256);

    function removeLiquidityForAccount(
        address _account,
        address _tokenOut,
        uint256 _glpAmount,
        uint256 _minOut,
        address _receiver
    ) external returns (uint256);

    function getAums() external view returns (uint256[] memory);

    function vault() external view returns (address);

    function getAumInUsdg(bool maximise) external view returns (uint256);

    function getAum(bool maximise) external view returns (uint256);

    function getGlobalShortAveragePrice(address token) external view returns (uint256);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IRewardRouterV2 {
    event StakeGmx(address account, address token, uint256 amount);
    event UnstakeGmx(address account, address token, uint256 amount);

    event StakeGlp(address account, uint256 amount);
    event UnstakeGlp(address account, uint256 amount);

    function stakedGmxTracker() external view returns (address);

    function gmx() external view returns (address);

    function esGmx() external view returns (address);

    function glpVester() external view returns (address);

    function glpManager() external view returns (address);

    function batchStakeGmxForAccount(address[] memory _accounts, uint256[] memory _amounts) external;

    function stakeGmxForAccount(address _account, uint256 _amount) external;

    function stakeGmx(uint256 _amount) external;

    function stakeEsGmx(uint256 _amount) external;

    function unstakeGmx(uint256 _amount) external;

    function unstakeEsGmx(uint256 _amount) external;

    function mintAndStakeGlp(
        address _token,
        uint256 _amount,
        uint256 _minUsdg,
        uint256 _minGlp
    ) external returns (uint256);

    function mintAndStakeGlpETH(uint256 _minUsdg, uint256 _minGlp) external payable returns (uint256);

    function unstakeAndRedeemGlp(
        address _tokenOut,
        uint256 _glpAmount,
        uint256 _minOut,
        address _receiver
    ) external returns (uint256);

    function unstakeAndRedeemGlpETH(
        uint256 _glpAmount,
        uint256 _minOut,
        address payable _receiver
    ) external returns (uint256);

    function claim() external;

    function claimEsGmx() external;

    function claimFees() external;

    function compound() external;

    function compoundForAccount(address _account) external;

    function handleRewards(
        bool shouldClaimGmx,
        bool shouldStakeGmx,
        bool shouldClaimEsGmx,
        bool shouldStakeEsGmx,
        bool shouldStakeMultiplierPoints,
        bool shouldClaimWeth,
        bool shouldConvertWethToEth
    ) external;

    function batchCompoundForAccounts(address[] memory _accounts) external;

    function signalTransfer(address _receiver) external;

    function acceptTransfer(address _sender) external;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IRewardTracker {
    function depositBalances(address _account, address _depositToken) external view returns (uint256);

    function stakedAmounts(address _account) external view returns (uint256);

    function updateRewards() external;

    function stake(address _depositToken, uint256 _amount) external;

    function stakeForAccount(
        address _fundingAccount,
        address _account,
        address _depositToken,
        uint256 _amount
    ) external;

    function unstake(address _depositToken, uint256 _amount) external;

    function unstakeForAccount(address _account, address _depositToken, uint256 _amount, address _receiver) external;

    function tokensPerInterval() external view returns (uint256);

    function claim(address _receiver) external returns (uint256);

    function claimForAccount(address _account, address _receiver) external returns (uint256);

    function claimable(address _account) external view returns (uint256);

    function averageStakedAmounts(address _account) external view returns (uint256);

    function cumulativeRewards(address _account) external view returns (uint256);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { IERC20 } from '@openzeppelin/contracts/interfaces/IERC20.sol';

interface ISglpExtended is IERC20 {
    function glp() external view returns (address);

    function glpManager() external view returns (address);

    function feeGlpTracker() external view returns (address);

    function stakedGlpTracker() external view returns (address);
}

File 68 of 83 : IVault.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IVault {
    function isInitialized() external view returns (bool);

    function isSwapEnabled() external view returns (bool);

    function isLeverageEnabled() external view returns (bool);

    function setError(uint256 _errorCode, string calldata _error) external;

    function router() external view returns (address);

    function usdg() external view returns (address);

    function gov() external view returns (address);

    function whitelistedTokenCount() external view returns (uint256);

    function maxLeverage() external view returns (uint256);

    function minProfitTime() external view returns (uint256);

    function hasDynamicFees() external view returns (bool);

    function fundingInterval() external view returns (uint256);

    function totalTokenWeights() external view returns (uint256);

    function inManagerMode() external view returns (bool);

    function inPrivateLiquidationMode() external view returns (bool);

    function maxGasPrice() external view returns (uint256);

    function approvedRouters(address _account, address _router) external view returns (bool);

    function isLiquidator(address _account) external view returns (bool);

    function isManager(address _account) external view returns (bool);

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

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

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

    function setInManagerMode(bool _inManagerMode) external;

    function setManager(address _manager, bool _isManager) external;

    function setIsSwapEnabled(bool _isSwapEnabled) external;

    function setIsLeverageEnabled(bool _isLeverageEnabled) external;

    function setMaxGasPrice(uint256 _maxGasPrice) external;

    function setFees(
        uint256 _taxBasisPoints,
        uint256 _stableTaxBasisPoints,
        uint256 _mintBurnFeeBasisPoints,
        uint256 _swapFeeBasisPoints,
        uint256 _stableSwapFeeBasisPoints,
        uint256 _marginFeeBasisPoints,
        uint256 _liquidationFeeUsd,
        uint256 _minProfitTime,
        bool _hasDynamicFees
    ) external;

    function setTokenConfig(
        address _token,
        uint256 _tokenDecimals,
        uint256 _redemptionBps,
        uint256 _minProfitBps,
        uint256 _maxUsdgAmount,
        bool _isStable,
        bool _isShortable
    ) external;

    function setPriceFeed(address _priceFeed) external;

    function withdrawFees(address _token, address _receiver) external returns (uint256);

    function directPoolDeposit(address _token) external;

    function buyUSDG(address _token, address _receiver) external returns (uint256);

    function sellUSDG(address _token, address _receiver) external returns (uint256);

    function swap(address _tokenIn, address _tokenOut, address _receiver) external returns (uint256);

    function increasePosition(
        address _account,
        address _collateralToken,
        address _indexToken,
        uint256 _sizeDelta,
        bool _isLong
    ) external;

    function decreasePosition(
        address _account,
        address _collateralToken,
        address _indexToken,
        uint256 _collateralDelta,
        uint256 _sizeDelta,
        bool _isLong,
        address _receiver
    ) external returns (uint256);

    function tokenToUsdMin(address _token, uint256 _tokenAmount) external view returns (uint256);

    function priceFeed() external view returns (address);

    function fundingRateFactor() external view returns (uint256);

    function stableFundingRateFactor() external view returns (uint256);

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

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

    function getFeeBasisPoints(
        address _token,
        uint256 _usdgDelta,
        uint256 _feeBasisPoints,
        uint256 _taxBasisPoints,
        bool _increment
    ) external view returns (uint256);

    function liquidationFeeUsd() external view returns (uint256);

    function taxBasisPoints() external view returns (uint256);

    function stableTaxBasisPoints() external view returns (uint256);

    function mintBurnFeeBasisPoints() external view returns (uint256);

    function swapFeeBasisPoints() external view returns (uint256);

    function stableSwapFeeBasisPoints() external view returns (uint256);

    function marginFeeBasisPoints() external view returns (uint256);

    function allWhitelistedTokensLength() external view returns (uint256);

    function allWhitelistedTokens(uint256) external view returns (address);

    function whitelistedTokens(address _token) external view returns (bool);

    function stableTokens(address _token) external view returns (bool);

    function shortableTokens(address _token) external view returns (bool);

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

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

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

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

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

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

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

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

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

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

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

    function getRedemptionAmount(address _token, uint256 _usdgAmount) external view returns (uint256);

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

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

    function getDelta(
        address _indexToken,
        uint256 _size,
        uint256 _averagePrice,
        bool _isLong,
        uint256 _lastIncreasedTime
    ) external view returns (bool, uint256);

    function getPosition(
        address _account,
        address _collateralToken,
        address _indexToken,
        bool _isLong
    ) external view returns (uint256, uint256, uint256, uint256, uint256, uint256, bool, uint256);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IVester {
    function rewardTracker() external view returns (address);

    function claimForAccount(address _account, address _receiver) external returns (uint256);

    function claimable(address _account) external view returns (uint256);

    function cumulativeClaimAmounts(address _account) external view returns (uint256);

    function claimedAmounts(address _account) external view returns (uint256);

    function pairAmounts(address _account) external view returns (uint256);

    function getVestedAmount(address _account) external view returns (uint256);

    function transferredAverageStakedAmounts(address _account) external view returns (uint256);

    function transferredCumulativeRewards(address _account) external view returns (uint256);

    function cumulativeRewardDeductions(address _account) external view returns (uint256);

    function bonusRewards(address _account) external view returns (uint256);

    function transferStakeValues(address _sender, address _receiver) external;

    function setTransferredAverageStakedAmounts(address _account, uint256 _amount) external;

    function setTransferredCumulativeRewards(address _account, uint256 _amount) external;

    function setCumulativeRewardDeductions(address _account, uint256 _amount) external;

    function setBonusRewards(address _account, uint256 _amount) external;

    function getMaxVestableAmount(address _account) external view returns (uint256);

    function getCombinedAverageStakedAmount(address _account) external view returns (uint256);

    function deposit(uint256 _amount) external;

    function withdraw() external;

    function claim() external returns (uint256);

    function balances(address account) external view returns (uint256);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

interface IBorrower {
    function harvestFees() external;

    function getUsdcBorrowed() external view returns (uint256);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import { IVariableDebtToken } from '@aave/core-v3/contracts/interfaces/IVariableDebtToken.sol';

interface IDebtToken is IVariableDebtToken {
    function totalSupply() external view returns (uint256);

    function balanceOf(address user) external view returns (uint256);
}

// SPDX-License-Identifier: MIT

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

pragma solidity ^0.8.0;

interface IDnGmxBatchingManager {
    error NoUsdcBalance();

    error CallerNotVault();
    error CallerNotKeeper();

    error InvalidInput(uint256 errorCode);
    error InsufficientShares(uint256 balance);

    error DepositCapBreached();
    error TargetAssetCapBreached(uint256 totalAssetsDeposited, uint256 depositAmount, uint256 targetAssetCap);

    event DepositToken(
        uint256 indexed round,
        address indexed token,
        address indexed receiver,
        uint256 amount,
        uint256 glpStaked
    );

    event ParamsV1Updated(address indexed rewardsHarvestingRouter, address weth);
    event KeeperUpdated(address newKeeper);
    event ThresholdsUpdated(uint256 newSlippageThresholdGmx, uint256 minUsdcConversionAmount);

    event BatchStake(uint256 indexed round, uint256 userUsdcAmount, uint256 userGlpAmount);
    event SharesClaimed(address indexed from, address indexed receiver, uint256 claimAmount);
    event BatchDeposit(uint256 indexed round, uint256 userUsdcAmount, uint256 userGlpAmount, uint256 userShareAmount);

    event ClaimedAndRedeemed(address indexed claimer, address indexed receiver, uint256 shares, uint256 assetsReceived);
    event DepositCapUpdated(uint256 newDepositCap);
    event TargetAssetCapUpdated(uint256 newTargeAssetCap);
    event PartialBatchDeposit(uint256 indexed round, uint256 partialGlpAmount, uint256 partialShareAmount);

    struct UserDeposit {
        uint256 round;
        uint128 usdcBalance;
        uint128 unclaimedShares;
    }
    struct RoundDeposit {
        uint128 totalUsdc;
        uint128 totalShares;
    }

    function executeBatch(uint128 usdcAmountToConvert) external;

    function currentRound() external view returns (uint256);

    function claim(address receiver, uint256 amount) external;

    function roundUsdcBalance() external view returns (uint256);

    function roundGlpStaked() external view returns (uint256);

    function usdcBalance(address account) external view returns (uint256 balance);

    function dnGmxJuniorVaultGlpBalance() external view returns (uint256 balance);

    function unclaimedShares(address account) external view returns (uint256 shares);

    function roundDeposits(uint256 round) external view returns (RoundDeposit memory);

    function depositUsdc(uint256 amount, address receiver) external;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import { IRewardsController } from '@aave/periphery-v3/contracts/rewards/interfaces/IRewardsController.sol';
import { IPoolAddressesProvider } from '@aave/core-v3/contracts/interfaces/IPoolAddressesProvider.sol';
import { IPool } from '@aave/core-v3/contracts/interfaces/IPool.sol';
import { IPriceOracle } from '@aave/core-v3/contracts/interfaces/IPriceOracle.sol';
import { ISwapRouter } from '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';
import { IERC4626 } from './IERC4626.sol';
import { IBorrower } from './IBorrower.sol';
import { IBalancerVault } from './balancer/IBalancerVault.sol';
import { IDnGmxTraderHedgeStrategy } from './IDnGmxTraderHedgeStrategy.sol';

interface IDnGmxJuniorVault is IERC4626, IBorrower {
    error InvalidWithdrawFeeBps();
    error InvalidSlippageThresholdSwapBtc();
    error InvalidSlippageThresholdSwapEth();
    error InvalidSlippageThresholdGmx();
    error InvalidRebalanceTimeThreshold();
    error InvalidRebalanceDeltaThresholdBps();
    error InvalidRebalanceHfThresholdBps();
    error InvalidTargetHealthFactor();

    error InvalidRebalance();
    error DepositCapExceeded();
    error OnlyKeeperAllowed(address msgSender, address authorisedKeeperAddress);
    error OnlyTraderHedgeStrategyAllowed(address msgSender, address authorisedKeeperAddress);
    error NotDnGmxSeniorVault();
    error NotBalancerVault();

    error ArraysLengthMismatch();
    error FlashloanNotInitiated();
    error TooMuchSlippage(uint netSlippage, uint assets);

    error InvalidFeeRecipient();
    error InvalidFeeBps();

    error InvalidTraderOIHedges(int128 btcTraderOIHedge, int128 ethTraderOIHedge);

    event Rebalanced();
    event AllowancesGranted();

    event DnGmxSeniorVaultUpdated(address _dnGmxSeniorVault);
    event KeeperUpdated(address _newKeeper);
    event FeeParamsUpdated(uint256 feeBps, address _newFeeRecipient);
    event WithdrawFeeUpdated(uint256 _withdrawFeeBps);
    event FeesWithdrawn(uint256 feeAmount);

    event DepositCapUpdated(uint256 _newDepositCap);

    event AdminParamsUpdated(
        address newKeeper,
        address dnGmxSeniorVault,
        uint256 newDepositCap,
        address batchingManager,
        uint16 withdrawFeeBps
    );
    event ThresholdsUpdated(
        uint16 slippageThresholdSwapBtcBps,
        uint16 slippageThresholdSwapEthBps,
        uint16 slippageThresholdGmxBps,
        uint128 usdcConversionThreshold,
        uint128 wethConversionThreshold,
        uint128 hedgeUsdcAmountThreshold,
        uint128 partialBtcHedgeUsdcAmountThreshold,
        uint128 partialEthHedgeUsdcAmountThreshold
    );

    event ParamsV1Updated(
        uint128 rebalanceProfitUsdcAmountThreshold,
        IDnGmxTraderHedgeStrategy dnGmxTraderHedgeStrategy
    );
    event RebalanceParamsUpdated(
        uint32 rebalanceTimeThreshold,
        uint16 rebalanceDeltaThresholdBps,
        uint16 rebalanceHfThresholdBps
    );

    event HedgeParamsUpdated(
        IBalancerVault vault,
        ISwapRouter swapRouter,
        uint256 targetHealthFactor,
        IRewardsController aaveRewardsController,
        IPool pool,
        IPriceOracle oracle
    );

    event EsGmxVested(uint256 amount);

    event EsGmxStaked(uint256 amount);

    event GmxClaimed(uint256 amount);

    event TraderOIHedgesUpdated(int256 btcTraderOIHedge, int256 ethTraderOIHedge);

    event AssetSlippage(address indexed user, uint slippage);

    function harvestFees() external;

    function depositCap() external view returns (uint256);

    function getPriceX128() external view returns (uint256);

    function getVaultMarketValue() external view returns (int256);

    function getMarketValue(uint256 assetAmount) external view returns (uint256 marketValue);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { IBorrower } from './IBorrower.sol';
import { IERC4626 } from './IERC4626.sol';

interface IDnGmxSeniorVault is IERC4626 {
    error InvalidMaxUtilizationBps();

    error CallerNotBorrower();

    error InvalidCapUpdate();
    error InvalidBorrowAmount();
    error InvalidBorrowerAddress();

    error DepositCapExceeded();
    error MaxUtilizationBreached();

    event AllowancesGranted();
    event DepositCapUpdated(uint256 _newDepositCap);
    event BorrowCapUpdated(address vault, uint256 newCap);

    event LeveragePoolUpdated(IBorrower leveragePool);
    event DnGmxJuniorVaultUpdated(IBorrower dnGmxJuniorVault);
    event MaxUtilizationBpsUpdated(uint256 maxUtilizationBps);

    event FeeStrategyUpdated(
        uint128 optimalUtilizationRate,
        uint128 baseVariableBorrowRate,
        uint128 variableRateSlope1,
        uint128 variableRateSlope2
    );

    // eventType - 0 = start of txn | 1 = end of txn
    event VaultState(uint256 indexed eventType, uint256 juniorVaultAusdc, uint256 seniorVaultAusdc);

    function borrow(uint256 amount) external;

    function repay(uint256 amount) external;

    function depositCap() external view returns (uint256);

    function getPriceX128() external view returns (uint256);

    function getEthRewardsSplitRate() external returns (uint256);

    function getVaultMarketValue() external view returns (uint256);

    function availableBorrow(address borrower) external view returns (uint256);
}

// SPDX-License-Identifier: MIT

import { IERC20 } from '@openzeppelin/contracts/token/ERC20/IERC20.sol';

pragma solidity ^0.8.9;

interface IDnGmxTraderHedgeStrategy {
    function overrideTraderOIHedges(int128 btcTraderOIHedge, int128 ethTraderOIHedge) external;

    function setTraderOIHedgeBps(uint16 _traderOIHedgeBps) external;

    function setTraderOIHedges() external;

    function btcTraderOIHedge() external view returns (int128);

    function ethTraderOIHedge() external view returns (int128);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { IERC20Upgradeable } from '@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol';
import { IERC20Metadata } from '@openzeppelin/contracts/interfaces/IERC20Metadata.sol';

interface IERC4626 is IERC20Upgradeable {
    event Deposit(address indexed caller, address indexed owner, uint256 assets, uint256 shares);

    event Withdraw(
        address indexed caller,
        address indexed receiver,
        address indexed owner,
        uint256 assets,
        uint256 shares
    );

    /**
     * @dev Returns the address of the underlying token used for the Vault for accounting, depositing, and withdrawing.
     *
     * - MUST be an ERC-20 token contract.
     * - MUST NOT revert.
     */
    function asset() external view returns (address assetTokenAddress);

    /**
     * @dev Returns the total amount of the underlying asset that is “managed” by Vault.
     *
     * - SHOULD include any compounding that occurs from yield.
     * - MUST be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT revert.
     */
    function totalAssets() external view returns (uint256 totalManagedAssets);

    /**
     * @dev Returns the amount of shares that the Vault would exchange for the amount of assets provided, in an ideal
     * scenario where all the conditions are met.
     *
     * - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT show any variations depending on the caller.
     * - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
     * - MUST NOT revert.
     *
     * NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
     * “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
     * from.
     */
    function convertToShares(uint256 assets) external view returns (uint256 shares);

    /**
     * @dev Returns the amount of assets that the Vault would exchange for the amount of shares provided, in an ideal
     * scenario where all the conditions are met.
     *
     * - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
     * - MUST NOT show any variations depending on the caller.
     * - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
     * - MUST NOT revert.
     *
     * NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
     * “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
     * from.
     */
    function convertToAssets(uint256 shares) external view returns (uint256 assets);

    /**
     * @dev Returns the maximum amount of the underlying asset that can be deposited into the Vault for the receiver,
     * through a deposit call.
     *
     * - MUST return a limited value if receiver is subject to some deposit limit.
     * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of assets that may be deposited.
     * - MUST NOT revert.
     */
    function maxDeposit(address receiver) external view returns (uint256 maxAssets);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their deposit at the current block, given
     * current on-chain conditions.
     *
     * - MUST return as close to and no more than the exact amount of Vault shares that would be minted in a deposit
     *   call in the same transaction. I.e. deposit should return the same or more shares as previewDeposit if called
     *   in the same transaction.
     * - MUST NOT account for deposit limits like those returned from maxDeposit and should always act as though the
     *   deposit would be accepted, regardless if the user has enough tokens approved, etc.
     * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToShares and previewDeposit SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by depositing.
     */
    function previewDeposit(uint256 assets) external view returns (uint256 shares);

    /**
     * @dev Mints shares Vault shares to receiver by depositing exactly amount of underlying tokens.
     *
     * - MUST emit the Deposit event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   deposit execution, and are accounted for during deposit.
     * - MUST revert if all of assets cannot be deposited (due to deposit limit being reached, slippage, the user not
     *   approving enough underlying tokens to the Vault contract, etc).
     *
     * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
     */
    function deposit(uint256 assets, address receiver) external returns (uint256 shares);

    /**
     * @dev Returns the maximum amount of the Vault shares that can be minted for the receiver, through a mint call.
     * - MUST return a limited value if receiver is subject to some mint limit.
     * - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of shares that may be minted.
     * - MUST NOT revert.
     */
    function maxMint(address receiver) external view returns (uint256 maxShares);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their mint at the current block, given
     * current on-chain conditions.
     *
     * - MUST return as close to and no fewer than the exact amount of assets that would be deposited in a mint call
     *   in the same transaction. I.e. mint should return the same or fewer assets as previewMint if called in the
     *   same transaction.
     * - MUST NOT account for mint limits like those returned from maxMint and should always act as though the mint
     *   would be accepted, regardless if the user has enough tokens approved, etc.
     * - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToAssets and previewMint SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by minting.
     */
    function previewMint(uint256 shares) external view returns (uint256 assets);

    /**
     * @dev Mints exactly shares Vault shares to receiver by depositing amount of underlying tokens.
     *
     * - MUST emit the Deposit event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the mint
     *   execution, and are accounted for during mint.
     * - MUST revert if all of shares cannot be minted (due to deposit limit being reached, slippage, the user not
     *   approving enough underlying tokens to the Vault contract, etc).
     *
     * NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
     */
    function mint(uint256 shares, address receiver) external returns (uint256 assets);

    /**
     * @dev Returns the maximum amount of the underlying asset that can be withdrawn from the owner balance in the
     * Vault, through a withdraw call.
     *
     * - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
     * - MUST NOT revert.
     */
    function maxWithdraw(address owner) external view returns (uint256 maxAssets);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal at the current block,
     * given current on-chain conditions.
     *
     * - MUST return as close to and no fewer than the exact amount of Vault shares that would be burned in a withdraw
     *   call in the same transaction. I.e. withdraw should return the same or fewer shares as previewWithdraw if
     *   called
     *   in the same transaction.
     * - MUST NOT account for withdrawal limits like those returned from maxWithdraw and should always act as though
     *   the withdrawal would be accepted, regardless if the user has enough shares, etc.
     * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToShares and previewWithdraw SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by depositing.
     */
    function previewWithdraw(uint256 assets) external view returns (uint256 shares);

    /**
     * @dev Burns shares from owner and sends exactly assets of underlying tokens to receiver.
     *
     * - MUST emit the Withdraw event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   withdraw execution, and are accounted for during withdraw.
     * - MUST revert if all of assets cannot be withdrawn (due to withdrawal limit being reached, slippage, the owner
     *   not having enough shares, etc).
     *
     * Note that some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
     * Those methods should be performed separately.
     */
    function withdraw(uint256 assets, address receiver, address owner) external returns (uint256 shares);

    /**
     * @dev Returns the maximum amount of Vault shares that can be redeemed from the owner balance in the Vault,
     * through a redeem call.
     *
     * - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
     * - MUST return balanceOf(owner) if owner is not subject to any withdrawal limit or timelock.
     * - MUST NOT revert.
     */
    function maxRedeem(address owner) external view returns (uint256 maxShares);

    /**
     * @dev Allows an on-chain or off-chain user to simulate the effects of their redeemption at the current block,
     * given current on-chain conditions.
     *
     * - MUST return as close to and no more than the exact amount of assets that would be withdrawn in a redeem call
     *   in the same transaction. I.e. redeem should return the same or more assets as previewRedeem if called in the
     *   same transaction.
     * - MUST NOT account for redemption limits like those returned from maxRedeem and should always act as though the
     *   redemption would be accepted, regardless if the user has enough shares, etc.
     * - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
     * - MUST NOT revert.
     *
     * NOTE: any unfavorable discrepancy between convertToAssets and previewRedeem SHOULD be considered slippage in
     * share price or some other type of condition, meaning the depositor will lose assets by redeeming.
     */
    function previewRedeem(uint256 shares) external view returns (uint256 assets);

    /**
     * @dev Burns exactly shares from owner and sends assets of underlying tokens to receiver.
     *
     * - MUST emit the Withdraw event.
     * - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
     *   redeem execution, and are accounted for during redeem.
     * - MUST revert if all of shares cannot be redeemed (due to withdrawal limit being reached, slippage, the owner
     *   not having enough shares, etc).
     *
     * NOTE: some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
     * Those methods should be performed separately.
     */
    function redeem(uint256 shares, address receiver, address owner) external returns (uint256 assets);
}

File 77 of 83 : DnGmxJuniorVaultManager.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { IVault } from '../interfaces/gmx/IVault.sol';
import { IGlpManager } from '../interfaces/gmx/IGlpManager.sol';
import { IRewardTracker } from '../interfaces/gmx/IRewardTracker.sol';
import { IRewardRouterV2 } from '../interfaces/gmx/IRewardRouterV2.sol';

import { IDebtToken } from '../interfaces/IDebtToken.sol';
import { IPool } from '@aave/core-v3/contracts/interfaces/IPool.sol';
import { IAToken } from '@aave/core-v3/contracts/interfaces/IAToken.sol';
import { IPriceOracle } from '@aave/core-v3/contracts/interfaces/IPriceOracle.sol';
import { DataTypes } from '@aave/core-v3/contracts/protocol/libraries/types/DataTypes.sol';
import { IPoolAddressesProvider } from '@aave/core-v3/contracts/interfaces/IPoolAddressesProvider.sol';
import { IRewardsController } from '@aave/periphery-v3/contracts/rewards/interfaces/IRewardsController.sol';
import { ReserveConfiguration } from '@aave/core-v3/contracts/protocol/libraries/configuration/ReserveConfiguration.sol';

import { IBalancerVault } from '../interfaces/balancer/IBalancerVault.sol';

import { IERC20 } from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import { IERC20Metadata } from '@openzeppelin/contracts/interfaces/IERC20Metadata.sol';

import { IDnGmxJuniorVault } from '../interfaces/IDnGmxJuniorVault.sol';
import { IDnGmxSeniorVault } from '../interfaces/IDnGmxSeniorVault.sol';
import { IDnGmxBatchingManager } from '../interfaces/IDnGmxBatchingManager.sol';
import { IDnGmxTraderHedgeStrategy } from '../interfaces/IDnGmxTraderHedgeStrategy.sol';

import { SafeCast } from '../libraries/SafeCast.sol';
import { FeeSplitStrategy } from '../libraries/FeeSplitStrategy.sol';
import { SignedFixedPointMathLib } from '../libraries/SignedFixedPointMathLib.sol';
import { QuoterLib } from '../libraries/QuoterLib.sol';
import { SwapPath } from '../libraries/SwapPath.sol';

import { FixedPointMathLib } from '@rari-capital/solmate/src/utils/FixedPointMathLib.sol';

import { Simulate } from '@uniswap/v3-core/contracts/libraries/Simulate.sol';
import { ISwapRouter } from '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';

/**
 * @title Helper library for junior vault
 * @dev junior vault delegates calls to this library for logic
 * @author RageTrade
 */

library DnGmxJuniorVaultManager {
    event RewardsHarvested(
        uint256 wethHarvested,
        uint256 esGmxStaked,
        uint256 juniorVaultWeth,
        uint256 seniorVaultWeth,
        uint256 juniorVaultGlp,
        uint256 seniorVaultAUsdc
    );

    event ProtocolFeeAccrued(uint256 fees);

    event GlpSwapped(uint256 glpQuantity, uint256 usdcQuantity, bool fromGlpToUsdc);

    event TokenSwapped(address indexed fromToken, address indexed toToken, uint256 fromQuantity, uint256 toQuantity);

    event VaultState(
        uint256 indexed eventType,
        uint256 btcBorrows,
        uint256 ethBorrows,
        uint256 btcPoolAmount,
        uint256 ethPoolAmount,
        int256 btcTraderOIHedge,
        int256 ethTraderOIHedge,
        uint256 glpPrice,
        uint256 glpBalance,
        uint256 totalAssets,
        int256 dnUsdcDeposited,
        uint256 unhedgedGlpInUsdc,
        uint256 juniorVaultAusdc,
        uint256 seniorVaultAusdc
    );

    using DnGmxJuniorVaultManager for State;
    using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

    using FixedPointMathLib for uint256;
    using SafeCast for uint256;
    using SignedFixedPointMathLib for int256;

    uint256 internal constant MAX_BPS = 10_000;

    uint256 internal constant PRICE_PRECISION = 1e30;
    uint256 internal constant VARIABLE_INTEREST_MODE = 2;

    struct Tokens {
        IERC20Metadata weth;
        IERC20Metadata wbtc;
        IERC20Metadata sGlp;
        IERC20Metadata usdc;
    }

    // prettier-ignore
    struct State {
        // core protocol roles
        address keeper;
        address feeRecipient;

        // accounting
        // amount of usdc deposited by junior tranche into AAVE
        int256 dnUsdcDeposited;
        // amount of asset which is in usdc (due to borrow limits / availability issue some glp might remain unhedged)
        uint256 unhedgedGlpInUsdc;
        // health factor to be targetted on AAVE
        uint256 targetHealthFactor;

        // accumulators
        // protocol fee taken from ETH rewards
        uint256 protocolFee;
        // protocol fee taken from esGMX rewards
        uint256 protocolEsGmx;
        // senior tranche part of eth rewards which is not converted to usdc
        uint256 seniorVaultWethRewards;

        // locks
        // true if a flashloan has been initiated by the vault
        bool hasFlashloaned;
        // ensures that the rebalance can be run only after certain intervals
        uint48 lastRebalanceTS;

        // fees
        // protocol fees charged on the eth and esGmx rewards
        uint16 feeBps;
        // fees on the withdrawn assets
        uint16 withdrawFeeBps;
        // fee tier for uniswap path
        uint24 feeTierWethWbtcPool;

        // thresholds
        uint256 depositCap;

        // slippage threshold on asset conversion into glp
        uint16 slippageThresholdGmxBps; // bps
        // slippage threshold on btc swap on uniswap
        uint16 slippageThresholdSwapBtcBps; // bps
        // slippage threshold on eth swap on uniswap
        uint16 slippageThresholdSwapEthBps; // bps
        // health factor treshold below which rebalance can be called
        uint16 rebalanceHfThresholdBps; // bps
        // time threshold beyond which on top of last rebalance, rebalance can be called
        uint32 rebalanceTimeThreshold; // seconds between rebalance
        // difference between current and optimal amounts beyond which rebalance can be called
        uint16 rebalanceDeltaThresholdBps; // bps
        // eth amount of weth rewards accrued beyond which they can be compounded
        uint128 wethConversionThreshold; // eth amount

        // usdc amount beyond which usdc can be converted to assets
        uint128 usdcConversionThreshold; // usdc amount
        // usdc value of token hedges below which hedges are not taken
        uint128 hedgeUsdcAmountThreshold; // usdc amount

        // usdc amount of btc hedge beyond which partial hedges are taken over multiple rebalances
        uint128 partialBtcHedgeUsdcAmountThreshold; // usdc amount
        // usdc amount of eth hedge beyond which partial hedges are taken over multiple rebalances
        uint128 partialEthHedgeUsdcAmountThreshold; // usdc amount

        // token addrs
        IERC20 fsGlp;
        IERC20Metadata glp;
        IERC20Metadata usdc;
        IERC20Metadata weth;
        IERC20Metadata wbtc;

        // aave protocol addrs
        // lending pool for liqudity market
        IPool pool;
        // aave interest bearing usdc
        IAToken aUsdc;
        // variable-rate debt accruing btc
        IDebtToken vWbtc;
        // variable-rate debt accruing eth
        IDebtToken vWeth;
        // cannocial oracle used by aave
        IPriceOracle oracle;
        // rewards controller to claim any emissions (for future use)
        IRewardsController aaveRewardsController;
        // immutable address provider to obtain various addresses
        IPoolAddressesProvider poolAddressProvider;

        // gmx protocol addrs
        // core gmx vault
        IVault gmxVault;
        // staked gmx
        IRewardTracker sGmx;
        // glp manager (for giving assets allowance and fetching AUM)
        IGlpManager glpManager;
        // old rewardRouter for all actions except minting and burning glp
        IRewardRouterV2 rewardRouter;
        // new rewardRouter to be used for mintAndStakeGlp and unstakeAndRedeem
        // ref: https://medium.com/@gmx.io/gmx-deployment-updates-nov-2022-16572314874d
        IRewardRouterV2 mintBurnRewardRouter;

        // other external protocols
        // uniswap swap router for token swaps
        ISwapRouter swapRouter;
        // balancer vault for flashloans
        IBalancerVault balancerVault;

        // core protocol addrs
        // senior tranche address
        IDnGmxSeniorVault dnGmxSeniorVault;
        // batching manager address !!! Deprecated !!!
        IDnGmxBatchingManager batchingManager;

        // switch to select route
        bool useDirectConversionBurn;
        bool useDirectConversionMint;

        // !!! STORAGE EXTENSIONS !!! (reduced gaps by no. of slots added here)
        uint128 btcPoolAmount;
        uint128 ethPoolAmount;

        int128 btcTraderOIHedge;
        int128 ethTraderOIHedge;

        IDnGmxTraderHedgeStrategy dnGmxTraderHedgeStrategy;

        uint128 rebalanceProfitUsdcAmountThreshold;

        // gaps for extending struct (if required during upgrade)
        uint256[46] __gaps;
    }

    /// @notice stakes the rewards from the staked Glp and claims WETH to buy glp
    /// @notice also update protocolEsGmx fees which can be vested and claimed
    /// @notice divides the fees between senior and junior tranches based on senior tranche util
    /// @notice for junior tranche weth is deposited to batching manager which handles conversion to sGLP
    /// @notice for senior tranche weth is converted into usdc and deposited on AAVE which increases the borrowed amount
    function harvestFees(State storage state) public {
        uint256 sGmxHarvested;
        {
            address esGmx = state.rewardRouter.esGmx();
            IRewardTracker sGmx = IRewardTracker(state.rewardRouter.stakedGmxTracker());

            // existing staked gmx balance
            uint256 sGmxPrevBalance = sGmx.depositBalances(address(this), esGmx);

            // handles claiming and staking of esGMX, staking of multiplier points and claim of WETH rewards on GMX
            state.rewardRouter.handleRewards({
                shouldClaimGmx: false,
                shouldStakeGmx: false,
                shouldClaimEsGmx: true,
                shouldStakeEsGmx: true,
                shouldStakeMultiplierPoints: true,
                shouldClaimWeth: true,
                shouldConvertWethToEth: false
            });

            // harvested staked gmx
            sGmxHarvested = sGmx.depositBalances(address(this), esGmx) - sGmxPrevBalance;
        }
        // protocol esGMX fees
        state.protocolEsGmx += sGmxHarvested.mulDivDown(state.feeBps, MAX_BPS);

        // total weth harvested which is not compounded
        // its possible that this is accumulated value over multiple rebalance if in all of those it was below threshold
        uint256 wethHarvested = state.weth.balanceOf(address(this)) - state.protocolFee - state.seniorVaultWethRewards;

        if (wethHarvested > state.wethConversionThreshold) {
            // weth harvested > conversion threshold
            uint256 protocolFeeHarvested = (wethHarvested * state.feeBps) / MAX_BPS;
            // protocol fee incremented
            state.protocolFee += protocolFeeHarvested;
            emit ProtocolFeeAccrued(protocolFeeHarvested);

            // protocol fee to be kept in weth
            // remaining amount needs to be compounded
            uint256 wethToCompound = wethHarvested - protocolFeeHarvested;

            // share of the wethToCompound that belongs to senior tranche
            uint256 dnGmxSeniorVaultWethShare = state.dnGmxSeniorVault.getEthRewardsSplitRate().mulDivDown(
                wethToCompound,
                FeeSplitStrategy.RATE_PRECISION
            );
            // share of the wethToCompound that belongs to junior tranche
            uint256 dnGmxWethShare = wethToCompound - dnGmxSeniorVaultWethShare;

            // total senior tranche weth which is not compounded
            uint256 _seniorVaultWethRewards = state.seniorVaultWethRewards + dnGmxSeniorVaultWethShare;

            uint256 glpReceived;
            {
                // converts junior tranche share of weth into glp using batching manager
                // we need to use batching manager since there is a cooldown period on sGLP
                // if deposited directly for next 15mins withdrawals would fail
                uint256 price = state.gmxVault.getMinPrice(address(state.weth));

                uint256 usdgAmount = dnGmxWethShare.mulDivDown(
                    price * (MAX_BPS - state.slippageThresholdGmxBps),
                    PRICE_PRECISION * MAX_BPS
                );

                // deposits weth into batching manager which handles the conversion into glp
                // can be taken back through batch execution
                glpReceived = _stakeGlp(state, address(state.weth), dnGmxWethShare, usdgAmount);
            }

            if (_seniorVaultWethRewards > state.wethConversionThreshold) {
                // converts senior tranche share of weth into usdc and deposit into AAVE
                // Deposit aave vault share to AAVE in usdc
                uint256 minUsdcAmount = _getTokenPriceInUsdc(state, state.weth).mulDivDown(
                    _seniorVaultWethRewards * (MAX_BPS - state.slippageThresholdSwapEthBps),
                    MAX_BPS * PRICE_PRECISION
                );
                // swaps weth into usdc
                (uint256 aaveUsdcAmount, ) = state._swapToken(
                    address(state.weth),
                    _seniorVaultWethRewards,
                    minUsdcAmount
                );

                // supplies usdc into AAVE
                state._executeSupply(address(state.usdc), aaveUsdcAmount);

                // resets senior tranche rewards
                state.seniorVaultWethRewards = 0;

                emit RewardsHarvested(
                    wethHarvested,
                    sGmxHarvested,
                    dnGmxWethShare,
                    dnGmxSeniorVaultWethShare,
                    glpReceived,
                    aaveUsdcAmount
                );
            } else {
                state.seniorVaultWethRewards = _seniorVaultWethRewards;
                emit RewardsHarvested(
                    wethHarvested,
                    sGmxHarvested,
                    dnGmxWethShare,
                    dnGmxSeniorVaultWethShare,
                    glpReceived,
                    0
                );
            }
        } else {
            emit RewardsHarvested(wethHarvested, sGmxHarvested, 0, 0, 0, 0);
        }
    }

    /* ##################################################################
                            REBALANCE HELPERS
    ################################################################## */

    ///@notice rebalances pnl on AAVE against the sGLP assets
    ///@param state set of all state variables of vault
    ///@param borrowValue value of the borrowed assets(ETH + BTC) from AAVE in USDC
    function rebalanceProfit(State storage state, uint256 borrowValue) external {
        return _rebalanceProfit(state, borrowValue);
    }

    ///@notice rebalances pnl on AAVE against the sGLP assets
    ///@dev converts assets into usdc and deposits to AAVE if profit on GMX and loss on AAVE
    ///@dev withdraws usdc from aave and converts to GLP if loss on GMX and profits on AAVE
    ///@param state set of all state variables of vault
    ///@param borrowValue value of the borrowed assets(ETH + BTC) from AAVE in USDC
    function _rebalanceProfit(State storage state, uint256 borrowValue) private {
        int256 borrowVal = borrowValue.toInt256();

        if (borrowVal > state.dnUsdcDeposited) {
            uint256 diff = uint256(borrowVal - state.dnUsdcDeposited);
            if (diff < state.rebalanceProfitUsdcAmountThreshold && !_isValidRebalanceHF(state)) return;
            // If glp goes up - there is profit on GMX and loss on AAVE
            // So convert some glp to usdc and deposit to AAVE
            state.dnUsdcDeposited += _convertAssetToAUsdc(state, diff).toInt256();
        } else if (borrowVal < state.dnUsdcDeposited) {
            uint256 diff = uint256(state.dnUsdcDeposited - borrowVal);
            if (diff < state.rebalanceProfitUsdcAmountThreshold) return;
            // If glp goes down - there is profit on AAVE and loss on GMX
            // So withdraw some aave usdc and convert to glp
            _convertAUsdcToAsset(state, diff);
            state.dnUsdcDeposited = borrowVal;
        }
    }

    ///@notice rebalances the assets borrowed from AAVE to hedge ETH and BTC underlying the assets
    ///@param state set of all state variables of vault
    ///@param optimalBtcBorrow optimal btc amount to hedge sGLP btc underlying completely
    ///@param currentBtcBorrow current btc amount borrowed from AAVE
    ///@param optimalEthBorrow optimal eth amount to hedge sGLP btc underlying completely
    ///@param currentEthBorrow current eth amount borrowed from AAVE
    function rebalanceBorrow(
        State storage state,
        uint256 optimalBtcBorrow,
        uint256 currentBtcBorrow,
        uint256 optimalEthBorrow,
        uint256 currentEthBorrow
    ) external {
        return _rebalanceBorrow(state, optimalBtcBorrow, currentBtcBorrow, optimalEthBorrow, currentEthBorrow);
    }

    ///@notice rebalances the assets borrowed from AAVE to hedge ETH and BTC underlying the assets
    ///@param state set of all state variables of vault
    ///@param optimalBtcBorrow optimal btc amount to hedge sGLP btc underlying completely
    ///@param currentBtcBorrow current btc amount borrowed from AAVE
    ///@param optimalEthBorrow optimal eth amount to hedge sGLP btc underlying completely
    ///@param currentEthBorrow current eth amount borrowed from AAVE
    function _rebalanceBorrow(
        State storage state,
        uint256 optimalBtcBorrow,
        uint256 currentBtcBorrow,
        uint256 optimalEthBorrow,
        uint256 currentEthBorrow
    ) private {
        address[] memory assets;
        uint256[] memory amounts;

        // calculate the token/usdc amount to be flashloaned from balancer
        (uint256 btcTokenAmount, uint256 btcUsdcAmount, bool repayDebtBtc) = _flashloanAmounts(
            state,
            address(state.wbtc),
            optimalBtcBorrow,
            currentBtcBorrow
        );
        (uint256 ethTokenAmount, uint256 ethUsdcAmount, bool repayDebtEth) = _flashloanAmounts(
            state,
            address(state.weth),
            optimalEthBorrow,
            currentEthBorrow
        );

        // no swap needs to happen if the amount to hedge < threshold
        if (btcUsdcAmount < state.hedgeUsdcAmountThreshold) {
            btcTokenAmount = 0;
            btcUsdcAmount = 0;
        }
        if (ethUsdcAmount < state.hedgeUsdcAmountThreshold) {
            ethTokenAmount = 0;
            ethUsdcAmount = 0;
        }

        // get asset amount basis increase/decrease of token amounts
        uint256 btcAssetAmount = repayDebtBtc ? btcUsdcAmount : btcTokenAmount;
        uint256 ethAssetAmount = repayDebtEth ? ethUsdcAmount : ethTokenAmount;

        // If both eth and btc swap amounts are not beyond the threshold then no flashloan needs to be executed | case 1
        if (btcAssetAmount == 0 && ethAssetAmount == 0) return;

        if (repayDebtBtc && repayDebtEth) {
            // case where both the token assets are USDC
            // only one entry required which is combined asset amount for both tokens
            assets = new address[](1);
            amounts = new uint256[](1);

            assets[0] = address(state.usdc);
            amounts[0] = (btcAssetAmount + ethAssetAmount);
        } else if (btcAssetAmount == 0 || ethAssetAmount == 0) {
            // Exactly one would be true since case-1 excluded (both false) | case-2
            // One token amount = 0 and other token amount > 0
            // only one entry required for the non-zero amount token
            assets = new address[](1);
            amounts = new uint256[](1);

            if (btcAssetAmount != 0) {
                assets[0] = (repayDebtBtc ? address(state.usdc) : address(state.wbtc));
                amounts[0] = btcAssetAmount;
            } else {
                assets[0] = (repayDebtEth ? address(state.usdc) : address(state.weth));
                amounts[0] = ethAssetAmount;
            }
        } else {
            // Both are true | case-3
            assets = new address[](2);
            amounts = new uint256[](2);

            assets[0] = repayDebtBtc ? address(state.usdc) : address(state.wbtc);

            assets[1] = repayDebtEth ? address(state.usdc) : address(state.weth);

            // ensure that assets and amount tuples are in sorted order of addresses
            // (required for balancer flashloans)
            if (assets[0] > assets[1]) {
                // if the order is descending
                // switch the order for assets tupe
                // assign amounts in opposite order
                address tempAsset = assets[0];
                assets[0] = assets[1];
                assets[1] = tempAsset;

                amounts[0] = ethAssetAmount;

                amounts[1] = btcAssetAmount;
            } else {
                // if the order is ascending
                // assign amount in same order
                amounts[0] = btcAssetAmount;

                amounts[1] = ethAssetAmount;
            }
        }
        // execute the flashloan
        _executeFlashloan(
            state,
            assets,
            amounts,
            btcTokenAmount,
            btcUsdcAmount,
            ethTokenAmount,
            ethUsdcAmount,
            repayDebtBtc,
            repayDebtEth
        );
    }

    ///@notice returns the optimal borrow amounts based on a swap threshold
    ///@dev if the swap amount is less than threshold then that is returned
    ///@dev if the swap amount is greater than threshold then threshold amount is returned
    ///@param state set of all state variables of vault
    ///@param token ETH / BTC token
    ///@param optimalTokenBorrow optimal btc amount to hedge sGLP btc underlying completely
    ///@param currentTokenBorrow current btc amount borrowed from AAVE
    ///@return optimalPartialTokenBorrow optimal token hedge if threshold is breached
    ///@return isPartialTokenHedge true if partial hedge needs to be executed for token
    function _getOptimalPartialBorrows(
        State storage state,
        IERC20Metadata token,
        uint256 optimalTokenBorrow,
        uint256 currentTokenBorrow
    ) internal view returns (uint256 optimalPartialTokenBorrow, bool isPartialTokenHedge) {
        // checks if token hedge needs to be increased or decreased
        bool isOptimalHigher = optimalTokenBorrow > currentTokenBorrow;
        // difference = amount of swap to be done for complete hedge
        uint256 diff = isOptimalHigher
            ? optimalTokenBorrow - currentTokenBorrow
            : currentTokenBorrow - optimalTokenBorrow;

        // get the correct threshold basis the token
        uint256 threshold = address(token) == address(state.wbtc)
            ? state.partialBtcHedgeUsdcAmountThreshold
            : state.partialEthHedgeUsdcAmountThreshold;

        // convert usdc threshold into token amount threshold
        uint256 tokenThreshold = threshold.mulDivDown(PRICE_PRECISION, _getTokenPriceInUsdc(state, token));

        if (diff > tokenThreshold) {
            // amount to swap > threshold
            // swap only the threshold amount in this rebalance (partial hedge)
            optimalPartialTokenBorrow = isOptimalHigher
                ? currentTokenBorrow + tokenThreshold
                : currentTokenBorrow - tokenThreshold;
            isPartialTokenHedge = true;
        } else {
            // amount to swap < threshold
            // swap the full amount in this rebalance (complete hedge)
            optimalPartialTokenBorrow = optimalTokenBorrow;
        }
    }

    function _getOptimalBorrowsFinal(
        State storage state,
        uint256 currentBtcBorrow,
        uint256 currentEthBorrow,
        uint256 glpDeposited,
        bool[2] memory conditions // (isPartialAllowed, useUpdatedPoolAmounts)
    )
        internal
        view
        returns (
            uint256 optimalBtcBorrow,
            uint256 optimalEthBorrow,
            uint256 targetDnGmxSeniorVaultAmount,
            uint256 optimalUncappedEthBorrow,
            bool isPartialHedge
        )
    {
        // optimal btc and eth borrows
        // calculated basis the underlying token weights in glp

        (optimalBtcBorrow, optimalEthBorrow) = _getOptimalBorrows({
            state: state,
            glpDeposited: glpDeposited,
            withUpdatedPoolAmounts: conditions[1]
        });

        if (conditions[0] /*isPartialHedge*/) {
            // if partial hedges are allowed (i.e. rebalance call and not deposit/withdraw)
            // check if swap amounts>threshold then basis that do a partial hedge
            bool isPartialBtcHedge;
            bool isPartialEthHedge;
            // get optimal borrows basis hedge thresholds
            (optimalBtcBorrow, isPartialBtcHedge) = _getOptimalPartialBorrows(
                state,
                state.wbtc,
                optimalBtcBorrow,
                currentBtcBorrow
            );
            (optimalEthBorrow, isPartialEthHedge) = _getOptimalPartialBorrows(
                state,
                state.weth,
                optimalEthBorrow,
                currentEthBorrow
            );
            // if some token is partially hedged then set that this rebalance is partial
            // lastRebalanceTime not updated in this case so a rebalance can be called again
            isPartialHedge = isPartialBtcHedge || isPartialEthHedge;
        }

        // calculate usdc value of optimal borrows
        uint256 optimalBorrowValue = _getBorrowValue(state, optimalBtcBorrow, optimalEthBorrow);

        // get liquidation threshold of usdc on AAVE
        uint256 usdcLiquidationThreshold = _getLiquidationThreshold(state, address(state.usdc));

        // Settle net change in market value and deposit/withdraw collateral tokens
        // Vault market value is just the collateral value since profit has been settled
        // AAVE target health factor = (usdc supply value * usdc liquidation threshold)/borrow value
        // whatever tokens we borrow from AAVE (ETH/BTC) we sell for usdc and deposit that usdc into AAVE
        // assuming 0 slippage borrow value of tokens = usdc deposit value (this leads to very small variation in hf)
        // usdc supply value = usdc borrowed from senior tranche + borrow value
        // replacing usdc supply value formula above in AAVE target health factor formula
        // we can derive usdc amount to borrow from senior tranche i.e. targetDnGmxSeniorVaultAmount
        targetDnGmxSeniorVaultAmount = (state.targetHealthFactor - usdcLiquidationThreshold).mulDivDown(
            optimalBorrowValue,
            usdcLiquidationThreshold
        );

        // current usdc borrowed from senior tranche
        uint256 currentDnGmxSeniorVaultAmount = _getUsdcBorrowed(state);

        if (targetDnGmxSeniorVaultAmount > currentDnGmxSeniorVaultAmount) {
            // case where we need to borrow more usdc
            // To get more usdc from senior tranche, so usdc is borrowed first and then hedge is updated on AAVE
            {
                uint256 amountToBorrow = targetDnGmxSeniorVaultAmount - currentDnGmxSeniorVaultAmount;
                uint256 availableBorrow = state.dnGmxSeniorVault.availableBorrow(address(this));
                if (amountToBorrow > availableBorrow) {
                    // if amount to borrow > available borrow amount
                    // we won't be able to hedge glp completely
                    // convert some glp into usdc to keep the vault delta neutral
                    // hedge the btc/eth of remaining amount
                    optimalUncappedEthBorrow = optimalEthBorrow;

                    // optimal btc and eth borrows basis the hedged part of glp
                    (optimalBtcBorrow, optimalEthBorrow) = _getOptimalCappedBorrows(
                        state,
                        currentDnGmxSeniorVaultAmount + availableBorrow,
                        usdcLiquidationThreshold
                    );
                }
            }
        }

        return (
            optimalBtcBorrow,
            optimalEthBorrow,
            targetDnGmxSeniorVaultAmount,
            optimalUncappedEthBorrow,
            isPartialHedge
        );
    }

    ///@notice rebalances btc and eth hedges according to underlying glp token weights
    ///@notice updates the borrowed amount from senior tranche basis the target health factor
    ///@notice if the amount of swap for a token > theshold then a partial hedge is taken and remaining is taken separately
    ///@notice if the amount of swap for a token < threshold complete hedge is taken
    ///@notice in case there is not enough money in senior tranche then relevant amount of glp is converted into usdc
    ///@dev to be called after settle profits only (since vaultMarketValue if after settlement of profits)
    ///@param state set of all state variables of vault
    ///@param currentBtcBorrow The amount of USDC collateral token deposited to LB Protocol
    ///@param currentEthBorrow The market value of ETH/BTC part in sGLP
    ///@param glpDeposited amount of glp deposited into the vault
    ///@param isPartialAllowed true if partial hedge is allowed
    ///@return isPartialHedge true if partial hedge is executed
    function rebalanceHedge(
        State storage state,
        uint256 currentBtcBorrow,
        uint256 currentEthBorrow,
        uint256 glpDeposited,
        bool isPartialAllowed
    ) external returns (bool isPartialHedge) {
        {
            uint256 optimalBtcBorrow;
            uint256 optimalEthBorrow;
            uint256 targetDnGmxSeniorVaultAmount;
            uint256 currentDnGmxSeniorVaultAmount;
            uint256 optimalUncappedEthBorrow;
            (
                optimalBtcBorrow,
                optimalEthBorrow,
                targetDnGmxSeniorVaultAmount,
                optimalUncappedEthBorrow,
                isPartialHedge
            ) = _getOptimalBorrowsFinal(
                state,
                currentBtcBorrow,
                currentEthBorrow,
                glpDeposited,
                [isPartialAllowed, false]
            );
            // current usdc borrowed from senior tranche
            currentDnGmxSeniorVaultAmount = _getUsdcBorrowed(state);
            if (targetDnGmxSeniorVaultAmount > currentDnGmxSeniorVaultAmount) {
                // case where we need to borrow more usdc
                // To get more usdc from senior tranche, so usdc is borrowed first and then hedge is updated on AAVE
                {
                    uint256 amountToBorrow = targetDnGmxSeniorVaultAmount - currentDnGmxSeniorVaultAmount;
                    uint256 availableBorrow = state.dnGmxSeniorVault.availableBorrow(address(this));

                    if (amountToBorrow > availableBorrow) {
                        // if amount to borrow > available borrow amount
                        // we won't be able to hedge glp completely
                        // convert some glp into usdc to keep the vault delta neutral
                        // hedge the btc/eth of remaining amount

                        // rebalance the unhedged glp (increase/decrease basis the capped optimal token hedges)
                        _rebalanceUnhedgedGlp(state, optimalUncappedEthBorrow, optimalEthBorrow);

                        if (availableBorrow > 0) {
                            // borrow whatever is available since required > available
                            state.dnGmxSeniorVault.borrow(availableBorrow);
                        }
                    } else {
                        //No unhedged glp remaining so just pass same value in capped and uncapped (should convert back any ausdc back to sglp)
                        _rebalanceUnhedgedGlp(state, optimalEthBorrow, optimalEthBorrow);

                        // Take from LB Vault
                        state.dnGmxSeniorVault.borrow(targetDnGmxSeniorVaultAmount - currentDnGmxSeniorVaultAmount);
                    }
                }

                // Rebalance Position
                // Executes a flashloan from balancer and btc/eth borrow updates on AAVE
                _rebalanceBorrow(state, optimalBtcBorrow, currentBtcBorrow, optimalEthBorrow, currentEthBorrow);
            } else {
                //No unhedged glp remaining so just pass same value in capped and uncapped (should convert back any ausdc back to sglp)
                _rebalanceUnhedgedGlp(state, optimalEthBorrow, optimalEthBorrow);

                // Executes a flashloan from balancer and btc/eth borrow updates on AAVE
                // To repay usdc to senior tranche so update the hedges on AAVE first
                // then remove usdc to pay back to senior tranche
                _rebalanceBorrow(state, optimalBtcBorrow, currentBtcBorrow, optimalEthBorrow, currentEthBorrow);
                uint256 totalCurrentBorrowValue;
                {
                    (uint256 currentBtc, uint256 currentEth) = _getCurrentBorrows(state);
                    totalCurrentBorrowValue = _getBorrowValue(state, currentBtc, currentEth);
                }
                _rebalanceProfit(state, totalCurrentBorrowValue);
                // Deposit to LB Vault

                state.dnGmxSeniorVault.repay(currentDnGmxSeniorVaultAmount - targetDnGmxSeniorVaultAmount);
            }
        }
    }

    ///@notice withdraws LP tokens from gauge, sells LP token for usdc
    ///@param state set of all state variables of vault
    ///@param usdcAmountDesired amount of USDC desired
    ///@return usdcAmountOut usdc amount returned by gmx
    function _convertAssetToAUsdc(
        State storage state,
        uint256 usdcAmountDesired
    ) internal returns (uint256 usdcAmountOut) {
        ///@dev if usdcAmountDesired < 10, then there is precision issue in gmx contracts while redeeming for usdg

        if (usdcAmountDesired < state.usdcConversionThreshold) return 0;
        address _usdc = address(state.usdc);

        // calculate the minimum required amount basis the set slippage param
        // uses current usdc max price from GMX and adds slippage on top

        // calculate the amount of glp to be converted to get the desired usdc amount
        uint256 glpAmountInput = usdcAmountDesired.mulDivDown(PRICE_PRECISION, _getGlpPriceInUsdc(state, false));

        if (state.useDirectConversionBurn) {
            uint256 minUsdcOut = usdcAmountDesired.mulDivDown((MAX_BPS - state.slippageThresholdGmxBps), MAX_BPS);

            usdcAmountOut = state.mintBurnRewardRouter.unstakeAndRedeemGlp(
                _usdc,
                glpAmountInput,
                minUsdcOut,
                address(this)
            );

            emit GlpSwapped(glpAmountInput, usdcAmountOut, true);

            _executeSupply(state, _usdc, usdcAmountOut);
        } else {
            uint256 ethPriceInUsdc = _getTokenPriceInUsdc(state, state.weth);

            uint256 minEthOut = (usdcAmountDesired.mulDivDown((MAX_BPS - state.slippageThresholdGmxBps), MAX_BPS))
                .mulDivDown(PRICE_PRECISION, ethPriceInUsdc);

            uint256 ethAmountOut = state.mintBurnRewardRouter.unstakeAndRedeemGlp(
                address(state.weth),
                glpAmountInput,
                minEthOut,
                address(this)
            );

            uint256 minUsdcAmount = ethPriceInUsdc.mulDivDown(
                ethAmountOut * (MAX_BPS - state.slippageThresholdSwapEthBps),
                MAX_BPS * PRICE_PRECISION
            );

            (usdcAmountOut, ) = _swapToken(state, address(state.weth), ethAmountOut, minUsdcAmount);

            emit GlpSwapped(glpAmountInput, usdcAmountOut, true);

            _executeSupply(state, _usdc, usdcAmountOut);
        }
    }

    ///@notice sells usdc for LP tokens and then stakes LP tokens
    ///@param state set of all state variables of vault
    ///@param amount amount of usdc
    function _convertAUsdcToAsset(State storage state, uint256 amount) internal {
        _executeWithdraw(state, address(state.usdc), amount, address(this));

        if (state.useDirectConversionMint) {
            uint256 price = state.gmxVault.getMinPrice(address(state.usdc));

            // USDG has 18 decimals and usdc has 6 decimals => 18-6 = 12
            uint256 usdgAmount = amount.mulDivDown(
                price * (MAX_BPS - state.slippageThresholdGmxBps) * 1e12,
                PRICE_PRECISION * MAX_BPS
            );

            // conversion of token into glp using batching manager
            // batching manager handles the conversion due to the cooldown
            // glp transferred to the vault on batch execution

            uint256 glpReceived = _stakeGlp(state, address(state.usdc), amount, usdgAmount);

            emit GlpSwapped(glpReceived, amount, false);
        } else {
            uint256 minWethOut = amount.mulDivDown(
                PRICE_PRECISION * (MAX_BPS - state.slippageThresholdSwapEthBps),
                _getTokenPriceInUsdc(state, state.weth) * MAX_BPS
            );

            (, uint256 wethReceived) = state._swapUSDC(false, address(state.weth), minWethOut, amount);

            uint256 price = state.gmxVault.getMinPrice(address(state.weth));

            // USDG has 18 decimals and weth has 18 decimals => 18-18 = 0
            uint256 usdgAmount = wethReceived.mulDivDown(
                price * (MAX_BPS - state.slippageThresholdGmxBps),
                PRICE_PRECISION * MAX_BPS
            );

            // conversion of token into glp using batching manager
            // batching manager handles the conversion due to the cooldown
            // glp transferred to the vault on batch execution

            uint256 glpReceived = _stakeGlp(state, address(state.weth), wethReceived, usdgAmount);

            emit GlpSwapped(glpReceived, amount, false);
        }
    }

    function _stakeGlp(
        State storage state,
        address token,
        uint256 amount,
        uint256 minUSDG
    ) internal returns (uint256 glpStaked) {
        // will revert if notional output is less than minUSDG
        glpStaked = state.mintBurnRewardRouter.mintAndStakeGlp(token, amount, minUSDG, 0);
    }

    ///@notice rebalances unhedged glp amount
    ///@notice converts some glp into usdc if there is lesser amount of usdc to back the hedges than required
    ///@notice converts some usdc into glp if some part of the unhedged glp can be hedged
    ///@notice used when there is not enough usdc available in senior tranche
    ///@param state set of all state variables of vault
    ///@param uncappedTokenHedge token hedge if there was no asset cap
    ///@param cappedTokenHedge token hedge if given there is limited about of assets available in senior tranche
    function _rebalanceUnhedgedGlp(State storage state, uint256 uncappedTokenHedge, uint256 cappedTokenHedge) private {
        // early return if optimal amounts are zero
        if (uncappedTokenHedge == 0) return;

        // part of glp assets to be kept unhedged
        // calculated basis the uncapped amount (assumes unlimited borrow availability)
        // and capped amount (basis available borrow)

        // uncappedTokenHedge is required to hedge totalAssets
        // cappedTokenHedge can be taken basis available borrow
        // so basis what % if hedge cannot be taken, same % of glp is converted to usdc
        uint256 targetUnhedgedGlp = _totalGlp(state, false).mulDivDown(
            uncappedTokenHedge - cappedTokenHedge,
            uncappedTokenHedge
        );

        // usdc value of unhedged glp assets
        uint256 targetUnhedgedGlpUsdcAmount = targetUnhedgedGlp.mulDivDown(
            _getGlpPriceInUsdc(state, false),
            PRICE_PRECISION
        );

        if (targetUnhedgedGlpUsdcAmount > state.unhedgedGlpInUsdc) {
            // if target unhedged amount > current unhedged amount
            // convert glp to aUSDC
            uint256 glpToUsdcAmount = targetUnhedgedGlpUsdcAmount - state.unhedgedGlpInUsdc;
            state.unhedgedGlpInUsdc += _convertAssetToAUsdc(state, glpToUsdcAmount);
        } else if (targetUnhedgedGlpUsdcAmount < state.unhedgedGlpInUsdc) {
            // if target unhedged amount < current unhedged amount
            // convert aUSDC to glp
            uint256 usdcToGlpAmount = state.unhedgedGlpInUsdc - targetUnhedgedGlpUsdcAmount;
            state.unhedgedGlpInUsdc -= usdcToGlpAmount;
            _convertAUsdcToAsset(state, usdcToGlpAmount);
        }
    }

    /* ##################################################################
                            FLASHLOAN RECEIVER
    ################################################################## */

    ///@notice flashloan receiver for balance vault
    ///@notice receives flashloaned tokens(WETH or WBTC or USDC) from balancer, swaps on uniswap and borrows/repays on AAVE
    ///@dev only allows balancer vault to call this
    ///@dev only runs when _hasFlashloaned is set to true (prevents someone else from initiating flashloan to vault)
    ///@param tokens list of tokens flashloaned
    ///@param amounts amounts of token flashloans in same order
    ///@param feeAmounts amounts of fee/premium charged for flashloan
    ///@param userData data passed to balancer for flashloan (includes token amounts, token usdc value and swap direction)
    function receiveFlashLoan(
        State storage state,
        IERC20[] memory tokens,
        uint256[] memory amounts,
        uint256[] memory feeAmounts,
        bytes memory userData
    ) external {
        // Decode user data containing btc/eth token & usdc amount
        // RepayDebt true means we need to reduce token hedge else we need to increase hedge
        (
            uint256 btcTokenAmount,
            uint256 btcUsdcAmount,
            uint256 ethTokenAmount,
            uint256 ethUsdcAmount,
            bool repayDebtBtc,
            bool repayDebtEth
        ) = abi.decode(userData, (uint256, uint256, uint256, uint256, bool, bool));

        // Asset premium charged for taking the flashloan from balancer
        uint256 btcAssetPremium;
        uint256 ethAssetPremium;

        // adjust asset amounts for premiums (zero for balancer at the time of dev)
        if (repayDebtBtc && repayDebtEth) {
            // Both token amounts are non zero
            // The assets are same (usdc only)
            // Here amounts[0] should be equal to btcTokenAmount+ethTokenAmount
            // Total premium on USDC is divided on a prorata basis for btc and eth usdc amounts
            btcAssetPremium = feeAmounts[0].mulDivDown(btcUsdcAmount, amounts[0]);

            ethAssetPremium = (feeAmounts[0] - btcAssetPremium);
        } else if (btcTokenAmount != 0 && ethTokenAmount != 0) {
            // Both token amounts are non zero
            // The assets are different (either usdc/btc, usdc/eth, btc/eth)
            // Here amounts[0] should be equal to btcTokenAmount and amounts[1] should be equal to ethTokenAmount
            bool btcFirst = false;

            // Checks if btc or eth is first since they are sorted basis token address when taking flashloan
            if (repayDebtBtc ? tokens[0] == state.usdc : tokens[0] == state.wbtc) btcFirst = true;

            // Premiums are assigned basis the token amount orders
            btcAssetPremium = feeAmounts[btcFirst ? 0 : 1];
            ethAssetPremium = feeAmounts[btcFirst ? 1 : 0];
        } else {
            // One of the token amounts is zero
            // The asset for non zero token can be both token or usdc
            // Premium is assigned to the non-zero amount token
            if (btcTokenAmount != 0) btcAssetPremium = feeAmounts[0];
            else ethAssetPremium = feeAmounts[0];
        }

        // Execute the token swap (usdc to token / token to usdc) and repay the debt
        if (btcTokenAmount > 0)
            _executeOperationToken(
                state,
                address(state.wbtc),
                btcTokenAmount,
                btcUsdcAmount,
                btcAssetPremium,
                repayDebtBtc
            );
        if (ethTokenAmount > 0)
            _executeOperationToken(
                state,
                address(state.weth),
                ethTokenAmount,
                ethUsdcAmount,
                ethAssetPremium,
                repayDebtEth
            );
    }

    /* ##################################################################
                            AAVE HELPERS
    ################################################################## */

    ///@notice executes borrow of "token" of "amount" quantity to AAVE at variable interest rate
    ///@param state set of all state variables of vault
    ///@param token address of token to borrow
    ///@param amount amount of token to borrow
    function _executeBorrow(State storage state, address token, uint256 amount) internal {
        state.pool.borrow(token, amount, VARIABLE_INTEREST_MODE, 0, address(this));
    }

    ///@notice executes repay of "token" of "amount" quantity to AAVE at variable interest rate
    ///@param state set of all state variables of vault
    ///@param token address of token to borrow
    ///@param amount amount of token to borrow
    function _executeRepay(State storage state, address token, uint256 amount) internal {
        state.pool.repay(token, amount, VARIABLE_INTEREST_MODE, address(this));
    }

    ///@notice executes supply of "token" of "amount" quantity to AAVE
    ///@param state set of all state variables of vault
    ///@param token address of token to borrow
    ///@param amount amount of token to borrow
    function _executeSupply(State storage state, address token, uint256 amount) internal {
        state.pool.supply(token, amount, address(this), 0);
    }

    ///@notice executes withdraw of "token" of "amount" quantity to AAVE
    ///@param state set of all state variables of vault
    ///@param token address of token to borrow
    ///@param amount amount of token to borrow
    ///@param receiver address to which withdrawn tokens should be sent
    function _executeWithdraw(State storage state, address token, uint256 amount, address receiver) internal {
        state.pool.withdraw(token, amount, receiver);
    }

    ///@notice returns liquidation threshold of the selected asset's AAVE pool
    ///@param state set of all state variables of vault
    ///@param asset address of asset to check liquidation threshold for
    function _getLiquidationThreshold(State storage state, address asset) private view returns (uint256) {
        DataTypes.ReserveConfigurationMap memory config = state.pool.getConfiguration(asset);
        (, uint256 liquidationThreshold, , , , ) = config.getParams();

        return liquidationThreshold;
    }

    /* ##################################################################
                            BALANCER HELPERS
    ################################################################## */

    ///@notice executes flashloan from balancer
    ///@dev assets should be ordered in ascending order of addresses
    ///@param assets list of token addresses
    ///@param amounts amount of tokens to be flashloaned in same order as assets
    ///@param _btcTokenAmount token amount of btc token by which hedge amount should be increased (if repayDebt false) or decreased (if repayDebt true)
    ///@param _btcUsdcAmount usdc value of btc token considering swap slippage. Minimum amount (if repayDebt false) or maximum amount (if repayDebt true)
    ///@param _ethTokenAmount token amount of eth token by which hedge amount should be increased (if repayDebt false) or decreased (if repayDebt true)
    ///@param _ethUsdcAmount usdc value of eth token considering swap slippage. Minimum amount (if repayDebt false) or maximum amount (if repayDebt true)
    ///@param _repayDebtBtc repay debt for btc token
    ///@param _repayDebtEth repay debt for eth token
    function _executeFlashloan(
        State storage state,
        address[] memory assets,
        uint256[] memory amounts,
        uint256 _btcTokenAmount,
        uint256 _btcUsdcAmount,
        uint256 _ethTokenAmount,
        uint256 _ethUsdcAmount,
        bool _repayDebtBtc,
        bool _repayDebtEth
    ) internal {
        if (assets.length != amounts.length) revert IDnGmxJuniorVault.ArraysLengthMismatch();

        // to ensure that only vault originated flashloans should be able to work with receive flashloan
        state.hasFlashloaned = true;

        state.balancerVault.flashLoan(
            address(this),
            assets,
            amounts,
            abi.encode(_btcTokenAmount, _btcUsdcAmount, _ethTokenAmount, _ethUsdcAmount, _repayDebtBtc, _repayDebtEth)
        );

        // receive flashloan has passed so the variable can be made false again
        state.hasFlashloaned = false;
    }

    ///@notice executes relevant token hedge update on receiving the flashloan from Balancer
    ///@dev if "repayDebt = true" then usdc flashloaned, swapped for token, repay token debt, withdraw usdc from AAVE and pay back usdc with premium
    ///@dev if "repayDebt = false" then token flashloaned, swapped for usdc, supply usdc, borrow tokens from AAVE and pay back tokens with premium
    ///@param token address of token to increase/decrease hedge by
    ///@param tokenAmount amount of tokens to swap
    ///@param usdcAmount if "repayDebt = false" then = minimum amount of usdc | if "repayDebt = true" then = maximum amount of usdc
    ///@param premium additional tokens/usdc to be repaid to balancer to cover flashloan fees
    ///@param repayDebt true if token hedge needs to be reduced
    function _executeOperationToken(
        State storage state,
        address token,
        uint256 tokenAmount,
        uint256 usdcAmount,
        uint256 premium,
        bool repayDebt
    ) internal {
        if (!repayDebt) {
            // increase token hedge amount
            // add premium to token amount (to be paid back to balancer)
            uint256 amountWithPremium = tokenAmount + premium;

            // swap token to usdc
            (uint256 usdcReceived, ) = state._swapToken(token, tokenAmount, usdcAmount);

            // supply received usdc to AAVE
            state._executeSupply(address(state.usdc), usdcReceived);

            // borrow amount with premium amount of tokens from AAVE
            state._executeBorrow(token, amountWithPremium);

            // increase junior tranche usdc deposits by usdc received
            state.dnUsdcDeposited += usdcReceived.toInt256();

            // transfer token amount borrowed with premium back to balancer pool
            IERC20(token).transfer(address(state.balancerVault), amountWithPremium);
        } else {
            // decrease token hedge amount
            // usdcAmount = amount flashloaned from balancer
            // usdcPaid = amount paid for receiving given token amount
            // usdcAmount-usdcPaid = amount of usdc remaining after the swap
            // so we just need to withdraw usdcPaid to transfer usdcAmount

            // swap usdc amount to token
            (uint256 usdcPaid, uint256 tokensReceived) = state._swapUSDC(true, token, tokenAmount, usdcAmount);

            // amount of usdc that got charged for the token required
            uint256 amountWithPremium = usdcPaid + premium;

            // reduce delta neutral usdc amount by amount with premium
            state.dnUsdcDeposited -= amountWithPremium.toInt256();

            // repay token debt on AAVE
            state._executeRepay(token, tokensReceived);

            // withdraw amount with premium supplied to AAVE
            state._executeWithdraw(address(state.usdc), amountWithPremium, address(this));

            // transfer usdc amount flashloaned + premium back to balancer
            state.usdc.transfer(address(state.balancerVault), usdcAmount + premium);
        }
    }

    /* ##################################################################
                            VIEW FUNCTIONS
    ################################################################## */

    ///@notice returns the usdc amount borrowed by junior tranche from senior tranche
    ///@param state set of all state variables of vault
    ///@return usdcAmount amount of usdc borrowed by junior tranche
    function _getUsdcBorrowed(State storage state) private view returns (uint256 usdcAmount) {
        // all the aave interest goes to senior tranche
        // so, usdc borrowed from senior tranche =
        // total aUSDC balance - (usdc deposited by delta neutral vault into AAVE) - (unhedged amount of glp in usdc)
        return
            uint256(
                state.aUsdc.balanceOf(address(this)).toInt256() -
                    state.dnUsdcDeposited -
                    state.unhedgedGlpInUsdc.toInt256()
            );
    }

    ///@notice returns the total assets deposited to the vault (in glp amount)
    ///@param state set of all state variables of vault
    ///@return total asset amount (glp + usdc (in glp terms))
    function totalAssets(State storage state) external view returns (uint256) {
        return _totalAssets(state, false);
    }

    ///@notice returns the total assets deposited to the vault (in glp amount)
    ///@param state set of all state variables of vault
    ///@param maximize true for maximizing the total assets value and false to minimize
    ///@return total asset amount (glp + usdc (in glp terms))
    function totalAssets(State storage state, bool maximize) external view returns (uint256) {
        return _totalAssets(state, maximize);
    }

    ///@notice returns the total assets deposited to the vault (in glp amount)
    ///@param state set of all state variables of vault
    ///@param maximize true for maximizing the total assets value and false to minimize
    ///@return total asset amount (glp + usdc (in glp terms))
    function _totalAssets(State storage state, bool maximize) private view returns (uint256) {
        // usdc deposited by junior tranche (can be negative)
        int256 dnUsdcDeposited = state.dnUsdcDeposited;

        // calculate current borrow amounts
        (uint256 currentBtc, uint256 currentEth) = _getCurrentBorrows(state);
        // total borrow value is the value of ETH and BTC required to be paid off
        uint256 totalCurrentBorrowValue = _getBorrowValue(state, currentBtc, currentEth);
        uint256 aaveProfitGlp;
        uint256 aaveLossGlp;
        {
            // convert it into two uints basis the sign
            uint256 aaveProfit = dnUsdcDeposited > int256(0) ? uint256(dnUsdcDeposited) : 0;
            uint256 aaveLoss = dnUsdcDeposited < int256(0)
                ? uint256(-dnUsdcDeposited) + totalCurrentBorrowValue
                : totalCurrentBorrowValue;

            if (aaveProfit > aaveLoss) {
                aaveProfitGlp = (aaveProfit - aaveLoss).mulDivDown(
                    PRICE_PRECISION,
                    _getGlpPriceInUsdc(state, !maximize)
                );
                if (!maximize)
                    aaveProfitGlp = aaveProfitGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);
                aaveLossGlp = 0;
            } else {
                aaveLossGlp = (aaveLoss - aaveProfit).mulDivDown(PRICE_PRECISION, _getGlpPriceInUsdc(state, maximize));
                if (!maximize) aaveLossGlp = aaveLossGlp.mulDivDown(MAX_BPS + state.slippageThresholdGmxBps, MAX_BPS);
                aaveProfitGlp = 0;
            }
        }

        // total assets considers 3 parts
        // part1: glp balance in vault
        // part2: usdc balance in vault (unhedged glp)
        // part3: pnl on AAVE (i.e. aaveProfitGlp - aaveLossGlp)
        return _totalGlp(state, maximize) + aaveProfitGlp - aaveLossGlp;
    }

    ///@notice returns the total assets deposited to the vault (in glp amount)
    ///@param state set of all state variables of vault
    ///@param maximize true for maximizing the total assets value and false to minimize
    ///@return total asset amount (glp + usdc (in glp terms))
    function totalGlp(State storage state, bool maximize) external view returns (uint256) {
        return _totalGlp(state, maximize);
    }

    function _totalGlp(State storage state, bool maximize) private view returns (uint256) {
        // convert usdc amount into glp amount
        // unhedged glp is kept in usdc so there would be conversion slippage on that
        uint256 unhedgedGlp = (state.unhedgedGlpInUsdc).mulDivDown(
            PRICE_PRECISION,
            _getGlpPriceInUsdc(state, !maximize)
        );

        // if we need to minimize then add additional slippage
        if (!maximize) unhedgedGlp = unhedgedGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);

        // total assets considers 3 parts
        // part1: glp balance in vault
        // part2: usdc balance in vault (unhedged glp)
        return state.fsGlp.balanceOf(address(this)) + unhedgedGlp;
    }

    ///@notice returns if the rebalance is valid basis last rebalance time and rebalanceTimeThreshold
    ///@param state set of all state variables of vault
    ///@return true if the rebalance is valid basis time threshold
    /* solhint-disable not-rely-on-time */
    function isValidRebalanceTime(State storage state) external view returns (bool) {
        // check if rebalanceTimeThreshold has passed since last rebalance time
        return (block.timestamp - state.lastRebalanceTS) > state.rebalanceTimeThreshold;
    }

    ///@notice returns if the rebalance is valid basis health factor on AAVE
    ///@notice returns true if current health factor < threshold
    ///@param state set of all state variables of vault
    ///@return true if the rebalance is valid basis AAVE health factor
    function isValidRebalanceHF(State storage state) external view returns (bool) {
        return _isValidRebalanceHF(state);
    }

    function _isValidRebalanceHF(State storage state) private view returns (bool) {
        // check if health factor on AAVE is below rebalanceHfThreshold
        (, , , , , uint256 healthFactor) = state.pool.getUserAccountData(address(this));

        return healthFactor < (uint256(state.rebalanceHfThresholdBps) * 1e14);
    }

    ///@notice returns if the rebalance is valid basis the difference between the current and optimal hedges of tokens(ETH/BTC)
    ///@param state set of all state variables of vault
    ///@return true if the rebalance is valid basis diffeence (current and optimal) threshold
    function isValidRebalanceDeviation(State storage state) external view returns (bool) {
        (uint128 optimalBtcPoolAmount, uint128 optimalEthPoolAmount) = _getPoolAmounts(state);

        uint128 currentBtcPoolAmount = state.btcPoolAmount;
        uint128 currentEthPoolAmount = state.ethPoolAmount;

        return
            !(_isWithinAllowedDelta(state, optimalBtcPoolAmount, currentBtcPoolAmount) &&
                _isWithinAllowedDelta(state, optimalEthPoolAmount, currentEthPoolAmount));
    }

    function isValidRebalanceDueToChangeInHedges(State storage state) external view returns (bool) {
        return _isValidRebalanceDueToChangeInHedges(state);
    }

    function _isValidRebalanceDueToChangeInHedges(State storage state) private view returns (bool) {
        (int128 currentBtcTraderOIHedge, int128 currentEthTraderOIHedge) = _getTraderOIHedgeAmounts(state);
        return
            !(currentBtcTraderOIHedge == state.btcTraderOIHedge && currentEthTraderOIHedge == state.ethTraderOIHedge);
    }

    function getTraderOIHedgeAmounts(
        State storage state
    ) external view returns (int128 currentBtcTraderOIHedge, int128 currentEthTraderOIHedge) {
        return _getTraderOIHedgeAmounts(state);
    }

    function _getTraderOIHedgeAmounts(
        State storage state
    ) private view returns (int128 currentBtcTraderOIHedge, int128 currentEthTraderOIHedge) {
        currentBtcTraderOIHedge = state.dnGmxTraderHedgeStrategy.btcTraderOIHedge();
        currentEthTraderOIHedge = state.dnGmxTraderHedgeStrategy.ethTraderOIHedge();
    }

    function getPoolAmounts(State storage state) external view returns (uint128 btcPoolAmount, uint128 ethPoolAmount) {
        return _getPoolAmounts(state);
    }

    function _getPoolAmounts(State storage state) private view returns (uint128 btcPoolAmount, uint128 ethPoolAmount) {
        btcPoolAmount = uint128(state.gmxVault.poolAmounts(address(state.wbtc)));
        ethPoolAmount = uint128(state.gmxVault.poolAmounts(address(state.weth)));
    }

    ///@notice returns the price of given token basis AAVE oracle
    ///@param state set of all state variables of vault
    ///@param token the token for which price is expected
    ///@return token price in usd
    function getTokenPrice(State storage state, IERC20Metadata token) external view returns (uint256) {
        return _getTokenPrice(state, token);
    }

    ///@notice returns the price of given token basis AAVE oracle
    ///@param state set of all state variables of vault
    ///@param token the token for which price is expected
    ///@return token price in usd
    function _getTokenPrice(State storage state, IERC20Metadata token) private view returns (uint256) {
        uint256 decimals = token.decimals();

        // AAVE oracle
        uint256 price = state.oracle.getAssetPrice(address(token));

        // @dev aave returns from same source as chainlink (which is 8 decimals)
        return price.mulDivDown(PRICE_PRECISION, 10 ** (decimals + 2));
    }

    ///@notice returns the price of glp token
    ///@param state set of all state variables of vault
    ///@param maximize true to get maximum price and false to get minimum
    ///@return glp price in usd
    function getGlpPrice(State storage state, bool maximize) external view returns (uint256) {
        return _getGlpPrice(state, maximize);
    }

    ///@notice returns the price of glp token
    ///@param state set of all state variables of vault
    ///@param maximize true to get maximum price and false to get minimum
    ///@return glp price in usd
    function _getGlpPrice(State storage state, bool maximize) private view returns (uint256) {
        uint256 aum = state.glpManager.getAum(maximize);
        uint256 totalSupply = state.glp.totalSupply();

        // price per glp token = (total AUM / total supply)
        return aum.mulDivDown(PRICE_PRECISION, totalSupply * 1e24);
    }

    ///@notice returns the price of glp token in usdc
    ///@param state set of all state variables of vault
    ///@param maximize true to get maximum price and false to get minimum
    ///@return glp price in usdc
    function getGlpPriceInUsdc(State storage state, bool maximize) external view returns (uint256) {
        return _getGlpPriceInUsdc(state, maximize);
    }

    ///@notice returns the price of glp token in usdc
    ///@param state set of all state variables of vault
    ///@param maximize true to get maximum price and false to get minimum
    ///@return glp price in usdc
    function _getGlpPriceInUsdc(State storage state, bool maximize) private view returns (uint256) {
        uint256 aum = state.glpManager.getAum(maximize);
        uint256 totalSupply = state.glp.totalSupply();

        // @dev aave returns from same source as chainlink (which is 8 decimals)
        uint256 quotePrice = state.oracle.getAssetPrice(address(state.usdc));

        // price per glp token = (total AUM / total supply)
        // scaling factor = 30(aum) -18(totalSupply) -8(quotePrice) +18(glp) -6(usdc) = 16
        return aum.mulDivDown(PRICE_PRECISION, totalSupply * quotePrice * 1e16);
    }

    ///@notice returns the price of given token in USDC using AAVE oracle
    ///@param state set of all state variables of vault
    ///@param token the token for which price is expected
    ///@return scaledPrice token price in usdc
    function getTokenPriceInUsdc(
        State storage state,
        IERC20Metadata token
    ) external view returns (uint256 scaledPrice) {
        return _getTokenPriceInUsdc(state, token);
    }

    ///@notice returns the price of given token in USDC using AAVE oracle
    ///@param state set of all state variables of vault
    ///@param token the token for which price is expected
    ///@return scaledPrice token price in usdc
    function _getTokenPriceInUsdc(
        State storage state,
        IERC20Metadata token
    ) internal view returns (uint256 scaledPrice) {
        uint256 decimals = token.decimals();
        uint256 price = state.oracle.getAssetPrice(address(token));

        // @dev aave returns from same source as chainlink (which is 8 decimals)
        uint256 quotePrice = state.oracle.getAssetPrice(address(state.usdc));

        // token price / usdc price
        scaledPrice = price.mulDivDown(PRICE_PRECISION, quotePrice * 10 ** (decimals - 6));
    }

    ///@notice returns liquidation threshold of the selected asset's AAVE pool
    ///@param state set of all state variables of vault
    ///@param asset address of asset to check liquidation threshold for
    ///@return liquidation threshold
    function getLiquidationThreshold(State storage state, address asset) external view returns (uint256) {
        return _getLiquidationThreshold(state, asset);
    }

    ///@notice returns the borrow value for a given amount of tokens
    ///@param state set of all state variables of vault
    ///@param btcAmount amount of btc
    ///@param ethAmount amount of eth
    ///@return borrowValue value of the given token amounts
    function getBorrowValue(
        State storage state,
        uint256 btcAmount,
        uint256 ethAmount
    ) external view returns (uint256 borrowValue) {
        return _getBorrowValue(state, btcAmount, ethAmount);
    }

    ///@notice returns the borrow value for a given amount of tokens
    ///@param state set of all state variables of vault
    ///@param btcAmount amount of btc
    ///@param ethAmount amount of eth
    ///@return borrowValue value of the given token amounts
    function _getBorrowValue(
        State storage state,
        uint256 btcAmount,
        uint256 ethAmount
    ) private view returns (uint256 borrowValue) {
        borrowValue =
            btcAmount.mulDivDown(_getTokenPrice(state, state.wbtc), PRICE_PRECISION) +
            ethAmount.mulDivDown(_getTokenPrice(state, state.weth), PRICE_PRECISION);
        borrowValue = borrowValue.mulDivDown(PRICE_PRECISION, _getTokenPrice(state, state.usdc));
    }

    function getSlippageAdjustedAssets(
        State storage state,
        uint256 assets,
        bool isDeposit
    ) external view returns (uint256) {
        return _getSlippageAdjustedAssets(state, assets, isDeposit);
    }

    function _getSlippageAdjustedAssets(
        State storage state,
        uint256 assets,
        bool isDeposit
    ) private view returns (uint256) {
        // get change in borrow positions to calculate amount to swap on uniswap
        (int256 netBtcBorrowChange, int256 netEthBorrowChange) = _getNetPositionChange(state, assets, isDeposit);

        uint256 dollarsLostDueToSlippage = _quoteSwapSlippageLoss(state, netBtcBorrowChange, netEthBorrowChange);

        // netSlippage returned is in glp (asset) terms
        uint256 glpPrice = _getGlpPriceInUsdc(state, false);
        uint256 netSlippage = dollarsLostDueToSlippage.mulDivUp(PRICE_PRECISION, glpPrice);

        // subtract slippage from assets, and calculate shares basis that slippage adjusted asset amount
        if (netSlippage >= assets) revert IDnGmxJuniorVault.TooMuchSlippage(netSlippage, assets);
        assets -= netSlippage;

        return assets;
    }

    function getNetPositionChange(
        State storage state,
        uint256 assetAmount,
        bool isDeposit
    ) external view returns (int256, int256) {
        return _getNetPositionChange(state, assetAmount, isDeposit);
    }

    function _getNetPositionChange(
        State storage state,
        uint256 assetAmount,
        bool isDeposit
    ) private view returns (int256 netBtcBorrowChange, int256 netEthBorrowChange) {
        // get current borrows
        (uint256 currentBtcBorrow, uint256 currentEthBorrow) = _getCurrentBorrows(state);

        // calculate new total assets basis assetAmount
        uint256 total = _totalGlp(state, true);
        uint256 totalAssetsAfter = isDeposit ? total + assetAmount : total - assetAmount;

        // get optimal borrows accounting for incoming glp deposit
        (uint256 optimalBtcBorrow, uint256 optimalEthBorrow, , , ) = _getOptimalBorrowsFinal(
            state,
            currentBtcBorrow,
            currentEthBorrow,
            totalAssetsAfter,
            [false, false]
        );

        // calculate the diff, i.e token amounts to be swapped on uniswap
        // if optimal > current, swapping token to usdc
        // if optimal < current, swapping usdc to token
        netBtcBorrowChange = optimalBtcBorrow.toInt256() - currentBtcBorrow.toInt256();
        netEthBorrowChange = optimalEthBorrow.toInt256() - currentEthBorrow.toInt256();
    }

    function quoteSwapSlippageLoss(
        State storage state,
        int256 btcAmount,
        int256 ethAmount
    ) external view returns (uint256) {
        return _quoteSwapSlippageLoss(state, btcAmount, ethAmount);
    }

    function _calculateSwapLoss(
        int256 tokenAmount,
        int256 otherTokenAmount,
        uint256 tokenPrice,
        uint256 otherTokenPrice
    ) internal pure returns (uint256) {
        uint256 dollarsPaid;
        uint256 dollarsReceived;
        if (tokenAmount > 0) {
            dollarsPaid = uint256(tokenAmount).mulDivUp(tokenPrice, PRICE_PRECISION);
            dollarsReceived = uint256(-otherTokenAmount).mulDivDown(otherTokenPrice, PRICE_PRECISION);
        } else if (tokenAmount < 0) {
            dollarsPaid = uint256(otherTokenAmount).mulDivUp(otherTokenPrice, PRICE_PRECISION);
            dollarsReceived = uint256(-tokenAmount).mulDivDown(tokenPrice, PRICE_PRECISION);
        }
        return (dollarsPaid > dollarsReceived) ? uint256(dollarsPaid - dollarsReceived) : 0;
    }

    /// @notice returns the amount of glp to be charged as slippage loss
    /// @param state set of all state variables of vault
    /// @param btcAmountInBtcSwap if positive btc sell amount else if negative btc buy amount
    /// @param ethAmountInEthSwap if positive eth sell amount else if negative eth buy amount
    function _quoteSwapSlippageLoss(
        State storage state,
        int256 btcAmountInBtcSwap,
        int256 ethAmountInEthSwap
    ) internal view returns (uint256 dollarsLostDueToSlippage) {
        (int256 usdcAmountInBtcSwap, int256 usdcAmountInEthSwap) = QuoterLib.quoteCombinedSwap(
            btcAmountInBtcSwap,
            ethAmountInEthSwap,
            SwapPath.generate({
                tokenIn: state.wbtc,
                feeIn: state.feeTierWethWbtcPool,
                tokenIntermediate: state.weth,
                feeOut: 500,
                tokenOut: state.usdc,
                isExactIn: true
            }),
            SwapPath.generate({ tokenIn: state.weth, fee: 500, tokenOut: state.usdc, isExactIn: true })
        );

        uint256 btcPrice = _getTokenPriceInUsdc(state, state.wbtc);
        uint256 ethPrice = _getTokenPriceInUsdc(state, state.weth);
        uint256 usdcPrice = _getTokenPriceInUsdc(state, state.usdc);

        return
            _calculateSwapLoss(btcAmountInBtcSwap, usdcAmountInBtcSwap, btcPrice, usdcPrice) +
            _calculateSwapLoss(ethAmountInEthSwap, usdcAmountInEthSwap, ethPrice, usdcPrice);
    }

    ///@notice returns the amount of flashloan for a given token
    ///@notice if token amount needs to be increased then usdcAmount is minimum amount
    ///@notice if token amount needs to be decreased then usdcAmount is maximum amount
    ///@param state set of all state variables of vault
    ///@param token address of token
    ///@param optimalBorrow optimal token borrow to completely hedge sGlp
    ///@param currentBorrow curret token borrow from AAVE
    ///@return tokenAmount amount of tokens to be swapped
    ///@return usdcAmount minimum/maximum amount of usdc basis swap direction
    ///@return repayDebt true then reduce token hedge, false then increase token hedge
    function flashloanAmounts(
        State storage state,
        address token,
        uint256 optimalBorrow,
        uint256 currentBorrow
    ) external view returns (uint256 tokenAmount, uint256 usdcAmount, bool repayDebt) {
        return _flashloanAmounts(state, token, optimalBorrow, currentBorrow);
    }

    ///@notice returns the amount of flashloan for a given token
    ///@notice if token amount needs to be increased then usdcAmount is minimum amount
    ///@notice if token amount needs to be decreased then usdcAmount is maximum amount
    ///@param state set of all state variables of vault
    ///@param token address of token
    ///@param optimalBorrow optimal token borrow to completely hedge sGlp
    ///@param currentBorrow curret token borrow from AAVE
    ///@return tokenAmount amount of tokens to be swapped
    ///@return usdcAmount minimum/maximum amount of usdc basis swap direction
    ///@return repayDebt true then reduce token hedge, false then increase token hedge
    function _flashloanAmounts(
        State storage state,
        address token,
        uint256 optimalBorrow,
        uint256 currentBorrow
    ) private view returns (uint256 tokenAmount, uint256 usdcAmount, bool repayDebt) {
        uint256 slippageThresholdSwap = token == address(state.wbtc)
            ? state.slippageThresholdSwapBtcBps
            : state.slippageThresholdSwapEthBps;
        // check the delta between optimal position and actual position in token terms
        // take that position using swap
        // To Increase
        if (optimalBorrow > currentBorrow) {
            tokenAmount = optimalBorrow - currentBorrow;
            // To swap with the amount in specified hence usdcAmount should be the min amount out
            usdcAmount = _getTokenPriceInUsdc(state, IERC20Metadata(token)).mulDivDown(
                tokenAmount * (MAX_BPS - slippageThresholdSwap),
                MAX_BPS * PRICE_PRECISION
            );

            repayDebt = false;
            // Flash loan ETH/BTC from AAVE
            // In callback: Sell loan for USDC and repay debt
        } else {
            // To Decrease
            tokenAmount = (currentBorrow - optimalBorrow);
            // To swap with amount out specified hence usdcAmount should be the max amount in
            usdcAmount = _getTokenPriceInUsdc(state, IERC20Metadata(token)).mulDivDown(
                tokenAmount * (MAX_BPS + slippageThresholdSwap),
                MAX_BPS * PRICE_PRECISION
            );

            repayDebt = true;
            // In callback: Swap to ETH/BTC and deposit to AAVE
            // Send back some aUSDC to LB vault
        }
    }

    ///@notice returns the amount of current borrows of btc and eth token from AAVE
    ///@param state set of all state variables of vault
    ///@return currentBtcBorrow amount of btc currently borrowed from AAVE
    ///@return currentEthBorrow amount of eth currently borrowed from AAVE
    function getCurrentBorrows(
        State storage state
    ) external view returns (uint256 currentBtcBorrow, uint256 currentEthBorrow) {
        return _getCurrentBorrows(state);
    }

    ///@notice returns the amount of current borrows of btc and eth token from AAVE
    ///@param state set of all state variables of vault
    ///@return currentBtcBorrow amount of btc currently borrowed from AAVE
    ///@return currentEthBorrow amount of eth currently borrowed from AAVE
    function _getCurrentBorrows(
        State storage state
    ) private view returns (uint256 currentBtcBorrow, uint256 currentEthBorrow) {
        return (state.vWbtc.balanceOf(address(this)), state.vWeth.balanceOf(address(this)));
    }

    ///@notice returns optimal borrows for BTC and ETH respectively basis glpDeposited amount and stored pool amount
    ///@param state set of all state variables of vault
    ///@param glpDeposited amount of glp for which optimal borrow needs to be calculated
    ///@return optimalBtcBorrow optimal amount of btc borrowed from AAVE
    ///@return optimalEthBorrow optimal amount of eth borrowed from AAVE
    function getOptimalBorrows(
        State storage state,
        uint256 glpDeposited,
        bool withUpdatedPoolAmounts
    ) external view returns (uint256 optimalBtcBorrow, uint256 optimalEthBorrow) {
        return _getOptimalBorrows(state, glpDeposited, withUpdatedPoolAmounts);
    }

    ///@notice returns optimal borrows for BTC and ETH respectively basis glpDeposited amount and stored pool amount
    ///@param state set of all state variables of vault
    ///@param glpDeposited amount of glp for which optimal borrow needs to be calculated
    ///@return optimalBtcBorrow optimal amount of btc borrowed from AAVE
    ///@return optimalEthBorrow optimal amount of eth borrowed from AAVE
    function _getOptimalBorrows(
        State storage state,
        uint256 glpDeposited,
        bool withUpdatedPoolAmounts
    ) private view returns (uint256 optimalBtcBorrow, uint256 optimalEthBorrow) {
        optimalBtcBorrow = _getTokenReservesInGlp(state, address(state.wbtc), glpDeposited, withUpdatedPoolAmounts);
        optimalEthBorrow = _getTokenReservesInGlp(state, address(state.weth), glpDeposited, withUpdatedPoolAmounts);
    }

    ///@notice returns optimal borrows for BTC and ETH respectively basis available borrow amount
    ///@param state set of all state variables of vault
    ///@param availableBorrowAmount available borrow amount from senior vault
    ///@param usdcLiquidationThreshold the usdc liquidation threshold on AAVE
    ///@return optimalBtcBorrow optimal amount of btc borrowed from AAVE
    ///@return optimalEthBorrow optimal amount of eth borrowed from AAVE
    function getOptimalCappedBorrows(
        State storage state,
        uint256 availableBorrowAmount,
        uint256 usdcLiquidationThreshold
    ) external view returns (uint256 optimalBtcBorrow, uint256 optimalEthBorrow) {
        return _getOptimalCappedBorrows(state, availableBorrowAmount, usdcLiquidationThreshold);
    }

    ///@notice returns optimal borrows for BTC and ETH respectively basis available borrow amount
    ///@param state set of all state variables of vault
    ///@param availableBorrowAmount available borrow amount from senior vault
    ///@param usdcLiquidationThreshold the usdc liquidation threshold on AAVE
    ///@return optimalBtcBorrow optimal amount of btc borrowed from AAVE
    ///@return optimalEthBorrow optimal amount of eth borrowed from AAVE
    function _getOptimalCappedBorrows(
        State storage state,
        uint256 availableBorrowAmount,
        uint256 usdcLiquidationThreshold
    ) private view returns (uint256 optimalBtcBorrow, uint256 optimalEthBorrow) {
        // The value of max possible value of ETH+BTC borrow
        // calculated basis available borrow amount, liqudation threshold and target health factor
        // AAVE target health factor = (usdc supply value * usdc liquidation threshold)/borrow value
        // whatever tokens we borrow from AAVE (ETH/BTC) we sell for usdc and deposit that usdc into AAVE
        // assuming 0 slippage borrow value of tokens = usdc deposit value (this leads to very small variation in hf)
        // usdc supply value = usdc borrowed from senior tranche + borrow value
        // replacing usdc supply value formula above in AAVE target health factor formula
        // we can replace usdc borrowed from senior tranche with available borrow amount
        // we can derive max borrow value of tokens possible i.e. maxBorrowValue
        uint256 maxBorrowValue = availableBorrowAmount.mulDivDown(
            usdcLiquidationThreshold,
            state.targetHealthFactor - usdcLiquidationThreshold
        );

        uint256 btcWeight;
        uint256 ethWeight;

        // get eth and btc price in usdc
        uint256 btcPrice = _getTokenPriceInUsdc(state, state.wbtc);
        uint256 ethPrice = _getTokenPriceInUsdc(state, state.weth);

        {
            int128 btcTokenTraderOIHedge = state.btcTraderOIHedge;
            int128 ethTokenTraderOIHedge = state.ethTraderOIHedge;

            uint256 btcPoolAmount = state.btcPoolAmount;
            uint256 ethPoolAmount = state.ethPoolAmount;

            // token reserve is the amount we short
            // tokenTraderOIHedge if >0 then we need to go long because of OI hence less short (i.e. subtract if value +ve)
            // tokenTraderOIHedge if <0 then we need to go short because of OI hence more long (i.e. add if value -ve)
            uint256 btcTokenReserve = btcTokenTraderOIHedge > 0
                ? btcPoolAmount - uint256(int256(btcTokenTraderOIHedge))
                : btcPoolAmount + uint256(int256(-btcTokenTraderOIHedge));

            uint256 ethTokenReserve = ethTokenTraderOIHedge > 0
                ? ethPoolAmount - uint256(int256(ethTokenTraderOIHedge))
                : ethPoolAmount + uint256(int256(-ethTokenTraderOIHedge));

            // calculate the borrow value of eth & btc using their weights
            btcWeight = btcTokenReserve.mulDivDown(btcPrice, PRICE_PRECISION);
            ethWeight = ethTokenReserve.mulDivDown(ethPrice, PRICE_PRECISION);
        }

        // get token amounts from usdc amount
        // total borrow should be divided basis the token weights
        // using that we can calculate the borrow value for each token
        // dividing that with token prices we can calculate the optimal token borrow amounts
        optimalBtcBorrow = maxBorrowValue.mulDivDown(btcWeight * PRICE_PRECISION, (btcWeight + ethWeight) * btcPrice);
        optimalEthBorrow = maxBorrowValue.mulDivDown(ethWeight * PRICE_PRECISION, (btcWeight + ethWeight) * ethPrice);
    }

    ///@notice returns token amount underlying glp amount deposited and stored pool amount
    ///@param state set of all state variables of vault
    ///@param token address of token
    ///@param glpDeposited amount of glp for which underlying token amount is being calculated
    ///@return amount of tokens of the supplied address underlying the given amount of glp
    function getTokenReservesInGlp(
        State storage state,
        address token,
        uint256 glpDeposited,
        bool withUpdatedPoolAmounts
    ) external view returns (uint256) {
        return _getTokenReservesInGlp(state, token, glpDeposited, withUpdatedPoolAmounts);
    }

    ///@notice returns token amount underlying glp amount deposited and stored pool amount
    ///@param state set of all state variables of vault
    ///@param token address of token
    ///@param glpDeposited amount of glp for which underlying token amount is being calculated
    ///@return amount of tokens of the supplied address underlying the given amount of glp
    function _getTokenReservesInGlp(
        State storage state,
        address token,
        uint256 glpDeposited,
        bool withUpdatedPoolAmounts
    ) private view returns (uint256) {
        uint256 poolAmount = withUpdatedPoolAmounts
            ? state.gmxVault.poolAmounts(token)
            : (token == address(state.wbtc) ? state.btcPoolAmount : state.ethPoolAmount);

        uint256 totalSupply = state.glp.totalSupply();

        int128 tokenTraderOIHedge = token == address(state.wbtc) ? state.btcTraderOIHedge : state.ethTraderOIHedge;

        // token reserve is the amount we short
        // tokenTraderOIHedge if >0 then we need to go long because of OI hence less short (i.e. subtract if value +ve)
        // tokenTraderOIHedge if <0 then we need to go short because of OI hence more long (i.e. add if value -ve)
        uint256 tokenReserve = tokenTraderOIHedge > 0
            ? poolAmount - uint256(int256(tokenTraderOIHedge))
            : poolAmount + uint256(int256(-tokenTraderOIHedge));

        return tokenReserve.mulDivDown(glpDeposited, totalSupply);
    }

    ///@notice returns token amount underlying glp amount deposited
    ///@param state set of all state variables of vault
    ///@param optimalBorrow optimal borrow amount basis glp deposits
    ///@param currentBorrow current borrow amount from AAVE
    ///@return true if the difference is below allowed threshold else false
    function isWithinAllowedDelta(
        State storage state,
        uint256 optimalBorrow,
        uint256 currentBorrow
    ) external view returns (bool) {
        return _isWithinAllowedDelta(state, optimalBorrow, currentBorrow);
    }

    ///@notice returns token amount underlying glp amount deposited
    ///@param state set of all state variables of vault
    ///@param optimalBorrow optimal borrow amount basis glp deposits
    ///@param currentBorrow current borrow amount from AAVE
    ///@return true if the difference is below allowed threshold else false
    function _isWithinAllowedDelta(
        State storage state,
        uint256 optimalBorrow,
        uint256 currentBorrow
    ) private view returns (bool) {
        // calcualte the absolute difference between the optimal and current borrows
        // optimal borrow is what we should borrow from AAVE
        // curret borrow is what is already borrowed from AAVE
        uint256 diff = optimalBorrow > currentBorrow ? optimalBorrow - currentBorrow : currentBorrow - optimalBorrow;

        // if absolute diff < threshold return true
        // if absolute diff > threshold return false
        return diff <= uint256(state.rebalanceDeltaThresholdBps).mulDivDown(currentBorrow, MAX_BPS);
    }

    // ISwapRouter internal constant swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
    ///@notice swaps token into usdc
    ///@param state set of all state variables of vault
    ///@param token address of token
    ///@param tokenAmount token amount to be sold
    ///@param minUsdcAmount minimum amount of usdc required
    ///@return usdcReceived amount of usdc received on swap
    ///@return tokensUsed amount of tokens paid for swap
    function _swapToken(
        State storage state,
        address token,
        uint256 tokenAmount,
        uint256 minUsdcAmount
    ) internal returns (uint256 usdcReceived, uint256 tokensUsed) {
        ISwapRouter swapRouter = state.swapRouter;

        // path of the token swap
        bytes memory path = token == address(state.weth)
            ? SwapPath.generate({ tokenIn: state.weth, fee: 500, tokenOut: state.usdc, isExactIn: true })
            : SwapPath.generate({
                tokenIn: state.wbtc,
                feeIn: state.feeTierWethWbtcPool,
                tokenIntermediate: state.weth,
                feeOut: 500,
                tokenOut: state.usdc,
                isExactIn: true
            });

        // executes the swap on uniswap pool
        // exact input swap to convert exact amount of tokens into usdc
        ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
            path: path,
            recipient: address(this),
            deadline: block.timestamp,
            amountIn: tokenAmount,
            amountOutMinimum: minUsdcAmount
        });

        // since exact input swap tokens used = token amount passed
        tokensUsed = tokenAmount;
        usdcReceived = swapRouter.exactInput(params);

        emit TokenSwapped(token, address(state.usdc), tokenAmount, usdcReceived);
    }

    ///@notice swaps usdc into token
    ///@param state set of all state variables of vault
    ///@param isExactOut whether swap is exactOut
    ///@param token address of token
    ///@param tokenAmount token amount to be bought
    ///@param usdcAmount maximum amount of usdc that can be sold
    ///@return usdcPaid amount of usdc paid for swap
    ///@return tokensReceived amount of tokens received on swap
    function _swapUSDC(
        State storage state,
        bool isExactOut,
        address token,
        uint256 tokenAmount,
        uint256 usdcAmount
    ) internal returns (uint256 usdcPaid, uint256 tokensReceived) {
        ISwapRouter swapRouter = state.swapRouter;

        bytes memory path = token == address(state.weth)
            ? SwapPath.generate({ tokenIn: state.usdc, fee: 500, tokenOut: state.weth, isExactIn: !isExactOut })
            : SwapPath.generate({
                tokenIn: state.usdc,
                feeIn: 500,
                tokenIntermediate: state.weth,
                feeOut: state.feeTierWethWbtcPool,
                tokenOut: state.wbtc,
                isExactIn: !isExactOut
            });

        if (isExactOut) {
            ISwapRouter.ExactOutputParams memory params = ISwapRouter.ExactOutputParams({
                path: path,
                recipient: address(this),
                deadline: block.timestamp,
                amountOut: tokenAmount,
                amountInMaximum: usdcAmount
            });

            // since exact output swap tokensReceived = tokenAmount passed
            tokensReceived = tokenAmount;
            usdcPaid = swapRouter.exactOutput(params);

            emit TokenSwapped(address(state.usdc), token, usdcPaid, tokensReceived);
        } else {
            // executes the swap on uniswap pool
            // exact input swap to convert exact amount of tokens into usdc
            ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
                path: path,
                recipient: address(this),
                deadline: block.timestamp,
                amountIn: usdcAmount,
                amountOutMinimum: tokenAmount
            });

            tokensReceived = swapRouter.exactInput(params);
            usdcPaid = usdcAmount;

            emit TokenSwapped(address(state.usdc), token, usdcPaid, tokensReceived);
        }
    }

    function emitVaultState(State storage state, uint256 eventType) external {
        (uint256 currentBtc, uint256 currentEth) = _getCurrentBorrows(state);

        uint256 ts = state.glp.totalSupply();
        uint256 bal = state.fsGlp.balanceOf(address(this));

        emit VaultState(
            eventType,
            currentBtc,
            currentEth,
            (state.btcPoolAmount * bal) / ts,
            (state.ethPoolAmount * bal) / ts,
            (state.btcTraderOIHedge * bal.toInt256()) / ts.toInt256(),
            (state.ethTraderOIHedge * bal.toInt256()) / ts.toInt256(),
            _getGlpPriceInUsdc(state, false),
            bal,
            _totalAssets(state, false),
            state.dnUsdcDeposited,
            state.unhedgedGlpInUsdc,
            state.aUsdc.balanceOf(address(this)),
            state.aUsdc.balanceOf(address(state.dnGmxSeniorVault))
        );
    }
}

// SPDX-License-Identifier: agpl-3.0

pragma solidity >=0.8.0;

import { FullMath } from '@uniswap/v3-core/contracts/libraries/FullMath.sol';

/**
 * @title FeeSplitStrategy library
 * @notice Implements the calculation of the eth reward split depending on the utilization of reserve
 * @dev The model of interest rate is based on 2 slopes, one before the `OPTIMAL_UTILIZATION_RATE`
 * point of utilization and another from that one to 100%
 * @author adapted from https://github.com/aave/protocol-v2/blob/6f57232358af0fd41d9dcf9309d7a8c0b9aa3912/contracts/protocol/lendingpool/DefaultReserveInterestRateStrategy.sol
 **/

library FeeSplitStrategy {
    using FullMath for uint128;
    using FullMath for uint256;

    uint256 internal constant RATE_PRECISION = 1e30;

    struct Info {
        /**
         * @dev this constant represents the utilization rate at which the pool aims to obtain most competitive borrow rates.
         * Expressed in ray
         **/
        uint128 optimalUtilizationRate;
        // Base variable borrow rate when Utilization rate = 0. Expressed in ray
        uint128 baseVariableBorrowRate;
        // Slope of the variable interest curve when utilization rate > 0 and <= OPTIMAL_UTILIZATION_RATE. Expressed in ray
        uint128 variableRateSlope1;
        // Slope of the variable interest curve when utilization rate > OPTIMAL_UTILIZATION_RATE. Expressed in ray
        uint128 variableRateSlope2;
    }

    function getMaxVariableBorrowRate(Info storage feeStrategyInfo) internal view returns (uint256) {
        return
            feeStrategyInfo.baseVariableBorrowRate +
            feeStrategyInfo.variableRateSlope1 +
            feeStrategyInfo.variableRateSlope2;
    }

    /**
     * @dev Calculates the interest rates depending on the reserve's state and configurations.
     * NOTE This function is kept for compatibility with the previous DefaultInterestRateStrategy interface.
     * New protocol implementation uses the new calculateInterestRates() interface
     * @param availableLiquidity The liquidity available in the corresponding aToken
     * @param usedLiquidity The total borrowed from the reserve at a variable rate
     **/
    function calculateFeeSplit(
        Info storage feeStrategy,
        uint256 availableLiquidity,
        uint256 usedLiquidity
    ) internal view returns (uint256 feeSplitRate) {
        uint256 utilizationRate = usedLiquidity == 0
            ? 0
            : usedLiquidity.mulDiv(RATE_PRECISION, availableLiquidity + usedLiquidity);

        uint256 excessUtilizationRate = RATE_PRECISION - feeStrategy.optimalUtilizationRate;

        if (utilizationRate > feeStrategy.optimalUtilizationRate) {
            uint256 excessUtilizationRateRatio = (utilizationRate - feeStrategy.optimalUtilizationRate).mulDiv(
                RATE_PRECISION,
                excessUtilizationRate
            );

            feeSplitRate =
                feeStrategy.baseVariableBorrowRate +
                feeStrategy.variableRateSlope1 +
                feeStrategy.variableRateSlope2.mulDiv(excessUtilizationRateRatio, RATE_PRECISION);
        } else {
            feeSplitRate =
                feeStrategy.baseVariableBorrowRate +
                utilizationRate.mulDiv(feeStrategy.variableRateSlope1, feeStrategy.optimalUtilizationRate);
        }
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import { SafeCast } from '@uniswap/v3-core/contracts/libraries/SafeCast.sol';
import { TickMath } from '@uniswap/v3-core/contracts/libraries/TickMath.sol';
import { IUniswapV3Pool } from '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
import { IUniswapV3SwapCallback } from '@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol';

import { Path } from '@uniswap/v3-periphery/contracts/libraries/Path.sol';
import { PoolAddress } from '@uniswap/v3-periphery/contracts/libraries/PoolAddress.sol';
import { CallbackValidation } from '@uniswap/v3-periphery/contracts/libraries/CallbackValidation.sol';

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

/// @title Provides quotes for swaps
/// @notice Allows getting the expected amount out or amount in for a given swap without executing the swap
/// @dev These functions are not gas efficient and should _not_ be called on chain. Instead, optimistically execute
/// the swap and check the amounts in the callback.
library QuoterLib {
    using Path for bytes;
    using SafeCast for uint256;
    using Simulate for IUniswapV3Pool;

    address constant factory = 0x1F98431c8aD98523631AE4a59f267346ea31F984;

    function _getPool(address tokenA, address tokenB, uint24 fee) internal pure returns (IUniswapV3Pool) {
        return IUniswapV3Pool(PoolAddress.computeAddress(factory, PoolAddress.getPoolKey(tokenA, tokenB, fee)));
    }

    function _decodeFirstPool(
        bytes memory path,
        bool exactIn
    ) internal pure returns (IUniswapV3Pool pool, bool zeroForOne) {
        (address tokenA, address tokenB, uint24 fee) = path.decodeFirstPool();
        pool = _getPool(tokenA, tokenB, fee);
        zeroForOne = exactIn == (tokenA < tokenB);
    }

    function _quoteExactInputSingle(
        IUniswapV3Pool pool,
        bool zeroForOne,
        uint256 amountIn,
        uint160 sqrtPriceLimitX96,
        Simulate.State memory swapState
    ) internal view returns (uint256 amountOut) {
        (int256 amount0, int256 amount1) = pool.simulateSwap(
            zeroForOne,
            amountIn.toInt256(),
            sqrtPriceLimitX96 == 0
                ? (zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1)
                : sqrtPriceLimitX96,
            swapState
        );
        return zeroForOne ? uint256(-amount1) : uint256(-amount0);
    }

    function _quoteExactInput(
        bytes memory path,
        uint256 amountIn,
        Simulate.State[] memory swapStates
    ) internal view returns (uint256[] memory amounts, Simulate.State[] memory swapStatesEnd) {
        uint256 i = path.numPools();
        amounts = new uint256[](i + 1);
        if (swapStates.length == 0) {
            swapStates = new Simulate.State[](i);
        }
        amounts[i] = amountIn;
        while (true) {
            (IUniswapV3Pool pool, bool zeroForOne) = _decodeFirstPool(path, true);

            // the outputs of prior swaps become the inputs to subsequent ones
            --i;
            amounts[i] = _quoteExactInputSingle(pool, zeroForOne, amounts[i + 1], 0, swapStates[i]);

            // decide whether to continue or terminate
            if (i > 0) {
                path = path.skipToken();
            } else {
                break;
            }
        }
        return (amounts, swapStates);
    }

    function _quoteExactOutputSingle(
        IUniswapV3Pool pool,
        bool zeroForOne,
        uint256 amountOut,
        uint160 sqrtPriceLimitX96,
        Simulate.State memory swapState
    ) internal view returns (uint256 amountIn) {
        (int256 amount0, int256 amount1) = pool.simulateSwap(
            zeroForOne,
            -amountOut.toInt256(),
            sqrtPriceLimitX96 == 0
                ? (zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1)
                : sqrtPriceLimitX96,
            swapState
        );
        return zeroForOne ? uint256(amount0) : uint256(amount1);
    }

    function _quoteExactOutput(
        bytes memory path,
        uint256 amountOut,
        Simulate.State[] memory swapStates
    ) internal view returns (uint256[] memory amounts, Simulate.State[] memory swapStatesEnd) {
        uint256 i = path.numPools();
        amounts = new uint256[](i + 1);
        if (swapStates.length == 0) {
            swapStates = new Simulate.State[](i);
        }
        amounts[i] = amountOut;
        while (true) {
            (IUniswapV3Pool pool, bool zeroForOne) = _decodeFirstPool(path, false);

            // the inputs of prior swaps become the outputs of subsequent ones
            --i;
            amounts[i] = _quoteExactOutputSingle(pool, zeroForOne, amounts[i + 1], 0, swapStates[i]);

            // decide whether to continue or terminate
            if (i > 0) {
                path = path.skipToken();
            } else {
                break;
            }
        }
        return (amounts, swapStates);
    }

    function _getQuote(
        int256 tokenAmount,
        bytes memory swapPath,
        Simulate.State[] memory swapStates
    ) internal view returns (int256, int256, Simulate.State[] memory) {
        if (tokenAmount > 0) {
            // swap wbtc to usdc
            (uint256[] memory otherTokenAmounts, Simulate.State[] memory swapStatesEnd) = _quoteExactInput(
                swapPath,
                uint256(tokenAmount),
                swapStates
            );
            return (-int256(otherTokenAmounts[0]), int256(otherTokenAmounts[1]), swapStatesEnd); // pool looses usdc hence negative
        } else if (tokenAmount < 0) {
            // swap usdc to wbtc
            (uint256[] memory otherTokenAmounts, Simulate.State[] memory swapStatesEnd) = _quoteExactOutput(
                swapPath,
                uint256(-tokenAmount),
                swapStates
            );
            return (int256(otherTokenAmounts[0]), -int256(otherTokenAmounts[1]), swapStatesEnd); // pool gains usdc hence positive
        } else {
            return (0, 0, swapStates);
        }
    }

    function _quoteCombinedSwap(
        int256 btcAmountInBtcSwap,
        int256 ethAmountInEthSwap,
        bytes memory btcSellPath,
        bytes memory ethSellPath
    ) internal view returns (int256 usdcAmountInBtcSwap, int256 usdcAmountInEthSwap) {
        // btc swap
        int256 ethAmountInBtcSwap;
        Simulate.State[] memory swapStates;
        (usdcAmountInBtcSwap, ethAmountInBtcSwap, swapStates) = _getQuote(
            btcAmountInBtcSwap,
            btcSellPath, // WBTC_TO_USDC(state),
            swapStates
        );

        // ensure btcAmountInBtcSwap and usdcAmountInBtcSwap are of opposite sign when they are both non-zero
        assert(
            btcAmountInBtcSwap == 0 || usdcAmountInBtcSwap == 0 || btcAmountInBtcSwap > 0 != usdcAmountInBtcSwap > 0
        );

        // eth swap (also accounting for price change in btc swap)
        Simulate.State[] memory swapStates2 = new Simulate.State[](1);
        if (ethAmountInBtcSwap != 0) {
            swapStates2[0] = swapStates[0];
        }
        (usdcAmountInEthSwap, , ) = _getQuote(
            ethAmountInEthSwap,
            ethSellPath, // WETH_TO_USDC(state),
            swapStates2
        );

        // ensure ethAmountInEthSwap and usdcAmountInEthSwap are of opposite sign when they are both non-zero
        assert(
            ethAmountInEthSwap == 0 || usdcAmountInEthSwap == 0 || ethAmountInEthSwap > 0 != usdcAmountInEthSwap > 0
        );
    }

    function quoteCombinedSwap(
        int256 btcAmountInBtcSwap,
        int256 ethAmountInEthSwap,
        bytes memory btcSellPath,
        bytes memory ethSellPath
    ) external view returns (int256 usdcAmountInBtcSwap, int256 usdcAmountInEthSwap) {
        return _quoteCombinedSwap(btcAmountInBtcSwap, ethAmountInEthSwap, btcSellPath, ethSellPath);
    }
}

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

/// @title safe casting methods
/// @notice contains methods for safely casting between types
/// @author adapted from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeCast.sol

/**
 * @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.
 *
 */
library SafeCast {
    /// @notice Cast a uint256 to a uint128, revert on overflow
    /// @param y The uint256 to be downcasted
    /// @return z The downcasted integer, now type uint160
    function toUint128(uint256 y) internal pure returns (uint128 z) {
        unchecked {
            /* solhint-disable reason-string */
            require((z = uint128(y)) == y);
        }
    }

    /// @notice Cast a uint256 to a uint128, revert on overflow
    /// @param y The uint256 to be downcasted
    /// @return z The downcasted integer, now type uint160
    function toInt128(int256 y) internal pure returns (int128 z) {
        unchecked {
            /* solhint-disable reason-string */
            require((z = int128(y)) == y);
        }
    }

    /// @notice Cast a uint256 to a int256, revert on overflow
    /// @param y The uint256 to be casted
    /// @return z The casted integer, now type int256
    function toInt256(uint256 y) internal pure returns (int256 z) {
        unchecked {
            require(y < 2 ** 255, 'Overflow');
            z = int256(y);
        }
    }
}

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { FixedPointMathLib } from '@rari-capital/solmate/src/utils/FixedPointMathLib.sol';
import { SignedMathUpgradeable } from '@openzeppelin/contracts-upgradeable/utils/math/SignedMathUpgradeable.sol';

library SignedFixedPointMathLib {
    function sign(int256 a) internal pure returns (int256) {
        return a < 0 ? -1 : int256(1);
    }

    function mulDivDown(int256 x, uint256 y, uint256 denominator) internal pure returns (int256 z) {
        int256 _sign = sign(x);
        z = _sign * int256(FixedPointMathLib.mulDivDown(SignedMathUpgradeable.abs(x), y, denominator));
        if (_sign < 0) {
            z--;
        }
    }
}

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import { SwapMath } from '@uniswap/v3-core/contracts/libraries/SwapMath.sol';
import { SafeCast } from '@uniswap/v3-core/contracts/libraries/SafeCast.sol';
import { TickMath } from '@uniswap/v3-core/contracts/libraries/TickMath.sol';
import { TickBitmap } from '@uniswap/v3-core/contracts/libraries/TickBitmap.sol';
import { BitMath } from '@uniswap/v3-core/contracts/libraries/BitMath.sol';

import { IUniswapV3Pool } from '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';

/// @title Library for simulating swaps.
/// @notice By fully replicating the swap logic, we can make a static call to get a quote.
library Simulate {
    using SafeCast for uint256;

    struct Cache {
        // the lp fee of the pool
        uint24 fee;
        // the tick spacing of the pool
        int24 tickSpacing;
    }

    struct State {
        // the amount remaining to be swapped in/out of the input/output asset
        int256 amountSpecifiedRemaining;
        // the amount already swapped out/in of the output/input asset
        int256 amountCalculated;
        // current sqrt(price)
        uint160 sqrtPriceX96;
        // the tick associated with the current price
        int24 tick;
        // the current liquidity in range
        uint128 liquidity;
    }

    // copied from UniswapV3Pool to avoid pragma issues associated with importing it
    struct StepComputations {
        // the price at the beginning of the step
        uint160 sqrtPriceStartX96;
        // the next tick to swap to from the current tick in the swap direction
        int24 tickNext;
        // whether tickNext is initialized or not
        bool initialized;
        // sqrt(price) for the next tick (1/0)
        uint160 sqrtPriceNextX96;
        // how much is being swapped in in this step
        uint256 amountIn;
        // how much is being swapped out
        uint256 amountOut;
        // how much fee is being paid in
        uint256 feeAmount;
    }

    function simulateSwap(
        IUniswapV3Pool pool,
        bool zeroForOne,
        int256 amountSpecified,
        uint160 sqrtPriceLimitX96
    ) internal view returns (int256 amount0, int256 amount1) {
        State memory state;
        return simulateSwap(pool, zeroForOne, amountSpecified, sqrtPriceLimitX96, state);
    }

    function simulateSwap(
        IUniswapV3Pool pool,
        bool zeroForOne,
        int256 amountSpecified,
        uint160 sqrtPriceLimitX96,
        State memory state
    ) internal view returns (int256 amount0, int256 amount1) {
        require(amountSpecified != 0, 'AS');

        if (state.sqrtPriceX96 == 0) {
            (uint160 sqrtPriceX96, int24 tick, , , , , ) = pool.slot0();
            state.sqrtPriceX96 = sqrtPriceX96;
            state.tick = tick;
            state.liquidity = pool.liquidity();
        }

        require(
            zeroForOne
                ? sqrtPriceLimitX96 < state.sqrtPriceX96 && sqrtPriceLimitX96 > TickMath.MIN_SQRT_RATIO
                : sqrtPriceLimitX96 > state.sqrtPriceX96 && sqrtPriceLimitX96 < TickMath.MAX_SQRT_RATIO,
            'SPL'
        );

        Cache memory cache = Cache({ fee: pool.fee(), tickSpacing: pool.tickSpacing() });

        bool exactInput = amountSpecified > 0;

        state.amountSpecifiedRemaining = amountSpecified;
        state.amountCalculated = 0;

        while (state.amountSpecifiedRemaining != 0 && state.sqrtPriceX96 != sqrtPriceLimitX96) {
            StepComputations memory step;

            step.sqrtPriceStartX96 = state.sqrtPriceX96;

            (step.tickNext, step.initialized) = nextInitializedTickWithinOneWord(
                pool.tickBitmap,
                state.tick,
                cache.tickSpacing,
                zeroForOne
            );

            if (step.tickNext < TickMath.MIN_TICK) {
                step.tickNext = TickMath.MIN_TICK;
            } else if (step.tickNext > TickMath.MAX_TICK) {
                step.tickNext = TickMath.MAX_TICK;
            }

            step.sqrtPriceNextX96 = TickMath.getSqrtRatioAtTick(step.tickNext);

            (state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount) = SwapMath.computeSwapStep(
                state.sqrtPriceX96,
                (zeroForOne ? step.sqrtPriceNextX96 < sqrtPriceLimitX96 : step.sqrtPriceNextX96 > sqrtPriceLimitX96)
                    ? sqrtPriceLimitX96
                    : step.sqrtPriceNextX96,
                state.liquidity,
                state.amountSpecifiedRemaining,
                cache.fee
            );

            if (exactInput) {
                unchecked {
                    state.amountSpecifiedRemaining -= (step.amountIn + step.feeAmount).toInt256();
                }
                state.amountCalculated -= step.amountOut.toInt256();
            } else {
                unchecked {
                    state.amountSpecifiedRemaining += step.amountOut.toInt256();
                }
                state.amountCalculated += (step.amountIn + step.feeAmount).toInt256();
            }

            if (state.sqrtPriceX96 == step.sqrtPriceNextX96) {
                if (step.initialized) {
                    (, int128 liquidityNet, , , , , , ) = pool.ticks(step.tickNext);
                    unchecked {
                        if (zeroForOne) liquidityNet = -liquidityNet;
                    }

                    state.liquidity = liquidityNet < 0
                        ? state.liquidity - uint128(-liquidityNet)
                        : state.liquidity + uint128(liquidityNet);
                }

                unchecked {
                    state.tick = zeroForOne ? step.tickNext - 1 : step.tickNext;
                }
            } else if (state.sqrtPriceX96 != step.sqrtPriceStartX96) {
                // recompute unless we're on a lower tick boundary (i.e. already transitioned ticks), and haven't moved
                state.tick = TickMath.getTickAtSqrtRatio(state.sqrtPriceX96);
            }
        }

        (amount0, amount1) = zeroForOne == exactInput
            ? (amountSpecified - state.amountSpecifiedRemaining, state.amountCalculated)
            : (state.amountCalculated, amountSpecified - state.amountSpecifiedRemaining);
    }

    // This function replicates TickBitmap, but accepts a function pointer argument.
    // It's private because it's messy, and shouldn't be re-used.
    function nextInitializedTickWithinOneWord(
        function(int16) external view returns (uint256) self,
        int24 tick,
        int24 tickSpacing,
        bool lte
    ) private view returns (int24 next, bool initialized) {
        unchecked {
            int24 compressed = tick / tickSpacing;
            if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity

            if (lte) {
                (int16 wordPos, uint8 bitPos) = TickBitmap.position(compressed);
                // all the 1s at or to the right of the current bitPos
                uint256 mask = (1 << bitPos) - 1 + (1 << bitPos);
                uint256 masked = self(wordPos) & mask;

                // if there are no initialized ticks to the right of or at the current tick, return rightmost in the word
                initialized = masked != 0;
                // overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
                next = initialized
                    ? (compressed - int24(uint24(bitPos - BitMath.mostSignificantBit(masked)))) * tickSpacing
                    : (compressed - int24(uint24(bitPos))) * tickSpacing;
            } else {
                // start from the word of the next tick, since the current tick state doesn't matter
                (int16 wordPos, uint8 bitPos) = TickBitmap.position(compressed + 1);
                // all the 1s at or to the left of the bitPos
                uint256 mask = ~((1 << bitPos) - 1);
                uint256 masked = self(wordPos) & mask;

                // if there are no initialized ticks to the left of the current tick, return leftmost in the word
                initialized = masked != 0;
                // overflow/underflow is possible, but prevented externally by limiting both tickSpacing and tick
                next = initialized
                    ? (compressed + 1 + int24(uint24(BitMath.leastSignificantBit(masked) - bitPos))) * tickSpacing
                    : (compressed + 1 + int24(uint24(type(uint8).max - bitPos))) * tickSpacing;
            }
        }
    }
}

// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

import { IERC20Metadata } from '@openzeppelin/contracts/interfaces/IERC20Metadata.sol';

library SwapPath {
    function generate(
        IERC20Metadata tokenIn,
        uint24 fee,
        IERC20Metadata tokenOut,
        bool isExactIn
    ) internal pure returns (bytes memory) {
        if (isExactIn) {
            return abi.encodePacked(tokenIn, fee, tokenOut);
        } else {
            return abi.encodePacked(tokenOut, fee, tokenIn);
        }
    }

    function generate(
        IERC20Metadata tokenIn,
        uint24 feeIn,
        IERC20Metadata tokenIntermediate,
        uint24 feeOut,
        IERC20Metadata tokenOut,
        bool isExactIn
    ) internal pure returns (bytes memory) {
        if (isExactIn) {
            return abi.encodePacked(tokenIn, feeIn, tokenIntermediate, feeOut, tokenOut);
        } else {
            return abi.encodePacked(tokenOut, feeOut, tokenIntermediate, feeIn, tokenIn);
        }
    }
}

Settings
{
  "viaIR": true,
  "optimizer": {
    "enabled": true,
    "runs": 256
  },
  "metadata": {
    "bytecodeHash": "none",
    "useLiteralContent": true
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "libraries": {
    "contracts/libraries/DnGmxJuniorVaultManager.sol": {
      "DnGmxJuniorVaultManager": "0x3fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c"
    }
  }
}

Contract Security Audit

Contract ABI

API
[{"inputs":[],"name":"ArraysLengthMismatch","type":"error"},{"inputs":[],"name":"DepositCapExceeded","type":"error"},{"inputs":[],"name":"FlashloanNotInitiated","type":"error"},{"inputs":[],"name":"InvalidFeeBps","type":"error"},{"inputs":[],"name":"InvalidFeeRecipient","type":"error"},{"inputs":[],"name":"InvalidRebalance","type":"error"},{"inputs":[],"name":"InvalidRebalanceDeltaThresholdBps","type":"error"},{"inputs":[],"name":"InvalidRebalanceHfThresholdBps","type":"error"},{"inputs":[],"name":"InvalidRebalanceTimeThreshold","type":"error"},{"inputs":[],"name":"InvalidSlippageThresholdGmx","type":"error"},{"inputs":[],"name":"InvalidSlippageThresholdSwapBtc","type":"error"},{"inputs":[],"name":"InvalidSlippageThresholdSwapEth","type":"error"},{"inputs":[],"name":"InvalidTargetHealthFactor","type":"error"},{"inputs":[{"internalType":"int128","name":"btcTraderOIHedge","type":"int128"},{"internalType":"int128","name":"ethTraderOIHedge","type":"int128"}],"name":"InvalidTraderOIHedges","type":"error"},{"inputs":[],"name":"InvalidWithdrawFeeBps","type":"error"},{"inputs":[],"name":"NotBalancerVault","type":"error"},{"inputs":[],"name":"NotDnGmxSeniorVault","type":"error"},{"inputs":[{"internalType":"address","name":"msgSender","type":"address"},{"internalType":"address","name":"authorisedKeeperAddress","type":"address"}],"name":"OnlyKeeperAllowed","type":"error"},{"inputs":[{"internalType":"address","name":"msgSender","type":"address"},{"internalType":"address","name":"authorisedKeeperAddress","type":"address"}],"name":"OnlyTraderHedgeStrategyAllowed","type":"error"},{"inputs":[{"internalType":"uint256","name":"netSlippage","type":"uint256"},{"internalType":"uint256","name":"assets","type":"uint256"}],"name":"TooMuchSlippage","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"newKeeper","type":"address"},{"indexed":false,"internalType":"address","name":"dnGmxSeniorVault","type":"address"},{"indexed":false,"internalType":"uint256","name":"newDepositCap","type":"uint256"},{"indexed":false,"internalType":"address","name":"batchingManager","type":"address"},{"indexed":false,"internalType":"uint16","name":"withdrawFeeBps","type":"uint16"}],"name":"AdminParamsUpdated","type":"event"},{"anonymous":false,"inputs":[],"name":"AllowancesGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"slippage","type":"uint256"}],"name":"AssetSlippage","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"caller","type":"address"},{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":false,"internalType":"uint256","name":"assets","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"shares","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_newDepositCap","type":"uint256"}],"name":"DepositCapUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_dnGmxSeniorVault","type":"address"}],"name":"DnGmxSeniorVaultUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"EsGmxStaked","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"EsGmxVested","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"feeBps","type":"uint256"},{"indexed":false,"internalType":"address","name":"_newFeeRecipient","type":"address"}],"name":"FeeParamsUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"feeAmount","type":"uint256"}],"name":"FeesWithdrawn","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"GmxClaimed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract IBalancerVault","name":"vault","type":"address"},{"indexed":false,"internalType":"contract ISwapRouter","name":"swapRouter","type":"address"},{"indexed":false,"internalType":"uint256","name":"targetHealthFactor","type":"uint256"},{"indexed":false,"internalType":"contract IRewardsController","name":"aaveRewardsController","type":"address"},{"indexed":false,"internalType":"contract IPool","name":"pool","type":"address"},{"indexed":false,"internalType":"contract IPriceOracle","name":"oracle","type":"address"}],"name":"HedgeParamsUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint8","name":"version","type":"uint8"}],"name":"Initialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newKeeper","type":"address"}],"name":"KeeperUpdated","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"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint128","name":"rebalanceProfitUsdcAmountThreshold","type":"uint128"},{"indexed":false,"internalType":"contract IDnGmxTraderHedgeStrategy","name":"dnGmxTraderHedgeStrategy","type":"address"}],"name":"ParamsV1Updated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint32","name":"rebalanceTimeThreshold","type":"uint32"},{"indexed":false,"internalType":"uint16","name":"rebalanceDeltaThresholdBps","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"rebalanceHfThresholdBps","type":"uint16"}],"name":"RebalanceParamsUpdated","type":"event"},{"anonymous":false,"inputs":[],"name":"Rebalanced","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint16","name":"slippageThresholdSwapBtcBps","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"slippageThresholdSwapEthBps","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"slippageThresholdGmxBps","type":"uint16"},{"indexed":false,"internalType":"uint128","name":"usdcConversionThreshold","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"wethConversionThreshold","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"hedgeUsdcAmountThreshold","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"partialBtcHedgeUsdcAmountThreshold","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"partialEthHedgeUsdcAmountThreshold","type":"uint128"}],"name":"ThresholdsUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"int256","name":"btcTraderOIHedge","type":"int256"},{"indexed":false,"internalType":"int256","name":"ethTraderOIHedge","type":"int256"}],"name":"TraderOIHedgesUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"caller","type":"address"},{"indexed":true,"internalType":"address","name":"receiver","type":"address"},{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":false,"internalType":"uint256","name":"assets","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"shares","type":"uint256"}],"name":"Withdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_withdrawFeeBps","type":"uint256"}],"name":"WithdrawFeeUpdated","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"asset","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"claimVestedGmx","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"name":"convertToAssets","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"assets","type":"uint256"}],"name":"convertToShares","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"to","type":"address"}],"name":"deposit","outputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"depositCap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"dnUsdcDeposited","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getAdminParams","outputs":[{"internalType":"address","name":"keeper","type":"address"},{"internalType":"contract IDnGmxSeniorVault","name":"dnGmxSeniorVault","type":"address"},{"internalType":"uint256","name":"depositCap_","type":"uint256"},{"internalType":"uint16","name":"withdrawFeeBps","type":"uint16"},{"internalType":"uint24","name":"feeTierWethWbtcPool","type":"uint24"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentBorrows","outputs":[{"internalType":"uint256","name":"currentBtcBorrow","type":"uint256"},{"internalType":"uint256","name":"currentEthBorrow","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getHedgeParams","outputs":[{"internalType":"contract IBalancerVault","name":"balancerVault","type":"address"},{"internalType":"contract ISwapRouter","name":"swapRouter","type":"address"},{"internalType":"uint256","name":"targetHealthFactor","type":"uint256"},{"internalType":"contract IRewardsController","name":"aaveRewardsController","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"assetAmount","type":"uint256"}],"name":"getMarketValue","outputs":[{"internalType":"uint256","name":"marketValue","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"glpDeposited","type":"uint256"},{"internalType":"bool","name":"withUpdatedPoolAmounts","type":"bool"}],"name":"getOptimalBorrows","outputs":[{"internalType":"uint256","name":"optimalBtcBorrow","type":"uint256"},{"internalType":"uint256","name":"optimalEthBorrow","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bool","name":"maximize","type":"bool"}],"name":"getPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPriceX128","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getRebalanceParams","outputs":[{"internalType":"uint32","name":"rebalanceTimeThreshold","type":"uint32"},{"internalType":"uint16","name":"rebalanceDeltaThresholdBps","type":"uint16"},{"internalType":"uint16","name":"rebalanceHfThresholdBps","type":"uint16"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getThresholds","outputs":[{"internalType":"uint16","name":"slippageThresholdSwapBtcBps","type":"uint16"},{"internalType":"uint16","name":"slippageThresholdSwapEthBps","type":"uint16"},{"internalType":"uint16","name":"slippageThresholdGmxBps","type":"uint16"},{"internalType":"uint128","name":"usdcConversionThreshold","type":"uint128"},{"internalType":"uint128","name":"wethConversionThreshold","type":"uint128"},{"internalType":"uint128","name":"hedgeUsdcAmountThreshold","type":"uint128"},{"internalType":"uint128","name":"partialBtcHedgeUsdcAmountThreshold","type":"uint128"},{"internalType":"uint128","name":"partialEthHedgeUsdcAmountThreshold","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUsdcBorrowed","outputs":[{"internalType":"uint256","name":"usdcAmount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getVaultMarketValue","outputs":[{"internalType":"int256","name":"vaultMarketValue","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"grantAllowances","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"harvestFees","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"address","name":"_swapRouter","type":"address"},{"internalType":"address","name":"_rewardRouter","type":"address"},{"internalType":"address","name":"_mintBurnRewardRouter","type":"address"},{"components":[{"internalType":"contract IERC20Metadata","name":"weth","type":"address"},{"internalType":"contract IERC20Metadata","name":"wbtc","type":"address"},{"internalType":"contract IERC20Metadata","name":"sGlp","type":"address"},{"internalType":"contract IERC20Metadata","name":"usdc","type":"address"}],"internalType":"struct DnGmxJuniorVaultManager.Tokens","name":"_tokens","type":"tuple"},{"internalType":"contract IPoolAddressesProvider","name":"_poolAddressesProvider","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"isValidRebalance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"maxDeposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"}],"name":"maxMint","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"maxRedeem","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"maxWithdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"},{"internalType":"address","name":"to","type":"address"}],"name":"mint","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"assets","type":"uint256"}],"name":"previewDeposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"name":"previewMint","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"name":"previewRedeem","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"assets","type":"uint256"}],"name":"previewWithdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rebalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rebalanceProfit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"},{"internalType":"uint256[]","name":"amounts","type":"uint256[]"},{"internalType":"uint256[]","name":"feeAmounts","type":"uint256[]"},{"internalType":"bytes","name":"userData","type":"bytes"}],"name":"receiveFlashLoan","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"shares","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"address","name":"owner","type":"address"}],"name":"redeem","outputs":[{"internalType":"uint256","name":"assets","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newKeeper","type":"address"},{"internalType":"address","name":"dnGmxSeniorVault","type":"address"},{"internalType":"uint256","name":"newDepositCap","type":"uint256"},{"internalType":"uint16","name":"withdrawFeeBps","type":"uint16"},{"internalType":"uint24","name":"feeTierWethWbtcPool","type":"uint24"}],"name":"setAdminParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"_useDirectConversionBurn","type":"bool"},{"internalType":"bool","name":"_useDirectConversionMint","type":"bool"}],"name":"setDirectConversion","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint16","name":"_feeBps","type":"uint16"},{"internalType":"address","name":"_feeRecipient","type":"address"}],"name":"setFeeParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IGlpManager","name":"_glpManager","type":"address"}],"name":"setGmxParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IBalancerVault","name":"vault","type":"address"},{"internalType":"contract ISwapRouter","name":"swapRouter","type":"address"},{"internalType":"uint256","name":"targetHealthFactor","type":"uint256"},{"internalType":"contract IRewardsController","name":"aaveRewardsController","type":"address"}],"name":"setHedgeParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint128","name":"rebalanceProfitUsdcAmountThreshold","type":"uint128"},{"internalType":"contract IDnGmxTraderHedgeStrategy","name":"dnGmxTraderHedgeStrategy","type":"address"}],"name":"setParamsV1","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint32","name":"rebalanceTimeThreshold","type":"uint32"},{"internalType":"uint16","name":"rebalanceDeltaThresholdBps","type":"uint16"},{"internalType":"uint16","name":"rebalanceHfThresholdBps","type":"uint16"}],"name":"setRebalanceParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint16","name":"slippageThresholdSwapBtcBps","type":"uint16"},{"internalType":"uint16","name":"slippageThresholdSwapEthBps","type":"uint16"},{"internalType":"uint16","name":"slippageThresholdGmxBps","type":"uint16"},{"internalType":"uint128","name":"usdcConversionThreshold","type":"uint128"},{"internalType":"uint128","name":"wethConversionThreshold","type":"uint128"},{"internalType":"uint128","name":"hedgeUsdcAmountThreshold","type":"uint128"},{"internalType":"uint128","name":"partialBtcHedgeUsdcAmountThreshold","type":"uint128"},{"internalType":"uint128","name":"partialEthHedgeUsdcAmountThreshold","type":"uint128"}],"name":"setThresholds","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"stopVestAndStakeEsGmx","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalAssets","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unstakeAndVestEsGmx","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"assets","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"address","name":"owner","type":"address"}],"name":"withdraw","outputs":[{"internalType":"uint256","name":"shares","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"withdrawFees","outputs":[],"stateMutability":"nonpayable","type":"function"}]

6080806040523461001757615ef590816200001d8239f35b600080fdfe6080604052600436101561001257600080fd5b60003560e01c80624ec12b1461042157806301e1d1141461041c57806306fdde031461041757806307a2d13a14610412578063095ea7b31461040d5780630a28a477146104085780630ee31e9414610403578063138cc18f146103fe57806318160ddd146103f95780631f5edd8d146103f457806323b872dd146103ef57806330af302d146103ea578063313ce567146103e557806338d52e0f146103e057806339509351146103db5780633d672a45146103d65780633f4ba83a146103d1578063402d267d146103cc578063476343ee146103c757806347f24f4b146103c25780634cdad506146103bd578063520c5fd9146103b8578063554070bc146103b357806359f3fe22146103ae5780635c975abb146103a95780635c9a9b55146103a457806365eda5571461039f5780636e553f651461039a57806370a0823114610313578063715018a6146103955780637d7c2a1c1461039057806382100a501461038b5780638456cb59146103865780638d315d7b146103815780638da5cb5b1461037c57806394bf804d1461037757806395956cc11461037257806395d89b411461036d57806395e2da7c146103685780639c3977b514610363578063a457c2d71461035e578063a9059cbb14610359578063b3d7f6b914610354578063b460af941461034f578063b811ad701461034a578063ba08765214610345578063ba7cc8d114610340578063bb8234d11461033b578063bf9571f114610336578063c63d75b614610331578063c6e6f5921461032c578063cad42dca14610327578063ce96cb7714610322578063d12f13b71461031d578063d627b53014610318578063d905777e14610313578063dbd5edc71461030e578063dd62ed3e14610309578063e0120cf814610304578063e245b5af146102ff578063ec279a18146102fa578063edbcc599146102f5578063ef8b30f7146102f0578063f04f2707146102eb5763f2fde38b146102e657600080fd5b61376f565b613691565b613501565b6134be565b6134a0565b6133be565b6132c0565b61325b565b61323c565b61180c565b613217565b61317a565b61313c565b6130d9565b6130bb565b61308d565b612b7e565b612ad9565b612a49565b612919565b6127e9565b6126a6565b61265f565b612635565b612582565b612524565b612442565b61239b565b61223f565b612010565b611fe9565b611e5c565b611e02565b611db7565b6118a8565b61184a565b6116e0565b6114e1565b6112dd565b6112ba565b61125f565b611241565b6111f9565b6111db565b611034565b610f2d565b610f07565b610e73565b610d7b565b610d18565b610cf1565b610cd5565b610b6b565b610a89565b6108e4565b6108c6565b61085a565b6107c8565b610763565b61072e565b610710565b61062b565b6105b4565b610448565b6001600160a01b0381160361043757565b600080fd5b61ffff81160361043757565b346104375760a03660031901126104375760043561046581610426565b60243561047181610426565b604435916064356104818161043c565b6084359062ffffff8216820361043757610499613800565b61271061ffff8216116105975760fc80546001600160a01b0319166001600160a01b038581169190911790915561010586905561010480546dffffffffff0000000000000000001916604884901b6affff000000000000000000161760589490941b62ffffff60581b16939093179092557fe15ba51cde2870a515ffc9834f5ac1bb010e259ba47eda16968c5bc5be83cbf694610592926105569086166001600160a01b0361011c91166001600160601b0360a01b825416179055565b604051948594859293608092959461ffff9260a08601976001600160a01b03809216875216602086015260408501526000606085015216910152565b0390a1005b6040516312ee160b60e11b8152600490fd5b600091031261043757565b346104375760003660031901126104375760206105cf6151f3565b604051908152f35b919082519283825260005b848110610603575050826000602080949584010152601f8019910116010190565b6020818301810151848301820152016105e2565b9060206106289281815201906105d7565b90565b346104375760008060031936011261070d57604051908060365461064e816138a2565b808552916001918083169081156106e35750600114610688575b610684856106788187038261356a565b60405191829182610617565b0390f35b9250603683527f4a11f94e20a93c79f6ec743a1954ec4fc2c08429ae2122118bf234b2185c81b85b8284106106cb57505050810160200161067882610684610668565b805460208587018101919091529093019281016106b0565b8695506106849693506020925061067894915060ff191682840152151560051b8201019293610668565b80fd5b346104375760203660031901126104375760206105cf60043561557b565b346104375760403660031901126104375761075860043561074e81610426565b6024359033613ad6565b602060405160018152f35b3461043757602036600319011261043757602061078160043561571b565b50604051908152f35b9181601f840112156104375782359167ffffffffffffffff8311610437576020838186019501011161043757565b61012435906107c682610426565b565b34610437576101403660031901126104375767ffffffffffffffff600435818111610437576107fb90369060040161078a565b916024359081116104375761081490369060040161078a565b60443561082081610426565b6064359161082d83610426565b6084359361083a85610426565b60803660a319011261043757610858966108526107b8565b96613c03565b005b346104375760008060031936011261070d57733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c81813b1561070d5760405163a07ae7bb60e01b815260fc600482015291829060249082905af480156108c1576108b5575080f35b6108be90613535565b80f35b613d86565b34610437576000366003190112610437576020603554604051908152f35b346104375760008060031936011261070d576108fe613800565b61011861092161091582546001600160a01b031690565b6001600160a01b031690565b906040518092633671df2560e01b825281600460209586935afa9081156108c1578491610a6c575b5060405190634e71d92d60e01b82528382600481886001600160a01b038096165af19182156108c15784908693610a34575b506109ca610915600494957f0124e1b96155213f77e0118f51f6173eb3606f76d89ba9dfc6a00fc629cb7dee604051806109ba8a829190602083019252565b0390a1546001600160a01b031690565b6040516331e67c7160e01b815293849182905afa9182156108c1576108be948693610a05575b505060fd546001600160a01b03169116614a3b565b610a25929350803d10610a2d575b610a1d818361356a565b810190613d71565b9038806109f0565b503d610a13565b60049350610915610a5d6109ca92843d8611610a65575b610a55818361356a565b810190614a2c565b94505061097b565b503d610a4b565b610a839150833d8511610a2d57610a1d818361356a565b38610949565b3461043757606036600319011261043757600435610aa681610426565b602435610ab281610426565b604435906001600160a01b0383166000526034602052610ae9336040600020906001600160a01b0316600052602052604060002090565b549260018401610b0a575b610afe93506139b2565b60405160018152602090f35b828410610b2657610b2183610afe95033383613ad6565b610af4565b60405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e63650000006044820152606490fd5b346104375760603660031901126104375760043563ffffffff8116908181036104375760243590610b9b8261043c565b6203f48060443593610bac8561043c565b610bb4613800565b11610cc45761271061ffff8181851611610cb2578416908110908115610ca5575b50610c935761010680546bffffffff00000000000000001916604083901b6bffffffff0000000000000000161790557f74666b2773815375e6daaf777fe5b0b6f04a4a15373485cfaf2cf48251b21d9e9261059290610106805461ffff60601b1916606086901b61ffff60601b16179055610106805467ffff0000000000001916603083901b67ffff000000000000161790556040519384938491604091949363ffffffff606085019616845261ffff809216602085015216910152565b60405163149d3c7560e21b8152600490fd5b9050610100541038610bd5565b60405163fd24448b60e01b8152600490fd5b6040516228254960e21b8152600490fd5b3461043757600036600319011261043757602060405160128152f35b346104375760003660031901126104375760206001600160a01b0360655416604051908152f35b3461043757604036600319011261043757600435610d3581610426565b336000526034602052610d5f816040600020906001600160a01b0316600052602052604060002090565b546024358101809111610d76576107589133613ad6565b6138dc565b3461043757600036600319011261043757610da2610915610117546001600160a01b031690565b60405163019c8a3b60e11b8152600060048201526020918290829060249082905afa80156108c157600491600091610e56575b5082610ded61091561010a546001600160a01b031690565b6040516318160ddd60e01b815293849182905afa9283156108c15761068493610e2993610e2392600092610e39575b505061524e565b906152dd565b6040519081529081906020820190565b610e4f9250803d10610a6557610a55818361356a565b3880610e1c565b610e6d9150833d8511610a6557610a55818361356a565b38610dd5565b3461043757600036600319011261043757610e8c613800565b60ca5460ff811615610ecb5760ff191660ca557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a1005b60405162461bcd60e51b815260206004820152601460248201527314185d5cd8589b194e881b9bdd081c185d5cd95960621b6044820152606490fd5b3461043757602036600319011261043757610f23600435610426565b60206105cf615472565b346104375760008060031936011261070d57610fa86101018281549155602081610f696001600160a01b0361010c54166001600160a01b031690565b60fd546001600160a01b03168660405180978195829463a9059cbb60e01b845260048401602090939291936001600160a01b0360408201951681520152565b03925af19081156108c1577f9800e6f57aeb4360eaa72295a820a4293e1e66fbfcabcd8874ae141304a76deb92610fef92610ff5575b506040519081529081906020820190565b0390a180f35b6110159060203d811161101c575b61100d818361356a565b810190614953565b5038610fde565b503d611003565b6001600160801b0381160361043757565b3461043757610100366003190112610437576004356110528161043c565b60243561105e8161043c565b6044359161106b8361043c565b6064359261107884611023565b60843561108481611023565b60a43561109081611023565b60c4359061109d82611023565b60e435926110aa84611023565b6110b2613800565b61271061ffff81818916116111c95781818a16116111b7578616116111a55761010680546fffffffffffffffffffffffffffffffff60701b607084901b167fffff00000000000000000000000000000000ffffffffffffffff00000000000090911663ffff000060108a901b161765ffff0000000060208b901b161761ffff881617179055608082901b6001600160801b0319166001600160801b03891617610107557f8405afe6e9767f91c282339dc75edd776ddeda3e9aad8e1bb15471db89f8d8679761059295608086901b6001600160801b0319166001600160801b0386161761010855604051988998896124d5565b6040516378fd03f760e01b8152600490fd5b604051632722719960e21b8152600490fd5b60405163669169e560e01b8152600490fd5b3461043757602036600319011261043757602061078160043561586a565b346104375760203660031901126104375760043561121681610426565b61121e613800565b6001600160a01b0361011791166001600160601b0360a01b825416179055600080f35b346104375760203660031901126104375760206105cf60043561536f565b346104375760003660031901126104375760a06001600160a01b0362ffffff8160fc54169161011c54166101055461010454916040519485526020850152604084015261ffff8160481c16606084015260581c166080820152f35b3461043757600036600319011261043757602060ff60ca54166040519015158152f35b34610437576080366003190112610437576004356112fa81610426565b60243561130681610426565b6044359160643561131681610426565b61131e613800565b614e2084116114cf5761011b80546001600160a01b0319166001600160a01b03841617905561011a80546001600160a01b0319166001600160a01b0385161790556113698461010055565b61011380546001600160a01b0319166001600160a01b03831617905561139b610915610114546001600160a01b031690565b936040519463026b1d5f60e01b86526020928387600481855afa80156108c1576004976000916114b2575b5061010e80546001600160a01b0319166001600160a01b03928316908117909155928590604051631f94a27560e31b8152998a9182905afa80156108c1577f04a2e4917e8cf09fc5f74a42cb8b240cb288a69c78dbf02719e5c7bf9ca1561e9861059296600092611495575b5050169261145a846001600160a01b0361011291166001600160601b0360a01b825416179055565b604080516001600160a01b0397881681529787166020890152870152841660608601528316608085015290911660a0830152819060c0820190565b6114ab9250803d10610a2d57610a1d818361356a565b3880611432565b6114c99150853d8711610a2d57610a1d818361356a565b386113c6565b60405163361804a960e21b8152600490fd5b346104375760008060031936011261070d576114fb613800565b61011861151261091582546001600160a01b031690565b9060409182518091633671df2560e01b825281600460209485935afa9283156108c157859182946116c1575b506001600160a01b03809416803b156116bd5782906004875180958193633ccfd60b60e01b83525af19081156108c15761158992610915926116aa575b50546001600160a01b031690565b918351630d43254f60e31b81528281600481875afa9182156108c1578391879361168b575b5085516370a0823160e01b81523060048201529283916024918391165afa9182156108c157859261166e575b5050813b1561166a578251633be3166560e21b8152600481018290529184908390602490829084905af180156108c1576108be9361164b937f5663ef0eef5f33e3884097c3ec29a0bfbc957992d83512d046b5fc3d35bd8f3e92611651575b5051828152602090a1610102546138f2565b61010255565b8061165e61166492613535565b806105a9565b38611639565b8380fd5b6116849250803d10610a6557610a55818361356a565b38806115da565b6116a3919350823d8411610a2d57610a1d818361356a565b91386115ae565b8061165e6116b792613535565b3861157b565b8280fd5b6116d9919450833d8511610a2d57610a1d818361356a565b923861153e565b346104375760403660031901126104375760243560043561170082610426565b6117086149a9565b6117106159cb565b602061171b826155fb565b90507f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a0177596001600160a01b038516928392604051908152a261175b826155fb565b509182156117d9576117958361068495611790846117876109156109156065546001600160a01b031690565b30903390614eba565b614df6565b604080519182526020820184905233917fdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d79190a36117d1615dc7565b610e29615925565b60405162461bcd60e51b815260206004820152600b60248201526a5a45524f5f53484152455360a81b6044820152606490fd5b34610437576020366003190112610437576001600160a01b0360043561183181610426565b1660005260336020526020604060002054604051908152f35b346104375760008060031936011261070d57611864613800565b806001600160a01b036098546001600160601b0360a01b8116609855167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b346104375760008060031936011261070d576001600160a01b0360fc5416803303611d9957506118dd6118d9614c38565b1590565b611d8757604080517fc741dbaad15a4f298fe8d80943fa8e005e7bcb2f5b0a0c8dec1fc35be457f1468380a1733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c637e51b4db60e01b825260049183818061193f8682019060fc602083019252565b0381855af49081156108c1576119a39186918791611d54575b506119859061011e805490916001600160801b039190911660809190911b6001600160801b031916179055565b6001600160801b0361011e91166001600160801b0319825416179055565b82516322b3d72160e11b81528381806119c38682019060fc602083019252565b0381855af480156108c157611a269186908792611d22575b506119ff9061011f906001600160801b0319825416906001600160801b0316179055565b61011f805490916001600160801b0390911660809190911b6001600160801b031916179055565b8251916373c2549d60e11b91828452848480611a498582019060fc602083019252565b0381845af49283156108c15786948794611cfd575b50855163199aa16760e21b815260fc8482019081526020818101889052604082018790529591908690829081906060010381875af49081156108c1578991611ce0575b50611aaa61597d565b833b15611cdc57875163a07ae7bb60e01b8152898180611ad18982019060fc602083019252565b0381885af480156108c157611cc9575b5088843b1561070d578851635941106d60e01b815260fc878201908152602081019390935291829081906040010381875af480156108c157611cb6575b50865195633859f9b960e21b8752858780611b478882019060006020604084019360fc81520152565b0381875af49687156108c1578997611c93575b50908591611b998951988993849363a999987d60e01b8552898501608091949360019360a083019660fc84526020840152604083015260608201520152565b0381855af480156108c157611bf49587958992611c76575b505015611c4457610104805466ffffffffffff0019169055611bd86118d960ca5460ff1690565b611c37575b8351809581948293835282019060fc602083019252565b03915af480156108c157611c0c575b826108be615925565b81611c2b92903d10611c30575b611c23818361356a565b810190614c22565b611c03565b503d611c19565b611c3f614968565b611bdd565b610104805466ffffffffffff0019164260081b66ffffffffffff001617905560ca5460ff1615611bdd57611c3f6149ed565b611c8c9250803d1061101c5761100d818361356a565b3880611bb1565b8692919750611cae90833d8511610a6557610a55818361356a565b969091611b5a565b8061165e611cc392613535565b38611b1e565b8061165e611cd692613535565b38611ae1565b8880fd5b611cf79150863d8811610a6557610a55818361356a565b38611aa1565b909350611d18919450853d8711611c3057611c23818361356a565b9390939238611a5e565b6119ff9250611d479150863d8811611d4d575b611d3f818361356a565b810190614dd5565b916119db565b503d611d35565b6119859250611d799150863d8811611d80575b611d71818361356a565b810190614da2565b9091611958565b503d611d67565b60405163a039e79960e01b8152600490fd5b604490604051906329b7946d60e01b82523360048301526024820152fd5b346104375760003660031901126104375760806001600160a01b038061011b5416908061011a5416906101005490610113541691604051938452602084015260408301526060820152f35b3461043757600036600319011261043757611e1b613800565b611e236149a9565b600160ff1960ca54161760ca557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a1005b346104375760008060031936011261070d57604080516373c2549d60e11b815260fc6004820152733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8282602481845af49182156108c15784908593611fc7575b50835163199aa16760e21b815260fc600482015260248101919091526044810192909252602091908290829060649082905af49081156108c1578491611faa575b50611f08610915610109546001600160a01b031690565b83516370a0823160e01b815230600482015294908390869060249082905afa80156108c157611f61611f58611f53611f729361068499611f7e98611f789792611f8d575b505061536f565b615433565b60fe54906153fe565b611f6c60ff54615433565b906153fe565b91615433565b9061541a565b90519081529081906020820190565b611fa39250803d10610a6557610a55818361356a565b3880611f4c565b611fc19150823d8411610a6557610a55818361356a565b38611ef1565b9050611fe1919250833d8511611c3057611c23818361356a565b919038611eb0565b346104375760003660031901126104375760206001600160a01b0360985416604051908152f35b3461043757604080600319360112610437576004356024359161203283610426565b61203a6149a9565b6120426159cb565b61204b8261567f565b9390506001600160a01b03926120a781858416938551978852847f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a0177596020809aa26117906120978361567f565b5080986065541630903390614eba565b8251848152602081019190915233907fdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d790604090a36120e46151f3565b610105541061222f5780516373c2549d60e11b815260fc600482015292733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8285602481845af480156108c157600095869161220c575b508351633859f9b960e21b815260fc600482015260006024820152918383604481845af49182156108c1576121a39785946000946121ed575b5086519889948593849363a999987d60e01b855260048501608091949360009360a083019660fc84526020840152604083015260608201520152565b03915af49384156108c157610684946121cf575b50506121c1615925565b519081529081906020820190565b816121e592903d1061101c5761100d818361356a565b5038806121b7565b612205919450853d8711610a6557610a55818361356a565b9238612167565b9050612226919550833d8511611c3057611c23818361356a565b9490943861212e565b516324d758c360e21b8152600490fd5b346104375760008060031936011261070d57612259613800565b6101025461011861227461091582546001600160a01b031690565b90813b1561166a576040516364f6446760e01b8152600481018490529184908390602490829084905af19182156108c1576109156020926122c4926004956116aa5750546001600160a01b031690565b604051633671df2560e01b815292839182905afa80156108c1576001600160a01b0391849161237d575b501690813b156116bd5760405163b6b55f2560e01b8152600481018290529183908390602490829084905af19081156108c1577f275563740cc51fbb46511cec625978872b9c5093b106db5ce55e071e57f249619261235c9261236a57506040519081529081906020820190565b0390a16108be600061010255565b8061165e61237792613535565b38610fde565b612395915060203d8111610a2d57610a1d818361356a565b386122ee565b346104375760008060031936011261070d5760405190806037546123be816138a2565b808552916001918083169081156106e357506001146123e757610684856106788187038261356a565b9250603783527f42a7b7dd785cd69714a189dffb3fd7d7174edc9ece837694ce50f7078f7c31ae5b82841061242a57505050810160200161067882610684610668565b8054602085870181019190915290930192810161240f565b346104375760003660031901126104375761010f546040516370a0823160e01b815230600482015290602090829060249082906001600160a01b03165afa9081156108c1576124ac6124a361068493610e29936000916124b7575b50615433565b60fe549061541a565b611f7860ff54615433565b6124cf915060203d8111610a6557610a55818361356a565b3861249d565b61ffff9182168152918116602083015290911660408201526001600160801b039182166060820152918116608083015291821660a082015291811660c083015290911660e08201526101000190565b34610437576000366003190112610437576101065461010754610684610108546040519384936001600160801b039061ffff92828560801c9516938260801c93808360701c1693169181811691808260201c169160101c16896124d5565b346104375760403660031901126104375760043561259f81610426565b602435903360005260346020526125cd816040600020906001600160a01b0316600052602052604060002090565b54918083106125e257610afe92039033613ad6565b60405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77604482015264207a65726f60d81b6064820152608490fd5b346104375760403660031901126104375761075860043561265581610426565b60243590336139b2565b3461043757602036600319011261043757602061078160043561567f565b6060906003190112610437576004359060243561269981610426565b9060443561062881610426565b34610437576106846117d16126ba3661267d565b9291906126c56149a9565b6126cd6159cb565b60206126d88361571b565b90506001600160a01b03907f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a017759828816938492604051908152a261273661271d8561571b565b508097843303612792575b61273187615c91565b614f2d565b60408051858152602081018890529184169133917ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db9190819081015b0390a461278d6109156109156065546001600160a01b031690565b614a3b565b846000526034602052816127bd336040600020906001600160a01b0316600052602052604060002090565b54600181016127ce575b5050612728565b6127e2916127db91614f20565b3383613ad6565b81386127c7565b346104375760008060031936011261070d57612803613800565b604080516373c2549d60e11b815260fc6004820152733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8282602481845af49182156108c15761287c92602091869087926128f9575b50855163199aa16760e21b815260fc60048201526024810191909152604481019190915292839081906064820190565b0381845af49081156108c157849283926128d9575b50803b156116bd579251635941106d60e01b815260fc6004820152602481019190915291829060449082905af480156108c1576128cc575080f35b8061165e6108be92613535565b6128f291925060203d8111610a6557610a55818361356a565b9038612891565b90506129129150853d8711611c3057611c23818361356a565b903861284c565b34610437576106846117d161292d3661267d565b906129399392936149a9565b6129416159cb565b602061294c8561586a565b90506001600160a01b03907f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a017759828616938492604051908152a28133036129f2575b856129b261299a8261586a565b509788966129a988151561505f565b61273188615c91565b6040805186815260208101929092529184169133917ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db9181908101612772565b81600052603460205285612a1d336040600020906001600160a01b0316600052602052604060002090565b5460018101612a2e575b505061298d565b612a4291612a3b91614f20565b3386613ad6565b8538612a27565b3461043757600036600319011261043757604080516373c2549d60e11b815260fc60048201529081602481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c1576000908192612aae575b50604080519182526020820192909252f35b9050612ac8915060403d8111611c3057611c23818361356a565b9038612a9c565b8015150361043757565b346104375760408060031936011261043757602435612af781612acf565b8151906303f7ab5160e61b825260fc60048301526004356024830152151560448201528181606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49182156108c1576000918293612b59575b50519081526020810191909152604090f35b81612b7692945061068493503d8511611c3057611c23818361356a565b929091612b47565b346104375760008060031936011261070d57612b98613800565b612bae61091561010e546001600160a01b031690565b612bc461091561011a546001600160a01b031690565b9061010d91612bdd61091584546001600160a01b031690565b9160409081519363095ea7b360e01b948581526020968782600494818c81612c1f8a8a83019190916001600160a01b0360408201931681526020600019910152565b03925af19081156108c157612c44926109159261303d5750546001600160a01b031690565b8684518092888252818b81612c738b8983019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157613070575b5061010c612c9a61091582546001600160a01b031690565b8785518092898252818c81612cc98a8a83019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157613053575b50612ced61091582546001600160a01b031690565b908785518093898252818c81612d1d8c8a83019190916001600160a01b0360408201931681526020600019910152565b03925af19081156108c157612d42926109159261303d5750546001600160a01b031690565b91612d8c8761011794612d5f61091587546001600160a01b031690565b90898c895180968195829483528a83019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157613020575b50612de48761010b92612db861091585546001600160a01b031690565b898c895180968195829483528a83019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c15789968992612e3c92613003575b50612e1061091585546001600160a01b031690565b9087519889809481938d83528983019190916001600160a01b0360408201931681526020600019910152565b03925af19081156108c157612e67610915612ea6928b988b95612fe65750546001600160a01b031690565b612e7b61091586546001600160a01b031690565b86519788809481938c83528883019190916001600160a01b0360408201931681526020600019910152565b03925af19384156108c1578794612fc9575b5085612ee9612ed361091561010f546001600160a01b031690565b612e7b61091561011c546001600160a01b031690565b03925af180156108c157612f579587958992612fac575b50612f2d610915612f1f6109156109156065546001600160a01b031690565b95546001600160a01b031690565b945196879586948593845283019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157612f8e575b827fba5bb3f899c7a3edcc9ff9d46c4e08449c6a608b6f8254132bc5af4898645cbc8180a180f35b81612fa492903d1061101c5761100d818361356a565b503880612f66565b612fc290873d891161101c5761100d818361356a565b5038612f00565b612fdf90873d891161101c5761100d818361356a565b5038612eb8565b612ffc90863d881161101c5761100d818361356a565b503861157b565b61301990843d861161101c5761100d818361356a565b5038612dfb565b61303690883d8a1161101c5761100d818361356a565b5038612d9b565b612ffc908a3d8c1161101c5761100d818361356a565b61306990883d8a1161101c5761100d818361356a565b5038612cd8565b61308690873d891161101c5761100d818361356a565b5038612c82565b34610437576020366003190112610437576130a9600435610426565b60206105cf6130b6615472565b6154f9565b346104375760203660031901126104375760206105cf6004356154f9565b34610437576040366003190112610437576004356130f681612acf565b6024359061310382612acf565b61310b613800565b61011d805461ffff60a01b191691151560a01b60ff60a01b169190911791151560a81b60ff60a81b16919091179055005b34610437576020366003190112610437576001600160a01b0360043561316181610426565b16600052603360205260206105cf60406000205461557b565b34610437576040366003190112610437577f0ad868c06d6009b56f49b7d8396d0a6fc92a3474593d2bab6c0382dff84e9d4c60406004356131ba81611023565b6001600160a01b036001600160801b03602435926131d784610426565b6131df613800565b1691610121836001600160801b031982541617905516610120816001600160601b0360a01b82541617905582519182526020820152a1005b34610437576000366003190112610437576020613232614c38565b6040519015158152f35b3461043757600036600319011261043757602061010554604051908152f35b346104375760403660031901126104375760206132b760043561327d81610426565b6001600160a01b036024359161329283610426565b16600052603483526040600020906001600160a01b0316600052602052604060002090565b54604051908152f35b34610437576040366003190112610437576004356132dd8161043c565b6024356132e981610426565b6132f1613800565b60fd546001600160a01b038281169116146133ac5760fd80546001600160a01b0319166001600160a01b038316179055610bb861ffff83161161339a57610104805468ffff000000000000001916603884901b68ffff00000000000000161790557feecc969cd8da3140016e16b0603215de782af59713abfc5055d917b88f4a7b91916040805161ffff90921682526001600160a01b0390921660208201529081908101610592565b604051638bff87cf60e01b8152600490fd5b604051630ed1b8b360e31b8152600490fd5b34610437576020806003193601126104375761341b816004356133e081612acf565b6133f6610915610117546001600160a01b031690565b60405163019c8a3b60e11b815291151560048301529092839190829081906024820190565b03915afa80156108c157600491600091613483575b508261344861091561010a546001600160a01b031690565b6040516318160ddd60e01b815293849182905afa9283156108c15761068493610e299361347d92600092610e3957505061524e565b90615297565b61349a9150833d8511610a6557610a55818361356a565b38613430565b3461043757600036600319011261043757602060fe54604051908152f35b3461043757600036600319011261043757610106546040805163ffffffff83831c16815261ffff606084811c8216602084015260309490941c1691810191909152f35b346104375760203660031901126104375760206107816004356155fb565b634e487b7160e01b600052604160045260246000fd5b67ffffffffffffffff811161354957604052565b61351f565b6040810190811067ffffffffffffffff82111761354957604052565b90601f8019910116810190811067ffffffffffffffff82111761354957604052565b604051906101e0820182811067ffffffffffffffff82111761354957604052565b67ffffffffffffffff81116135495760051b60200190565b81601f82011215610437578035916135dc836135ad565b926135ea604051948561356a565b808452602092838086019260051b820101928311610437578301905b828210613614575050505090565b81358152908301908301613606565b67ffffffffffffffff811161354957601f01601f191660200190565b92919261364b82613623565b91613659604051938461356a565b829481845281830111610437578281602093846000960137010152565b9080601f83011215610437578160206106289335910161363f565b346104375760803660031901126104375767ffffffffffffffff60043581811161043757366023820112156104375780600401356136ce816135ad565b916136dc604051938461356a565b81835260209160248385019160051b8301019136831161043757602401905b8282106137565785856024358281116104375761371c9036906004016135c5565b604435838111610437576137349036906004016135c5565b9060643593841161043757613750610858943690600401613676565b92615099565b838091833561376481610426565b8152019101906136fb565b346104375760203660031901126104375760043561378c81610426565b613794613800565b6001600160a01b038116156137ac5761085890613858565b60405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608490fd5b6001600160a01b0360985416330361381457565b606460405162461bcd60e51b815260206004820152602060248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152fd5b609854906001600160a01b0380911691826001600160601b0360a01b821617609855167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0600080a3565b90600182811c921680156138d2575b60208310146138bc57565b634e487b7160e01b600052602260045260246000fd5b91607f16916138b1565b634e487b7160e01b600052601160045260246000fd5b91908201809211610d7657565b1561390657565b60405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b6064820152608490fd5b1561395e57565b60405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e7420657863656564732062604482015265616c616e636560d01b6064820152608490fd5b91906001600160a01b0390818416928315613a8357613a61827fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef94613a7e9416966139fe8815156138ff565b613a4784613a1f836001600160a01b03166000526033602052604060002090565b54613a2c82821015613957565b03916001600160a01b03166000526033602052604060002090565b556001600160a01b03166000526033602052604060002090565b613a6c8282546138f2565b90556040519081529081906020820190565b0390a3565b60405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f206164604482015264647265737360d81b6064820152608490fd5b906001600160a01b0391828116928315613bb2578216938415613b625780613b517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92594613b39613a7e956001600160a01b03166000526034602052604060002090565b906001600160a01b0316600052602052604060002090565b556040519081529081906020820190565b60405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b6064820152608490fd5b60405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b6064820152608490fd5b95939196949290966000549760ff8960081c16158099819a613d27575b8115613d07575b5015613cab57613c4d9789613c44600160ff196000541617600055565b613c9257613f00565b613c5357565b613c6361ff001960005416600055565b604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb384740249890602090a1565b613ca661010061ff00196000541617600055565b613f00565b60405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b6064820152608490fd5b303b15915081613d19575b5038613c27565b6001915060ff161438613d12565b600160ff8216109150613c20565b60e43561062881610426565b60a43561062881610426565b60c43561062881610426565b6101043561062881610426565b51906107c682610426565b90816020910312610437575161062881610426565b6040513d6000823e3d90fd5b9190826020910312610437576040516020810181811067ffffffffffffffff8211176135495760405291518252565b51906107c682611023565b519064ffffffffff8216820361043757565b51906107c68261043c565b6101e08183031261043757613e06613dff61358c565b9282613d92565b8252613e1460208201613dc1565b6020830152613e2560408201613dc1565b6040830152613e3660608201613dc1565b6060830152613e4760808201613dc1565b6080830152613e5860a08201613dc1565b60a0830152613e6960c08201613dcc565b60c0830152613e7a60e08201613dde565b60e0830152610100613e8d818301613d66565b90830152610120613e9f818301613d66565b90830152610140613eb1818301613d66565b90830152610160613ec3818301613d66565b90830152610180613ed5818301613dc1565b908301526101a0613ee7818301613dc1565b90830152613ef96101c0809201613dc1565b9082015290565b613ff1949161405896989794613f4561401295613f3d613f4b95613f2261460a565b613f2a61462d565b613f35610915613d35565b95369161363f565b92369161363f565b9161464e565b613f76613f56613d41565b6001600160a01b0361010c91166001600160601b0360a01b825416179055565b613fa1613f81613d4d565b6001600160a01b0361010d91166001600160601b0360a01b825416179055565b613fcc613fac613d59565b6001600160a01b0361010b91166001600160601b0360a01b825416179055565b61011a80546001600160a01b0319166001600160a01b03928316179055958693849190565b166001600160a01b0361011891166001600160601b0360a01b825416179055565b1691614038836001600160a01b0361011991166001600160601b0360a01b825416179055565b6001600160a01b0361011491166001600160601b0360a01b825416179055565b6140706109156109156065546001600160a01b031690565b604092835192838093633c5103f760e11b8252602094859160049788915afa9182156108c1576140c8848794879460009161458d575b50166001600160a01b0361010a91166001600160601b0360a01b825416179055565b8651633e9b6c6f60e21b815292839182905afa9081156108c157614112918391600091614570575b50166001600160a01b0361011791166001600160601b0360a01b825416179055565b828261412c6109156109156065546001600160a01b031690565b865162af394d60e81b815292839182905afa9081156108c157614175918391600091614553575b50166001600160a01b0361010991166001600160601b0360a01b825416179055565b828261418d610915610117546001600160a01b031690565b865163fbfa77cf60e01b815292839182905afa9081156108c1576141d7918391600091614536575b50166001600160a01b0361011591166001600160601b0360a01b825416179055565b82610114836141f061091583546001600160a01b031690565b875163026b1d5f60e01b815293849182905afa80156108c15761091585926142428661424f948a97600091614519575b50166001600160a01b0361010e91166001600160601b0360a01b825416179055565b546001600160a01b031690565b8651631f94a27560e31b815292839182905afa9081156108c157614298936000926144fc575b5050166001600160a01b0361011291166001600160601b0360a01b825416179055565b6142fa61010e926142b361091585546001600160a01b031690565b6142c961091561010b546001600160a01b031690565b93825194856335ea6a7560e01b9384825281806101e09889958783019190916001600160a01b036020820193169052565b03915afa80156108c15761432c61091561010061434c9361439a996000916144df575b5001516001600160a01b031690565b6001600160a01b0361010f91166001600160601b0360a01b825416179055565b8361436161091588546001600160a01b031690565b61437761091561010d546001600160a01b031690565b90855180809981948883528783019190916001600160a01b036020820193169052565b03915afa9485156108c1576000956144ae575b50908392916143f961091561443297986142426143d9610915610140809d01516001600160a01b031690565b6001600160a01b0361011091166001600160601b0360a01b825416179055565b9161441061091561010c546001600160a01b031690565b935196879485938493845283019190916001600160a01b036020820193169052565b03915afa9283156108c1576107c6936144619361091593600092614481575b505001516001600160a01b031690565b6001600160a01b0361011191166001600160601b0360a01b825416179055565b6144a09250803d106144a7575b614498818361356a565b810190613de9565b3880614451565b503d61448e565b6144329550906143f96109156144d387969594873d89116144a757614498818361356a565b975050509091926143ad565b6144f69150893d8b116144a757614498818361356a565b3861431d565b6145129250803d10610a2d57610a1d818361356a565b3880614275565b6145309150873d8911610a2d57610a1d818361356a565b38614220565b61454d9150853d8711610a2d57610a1d818361356a565b386141b5565b61456a9150853d8711610a2d57610a1d818361356a565b38614153565b6145879150853d8711610a2d57610a1d818361356a565b386140f0565b6145a49150853d8711610a2d57610a1d818361356a565b386140a6565b156145b157565b60405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201526a6e697469616c697a696e6760a81b6064820152608490fd5b61462460ff60005460081c1661461f816145aa565b6145aa565b6107c633613858565b61464260ff60005460081c1661461f816145aa565b60ff1960ca541660ca55565b90929161466660ff60005460081c1661461f816145aa565b835167ffffffffffffffff81116135495761468b816146866036546138a2565b614787565b602080601f83116001146146f1575090806146c893926107c696976000926146e6575b50508160011b916000199060031b1c191617603655614869565b6001600160a01b03166001600160601b0360a01b6065541617606555565b0151905038806146ae565b90601f1983169661472460366000527f4a11f94e20a93c79f6ec743a1954ec4fc2c08429ae2122118bf234b2185c81b890565b926000905b89821061476f5750509183916001936146c896956107c6999a10614756575b505050811b01603655614869565b015160001960f88460031b161c19169055388080614748565b80600185968294968601518155019501930190614729565b601f8111614793575050565b600090603682527f4a11f94e20a93c79f6ec743a1954ec4fc2c08429ae2122118bf234b2185c81b8906020601f850160051c830194106147ee575b601f0160051c01915b8281106147e357505050565b8181556001016147d7565b90925082906147ce565b601f8111614804575050565b600090603782527f42a7b7dd785cd69714a189dffb3fd7d7174edc9ece837694ce50f7078f7c31ae906020601f850160051c8301941061485f575b601f0160051c01915b82811061485457505050565b818155600101614848565b909250829061483f565b90815167ffffffffffffffff81116135495761488f8161488a6037546138a2565b6147f8565b602080601f83116001146148cb57508192936000926148c0575b50508160011b916000199060031b1c191617603755565b0151905038806148a9565b90601f198316946148fe60376000527f42a7b7dd785cd69714a189dffb3fd7d7174edc9ece837694ce50f7078f7c31ae90565b926000905b87821061493b575050836001959610614922575b505050811b01603755565b015160001960f88460031b161c19169055388080614917565b80600185968294968601518155019501930190614903565b90816020910312610437575161062881612acf565b6149706149a9565b600160ff1960ca54161760ca557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a1565b60ff60ca54166149b557565b60405162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b6044820152606490fd5b60ca5460ff811615610ecb5760ff191660ca557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a1565b90816020910312610437575190565b60405163a9059cbb60e01b60208201526001600160a01b039290921660248301526044808301939093529181526107c691614a7760648361356a565b614adb565b15614a8357565b60405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608490fd5b6001600160a01b03169060405190614af28261354e565b6020928383527f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c656484840152803b15614b695760008281928287614b449796519301915af1614b3e614bae565b90614bde565b80519081614b5157505050565b826107c693614b64938301019101614953565b614a7c565b60405162461bcd60e51b815260048101859052601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606490fd5b3d15614bd9573d90614bbf82613623565b91614bcd604051938461356a565b82523d6000602084013e565b606090565b90919015614bea575090565b815115614bfa5750805190602001fd5b60405162461bcd60e51b815260206004820152908190614c1e9060248301906105d7565b0390fd5b9190826040910312610437576020825192015190565b6040516335c5382560e01b815260fc60048201526020733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8183602481845af49283156108c157600093614d83575b508215614d31575b8215614cdf575b8215614c9457505090565b6040516368b47bdd60e01b815260fc60048201529192508290829060249082905af49182156108c157600092614cc957505090565b6106289250803d1061101c5761100d818361356a565b604051634ea6949160e01b815260fc60048201529092508181602481865af49081156108c157600091614d14575b5091614c89565b614d2b9150823d841161101c5761100d818361356a565b38614d0d565b60405163e01baf4f60e01b815260fc60048201529092508181602481865af49081156108c157600091614d66575b5091614c82565b614d7d9150823d841161101c5761100d818361356a565b38614d5f565b614d9b919350823d841161101c5761100d818361356a565b9138614c7a565b91908260409103126104375760208251614dbb81611023565b92015161062881611023565b519081600f0b820361043757565b9190826040910312610437576106286020614def84614dc7565b9301614dc7565b906001600160a01b038216918215614e755760355490828201809211610d7657614e36916035556001600160a01b03166000526033602052604060002090565b805490828201809211610d76576000927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9260209255604051908152a3565b60405162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f2061646472657373006044820152606490fd5b9290604051926323b872dd60e01b60208501526001600160a01b03809216602485015216604483015260648201526064815260a081019181831067ffffffffffffffff841117613549576107c692604052614adb565b90612710918203918211610d7657565b91908203918211610d7657565b6001600160a01b03811690811561501057614f5b816001600160a01b03166000526033602052604060002090565b54838110614fc057837fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92614fab600096613a7e9403916001600160a01b03166000526033602052604060002090565b55610e29614fbb82603554614f20565b603555565b60405162461bcd60e51b815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e604482015261636560f01b6064820152608490fd5b60405162461bcd60e51b815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f206164647265736044820152607360f81b6064820152608490fd5b1561506657565b60405162461bcd60e51b815260206004820152600b60248201526a5a45524f5f41535345545360a81b6044820152606490fd5b926001600160a01b03928361011b541633036151ad5760ff61010454161561519b57733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c93843b1561043757929190604051958694628453fd60e51b865260a4860160fc600488015260a06024880152825180915260c48701926020809101926000905b83821061517e57505050505060009593859361515185946151416151609560031995868983030160448a01526151bf565b90848783030160648801526151bf565b918483030160848501526105d7565b03915af480156108c1576151715750565b8061165e6107c692613535565b8451811686528b9950948201949382019360019190910190615110565b604051637869c16160e11b8152600490fd5b60405163f4a8e62160e01b8152600490fd5b90815180825260208080930193019160005b8281106151df575050505090565b8351855293810193928101926001016151d1565b60405163ab42ed5760e01b815260fc6004820152602081602481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c157600091615236575090565b610628915060203d8111610a6557610a55818361356a565b9069d3c21bcecceda100000091828102928184041490151715610d7657565b9061271091828102928184041490151715610d7657565b81810292918115918404141715610d7657565b6c0c9f2c9cd04674edea400000009081810291818304149015178215151615610437570490565b81810291818304149015178215151615610437570490565b1561043757565b600160801b91600019838309928260801b9283808610950394808603951461535c5790829161530d8684116152d6565b0981806000031680920460028082600302188083028203028083028203028083028203028083028203028083028203028092029003029360018380600003040190848311900302920304170290565b5050915061536b8215156152d6565b0490565b604051631acd643160e11b815260fc60048201526000602482015290602082604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49182156108c1576000926153de575b50818102918183041490151760011615610437576c0c9f2c9cd04674edea40000000900490565b6153f791925060203d8111610a6557610a55818361356a565b90386153b7565b91909160008382019384129112908015821691151617610d7657565b81810392916000138015828513169184121617610d7657565b600160ff1b8110156154425790565b60405162461bcd60e51b81526020600482015260086024820152674f766572666c6f7760c01b6044820152606490fd5b61010554604051631b22edb960e01b815260fc600482015260016024820152602081604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c1576000916154db575b506000818311156154d557508103908111610d765790565b91505090565b6154f3915060203d8111610a6557610a55818361356a565b386154bd565b603554600091908061550b5750905090565b604051631b22edb960e01b815260fc60048201526001602482015291602083604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49283156108c157610628949361555b575b506152be565b61557491935060203d8111610a6557610a55818361356a565b9138615555565b6035546000908061558b57505090565b909160405190631b22edb960e01b825260fc6004830152836024830152602082604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49182156108c15761062894926155db57506152be565b6155f491925060203d8111610a6557610a55818361356a565b9038615555565b9060405163011e1eed60e71b815260fc600482015282602482015260016044820152602081606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c157600091615661575b50615653816154f9565b908303928311610d76579190565b615679915060203d8111610a6557610a55818361356a565b38615649565b60355415615715576156909061557b565b9060405163011e1eed60e71b815260fc600482015282602482015260016044820152602081606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c1576000916156f7575b50820391808311610d7657826156f3916138f2565b9190565b61570f915060203d8111610a6557610a55818361356a565b386156de565b90600090565b90603554801561583c5760405163011e1eed60e71b815260fc60048201526024810184905260006044820152602091733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c908383606481855af49283156108c157600093615819575b506157819061526d565b604051631b22edb960e01b815260fc600482015260006024820152918490839060449082905af49384156108c157610628946157f6936157ef926000926157fc575b50506157e96157e46157dd6101045461ffff9060481c1690565b61ffff1690565b614f10565b90615284565b9083615842565b93614f20565b6158129250803d10610a6557610a55818361356a565b38806157c3565b61578191935061583590853d8711610a6557610a55818361356a565b9290615777565b50600090565b9190918281029281840414901517811515161561043757600190600019830104019015150290565b603554156157155761587b9061557b565b60405163011e1eed60e71b815260fc60048201526024810182905260006044820152909190602081606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c157600091615907575b506158e061ffff6101045460481c16614f10565b92838202938285041482151760011615610437576127109161590191614f20565b92049190565b61591f915060203d8111610a6557610a55818361356a565b386158cc565b733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c803b1561043757600060449160405192838092638331458760e01b825260fc6004830152600160248301525af480156108c1576159745750565b6107c690613535565b733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c803b1561043757600060449160405192838092638331458760e01b825260fc60048301528460248301525af480156108c1576159745750565b61011e6001600160801b036159e782546001600160801b031690565b1615615c1e575b54615a039060801c5b6001600160801b031690565b15615b55575b604080516373c2549d60e11b815260fc6004820152733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c91908181602481865af49081156108c157615a85916020916000908192615b35575b50845163199aa16760e21b815260fc60048201526024810191909152604481019190915291829081906064820190565b0381865af49081156108c157600091615b17575b50615aa261597d565b823b1561043757815163a07ae7bb60e01b815260fc6004820152600081602481875af480156108c157615b04575b50823b15610437579051635941106d60e01b815260fc60048201526024810191909152906000908290818060448101615160565b8061165e615b1192613535565b38615ad0565b615b2f915060203d8111610a6557610a55818361356a565b38615a99565b9050615b4e9150843d8611611c3057611c23818361356a565b9038615a55565b615bb26020615b70610915610115546001600160a01b031690565b615b8661091561010c546001600160a01b031690565b6040516352f55eed60e01b81526001600160a01b03909116600482015292839190829081906024820190565b03915afa9081156108c157615bfb91615bd391600091615c00575b50615c7d565b61011e805490916001600160801b039190911660809190911b6001600160801b031916179055565b615a09565b615c18915060203d8111610a6557610a55818361356a565b38615bcd565b615c4f6020615c39610915610115546001600160a01b031690565b615b8661091561010d546001600160a01b031690565b03915afa9182156108c157615c75611985615a03946159f794600091615c005750615c7d565b9150506159ee565b906001600160801b03821691820361043757565b604080516373c2549d60e11b815260fc60048201529190733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c908084602481855af49182156108c1576000948593615da2575b508151633859f9b960e21b815260fc6004820152600060248201526020959093908685604481865af49485156108c157615d5a968896615d1f92600091615d85575b50614f20565b93519586948593849363a999987d60e01b855260048501608091949360009360a083019660fc84526020840152604083015260608201520152565b03915af480156108c157615d6c575050565b81615d8292903d1061101c5761100d818361356a565b50565b615d9c9150883d8a11610a6557610a55818361356a565b38615d19565b90925081615dbd9295503d8611611c3057611c23818361356a565b9390939138615cd7565b615dcf6151f3565b6101055410615ed657604080516373c2549d60e11b815260fc600482015290733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8183602481845af49283156108c1576000908194615eb4575b508251633859f9b960e21b815260fc6004820152600060248201526020949092908584604481855af49283156108c157615d5a958795600095615e95575b50519586948593849363a999987d60e01b855260048501608091949360009360a083019660fc84526020840152604083015260608201520152565b615ead919550863d8811610a6557610a55818361356a565b9338615e5a565b9050615ece919350823d8411611c3057611c23818361356a565b929038615e1c565b6040516324d758c360e21b8152600490fdfea164736f6c6343000812000a

Deployed Bytecode

0x6080604052600436101561001257600080fd5b60003560e01c80624ec12b1461042157806301e1d1141461041c57806306fdde031461041757806307a2d13a14610412578063095ea7b31461040d5780630a28a477146104085780630ee31e9414610403578063138cc18f146103fe57806318160ddd146103f95780631f5edd8d146103f457806323b872dd146103ef57806330af302d146103ea578063313ce567146103e557806338d52e0f146103e057806339509351146103db5780633d672a45146103d65780633f4ba83a146103d1578063402d267d146103cc578063476343ee146103c757806347f24f4b146103c25780634cdad506146103bd578063520c5fd9146103b8578063554070bc146103b357806359f3fe22146103ae5780635c975abb146103a95780635c9a9b55146103a457806365eda5571461039f5780636e553f651461039a57806370a0823114610313578063715018a6146103955780637d7c2a1c1461039057806382100a501461038b5780638456cb59146103865780638d315d7b146103815780638da5cb5b1461037c57806394bf804d1461037757806395956cc11461037257806395d89b411461036d57806395e2da7c146103685780639c3977b514610363578063a457c2d71461035e578063a9059cbb14610359578063b3d7f6b914610354578063b460af941461034f578063b811ad701461034a578063ba08765214610345578063ba7cc8d114610340578063bb8234d11461033b578063bf9571f114610336578063c63d75b614610331578063c6e6f5921461032c578063cad42dca14610327578063ce96cb7714610322578063d12f13b71461031d578063d627b53014610318578063d905777e14610313578063dbd5edc71461030e578063dd62ed3e14610309578063e0120cf814610304578063e245b5af146102ff578063ec279a18146102fa578063edbcc599146102f5578063ef8b30f7146102f0578063f04f2707146102eb5763f2fde38b146102e657600080fd5b61376f565b613691565b613501565b6134be565b6134a0565b6133be565b6132c0565b61325b565b61323c565b61180c565b613217565b61317a565b61313c565b6130d9565b6130bb565b61308d565b612b7e565b612ad9565b612a49565b612919565b6127e9565b6126a6565b61265f565b612635565b612582565b612524565b612442565b61239b565b61223f565b612010565b611fe9565b611e5c565b611e02565b611db7565b6118a8565b61184a565b6116e0565b6114e1565b6112dd565b6112ba565b61125f565b611241565b6111f9565b6111db565b611034565b610f2d565b610f07565b610e73565b610d7b565b610d18565b610cf1565b610cd5565b610b6b565b610a89565b6108e4565b6108c6565b61085a565b6107c8565b610763565b61072e565b610710565b61062b565b6105b4565b610448565b6001600160a01b0381160361043757565b600080fd5b61ffff81160361043757565b346104375760a03660031901126104375760043561046581610426565b60243561047181610426565b604435916064356104818161043c565b6084359062ffffff8216820361043757610499613800565b61271061ffff8216116105975760fc80546001600160a01b0319166001600160a01b038581169190911790915561010586905561010480546dffffffffff0000000000000000001916604884901b6affff000000000000000000161760589490941b62ffffff60581b16939093179092557fe15ba51cde2870a515ffc9834f5ac1bb010e259ba47eda16968c5bc5be83cbf694610592926105569086166001600160a01b0361011c91166001600160601b0360a01b825416179055565b604051948594859293608092959461ffff9260a08601976001600160a01b03809216875216602086015260408501526000606085015216910152565b0390a1005b6040516312ee160b60e11b8152600490fd5b600091031261043757565b346104375760003660031901126104375760206105cf6151f3565b604051908152f35b919082519283825260005b848110610603575050826000602080949584010152601f8019910116010190565b6020818301810151848301820152016105e2565b9060206106289281815201906105d7565b90565b346104375760008060031936011261070d57604051908060365461064e816138a2565b808552916001918083169081156106e35750600114610688575b610684856106788187038261356a565b60405191829182610617565b0390f35b9250603683527f4a11f94e20a93c79f6ec743a1954ec4fc2c08429ae2122118bf234b2185c81b85b8284106106cb57505050810160200161067882610684610668565b805460208587018101919091529093019281016106b0565b8695506106849693506020925061067894915060ff191682840152151560051b8201019293610668565b80fd5b346104375760203660031901126104375760206105cf60043561557b565b346104375760403660031901126104375761075860043561074e81610426565b6024359033613ad6565b602060405160018152f35b3461043757602036600319011261043757602061078160043561571b565b50604051908152f35b9181601f840112156104375782359167ffffffffffffffff8311610437576020838186019501011161043757565b61012435906107c682610426565b565b34610437576101403660031901126104375767ffffffffffffffff600435818111610437576107fb90369060040161078a565b916024359081116104375761081490369060040161078a565b60443561082081610426565b6064359161082d83610426565b6084359361083a85610426565b60803660a319011261043757610858966108526107b8565b96613c03565b005b346104375760008060031936011261070d57733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c81813b1561070d5760405163a07ae7bb60e01b815260fc600482015291829060249082905af480156108c1576108b5575080f35b6108be90613535565b80f35b613d86565b34610437576000366003190112610437576020603554604051908152f35b346104375760008060031936011261070d576108fe613800565b61011861092161091582546001600160a01b031690565b6001600160a01b031690565b906040518092633671df2560e01b825281600460209586935afa9081156108c1578491610a6c575b5060405190634e71d92d60e01b82528382600481886001600160a01b038096165af19182156108c15784908693610a34575b506109ca610915600494957f0124e1b96155213f77e0118f51f6173eb3606f76d89ba9dfc6a00fc629cb7dee604051806109ba8a829190602083019252565b0390a1546001600160a01b031690565b6040516331e67c7160e01b815293849182905afa9182156108c1576108be948693610a05575b505060fd546001600160a01b03169116614a3b565b610a25929350803d10610a2d575b610a1d818361356a565b810190613d71565b9038806109f0565b503d610a13565b60049350610915610a5d6109ca92843d8611610a65575b610a55818361356a565b810190614a2c565b94505061097b565b503d610a4b565b610a839150833d8511610a2d57610a1d818361356a565b38610949565b3461043757606036600319011261043757600435610aa681610426565b602435610ab281610426565b604435906001600160a01b0383166000526034602052610ae9336040600020906001600160a01b0316600052602052604060002090565b549260018401610b0a575b610afe93506139b2565b60405160018152602090f35b828410610b2657610b2183610afe95033383613ad6565b610af4565b60405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e63650000006044820152606490fd5b346104375760603660031901126104375760043563ffffffff8116908181036104375760243590610b9b8261043c565b6203f48060443593610bac8561043c565b610bb4613800565b11610cc45761271061ffff8181851611610cb2578416908110908115610ca5575b50610c935761010680546bffffffff00000000000000001916604083901b6bffffffff0000000000000000161790557f74666b2773815375e6daaf777fe5b0b6f04a4a15373485cfaf2cf48251b21d9e9261059290610106805461ffff60601b1916606086901b61ffff60601b16179055610106805467ffff0000000000001916603083901b67ffff000000000000161790556040519384938491604091949363ffffffff606085019616845261ffff809216602085015216910152565b60405163149d3c7560e21b8152600490fd5b9050610100541038610bd5565b60405163fd24448b60e01b8152600490fd5b6040516228254960e21b8152600490fd5b3461043757600036600319011261043757602060405160128152f35b346104375760003660031901126104375760206001600160a01b0360655416604051908152f35b3461043757604036600319011261043757600435610d3581610426565b336000526034602052610d5f816040600020906001600160a01b0316600052602052604060002090565b546024358101809111610d76576107589133613ad6565b6138dc565b3461043757600036600319011261043757610da2610915610117546001600160a01b031690565b60405163019c8a3b60e11b8152600060048201526020918290829060249082905afa80156108c157600491600091610e56575b5082610ded61091561010a546001600160a01b031690565b6040516318160ddd60e01b815293849182905afa9283156108c15761068493610e2993610e2392600092610e39575b505061524e565b906152dd565b6040519081529081906020820190565b610e4f9250803d10610a6557610a55818361356a565b3880610e1c565b610e6d9150833d8511610a6557610a55818361356a565b38610dd5565b3461043757600036600319011261043757610e8c613800565b60ca5460ff811615610ecb5760ff191660ca557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a1005b60405162461bcd60e51b815260206004820152601460248201527314185d5cd8589b194e881b9bdd081c185d5cd95960621b6044820152606490fd5b3461043757602036600319011261043757610f23600435610426565b60206105cf615472565b346104375760008060031936011261070d57610fa86101018281549155602081610f696001600160a01b0361010c54166001600160a01b031690565b60fd546001600160a01b03168660405180978195829463a9059cbb60e01b845260048401602090939291936001600160a01b0360408201951681520152565b03925af19081156108c1577f9800e6f57aeb4360eaa72295a820a4293e1e66fbfcabcd8874ae141304a76deb92610fef92610ff5575b506040519081529081906020820190565b0390a180f35b6110159060203d811161101c575b61100d818361356a565b810190614953565b5038610fde565b503d611003565b6001600160801b0381160361043757565b3461043757610100366003190112610437576004356110528161043c565b60243561105e8161043c565b6044359161106b8361043c565b6064359261107884611023565b60843561108481611023565b60a43561109081611023565b60c4359061109d82611023565b60e435926110aa84611023565b6110b2613800565b61271061ffff81818916116111c95781818a16116111b7578616116111a55761010680546fffffffffffffffffffffffffffffffff60701b607084901b167fffff00000000000000000000000000000000ffffffffffffffff00000000000090911663ffff000060108a901b161765ffff0000000060208b901b161761ffff881617179055608082901b6001600160801b0319166001600160801b03891617610107557f8405afe6e9767f91c282339dc75edd776ddeda3e9aad8e1bb15471db89f8d8679761059295608086901b6001600160801b0319166001600160801b0386161761010855604051988998896124d5565b6040516378fd03f760e01b8152600490fd5b604051632722719960e21b8152600490fd5b60405163669169e560e01b8152600490fd5b3461043757602036600319011261043757602061078160043561586a565b346104375760203660031901126104375760043561121681610426565b61121e613800565b6001600160a01b0361011791166001600160601b0360a01b825416179055600080f35b346104375760203660031901126104375760206105cf60043561536f565b346104375760003660031901126104375760a06001600160a01b0362ffffff8160fc54169161011c54166101055461010454916040519485526020850152604084015261ffff8160481c16606084015260581c166080820152f35b3461043757600036600319011261043757602060ff60ca54166040519015158152f35b34610437576080366003190112610437576004356112fa81610426565b60243561130681610426565b6044359160643561131681610426565b61131e613800565b614e2084116114cf5761011b80546001600160a01b0319166001600160a01b03841617905561011a80546001600160a01b0319166001600160a01b0385161790556113698461010055565b61011380546001600160a01b0319166001600160a01b03831617905561139b610915610114546001600160a01b031690565b936040519463026b1d5f60e01b86526020928387600481855afa80156108c1576004976000916114b2575b5061010e80546001600160a01b0319166001600160a01b03928316908117909155928590604051631f94a27560e31b8152998a9182905afa80156108c1577f04a2e4917e8cf09fc5f74a42cb8b240cb288a69c78dbf02719e5c7bf9ca1561e9861059296600092611495575b5050169261145a846001600160a01b0361011291166001600160601b0360a01b825416179055565b604080516001600160a01b0397881681529787166020890152870152841660608601528316608085015290911660a0830152819060c0820190565b6114ab9250803d10610a2d57610a1d818361356a565b3880611432565b6114c99150853d8711610a2d57610a1d818361356a565b386113c6565b60405163361804a960e21b8152600490fd5b346104375760008060031936011261070d576114fb613800565b61011861151261091582546001600160a01b031690565b9060409182518091633671df2560e01b825281600460209485935afa9283156108c157859182946116c1575b506001600160a01b03809416803b156116bd5782906004875180958193633ccfd60b60e01b83525af19081156108c15761158992610915926116aa575b50546001600160a01b031690565b918351630d43254f60e31b81528281600481875afa9182156108c1578391879361168b575b5085516370a0823160e01b81523060048201529283916024918391165afa9182156108c157859261166e575b5050813b1561166a578251633be3166560e21b8152600481018290529184908390602490829084905af180156108c1576108be9361164b937f5663ef0eef5f33e3884097c3ec29a0bfbc957992d83512d046b5fc3d35bd8f3e92611651575b5051828152602090a1610102546138f2565b61010255565b8061165e61166492613535565b806105a9565b38611639565b8380fd5b6116849250803d10610a6557610a55818361356a565b38806115da565b6116a3919350823d8411610a2d57610a1d818361356a565b91386115ae565b8061165e6116b792613535565b3861157b565b8280fd5b6116d9919450833d8511610a2d57610a1d818361356a565b923861153e565b346104375760403660031901126104375760243560043561170082610426565b6117086149a9565b6117106159cb565b602061171b826155fb565b90507f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a0177596001600160a01b038516928392604051908152a261175b826155fb565b509182156117d9576117958361068495611790846117876109156109156065546001600160a01b031690565b30903390614eba565b614df6565b604080519182526020820184905233917fdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d79190a36117d1615dc7565b610e29615925565b60405162461bcd60e51b815260206004820152600b60248201526a5a45524f5f53484152455360a81b6044820152606490fd5b34610437576020366003190112610437576001600160a01b0360043561183181610426565b1660005260336020526020604060002054604051908152f35b346104375760008060031936011261070d57611864613800565b806001600160a01b036098546001600160601b0360a01b8116609855167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b346104375760008060031936011261070d576001600160a01b0360fc5416803303611d9957506118dd6118d9614c38565b1590565b611d8757604080517fc741dbaad15a4f298fe8d80943fa8e005e7bcb2f5b0a0c8dec1fc35be457f1468380a1733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c637e51b4db60e01b825260049183818061193f8682019060fc602083019252565b0381855af49081156108c1576119a39186918791611d54575b506119859061011e805490916001600160801b039190911660809190911b6001600160801b031916179055565b6001600160801b0361011e91166001600160801b0319825416179055565b82516322b3d72160e11b81528381806119c38682019060fc602083019252565b0381855af480156108c157611a269186908792611d22575b506119ff9061011f906001600160801b0319825416906001600160801b0316179055565b61011f805490916001600160801b0390911660809190911b6001600160801b031916179055565b8251916373c2549d60e11b91828452848480611a498582019060fc602083019252565b0381845af49283156108c15786948794611cfd575b50855163199aa16760e21b815260fc8482019081526020818101889052604082018790529591908690829081906060010381875af49081156108c1578991611ce0575b50611aaa61597d565b833b15611cdc57875163a07ae7bb60e01b8152898180611ad18982019060fc602083019252565b0381885af480156108c157611cc9575b5088843b1561070d578851635941106d60e01b815260fc878201908152602081019390935291829081906040010381875af480156108c157611cb6575b50865195633859f9b960e21b8752858780611b478882019060006020604084019360fc81520152565b0381875af49687156108c1578997611c93575b50908591611b998951988993849363a999987d60e01b8552898501608091949360019360a083019660fc84526020840152604083015260608201520152565b0381855af480156108c157611bf49587958992611c76575b505015611c4457610104805466ffffffffffff0019169055611bd86118d960ca5460ff1690565b611c37575b8351809581948293835282019060fc602083019252565b03915af480156108c157611c0c575b826108be615925565b81611c2b92903d10611c30575b611c23818361356a565b810190614c22565b611c03565b503d611c19565b611c3f614968565b611bdd565b610104805466ffffffffffff0019164260081b66ffffffffffff001617905560ca5460ff1615611bdd57611c3f6149ed565b611c8c9250803d1061101c5761100d818361356a565b3880611bb1565b8692919750611cae90833d8511610a6557610a55818361356a565b969091611b5a565b8061165e611cc392613535565b38611b1e565b8061165e611cd692613535565b38611ae1565b8880fd5b611cf79150863d8811610a6557610a55818361356a565b38611aa1565b909350611d18919450853d8711611c3057611c23818361356a565b9390939238611a5e565b6119ff9250611d479150863d8811611d4d575b611d3f818361356a565b810190614dd5565b916119db565b503d611d35565b6119859250611d799150863d8811611d80575b611d71818361356a565b810190614da2565b9091611958565b503d611d67565b60405163a039e79960e01b8152600490fd5b604490604051906329b7946d60e01b82523360048301526024820152fd5b346104375760003660031901126104375760806001600160a01b038061011b5416908061011a5416906101005490610113541691604051938452602084015260408301526060820152f35b3461043757600036600319011261043757611e1b613800565b611e236149a9565b600160ff1960ca54161760ca557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a1005b346104375760008060031936011261070d57604080516373c2549d60e11b815260fc6004820152733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8282602481845af49182156108c15784908593611fc7575b50835163199aa16760e21b815260fc600482015260248101919091526044810192909252602091908290829060649082905af49081156108c1578491611faa575b50611f08610915610109546001600160a01b031690565b83516370a0823160e01b815230600482015294908390869060249082905afa80156108c157611f61611f58611f53611f729361068499611f7e98611f789792611f8d575b505061536f565b615433565b60fe54906153fe565b611f6c60ff54615433565b906153fe565b91615433565b9061541a565b90519081529081906020820190565b611fa39250803d10610a6557610a55818361356a565b3880611f4c565b611fc19150823d8411610a6557610a55818361356a565b38611ef1565b9050611fe1919250833d8511611c3057611c23818361356a565b919038611eb0565b346104375760003660031901126104375760206001600160a01b0360985416604051908152f35b3461043757604080600319360112610437576004356024359161203283610426565b61203a6149a9565b6120426159cb565b61204b8261567f565b9390506001600160a01b03926120a781858416938551978852847f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a0177596020809aa26117906120978361567f565b5080986065541630903390614eba565b8251848152602081019190915233907fdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d790604090a36120e46151f3565b610105541061222f5780516373c2549d60e11b815260fc600482015292733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8285602481845af480156108c157600095869161220c575b508351633859f9b960e21b815260fc600482015260006024820152918383604481845af49182156108c1576121a39785946000946121ed575b5086519889948593849363a999987d60e01b855260048501608091949360009360a083019660fc84526020840152604083015260608201520152565b03915af49384156108c157610684946121cf575b50506121c1615925565b519081529081906020820190565b816121e592903d1061101c5761100d818361356a565b5038806121b7565b612205919450853d8711610a6557610a55818361356a565b9238612167565b9050612226919550833d8511611c3057611c23818361356a565b9490943861212e565b516324d758c360e21b8152600490fd5b346104375760008060031936011261070d57612259613800565b6101025461011861227461091582546001600160a01b031690565b90813b1561166a576040516364f6446760e01b8152600481018490529184908390602490829084905af19182156108c1576109156020926122c4926004956116aa5750546001600160a01b031690565b604051633671df2560e01b815292839182905afa80156108c1576001600160a01b0391849161237d575b501690813b156116bd5760405163b6b55f2560e01b8152600481018290529183908390602490829084905af19081156108c1577f275563740cc51fbb46511cec625978872b9c5093b106db5ce55e071e57f249619261235c9261236a57506040519081529081906020820190565b0390a16108be600061010255565b8061165e61237792613535565b38610fde565b612395915060203d8111610a2d57610a1d818361356a565b386122ee565b346104375760008060031936011261070d5760405190806037546123be816138a2565b808552916001918083169081156106e357506001146123e757610684856106788187038261356a565b9250603783527f42a7b7dd785cd69714a189dffb3fd7d7174edc9ece837694ce50f7078f7c31ae5b82841061242a57505050810160200161067882610684610668565b8054602085870181019190915290930192810161240f565b346104375760003660031901126104375761010f546040516370a0823160e01b815230600482015290602090829060249082906001600160a01b03165afa9081156108c1576124ac6124a361068493610e29936000916124b7575b50615433565b60fe549061541a565b611f7860ff54615433565b6124cf915060203d8111610a6557610a55818361356a565b3861249d565b61ffff9182168152918116602083015290911660408201526001600160801b039182166060820152918116608083015291821660a082015291811660c083015290911660e08201526101000190565b34610437576000366003190112610437576101065461010754610684610108546040519384936001600160801b039061ffff92828560801c9516938260801c93808360701c1693169181811691808260201c169160101c16896124d5565b346104375760403660031901126104375760043561259f81610426565b602435903360005260346020526125cd816040600020906001600160a01b0316600052602052604060002090565b54918083106125e257610afe92039033613ad6565b60405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77604482015264207a65726f60d81b6064820152608490fd5b346104375760403660031901126104375761075860043561265581610426565b60243590336139b2565b3461043757602036600319011261043757602061078160043561567f565b6060906003190112610437576004359060243561269981610426565b9060443561062881610426565b34610437576106846117d16126ba3661267d565b9291906126c56149a9565b6126cd6159cb565b60206126d88361571b565b90506001600160a01b03907f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a017759828816938492604051908152a261273661271d8561571b565b508097843303612792575b61273187615c91565b614f2d565b60408051858152602081018890529184169133917ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db9190819081015b0390a461278d6109156109156065546001600160a01b031690565b614a3b565b846000526034602052816127bd336040600020906001600160a01b0316600052602052604060002090565b54600181016127ce575b5050612728565b6127e2916127db91614f20565b3383613ad6565b81386127c7565b346104375760008060031936011261070d57612803613800565b604080516373c2549d60e11b815260fc6004820152733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8282602481845af49182156108c15761287c92602091869087926128f9575b50855163199aa16760e21b815260fc60048201526024810191909152604481019190915292839081906064820190565b0381845af49081156108c157849283926128d9575b50803b156116bd579251635941106d60e01b815260fc6004820152602481019190915291829060449082905af480156108c1576128cc575080f35b8061165e6108be92613535565b6128f291925060203d8111610a6557610a55818361356a565b9038612891565b90506129129150853d8711611c3057611c23818361356a565b903861284c565b34610437576106846117d161292d3661267d565b906129399392936149a9565b6129416159cb565b602061294c8561586a565b90506001600160a01b03907f77439e914e0fa88dfd909bd08c755cb526ba370acd4f08f8f9b353581a017759828616938492604051908152a28133036129f2575b856129b261299a8261586a565b509788966129a988151561505f565b61273188615c91565b6040805186815260208101929092529184169133917ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db9181908101612772565b81600052603460205285612a1d336040600020906001600160a01b0316600052602052604060002090565b5460018101612a2e575b505061298d565b612a4291612a3b91614f20565b3386613ad6565b8538612a27565b3461043757600036600319011261043757604080516373c2549d60e11b815260fc60048201529081602481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c1576000908192612aae575b50604080519182526020820192909252f35b9050612ac8915060403d8111611c3057611c23818361356a565b9038612a9c565b8015150361043757565b346104375760408060031936011261043757602435612af781612acf565b8151906303f7ab5160e61b825260fc60048301526004356024830152151560448201528181606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49182156108c1576000918293612b59575b50519081526020810191909152604090f35b81612b7692945061068493503d8511611c3057611c23818361356a565b929091612b47565b346104375760008060031936011261070d57612b98613800565b612bae61091561010e546001600160a01b031690565b612bc461091561011a546001600160a01b031690565b9061010d91612bdd61091584546001600160a01b031690565b9160409081519363095ea7b360e01b948581526020968782600494818c81612c1f8a8a83019190916001600160a01b0360408201931681526020600019910152565b03925af19081156108c157612c44926109159261303d5750546001600160a01b031690565b8684518092888252818b81612c738b8983019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157613070575b5061010c612c9a61091582546001600160a01b031690565b8785518092898252818c81612cc98a8a83019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157613053575b50612ced61091582546001600160a01b031690565b908785518093898252818c81612d1d8c8a83019190916001600160a01b0360408201931681526020600019910152565b03925af19081156108c157612d42926109159261303d5750546001600160a01b031690565b91612d8c8761011794612d5f61091587546001600160a01b031690565b90898c895180968195829483528a83019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157613020575b50612de48761010b92612db861091585546001600160a01b031690565b898c895180968195829483528a83019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c15789968992612e3c92613003575b50612e1061091585546001600160a01b031690565b9087519889809481938d83528983019190916001600160a01b0360408201931681526020600019910152565b03925af19081156108c157612e67610915612ea6928b988b95612fe65750546001600160a01b031690565b612e7b61091586546001600160a01b031690565b86519788809481938c83528883019190916001600160a01b0360408201931681526020600019910152565b03925af19384156108c1578794612fc9575b5085612ee9612ed361091561010f546001600160a01b031690565b612e7b61091561011c546001600160a01b031690565b03925af180156108c157612f579587958992612fac575b50612f2d610915612f1f6109156109156065546001600160a01b031690565b95546001600160a01b031690565b945196879586948593845283019190916001600160a01b0360408201931681526020600019910152565b03925af180156108c157612f8e575b827fba5bb3f899c7a3edcc9ff9d46c4e08449c6a608b6f8254132bc5af4898645cbc8180a180f35b81612fa492903d1061101c5761100d818361356a565b503880612f66565b612fc290873d891161101c5761100d818361356a565b5038612f00565b612fdf90873d891161101c5761100d818361356a565b5038612eb8565b612ffc90863d881161101c5761100d818361356a565b503861157b565b61301990843d861161101c5761100d818361356a565b5038612dfb565b61303690883d8a1161101c5761100d818361356a565b5038612d9b565b612ffc908a3d8c1161101c5761100d818361356a565b61306990883d8a1161101c5761100d818361356a565b5038612cd8565b61308690873d891161101c5761100d818361356a565b5038612c82565b34610437576020366003190112610437576130a9600435610426565b60206105cf6130b6615472565b6154f9565b346104375760203660031901126104375760206105cf6004356154f9565b34610437576040366003190112610437576004356130f681612acf565b6024359061310382612acf565b61310b613800565b61011d805461ffff60a01b191691151560a01b60ff60a01b169190911791151560a81b60ff60a81b16919091179055005b34610437576020366003190112610437576001600160a01b0360043561316181610426565b16600052603360205260206105cf60406000205461557b565b34610437576040366003190112610437577f0ad868c06d6009b56f49b7d8396d0a6fc92a3474593d2bab6c0382dff84e9d4c60406004356131ba81611023565b6001600160a01b036001600160801b03602435926131d784610426565b6131df613800565b1691610121836001600160801b031982541617905516610120816001600160601b0360a01b82541617905582519182526020820152a1005b34610437576000366003190112610437576020613232614c38565b6040519015158152f35b3461043757600036600319011261043757602061010554604051908152f35b346104375760403660031901126104375760206132b760043561327d81610426565b6001600160a01b036024359161329283610426565b16600052603483526040600020906001600160a01b0316600052602052604060002090565b54604051908152f35b34610437576040366003190112610437576004356132dd8161043c565b6024356132e981610426565b6132f1613800565b60fd546001600160a01b038281169116146133ac5760fd80546001600160a01b0319166001600160a01b038316179055610bb861ffff83161161339a57610104805468ffff000000000000001916603884901b68ffff00000000000000161790557feecc969cd8da3140016e16b0603215de782af59713abfc5055d917b88f4a7b91916040805161ffff90921682526001600160a01b0390921660208201529081908101610592565b604051638bff87cf60e01b8152600490fd5b604051630ed1b8b360e31b8152600490fd5b34610437576020806003193601126104375761341b816004356133e081612acf565b6133f6610915610117546001600160a01b031690565b60405163019c8a3b60e11b815291151560048301529092839190829081906024820190565b03915afa80156108c157600491600091613483575b508261344861091561010a546001600160a01b031690565b6040516318160ddd60e01b815293849182905afa9283156108c15761068493610e299361347d92600092610e3957505061524e565b90615297565b61349a9150833d8511610a6557610a55818361356a565b38613430565b3461043757600036600319011261043757602060fe54604051908152f35b3461043757600036600319011261043757610106546040805163ffffffff83831c16815261ffff606084811c8216602084015260309490941c1691810191909152f35b346104375760203660031901126104375760206107816004356155fb565b634e487b7160e01b600052604160045260246000fd5b67ffffffffffffffff811161354957604052565b61351f565b6040810190811067ffffffffffffffff82111761354957604052565b90601f8019910116810190811067ffffffffffffffff82111761354957604052565b604051906101e0820182811067ffffffffffffffff82111761354957604052565b67ffffffffffffffff81116135495760051b60200190565b81601f82011215610437578035916135dc836135ad565b926135ea604051948561356a565b808452602092838086019260051b820101928311610437578301905b828210613614575050505090565b81358152908301908301613606565b67ffffffffffffffff811161354957601f01601f191660200190565b92919261364b82613623565b91613659604051938461356a565b829481845281830111610437578281602093846000960137010152565b9080601f83011215610437578160206106289335910161363f565b346104375760803660031901126104375767ffffffffffffffff60043581811161043757366023820112156104375780600401356136ce816135ad565b916136dc604051938461356a565b81835260209160248385019160051b8301019136831161043757602401905b8282106137565785856024358281116104375761371c9036906004016135c5565b604435838111610437576137349036906004016135c5565b9060643593841161043757613750610858943690600401613676565b92615099565b838091833561376481610426565b8152019101906136fb565b346104375760203660031901126104375760043561378c81610426565b613794613800565b6001600160a01b038116156137ac5761085890613858565b60405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608490fd5b6001600160a01b0360985416330361381457565b606460405162461bcd60e51b815260206004820152602060248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152fd5b609854906001600160a01b0380911691826001600160601b0360a01b821617609855167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0600080a3565b90600182811c921680156138d2575b60208310146138bc57565b634e487b7160e01b600052602260045260246000fd5b91607f16916138b1565b634e487b7160e01b600052601160045260246000fd5b91908201809211610d7657565b1561390657565b60405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b6064820152608490fd5b1561395e57565b60405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e7420657863656564732062604482015265616c616e636560d01b6064820152608490fd5b91906001600160a01b0390818416928315613a8357613a61827fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef94613a7e9416966139fe8815156138ff565b613a4784613a1f836001600160a01b03166000526033602052604060002090565b54613a2c82821015613957565b03916001600160a01b03166000526033602052604060002090565b556001600160a01b03166000526033602052604060002090565b613a6c8282546138f2565b90556040519081529081906020820190565b0390a3565b60405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f206164604482015264647265737360d81b6064820152608490fd5b906001600160a01b0391828116928315613bb2578216938415613b625780613b517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92594613b39613a7e956001600160a01b03166000526034602052604060002090565b906001600160a01b0316600052602052604060002090565b556040519081529081906020820190565b60405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b6064820152608490fd5b60405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b6064820152608490fd5b95939196949290966000549760ff8960081c16158099819a613d27575b8115613d07575b5015613cab57613c4d9789613c44600160ff196000541617600055565b613c9257613f00565b613c5357565b613c6361ff001960005416600055565b604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb384740249890602090a1565b613ca661010061ff00196000541617600055565b613f00565b60405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b6064820152608490fd5b303b15915081613d19575b5038613c27565b6001915060ff161438613d12565b600160ff8216109150613c20565b60e43561062881610426565b60a43561062881610426565b60c43561062881610426565b6101043561062881610426565b51906107c682610426565b90816020910312610437575161062881610426565b6040513d6000823e3d90fd5b9190826020910312610437576040516020810181811067ffffffffffffffff8211176135495760405291518252565b51906107c682611023565b519064ffffffffff8216820361043757565b51906107c68261043c565b6101e08183031261043757613e06613dff61358c565b9282613d92565b8252613e1460208201613dc1565b6020830152613e2560408201613dc1565b6040830152613e3660608201613dc1565b6060830152613e4760808201613dc1565b6080830152613e5860a08201613dc1565b60a0830152613e6960c08201613dcc565b60c0830152613e7a60e08201613dde565b60e0830152610100613e8d818301613d66565b90830152610120613e9f818301613d66565b90830152610140613eb1818301613d66565b90830152610160613ec3818301613d66565b90830152610180613ed5818301613dc1565b908301526101a0613ee7818301613dc1565b90830152613ef96101c0809201613dc1565b9082015290565b613ff1949161405896989794613f4561401295613f3d613f4b95613f2261460a565b613f2a61462d565b613f35610915613d35565b95369161363f565b92369161363f565b9161464e565b613f76613f56613d41565b6001600160a01b0361010c91166001600160601b0360a01b825416179055565b613fa1613f81613d4d565b6001600160a01b0361010d91166001600160601b0360a01b825416179055565b613fcc613fac613d59565b6001600160a01b0361010b91166001600160601b0360a01b825416179055565b61011a80546001600160a01b0319166001600160a01b03928316179055958693849190565b166001600160a01b0361011891166001600160601b0360a01b825416179055565b1691614038836001600160a01b0361011991166001600160601b0360a01b825416179055565b6001600160a01b0361011491166001600160601b0360a01b825416179055565b6140706109156109156065546001600160a01b031690565b604092835192838093633c5103f760e11b8252602094859160049788915afa9182156108c1576140c8848794879460009161458d575b50166001600160a01b0361010a91166001600160601b0360a01b825416179055565b8651633e9b6c6f60e21b815292839182905afa9081156108c157614112918391600091614570575b50166001600160a01b0361011791166001600160601b0360a01b825416179055565b828261412c6109156109156065546001600160a01b031690565b865162af394d60e81b815292839182905afa9081156108c157614175918391600091614553575b50166001600160a01b0361010991166001600160601b0360a01b825416179055565b828261418d610915610117546001600160a01b031690565b865163fbfa77cf60e01b815292839182905afa9081156108c1576141d7918391600091614536575b50166001600160a01b0361011591166001600160601b0360a01b825416179055565b82610114836141f061091583546001600160a01b031690565b875163026b1d5f60e01b815293849182905afa80156108c15761091585926142428661424f948a97600091614519575b50166001600160a01b0361010e91166001600160601b0360a01b825416179055565b546001600160a01b031690565b8651631f94a27560e31b815292839182905afa9081156108c157614298936000926144fc575b5050166001600160a01b0361011291166001600160601b0360a01b825416179055565b6142fa61010e926142b361091585546001600160a01b031690565b6142c961091561010b546001600160a01b031690565b93825194856335ea6a7560e01b9384825281806101e09889958783019190916001600160a01b036020820193169052565b03915afa80156108c15761432c61091561010061434c9361439a996000916144df575b5001516001600160a01b031690565b6001600160a01b0361010f91166001600160601b0360a01b825416179055565b8361436161091588546001600160a01b031690565b61437761091561010d546001600160a01b031690565b90855180809981948883528783019190916001600160a01b036020820193169052565b03915afa9485156108c1576000956144ae575b50908392916143f961091561443297986142426143d9610915610140809d01516001600160a01b031690565b6001600160a01b0361011091166001600160601b0360a01b825416179055565b9161441061091561010c546001600160a01b031690565b935196879485938493845283019190916001600160a01b036020820193169052565b03915afa9283156108c1576107c6936144619361091593600092614481575b505001516001600160a01b031690565b6001600160a01b0361011191166001600160601b0360a01b825416179055565b6144a09250803d106144a7575b614498818361356a565b810190613de9565b3880614451565b503d61448e565b6144329550906143f96109156144d387969594873d89116144a757614498818361356a565b975050509091926143ad565b6144f69150893d8b116144a757614498818361356a565b3861431d565b6145129250803d10610a2d57610a1d818361356a565b3880614275565b6145309150873d8911610a2d57610a1d818361356a565b38614220565b61454d9150853d8711610a2d57610a1d818361356a565b386141b5565b61456a9150853d8711610a2d57610a1d818361356a565b38614153565b6145879150853d8711610a2d57610a1d818361356a565b386140f0565b6145a49150853d8711610a2d57610a1d818361356a565b386140a6565b156145b157565b60405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201526a6e697469616c697a696e6760a81b6064820152608490fd5b61462460ff60005460081c1661461f816145aa565b6145aa565b6107c633613858565b61464260ff60005460081c1661461f816145aa565b60ff1960ca541660ca55565b90929161466660ff60005460081c1661461f816145aa565b835167ffffffffffffffff81116135495761468b816146866036546138a2565b614787565b602080601f83116001146146f1575090806146c893926107c696976000926146e6575b50508160011b916000199060031b1c191617603655614869565b6001600160a01b03166001600160601b0360a01b6065541617606555565b0151905038806146ae565b90601f1983169661472460366000527f4a11f94e20a93c79f6ec743a1954ec4fc2c08429ae2122118bf234b2185c81b890565b926000905b89821061476f5750509183916001936146c896956107c6999a10614756575b505050811b01603655614869565b015160001960f88460031b161c19169055388080614748565b80600185968294968601518155019501930190614729565b601f8111614793575050565b600090603682527f4a11f94e20a93c79f6ec743a1954ec4fc2c08429ae2122118bf234b2185c81b8906020601f850160051c830194106147ee575b601f0160051c01915b8281106147e357505050565b8181556001016147d7565b90925082906147ce565b601f8111614804575050565b600090603782527f42a7b7dd785cd69714a189dffb3fd7d7174edc9ece837694ce50f7078f7c31ae906020601f850160051c8301941061485f575b601f0160051c01915b82811061485457505050565b818155600101614848565b909250829061483f565b90815167ffffffffffffffff81116135495761488f8161488a6037546138a2565b6147f8565b602080601f83116001146148cb57508192936000926148c0575b50508160011b916000199060031b1c191617603755565b0151905038806148a9565b90601f198316946148fe60376000527f42a7b7dd785cd69714a189dffb3fd7d7174edc9ece837694ce50f7078f7c31ae90565b926000905b87821061493b575050836001959610614922575b505050811b01603755565b015160001960f88460031b161c19169055388080614917565b80600185968294968601518155019501930190614903565b90816020910312610437575161062881612acf565b6149706149a9565b600160ff1960ca54161760ca557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a1565b60ff60ca54166149b557565b60405162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b6044820152606490fd5b60ca5460ff811615610ecb5760ff191660ca557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a1565b90816020910312610437575190565b60405163a9059cbb60e01b60208201526001600160a01b039290921660248301526044808301939093529181526107c691614a7760648361356a565b614adb565b15614a8357565b60405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608490fd5b6001600160a01b03169060405190614af28261354e565b6020928383527f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c656484840152803b15614b695760008281928287614b449796519301915af1614b3e614bae565b90614bde565b80519081614b5157505050565b826107c693614b64938301019101614953565b614a7c565b60405162461bcd60e51b815260048101859052601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606490fd5b3d15614bd9573d90614bbf82613623565b91614bcd604051938461356a565b82523d6000602084013e565b606090565b90919015614bea575090565b815115614bfa5750805190602001fd5b60405162461bcd60e51b815260206004820152908190614c1e9060248301906105d7565b0390fd5b9190826040910312610437576020825192015190565b6040516335c5382560e01b815260fc60048201526020733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8183602481845af49283156108c157600093614d83575b508215614d31575b8215614cdf575b8215614c9457505090565b6040516368b47bdd60e01b815260fc60048201529192508290829060249082905af49182156108c157600092614cc957505090565b6106289250803d1061101c5761100d818361356a565b604051634ea6949160e01b815260fc60048201529092508181602481865af49081156108c157600091614d14575b5091614c89565b614d2b9150823d841161101c5761100d818361356a565b38614d0d565b60405163e01baf4f60e01b815260fc60048201529092508181602481865af49081156108c157600091614d66575b5091614c82565b614d7d9150823d841161101c5761100d818361356a565b38614d5f565b614d9b919350823d841161101c5761100d818361356a565b9138614c7a565b91908260409103126104375760208251614dbb81611023565b92015161062881611023565b519081600f0b820361043757565b9190826040910312610437576106286020614def84614dc7565b9301614dc7565b906001600160a01b038216918215614e755760355490828201809211610d7657614e36916035556001600160a01b03166000526033602052604060002090565b805490828201809211610d76576000927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9260209255604051908152a3565b60405162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f2061646472657373006044820152606490fd5b9290604051926323b872dd60e01b60208501526001600160a01b03809216602485015216604483015260648201526064815260a081019181831067ffffffffffffffff841117613549576107c692604052614adb565b90612710918203918211610d7657565b91908203918211610d7657565b6001600160a01b03811690811561501057614f5b816001600160a01b03166000526033602052604060002090565b54838110614fc057837fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92614fab600096613a7e9403916001600160a01b03166000526033602052604060002090565b55610e29614fbb82603554614f20565b603555565b60405162461bcd60e51b815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e604482015261636560f01b6064820152608490fd5b60405162461bcd60e51b815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f206164647265736044820152607360f81b6064820152608490fd5b1561506657565b60405162461bcd60e51b815260206004820152600b60248201526a5a45524f5f41535345545360a81b6044820152606490fd5b926001600160a01b03928361011b541633036151ad5760ff61010454161561519b57733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c93843b1561043757929190604051958694628453fd60e51b865260a4860160fc600488015260a06024880152825180915260c48701926020809101926000905b83821061517e57505050505060009593859361515185946151416151609560031995868983030160448a01526151bf565b90848783030160648801526151bf565b918483030160848501526105d7565b03915af480156108c1576151715750565b8061165e6107c692613535565b8451811686528b9950948201949382019360019190910190615110565b604051637869c16160e11b8152600490fd5b60405163f4a8e62160e01b8152600490fd5b90815180825260208080930193019160005b8281106151df575050505090565b8351855293810193928101926001016151d1565b60405163ab42ed5760e01b815260fc6004820152602081602481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c157600091615236575090565b610628915060203d8111610a6557610a55818361356a565b9069d3c21bcecceda100000091828102928184041490151715610d7657565b9061271091828102928184041490151715610d7657565b81810292918115918404141715610d7657565b6c0c9f2c9cd04674edea400000009081810291818304149015178215151615610437570490565b81810291818304149015178215151615610437570490565b1561043757565b600160801b91600019838309928260801b9283808610950394808603951461535c5790829161530d8684116152d6565b0981806000031680920460028082600302188083028203028083028203028083028203028083028203028083028203028092029003029360018380600003040190848311900302920304170290565b5050915061536b8215156152d6565b0490565b604051631acd643160e11b815260fc60048201526000602482015290602082604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49182156108c1576000926153de575b50818102918183041490151760011615610437576c0c9f2c9cd04674edea40000000900490565b6153f791925060203d8111610a6557610a55818361356a565b90386153b7565b91909160008382019384129112908015821691151617610d7657565b81810392916000138015828513169184121617610d7657565b600160ff1b8110156154425790565b60405162461bcd60e51b81526020600482015260086024820152674f766572666c6f7760c01b6044820152606490fd5b61010554604051631b22edb960e01b815260fc600482015260016024820152602081604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c1576000916154db575b506000818311156154d557508103908111610d765790565b91505090565b6154f3915060203d8111610a6557610a55818361356a565b386154bd565b603554600091908061550b5750905090565b604051631b22edb960e01b815260fc60048201526001602482015291602083604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49283156108c157610628949361555b575b506152be565b61557491935060203d8111610a6557610a55818361356a565b9138615555565b6035546000908061558b57505090565b909160405190631b22edb960e01b825260fc6004830152836024830152602082604481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49182156108c15761062894926155db57506152be565b6155f491925060203d8111610a6557610a55818361356a565b9038615555565b9060405163011e1eed60e71b815260fc600482015282602482015260016044820152602081606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c157600091615661575b50615653816154f9565b908303928311610d76579190565b615679915060203d8111610a6557610a55818361356a565b38615649565b60355415615715576156909061557b565b9060405163011e1eed60e71b815260fc600482015282602482015260016044820152602081606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c1576000916156f7575b50820391808311610d7657826156f3916138f2565b9190565b61570f915060203d8111610a6557610a55818361356a565b386156de565b90600090565b90603554801561583c5760405163011e1eed60e71b815260fc60048201526024810184905260006044820152602091733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c908383606481855af49283156108c157600093615819575b506157819061526d565b604051631b22edb960e01b815260fc600482015260006024820152918490839060449082905af49384156108c157610628946157f6936157ef926000926157fc575b50506157e96157e46157dd6101045461ffff9060481c1690565b61ffff1690565b614f10565b90615284565b9083615842565b93614f20565b6158129250803d10610a6557610a55818361356a565b38806157c3565b61578191935061583590853d8711610a6557610a55818361356a565b9290615777565b50600090565b9190918281029281840414901517811515161561043757600190600019830104019015150290565b603554156157155761587b9061557b565b60405163011e1eed60e71b815260fc60048201526024810182905260006044820152909190602081606481733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c5af49081156108c157600091615907575b506158e061ffff6101045460481c16614f10565b92838202938285041482151760011615610437576127109161590191614f20565b92049190565b61591f915060203d8111610a6557610a55818361356a565b386158cc565b733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c803b1561043757600060449160405192838092638331458760e01b825260fc6004830152600160248301525af480156108c1576159745750565b6107c690613535565b733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c803b1561043757600060449160405192838092638331458760e01b825260fc60048301528460248301525af480156108c1576159745750565b61011e6001600160801b036159e782546001600160801b031690565b1615615c1e575b54615a039060801c5b6001600160801b031690565b15615b55575b604080516373c2549d60e11b815260fc6004820152733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c91908181602481865af49081156108c157615a85916020916000908192615b35575b50845163199aa16760e21b815260fc60048201526024810191909152604481019190915291829081906064820190565b0381865af49081156108c157600091615b17575b50615aa261597d565b823b1561043757815163a07ae7bb60e01b815260fc6004820152600081602481875af480156108c157615b04575b50823b15610437579051635941106d60e01b815260fc60048201526024810191909152906000908290818060448101615160565b8061165e615b1192613535565b38615ad0565b615b2f915060203d8111610a6557610a55818361356a565b38615a99565b9050615b4e9150843d8611611c3057611c23818361356a565b9038615a55565b615bb26020615b70610915610115546001600160a01b031690565b615b8661091561010c546001600160a01b031690565b6040516352f55eed60e01b81526001600160a01b03909116600482015292839190829081906024820190565b03915afa9081156108c157615bfb91615bd391600091615c00575b50615c7d565b61011e805490916001600160801b039190911660809190911b6001600160801b031916179055565b615a09565b615c18915060203d8111610a6557610a55818361356a565b38615bcd565b615c4f6020615c39610915610115546001600160a01b031690565b615b8661091561010d546001600160a01b031690565b03915afa9182156108c157615c75611985615a03946159f794600091615c005750615c7d565b9150506159ee565b906001600160801b03821691820361043757565b604080516373c2549d60e11b815260fc60048201529190733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c908084602481855af49182156108c1576000948593615da2575b508151633859f9b960e21b815260fc6004820152600060248201526020959093908685604481865af49485156108c157615d5a968896615d1f92600091615d85575b50614f20565b93519586948593849363a999987d60e01b855260048501608091949360009360a083019660fc84526020840152604083015260608201520152565b03915af480156108c157615d6c575050565b81615d8292903d1061101c5761100d818361356a565b50565b615d9c9150883d8a11610a6557610a55818361356a565b38615d19565b90925081615dbd9295503d8611611c3057611c23818361356a565b9390939138615cd7565b615dcf6151f3565b6101055410615ed657604080516373c2549d60e11b815260fc600482015290733fcb2c03d15a6ef6bfa188f97bb89c738a17ec5c8183602481845af49283156108c1576000908194615eb4575b508251633859f9b960e21b815260fc6004820152600060248201526020949092908584604481855af49283156108c157615d5a958795600095615e95575b50519586948593849363a999987d60e01b855260048501608091949360009360a083019660fc84526020840152604083015260608201520152565b615ead919550863d8811610a6557610a55818361356a565b9338615e5a565b9050615ece919350823d8411611c3057611c23818361356a565b929038615e1c565b6040516324d758c360e21b8152600490fdfea164736f6c6343000812000a

Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

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

Validator Index Block Amount
View All Withdrawals

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

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.