ETH Price: $2,926.80 (-0.89%)

Contract

0x21e159bC5d0D950Ca67a230a1989624925631683
 

Overview

ETH Balance

0 ETH

ETH Value

$0.00

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Block
From
To
Multicall52227902024-06-24 18:56:35579 days ago1719255395IN
0x21e159bC...925631683
0 ETH0.00000310.12913583
Multicall52227702024-06-24 18:55:55579 days ago1719255355IN
0x21e159bC...925631683
0 ETH0.000003080.12814384

View more zero value Internal Transactions in Advanced View mode

Advanced mode:

Cross-Chain Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
ParticlePositionManager

Compiler Version
v0.8.23+commit.f704f362

Optimization Enabled:
Yes with 2000 runs

Other Settings:
paris EvmVersion
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

import {IERC721Receiver} from "../../lib/openzeppelin-contracts/contracts/token/ERC721/IERC721Receiver.sol";
import {Multicall} from "../../lib/openzeppelin-contracts/contracts/utils/Multicall.sol";
import {ReentrancyGuard} from "../../lib/openzeppelin-contracts/contracts/security/ReentrancyGuard.sol";
import {Ownable2StepUpgradeable} from "../../lib/openzeppelin-contracts-upgradeable/contracts/access/Ownable2StepUpgradeable.sol";
import {UUPSUpgradeable} from "../../lib/openzeppelin-contracts-upgradeable/contracts/proxy/utils/UUPSUpgradeable.sol";
import {TransferHelper} from "../../lib/v3-periphery/contracts/libraries/TransferHelper.sol";

import {IParticlePositionManager} from "../interfaces/IParticlePositionManager.sol";
import {Base} from "../libraries/Base.sol";
import {Blast} from "../libraries/Blast.sol";
import {LiquidityPosition} from "../libraries/LiquidityPosition.sol";
import {Lien} from "../libraries/Lien.sol";
import {Signatures} from "../libraries/Signatures.sol";
import {DataStruct} from "../libraries/Structs.sol";
import {Errors} from "../libraries/Errors.sol";

contract ParticlePositionManager is
    IParticlePositionManager,
    Ownable2StepUpgradeable,
    UUPSUpgradeable,
    IERC721Receiver,
    ReentrancyGuard,
    Multicall
{
    using LiquidityPosition for mapping(uint256 => LiquidityPosition.Info);
    using Lien for mapping(bytes32 => Lien.Info);

    /* Constants */
    uint256 private constant _TREASURY_RATE_MAX = 500_000;
    uint256 private constant _FEE_FACTOR_MAX = 1_000;
    uint128 private constant _LIQUIDATION_REWARD_FACTOR_MAX = 100_000;
    uint256 private constant _LOAN_TERM_MAX = 30 days;

    /* Variables */
    uint96 private _nextLienId;
    uint256 private _treasuryRate;
    // solhint-disable var-name-mixedcase
    address public ORACLE_SIGNER;
    uint256 public FEE_FACTOR;
    uint128 public LIQUIDATION_REWARD_FACTOR;
    uint256 public LOAN_TERM;
    uint256 public VALID_BLOCKS;
    // solhint-enable var-name-mixedcase

    /* Storage */
    mapping(uint256 => LiquidityPosition.Info) public lps; ///@dev tokenId => liquidity position
    mapping(bytes32 => Lien.Info) public liens; ///@dev (address, lienId) => lien
    mapping(address => uint256) private _treasury; ///@dev address => amount

    // required by openzeppelin UUPS module
    // solhint-disable-next-line no-empty-blocks
    function _authorizeUpgrade(address) internal override onlyOwner {}

    constructor() payable {
        _disableInitializers();
    }

    function initialize(
        uint256 feeFactor,
        uint128 liquidationRewardFactor,
        uint256 loanTerm,
        uint256 treasuryRate,
        uint256 validBlocks,
        address oracleSigner,
        address blastAdmin
    ) external initializer {
        __UUPSUpgradeable_init();
        __Ownable_init();
        Blast.configure();
        updateFeeFactor(feeFactor);
        updateLiquidationRewardFactor(liquidationRewardFactor);
        updateLoanTerm(loanTerm);
        updateTreasuryRate(treasuryRate);
        updateValidBlocks(validBlocks);
        updateOracleSigner(oracleSigner);
        updateBlastPointsAdmin(blastAdmin);
    }

    /*==============================================================
                        Liquidity Provision Logic
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function mint(
        DataStruct.MintParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    )
        external
        override
        nonReentrant
        returns (uint256 tokenId, uint128 liquidity, uint256 amount0Minted, uint256 amount1Minted)
    {
        Signatures.verifySignature(
            keccak256(abi.encode(params, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        (tokenId, liquidity, amount0Minted, amount1Minted) = lps.mint(params);
        emit LiquidityPosition.SupplyLiquidity(tokenId, msg.sender, liquidity);
    }

    /**
     * @notice Receiver function upon ERC721 LP position transfer
     * @dev LP must use safeTransferFrom to trigger onERC721Received
     * @param from the address which previously owned the NFT
     * @param tokenId the NFT identifier which is being transferred
     */
    function onERC721Received(
        address,
        address from,
        uint256 tokenId,
        bytes calldata
    ) external override returns (bytes4) {
        if (msg.sender == address(Base.UNI_POSITION_MANAGER)) {
            // matched with Uniswap v3 position NFTs
            lps[tokenId] = LiquidityPosition.Info({owner: from, renewalCutoffTime: 0, token0Owed: 0, token1Owed: 0});
            (, , , , , , , uint128 liquidity, , , , ) = Base.UNI_POSITION_MANAGER.positions(tokenId);
            emit LiquidityPosition.SupplyLiquidity(tokenId, from, liquidity);
        }
        return this.onERC721Received.selector;
    }

    /*==============================================================
                       Liquidity Management Logic
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function increaseLiquidity(
        uint256 tokenId,
        uint256 amount0,
        uint256 amount1,
        uint256 amount0Min,
        uint256 amount1Min,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant returns (uint128 liquidity, uint256 amount0Added, uint256 amount1Added) {
        Signatures.verifySignature(
            keccak256(abi.encode(tokenId, amount0, amount1, amount0Min, amount1Min, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        (liquidity, amount0Added, amount1Added) = lps.increaseLiquidity(
            tokenId,
            amount0,
            amount1,
            amount0Min,
            amount1Min
        );
        emit LiquidityPosition.IncreaseLiquidity(tokenId, liquidity);
    }

    /// @inheritdoc IParticlePositionManager
    function decreaseLiquidity(
        uint256 tokenId,
        uint128 liquidity,
        uint256 amount0Min,
        uint256 amount1Min,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant returns (uint256 amount0Decreased, uint256 amount1Decreased) {
        Signatures.verifySignature(
            keccak256(abi.encode(tokenId, liquidity, amount0Min, amount1Min, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        (amount0Decreased, amount1Decreased) = lps.decreaseLiquidity(tokenId, liquidity, amount0Min, amount1Min);
        emit LiquidityPosition.DecreaseLiquidity(tokenId, liquidity);
    }

    /// @inheritdoc IParticlePositionManager
    function collectLiquidity(
        uint256 tokenId,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant returns (uint256 amount0Collected, uint256 amount1Collected) {
        Signatures.verifySignature(
            keccak256(abi.encode(tokenId, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        (amount0Collected, amount1Collected) = lps.collectLiquidity(tokenId);
        emit LiquidityPosition.CollectLiquidity(tokenId, amount0Collected, amount1Collected);
    }

    /// @inheritdoc IParticlePositionManager
    function reclaimLiquidity(
        uint256 tokenId,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant {
        Signatures.verifySignature(
            keccak256(abi.encode(tokenId, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        uint32 cutOffTime = lps.reclaimLiquidity(tokenId);
        emit LiquidityPosition.ReclaimLiquidity(tokenId, cutOffTime);
    }

    /*=============================================================
                             Open Position
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function openPosition(
        DataStruct.OpenPositionParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant returns (uint96 lienId, uint256 collateralTo) {
        Signatures.verifySignature(
            keccak256(abi.encode(params, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        lienId = _nextLienId++;
        collateralTo = liens.openPosition(lps, _treasury, lienId, FEE_FACTOR, _treasuryRate, params);
        emit Lien.OpenPosition(msg.sender, lienId, collateralTo);
    }

    /*=============================================================
                             Close Position
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function closePosition(
        DataStruct.ClosePositionParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant returns (uint40 tokenId, uint256 token0Refund, uint256 token1Refund) {
        Signatures.verifySignature(
            keccak256(abi.encode(params, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        (tokenId, token0Refund, token1Refund) = liens.closePosition(lps, params);
        emit Lien.ClosePosition(msg.sender, tokenId, token0Refund, token1Refund);
    }

    /*=============================================================
                           Liquidate Position
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function liquidatePosition(
        DataStruct.ClosePositionParams calldata params,
        address borrower,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant returns (uint40 tokenId, uint256 token0Refund, uint256 token1Refund) {
        Signatures.verifySignature(
            keccak256(abi.encode(params, borrower, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        (tokenId, token0Refund, token1Refund) = liens.liquidatePosition(
            lps,
            params,
            borrower,
            LIQUIDATION_REWARD_FACTOR,
            LOAN_TERM
        );
        emit Lien.LiquidatePosition(borrower, tokenId, token0Refund, token1Refund);
    }

    /*=============================================================
                             Premium Logic
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function addPremium(
        DataStruct.AddPremiumParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant {
        Signatures.verifySignature(
            keccak256(abi.encode(params, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        (uint128 premium0, uint128 premium1, uint256 spending) = liens.addPremium(params);
        emit Lien.AddPremium(params.lienId, premium0, premium1, spending);
    }

    /*=============================================================
                              Vanilla Swap
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function swap(
        address tokenFrom,
        address tokenTo,
        uint24 fee,
        uint256 amountFrom,
        uint256 amountToMinimum,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external override nonReentrant returns (uint256 amountReceived) {
        Signatures.verifySignature(
            keccak256(abi.encode(tokenFrom, tokenTo, fee, amountFrom, amountToMinimum, blockNumber, msg.sender)),
            oracleSignature,
            blockNumber,
            ORACLE_SIGNER,
            VALID_BLOCKS
        );
        TransferHelper.safeTransferFrom(tokenFrom, msg.sender, address(this), amountFrom);
        amountReceived = Base.swapExactIn(tokenFrom, tokenTo, fee, amountFrom, amountToMinimum, 0);
        TransferHelper.safeTransfer(tokenTo, msg.sender, amountReceived);
        emit Base.Swap(tokenFrom, tokenTo, amountFrom, amountReceived);
    }

    /*=============================================================
                              Blast logic
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function configureClaimableGas() external override onlyOwner {
        Blast.configureClaimableGas();
    }

    /// @inheritdoc IParticlePositionManager
    function updateBlastPointsAdmin(address admin) public override onlyOwner {
        Blast.updateBlastPointsAdmin(admin);
        emit Blast.UpdateBlastPointsAdmin(admin);
    }

    /// @inheritdoc IParticlePositionManager
    function readYield() external view override returns (uint256 weth, uint256 usdb) {
        (weth, usdb) = Blast.readYield(address(this));
    }

    /// @inheritdoc IParticlePositionManager
    function claimYieldMaxGas(
        address recipient,
        uint256 wethToClaim,
        uint256 usdbToClaim
    ) external override onlyOwner nonReentrant returns (uint256 weth, uint256 usdb, uint256 gas) {
        (weth, usdb, gas) = Blast.claimYieldMaxGas(recipient, address(this), wethToClaim, usdbToClaim);
        emit Blast.ClaimYieldGas(recipient, weth, usdb, gas);
    }

    /*=============================================================
                              Admin logic
    ==============================================================*/

    /// @inheritdoc IParticlePositionManager
    function updateLiquidationRewardFactor(uint128 liquidationRewardFactor) public override onlyOwner {
        if (liquidationRewardFactor > _LIQUIDATION_REWARD_FACTOR_MAX) revert Errors.InvalidValue();
        LIQUIDATION_REWARD_FACTOR = liquidationRewardFactor;
        emit UpdateLiquidationRewardFactor(liquidationRewardFactor);
    }

    /// @inheritdoc IParticlePositionManager
    function updateFeeFactor(uint256 feeFactor) public override onlyOwner {
        if (feeFactor > _FEE_FACTOR_MAX) revert Errors.InvalidValue();
        FEE_FACTOR = feeFactor;
        emit UpdateFeeFactor(feeFactor);
    }

    /// @inheritdoc IParticlePositionManager
    function updateLoanTerm(uint256 loanTerm) public override onlyOwner {
        if (loanTerm > _LOAN_TERM_MAX) revert Errors.InvalidValue();
        LOAN_TERM = loanTerm;
        emit UpdateLoanTerm(loanTerm);
    }

    /// @inheritdoc IParticlePositionManager
    function updateTreasuryRate(uint256 treasuryRate) public override onlyOwner {
        if (treasuryRate > _TREASURY_RATE_MAX) revert Errors.InvalidValue();
        _treasuryRate = treasuryRate;
        emit UpdateTreasuryRate(treasuryRate);
    }

    /// @inheritdoc IParticlePositionManager
    function updateOracleSigner(address signer) public override onlyOwner {
        ORACLE_SIGNER = signer;
        emit UpdateOracleSigner(signer);
    }

    /// @inheritdoc IParticlePositionManager
    function updateValidBlocks(uint256 validBlocks) public override onlyOwner {
        VALID_BLOCKS = validBlocks;
        emit UpdateValidBlocks(validBlocks);
    }

    /// @inheritdoc IParticlePositionManager
    function withdrawTreasury(address token, address recipient) external override onlyOwner nonReentrant {
        uint256 withdrawAmount = _treasury[token];
        if (withdrawAmount > 0) {
            if (recipient == address(0)) {
                revert Errors.InvalidRecipient();
            }
            _treasury[token] = 0;
            TransferHelper.safeTransfer(token, recipient, withdrawAmount);
            emit WithdrawTreasury(token, recipient, withdrawAmount);
        }
    }
}

File 2 of 56 : IERC721Receiver.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

File 3 of 56 : Multicall.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Multicall.sol)

pragma solidity ^0.8.0;

import "./Address.sol";

/**
 * @dev Provides a function to batch together multiple calls in a single external call.
 *
 * _Available since v4.1._
 */
abstract contract Multicall {
    /**
     * @dev Receives and executes a batch of function calls on this contract.
     * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
     */
    function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
        results = new bytes[](data.length);
        for (uint256 i = 0; i < data.length; i++) {
            results[i] = Address.functionDelegateCall(address(this), data[i]);
        }
        return results;
    }
}

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

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be _NOT_ENTERED
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;
    }

    function _nonReentrantAfter() private {
        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
     * `nonReentrant` function in the call stack.
     */
    function _reentrancyGuardEntered() internal view returns (bool) {
        return _status == _ENTERED;
    }
}

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

pragma solidity ^0.8.0;

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

/**
 * @dev Contract module which provides access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership} and {acceptOwnership}.
 *
 * This module is used through inheritance. It will make available all functions
 * from parent (Ownable).
 */
abstract contract Ownable2StepUpgradeable is Initializable, OwnableUpgradeable {
    function __Ownable2Step_init() internal onlyInitializing {
        __Ownable_init_unchained();
    }

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

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

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

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

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

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

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

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

pragma solidity ^0.8.0;

import "../../interfaces/draft-IERC1822Upgradeable.sol";
import "../ERC1967/ERC1967UpgradeUpgradeable.sol";
import "./Initializable.sol";

/**
 * @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
 * {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
 *
 * A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
 * reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
 * `UUPSUpgradeable` with a custom implementation of upgrades.
 *
 * The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
 *
 * _Available since v4.1._
 */
abstract contract UUPSUpgradeable is Initializable, IERC1822ProxiableUpgradeable, ERC1967UpgradeUpgradeable {
    function __UUPSUpgradeable_init() internal onlyInitializing {
    }

    function __UUPSUpgradeable_init_unchained() internal onlyInitializing {
    }
    /// @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
    address private immutable __self = address(this);

    /**
     * @dev Check that the execution is being performed through a delegatecall call and that the execution context is
     * a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
     * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
     * function through ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
     * fail.
     */
    modifier onlyProxy() {
        require(address(this) != __self, "Function must be called through delegatecall");
        require(_getImplementation() == __self, "Function must be called through active proxy");
        _;
    }

    /**
     * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
     * callable on the implementing contract but not through proxies.
     */
    modifier notDelegated() {
        require(address(this) == __self, "UUPSUpgradeable: must not be called through delegatecall");
        _;
    }

    /**
     * @dev Implementation of the ERC1822 {proxiableUUID} function. This returns the storage slot used by the
     * implementation. It is used to validate the implementation's compatibility when performing an upgrade.
     *
     * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
     * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
     * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
     */
    function proxiableUUID() external view virtual override notDelegated returns (bytes32) {
        return _IMPLEMENTATION_SLOT;
    }

    /**
     * @dev Upgrade the implementation of the proxy to `newImplementation`.
     *
     * Calls {_authorizeUpgrade}.
     *
     * Emits an {Upgraded} event.
     *
     * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
     */
    function upgradeTo(address newImplementation) public virtual onlyProxy {
        _authorizeUpgrade(newImplementation);
        _upgradeToAndCallUUPS(newImplementation, new bytes(0), false);
    }

    /**
     * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
     * encoded in `data`.
     *
     * Calls {_authorizeUpgrade}.
     *
     * Emits an {Upgraded} event.
     *
     * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
     */
    function upgradeToAndCall(address newImplementation, bytes memory data) public payable virtual onlyProxy {
        _authorizeUpgrade(newImplementation);
        _upgradeToAndCallUUPS(newImplementation, data, true);
    }

    /**
     * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
     * {upgradeTo} and {upgradeToAndCall}.
     *
     * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
     *
     * ```solidity
     * function _authorizeUpgrade(address) internal override onlyOwner {}
     * ```
     */
    function _authorizeUpgrade(address newImplementation) 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[50] private __gap;
}

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

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

library TransferHelper {
    /// @notice Transfers tokens from the targeted address to the given destination
    /// @notice Errors with 'STF' if transfer fails
    /// @param token The contract address of the token to be transferred
    /// @param from The originating address from which the tokens will be transferred
    /// @param to The destination address of the transfer
    /// @param value The amount to be transferred
    function safeTransferFrom(
        address token,
        address from,
        address to,
        uint256 value
    ) internal {
        (bool success, bytes memory data) =
            token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
        require(success && (data.length == 0 || abi.decode(data, (bool))), 'STF');
    }

    /// @notice Transfers tokens from msg.sender to a recipient
    /// @dev Errors with ST if transfer fails
    /// @param token The contract address of the token which will be transferred
    /// @param to The recipient of the transfer
    /// @param value The value of the transfer
    function safeTransfer(
        address token,
        address to,
        uint256 value
    ) internal {
        (bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.transfer.selector, to, value));
        require(success && (data.length == 0 || abi.decode(data, (bool))), 'ST');
    }

    /// @notice Approves the stipulated contract to spend the given allowance in the given token
    /// @dev Errors with 'SA' if transfer fails
    /// @param token The contract address of the token to be approved
    /// @param to The target of the approval
    /// @param value The amount of the given token the target will be allowed to spend
    function safeApprove(
        address token,
        address to,
        uint256 value
    ) internal {
        (bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.approve.selector, to, value));
        require(success && (data.length == 0 || abi.decode(data, (bool))), 'SA');
    }

    /// @notice Transfers ETH to the recipient address
    /// @dev Fails with `STE`
    /// @param to The destination of the transfer
    /// @param value The value to be transferred
    function safeTransferETH(address to, uint256 value) internal {
        (bool success, ) = to.call{value: value}(new bytes(0));
        require(success, 'STE');
    }
}

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

import {DataStruct} from "../libraries/Structs.sol";

interface IParticlePositionManager {
    /*==============================================================
                               Event Logs
    ==============================================================*/

    event UpdateLiquidationRewardFactor(uint128 liquidationRewardFactor);
    event UpdateFeeFactor(uint256 feeFactor);
    event UpdateLoanTerm(uint256 loanTerm);
    event UpdateTreasuryRate(uint256 treasuryRate);
    event UpdateValidBlocks(uint256 validBlocks);
    event UpdateOracleSigner(address oracleSigner);
    event WithdrawTreasury(address token, address recipient, uint256 amount);

    /*==============================================================
                       Liquidity Provision Logic
    ==============================================================*/

    ///@dev inheritdoc LiquidityPosition.mint
    function mint(
        DataStruct.MintParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint256 tokenId, uint128 liquidity, uint256 amount0Minted, uint256 amount1Minted);

    /*==============================================================
                       Liquidity Management Logic
    ==============================================================*/

    ///@dev inheritdoc LiquidityPosition.increaseLiquidity
    function increaseLiquidity(
        uint256 tokenId,
        uint256 amount0,
        uint256 amount1,
        uint256 amount0Min,
        uint256 amount1Min,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint128 liquidity, uint256 amount0Added, uint256 amount1Added);

    ///@dev inheritdoc LiquidityPosition.decreaseLiquidity
    function decreaseLiquidity(
        uint256 tokenId,
        uint128 liquidity,
        uint256 amount0Min,
        uint256 amount1Min,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint256 amount0Decreased, uint256 amount1Decreased);

    ///@dev inheritdoc LiquidityPosition.collectLiquidity
    function collectLiquidity(
        uint256 tokenId,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint256 amount0Collected, uint256 amount1Collected);

    ///@dev inheritdoc LiquidityPosition.reclaimLiquidity
    function reclaimLiquidity(uint256 tokenId, uint256 blockNumber, bytes calldata oracleSignature) external;

    /*=============================================================
                              Trading Logic
    ==============================================================*/

    ///@dev inheritdoc Lien.openPosition
    function openPosition(
        DataStruct.OpenPositionParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint96 lienId, uint256 collateralTo);

    ///@dev inheritdoc Lien.closePosition
    function closePosition(
        DataStruct.ClosePositionParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint40 tokenId, uint256 token0Refund, uint256 token1Refund);

    ///@dev inheritdoc Lien.liquidatePosition
    function liquidatePosition(
        DataStruct.ClosePositionParams calldata params,
        address borrower,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint40 tokenId, uint256 token0Refund, uint256 token1Refund);

    ///@dev inheritdoc Lien.addPremium
    function addPremium(
        DataStruct.AddPremiumParams calldata params,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external;

    /*=============================================================
                              Vanilla Swap
    ==============================================================*/

    ///@dev inheritdoc Base.swapExactIn
    function swap(
        address tokenFrom,
        address tokenTo,
        uint24 fee,
        uint256 amountFrom,
        uint256 amountToMinimum,
        uint256 blockNumber,
        bytes calldata oracleSignature
    ) external returns (uint256 amountReceived);

    /*=============================================================
                              Blast Logic
    ==============================================================*/

    ///@dev inheritdoc Blast.configureClaimableGas
    function configureClaimableGas() external;

    ///@dev inheritdoc Blast.updateBlastPointsAdmin
    function updateBlastPointsAdmin(address blastPointsAdmin) external;

    ///@dev inheritdoc Blast.readYield
    function readYield() external returns (uint256 weth, uint256 usdb);

    ///@dev inheritdoc Blast.claimYieldMaxGas
    function claimYieldMaxGas(
        address recipient,
        uint256 wethToClaim,
        uint256 usdbToClaim
    ) external returns (uint256 weth, uint256 usdb, uint256 gas);

    /*=============================================================
                              Admin Logic
    ==============================================================*/

    /**
     * @notice Update liquidation reward factor
     * @param liquidationRewardFactor new liquidation reward factor
     */
    function updateLiquidationRewardFactor(uint128 liquidationRewardFactor) external;

    /**
     * @notice Update fee factor
     * @param feeFactor new fee factor
     */
    function updateFeeFactor(uint256 feeFactor) external;

    /**
     * @notice Update loan term
     * @param loanTerm new loan term
     */
    function updateLoanTerm(uint256 loanTerm) external;

    /**
     * @notice Update treasury rate
     * @param treasuryRate new treasury rate
     */
    function updateTreasuryRate(uint256 treasuryRate) external;

    /**
     * @notice Update oracle signer
     * @param oracleSigner new oracle signer
     */
    function updateOracleSigner(address oracleSigner) external;

    /**
     * @notice Update the valid blocks for oracle signature
     * @param validBlocks new valid blocks
     */
    function updateValidBlocks(uint256 validBlocks) external;

    /**
     * @notice Withdraw from treasury
     * @param token address to token to withdraw
     * @param recipient receiver of the token in treasury
     */
    function withdrawTreasury(address token, address recipient) external;
}

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

import {IERC20} from "../../lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol";
import {SafeCast} from "../../lib/v3-core/contracts/libraries/SafeCast.sol";
import {FixedPoint128} from "../../lib/v3-core/contracts/libraries/FixedPoint128.sol";
import {IUniswapV3Pool} from "../../lib/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import {IUniswapV3Factory} from "../../lib/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
import {ISwapRouter} from "../../lib/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import {TransferHelper} from "../../lib/v3-periphery/contracts/libraries/TransferHelper.sol";

import {INonfungiblePositionManager} from "../interfaces/INonfungiblePositionManager.sol";
import {PoolAddress} from "../libraries/PoolAddress.sol";
import {Errors} from "./Errors.sol";
import {FullMath} from "./FullMath.sol";
import {TickMath} from "./TickMath.sol";
import {LiquidityAmounts} from "./LiquidityAmounts.sol";
import {DataStruct, DataCache} from "../libraries/Structs.sol";

/// @title Base Library
/// @notice Contains internal helper functions for all contracts
library Base {
    using SafeCast for uint256;

    // solhint-disable private-vars-leading-underscore
    uint256 internal constant BASIS_POINT = 1_000_000;

    INonfungiblePositionManager internal constant UNI_POSITION_MANAGER =
        INonfungiblePositionManager(0x434575EaEa081b735C985FA9bf63CD7b87e227F9);
    ISwapRouter internal constant UNI_ROUTER = ISwapRouter(0x337827814155ECBf24D20231fCA4444F530C0555);
    IUniswapV3Factory internal constant UNI_FACTORY = IUniswapV3Factory(0x71b08f13B3c3aF35aAdEb3949AFEb1ded1016127);

    // solhint-enable private-vars-leading-underscore

    event Swap(address tokenFrom, address tokenTo, uint256 amountFrom, uint256 amountTo);

    /**
     * @notice Swap exactly `amountFrom` of `tokenFrom` into `tokenTo`.
     * @param tokenFrom address of token to swap from
     * @param tokenTo address of token to swap to
     * @param fee fee level of the pool
     * @param amountFrom amount of tokenFrom to swap
     * @param amountToMinimum minimum amount of tokenTo to receive
     * @param sqrtPriceLimitX96 the sqrt price limit of the pool for slippage protection
     * @return amountReceived amount of tokenTo received
     */
    function swapExactIn(
        address tokenFrom,
        address tokenTo,
        uint24 fee,
        uint256 amountFrom,
        uint256 amountToMinimum,
        uint160 sqrtPriceLimitX96
    ) internal returns (uint256 amountReceived) {
        TransferHelper.safeApprove(tokenFrom, address(UNI_ROUTER), amountFrom);
        amountReceived = UNI_ROUTER.exactInputSingle(
            ISwapRouter.ExactInputSingleParams({
                tokenIn: tokenFrom,
                tokenOut: tokenTo,
                fee: fee,
                recipient: address(this),
                deadline: block.timestamp,
                amountIn: amountFrom,
                amountOutMinimum: amountToMinimum,
                sqrtPriceLimitX96: sqrtPriceLimitX96
            })
        );
        TransferHelper.safeApprove(tokenFrom, address(UNI_ROUTER), 0);
    }

    /**
     * @notice Deterministically compute the pool for the given token pair and fee.
     * @dev The pool contract may or may not exist.
     * @param token0 address of token0
     * @param token1 address of token1
     * @param fee amount of swap fee
     * @return pool interface of the pool
     */
    function getPool(address token0, address token1, uint24 fee) internal view returns (IUniswapV3Pool) {
        return IUniswapV3Pool(UNI_FACTORY.getPool(token0, token1, fee));
    }

    /**
     * @notice Helper function to refund a token
     * @param recipient the address to receive the refund
     * @param token address of token to potentially refund
     * @param amountExpected amount of token0 expected to spend
     * @param amountActual amount of token0 actually spent
     * @return amountRefund amount of token to refund
     */
    function refund(
        address recipient,
        address token,
        uint256 amountExpected,
        uint256 amountActual
    ) internal returns (uint256 amountRefund) {
        if (amountExpected > amountActual) {
            unchecked {
                amountRefund = amountExpected - amountActual;
                TransferHelper.safeTransfer(token, recipient, amountRefund);
            }
        }
    }

    /**
     * @notice Helper function to refund a token, with additional check that amountActual must not exceed amountExpected
     * @param recipient the address to receive the refund
     * @param token address of token to potentially refund
     * @param amountExpected amount of token0 expected to spend
     * @param amountActual amount of token0 actually spent
     * @return amountRefund amount of token to refund
     */
    function refundWithCheck(
        address recipient,
        address token,
        uint256 amountExpected,
        uint256 amountActual
    ) internal returns (uint256 amountRefund) {
        if (amountActual > amountExpected) revert Errors.OverRefund();
        amountRefund = refund(recipient, token, amountExpected, amountActual);
    }

    /**
     * @notice Helper function to prepare data for leveraged swap
     * @param tokenId tokenId of the liquidity position NFT
     * @param liquidity amount of liquidity to borrow out
     * @param zeroForOne direction of the swap
     * @return tokenFrom token to swap from
     * @return tokenTo token to swap to
     * @return collateralFrom amount of `tokenFrom` that `liquidity` concentrates to at its end
     * @return collateralTo amount of `tokenTo` that `liquidity` concentrate to at its end
     */
    function prepareLeverage(
        uint256 tokenId,
        uint128 liquidity,
        bool zeroForOne
    ) internal view returns (address tokenFrom, address tokenTo, uint256 collateralFrom, uint256 collateralTo) {
        int24 tickLower;
        int24 tickUpper;
        (, , tokenFrom, tokenTo, , tickLower, tickUpper, , , , , ) = UNI_POSITION_MANAGER.positions(tokenId);

        (collateralFrom, collateralTo) = getRequiredCollateral(liquidity, tickLower, tickUpper);
        if (!zeroForOne) {
            (tokenFrom, tokenTo) = (tokenTo, tokenFrom);
            (collateralFrom, collateralTo) = (collateralTo, collateralFrom);
        }
    }

    /**
     * @notice Calculates the amount of collateral needed when borrowing liquidity from a position
     * @param liquidity amount of liquidity to borrow
     * @param tickLower lower tick of the position
     * @param tickUpper upper tick of the position
     * @return amount0 amount that the liquidity concentrates to at tickLower
     * @return amount1 amount that the liquidity concentrates to at tickHigher
     */
    function getRequiredCollateral(
        uint128 liquidity,
        int24 tickLower,
        int24 tickUpper
    ) internal pure returns (uint256 amount0, uint256 amount1) {
        uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(tickLower);
        uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(tickUpper);
        (amount0, amount1) = LiquidityAmounts.getAmountsForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
    }

    function getRequiredRepay(
        uint128 liquidity,
        uint256 tokenId
    ) internal view returns (uint256 amount0, uint256 amount1) {
        DataCache.RepayCache memory repayCache;
        (
            ,
            ,
            repayCache.token0,
            repayCache.token1,
            repayCache.fee,
            repayCache.tickLower,
            repayCache.tickUpper,
            ,
            ,
            ,
            ,

        ) = UNI_POSITION_MANAGER.positions(tokenId);
        IUniswapV3Pool pool = getPool(repayCache.token0, repayCache.token1, repayCache.fee);
        (repayCache.sqrtRatioX96, , , , , , ) = pool.slot0();
        repayCache.sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(repayCache.tickLower);
        repayCache.sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(repayCache.tickUpper);
        (amount0, amount1) = LiquidityAmounts.getAmountsForLiquidity(
            repayCache.sqrtRatioX96,
            repayCache.sqrtRatioAX96,
            repayCache.sqrtRatioBX96,
            liquidity
        );
    }

    /**
     * @notice Get the current owed fees for a position
     * @param params.tokenId tokenId of the liquidity position NFT
     * @param params.liquidity amount of liquidity in the position
     * @param params.feeGrowthInside0LastX128 the fee growth of the position for token0 as of the last fee update
     * @param params.feeGrowthInside1LastX128 the fee growth of the position for token1 as of the last fee update
     * @param params.token0PremiumPortion the portion of token0 premium locked in the lien
     * @param params.token1PremiumPortion the portion of token1 premium locked in the lien
     * @param zeroForOne direction of the swap
     * @return tokenFrom  address of token to swap from at close position
     * @return tokenTo address of token to swap to at close position
     * @return tokenFromOwed amount owed to the liquidity provider for the token on the side of swap from
     * @return tokenToOwed amount owed to the liquidity provider for the token on the side of swap to
     * @return tokenFromPremium amount of premium for the token on the side of swap from
     * @return tokenToPremium amount of premium for the token on the side of swap to
     * @return collateralFrom amount of collateral for the token on the side of swap from
     * @return collateralTo amount of collateral for the token on the side of swap to
     */
    function getOwedInfoConverted(
        DataStruct.OwedInfoParams memory params,
        bool zeroForOne
    )
        internal
        view
        returns (
            address tokenFrom,
            address tokenTo,
            uint128 tokenFromOwed,
            uint128 tokenToOwed,
            uint128 tokenFromPremium,
            uint128 tokenToPremium,
            uint256 collateralFrom,
            uint256 collateralTo
        )
    {
        (
            tokenFrom,
            tokenTo,
            tokenFromOwed,
            tokenToOwed,
            tokenFromPremium,
            tokenToPremium,
            collateralFrom,
            collateralTo
        ) = getOwedInfo(params);
        if (zeroForOne) {
            (tokenFrom, tokenTo) = (tokenTo, tokenFrom);
            (tokenFromOwed, tokenToOwed) = (tokenToOwed, tokenFromOwed);
            (tokenFromPremium, tokenToPremium) = (tokenToPremium, tokenFromPremium);
            (collateralFrom, collateralTo) = (collateralTo, collateralFrom);
        }
    }

    /**
     * @notice Get the current owed fees for a position
     * @param params.tokenId tokenId of the liquidity position NFT
     * @param params.liquidity amount of liquidity in the position
     * @param params.feeGrowthInside0LastX128 the fee growth of the position for token0 as of the last fee update
     * @param params.feeGrowthInside1LastX128 the fee growth of the position for token1 as of the last fee update
     * @param params.token0PremiumPortion the portion of token0 premium locked in the lien
     * @param params.token1PremiumPortion the portion of token1 premium locked in the lien
     * @return token0 address of token0
     * @return token1 address of token1
     * @return token0Owed amount of token0 owed to the liquidity provider
     * @return token1Owed amount of token1 owed to the liquidity provider
     * @return token0Premium amount of token0 premium locked in the lien
     * @return token1Premium amount of token1 premium locked in the lien
     * @return collateral0 amount of token0 required by the lp (if oneForZero)
     * @return collateral1 amount of token1 required by the lp (if zeroForOne)
     */
    function getOwedInfo(
        DataStruct.OwedInfoParams memory params
    )
        internal
        view
        returns (
            address token0,
            address token1,
            uint128 token0Owed,
            uint128 token1Owed,
            uint128 token0Premium,
            uint128 token1Premium,
            uint256 collateral0,
            uint256 collateral1
        )
    {
        DataCache.OwedInfoCache memory cache;
        (, , token0, token1, cache.fee, cache.tickLower, cache.tickUpper, , , , , ) = UNI_POSITION_MANAGER.positions(
            params.tokenId
        );
        (cache.feeGrowthInside0X128, cache.feeGrowthInside1X128) = getFeeGrowthInside(
            token0,
            token1,
            cache.fee,
            cache.tickLower,
            cache.tickUpper
        );
        (token0Owed, token1Owed) = getOwedFee(
            cache.feeGrowthInside0X128,
            cache.feeGrowthInside1X128,
            params.feeGrowthInside0LastX128,
            params.feeGrowthInside1LastX128,
            params.liquidity
        );
        (collateral0, collateral1) = getRequiredCollateral(params.liquidity, cache.tickLower, cache.tickUpper);
        (token0Premium, token1Premium) = getPremium(
            collateral0,
            collateral1,
            params.token0PremiumPortion,
            params.token1PremiumPortion
        );
    }

    /**
     * @notice Helper function to calculate the current feeGrothInside(0/1)X128 based on tickLower and tickUpper
     * @dev feeGrowthInsideX128 calculation adopted from uniswap v3 periphery PositionValue
     * @param token0 address of token0
     * @param token1 address of token1
     * @param fee fee level of the pool
     * @param tickLower lower tick of the position
     * @param tickUpper upper tick of the position
     * @return feeGrowthInside0X128 the current fee growth of the position for token0
     * @return feeGrowthInside1X128 the current fee growth of the position for token1
     */
    function getFeeGrowthInside(
        address token0,
        address token1,
        uint24 fee,
        int24 tickLower,
        int24 tickUpper
    ) internal view returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) {
        DataCache.FeeGrowthInsideCache memory cache;
        IUniswapV3Pool pool = getPool(token0, token1, fee);
        (, cache.tickCurrent, , , , , ) = pool.slot0();

        cache.feeGrowthGlobal0X128 = pool.feeGrowthGlobal0X128();
        cache.feeGrowthGlobal1X128 = pool.feeGrowthGlobal1X128();
        (, , cache.lowerFeeGrowthOutside0X128, cache.lowerFeeGrowthOutside1X128, , , , ) = pool.ticks(tickLower);
        (, , cache.upperFeeGrowthOutside0X128, cache.upperFeeGrowthOutside1X128, , , , ) = pool.ticks(tickUpper);

        /// @dev original code with solidity <0.8 relies on under/overflow https://github.com/Uniswap/v3-core/issues/573
        unchecked {
            // calculate fee growth below
            if (cache.tickCurrent >= tickLower) {
                cache.feeGrowthBelow0X128 = cache.lowerFeeGrowthOutside0X128;
                cache.feeGrowthBelow1X128 = cache.lowerFeeGrowthOutside1X128;
            } else {
                cache.feeGrowthBelow0X128 = cache.feeGrowthGlobal0X128 - cache.lowerFeeGrowthOutside0X128;
                cache.feeGrowthBelow1X128 = cache.feeGrowthGlobal1X128 - cache.lowerFeeGrowthOutside1X128;
            }

            // calculate fee growth above
            if (cache.tickCurrent < tickUpper) {
                cache.feeGrowthAbove0X128 = cache.upperFeeGrowthOutside0X128;
                cache.feeGrowthAbove1X128 = cache.upperFeeGrowthOutside1X128;
            } else {
                cache.feeGrowthAbove0X128 = cache.feeGrowthGlobal0X128 - cache.upperFeeGrowthOutside0X128;
                cache.feeGrowthAbove1X128 = cache.feeGrowthGlobal1X128 - cache.upperFeeGrowthOutside1X128;
            }

            feeGrowthInside0X128 = cache.feeGrowthGlobal0X128 - cache.feeGrowthBelow0X128 - cache.feeGrowthAbove0X128;
            feeGrowthInside1X128 = cache.feeGrowthGlobal1X128 - cache.feeGrowthBelow1X128 - cache.feeGrowthAbove1X128;
        }
    }

    /**
     * @notice Helper function to get the fee owed based on the current and last feeGrowthInside
     * @param feeGrowthInside0X128 the current fee growth of the position for token0
     * @param feeGrowthInside1X128 the current fee growth of the position for token1
     * @param feeGrowthInside0LastX128 the fee growth of the position for token0 at the last borrow / fee collection
     * @param feeGrowthInside1LastX128 the fee growth of the position for token1 at the last borrow / fee collection
     * @param liquidity liquidity of the position
     * @return token0Owed amount of token0 owed
     * @return token1Owed amount of token1 owed
     */
    function getOwedFee(
        uint256 feeGrowthInside0X128,
        uint256 feeGrowthInside1X128,
        uint256 feeGrowthInside0LastX128,
        uint256 feeGrowthInside1LastX128,
        uint128 liquidity
    ) internal pure returns (uint128 token0Owed, uint128 token1Owed) {
        unchecked {
            token0Owed = uint128(
                FullMath.mulDiv(feeGrowthInside0X128 - feeGrowthInside0LastX128, liquidity, FixedPoint128.Q128)
            );
            token1Owed = uint128(
                FullMath.mulDiv(feeGrowthInside1X128 - feeGrowthInside1LastX128, liquidity, FixedPoint128.Q128)
            );
        }
    }

    /**
     * @notice Helper function to get the premium amount based on the premium portion and collateral as base
     * @param collateral0 the amount of collateral for token0
     * @param collateral1 the amount of collateral for token1
     * @param token0PremiumPortion the premium portion based on collateral0 and BASIS_POINT
     * @param token1PremiumPortion the premium portion based on collateral1 and BASIS_POINT
     * @return token0Premium amount of premium for token0
     * @return token1Premium amount of premium for token1
     */
    function getPremium(
        uint256 collateral0,
        uint256 collateral1,
        uint24 token0PremiumPortion,
        uint24 token1PremiumPortion
    ) internal pure returns (uint128 token0Premium, uint128 token1Premium) {
        token0Premium = uint256ToUint128((token0PremiumPortion * collateral0) / BASIS_POINT);
        token1Premium = uint256ToUint128((token1PremiumPortion * collateral1) / BASIS_POINT);
    }

    /**
     * @notice Helper function to fit a non-overflow uint256 value to uint128
     * @param value the uint256 value to fit
     * @return result uint128 value that fits
     */
    function uint256ToUint128(uint256 value) internal pure returns (uint128 result) {
        if (value > type(uint128).max) revert Errors.Overflow();
        result = uint128(value);
    }

    /**
     * @notice Helper function to fit a non-overflow uint256 value to uint24
     * @param value the uint256 value to fit
     * @return result uint24 value that fits
     */
    function uint256ToUint24(uint256 value) internal pure returns (uint24 result) {
        if (value > type(uint24).max) revert Errors.Overflow();
        result = uint24(value);
    }
}

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

import {IBlast} from "../interfaces/IBlast.sol";
import {IBlastPoints} from "../interfaces/IBlastPoints.sol";
import {IERC20Rebasing, YieldMode} from "../interfaces/IERC20Rebasing.sol";

address constant BLAST = 0x4300000000000000000000000000000000000002;
address constant USDB = 0x4300000000000000000000000000000000000003;
address constant WETHB = 0x4300000000000000000000000000000000000004;
address constant BLAST_POINTS = 0x2536FE9ab3F511540F2f9e2eC2A805005C3Dd800;

library Blast {
    event UpdateBlastPointsAdmin(address blastPointsAdmin);
    event ClaimYieldGas(address recipient, uint256 wethb, uint256 usdb, uint256 gas);

    /**
     * @notice Configure the protocol to claimable yield and gas
     */
    function configure() external {
        IBlast(BLAST).configureClaimableGas();
        IERC20Rebasing(WETHB).configure(YieldMode.CLAIMABLE);
        IERC20Rebasing(USDB).configure(YieldMode.CLAIMABLE);
    }

    /**
     * @notice Configure the protocol to claimable yield
     * @dev Need this switch in case gas mode is set to void by accident
     */
    function configureClaimableGas() external {
        IBlast(BLAST).configureClaimableGas();
    }

    /**
     * @notice Update blast points admin
     * @param admin new blast points admin
     */
    function updateBlastPointsAdmin(address admin) external {
        IBlastPoints(BLAST_POINTS).configurePointsOperator(admin);
    }

    /**
     * @notice Read the claimable amount of yield
     * @param protocol address of the protocol
     * @return weth amount of WETH to claim
     * @return usdb amount of USDB to claim
     */
    function readYield(address protocol) external view returns (uint256 weth, uint256 usdb) {
        weth = IERC20Rebasing(WETHB).getClaimableAmount(protocol);
        usdb = IERC20Rebasing(USDB).getClaimableAmount(protocol);
    }

    /**
     * @notice Claim yield and gas
     * @param recipient receiver of the yield and gas
     * @param protocol address of the protocol
     * @param wethToClaim amount of WETH to claim
     * @param usdbToClaim amount of USDB to claim
     * @return weth amount of WETH claimed
     * @return usdb amount of USDB claimed
     * @return gas amount of gas claimed
     */
    function claimYieldMaxGas(
        address recipient,
        address protocol,
        uint256 wethToClaim,
        uint256 usdbToClaim
    ) external returns (uint256 weth, uint256 usdb, uint256 gas) {
        weth = IERC20Rebasing(WETHB).claim(recipient, wethToClaim);
        usdb = IERC20Rebasing(USDB).claim(recipient, usdbToClaim);
        gas = IBlast(BLAST).claimMaxGas(protocol, recipient);
    }
}

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

import {TransferHelper} from "../../lib/v3-periphery/contracts/libraries/TransferHelper.sol";
import {INonfungiblePositionManager} from "../interfaces/INonfungiblePositionManager.sol";
import {DataStruct} from "./Structs.sol";
import {Errors} from "./Errors.sol";
import {Base} from "./Base.sol";

/// @title Liquidity Position
/// @notice Represents a liquidity position's underlying owner and fee tokens accrued from lending
library LiquidityPosition {
    struct Info {
        address owner;
        uint32 renewalCutoffTime; ///@dev loan with non-zero cutoff time can't be renewed
        uint128 token0Owed;
        uint128 token1Owed;
    }

    event SupplyLiquidity(uint256 tokenId, address lp, uint128 liquidity);
    event IncreaseLiquidity(uint256 tokenId, uint128 liquidity);
    event DecreaseLiquidity(uint256 tokenId, uint128 liquidity);
    event CollectLiquidity(uint256 tokenId, uint256 amount0, uint256 amount1);
    event ReclaimLiquidity(uint256 tokenId, uint32 cutOffTime);

    /*=============================================================
                               Info Logic
    ==============================================================*/

    /**
     * @notice Getter for a liquidity position's renewal cutoff time
     * @param self The mapping containing all liquidity positions
     * @param tokenId The token id of the liquidity position NFT
     * @return renewalCutoffTime renewal cutoff time for all previous loans
     */
    function getRenewalCutoffTime(
        mapping(uint256 => Info) storage self,
        uint256 tokenId
    ) internal view returns (uint32 renewalCutoffTime) {
        renewalCutoffTime = self[tokenId].renewalCutoffTime;
    }

    /**
     * @notice Getter for a liquidity position's tokens currently owed to owner
     * @param self The mapping containing all liquidity positions
     * @param tokenId The token id of the liquidity position NFT
     * @return token0Owed The amount of token0 owed to the owner
     * @return token1Owed The amount of token1 owed to the owner
     */
    function getTokensOwed(
        mapping(uint256 => Info) storage self,
        uint256 tokenId
    ) internal view returns (uint128 token0Owed, uint128 token1Owed) {
        Info memory info = self[tokenId];
        token0Owed = info.token0Owed;
        token1Owed = info.token1Owed;
    }

    /*=============================================================
                            Tokens Owed Logic
    ==============================================================*/

    /**
     * @notice Update a liquidity positon's owed tokens
     * @param self The mapping containing all liquidity positions
     * @param tokenId The token id of the liquidity position NFT
     * @param token0Owed The amount of token0 owed to the owner to be added
     * @param token1Owed The amount of token1 owed to the owner to be added
     */
    function addTokensOwed(
        mapping(uint256 => Info) storage self,
        uint256 tokenId,
        uint128 token0Owed,
        uint128 token1Owed
    ) internal {
        Info storage info = self[tokenId];
        info.token0Owed += token0Owed;
        info.token1Owed += token1Owed;
    }

    /**
     * @notice Reset a liquidity positon's owed tokens to 0
     * @param self The mapping containing all liquidity positions
     * @param tokenId The token id of the liquidity position NFT
     */
    function resetTokensOwed(mapping(uint256 => Info) storage self, uint256 tokenId) internal {
        Info storage info = self[tokenId];
        info.token0Owed = 0;
        info.token1Owed = 0;
    }

    /*=============================================================
                           Renewal Time Logic
    ==============================================================*/

    /**
     * @notice Update a liquidity positon's renewal cutoff time
     * @param self The mapping containing all liquidity positions
     * @param tokenId The token id of the liquidity position NFT
     * @return cutOffTime renewal cutoff time of the liquidity position
     */
    function updateRenewalCutoffTime(
        mapping(uint256 => Info) storage self,
        uint256 tokenId
    ) internal returns (uint32 cutOffTime) {
        Info storage info = self[tokenId];
        cutOffTime = uint32(block.timestamp);
        info.renewalCutoffTime = cutOffTime;
    }

    /*=============================================================
                               Mint Logic
    ==============================================================*/

    /**
     * @notice Supply liquidity to mint position NFT to the contract
     * @param self The mapping containing all liquidity positions
     * @param params mint parameters containing token pairs, fee, tick info and amount to mint
     * @return tokenId newly minted tokenId
     * @return liquidity amount of liquidity minted
     * @return amount0Minted amount of token 0 minted
     * @return amount1Minted amount of token 1 minted
     */

    function mint(
        mapping(uint256 => Info) storage self,
        DataStruct.MintParams calldata params
    ) external returns (uint256 tokenId, uint128 liquidity, uint256 amount0Minted, uint256 amount1Minted) {
        // transfer in the tokens
        TransferHelper.safeTransferFrom(params.token0, msg.sender, address(this), params.amount0ToMint);
        TransferHelper.safeTransferFrom(params.token1, msg.sender, address(this), params.amount1ToMint);

        // approve position manager to spend the tokens
        TransferHelper.safeApprove(params.token0, address(Base.UNI_POSITION_MANAGER), params.amount0ToMint);
        TransferHelper.safeApprove(params.token1, address(Base.UNI_POSITION_MANAGER), params.amount1ToMint);

        // mint the position
        (tokenId, liquidity, amount0Minted, amount1Minted) = Base.UNI_POSITION_MANAGER.mint(
            INonfungiblePositionManager.MintParams({
                token0: params.token0,
                token1: params.token1,
                fee: params.fee,
                tickLower: params.tickLower,
                tickUpper: params.tickUpper,
                amount0Desired: params.amount0ToMint,
                amount1Desired: params.amount1ToMint,
                amount0Min: params.amount0Min,
                amount1Min: params.amount1Min,
                recipient: address(this),
                deadline: block.timestamp
            })
        );

        // create the LP position
        self[tokenId] = LiquidityPosition.Info({owner: msg.sender, renewalCutoffTime: 0, token0Owed: 0, token1Owed: 0});

        // reset the approval
        TransferHelper.safeApprove(params.token0, address(Base.UNI_POSITION_MANAGER), 0);
        TransferHelper.safeApprove(params.token1, address(Base.UNI_POSITION_MANAGER), 0);

        // refund if necessary
        Base.refund(msg.sender, params.token0, params.amount0ToMint, amount0Minted);
        Base.refund(msg.sender, params.token1, params.amount1ToMint, amount1Minted);
    }

    /*=============================================================
                        Increase Liquidity Logic
    ==============================================================*/

    /**
     * @notice Increase liquidity to a liquidity position
     * @dev Caller must check for authorization and non-reentrancy
     * @param params.token0 The address of token0
     * @param params.token1 The address of token1
     * @param params.tokenId The token id of the liquidity position NFT
     * @param params.amount0 The amount of token0 to add to the liquidity position
     * @param params.amount1 The amount of token1 to add to the liquidity position
     * @param params.amount0Min The minimum amount of token0 to add to the liquidity position
     * @param params.amount1Min The minimum amount of token1 to add to the liquidity position
     * @return liquidity The amount of liquidity added
     * @return amount0Added The amount of token0 added
     * @return amount1Added The amount of token1 added
     */
    function increaseLiquidity(
        DataStruct.IncreaseLiquidityParams memory params
    ) internal returns (uint128 liquidity, uint256 amount0Added, uint256 amount1Added) {
        // approve spending for uniswap's position manager
        TransferHelper.safeApprove(params.token0, address(Base.UNI_POSITION_MANAGER), params.amount0);
        TransferHelper.safeApprove(params.token1, address(Base.UNI_POSITION_MANAGER), params.amount1);

        // increase liquidity via position manager
        (liquidity, amount0Added, amount1Added) = Base.UNI_POSITION_MANAGER.increaseLiquidity(
            INonfungiblePositionManager.IncreaseLiquidityParams({
                tokenId: params.tokenId,
                amount0Desired: params.amount0,
                amount1Desired: params.amount1,
                amount0Min: params.amount0Min,
                amount1Min: params.amount1Min,
                deadline: block.timestamp
            })
        );

        // reset approval
        TransferHelper.safeApprove(params.token0, address(Base.UNI_POSITION_MANAGER), 0);
        TransferHelper.safeApprove(params.token1, address(Base.UNI_POSITION_MANAGER), 0);
    }

    /**
     * @notice Increase liquidity of a position
     * @param self The mapping containing all liquidity positions
     * @param tokenId tokenId of the liquidity position NFT
     * @param amount0 amount to add for token 0
     * @param amount1 amount to add for token 1
     * @param amount0Min The minimum amount of token0 to add to the liquidity position
     * @param amount1Min The minimum amount of token1 to add to the liquidity position
     * @return liquidity amount of liquidity added
     * @return amount0Added amount of token 0 added
     * @return amount1Added amount of token 1 added
     */
    function increaseLiquidity(
        mapping(uint256 => Info) storage self,
        uint256 tokenId,
        uint256 amount0,
        uint256 amount1,
        uint256 amount0Min,
        uint256 amount1Min
    ) external returns (uint128 liquidity, uint256 amount0Added, uint256 amount1Added) {
        if (self[tokenId].owner != msg.sender) revert Errors.Unauthorized();

        // get token0 and token1 from the position NFT
        (, , address token0, address token1, , , , , , , , ) = Base.UNI_POSITION_MANAGER.positions(tokenId);

        // transfer in liquidity to add
        TransferHelper.safeTransferFrom(token0, msg.sender, address(this), amount0);
        TransferHelper.safeTransferFrom(token1, msg.sender, address(this), amount1);

        // add liquidity
        (liquidity, amount0Added, amount1Added) = increaseLiquidity(
            DataStruct.IncreaseLiquidityParams({
                token0: token0,
                token1: token1,
                tokenId: tokenId,
                amount0: amount0,
                amount1: amount1,
                amount0Min: amount0Min,
                amount1Min: amount1Min
            })
        );

        // refund if necessary
        Base.refund(msg.sender, token0, amount0, amount0Added);
        Base.refund(msg.sender, token1, amount1, amount1Added);
    }

    /*=============================================================
                        Decrease Liquidity Logic
    ==============================================================*/

    /**
     * @notice Decrease liquidity from an existing position
     * @dev Caller must check for authorization and non-reentrancy
     * @param tokenId tokenId of the liquidity position NFT
     * @param liquidity amount to decrease
     * @param amount0Min minimum amount of token0 to decrease
     * @param amount1Min minimum amount of token1 to decrease
     * @return amount0 amount decreased for token0
     * @return amount1 amount decreased for token1
     */
    function decreaseLiquidity(
        uint256 tokenId,
        uint128 liquidity,
        uint256 amount0Min,
        uint256 amount1Min
    ) internal returns (uint256 amount0, uint256 amount1) {
        (amount0, amount1) = Base.UNI_POSITION_MANAGER.decreaseLiquidity(
            INonfungiblePositionManager.DecreaseLiquidityParams({
                tokenId: tokenId,
                liquidity: liquidity,
                amount0Min: amount0Min,
                amount1Min: amount1Min,
                deadline: block.timestamp
            })
        );
    }

    /**
     * @notice Decrease liquidity from a position
     * @param self The mapping containing all liquidity positions
     * @param tokenId tokenId of the liquidity position NFT
     * @param liquidity amount of liquidity to add
     * @param amount0Min minimum amount of token0 to decrease
     * @param amount1Min minimum amount of token1 to decrease
     * @return amount0Decreased amount of token 0 decreased
     * @return amount1Decreased amount of token 1 decreased
     */
    function decreaseLiquidity(
        mapping(uint256 => Info) storage self,
        uint256 tokenId,
        uint128 liquidity,
        uint256 amount0Min,
        uint256 amount1Min
    ) external returns (uint256 amount0Decreased, uint256 amount1Decreased) {
        if (self[tokenId].owner != msg.sender) revert Errors.Unauthorized();
        (amount0Decreased, amount1Decreased) = decreaseLiquidity(tokenId, liquidity, amount0Min, amount1Min);
    }

    /*=============================================================
                        Collect Liquidity Logic
    ==============================================================*/

    /**
     * @notice Collect fees from a position
     * @dev Caller must check for authorization and non-reentrancy
     * @param tokenId tokenId of the liquidity position NFT
     * @param amount0Max maximum amount of token0 to collect
     * @param amount1Max maximum amount of token1 to collect
     * @param recipient the address to collect the liquidity
     * @return amount0 amount collected for token0
     * @return amount1 amount collected for token1
     */
    function collectLiquidity(
        uint256 tokenId,
        uint128 amount0Max,
        uint128 amount1Max,
        address recipient
    ) internal returns (uint256 amount0, uint256 amount1) {
        (amount0, amount1) = Base.UNI_POSITION_MANAGER.collect(
            INonfungiblePositionManager.CollectParams({
                tokenId: tokenId,
                recipient: recipient,
                amount0Max: amount0Max,
                amount1Max: amount1Max
            })
        );
    }

    /**
     * @notice Collect fees from a position
     * @param self The mapping containing all liquidity positions
     * @param tokenId tokenId of the liquidity position NFT
     * @return amount0Collected amount of fees collected in token 0
     * @return amount1Collected amount of fees collected in token 1
     */
    function collectLiquidity(
        mapping(uint256 => Info) storage self,
        uint256 tokenId
    ) external returns (uint256 amount0Collected, uint256 amount1Collected) {
        if (self[tokenId].owner != msg.sender) revert Errors.Unauthorized();
        (amount0Collected, amount1Collected) = LiquidityPosition.collectLiquidity(
            tokenId,
            type(uint128).max,
            type(uint128).max,
            msg.sender
        );
        (uint128 token0Owed, uint128 token1Owed) = getTokensOwed(self, tokenId);
        resetTokensOwed(self, tokenId);
        (, , address token0, address token1, , , , , , , , ) = Base.UNI_POSITION_MANAGER.positions(tokenId);
        if (token0Owed > 0) {
            amount0Collected += token0Owed;
            TransferHelper.safeTransfer(token0, msg.sender, token0Owed);
        }
        if (token1Owed > 0) {
            amount1Collected += token1Owed;
            TransferHelper.safeTransfer(token1, msg.sender, token1Owed);
        }
    }

    /*=============================================================
                         Reclaim Liquidity Logic
    ==============================================================*/

    /**
     * @notice LP reclaims borrowed liquidity from being renewed
     * @param self The mapping containing all liquidity positions
     * @param tokenId tokenId of the liquidity position NFT
     * @return cutOffTime renewal cutoff time of the liquidity position
     */
    function reclaimLiquidity(
        mapping(uint256 => Info) storage self,
        uint256 tokenId
    ) external returns (uint32 cutOffTime) {
        if (self[tokenId].owner != msg.sender) revert Errors.Unauthorized();
        cutOffTime = updateRenewalCutoffTime(self, tokenId);
    }
}

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

import {TransferHelper} from "../../lib/v3-periphery/contracts/libraries/TransferHelper.sol";
import {DataStruct, DataCache} from "./Structs.sol";
import {Errors} from "./Errors.sol";
import {Base} from "./Base.sol";
import {LiquidityPosition} from "./LiquidityPosition.sol";

/// @title Lien
/// @notice Represents a open position's locked premium, liquidity and owed tokwn amounts
/// @dev Only stores the minimally required information to close/liquidate a position
library Lien {
    using LiquidityPosition for mapping(uint256 => LiquidityPosition.Info);

    struct Info {
        uint40 tokenId;
        uint128 liquidity;
        uint24 token0PremiumPortion;
        uint24 token1PremiumPortion;
        uint32 startTime;
        bool zeroForOne;
        uint256 feeGrowthInside0LastX128;
        uint256 feeGrowthInside1LastX128;
    }

    event OpenPosition(address borrower, uint96 lienId, uint256 positionAmount);
    event ClosePosition(address borrower, uint256 tokenId, uint256 token0Refund, uint256 token1Refund);
    event LiquidatePosition(address borrower, uint256 tokenId, uint256 token0Refund, uint256 token1Refund);
    event AddPremium(uint256 lienId, uint128 premium0, uint128 premium1, uint256 spending);

    /*=============================================================
                             Open Position
    ==============================================================*/

    /**
     * @notice Basic open position of a leveraged swap
     * @dev The passed-in collateral amountIn and liquidity are best effort simulated from frontend
     * @param self The mapping containing all liens
     * @param lps The mapping containing all liquidity positions
     * @param treasury The mapping containing all treasury amounts
     * @param lienId ID for the newly created lien
     * @param feeFactor The factor to calculate fee
     * @param treasuryRate The rate to calculate treasury amount
     * @param params.tokenId tokenId of the liquidity position NFT
     * @param params.marginFrom amount of collateral + premium for the token to be swapped from
     * @param params.marginTo amount of collateral + premium for the token to be swapped to
     * @param params.amountSwap amount of tokenFrom to actually swap (will be margin + borrowed - premium)
     * @param params.liquidity amount of liquidity to borrow out
     * @param params.tokenFromPremiumPortionMin minimum premium of tokenFrom as portion of tokenFrom collateral
     * @param params.tokenToPremiumPortionMin minimum premium of tokenTo as portion of tokenTo collateral
     * @param params.fee fee tier for the swap
     * @param params.amountReceivedMinimum minimum amount to receive from swap
     * @param params.amount0BorrowedMin minimum amount of token0 to borrow for slippage protection
     * @param params.amount1BorrowedMin minimum amount of token1 to borrow for slippage protection
     * @param params.leverageRatio @dev only for frontend display purpose, all leftover amounts are premiums
     * @param params.zeroForOne direction of the swap
     * @return collateralTo amount of target token locked in the position (deposited, borrowed and swapped)
     */
    function openPosition(
        mapping(bytes32 => Info) storage self,
        mapping(uint256 => LiquidityPosition.Info) storage lps,
        mapping(address => uint256) storage treasury,
        uint96 lienId,
        uint256 feeFactor,
        uint256 treasuryRate,
        DataStruct.OpenPositionParams calldata params
    ) external returns (uint256 collateralTo) {
        if (params.liquidity == 0) revert Errors.InsufficientBorrow();

        /// @dev avoid approval misuse (uniswap decrease/collect liquidity only requires _isApprovedOrOwner)
        if (Base.UNI_POSITION_MANAGER.ownerOf(params.tokenId) != address(this)) revert Errors.InvalidToken();

        // check if the liquidity already reclaimed
        if (lps.getRenewalCutoffTime(params.tokenId) > 0) revert Errors.LiquidityReclaimed();

        // local cache to avoid stack too deep
        DataCache.OpenPositionCache memory cache;

        // prepare data for swap
        (cache.tokenFrom, cache.tokenTo, cache.collateralFrom, collateralTo) = Base.prepareLeverage(
            params.tokenId,
            params.liquidity,
            params.zeroForOne
        );

        // decrease liquidity from LP position, pull the amount to this contract
        (cache.amountFromBorrowed, cache.amountToBorrowed) = LiquidityPosition.decreaseLiquidity(
            params.tokenId,
            params.liquidity,
            params.amount0BorrowedMin,
            params.amount1BorrowedMin
        );
        LiquidityPosition.collectLiquidity(
            params.tokenId,
            uint128(cache.amountFromBorrowed),
            uint128(cache.amountToBorrowed),
            address(this)
        );
        if (!params.zeroForOne)
            (cache.amountFromBorrowed, cache.amountToBorrowed) = (cache.amountToBorrowed, cache.amountFromBorrowed);

        // start counting the fee that the borrowed liquidity would have otherwise earned
        (, , , , , , , , cache.feeGrowthInside0LastX128, cache.feeGrowthInside1LastX128, , ) = Base
            .UNI_POSITION_MANAGER
            .positions(params.tokenId);

        // transfer in enough collateral
        if (params.marginFrom > 0) {
            TransferHelper.safeTransferFrom(cache.tokenFrom, msg.sender, address(this), params.marginFrom);
        }
        if (params.marginTo > 0) {
            TransferHelper.safeTransferFrom(cache.tokenTo, msg.sender, address(this), params.marginTo);
        }

        // pay for fee
        if (feeFactor > 0) {
            cache.feeAmount = ((params.marginFrom + cache.amountFromBorrowed) * feeFactor) / Base.BASIS_POINT;
            if (treasuryRate > 0) {
                cache.treasuryAmount = (cache.feeAmount * treasuryRate) / Base.BASIS_POINT;
                treasury[cache.tokenFrom] += cache.treasuryAmount;
            }
            if (params.zeroForOne) {
                lps.addTokensOwed(params.tokenId, uint128(cache.feeAmount - cache.treasuryAmount), 0);
            } else {
                lps.addTokensOwed(params.tokenId, 0, uint128(cache.feeAmount - cache.treasuryAmount));
            }
        }

        // cannot swap more than available amount
        if (params.amountSwap > params.marginFrom + cache.amountFromBorrowed - cache.feeAmount)
            revert Errors.OverSpend();

        // swap to meet the collateral requirement
        if (params.amountSwap > 0) {
            cache.amountReceived = Base.swapExactIn(
                cache.tokenFrom,
                cache.tokenTo,
                params.fee,
                params.amountSwap,
                (cache.amountToBorrowed + params.marginTo) > collateralTo /// @dev to avoid large marginTo overflow
                    ? params.amountReceivedMinimum
                    : params.amountReceivedMinimum > (collateralTo - cache.amountToBorrowed - params.marginTo)
                        ? params.amountReceivedMinimum
                        : (collateralTo - cache.amountToBorrowed - params.marginTo), // amount needed to meet requirement
                0
            );
        }

        // leftover amounts from the collateral are now premiums, and ensure enough premium is stored
        if (params.zeroForOne) {
            cache.token0PremiumPortion = Base.uint256ToUint24(
                ((params.marginFrom + cache.amountFromBorrowed - cache.feeAmount - params.amountSwap) *
                    Base.BASIS_POINT) / cache.collateralFrom
            );
            cache.token1PremiumPortion = Base.uint256ToUint24(
                ((cache.amountReceived + cache.amountToBorrowed + params.marginTo - collateralTo) * Base.BASIS_POINT) /
                    collateralTo
            );
            if (
                cache.token0PremiumPortion < params.tokenFromPremiumPortionMin ||
                cache.token1PremiumPortion < params.tokenToPremiumPortionMin
            ) revert Errors.InsufficientPremium();
        } else {
            cache.token1PremiumPortion = Base.uint256ToUint24(
                ((params.marginFrom + cache.amountFromBorrowed - cache.feeAmount - params.amountSwap) *
                    Base.BASIS_POINT) / cache.collateralFrom
            );
            cache.token0PremiumPortion = Base.uint256ToUint24(
                ((cache.amountReceived + cache.amountToBorrowed + params.marginTo - collateralTo) * Base.BASIS_POINT) /
                    collateralTo
            );
            if (
                cache.token0PremiumPortion < params.tokenToPremiumPortionMin ||
                cache.token1PremiumPortion < params.tokenFromPremiumPortionMin
            ) revert Errors.InsufficientPremium();
        }

        // create a new lien
        self[keccak256(abi.encodePacked(msg.sender, lienId))] = Lien.Info({
            tokenId: uint40(params.tokenId),
            liquidity: params.liquidity,
            token0PremiumPortion: cache.token0PremiumPortion,
            token1PremiumPortion: cache.token1PremiumPortion,
            startTime: uint32(block.timestamp),
            feeGrowthInside0LastX128: cache.feeGrowthInside0LastX128,
            feeGrowthInside1LastX128: cache.feeGrowthInside1LastX128,
            zeroForOne: params.zeroForOne
        });
    }

    /*=============================================================
                             Close Position
    ==============================================================*/

    /**
     * @notice Basic close position of a leveraged swap
     * @dev The passed-in amount to return/gain are best effort simulated from frontend
     * @param self The mapping containing all liens
     * @param lps The mapping containing all liquidity positions
     * @param params.lienId the ID for the existing loan
     * @param params.amountSwap total amount of tokenFrom (the tokenTo in openPosition) to swap back
     * @param params.fee fee tier for the swap
     * @param params.amountReceivedMinimum minimum amount to receive from swap
     * @param params.liquidityFactor liquidity factor for increaseLiquidity slippage protection
     * @return tokenId The tokenId of the liquidity position NFT
     * @return token0Refund The amount of token0 to refund
     * @return token1Refund The amount of token1 to refund
     */
    function closePosition(
        mapping(bytes32 => Info) storage self,
        mapping(uint256 => LiquidityPosition.Info) storage lps,
        DataStruct.ClosePositionParams calldata params
    ) external returns (uint40 tokenId, uint256 token0Refund, uint256 token1Refund) {
        bytes32 lienKey = keccak256(abi.encodePacked(msg.sender, params.lienId));
        Lien.Info memory lien = self[lienKey];
        tokenId = lien.tokenId;

        // check lien is valid
        if (lien.liquidity == 0) revert Errors.RecordEmpty();

        // delete lien from storage
        delete self[lienKey];

        // local cache to avoid stack too deep
        DataCache.ClosePositionCache memory cache;

        // prepare data for swap back
        ///@dev the token/collateralFrom and token/collateralTo are swapped compared to openPosition
        (cache.tokenTo, cache.tokenFrom, cache.collateralTo, cache.collateralFrom) = Base.prepareLeverage(
            lien.tokenId,
            lien.liquidity,
            lien.zeroForOne
        );

        // get the amount of premium in the lien
        if (lien.zeroForOne) {
            (cache.tokenToPremium, cache.tokenFromPremium) = Base.getPremium(
                cache.collateralTo,
                cache.collateralFrom,
                lien.token0PremiumPortion,
                lien.token1PremiumPortion
            );
        } else {
            (cache.tokenFromPremium, cache.tokenToPremium) = Base.getPremium(
                cache.collateralFrom,
                cache.collateralTo,
                lien.token0PremiumPortion,
                lien.token1PremiumPortion
            );
        }

        // execute actual position closing
        (token0Refund, token1Refund) = executeClosePosition(lps, params, cache, lien, msg.sender);
    }

    /**
     * @notice Internal function to close a position
     * @dev Caller must ensure either the msg.sender is borrower or liquidation condition is met
     * @param lps The mapping containing all liquidity positions
     * @param params close position parameters
     * @param cache local cache to avoid stack too deep
     * @param lien lien info
     * @param borrower borrower address
     * @return token0Refund The amount of token0 to refund
     * @return token1Refund The amount of token1 to refund
     */
    function executeClosePosition(
        mapping(uint256 => LiquidityPosition.Info) storage lps,
        DataStruct.ClosePositionParams calldata params,
        DataCache.ClosePositionCache memory cache,
        Lien.Info memory lien,
        address borrower
    ) internal returns (uint256 token0Refund, uint256 token1Refund) {
        // optimistically use the input numbers to swap for repay
        /// @dev amountSwap overspend will be caught by refundWithCheck step in below
        if (params.amountSwap > 0) {
            cache.amountReceived = Base.swapExactIn(
                cache.tokenFrom,
                cache.tokenTo,
                params.fee,
                params.amountSwap,
                params.amountReceivedMinimum,
                0
            );
        }

        // based on borrowed liquidity, compute the required return amount
        /// @dev the from-to swapping direction is reverted compared to openPosition
        (cache.amountToAdd, cache.amountFromAdd) = Base.getRequiredRepay(lien.liquidity, lien.tokenId);
        if (!lien.zeroForOne) (cache.amountToAdd, cache.amountFromAdd) = (cache.amountFromAdd, cache.amountToAdd);

        // the liquidity to add must be no less than the available amount
        /// @dev the max available amount contains the tokensOwed, will have another check in below at refundWithCheck
        if (
            cache.amountFromAdd + params.amountSwap > cache.collateralFrom + cache.tokenFromPremium ||
            cache.amountToAdd > cache.amountReceived + cache.tokenToPremium
        ) {
            revert Errors.InsufficientRepay();
        }

        // add liquidity back to borrower
        if (lien.zeroForOne) {
            (cache.liquidityAdded, cache.amountToAdd, cache.amountFromAdd) = LiquidityPosition.increaseLiquidity(
                DataStruct.IncreaseLiquidityParams({
                    token0: cache.tokenTo,
                    token1: cache.tokenFrom,
                    tokenId: lien.tokenId,
                    amount0: cache.amountToAdd,
                    amount1: cache.amountFromAdd,
                    amount0Min: (cache.amountToAdd * params.liquidityFactor) / Base.BASIS_POINT,
                    amount1Min: (cache.amountFromAdd * params.liquidityFactor) / Base.BASIS_POINT
                })
            );
        } else {
            (cache.liquidityAdded, cache.amountFromAdd, cache.amountToAdd) = LiquidityPosition.increaseLiquidity(
                DataStruct.IncreaseLiquidityParams({
                    token0: cache.tokenFrom,
                    token1: cache.tokenTo,
                    tokenId: lien.tokenId,
                    amount0: cache.amountFromAdd,
                    amount1: cache.amountToAdd,
                    amount0Min: (cache.amountFromAdd * params.liquidityFactor) / Base.BASIS_POINT,
                    amount1Min: (cache.amountToAdd * params.liquidityFactor) / Base.BASIS_POINT
                })
            );
        }

        // obtain the position's latest FeeGrowthInside after increaseLiquidity
        (, , , , , , , , cache.feeGrowthInside0LastX128, cache.feeGrowthInside1LastX128, , ) = Base
            .UNI_POSITION_MANAGER
            .positions(lien.tokenId);

        // caculate the amounts owed since last fee collection during the borrowing period
        (cache.token0Owed, cache.token1Owed) = Base.getOwedFee(
            cache.feeGrowthInside0LastX128,
            cache.feeGrowthInside1LastX128,
            lien.feeGrowthInside0LastX128,
            lien.feeGrowthInside1LastX128,
            lien.liquidity
        );

        // calculate the the amounts owed to LP up to the premium in the lien
        // must ensure enough amount is left to pay for interest first, then send gains and fund left to borrower
        ///@dev refundWithCheck ensures actual cannot be more than expected, since amount owed to LP is in actual,
        ///     it ensures (1) on the collateralFrom part of refund, tokenOwed is covered, and (2) on the amountReceived
        ///      part, received is no less than liquidity addback + token owed.
        if (lien.zeroForOne) {
            cache.token0Owed = cache.token0Owed < cache.tokenToPremium ? cache.token0Owed : cache.tokenToPremium;
            cache.token1Owed = cache.token1Owed < cache.tokenFromPremium ? cache.token1Owed : cache.tokenFromPremium;
            token1Refund = Base.refundWithCheck(
                borrower,
                cache.tokenFrom,
                cache.collateralFrom + cache.tokenFromPremium,
                params.amountSwap + cache.amountFromAdd + cache.token1Owed
            );
            token0Refund = Base.refundWithCheck(
                borrower,
                cache.tokenTo,
                cache.amountReceived + cache.tokenToPremium,
                cache.amountToAdd + cache.token0Owed
            );
        } else {
            cache.token0Owed = cache.token0Owed < cache.tokenFromPremium ? cache.token0Owed : cache.tokenFromPremium;
            cache.token1Owed = cache.token1Owed < cache.tokenToPremium ? cache.token1Owed : cache.tokenToPremium;
            token0Refund = Base.refundWithCheck(
                borrower,
                cache.tokenFrom,
                cache.collateralFrom + cache.tokenFromPremium,
                params.amountSwap + cache.amountFromAdd + cache.token0Owed
            );
            token1Refund = Base.refundWithCheck(
                borrower,
                cache.tokenTo,
                cache.amountReceived + cache.tokenToPremium,
                cache.amountToAdd + cache.token1Owed
            );
        }

        // pay for interest
        lps.addTokensOwed(lien.tokenId, cache.token0Owed, cache.token1Owed);
    }

    /*=============================================================
                           Liquidate Position
    ==============================================================*/

    /**
     * @notice Basic liquidation position of a leveraged swap
     * @dev The passed-in amount to pay for LP is best effort simulated from frontend
     * @param self The mapping containing all liens
     * @param lps The mapping containing all liquidity positions
     * @param params.lienId the ID for the existing loan
     * @param params.amountSwap total amount of tokenFrom (the tokenTo in openPosition) to swap back
     * @param params.fee fee tier for the swap
     * @param params.amountReceivedMinimum minimum amount to receive from swap
     * @param params.liquidityFactor liquidity factor for increaseLiquidity slippage protection
     * @param borrower the address
     * @param liquidationRewardFactor the factor to calculate liquidation reward
     * @param loanTerm the duration of the loan
     * @return tokenId The tokenId of the liquidity position NFT
     * @return token0Refund The amount of token0 to refund
     * @return token1Refund The amount of token1 to refund
     */
    function liquidatePosition(
        mapping(bytes32 => Info) storage self,
        mapping(uint256 => LiquidityPosition.Info) storage lps,
        DataStruct.ClosePositionParams calldata params,
        address borrower,
        uint128 liquidationRewardFactor,
        uint256 loanTerm
    ) external returns (uint40 tokenId, uint256 token0Refund, uint256 token1Refund) {
        bytes32 lienKey = keccak256(abi.encodePacked(borrower, params.lienId));
        Lien.Info memory lien = self[lienKey];
        tokenId = lien.tokenId;

        // check lien is valid
        if (lien.liquidity == 0) revert Errors.RecordEmpty();

        // local cache to avoid stack too deep
        DataCache.ClosePositionCache memory closeCache;
        DataCache.LiquidatePositionCache memory liquidateCache;

        // get liquidation parameters
        ///@dev calculate premium outside of executeClosePosition to allow liquidatePosition to take reward from premium
        (
            closeCache.tokenFrom,
            closeCache.tokenTo,
            liquidateCache.tokenFromOwed,
            liquidateCache.tokenToOwed,
            closeCache.tokenFromPremium,
            closeCache.tokenToPremium,
            closeCache.collateralFrom,

        ) = Base.getOwedInfoConverted(
            DataStruct.OwedInfoParams({
                tokenId: lien.tokenId,
                liquidity: lien.liquidity,
                feeGrowthInside0LastX128: lien.feeGrowthInside0LastX128,
                feeGrowthInside1LastX128: lien.feeGrowthInside1LastX128,
                token0PremiumPortion: lien.token0PremiumPortion,
                token1PremiumPortion: lien.token1PremiumPortion
            }),
            lien.zeroForOne
        );

        // calculate liquidation reward
        liquidateCache.liquidationRewardFrom =
            ((closeCache.tokenFromPremium) * liquidationRewardFactor) /
            uint128(Base.BASIS_POINT);
        liquidateCache.liquidationRewardTo =
            ((closeCache.tokenToPremium) * liquidationRewardFactor) /
            uint128(Base.BASIS_POINT);
        closeCache.tokenFromPremium -= liquidateCache.liquidationRewardFrom;
        closeCache.tokenToPremium -= liquidateCache.liquidationRewardTo;

        // check for liquidation condition
        liquidateCache.renewalCutoffTime = lps.getRenewalCutoffTime(tokenId);
        if (
            !isLiquidatable(
                closeCache.tokenFromPremium,
                closeCache.tokenToPremium,
                liquidateCache.tokenFromOwed,
                liquidateCache.tokenToOwed,
                liquidateCache.renewalCutoffTime,
                loanTerm
            )
        ) {
            revert Errors.LiquidationNotMet();
        }

        // delete lien from storage
        delete self[lienKey];

        // execute actual position closing
        (token0Refund, token1Refund) = executeClosePosition(lps, params, closeCache, lien, borrower);

        // reward liquidator
        if (liquidateCache.liquidationRewardFrom > 0) {
            TransferHelper.safeTransfer(closeCache.tokenFrom, msg.sender, liquidateCache.liquidationRewardFrom);
        }
        if (liquidateCache.liquidationRewardTo > 0) {
            TransferHelper.safeTransfer(closeCache.tokenTo, msg.sender, liquidateCache.liquidationRewardTo);
        }
    }

    /**
     * @notice Helper function to check if a position is liquidatable
     * @param tokenFromPremium premium of tokenFrom
     * @param tokenToPremium premium of tokenTo
     * @param tokenFromOwed amount of tokenFrom owed to LP
     * @param tokenToOwed amount of tokenTo owed to LP
     * @param cutoffTime position renewal cutoff time
     * @return true if the position is liquidatable
     */
    function isLiquidatable(
        uint128 tokenFromPremium,
        uint128 tokenToPremium,
        uint128 tokenFromOwed,
        uint128 tokenToOwed,
        uint32 cutoffTime,
        uint256 loanTerm
    ) public view returns (bool) {
        ///@dev the liquidation condition is that
        ///     EITHER (premium is not enough) OR (cutOffTime > 0 AND currentTime > cutOffTime + LOAN_TERM)
        return
            (tokenFromPremium < tokenFromOwed || tokenToPremium < tokenToOwed) ||
            (cutoffTime > 0 && cutoffTime + loanTerm < block.timestamp);
    }

    /*=============================================================
                             Premium Logic
    ==============================================================*/

    /**
     * @notice Update a lien's preminum amounts
     * @dev Caller of this function should ensure lien is renewable by checking cutoff time
     * @param self The mapping containing all liens
     * @param lienKey The key of the lien (borrower, lienId)
     * @param token0PremiumPortion The amount of token0 premium porition to update to
     * @param token1PremiumPortion The amount of token1 premium porition to update to
     * @dev the premium amount (lifetime premium in a position) is capped at uint24.MAX
     */
    function updatePremium(
        mapping(bytes32 => Info) storage self,
        bytes32 lienKey,
        uint24 token0PremiumPortion,
        uint24 token1PremiumPortion
    ) internal {
        self[lienKey].token0PremiumPortion = token0PremiumPortion;
        self[lienKey].token1PremiumPortion = token1PremiumPortion;
        self[lienKey].startTime = uint32(block.timestamp);
    }

    /**
     * @notice Add premium to a lien
     * @param self The mapping containing all liens
     * @param params.lienId ID for the existing lien
     * @param params.amountToAdd amount of a token premium to directly add
     * @param params.amountToSwap amount of a token to swap to the other side
     * @param params.fee fee tier for the swap
     * @param params.amountToReceiveMinimum minimum amount to receive from the swap
     * @param params.zeroForone direction of the swap
     * @return premium0 The new amount of token0 premium
     * @return premium1 The new amount of token1 premium
     * @return spending The amount of token spent (amountToAdd and amountToSwap)
     */
    function addPremium(
        mapping(bytes32 => Info) storage self,
        DataStruct.AddPremiumParams calldata params
    ) external returns (uint128 premium0, uint128 premium1, uint256 spending) {
        DataCache.AddPremiumCache memory cache;

        cache.lienKey = keccak256(abi.encodePacked(msg.sender, params.lienId));
        Lien.Info memory lien = self[cache.lienKey];

        // check lien is valid
        if (lien.liquidity == 0) revert Errors.RecordEmpty();

        // fetch information based on lien
        (, , cache.token0, cache.token1, , cache.tickLower, cache.tickUpper, , , , , ) = Base
            .UNI_POSITION_MANAGER
            .positions(lien.tokenId);
        (cache.collateral0, cache.collateral1) = Base.getRequiredCollateral(
            lien.liquidity,
            cache.tickLower,
            cache.tickUpper
        );

        // get existing premium
        (cache.token0Premium, cache.token1Premium) = Base.getPremium(
            cache.collateral0,
            cache.collateral1,
            lien.token0PremiumPortion,
            lien.token1PremiumPortion
        );

        // transfer in enough amount
        spending = params.amountToAdd + params.amountToSwap;
        if (params.amountToAdd > 0 || params.amountToSwap > 0) {
            TransferHelper.safeTransferFrom(
                params.zeroForOne ? cache.token0 : cache.token1,
                msg.sender,
                address(this),
                spending
            );
        }

        // perform swap
        if (params.amountToSwap > 0) {
            if (params.zeroForOne) {
                cache.amountReceived = Base.swapExactIn(
                    cache.token0,
                    cache.token1,
                    params.fee,
                    params.amountToSwap,
                    params.amountToReceiveMinimum,
                    0
                );
            } else {
                cache.amountReceived = Base.swapExactIn(
                    cache.token1,
                    cache.token0,
                    params.fee,
                    params.amountToSwap,
                    params.amountToReceiveMinimum,
                    0
                );
            }
        }

        // determine the premium to add
        (premium0, premium1) = params.zeroForOne
            ? (
                params.amountToAdd + cache.token0Premium,
                Base.uint256ToUint128(cache.amountReceived) + cache.token1Premium
            )
            : (
                Base.uint256ToUint128(cache.amountReceived) + cache.token0Premium,
                params.amountToAdd + cache.token1Premium
            );

        // add premium
        updatePremium(
            self,
            cache.lienKey,
            Base.uint256ToUint24((premium0 * Base.BASIS_POINT) / cache.collateral0),
            Base.uint256ToUint24((premium1 * Base.BASIS_POINT) / cache.collateral1)
        );
    }
}

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

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

/// @title Signatures
/// @notice Verifies oracle signature for function arguments and message sender
library Signatures {
    /**
     * @notice Verifies the signature of the message sender for the given hash
     * @param hash The hash of the message
     * @param signature The signature of the message
     * @param blockNumber The block number of the message
     * @param oracleSigner The address of the oracle signer
     * @param validBlocks The number of valid blocks for the message
     */
    function verifySignature(
        bytes32 hash,
        bytes calldata signature,
        uint256 blockNumber,
        address oracleSigner,
        uint256 validBlocks
    ) internal view {
        if (oracleSigner != address(0)) {
            // Verify the block number
            if (blockNumber + validBlocks < block.number) revert Errors.InvalidBlockNumber();

            // Verify the signature
            address signer = recoverSigner(
                keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)), ///@dev EIP191
                signature
            );

            if (signer != oracleSigner) revert Errors.InvalidSignature();
        }
    }

    /**
     * @notice Recovers the signer of the message from the signature
     * @param hash The hash of the message
     * @param signature The signature of the message
     * @return recoveredSigner The address of the recovered signer
     */
    function recoverSigner(bytes32 hash, bytes calldata signature) internal pure returns (address) {
        // Signature recovery logic
        (bytes32 r, bytes32 s, uint8 v) = splitSignature(signature);
        return ecrecover(hash, v, r, s);
    }

    /**
     * @notice Splits the signature into r, s and v
     * @param sig The signature of the message
     * @return r The r value of the signature
     * @return s The s value of the signature
     * @return v The v value of the signature
     */
    function splitSignature(bytes memory sig) internal pure returns (bytes32 r, bytes32 s, uint8 v) {
        require(sig.length == 65, "Invalid signature length");

        // solhint-disable-next-line no-inline-assembly
        assembly {
            /*
            First 32 bytes stores the length of the signature

            add(sig, 32) = pointer of sig + 32
            effectively, skips first 32 bytes of signature

            mload(p) loads next 32 bytes starting at the memory address p into memory
            */

            // first 32 bytes, after the length prefix
            r := mload(add(sig, 32))
            // second 32 bytes
            s := mload(add(sig, 64))
            // final byte (first byte of the next 32 bytes)
            v := byte(0, mload(add(sig, 96)))
        }
    }
}

File 14 of 56 : Structs.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// structs to pass in between functions
library DataStruct {
    struct OpenPositionParams {
        uint256 tokenId;
        uint256 marginFrom;
        uint256 marginTo;
        uint256 amountSwap;
        uint128 liquidity;
        uint24 tokenFromPremiumPortionMin;
        uint24 tokenToPremiumPortionMin;
        uint24 fee;
        uint256 amountReceivedMinimum;
        uint256 amount0BorrowedMin;
        uint256 amount1BorrowedMin;
        uint8 leverageRatio; /// @dev only for frontend display purpose
        bool zeroForOne;
    }

    struct ClosePositionParams {
        uint96 lienId;
        uint256 amountSwap;
        uint24 fee;
        uint256 amountReceivedMinimum;
        uint256 liquidityFactor;
    }

    struct AddPremiumParams {
        uint96 lienId;
        uint128 amountToAdd;
        uint256 amountToSwap;
        uint24 fee;
        uint256 amountToReceiveMinimum;
        bool zeroForOne;
    }

    struct OwedInfoParams {
        uint40 tokenId;
        uint128 liquidity;
        uint256 feeGrowthInside0LastX128;
        uint256 feeGrowthInside1LastX128;
        uint24 token0PremiumPortion;
        uint24 token1PremiumPortion;
    }

    struct MintParams {
        address token0;
        address token1;
        uint24 fee;
        int24 tickLower;
        int24 tickUpper;
        uint256 amount0ToMint;
        uint256 amount1ToMint;
        uint256 amount0Min;
        uint256 amount1Min;
    }

    struct IncreaseLiquidityParams {
        address token0;
        address token1;
        uint256 tokenId;
        uint256 amount0;
        uint256 amount1;
        uint256 amount0Min;
        uint256 amount1Min;
    }
}

// structs to use locally within functions
library DataCache {
    struct OpenPositionCache {
        address tokenFrom;
        address tokenTo;
        uint256 amountFromBorrowed;
        uint256 amountToBorrowed;
        uint256 amountReceived;
        uint256 collateralFrom; ///@dev collateralTo is the position amount in the returns
        uint24 token0PremiumPortion;
        uint24 token1PremiumPortion;
        uint256 feeGrowthInside0LastX128;
        uint256 feeGrowthInside1LastX128;
        uint256 feeAmount;
        uint256 treasuryAmount;
    }

    struct ClosePositionCache {
        address tokenFrom;
        address tokenTo;
        uint256 collateralFrom;
        uint256 collateralTo;
        uint128 tokenFromPremium;
        uint128 tokenToPremium;
        uint256 amountReceived;
        uint128 liquidityAdded;
        uint256 amountFromAdd;
        uint256 amountToAdd;
        uint256 feeGrowthInside0LastX128;
        uint256 feeGrowthInside1LastX128;
        uint128 token0Owed;
        uint128 token1Owed;
    }

    struct LiquidatePositionCache {
        uint128 tokenFromOwed;
        uint128 tokenToOwed;
        uint32 renewalCutoffTime;
        uint128 liquidationRewardFrom;
        uint128 liquidationRewardTo;
    }

    struct AddPremiumCache {
        bytes32 lienKey;
        address token0;
        address token1;
        int24 tickLower;
        int24 tickUpper;
        uint256 collateral0;
        uint256 collateral1;
        uint128 token0Premium;
        uint128 token1Premium;
        uint256 amountReceived;
    }

    struct OwedInfoCache {
        uint24 fee;
        int24 tickLower;
        int24 tickUpper;
        uint256 feeGrowthInside0X128;
        uint256 feeGrowthInside1X128;
    }

    struct RepayCache {
        address token0;
        address token1;
        uint24 fee;
        int24 tickLower;
        int24 tickUpper;
        uint160 sqrtRatioX96;
        uint160 sqrtRatioAX96;
        uint160 sqrtRatioBX96;
    }

    struct FeeGrowthInsideCache {
        int24 tickCurrent;
        uint256 feeGrowthGlobal0X128;
        uint256 feeGrowthGlobal1X128;
        uint256 feeGrowthBelow0X128;
        uint256 feeGrowthBelow1X128;
        uint256 feeGrowthAbove0X128;
        uint256 feeGrowthAbove1X128;
        uint256 lowerFeeGrowthOutside0X128;
        uint256 lowerFeeGrowthOutside1X128;
        uint256 upperFeeGrowthOutside0X128;
        uint256 upperFeeGrowthOutside1X128;
    }
}

File 15 of 56 : Errors.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

library Errors {
    error Unauthorized();
    error RecordEmpty();
    error SwapFailed();
    error OverSpend();
    error OverRefund();
    error Overflow();
    error InsufficientSwap();
    error InsufficientBorrow();
    error InsufficientRepay();
    error InsufficientPremium();
    error LiquidityReclaimed();
    error LiquidationNotMet();
    error InvalidSignature();
    error InvalidBlockNumber();
    error InvalidToken();
    error InvalidRecipient();
    error InvalidValue();
}

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

pragma solidity ^0.8.1;

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

        return account.code.length > 0;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.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. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }

    /**
     * @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.9.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]
 * ```solidity
 * contract MyToken is ERC20Upgradeable {
 *     function initialize() initializer public {
 *         __ERC20_init("MyToken", "MTK");
 *     }
 * }
 *
 * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
 *     function initializeV2() reinitializer(2) public {
 *         __ERC20Permit_init("MyToken");
 *     }
 * }
 * ```
 *
 * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
 * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
 *
 * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
 * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
 *
 * [CAUTION]
 * ====
 * Avoid leaving a contract uninitialized.
 *
 * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
 * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
 * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
 *
 * [.hljs-theme-light.nopadding]
 * ```
 * /// @custom:oz-upgrades-unsafe-allow constructor
 * constructor() {
 *     _disableInitializers();
 * }
 * ```
 * ====
 */
abstract contract Initializable {
    /**
     * @dev Indicates that the contract has been initialized.
     * @custom:oz-retyped-from bool
     */
    uint8 private _initialized;

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

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

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

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

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

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

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

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

File 19 of 56 : draft-IERC1822Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (interfaces/draft-IERC1822.sol)

pragma solidity ^0.8.0;

/**
 * @dev ERC1822: Universal Upgradeable Proxy Standard (UUPS) documents a method for upgradeability through a simplified
 * proxy whose upgrades are fully controlled by the current implementation.
 */
interface IERC1822ProxiableUpgradeable {
    /**
     * @dev Returns the storage slot that the proxiable contract assumes is being used to store the implementation
     * address.
     *
     * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
     * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
     * function revert if invoked through a proxy.
     */
    function proxiableUUID() external view returns (bytes32);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Upgrade.sol)

pragma solidity ^0.8.2;

import "../beacon/IBeaconUpgradeable.sol";
import "../../interfaces/IERC1967Upgradeable.sol";
import "../../interfaces/draft-IERC1822Upgradeable.sol";
import "../../utils/AddressUpgradeable.sol";
import "../../utils/StorageSlotUpgradeable.sol";
import "../utils/Initializable.sol";

/**
 * @dev This abstract contract provides getters and event emitting update functions for
 * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
 *
 * _Available since v4.1._
 */
abstract contract ERC1967UpgradeUpgradeable is Initializable, IERC1967Upgradeable {
    function __ERC1967Upgrade_init() internal onlyInitializing {
    }

    function __ERC1967Upgrade_init_unchained() internal onlyInitializing {
    }
    // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
    bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;

    /**
     * @dev Storage slot with the address of the current implementation.
     * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
     * validated in the constructor.
     */
    bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;

    /**
     * @dev Returns the current implementation address.
     */
    function _getImplementation() internal view returns (address) {
        return StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value;
    }

    /**
     * @dev Stores a new address in the EIP1967 implementation slot.
     */
    function _setImplementation(address newImplementation) private {
        require(AddressUpgradeable.isContract(newImplementation), "ERC1967: new implementation is not a contract");
        StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
    }

    /**
     * @dev Perform implementation upgrade
     *
     * Emits an {Upgraded} event.
     */
    function _upgradeTo(address newImplementation) internal {
        _setImplementation(newImplementation);
        emit Upgraded(newImplementation);
    }

    /**
     * @dev Perform implementation upgrade with additional setup call.
     *
     * Emits an {Upgraded} event.
     */
    function _upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
        _upgradeTo(newImplementation);
        if (data.length > 0 || forceCall) {
            AddressUpgradeable.functionDelegateCall(newImplementation, data);
        }
    }

    /**
     * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
     *
     * Emits an {Upgraded} event.
     */
    function _upgradeToAndCallUUPS(address newImplementation, bytes memory data, bool forceCall) internal {
        // Upgrades from old implementations will perform a rollback test. This test requires the new
        // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
        // this special case will break upgrade paths from old UUPS implementation to new ones.
        if (StorageSlotUpgradeable.getBooleanSlot(_ROLLBACK_SLOT).value) {
            _setImplementation(newImplementation);
        } else {
            try IERC1822ProxiableUpgradeable(newImplementation).proxiableUUID() returns (bytes32 slot) {
                require(slot == _IMPLEMENTATION_SLOT, "ERC1967Upgrade: unsupported proxiableUUID");
            } catch {
                revert("ERC1967Upgrade: new implementation is not UUPS");
            }
            _upgradeToAndCall(newImplementation, data, forceCall);
        }
    }

    /**
     * @dev Storage slot with the admin of the contract.
     * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
     * validated in the constructor.
     */
    bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;

    /**
     * @dev Returns the current admin.
     */
    function _getAdmin() internal view returns (address) {
        return StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value;
    }

    /**
     * @dev Stores a new address in the EIP1967 admin slot.
     */
    function _setAdmin(address newAdmin) private {
        require(newAdmin != address(0), "ERC1967: new admin is the zero address");
        StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
    }

    /**
     * @dev Changes the admin of the proxy.
     *
     * Emits an {AdminChanged} event.
     */
    function _changeAdmin(address newAdmin) internal {
        emit AdminChanged(_getAdmin(), newAdmin);
        _setAdmin(newAdmin);
    }

    /**
     * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
     * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
     */
    bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;

    /**
     * @dev Returns the current beacon.
     */
    function _getBeacon() internal view returns (address) {
        return StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value;
    }

    /**
     * @dev Stores a new beacon in the EIP1967 beacon slot.
     */
    function _setBeacon(address newBeacon) private {
        require(AddressUpgradeable.isContract(newBeacon), "ERC1967: new beacon is not a contract");
        require(
            AddressUpgradeable.isContract(IBeaconUpgradeable(newBeacon).implementation()),
            "ERC1967: beacon implementation is not a contract"
        );
        StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value = newBeacon;
    }

    /**
     * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
     * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
     *
     * Emits a {BeaconUpgraded} event.
     */
    function _upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
        _setBeacon(newBeacon);
        emit BeaconUpgraded(newBeacon);
        if (data.length > 0 || forceCall) {
            AddressUpgradeable.functionDelegateCall(IBeaconUpgradeable(newBeacon).implementation(), 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.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

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

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

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

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

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

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

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

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

File 22 of 56 : IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC20.sol)

pragma solidity ^0.8.0;

import "../token/ERC20/IERC20.sol";

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

File 24 of 56 : FixedPoint128.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.4.0;

/// @title FixedPoint128
/// @notice A library for handling binary fixed point numbers, see https://en.wikipedia.org/wiki/Q_(number_format)
library FixedPoint128 {
    uint256 internal constant Q128 = 0x100000000000000000000000000000000;
}

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

import './pool/IUniswapV3PoolImmutables.sol';
import './pool/IUniswapV3PoolState.sol';
import './pool/IUniswapV3PoolDerivedState.sol';
import './pool/IUniswapV3PoolActions.sol';
import './pool/IUniswapV3PoolOwnerActions.sol';
import './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,
    IUniswapV3PoolEvents
{

}

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

/// @title The interface for the Uniswap V3 Factory
/// @notice The Uniswap V3 Factory facilitates creation of Uniswap V3 pools and control over the protocol fees
interface IUniswapV3Factory {
    /// @notice Emitted when the owner of the factory is changed
    /// @param oldOwner The owner before the owner was changed
    /// @param newOwner The owner after the owner was changed
    event OwnerChanged(address indexed oldOwner, address indexed newOwner);

    /// @notice Emitted when a pool is created
    /// @param token0 The first token of the pool by address sort order
    /// @param token1 The second token of the pool by address sort order
    /// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
    /// @param tickSpacing The minimum number of ticks between initialized ticks
    /// @param pool The address of the created pool
    event PoolCreated(
        address indexed token0,
        address indexed token1,
        uint24 indexed fee,
        int24 tickSpacing,
        address pool
    );

    /// @notice Emitted when a new fee amount is enabled for pool creation via the factory
    /// @param fee The enabled fee, denominated in hundredths of a bip
    /// @param tickSpacing The minimum number of ticks between initialized ticks for pools created with the given fee
    event FeeAmountEnabled(uint24 indexed fee, int24 indexed tickSpacing);

    /// @notice Returns the current owner of the factory
    /// @dev Can be changed by the current owner via setOwner
    /// @return The address of the factory owner
    function owner() external view returns (address);

    /// @notice Returns the tick spacing for a given fee amount, if enabled, or 0 if not enabled
    /// @dev A fee amount can never be removed, so this value should be hard coded or cached in the calling context
    /// @param fee The enabled fee, denominated in hundredths of a bip. Returns 0 in case of unenabled fee
    /// @return The tick spacing
    function feeAmountTickSpacing(uint24 fee) external view returns (int24);

    /// @notice Returns the pool address for a given pair of tokens and a fee, or address 0 if it does not exist
    /// @dev tokenA and tokenB may be passed in either token0/token1 or token1/token0 order
    /// @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 pool address
    function getPool(
        address tokenA,
        address tokenB,
        uint24 fee
    ) external view returns (address pool);

    /// @notice Creates a pool for the given two tokens and fee
    /// @param tokenA One of the two tokens in the desired pool
    /// @param tokenB The other of the two tokens in the desired pool
    /// @param fee The desired fee for the pool
    /// @dev tokenA and tokenB may be passed in either order: token0/token1 or token1/token0. tickSpacing is retrieved
    /// from the fee. The call will revert if the pool already exists, the fee is invalid, or the token arguments
    /// are invalid.
    /// @return pool The address of the newly created pool
    function createPool(
        address tokenA,
        address tokenB,
        uint24 fee
    ) external returns (address pool);

    /// @notice Updates the owner of the factory
    /// @dev Must be called by the current owner
    /// @param _owner The new owner of the factory
    function setOwner(address _owner) external;

    /// @notice Enables a fee amount with the given tickSpacing
    /// @dev Fee amounts may never be removed once enabled
    /// @param fee The fee amount to enable, denominated in hundredths of a bip (i.e. 1e-6)
    /// @param tickSpacing The spacing between ticks to be enforced for all pools created with the given fee amount
    function enableFeeAmount(uint24 fee, int24 tickSpacing) external;
}

// 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
// Adopted from Uniswap v3-periphery (contracts/interfaces/INonfungiblePositionManger.sol)

pragma solidity 0.8.23;
pragma abicoder v2;

import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import "@uniswap/v3-periphery/contracts/interfaces/IPoolInitializer.sol";
import "@uniswap/v3-periphery/contracts/interfaces/IERC721Permit.sol";
import "@uniswap/v3-periphery/contracts/interfaces/IPeripheryPayments.sol";
import "@uniswap/v3-periphery/contracts/interfaces/IPeripheryImmutableState.sol";

import {PoolAddress} from "../libraries/PoolAddress.sol";

/// @title Non-fungible token for positions
/// @notice Wraps Uniswap V3 positions in a non-fungible token interface which allows for them to be transferred
/// and authorized.
interface INonfungiblePositionManager is
    IPoolInitializer,
    IPeripheryPayments,
    IPeripheryImmutableState,
    IERC721Metadata,
    IERC721Enumerable,
    IERC721Permit
{
    /// @notice Emitted when liquidity is increased for a position NFT
    /// @dev Also emitted when a token is minted
    /// @param tokenId The ID of the token for which liquidity was increased
    /// @param liquidity The amount by which liquidity for the NFT position was increased
    /// @param amount0 The amount of token0 that was paid for the increase in liquidity
    /// @param amount1 The amount of token1 that was paid for the increase in liquidity
    event IncreaseLiquidity(uint256 indexed tokenId, uint128 liquidity, uint256 amount0, uint256 amount1);
    /// @notice Emitted when liquidity is decreased for a position NFT
    /// @param tokenId The ID of the token for which liquidity was decreased
    /// @param liquidity The amount by which liquidity for the NFT position was decreased
    /// @param amount0 The amount of token0 that was accounted for the decrease in liquidity
    /// @param amount1 The amount of token1 that was accounted for the decrease in liquidity
    event DecreaseLiquidity(uint256 indexed tokenId, uint128 liquidity, uint256 amount0, uint256 amount1);
    /// @notice Emitted when tokens are collected for a position NFT
    /// @dev The amounts reported may not be exactly equivalent to the amounts transferred, due to rounding behavior
    /// @param tokenId The ID of the token for which underlying tokens were collected
    /// @param recipient The address of the account that received the collected tokens
    /// @param amount0 The amount of token0 owed to the position that was collected
    /// @param amount1 The amount of token1 owed to the position that was collected
    event Collect(uint256 indexed tokenId, address recipient, uint256 amount0, uint256 amount1);

    /// @notice Returns the position information associated with a given token ID.
    /// @dev Throws if the token ID is not valid.
    /// @param tokenId The ID of the token that represents the position
    /// @return nonce The nonce for permits
    /// @return operator The address that is approved for spending
    /// @return token0 The address of the token0 for a specific pool
    /// @return token1 The address of the token1 for a specific pool
    /// @return fee The fee associated with the pool
    /// @return tickLower The lower end of the tick range for the position
    /// @return tickUpper The higher end of the tick range for the position
    /// @return liquidity The liquidity of the position
    /// @return feeGrowthInside0LastX128 The fee growth of token0 as of the last action on the individual position
    /// @return feeGrowthInside1LastX128 The fee growth of token1 as of the last action on the individual position
    /// @return tokensOwed0 The uncollected amount of token0 owed to the position as of the last computation
    /// @return tokensOwed1 The uncollected amount of token1 owed to the position as of the last computation
    function positions(
        uint256 tokenId
    )
        external
        view
        returns (
            uint96 nonce,
            address operator,
            address token0,
            address token1,
            uint24 fee,
            int24 tickLower,
            int24 tickUpper,
            uint128 liquidity,
            uint256 feeGrowthInside0LastX128,
            uint256 feeGrowthInside1LastX128,
            uint128 tokensOwed0,
            uint128 tokensOwed1
        );

    struct MintParams {
        address token0;
        address token1;
        uint24 fee;
        int24 tickLower;
        int24 tickUpper;
        uint256 amount0Desired;
        uint256 amount1Desired;
        uint256 amount0Min;
        uint256 amount1Min;
        address recipient;
        uint256 deadline;
    }

    /// @notice Creates a new position wrapped in a NFT
    /// @dev Call this when the pool does exist and is initialized. Note that if the pool is created but not initialized
    /// a method does not exist, i.e. the pool is assumed to be initialized.
    /// @param params The params necessary to mint a position, encoded as `MintParams` in calldata
    /// @return tokenId The ID of the token that represents the minted position
    /// @return liquidity The amount of liquidity for this position
    /// @return amount0 The amount of token0
    /// @return amount1 The amount of token1
    function mint(
        MintParams calldata params
    ) external payable returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1);

    struct IncreaseLiquidityParams {
        uint256 tokenId;
        uint256 amount0Desired;
        uint256 amount1Desired;
        uint256 amount0Min;
        uint256 amount1Min;
        uint256 deadline;
    }

    /// @notice Increases the amount of liquidity in a position, with tokens paid by the `msg.sender`
    /// @param params tokenId The ID of the token for which liquidity is being increased,
    /// amount0Desired The desired amount of token0 to be spent,
    /// amount1Desired The desired amount of token1 to be spent,
    /// amount0Min The minimum amount of token0 to spend, which serves as a slippage check,
    /// amount1Min The minimum amount of token1 to spend, which serves as a slippage check,
    /// deadline The time by which the transaction must be included to effect the change
    /// @return liquidity The new liquidity amount as a result of the increase
    /// @return amount0 The amount of token0 to acheive resulting liquidity
    /// @return amount1 The amount of token1 to acheive resulting liquidity
    function increaseLiquidity(
        IncreaseLiquidityParams calldata params
    ) external payable returns (uint128 liquidity, uint256 amount0, uint256 amount1);

    struct DecreaseLiquidityParams {
        uint256 tokenId;
        uint128 liquidity;
        uint256 amount0Min;
        uint256 amount1Min;
        uint256 deadline;
    }

    /// @notice Decreases the amount of liquidity in a position and accounts it to the position
    /// @param params tokenId The ID of the token for which liquidity is being decreased,
    /// amount The amount by which liquidity will be decreased,
    /// amount0Min The minimum amount of token0 that should be accounted for the burned liquidity,
    /// amount1Min The minimum amount of token1 that should be accounted for the burned liquidity,
    /// deadline The time by which the transaction must be included to effect the change
    /// @return amount0 The amount of token0 accounted to the position's tokens owed
    /// @return amount1 The amount of token1 accounted to the position's tokens owed
    function decreaseLiquidity(
        DecreaseLiquidityParams calldata params
    ) external payable returns (uint256 amount0, uint256 amount1);

    struct CollectParams {
        uint256 tokenId;
        address recipient;
        uint128 amount0Max;
        uint128 amount1Max;
    }

    /// @notice Collects up to a maximum amount of fees owed to a specific position to the recipient
    /// @param params tokenId The ID of the NFT for which tokens are being collected,
    /// recipient The account that should receive the tokens,
    /// amount0Max The maximum amount of token0 to collect,
    /// amount1Max The maximum amount of token1 to collect
    /// @return amount0 The amount of fees collected in token0
    /// @return amount1 The amount of fees collected in token1
    function collect(CollectParams calldata params) external payable returns (uint256 amount0, uint256 amount1);

    /// @notice Burns a token ID, which deletes it from the NFT contract. The token must have 0 liquidity and all tokens
    /// must be collected first.
    /// @param tokenId The ID of the token that is being burned
    function burn(uint256 tokenId) external payable;
}

// SPDX-License-Identifier: GPL-2.0-or-later
// Adopted from Uniswap v3-periphery (contracts/libraries/PoolAddress.sol)

pragma solidity 0.8.23;

/// @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
// Adopted from Uniswap v4-core (contracts/libraries/FullMath.sol)
pragma solidity 0.8.23;

/// @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 = a * b; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(a, b, not(0))
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

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

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

            ///////////////////////////////////////////////
            // 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 preconditions 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
// Adopted from Uniswap v3-core (contracts/libraries/TickMath.sol)

pragma solidity 0.8.23;

/// @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 {
    /// @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) {
        uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick));
        require(absTick <= uint256(uint24(MAX_TICK)), "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) {
        // second inequality must be < because the price can never reach the price at the max tick
        require(sqrtPriceX96 >= MIN_SQRT_RATIO && sqrtPriceX96 < MAX_SQRT_RATIO, "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
// Adopted from Uniswap v3-periphery (contracts/libraries/LiquidityAmount.sol)

pragma solidity 0.8.23;

import "./FullMath.sol";
import "@uniswap/v3-core/contracts/libraries/FixedPoint96.sol";

/// @title Liquidity amount functions
/// @notice Provides functions for computing liquidity amounts from token amounts and prices
library LiquidityAmounts {
    /// @notice Downcasts uint256 to uint128
    /// @param x The uint258 to be downcasted
    /// @return y The passed value, downcasted to uint128
    function toUint128(uint256 x) private pure returns (uint128 y) {
        require((y = uint128(x)) == x);
    }

    /// @notice Computes the amount of liquidity received for a given amount of token0 and price range
    /// @dev Calculates amount0 * (sqrt(upper) * sqrt(lower)) / (sqrt(upper) - sqrt(lower))
    /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
    /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
    /// @param amount0 The amount0 being sent in
    /// @return liquidity The amount of returned liquidity
    function getLiquidityForAmount0(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint256 amount0
    ) internal pure returns (uint128 liquidity) {
        if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
        uint256 intermediate = FullMath.mulDiv(sqrtRatioAX96, sqrtRatioBX96, FixedPoint96.Q96);
        return toUint128(FullMath.mulDiv(amount0, intermediate, sqrtRatioBX96 - sqrtRatioAX96));
    }

    /// @notice Computes the amount of liquidity received for a given amount of token1 and price range
    /// @dev Calculates amount1 / (sqrt(upper) - sqrt(lower)).
    /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
    /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
    /// @param amount1 The amount1 being sent in
    /// @return liquidity The amount of returned liquidity
    function getLiquidityForAmount1(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint256 amount1
    ) internal pure returns (uint128 liquidity) {
        if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
        return toUint128(FullMath.mulDiv(amount1, FixedPoint96.Q96, sqrtRatioBX96 - sqrtRatioAX96));
    }

    /// @notice Computes the maximum amount of liquidity received for a given amount of token0, token1, the current
    /// pool prices and the prices at the tick boundaries
    /// @param sqrtRatioX96 A sqrt price representing the current pool prices
    /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
    /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
    /// @param amount0 The amount of token0 being sent in
    /// @param amount1 The amount of token1 being sent in
    /// @return liquidity The maximum amount of liquidity received
    function getLiquidityForAmounts(
        uint160 sqrtRatioX96,
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint256 amount0,
        uint256 amount1
    ) internal pure returns (uint128 liquidity) {
        if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

        if (sqrtRatioX96 <= sqrtRatioAX96) {
            liquidity = getLiquidityForAmount0(sqrtRatioAX96, sqrtRatioBX96, amount0);
        } else if (sqrtRatioX96 < sqrtRatioBX96) {
            uint128 liquidity0 = getLiquidityForAmount0(sqrtRatioX96, sqrtRatioBX96, amount0);
            uint128 liquidity1 = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioX96, amount1);

            liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1;
        } else {
            liquidity = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1);
        }
    }

    /// @notice Computes the amount of token0 for a given amount of liquidity and a price range
    /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
    /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
    /// @param liquidity The liquidity being valued
    /// @return amount0 The amount of token0
    function getAmount0ForLiquidity(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity
    ) internal pure returns (uint256 amount0) {
        if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

        return
            FullMath.mulDiv(
                uint256(liquidity) << FixedPoint96.RESOLUTION,
                sqrtRatioBX96 - sqrtRatioAX96,
                sqrtRatioBX96
            ) / sqrtRatioAX96;
    }

    /// @notice Computes the amount of token1 for a given amount of liquidity and a price range
    /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
    /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
    /// @param liquidity The liquidity being valued
    /// @return amount1 The amount of token1
    function getAmount1ForLiquidity(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity
    ) internal pure returns (uint256 amount1) {
        if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

        return FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
    }

    /// @notice Computes the amount of token 0 and token1 at two ends for a given amount of liquidity and a price range
    /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
    /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
    /// @param liquidity The liquidity being valued
    /// @return amount0 The amount of token0
    /// @return amount1 The amount of token1
    function getAmountsForLiquidity(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity
    ) internal pure returns (uint256 amount0, uint256 amount1) {
        if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

        amount0 =
            FullMath.mulDiv(
                uint256(liquidity) << FixedPoint96.RESOLUTION,
                sqrtRatioBX96 - sqrtRatioAX96,
                sqrtRatioBX96
            ) /
            sqrtRatioAX96;

        amount1 = FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
    }

    /// @notice Computes the token0 and token1 value for a given amount of liquidity, the current
    /// pool prices and the prices at the tick boundaries
    /// @param sqrtRatioX96 A sqrt price representing the current pool prices
    /// @param sqrtRatioAX96 A sqrt price representing the first tick boundary
    /// @param sqrtRatioBX96 A sqrt price representing the second tick boundary
    /// @param liquidity The liquidity being valued
    /// @return amount0 The amount of token0
    /// @return amount1 The amount of token1
    function getAmountsForLiquidity(
        uint160 sqrtRatioX96,
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint128 liquidity
    ) internal pure returns (uint256 amount0, uint256 amount1) {
        if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

        if (sqrtRatioX96 <= sqrtRatioAX96) {
            amount0 = getAmount0ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
        } else if (sqrtRatioX96 < sqrtRatioBX96) {
            amount0 = getAmount0ForLiquidity(sqrtRatioX96, sqrtRatioBX96, liquidity);
            amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioX96, liquidity);
        } else {
            amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
        }
    }
}

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

enum YieldMode {
    AUTOMATIC,
    DISABLED,
    CLAIMABLE
}

enum GasMode {
    VOID,
    CLAIMABLE
}

interface IBlast {
    // configure
    function configureContract(address contractAddress, YieldMode _yield, GasMode gasMode, address governor) external;

    function configure(YieldMode _yield, GasMode gasMode, address governor) external;

    // base configuration options
    function configureClaimableYield() external;

    function configureClaimableYieldOnBehalf(address contractAddress) external;

    function configureAutomaticYield() external;

    function configureAutomaticYieldOnBehalf(address contractAddress) external;

    function configureVoidYield() external;

    function configureVoidYieldOnBehalf(address contractAddress) external;

    function configureClaimableGas() external;

    function configureClaimableGasOnBehalf(address contractAddress) external;

    function configureVoidGas() external;

    function configureVoidGasOnBehalf(address contractAddress) external;

    function configureGovernor(address _governor) external;

    function configureGovernorOnBehalf(address _newGovernor, address contractAddress) external;

    // claim yield
    function claimYield(address contractAddress, address recipientOfYield, uint256 amount) external returns (uint256);

    function claimAllYield(address contractAddress, address recipientOfYield) external returns (uint256);

    // claim gas
    function claimAllGas(address contractAddress, address recipientOfGas) external returns (uint256);

    function claimGasAtMinClaimRate(
        address contractAddress,
        address recipientOfGas,
        uint256 minClaimRateBips
    ) external returns (uint256);

    function claimMaxGas(address contractAddress, address recipientOfGas) external returns (uint256);

    function claimGas(
        address contractAddress,
        address recipientOfGas,
        uint256 gasToClaim,
        uint256 gasSecondsToConsume
    ) external returns (uint256);

    // read functions
    function readClaimableYield(address contractAddress) external view returns (uint256);

    function readYieldConfiguration(address contractAddress) external view returns (uint8);

    function readGasParams(
        address contractAddress
    ) external view returns (uint256 etherSeconds, uint256 etherBalance, uint256 lastUpdated, GasMode);
}

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

interface IBlastPoints {
    /**
     * @notice configure for blast point operator address
     * @param operator the blast points operator address
     */
    function configurePointsOperator(address operator) external;
}

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

enum YieldMode {
    AUTOMATIC,
    VOID,
    CLAIMABLE
}

interface IERC20Rebasing {
    // changes the yield mode of the caller and update the balance
    // to reflect the configuration
    function configure(YieldMode) external returns (uint256);

    // "claimable" yield mode accounts can call this this claim their yield
    // to another address
    function claim(address recipient, uint256 amount) external returns (uint256);

    // read the claimable amount for an account
    function getClaimableAmount(address account) external view returns (uint256);
}

// 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.9.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
     *
     * Furthermore, `isContract` will also return true if the target contract within
     * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
     * which only has an effect at the end of a transaction.
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

File 38 of 56 : IBeaconUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (proxy/beacon/IBeacon.sol)

pragma solidity ^0.8.0;

/**
 * @dev This is the interface that {BeaconProxy} expects of its beacon.
 */
interface IBeaconUpgradeable {
    /**
     * @dev Must return an address that can be used as a delegate call target.
     *
     * {BeaconProxy} will check that this address is a contract.
     */
    function implementation() external view returns (address);
}

File 39 of 56 : IERC1967Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (interfaces/IERC1967.sol)

pragma solidity ^0.8.0;

/**
 * @dev ERC-1967: Proxy Storage Slots. This interface contains the events defined in the ERC.
 *
 * _Available since v4.8.3._
 */
interface IERC1967Upgradeable {
    /**
     * @dev Emitted when the implementation is upgraded.
     */
    event Upgraded(address indexed implementation);

    /**
     * @dev Emitted when the admin account has changed.
     */
    event AdminChanged(address previousAdmin, address newAdmin);

    /**
     * @dev Emitted when the beacon is changed.
     */
    event BeaconUpgraded(address indexed beacon);
}

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

pragma solidity ^0.8.0;

/**
 * @dev Library for reading and writing primitive types to specific storage slots.
 *
 * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
 * This library helps with reading and writing to such slots without the need for inline assembly.
 *
 * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
 *
 * Example usage to set ERC1967 implementation slot:
 * ```solidity
 * contract ERC1967 {
 *     bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
 *
 *     function _getImplementation() internal view returns (address) {
 *         return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
 *     }
 *
 *     function _setImplementation(address newImplementation) internal {
 *         require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
 *         StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
 *     }
 * }
 * ```
 *
 * _Available since v4.1 for `address`, `bool`, `bytes32`, `uint256`._
 * _Available since v4.9 for `string`, `bytes`._
 */
library StorageSlotUpgradeable {
    struct AddressSlot {
        address value;
    }

    struct BooleanSlot {
        bool value;
    }

    struct Bytes32Slot {
        bytes32 value;
    }

    struct Uint256Slot {
        uint256 value;
    }

    struct StringSlot {
        string value;
    }

    struct BytesSlot {
        bytes value;
    }

    /**
     * @dev Returns an `AddressSlot` with member `value` located at `slot`.
     */
    function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
     */
    function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
     */
    function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
     */
    function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `StringSlot` with member `value` located at `slot`.
     */
    function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
     */
    function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := store.slot
        }
    }

    /**
     * @dev Returns an `BytesSlot` with member `value` located at `slot`.
     */
    function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
     */
    function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := store.slot
        }
    }
}

// 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 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
    /// 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.
    /// observationIndex The index of the last oracle observation that was written,
    /// observationCardinality The current maximum number of observations stored in the pool,
    /// observationCardinalityNext The next maximum number of observations, to be updated when the observation.
    /// 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
    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,
    /// liquidityNet how much liquidity changes when the pool price crosses the tick,
    /// feeGrowthOutside0X128 the fee growth on the other side of the tick from the current tick in token0,
    /// feeGrowthOutside1X128 the fee growth on the other side of the tick from the current tick in token1,
    /// tickCumulativeOutside the cumulative tick value on the other side of the tick from the current tick
    /// secondsPerLiquidityOutsideX128 the seconds spent per liquidity on the other side of the tick from the current tick,
    /// secondsOutside the seconds spent on the other side of the tick from the current tick,
    /// 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,
    /// Returns feeGrowthInside0LastX128 fee growth of token0 inside the tick range as of the last mint/burn/poke,
    /// Returns feeGrowthInside1LastX128 fee growth of token1 inside the tick range as of the last mint/burn/poke,
    /// Returns tokensOwed0 the computed amount of token0 owed to the position as of the last mint/burn/poke,
    /// Returns 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,
    /// Returns tickCumulative the tick multiplied by seconds elapsed for the life of the pool as of the observation timestamp,
    /// Returns secondsPerLiquidityCumulativeX128 the seconds per in range liquidity for the life of the pool as of the observation timestamp,
    /// Returns 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.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
        );
}

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

File 46 of 56 : 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);
}

File 47 of 56 : 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;
}

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

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional metadata extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Metadata is IERC721 {
    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

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

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC721/extensions/IERC721Enumerable.sol)

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Enumerable is IERC721 {
    /**
     * @dev Returns the total amount of tokens stored by the contract.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns a token ID owned by `owner` at a given `index` of its token list.
     * Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256);

    /**
     * @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
     * Use along with {totalSupply} to enumerate all tokens.
     */
    function tokenByIndex(uint256 index) external view returns (uint256);
}

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

/// @title Creates and initializes V3 Pools
/// @notice Provides a method for creating and initializing a pool, if necessary, for bundling with other methods that
/// require the pool to exist.
interface IPoolInitializer {
    /// @notice Creates a new pool if it does not exist, then initializes if not initialized
    /// @dev This method can be bundled with others via IMulticall for the first action (e.g. mint) performed against a pool
    /// @param token0 The contract address of token0 of the pool
    /// @param token1 The contract address of token1 of the pool
    /// @param fee The fee amount of the v3 pool for the specified token pair
    /// @param sqrtPriceX96 The initial square root price of the pool as a Q64.96 value
    /// @return pool Returns the pool address based on the pair of tokens and fee, will return the newly created pool address if necessary
    function createAndInitializePoolIfNecessary(
        address token0,
        address token1,
        uint24 fee,
        uint160 sqrtPriceX96
    ) external payable returns (address pool);
}

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

import '@openzeppelin/contracts/token/ERC721/IERC721.sol';

/// @title ERC721 with permit
/// @notice Extension to ERC721 that includes a permit function for signature based approvals
interface IERC721Permit is IERC721 {
    /// @notice The permit typehash used in the permit signature
    /// @return The typehash for the permit
    function PERMIT_TYPEHASH() external pure returns (bytes32);

    /// @notice The domain separator used in the permit signature
    /// @return The domain seperator used in encoding of permit signature
    function DOMAIN_SEPARATOR() external view returns (bytes32);

    /// @notice Approve of a specific token ID for spending by spender via signature
    /// @param spender The account that is being approved
    /// @param tokenId The ID of the token that is being approved for spending
    /// @param deadline The deadline timestamp by which the call must be mined for the approve to work
    /// @param v Must produce valid secp256k1 signature from the holder along with `r` and `s`
    /// @param r Must produce valid secp256k1 signature from the holder along with `v` and `s`
    /// @param s Must produce valid secp256k1 signature from the holder along with `r` and `v`
    function permit(
        address spender,
        uint256 tokenId,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external payable;
}

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

/// @title Periphery Payments
/// @notice Functions to ease deposits and withdrawals of ETH
interface IPeripheryPayments {
    /// @notice Unwraps the contract's WETH9 balance and sends it to recipient as ETH.
    /// @dev The amountMinimum parameter prevents malicious contracts from stealing WETH9 from users.
    /// @param amountMinimum The minimum amount of WETH9 to unwrap
    /// @param recipient The address receiving ETH
    function unwrapWETH9(uint256 amountMinimum, address recipient) external payable;

    /// @notice Refunds any ETH balance held by this contract to the `msg.sender`
    /// @dev Useful for bundling with mint or increase liquidity that uses ether, or exact output swaps
    /// that use ether for the input amount
    function refundETH() external payable;

    /// @notice Transfers the full amount of a token held by this contract to recipient
    /// @dev The amountMinimum parameter prevents malicious contracts from stealing the token from users
    /// @param token The contract address of the token which will be transferred to `recipient`
    /// @param amountMinimum The minimum amount of token required for a transfer
    /// @param recipient The destination address of the token
    function sweepToken(
        address token,
        uint256 amountMinimum,
        address recipient
    ) external payable;
}

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

/// @title Immutable state
/// @notice Functions that return immutable state of the router
interface IPeripheryImmutableState {
    /// @return Returns the address of the Uniswap V3 factory
    function factory() external view returns (address);

    /// @return Returns the address of WETH9
    function WETH9() external view returns (address);
}

File 54 of 56 : 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
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
     * or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
     * understand this adds an external call which potentially creates a reentrancy vulnerability.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

Settings
{
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "@solmate/=lib/solmate/src/",
    "@uniswap/v3-core/=lib/v3-core/",
    "@uniswap/v3-periphery/=lib/v3-periphery/",
    "ds-test/=lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "openzeppelin/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "solmate/=lib/solmate/src/",
    "v3-core/=lib/v3-core/",
    "v3-periphery/=lib/v3-periphery/contracts/"
  ],
  "optimizer": {
    "enabled": true,
    "runs": 2000
  },
  "metadata": {
    "useLiteralContent": false,
    "bytecodeHash": "ipfs",
    "appendCBOR": true
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "evmVersion": "paris",
  "libraries": {
    "contracts/libraries/Blast.sol": {
      "Blast": "0x897B67Fb9fA3ae2F66e6c1b0bb3E9da7E9231585"
    },
    "contracts/libraries/Lien.sol": {
      "Lien": "0x61D591343e022baFD1C279a7b996168669eC0517"
    },
    "contracts/libraries/LiquidityPosition.sol": {
      "LiquidityPosition": "0x7dDC548F8d68D6Ca01Ca36A51dC8a05F1E7F20BA"
    }
  }
}

Contract Security Audit

Contract ABI

API
[{"inputs":[],"stateMutability":"payable","type":"constructor"},{"inputs":[],"name":"InvalidBlockNumber","type":"error"},{"inputs":[],"name":"InvalidRecipient","type":"error"},{"inputs":[],"name":"InvalidSignature","type":"error"},{"inputs":[],"name":"InvalidValue","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"lienId","type":"uint256"},{"indexed":false,"internalType":"uint128","name":"premium0","type":"uint128"},{"indexed":false,"internalType":"uint128","name":"premium1","type":"uint128"},{"indexed":false,"internalType":"uint256","name":"spending","type":"uint256"}],"name":"AddPremium","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"previousAdmin","type":"address"},{"indexed":false,"internalType":"address","name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"beacon","type":"address"}],"name":"BeaconUpgraded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"wethb","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"usdb","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"gas","type":"uint256"}],"name":"ClaimYieldGas","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"borrower","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token0Refund","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token1Refund","type":"uint256"}],"name":"ClosePosition","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"CollectLiquidity","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint128","name":"liquidity","type":"uint128"}],"name":"DecreaseLiquidity","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint128","name":"liquidity","type":"uint128"}],"name":"IncreaseLiquidity","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":"borrower","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token0Refund","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token1Refund","type":"uint256"}],"name":"LiquidatePosition","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"borrower","type":"address"},{"indexed":false,"internalType":"uint96","name":"lienId","type":"uint96"},{"indexed":false,"internalType":"uint256","name":"positionAmount","type":"uint256"}],"name":"OpenPosition","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint32","name":"cutOffTime","type":"uint32"}],"name":"ReclaimLiquidity","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"address","name":"lp","type":"address"},{"indexed":false,"internalType":"uint128","name":"liquidity","type":"uint128"}],"name":"SupplyLiquidity","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"tokenFrom","type":"address"},{"indexed":false,"internalType":"address","name":"tokenTo","type":"address"},{"indexed":false,"internalType":"uint256","name":"amountFrom","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountTo","type":"uint256"}],"name":"Swap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"blastPointsAdmin","type":"address"}],"name":"UpdateBlastPointsAdmin","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"feeFactor","type":"uint256"}],"name":"UpdateFeeFactor","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint128","name":"liquidationRewardFactor","type":"uint128"}],"name":"UpdateLiquidationRewardFactor","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"loanTerm","type":"uint256"}],"name":"UpdateLoanTerm","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oracleSigner","type":"address"}],"name":"UpdateOracleSigner","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"treasuryRate","type":"uint256"}],"name":"UpdateTreasuryRate","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"validBlocks","type":"uint256"}],"name":"UpdateValidBlocks","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"WithdrawTreasury","type":"event"},{"inputs":[],"name":"FEE_FACTOR","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"LIQUIDATION_REWARD_FACTOR","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"LOAN_TERM","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ORACLE_SIGNER","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"VALID_BLOCKS","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint96","name":"lienId","type":"uint96"},{"internalType":"uint128","name":"amountToAdd","type":"uint128"},{"internalType":"uint256","name":"amountToSwap","type":"uint256"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint256","name":"amountToReceiveMinimum","type":"uint256"},{"internalType":"bool","name":"zeroForOne","type":"bool"}],"internalType":"struct DataStruct.AddPremiumParams","name":"params","type":"tuple"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"addPremium","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"wethToClaim","type":"uint256"},{"internalType":"uint256","name":"usdbToClaim","type":"uint256"}],"name":"claimYieldMaxGas","outputs":[{"internalType":"uint256","name":"weth","type":"uint256"},{"internalType":"uint256","name":"usdb","type":"uint256"},{"internalType":"uint256","name":"gas","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint96","name":"lienId","type":"uint96"},{"internalType":"uint256","name":"amountSwap","type":"uint256"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint256","name":"amountReceivedMinimum","type":"uint256"},{"internalType":"uint256","name":"liquidityFactor","type":"uint256"}],"internalType":"struct DataStruct.ClosePositionParams","name":"params","type":"tuple"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"closePosition","outputs":[{"internalType":"uint40","name":"tokenId","type":"uint40"},{"internalType":"uint256","name":"token0Refund","type":"uint256"},{"internalType":"uint256","name":"token1Refund","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"collectLiquidity","outputs":[{"internalType":"uint256","name":"amount0Collected","type":"uint256"},{"internalType":"uint256","name":"amount1Collected","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"configureClaimableGas","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint256","name":"amount0Min","type":"uint256"},{"internalType":"uint256","name":"amount1Min","type":"uint256"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"decreaseLiquidity","outputs":[{"internalType":"uint256","name":"amount0Decreased","type":"uint256"},{"internalType":"uint256","name":"amount1Decreased","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"},{"internalType":"uint256","name":"amount0Min","type":"uint256"},{"internalType":"uint256","name":"amount1Min","type":"uint256"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"increaseLiquidity","outputs":[{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint256","name":"amount0Added","type":"uint256"},{"internalType":"uint256","name":"amount1Added","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"feeFactor","type":"uint256"},{"internalType":"uint128","name":"liquidationRewardFactor","type":"uint128"},{"internalType":"uint256","name":"loanTerm","type":"uint256"},{"internalType":"uint256","name":"treasuryRate","type":"uint256"},{"internalType":"uint256","name":"validBlocks","type":"uint256"},{"internalType":"address","name":"oracleSigner","type":"address"},{"internalType":"address","name":"blastAdmin","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"liens","outputs":[{"internalType":"uint40","name":"tokenId","type":"uint40"},{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint24","name":"token0PremiumPortion","type":"uint24"},{"internalType":"uint24","name":"token1PremiumPortion","type":"uint24"},{"internalType":"uint32","name":"startTime","type":"uint32"},{"internalType":"bool","name":"zeroForOne","type":"bool"},{"internalType":"uint256","name":"feeGrowthInside0LastX128","type":"uint256"},{"internalType":"uint256","name":"feeGrowthInside1LastX128","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint96","name":"lienId","type":"uint96"},{"internalType":"uint256","name":"amountSwap","type":"uint256"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint256","name":"amountReceivedMinimum","type":"uint256"},{"internalType":"uint256","name":"liquidityFactor","type":"uint256"}],"internalType":"struct DataStruct.ClosePositionParams","name":"params","type":"tuple"},{"internalType":"address","name":"borrower","type":"address"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"liquidatePosition","outputs":[{"internalType":"uint40","name":"tokenId","type":"uint40"},{"internalType":"uint256","name":"token0Refund","type":"uint256"},{"internalType":"uint256","name":"token1Refund","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"lps","outputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint32","name":"renewalCutoffTime","type":"uint32"},{"internalType":"uint128","name":"token0Owed","type":"uint128"},{"internalType":"uint128","name":"token1Owed","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"token0","type":"address"},{"internalType":"address","name":"token1","type":"address"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"int24","name":"tickLower","type":"int24"},{"internalType":"int24","name":"tickUpper","type":"int24"},{"internalType":"uint256","name":"amount0ToMint","type":"uint256"},{"internalType":"uint256","name":"amount1ToMint","type":"uint256"},{"internalType":"uint256","name":"amount0Min","type":"uint256"},{"internalType":"uint256","name":"amount1Min","type":"uint256"}],"internalType":"struct DataStruct.MintParams","name":"params","type":"tuple"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"mint","outputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint256","name":"amount0Minted","type":"uint256"},{"internalType":"uint256","name":"amount1Minted","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes[]","name":"data","type":"bytes[]"}],"name":"multicall","outputs":[{"internalType":"bytes[]","name":"results","type":"bytes[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"from","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onERC721Received","outputs":[{"internalType":"bytes4","name":"","type":"bytes4"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"marginFrom","type":"uint256"},{"internalType":"uint256","name":"marginTo","type":"uint256"},{"internalType":"uint256","name":"amountSwap","type":"uint256"},{"internalType":"uint128","name":"liquidity","type":"uint128"},{"internalType":"uint24","name":"tokenFromPremiumPortionMin","type":"uint24"},{"internalType":"uint24","name":"tokenToPremiumPortionMin","type":"uint24"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint256","name":"amountReceivedMinimum","type":"uint256"},{"internalType":"uint256","name":"amount0BorrowedMin","type":"uint256"},{"internalType":"uint256","name":"amount1BorrowedMin","type":"uint256"},{"internalType":"uint8","name":"leverageRatio","type":"uint8"},{"internalType":"bool","name":"zeroForOne","type":"bool"}],"internalType":"struct DataStruct.OpenPositionParams","name":"params","type":"tuple"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"openPosition","outputs":[{"internalType":"uint96","name":"lienId","type":"uint96"},{"internalType":"uint256","name":"collateralTo","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"proxiableUUID","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"readYield","outputs":[{"internalType":"uint256","name":"weth","type":"uint256"},{"internalType":"uint256","name":"usdb","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"reclaimLiquidity","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenFrom","type":"address"},{"internalType":"address","name":"tokenTo","type":"address"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint256","name":"amountFrom","type":"uint256"},{"internalType":"uint256","name":"amountToMinimum","type":"uint256"},{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes","name":"oracleSignature","type":"bytes"}],"name":"swap","outputs":[{"internalType":"uint256","name":"amountReceived","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"admin","type":"address"}],"name":"updateBlastPointsAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"feeFactor","type":"uint256"}],"name":"updateFeeFactor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint128","name":"liquidationRewardFactor","type":"uint128"}],"name":"updateLiquidationRewardFactor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"loanTerm","type":"uint256"}],"name":"updateLoanTerm","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"signer","type":"address"}],"name":"updateOracleSigner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"treasuryRate","type":"uint256"}],"name":"updateTreasuryRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"validBlocks","type":"uint256"}],"name":"updateValidBlocks","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"recipient","type":"address"}],"name":"withdrawTreasury","outputs":[],"stateMutability":"nonpayable","type":"function"}]

60a060405230608052600160fb55620000176200001d565b620000de565b600054610100900460ff16156200008a5760405162461bcd60e51b815260206004820152602760248201527f496e697469616c697a61626c653a20636f6e747261637420697320696e697469604482015266616c697a696e6760c81b606482015260840160405180910390fd5b60005460ff90811614620000dc576000805460ff191660ff9081179091556040519081527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b565b6080516145af620001166000396000818161124d015281816112e80152818161175a015281816117f001526118eb01526145af6000f3fe6080604052600436106102a05760003560e01c806379ba50971161016e578063cf4b10bd116100cb578063e30c39781161007f578063e8c0858711610064578063e8c08587146109c2578063ed8964f1146109e2578063f2fde38b14610a0257600080fd5b8063e30c397814610969578063e31c5ee61461098757600080fd5b8063db9b1050116100b0578063db9b10501461086b578063dcc151471461088b578063e0d539c01461094957600080fd5b8063cf4b10bd1461082b578063db04feac1461084b57600080fd5b8063ac9650d811610122578063af2eb85411610107578063af2eb854146107b2578063b78f094c146107d2578063b7c3da411461080b57600080fd5b8063ac9650d814610646578063ad00c1df1461067357600080fd5b80638da5cb5b116101535780638da5cb5b146105b75780639f1a236d146105e9578063a10d40591461063157600080fd5b806379ba5097146105825780638025f3081461059757600080fd5b8063397457ab1161021c57806352d1902d116101d05780636bfa9e58116101b55780636bfa9e581461050a5780636f75b87d1461054d578063715018a61461056d57600080fd5b806352d1902d146104df5780635afbc4a8146104f457600080fd5b80633fd64687116102015780633fd64687146104975780634e606c47146104b75780634f1ef286146104cc57600080fd5b8063397457ab146104325780633a5074951461045257600080fd5b80632601d7bb116102735780632ecacaed116102585780632ecacaed146103db57806332a7d8de146103fb5780633659cfe61461041257600080fd5b80632601d7bb146103775780632638aceb146103bb57600080fd5b80630a22ac68146102a557806312cbc96d146102df578063150b7a02146103045780631b40e47a14610355575b600080fd5b3480156102b157600080fd5b506102c56102c03660046135d6565b610a22565b604080519283526020830191909152015b60405180910390f35b3480156102eb57600080fd5b506102f66101025481565b6040519081526020016102d6565b34801561031057600080fd5b5061032461031f36600461363e565b610b6e565b6040517fffffffff0000000000000000000000000000000000000000000000000000000090911681526020016102d6565b34801561036157600080fd5b506103756103703660046136b1565b610d7f565b005b34801561038357600080fd5b506103976103923660046136fc565b610ef5565b604080516bffffffffffffffffffffffff90931683526020830191909152016102d6565b3480156103c757600080fd5b506102c56103d636600461376d565b611044565b3480156103e757600080fd5b506103756103f63660046137ea565b6111a0565b34801561040757600080fd5b506102f66101015481565b34801561041e57600080fd5b5061037561042d366004613807565b611243565b34801561043e57600080fd5b506102f661044d366004613840565b6113e5565b34801561045e57600080fd5b5061047261046d3660046138d8565b6114fe565b604080516001600160801b0390941684526020840192909252908201526060016102d6565b3480156104a357600080fd5b506103756104b2366004613807565b61168d565b3480156104c357600080fd5b506103756116f0565b6103756104da36600461394b565b611750565b3480156104eb57600080fd5b506102f66118de565b34801561050057600080fd5b506102f660ff5481565b34801561051657600080fd5b5061052a610525366004613a27565b6119a3565b6040805164ffffffffff90941684526020840192909252908201526060016102d6565b34801561055957600080fd5b5061052a610568366004613a6a565b611acb565b34801561057957600080fd5b50610375611c14565b34801561058e57600080fd5b50610375611c28565b3480156105a357600080fd5b506103756105b2366004613ac0565b611cb3565b3480156105c357600080fd5b506033546001600160a01b03165b6040516001600160a01b0390911681526020016102d6565b3480156105f557600080fd5b50610609610604366004613af9565b611d9f565b604080519485526001600160801b0390931660208501529183015260608201526080016102d6565b34801561063d57600080fd5b506102c5611ef6565b34801561065257600080fd5b50610666610661366004613b45565b611f8f565b6040516102d69190613c0a565b34801561067f57600080fd5b5061075761068e366004613c8c565b6101046020526000908152604090208054600182015460029092015464ffffffffff8216926001600160801b03650100000000008404169262ffffff75010000000000000000000000000000000000000000008204811693780100000000000000000000000000000000000000000000000083049091169263ffffffff7b010000000000000000000000000000000000000000000000000000008404169260ff7f0100000000000000000000000000000000000000000000000000000000000000909104169188565b6040805164ffffffffff90991689526001600160801b03909716602089015262ffffff9586169688019690965293909216606086015263ffffffff166080850152151560a084015260c083015260e0820152610100016102d6565b3480156107be57600080fd5b506103756107cd366004613c8c565b61207a565b3480156107de57600080fd5b50610100546107f3906001600160801b031681565b6040516001600160801b0390911681526020016102d6565b34801561081757600080fd5b506103756108263660046135d6565b6120b7565b34801561083757600080fd5b50610375610846366004613c8c565b6121c6565b34801561085757600080fd5b5060fe546105d1906001600160a01b031681565b34801561087757600080fd5b50610375610886366004613c8c565b612226565b34801561089757600080fd5b5061090b6108a6366004613c8c565b61010360205260009081526040902080546001909101546001600160a01b0382169174010000000000000000000000000000000000000000900463ffffffff16906001600160801b038082169170010000000000000000000000000000000090041684565b604080516001600160a01b03909516855263ffffffff90931660208501526001600160801b03918216928401929092521660608201526080016102d6565b34801561095557600080fd5b50610375610964366004613807565b612287565b34801561097557600080fd5b506065546001600160a01b03166105d1565b34801561099357600080fd5b506109a76109a2366004613ca5565b61234d565b604080519384526020840192909252908201526060016102d6565b3480156109ce57600080fd5b506103756109dd366004613cda565b612477565b3480156109ee57600080fd5b506103756109fd366004613c8c565b61265f565b348015610a0e57600080fd5b50610375610a1d366004613807565b6126c1565b600080610a2d61273f565b6040805160208101889052908101869052336060820152610a80906080015b60408051601f19818403018152919052805160209091012060fe5461010254879187918a916001600160a01b031690612798565b6040517ffac40cbd000000000000000000000000000000000000000000000000000000008152610103600482015260248101879052737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba9063fac40cbd906044016040805180830381865af4158015610af0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b149190613d4c565b604080518981526020810184905290810182905291935091507f37ee88ce1be0fa51345d28686f8168c9c2a360867cb1efb29bff32a57608f63b906060015b60405180910390a1610b65600160fb55565b94509492505050565b60007fffffffffffffffffffffffffbcba8a1515f7e48ca367a056409c3284781dd8073301610d5457604080516080810182526001600160a01b03808816825260006020808401828152848601838152606086018481528b855261010390935286842095518654925163ffffffff1674010000000000000000000000000000000000000000027fffffffffffffffff000000000000000000000000000000000000000000000000909316951694909417178455915191516001600160801b039081167001000000000000000000000000000000000292169190911760019092019190915590517f99fbab8800000000000000000000000000000000000000000000000000000000815273434575eaea081b735c985fa9bf63cd7b87e227f9906399fbab8890610ca590889060040190815260200190565b61018060405180830381865afa158015610cc3573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ce79190613daf565b505050509750505050505050507f6039006ce781c6d09892c65c037c88f8eb546a9d61859ee09b8265dc6381dbb9858783604051610d4a939291909283526001600160a01b039190911660208301526001600160801b0316604082015260600190565b60405180910390a1505b507f150b7a020000000000000000000000000000000000000000000000000000000095945050505050565b610d8761273f565b610dd3848433604051602001610d9f93929190613f25565b60408051601f19818403018152919052805160209091012060fe54610102548591859188916001600160a01b031690612798565b6040517f7598d704000000000000000000000000000000000000000000000000000000008152600090819081907361d591343e022bafd1c279a7b996168669ec051790637598d70490610e2e90610104908b90600401613f51565b606060405180830381865af4158015610e4b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e6f9190613f65565b919450925090507f2b352f5a23b0848e68ca107eca5debad99df8c9441ff4e7a4aacb6ff9813aa07610ea46020890189613fa8565b604080516bffffffffffffffffffffffff90921682526001600160801b0380871660208401528516908201526060810183905260800160405180910390a1505050610eef600160fb55565b50505050565b600080610f0061273f565b610f18868633604051602001610a4c939291906140a4565b60fc80546bffffffffffffffffffffffff16906000610f36836140e8565b91906101000a8154816bffffffffffffffffffffffff02191690836bffffffffffffffffffffffff16021790555091506101047361d591343e022bafd1c279a7b996168669ec0517633b71d31e90916101036101058660ff5460fd548d6040518863ffffffff1660e01b8152600401610fb59796959493929190614113565b602060405180830381865af4158015610fd2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ff69190614158565b604080513381526bffffffffffffffffffffffff851660208201529081018290529091507fdb5a70a3d96ee3cc6ee0bc4cb26b31c01a9c7ee4f3ac019de297128e781692a690606001610b53565b60008061104f61273f565b60408051602081018b90526001600160801b038a1691810191909152606081018890526080810187905260a081018690523360c08201526110929060e001610a4c565b6040517f4d3f603d0000000000000000000000000000000000000000000000000000000081526101036004820152602481018a90526001600160801b03891660448201526064810188905260848101879052737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba90634d3f603d9060a4016040805180830381865af415801561111f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111439190613d4c565b604080518c81526001600160801b038c1660208201529294509092507fa6899bf7f2926cb46c42d4198b3edfe8b3a22a96ff039b407935c084a4b8ca77910160405180910390a1611194600160fb55565b97509795505050505050565b6111a8612898565b620186a06001600160801b03821611156111d557604051632a9ffab760e21b815260040160405180910390fd5b61010080547fffffffffffffffffffffffffffffffff00000000000000000000000000000000166001600160801b0383169081179091556040519081527f921db5ef6bde1b685b331a2ba094085211b88e316c6d9a37a72369f632ef2097906020015b60405180910390a150565b6001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001630036112e65760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f64656c656761746563616c6c000000000000000000000000000000000000000060648201526084015b60405180910390fd5b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03166113417f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b6001600160a01b0316146113bd5760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f6163746976652070726f7879000000000000000000000000000000000000000060648201526084016112dd565b6113c6816128f2565b604080516000808252602082019092526113e2918391906128fa565b50565b60006113ef61273f565b604080516001600160a01b03808c1660208301528a169181019190915262ffffff881660608201526080810187905260a0810186905260c081018590523360e082015261146e906101000160408051601f19818403018152919052805160209091012060fe54610102548691869189916001600160a01b031690612798565b61147a89333089612a9f565b61148989898989896000612be9565b9050611496883383612d56565b604080516001600160a01b03808c1682528a166020820152908101879052606081018290527ffa2dda1cc1b86e41239702756b13effbc1a092b5c57e3ad320fbe4f3b13fe2359060800160405180910390a16114f2600160fb55565b98975050505050505050565b600080600061150b61273f565b60408051602081018d90529081018b9052606081018a90526080810189905260a0810188905260c081018790523360e082015261157b90610100015b60408051601f19818403018152919052805160209091012060fe5461010254889188918b916001600160a01b031690612798565b6040517fd196491c0000000000000000000000000000000000000000000000000000000081526101036004820152602481018c9052604481018b9052606481018a90526084810189905260a48101889052737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba9063d196491c9060c401606060405180830381865af4158015611608573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061162c9190614171565b604080518f81526001600160801b038516602082015293965091945092507f6207366d3a58e6913953e71396c359ed2f3d3a2b7c9c48bb97313e84a41c55e3910160405180910390a161167f600160fb55565b985098509895505050505050565b611695612898565b60fe805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0383169081179091556040519081527f750f4f0e6aa61a09c89e9f220db3f474b4794b9a22cd824e3e6ed1133070b41f90602001611238565b6116f8612898565b73897b67fb9fa3ae2f66e6c1b0bb3e9da7e9231585634e606c476040518163ffffffff1660e01b815260040160006040518083038186803b15801561173c57600080fd5b505af4158015610eef573d6000803e3d6000fd5b6001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001630036117ee5760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f64656c656761746563616c6c000000000000000000000000000000000000000060648201526084016112dd565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03166118497f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b6001600160a01b0316146118c55760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f6163746976652070726f7879000000000000000000000000000000000000000060648201526084016112dd565b6118ce826128f2565b6118da828260016128fa565b5050565b6000306001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000161461197e5760405162461bcd60e51b815260206004820152603860248201527f555550535570677261646561626c653a206d757374206e6f742062652063616c60448201527f6c6564207468726f7567682064656c656761746563616c6c000000000000000060648201526084016112dd565b507f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc90565b60008060006119b061273f565b6119c8878733604051602001611547939291906141fa565b6040517f572800690000000000000000000000000000000000000000000000000000000081527361d591343e022bafd1c279a7b996168669ec051790635728006990611a209061010490610103908c90600401614225565b606060405180830381865af4158015611a3d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611a619190614240565b6040805133815264ffffffffff851660208201529081018390526060810182905292955090935091507f60d2d76145abf40e85498d9f144bcc4e67a6dd3827f6ab2ef67d82d4514ee5629060800160405180910390a1611ac1600160fb55565b9450945094915050565b6000806000611ad861273f565b611af288888833604051602001611547949392919061426a565b61010054610101546040517f021989850000000000000000000000000000000000000000000000000000000081527361d591343e022bafd1c279a7b996168669ec051792630219898592611b5f9261010492610103928f928f926001600160801b031691906004016142a1565b606060405180830381865af4158015611b7c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ba09190614240565b604080516001600160a01b038c16815264ffffffffff851660208201529081018390526060810182905292955090935091507f0344c58cf68b993ae8f08b1fd51903894806bf0b2a7f7c7dfe941ea083c3c6329060800160405180910390a1611c09600160fb55565b955095509592505050565b611c1c612898565b611c266000612e9f565b565b60655433906001600160a01b03168114611caa5760405162461bcd60e51b815260206004820152602960248201527f4f776e61626c6532537465703a2063616c6c6572206973206e6f74207468652060448201527f6e6577206f776e6572000000000000000000000000000000000000000000000060648201526084016112dd565b6113e281612e9f565b611cbb612898565b611cc361273f565b6001600160a01b038216600090815261010560205260409020548015611d94576001600160a01b038216611d23576040517f9c8d2cd200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6001600160a01b03831660009081526101056020526040812055611d48838383612d56565b604080516001600160a01b038086168252841660208201529081018290527f466808b09c0f23dfb5782c6a8bf8a2687ac785e1066359249f4e168d0bc35ced9060600160405180910390a15b506118da600160fb55565b600080600080611dad61273f565b611df9888833604051602001611dc5939291906143a3565b60408051601f19818403018152919052805160209091012060fe5461010254899189918c916001600160a01b031690612798565b6040517f015cc1ed000000000000000000000000000000000000000000000000000000008152737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba9063015cc1ed90611e4d90610103908c906004016143d1565b608060405180830381865af4158015611e6a573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611e8e91906143e6565b604080518581523360208201526001600160801b03851681830152905194985092965090945092507f6039006ce781c6d09892c65c037c88f8eb546a9d61859ee09b8265dc6381dbb9919081900360600190a1611eeb600160fb55565b945094509450949050565b6040517f67d9671a000000000000000000000000000000000000000000000000000000008152306004820152600090819073897b67fb9fa3ae2f66e6c1b0bb3e9da7e9231585906367d9671a906024016040805180830381865af4158015611f62573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f869190613d4c565b90939092509050565b60608167ffffffffffffffff811115611faa57611faa613935565b604051908082528060200260200182016040528015611fdd57816020015b6060815260200190600190039081611fc85790505b50905060005b828110156120725761204d3085858481811061200157612001614424565b9050602002810190612013919061443a565b8080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250612ec592505050565b82828151811061205f5761205f614424565b6020908102919091010152600101611fe3565b505b92915050565b612082612898565b6101028190556040518181527e473af516250a6b95122a50d5c82832675e43711a309cde4bb0dcc0a019669890602001611238565b6120bf61273f565b60408051602081018690529081018490523360608201526120e290608001610d9f565b6040517f43c72ee8000000000000000000000000000000000000000000000000000000008152610103600482015260248101859052600090737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba906343c72ee890604401602060405180830381865af4158015612156573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061217a919061449f565b6040805187815263ffffffff831660208201529192507fc93aa1a74d15c700d86f60bbb3f92fac72d5a5c83c76bf0ee08254e433ae0a56910160405180910390a150610eef600160fb55565b6121ce612898565b6103e88111156121f157604051632a9ffab760e21b815260040160405180910390fd5b60ff8190556040518181527f712c4ab1cda21cea40006009ce8f8c99fdc923b01871dd25f9af21a945bce4fa90602001611238565b61222e612898565b6207a12081111561225257604051632a9ffab760e21b815260040160405180910390fd5b60fd8190556040518181527f2f1e1f4c9db00c1f5b9f1fb7b358f5beb59f9d4f72b9ba22a03240abf234494690602001611238565b61228f612898565b6040517fe0d539c00000000000000000000000000000000000000000000000000000000081526001600160a01b038216600482015273897b67fb9fa3ae2f66e6c1b0bb3e9da7e92315859063e0d539c09060240160006040518083038186803b1580156122fb57600080fd5b505af415801561230f573d6000803e3d6000fd5b50506040516001600160a01b03841681527fd0ef6ee3edffa8a0434beeff8ff238a9150137cbe9d80a1a5f10c98dc013cf4192506020019050611238565b600080600061235a612898565b61236261273f565b6040517f564fb9fe0000000000000000000000000000000000000000000000000000000081526001600160a01b0387166004820152306024820152604481018690526064810185905273897b67fb9fa3ae2f66e6c1b0bb3e9da7e92315859063564fb9fe90608401606060405180830381865af41580156123e7573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061240b91906144c5565b604080516001600160a01b038b168152602081018590529081018390526060810182905292955090935091507fa87450db45edc6fc182f3e7324bef0a339634e599f43ddf35ba8e1330adad24d9060800160405180910390a161246e600160fb55565b93509350939050565b600054610100900460ff16158080156124975750600054600160ff909116105b806124b15750303b1580156124b1575060005460ff166001145b6125235760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201527f647920696e697469616c697a656400000000000000000000000000000000000060648201526084016112dd565b600080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790558015612564576000805461ff0019166101001790555b61256c612ef1565b612574612f6e565b73897b67fb9fa3ae2f66e6c1b0bb3e9da7e9231585633e0b1a236040518163ffffffff1660e01b815260040160006040518083038186803b1580156125b857600080fd5b505af41580156125cc573d6000803e3d6000fd5b505050506125d9886121c6565b6125e2876111a0565b6125eb8661265f565b6125f485612226565b6125fd8461207a565b6126068361168d565b61260f82612287565b8015612655576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050505050505050565b612667612898565b62278d0081111561268b57604051632a9ffab760e21b815260040160405180910390fd5b6101018190556040518181527f2275b941166d7eea0c8a1682929372ab7ec1bdb53c55261a23677a2eeebd3b1990602001611238565b6126c9612898565b606580546001600160a01b03831673ffffffffffffffffffffffffffffffffffffffff1990911681179091556127076033546001600160a01b031690565b6001600160a01b03167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e2270060405160405180910390a350565b600260fb54036127915760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c0060448201526064016112dd565b600260fb55565b6001600160a01b0382161561289057436127b282856144f3565b10156127ea576040517f4e47846c00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6040517f19457468657265756d205369676e6564204d6573736167653a0a3332000000006020820152603c810187905260009061284190605c01604051602081830303815290604052805190602001208787612ff3565b9050826001600160a01b0316816001600160a01b03161461288e576040517f8baa579f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b505050505050565b6033546001600160a01b03163314611c265760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016112dd565b6113e2612898565b7f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff16156129325761292d836130a9565b505050565b826001600160a01b03166352d1902d6040518163ffffffff1660e01b8152600401602060405180830381865afa92505050801561298c575060408051601f3d908101601f1916820190925261298991810190614158565b60015b6129fe5760405162461bcd60e51b815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201527f6f6e206973206e6f74205555505300000000000000000000000000000000000060648201526084016112dd565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8114612a935760405162461bcd60e51b815260206004820152602960248201527f45524331393637557067726164653a20756e737570706f727465642070726f7860448201527f6961626c6555554944000000000000000000000000000000000000000000000060648201526084016112dd565b5061292d838383613174565b604080516001600160a01b0385811660248301528481166044830152606480830185905283518084039091018152608490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f23b872dd000000000000000000000000000000000000000000000000000000001790529151600092839290881691612b319190614506565b6000604051808303816000865af19150503d8060008114612b6e576040519150601f19603f3d011682016040523d82523d6000602084013e612b73565b606091505b5091509150818015612b9d575080511580612b9d575080806020019051810190612b9d9190614522565b6128905760405162461bcd60e51b815260206004820152600360248201527f535446000000000000000000000000000000000000000000000000000000000060448201526064016112dd565b6000612c0a8773337827814155ecbf24d20231fca4444f530c055586613199565b60408051610100810182526001600160a01b0389811682528881166020830190815262ffffff8981168486019081523060608601908152426080870190815260a087018c815260c088018c81528b881660e08a0190815299517f414bf3890000000000000000000000000000000000000000000000000000000081529851881660048a0152955187166024890152925190931660448701525184166064860152905160848501525160a48401525160c4830152915190911660e482015273337827814155ecbf24d20231fca4444f530c05559063414bf38990610104016020604051808303816000875af1158015612d06573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d2a9190614158565b9050612d4c8773337827814155ecbf24d20231fca4444f530c05556000613199565b9695505050505050565b604080516001600160a01b038481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb000000000000000000000000000000000000000000000000000000001790529151600092839290871691612de09190614506565b6000604051808303816000865af19150503d8060008114612e1d576040519150601f19603f3d011682016040523d82523d6000602084013e612e22565b606091505b5091509150818015612e4c575080511580612e4c575080806020019051810190612e4c9190614522565b612e985760405162461bcd60e51b815260206004820152600260248201527f535400000000000000000000000000000000000000000000000000000000000060448201526064016112dd565b5050505050565b6065805473ffffffffffffffffffffffffffffffffffffffff191690556113e2816132db565b6060612eea83836040518060600160405280602781526020016145536027913961333a565b9392505050565b600054610100900460ff16611c265760405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201527f6e697469616c697a696e6700000000000000000000000000000000000000000060648201526084016112dd565b600054610100900460ff16612feb5760405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201527f6e697469616c697a696e6700000000000000000000000000000000000000000060648201526084016112dd565b611c266133a8565b60008060008061303886868080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525061342e92505050565b6040805160008152602081018083528c905260ff8316918101919091526060810184905260808101839052929550909350915060019060a0016020604051602081039080840390855afa158015613093573d6000803e3d6000fd5b5050604051601f19015198975050505050505050565b6001600160a01b0381163b6131265760405162461bcd60e51b815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201527f6f74206120636f6e74726163740000000000000000000000000000000000000060648201526084016112dd565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0392909216919091179055565b61317d836134a2565b60008251118061318a5750805b1561292d57610eef8383612ec5565b604080516001600160a01b038481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f095ea7b30000000000000000000000000000000000000000000000000000000017905291516000928392908716916132239190614506565b6000604051808303816000865af19150503d8060008114613260576040519150601f19603f3d011682016040523d82523d6000602084013e613265565b606091505b509150915081801561328f57508051158061328f57508080602001905181019061328f9190614522565b612e985760405162461bcd60e51b815260206004820152600260248201527f534100000000000000000000000000000000000000000000000000000000000060448201526064016112dd565b603380546001600160a01b0383811673ffffffffffffffffffffffffffffffffffffffff19831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b6060600080856001600160a01b0316856040516133579190614506565b600060405180830381855af49150503d8060008114613392576040519150601f19603f3d011682016040523d82523d6000602084013e613397565b606091505b5091509150612d4c868383876134e2565b600054610100900460ff166134255760405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201527f6e697469616c697a696e6700000000000000000000000000000000000000000060648201526084016112dd565b611c2633612e9f565b600080600083516041146134845760405162461bcd60e51b815260206004820152601860248201527f496e76616c6964207369676e6174757265206c656e677468000000000000000060448201526064016112dd565b50505060208101516040820151606090920151909260009190911a90565b6134ab816130a9565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b6060831561355157825160000361354a576001600160a01b0385163b61354a5760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016112dd565b508161355b565b61355b8383613563565b949350505050565b8151156135735781518083602001fd5b8060405162461bcd60e51b81526004016112dd919061453f565b60008083601f84011261359f57600080fd5b50813567ffffffffffffffff8111156135b757600080fd5b6020830191508360208285010111156135cf57600080fd5b9250929050565b600080600080606085870312156135ec57600080fd5b8435935060208501359250604085013567ffffffffffffffff81111561361157600080fd5b61361d8782880161358d565b95989497509550505050565b6001600160a01b03811681146113e257600080fd5b60008060008060006080868803121561365657600080fd5b853561366181613629565b9450602086013561367181613629565b935060408601359250606086013567ffffffffffffffff81111561369457600080fd5b6136a08882890161358d565b969995985093965092949392505050565b6000806000808486036101008112156136c957600080fd5b60c08112156136d757600080fd5b5084935060c0850135925060e085013567ffffffffffffffff81111561361157600080fd5b6000806000808486036101e081121561371457600080fd5b6101a08082121561372457600080fd5b8695508501359350506101c084013567ffffffffffffffff81111561361157600080fd5b6001600160801b03811681146113e257600080fd5b803561376881613748565b919050565b600080600080600080600060c0888a03121561378857600080fd5b87359650602088013561379a81613748565b955060408801359450606088013593506080880135925060a088013567ffffffffffffffff8111156137cb57600080fd5b6137d78a828b0161358d565b989b979a50959850939692959293505050565b6000602082840312156137fc57600080fd5b8135612eea81613748565b60006020828403121561381957600080fd5b8135612eea81613629565b62ffffff811681146113e257600080fd5b803561376881613824565b60008060008060008060008060e0898b03121561385c57600080fd5b883561386781613629565b9750602089013561387781613629565b9650604089013561388781613824565b9550606089013594506080890135935060a0890135925060c089013567ffffffffffffffff8111156138b857600080fd5b6138c48b828c0161358d565b999c989b5096995094979396929594505050565b60008060008060008060008060e0898b0312156138f457600080fd5b883597506020890135965060408901359550606089013594506080890135935060a0890135925060c089013567ffffffffffffffff8111156138b857600080fd5b634e487b7160e01b600052604160045260246000fd5b6000806040838503121561395e57600080fd5b823561396981613629565b9150602083013567ffffffffffffffff8082111561398657600080fd5b818501915085601f83011261399a57600080fd5b8135818111156139ac576139ac613935565b604051601f8201601f19908116603f011681019083821181831017156139d4576139d4613935565b816040528281528860208487010111156139ed57600080fd5b8260208601602083013760006020848301015280955050505050509250929050565b600060a08284031215613a2157600080fd5b50919050565b60008060008060e08587031215613a3d57600080fd5b613a478686613a0f565b935060a0850135925060c085013567ffffffffffffffff81111561361157600080fd5b60008060008060006101008688031215613a8357600080fd5b613a8d8787613a0f565b945060a0860135613a9d81613629565b935060c0860135925060e086013567ffffffffffffffff81111561369457600080fd5b60008060408385031215613ad357600080fd5b8235613ade81613629565b91506020830135613aee81613629565b809150509250929050565b600080600080848603610160811215613b1157600080fd5b61012080821215613b2157600080fd5b86955085013593505061014084013567ffffffffffffffff81111561361157600080fd5b60008060208385031215613b5857600080fd5b823567ffffffffffffffff80821115613b7057600080fd5b818501915085601f830112613b8457600080fd5b813581811115613b9357600080fd5b8660208260051b8501011115613ba857600080fd5b60209290920196919550909350505050565b60005b83811015613bd5578181015183820152602001613bbd565b50506000910152565b60008151808452613bf6816020860160208601613bba565b601f01601f19169290920160200192915050565b600060208083016020845280855180835260408601915060408160051b87010192506020870160005b82811015613c7f577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0888603018452613c6d858351613bde565b94509285019290850190600101613c33565b5092979650505050505050565b600060208284031215613c9e57600080fd5b5035919050565b600080600060608486031215613cba57600080fd5b8335613cc581613629565b95602085013595506040909401359392505050565b600080600080600080600060e0888a031215613cf557600080fd5b873596506020880135613d0781613748565b955060408801359450606088013593506080880135925060a0880135613d2c81613629565b915060c0880135613d3c81613629565b8091505092959891949750929550565b60008060408385031215613d5f57600080fd5b505080516020909101519092909150565b6bffffffffffffffffffffffff811681146113e257600080fd5b8060020b81146113e257600080fd5b805161376881613d8a565b805161376881613748565b6000806000806000806000806000806000806101808d8f031215613dd257600080fd5b8c51613ddd81613d70565b60208e0151909c50613dee81613629565b60408e0151909b50613dff81613629565b60608e0151909a50613e1081613629565b60808e0151909950613e2181613824565b60a08e0151909850613e3281613d8a565b9650613e4060c08e01613d99565b9550613e4e60e08e01613da4565b94506101008d015193506101208d01519250613e6d6101408e01613da4565b9150613e7c6101608e01613da4565b90509295989b509295989b509295989b565b80151581146113e257600080fd5b803561376881613e8e565b8035613eb281613d70565b6bffffffffffffffffffffffff1682526020810135613ed081613748565b6001600160801b03166020830152604081810135908301526060810135613ef681613824565b62ffffff1660608301526080818101359083015260a0810135613f1881613e8e565b80151560a0840152505050565b6101008101613f348286613ea7565b8360c08301526001600160a01b03831660e0830152949350505050565b82815260e08101612eea6020830184613ea7565b600080600060608486031215613f7a57600080fd5b8351613f8581613748565b6020850151909350613f9681613748565b80925050604084015190509250925092565b600060208284031215613fba57600080fd5b8135612eea81613d70565b803560ff8116811461376857600080fd5b803582526020810135602083015260408101356040830152606081013560608301526140046080820161375d565b6001600160801b0316608083015261401e60a08201613835565b62ffffff1660a083015261403460c08201613835565b62ffffff1660c083015261404a60e08201613835565b62ffffff1660e0830152610100818101359083015261012080820135908301526101408082013590830152610160614083818301613fc5565b60ff1690830152610180614098828201613e9c565b80151584830152610eef565b6101e081016140b38286613fd6565b836101a08301526001600160a01b0383166101c0830152949350505050565b634e487b7160e01b600052601160045260246000fd5b60006bffffffffffffffffffffffff808316818103614109576141096140d2565b6001019392505050565b6000610260820190508882528760208301528660408301526bffffffffffffffffffffffff861660608301528460808301528360a08301526114f260c0830184613fd6565b60006020828403121561416a57600080fd5b5051919050565b60008060006060848603121561418657600080fd5b835161419181613748565b602085015160409095015190969495509392505050565b80356141b381613d70565b6bffffffffffffffffffffffff1682526020818101359083015260408101356141db81613824565b62ffffff16604083015260608181013590830152608090810135910152565b60e0810161420882866141a8565b8360a08301526001600160a01b03831660c0830152949350505050565b8381526020810183905260e0810161355b60408301846141a8565b60008060006060848603121561425557600080fd5b835164ffffffffff8116811461419157600080fd5b610100810161427982876141a8565b6001600160a01b0380861660a08401528460c084015280841660e08401525095945050505050565b8681526020810186905261014081016142bd60408301876141a8565b6001600160a01b03851660e08301526001600160801b03841661010083015282610120830152979650505050505050565b803561376881613d8a565b803561430481613629565b6001600160a01b03168252602081013561431d81613629565b6001600160a01b0316602083015261433760408201613835565b62ffffff16604083015261434d606082016142ee565b61435c606084018260020b9052565b50614369608082016142ee565b614378608084018260020b9052565b5060a0818101359083015260c0808201359083015260e0808201359083015261010090810135910152565b61016081016143b282866142f9565b836101208301526001600160a01b038316610140830152949350505050565b8281526101408101612eea60208301846142f9565b600080600080608085870312156143fc57600080fd5b84519350602085015161440e81613748565b6040860151606090960151949790965092505050565b634e487b7160e01b600052603260045260246000fd5b60008083357fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe184360301811261446f57600080fd5b83018035915067ffffffffffffffff82111561448a57600080fd5b6020019150368190038213156135cf57600080fd5b6000602082840312156144b157600080fd5b815163ffffffff81168114612eea57600080fd5b6000806000606084860312156144da57600080fd5b8351925060208401519150604084015190509250925092565b80820180821115612074576120746140d2565b60008251614518818460208701613bba565b9190910192915050565b60006020828403121561453457600080fd5b8151612eea81613e8e565b602081526000612eea6020830184613bde56fe416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a264697066735822122079c11acf590e39cbf716a41ce7b96c516436794fd356c3233de65ed3089e94a364736f6c63430008170033

Deployed Bytecode

0x6080604052600436106102a05760003560e01c806379ba50971161016e578063cf4b10bd116100cb578063e30c39781161007f578063e8c0858711610064578063e8c08587146109c2578063ed8964f1146109e2578063f2fde38b14610a0257600080fd5b8063e30c397814610969578063e31c5ee61461098757600080fd5b8063db9b1050116100b0578063db9b10501461086b578063dcc151471461088b578063e0d539c01461094957600080fd5b8063cf4b10bd1461082b578063db04feac1461084b57600080fd5b8063ac9650d811610122578063af2eb85411610107578063af2eb854146107b2578063b78f094c146107d2578063b7c3da411461080b57600080fd5b8063ac9650d814610646578063ad00c1df1461067357600080fd5b80638da5cb5b116101535780638da5cb5b146105b75780639f1a236d146105e9578063a10d40591461063157600080fd5b806379ba5097146105825780638025f3081461059757600080fd5b8063397457ab1161021c57806352d1902d116101d05780636bfa9e58116101b55780636bfa9e581461050a5780636f75b87d1461054d578063715018a61461056d57600080fd5b806352d1902d146104df5780635afbc4a8146104f457600080fd5b80633fd64687116102015780633fd64687146104975780634e606c47146104b75780634f1ef286146104cc57600080fd5b8063397457ab146104325780633a5074951461045257600080fd5b80632601d7bb116102735780632ecacaed116102585780632ecacaed146103db57806332a7d8de146103fb5780633659cfe61461041257600080fd5b80632601d7bb146103775780632638aceb146103bb57600080fd5b80630a22ac68146102a557806312cbc96d146102df578063150b7a02146103045780631b40e47a14610355575b600080fd5b3480156102b157600080fd5b506102c56102c03660046135d6565b610a22565b604080519283526020830191909152015b60405180910390f35b3480156102eb57600080fd5b506102f66101025481565b6040519081526020016102d6565b34801561031057600080fd5b5061032461031f36600461363e565b610b6e565b6040517fffffffff0000000000000000000000000000000000000000000000000000000090911681526020016102d6565b34801561036157600080fd5b506103756103703660046136b1565b610d7f565b005b34801561038357600080fd5b506103976103923660046136fc565b610ef5565b604080516bffffffffffffffffffffffff90931683526020830191909152016102d6565b3480156103c757600080fd5b506102c56103d636600461376d565b611044565b3480156103e757600080fd5b506103756103f63660046137ea565b6111a0565b34801561040757600080fd5b506102f66101015481565b34801561041e57600080fd5b5061037561042d366004613807565b611243565b34801561043e57600080fd5b506102f661044d366004613840565b6113e5565b34801561045e57600080fd5b5061047261046d3660046138d8565b6114fe565b604080516001600160801b0390941684526020840192909252908201526060016102d6565b3480156104a357600080fd5b506103756104b2366004613807565b61168d565b3480156104c357600080fd5b506103756116f0565b6103756104da36600461394b565b611750565b3480156104eb57600080fd5b506102f66118de565b34801561050057600080fd5b506102f660ff5481565b34801561051657600080fd5b5061052a610525366004613a27565b6119a3565b6040805164ffffffffff90941684526020840192909252908201526060016102d6565b34801561055957600080fd5b5061052a610568366004613a6a565b611acb565b34801561057957600080fd5b50610375611c14565b34801561058e57600080fd5b50610375611c28565b3480156105a357600080fd5b506103756105b2366004613ac0565b611cb3565b3480156105c357600080fd5b506033546001600160a01b03165b6040516001600160a01b0390911681526020016102d6565b3480156105f557600080fd5b50610609610604366004613af9565b611d9f565b604080519485526001600160801b0390931660208501529183015260608201526080016102d6565b34801561063d57600080fd5b506102c5611ef6565b34801561065257600080fd5b50610666610661366004613b45565b611f8f565b6040516102d69190613c0a565b34801561067f57600080fd5b5061075761068e366004613c8c565b6101046020526000908152604090208054600182015460029092015464ffffffffff8216926001600160801b03650100000000008404169262ffffff75010000000000000000000000000000000000000000008204811693780100000000000000000000000000000000000000000000000083049091169263ffffffff7b010000000000000000000000000000000000000000000000000000008404169260ff7f0100000000000000000000000000000000000000000000000000000000000000909104169188565b6040805164ffffffffff90991689526001600160801b03909716602089015262ffffff9586169688019690965293909216606086015263ffffffff166080850152151560a084015260c083015260e0820152610100016102d6565b3480156107be57600080fd5b506103756107cd366004613c8c565b61207a565b3480156107de57600080fd5b50610100546107f3906001600160801b031681565b6040516001600160801b0390911681526020016102d6565b34801561081757600080fd5b506103756108263660046135d6565b6120b7565b34801561083757600080fd5b50610375610846366004613c8c565b6121c6565b34801561085757600080fd5b5060fe546105d1906001600160a01b031681565b34801561087757600080fd5b50610375610886366004613c8c565b612226565b34801561089757600080fd5b5061090b6108a6366004613c8c565b61010360205260009081526040902080546001909101546001600160a01b0382169174010000000000000000000000000000000000000000900463ffffffff16906001600160801b038082169170010000000000000000000000000000000090041684565b604080516001600160a01b03909516855263ffffffff90931660208501526001600160801b03918216928401929092521660608201526080016102d6565b34801561095557600080fd5b50610375610964366004613807565b612287565b34801561097557600080fd5b506065546001600160a01b03166105d1565b34801561099357600080fd5b506109a76109a2366004613ca5565b61234d565b604080519384526020840192909252908201526060016102d6565b3480156109ce57600080fd5b506103756109dd366004613cda565b612477565b3480156109ee57600080fd5b506103756109fd366004613c8c565b61265f565b348015610a0e57600080fd5b50610375610a1d366004613807565b6126c1565b600080610a2d61273f565b6040805160208101889052908101869052336060820152610a80906080015b60408051601f19818403018152919052805160209091012060fe5461010254879187918a916001600160a01b031690612798565b6040517ffac40cbd000000000000000000000000000000000000000000000000000000008152610103600482015260248101879052737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba9063fac40cbd906044016040805180830381865af4158015610af0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b149190613d4c565b604080518981526020810184905290810182905291935091507f37ee88ce1be0fa51345d28686f8168c9c2a360867cb1efb29bff32a57608f63b906060015b60405180910390a1610b65600160fb55565b94509492505050565b60007fffffffffffffffffffffffffbcba8a1515f7e48ca367a056409c3284781dd8073301610d5457604080516080810182526001600160a01b03808816825260006020808401828152848601838152606086018481528b855261010390935286842095518654925163ffffffff1674010000000000000000000000000000000000000000027fffffffffffffffff000000000000000000000000000000000000000000000000909316951694909417178455915191516001600160801b039081167001000000000000000000000000000000000292169190911760019092019190915590517f99fbab8800000000000000000000000000000000000000000000000000000000815273434575eaea081b735c985fa9bf63cd7b87e227f9906399fbab8890610ca590889060040190815260200190565b61018060405180830381865afa158015610cc3573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ce79190613daf565b505050509750505050505050507f6039006ce781c6d09892c65c037c88f8eb546a9d61859ee09b8265dc6381dbb9858783604051610d4a939291909283526001600160a01b039190911660208301526001600160801b0316604082015260600190565b60405180910390a1505b507f150b7a020000000000000000000000000000000000000000000000000000000095945050505050565b610d8761273f565b610dd3848433604051602001610d9f93929190613f25565b60408051601f19818403018152919052805160209091012060fe54610102548591859188916001600160a01b031690612798565b6040517f7598d704000000000000000000000000000000000000000000000000000000008152600090819081907361d591343e022bafd1c279a7b996168669ec051790637598d70490610e2e90610104908b90600401613f51565b606060405180830381865af4158015610e4b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e6f9190613f65565b919450925090507f2b352f5a23b0848e68ca107eca5debad99df8c9441ff4e7a4aacb6ff9813aa07610ea46020890189613fa8565b604080516bffffffffffffffffffffffff90921682526001600160801b0380871660208401528516908201526060810183905260800160405180910390a1505050610eef600160fb55565b50505050565b600080610f0061273f565b610f18868633604051602001610a4c939291906140a4565b60fc80546bffffffffffffffffffffffff16906000610f36836140e8565b91906101000a8154816bffffffffffffffffffffffff02191690836bffffffffffffffffffffffff16021790555091506101047361d591343e022bafd1c279a7b996168669ec0517633b71d31e90916101036101058660ff5460fd548d6040518863ffffffff1660e01b8152600401610fb59796959493929190614113565b602060405180830381865af4158015610fd2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ff69190614158565b604080513381526bffffffffffffffffffffffff851660208201529081018290529091507fdb5a70a3d96ee3cc6ee0bc4cb26b31c01a9c7ee4f3ac019de297128e781692a690606001610b53565b60008061104f61273f565b60408051602081018b90526001600160801b038a1691810191909152606081018890526080810187905260a081018690523360c08201526110929060e001610a4c565b6040517f4d3f603d0000000000000000000000000000000000000000000000000000000081526101036004820152602481018a90526001600160801b03891660448201526064810188905260848101879052737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba90634d3f603d9060a4016040805180830381865af415801561111f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111439190613d4c565b604080518c81526001600160801b038c1660208201529294509092507fa6899bf7f2926cb46c42d4198b3edfe8b3a22a96ff039b407935c084a4b8ca77910160405180910390a1611194600160fb55565b97509795505050505050565b6111a8612898565b620186a06001600160801b03821611156111d557604051632a9ffab760e21b815260040160405180910390fd5b61010080547fffffffffffffffffffffffffffffffff00000000000000000000000000000000166001600160801b0383169081179091556040519081527f921db5ef6bde1b685b331a2ba094085211b88e316c6d9a37a72369f632ef2097906020015b60405180910390a150565b6001600160a01b037f00000000000000000000000021e159bc5d0d950ca67a230a19896249256316831630036112e65760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f64656c656761746563616c6c000000000000000000000000000000000000000060648201526084015b60405180910390fd5b7f00000000000000000000000021e159bc5d0d950ca67a230a19896249256316836001600160a01b03166113417f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b6001600160a01b0316146113bd5760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f6163746976652070726f7879000000000000000000000000000000000000000060648201526084016112dd565b6113c6816128f2565b604080516000808252602082019092526113e2918391906128fa565b50565b60006113ef61273f565b604080516001600160a01b03808c1660208301528a169181019190915262ffffff881660608201526080810187905260a0810186905260c081018590523360e082015261146e906101000160408051601f19818403018152919052805160209091012060fe54610102548691869189916001600160a01b031690612798565b61147a89333089612a9f565b61148989898989896000612be9565b9050611496883383612d56565b604080516001600160a01b03808c1682528a166020820152908101879052606081018290527ffa2dda1cc1b86e41239702756b13effbc1a092b5c57e3ad320fbe4f3b13fe2359060800160405180910390a16114f2600160fb55565b98975050505050505050565b600080600061150b61273f565b60408051602081018d90529081018b9052606081018a90526080810189905260a0810188905260c081018790523360e082015261157b90610100015b60408051601f19818403018152919052805160209091012060fe5461010254889188918b916001600160a01b031690612798565b6040517fd196491c0000000000000000000000000000000000000000000000000000000081526101036004820152602481018c9052604481018b9052606481018a90526084810189905260a48101889052737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba9063d196491c9060c401606060405180830381865af4158015611608573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061162c9190614171565b604080518f81526001600160801b038516602082015293965091945092507f6207366d3a58e6913953e71396c359ed2f3d3a2b7c9c48bb97313e84a41c55e3910160405180910390a161167f600160fb55565b985098509895505050505050565b611695612898565b60fe805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0383169081179091556040519081527f750f4f0e6aa61a09c89e9f220db3f474b4794b9a22cd824e3e6ed1133070b41f90602001611238565b6116f8612898565b73897b67fb9fa3ae2f66e6c1b0bb3e9da7e9231585634e606c476040518163ffffffff1660e01b815260040160006040518083038186803b15801561173c57600080fd5b505af4158015610eef573d6000803e3d6000fd5b6001600160a01b037f00000000000000000000000021e159bc5d0d950ca67a230a19896249256316831630036117ee5760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f64656c656761746563616c6c000000000000000000000000000000000000000060648201526084016112dd565b7f00000000000000000000000021e159bc5d0d950ca67a230a19896249256316836001600160a01b03166118497f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b6001600160a01b0316146118c55760405162461bcd60e51b815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f6163746976652070726f7879000000000000000000000000000000000000000060648201526084016112dd565b6118ce826128f2565b6118da828260016128fa565b5050565b6000306001600160a01b037f00000000000000000000000021e159bc5d0d950ca67a230a1989624925631683161461197e5760405162461bcd60e51b815260206004820152603860248201527f555550535570677261646561626c653a206d757374206e6f742062652063616c60448201527f6c6564207468726f7567682064656c656761746563616c6c000000000000000060648201526084016112dd565b507f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc90565b60008060006119b061273f565b6119c8878733604051602001611547939291906141fa565b6040517f572800690000000000000000000000000000000000000000000000000000000081527361d591343e022bafd1c279a7b996168669ec051790635728006990611a209061010490610103908c90600401614225565b606060405180830381865af4158015611a3d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611a619190614240565b6040805133815264ffffffffff851660208201529081018390526060810182905292955090935091507f60d2d76145abf40e85498d9f144bcc4e67a6dd3827f6ab2ef67d82d4514ee5629060800160405180910390a1611ac1600160fb55565b9450945094915050565b6000806000611ad861273f565b611af288888833604051602001611547949392919061426a565b61010054610101546040517f021989850000000000000000000000000000000000000000000000000000000081527361d591343e022bafd1c279a7b996168669ec051792630219898592611b5f9261010492610103928f928f926001600160801b031691906004016142a1565b606060405180830381865af4158015611b7c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ba09190614240565b604080516001600160a01b038c16815264ffffffffff851660208201529081018390526060810182905292955090935091507f0344c58cf68b993ae8f08b1fd51903894806bf0b2a7f7c7dfe941ea083c3c6329060800160405180910390a1611c09600160fb55565b955095509592505050565b611c1c612898565b611c266000612e9f565b565b60655433906001600160a01b03168114611caa5760405162461bcd60e51b815260206004820152602960248201527f4f776e61626c6532537465703a2063616c6c6572206973206e6f74207468652060448201527f6e6577206f776e6572000000000000000000000000000000000000000000000060648201526084016112dd565b6113e281612e9f565b611cbb612898565b611cc361273f565b6001600160a01b038216600090815261010560205260409020548015611d94576001600160a01b038216611d23576040517f9c8d2cd200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6001600160a01b03831660009081526101056020526040812055611d48838383612d56565b604080516001600160a01b038086168252841660208201529081018290527f466808b09c0f23dfb5782c6a8bf8a2687ac785e1066359249f4e168d0bc35ced9060600160405180910390a15b506118da600160fb55565b600080600080611dad61273f565b611df9888833604051602001611dc5939291906143a3565b60408051601f19818403018152919052805160209091012060fe5461010254899189918c916001600160a01b031690612798565b6040517f015cc1ed000000000000000000000000000000000000000000000000000000008152737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba9063015cc1ed90611e4d90610103908c906004016143d1565b608060405180830381865af4158015611e6a573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611e8e91906143e6565b604080518581523360208201526001600160801b03851681830152905194985092965090945092507f6039006ce781c6d09892c65c037c88f8eb546a9d61859ee09b8265dc6381dbb9919081900360600190a1611eeb600160fb55565b945094509450949050565b6040517f67d9671a000000000000000000000000000000000000000000000000000000008152306004820152600090819073897b67fb9fa3ae2f66e6c1b0bb3e9da7e9231585906367d9671a906024016040805180830381865af4158015611f62573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f869190613d4c565b90939092509050565b60608167ffffffffffffffff811115611faa57611faa613935565b604051908082528060200260200182016040528015611fdd57816020015b6060815260200190600190039081611fc85790505b50905060005b828110156120725761204d3085858481811061200157612001614424565b9050602002810190612013919061443a565b8080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250612ec592505050565b82828151811061205f5761205f614424565b6020908102919091010152600101611fe3565b505b92915050565b612082612898565b6101028190556040518181527e473af516250a6b95122a50d5c82832675e43711a309cde4bb0dcc0a019669890602001611238565b6120bf61273f565b60408051602081018690529081018490523360608201526120e290608001610d9f565b6040517f43c72ee8000000000000000000000000000000000000000000000000000000008152610103600482015260248101859052600090737ddc548f8d68d6ca01ca36a51dc8a05f1e7f20ba906343c72ee890604401602060405180830381865af4158015612156573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061217a919061449f565b6040805187815263ffffffff831660208201529192507fc93aa1a74d15c700d86f60bbb3f92fac72d5a5c83c76bf0ee08254e433ae0a56910160405180910390a150610eef600160fb55565b6121ce612898565b6103e88111156121f157604051632a9ffab760e21b815260040160405180910390fd5b60ff8190556040518181527f712c4ab1cda21cea40006009ce8f8c99fdc923b01871dd25f9af21a945bce4fa90602001611238565b61222e612898565b6207a12081111561225257604051632a9ffab760e21b815260040160405180910390fd5b60fd8190556040518181527f2f1e1f4c9db00c1f5b9f1fb7b358f5beb59f9d4f72b9ba22a03240abf234494690602001611238565b61228f612898565b6040517fe0d539c00000000000000000000000000000000000000000000000000000000081526001600160a01b038216600482015273897b67fb9fa3ae2f66e6c1b0bb3e9da7e92315859063e0d539c09060240160006040518083038186803b1580156122fb57600080fd5b505af415801561230f573d6000803e3d6000fd5b50506040516001600160a01b03841681527fd0ef6ee3edffa8a0434beeff8ff238a9150137cbe9d80a1a5f10c98dc013cf4192506020019050611238565b600080600061235a612898565b61236261273f565b6040517f564fb9fe0000000000000000000000000000000000000000000000000000000081526001600160a01b0387166004820152306024820152604481018690526064810185905273897b67fb9fa3ae2f66e6c1b0bb3e9da7e92315859063564fb9fe90608401606060405180830381865af41580156123e7573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061240b91906144c5565b604080516001600160a01b038b168152602081018590529081018390526060810182905292955090935091507fa87450db45edc6fc182f3e7324bef0a339634e599f43ddf35ba8e1330adad24d9060800160405180910390a161246e600160fb55565b93509350939050565b600054610100900460ff16158080156124975750600054600160ff909116105b806124b15750303b1580156124b1575060005460ff166001145b6125235760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201527f647920696e697469616c697a656400000000000000000000000000000000000060648201526084016112dd565b600080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790558015612564576000805461ff0019166101001790555b61256c612ef1565b612574612f6e565b73897b67fb9fa3ae2f66e6c1b0bb3e9da7e9231585633e0b1a236040518163ffffffff1660e01b815260040160006040518083038186803b1580156125b857600080fd5b505af41580156125cc573d6000803e3d6000fd5b505050506125d9886121c6565b6125e2876111a0565b6125eb8661265f565b6125f485612226565b6125fd8461207a565b6126068361168d565b61260f82612287565b8015612655576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050505050505050565b612667612898565b62278d0081111561268b57604051632a9ffab760e21b815260040160405180910390fd5b6101018190556040518181527f2275b941166d7eea0c8a1682929372ab7ec1bdb53c55261a23677a2eeebd3b1990602001611238565b6126c9612898565b606580546001600160a01b03831673ffffffffffffffffffffffffffffffffffffffff1990911681179091556127076033546001600160a01b031690565b6001600160a01b03167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e2270060405160405180910390a350565b600260fb54036127915760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c0060448201526064016112dd565b600260fb55565b6001600160a01b0382161561289057436127b282856144f3565b10156127ea576040517f4e47846c00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6040517f19457468657265756d205369676e6564204d6573736167653a0a3332000000006020820152603c810187905260009061284190605c01604051602081830303815290604052805190602001208787612ff3565b9050826001600160a01b0316816001600160a01b03161461288e576040517f8baa579f00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505b505050505050565b6033546001600160a01b03163314611c265760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016112dd565b6113e2612898565b7f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff16156129325761292d836130a9565b505050565b826001600160a01b03166352d1902d6040518163ffffffff1660e01b8152600401602060405180830381865afa92505050801561298c575060408051601f3d908101601f1916820190925261298991810190614158565b60015b6129fe5760405162461bcd60e51b815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201527f6f6e206973206e6f74205555505300000000000000000000000000000000000060648201526084016112dd565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8114612a935760405162461bcd60e51b815260206004820152602960248201527f45524331393637557067726164653a20756e737570706f727465642070726f7860448201527f6961626c6555554944000000000000000000000000000000000000000000000060648201526084016112dd565b5061292d838383613174565b604080516001600160a01b0385811660248301528481166044830152606480830185905283518084039091018152608490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f23b872dd000000000000000000000000000000000000000000000000000000001790529151600092839290881691612b319190614506565b6000604051808303816000865af19150503d8060008114612b6e576040519150601f19603f3d011682016040523d82523d6000602084013e612b73565b606091505b5091509150818015612b9d575080511580612b9d575080806020019051810190612b9d9190614522565b6128905760405162461bcd60e51b815260206004820152600360248201527f535446000000000000000000000000000000000000000000000000000000000060448201526064016112dd565b6000612c0a8773337827814155ecbf24d20231fca4444f530c055586613199565b60408051610100810182526001600160a01b0389811682528881166020830190815262ffffff8981168486019081523060608601908152426080870190815260a087018c815260c088018c81528b881660e08a0190815299517f414bf3890000000000000000000000000000000000000000000000000000000081529851881660048a0152955187166024890152925190931660448701525184166064860152905160848501525160a48401525160c4830152915190911660e482015273337827814155ecbf24d20231fca4444f530c05559063414bf38990610104016020604051808303816000875af1158015612d06573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d2a9190614158565b9050612d4c8773337827814155ecbf24d20231fca4444f530c05556000613199565b9695505050505050565b604080516001600160a01b038481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb000000000000000000000000000000000000000000000000000000001790529151600092839290871691612de09190614506565b6000604051808303816000865af19150503d8060008114612e1d576040519150601f19603f3d011682016040523d82523d6000602084013e612e22565b606091505b5091509150818015612e4c575080511580612e4c575080806020019051810190612e4c9190614522565b612e985760405162461bcd60e51b815260206004820152600260248201527f535400000000000000000000000000000000000000000000000000000000000060448201526064016112dd565b5050505050565b6065805473ffffffffffffffffffffffffffffffffffffffff191690556113e2816132db565b6060612eea83836040518060600160405280602781526020016145536027913961333a565b9392505050565b600054610100900460ff16611c265760405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201527f6e697469616c697a696e6700000000000000000000000000000000000000000060648201526084016112dd565b600054610100900460ff16612feb5760405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201527f6e697469616c697a696e6700000000000000000000000000000000000000000060648201526084016112dd565b611c266133a8565b60008060008061303886868080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525061342e92505050565b6040805160008152602081018083528c905260ff8316918101919091526060810184905260808101839052929550909350915060019060a0016020604051602081039080840390855afa158015613093573d6000803e3d6000fd5b5050604051601f19015198975050505050505050565b6001600160a01b0381163b6131265760405162461bcd60e51b815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201527f6f74206120636f6e74726163740000000000000000000000000000000000000060648201526084016112dd565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0392909216919091179055565b61317d836134a2565b60008251118061318a5750805b1561292d57610eef8383612ec5565b604080516001600160a01b038481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f095ea7b30000000000000000000000000000000000000000000000000000000017905291516000928392908716916132239190614506565b6000604051808303816000865af19150503d8060008114613260576040519150601f19603f3d011682016040523d82523d6000602084013e613265565b606091505b509150915081801561328f57508051158061328f57508080602001905181019061328f9190614522565b612e985760405162461bcd60e51b815260206004820152600260248201527f534100000000000000000000000000000000000000000000000000000000000060448201526064016112dd565b603380546001600160a01b0383811673ffffffffffffffffffffffffffffffffffffffff19831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b6060600080856001600160a01b0316856040516133579190614506565b600060405180830381855af49150503d8060008114613392576040519150601f19603f3d011682016040523d82523d6000602084013e613397565b606091505b5091509150612d4c868383876134e2565b600054610100900460ff166134255760405162461bcd60e51b815260206004820152602b60248201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960448201527f6e697469616c697a696e6700000000000000000000000000000000000000000060648201526084016112dd565b611c2633612e9f565b600080600083516041146134845760405162461bcd60e51b815260206004820152601860248201527f496e76616c6964207369676e6174757265206c656e677468000000000000000060448201526064016112dd565b50505060208101516040820151606090920151909260009190911a90565b6134ab816130a9565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b6060831561355157825160000361354a576001600160a01b0385163b61354a5760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016112dd565b508161355b565b61355b8383613563565b949350505050565b8151156135735781518083602001fd5b8060405162461bcd60e51b81526004016112dd919061453f565b60008083601f84011261359f57600080fd5b50813567ffffffffffffffff8111156135b757600080fd5b6020830191508360208285010111156135cf57600080fd5b9250929050565b600080600080606085870312156135ec57600080fd5b8435935060208501359250604085013567ffffffffffffffff81111561361157600080fd5b61361d8782880161358d565b95989497509550505050565b6001600160a01b03811681146113e257600080fd5b60008060008060006080868803121561365657600080fd5b853561366181613629565b9450602086013561367181613629565b935060408601359250606086013567ffffffffffffffff81111561369457600080fd5b6136a08882890161358d565b969995985093965092949392505050565b6000806000808486036101008112156136c957600080fd5b60c08112156136d757600080fd5b5084935060c0850135925060e085013567ffffffffffffffff81111561361157600080fd5b6000806000808486036101e081121561371457600080fd5b6101a08082121561372457600080fd5b8695508501359350506101c084013567ffffffffffffffff81111561361157600080fd5b6001600160801b03811681146113e257600080fd5b803561376881613748565b919050565b600080600080600080600060c0888a03121561378857600080fd5b87359650602088013561379a81613748565b955060408801359450606088013593506080880135925060a088013567ffffffffffffffff8111156137cb57600080fd5b6137d78a828b0161358d565b989b979a50959850939692959293505050565b6000602082840312156137fc57600080fd5b8135612eea81613748565b60006020828403121561381957600080fd5b8135612eea81613629565b62ffffff811681146113e257600080fd5b803561376881613824565b60008060008060008060008060e0898b03121561385c57600080fd5b883561386781613629565b9750602089013561387781613629565b9650604089013561388781613824565b9550606089013594506080890135935060a0890135925060c089013567ffffffffffffffff8111156138b857600080fd5b6138c48b828c0161358d565b999c989b5096995094979396929594505050565b60008060008060008060008060e0898b0312156138f457600080fd5b883597506020890135965060408901359550606089013594506080890135935060a0890135925060c089013567ffffffffffffffff8111156138b857600080fd5b634e487b7160e01b600052604160045260246000fd5b6000806040838503121561395e57600080fd5b823561396981613629565b9150602083013567ffffffffffffffff8082111561398657600080fd5b818501915085601f83011261399a57600080fd5b8135818111156139ac576139ac613935565b604051601f8201601f19908116603f011681019083821181831017156139d4576139d4613935565b816040528281528860208487010111156139ed57600080fd5b8260208601602083013760006020848301015280955050505050509250929050565b600060a08284031215613a2157600080fd5b50919050565b60008060008060e08587031215613a3d57600080fd5b613a478686613a0f565b935060a0850135925060c085013567ffffffffffffffff81111561361157600080fd5b60008060008060006101008688031215613a8357600080fd5b613a8d8787613a0f565b945060a0860135613a9d81613629565b935060c0860135925060e086013567ffffffffffffffff81111561369457600080fd5b60008060408385031215613ad357600080fd5b8235613ade81613629565b91506020830135613aee81613629565b809150509250929050565b600080600080848603610160811215613b1157600080fd5b61012080821215613b2157600080fd5b86955085013593505061014084013567ffffffffffffffff81111561361157600080fd5b60008060208385031215613b5857600080fd5b823567ffffffffffffffff80821115613b7057600080fd5b818501915085601f830112613b8457600080fd5b813581811115613b9357600080fd5b8660208260051b8501011115613ba857600080fd5b60209290920196919550909350505050565b60005b83811015613bd5578181015183820152602001613bbd565b50506000910152565b60008151808452613bf6816020860160208601613bba565b601f01601f19169290920160200192915050565b600060208083016020845280855180835260408601915060408160051b87010192506020870160005b82811015613c7f577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0888603018452613c6d858351613bde565b94509285019290850190600101613c33565b5092979650505050505050565b600060208284031215613c9e57600080fd5b5035919050565b600080600060608486031215613cba57600080fd5b8335613cc581613629565b95602085013595506040909401359392505050565b600080600080600080600060e0888a031215613cf557600080fd5b873596506020880135613d0781613748565b955060408801359450606088013593506080880135925060a0880135613d2c81613629565b915060c0880135613d3c81613629565b8091505092959891949750929550565b60008060408385031215613d5f57600080fd5b505080516020909101519092909150565b6bffffffffffffffffffffffff811681146113e257600080fd5b8060020b81146113e257600080fd5b805161376881613d8a565b805161376881613748565b6000806000806000806000806000806000806101808d8f031215613dd257600080fd5b8c51613ddd81613d70565b60208e0151909c50613dee81613629565b60408e0151909b50613dff81613629565b60608e0151909a50613e1081613629565b60808e0151909950613e2181613824565b60a08e0151909850613e3281613d8a565b9650613e4060c08e01613d99565b9550613e4e60e08e01613da4565b94506101008d015193506101208d01519250613e6d6101408e01613da4565b9150613e7c6101608e01613da4565b90509295989b509295989b509295989b565b80151581146113e257600080fd5b803561376881613e8e565b8035613eb281613d70565b6bffffffffffffffffffffffff1682526020810135613ed081613748565b6001600160801b03166020830152604081810135908301526060810135613ef681613824565b62ffffff1660608301526080818101359083015260a0810135613f1881613e8e565b80151560a0840152505050565b6101008101613f348286613ea7565b8360c08301526001600160a01b03831660e0830152949350505050565b82815260e08101612eea6020830184613ea7565b600080600060608486031215613f7a57600080fd5b8351613f8581613748565b6020850151909350613f9681613748565b80925050604084015190509250925092565b600060208284031215613fba57600080fd5b8135612eea81613d70565b803560ff8116811461376857600080fd5b803582526020810135602083015260408101356040830152606081013560608301526140046080820161375d565b6001600160801b0316608083015261401e60a08201613835565b62ffffff1660a083015261403460c08201613835565b62ffffff1660c083015261404a60e08201613835565b62ffffff1660e0830152610100818101359083015261012080820135908301526101408082013590830152610160614083818301613fc5565b60ff1690830152610180614098828201613e9c565b80151584830152610eef565b6101e081016140b38286613fd6565b836101a08301526001600160a01b0383166101c0830152949350505050565b634e487b7160e01b600052601160045260246000fd5b60006bffffffffffffffffffffffff808316818103614109576141096140d2565b6001019392505050565b6000610260820190508882528760208301528660408301526bffffffffffffffffffffffff861660608301528460808301528360a08301526114f260c0830184613fd6565b60006020828403121561416a57600080fd5b5051919050565b60008060006060848603121561418657600080fd5b835161419181613748565b602085015160409095015190969495509392505050565b80356141b381613d70565b6bffffffffffffffffffffffff1682526020818101359083015260408101356141db81613824565b62ffffff16604083015260608181013590830152608090810135910152565b60e0810161420882866141a8565b8360a08301526001600160a01b03831660c0830152949350505050565b8381526020810183905260e0810161355b60408301846141a8565b60008060006060848603121561425557600080fd5b835164ffffffffff8116811461419157600080fd5b610100810161427982876141a8565b6001600160a01b0380861660a08401528460c084015280841660e08401525095945050505050565b8681526020810186905261014081016142bd60408301876141a8565b6001600160a01b03851660e08301526001600160801b03841661010083015282610120830152979650505050505050565b803561376881613d8a565b803561430481613629565b6001600160a01b03168252602081013561431d81613629565b6001600160a01b0316602083015261433760408201613835565b62ffffff16604083015261434d606082016142ee565b61435c606084018260020b9052565b50614369608082016142ee565b614378608084018260020b9052565b5060a0818101359083015260c0808201359083015260e0808201359083015261010090810135910152565b61016081016143b282866142f9565b836101208301526001600160a01b038316610140830152949350505050565b8281526101408101612eea60208301846142f9565b600080600080608085870312156143fc57600080fd5b84519350602085015161440e81613748565b6040860151606090960151949790965092505050565b634e487b7160e01b600052603260045260246000fd5b60008083357fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe184360301811261446f57600080fd5b83018035915067ffffffffffffffff82111561448a57600080fd5b6020019150368190038213156135cf57600080fd5b6000602082840312156144b157600080fd5b815163ffffffff81168114612eea57600080fd5b6000806000606084860312156144da57600080fd5b8351925060208401519150604084015190509250925092565b80820180821115612074576120746140d2565b60008251614518818460208701613bba565b9190910192915050565b60006020828403121561453457600080fd5b8151612eea81613e8e565b602081526000612eea6020830184613bde56fe416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a264697066735822122079c11acf590e39cbf716a41ce7b96c516436794fd356c3233de65ed3089e94a364736f6c63430008170033

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.