ETH Price: $1,972.27 (+0.18%)
 

Overview

ETH Balance

0 ETH

Eth Value

$0.00

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Method
Block
From
To
Borrow Given Pri...168779322023-03-21 18:57:471068 days ago1679425067IN
Timeswap V2: Periphery Uniswap V3 Borrow Given Principal
0 ETH0.0118917121.77456208
Borrow Given Pri...168529192023-03-18 6:39:591072 days ago1679121599IN
Timeswap V2: Periphery Uniswap V3 Borrow Given Principal
0 ETH0.0094466614.84661821
Borrow Given Pri...168094312023-03-12 3:58:591078 days ago1678593539IN
Timeswap V2: Periphery Uniswap V3 Borrow Given Principal
0 ETH0.0107872316.89375792
Borrow Given Pri...167983182023-03-10 14:27:471079 days ago1678458467IN
Timeswap V2: Periphery Uniswap V3 Borrow Given Principal
0 ETH0.0223567229.9322041
Borrow Given Pri...167832372023-03-08 11:27:231081 days ago1678274843IN
Timeswap V2: Periphery Uniswap V3 Borrow Given Principal
0 ETH0.0128313520.68451696
Borrow Given Pri...167788372023-03-07 20:36:591082 days ago1678221419IN
Timeswap V2: Periphery Uniswap V3 Borrow Given Principal
0 ETH0.0288713238.8648901

Latest 1 internal transaction

Advanced mode:
Parent Transaction Hash Method Block
From
To
0x61012060167783642023-03-07 19:01:231082 days ago1678215683  Contract Creation0 ETH
Loading...
Loading
Loading...
Loading
Cross-Chain Transactions

Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
TimeswapV2PeripheryUniswapV3BorrowGivenPrincipal

Compiler Version
v0.8.8+commit.dddeac2f

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {Error} from "@timeswap-labs/v2-library/contracts/Error.sol";

import {ITimeswapV2Pool} from "@timeswap-labs/v2-pool/contracts/interfaces/ITimeswapV2Pool.sol";

import {PoolFactoryLibrary} from "@timeswap-labs/v2-pool/contracts/libraries/PoolFactory.sol";

import {TimeswapV2PeripheryBorrowGivenPrincipal} from "@timeswap-labs/v2-periphery/contracts/TimeswapV2PeripheryBorrowGivenPrincipal.sol";

import {TimeswapV2PeripheryBorrowGivenPrincipalParam} from "@timeswap-labs/v2-periphery/contracts/structs/Param.sol";
import {TimeswapV2PeripheryBorrowGivenPrincipalInternalParam} from "@timeswap-labs/v2-periphery/contracts/structs/InternalParam.sol";

import {UniswapV3FactoryLibrary} from "./libraries/UniswapV3Factory.sol";
import {UniswapV3PoolLibrary} from "./libraries/UniswapV3Pool.sol";

import {ITimeswapV2PeripheryUniswapV3BorrowGivenPrincipal} from "./interfaces/ITimeswapV2PeripheryUniswapV3BorrowGivenPrincipal.sol";

import {TimeswapV2PeripheryUniswapV3BorrowGivenPrincipalParam} from "./structs/Param.sol";
import {UniswapV3SwapParam, UniswapV3CalculateSwapParam} from "./structs/SwapParam.sol";

import {NativeImmutableState, NativeWithdraws, NativePayments} from "./base/Native.sol";
import {UniswapImmutableState, UniswapV3CallbackWithOptionalNative} from "./base/UniswapV3SwapCallback.sol";
import {Multicall} from "./base/Multicall.sol";
import {Math} from "@timeswap-labs/v2-library/contracts/Math.sol";

/// @title Capable of borrowing a given amount of principal from a Timeswap V2 pool
/// @author Timeswap Labs
contract TimeswapV2PeripheryUniswapV3BorrowGivenPrincipal is
  ITimeswapV2PeripheryUniswapV3BorrowGivenPrincipal,
  TimeswapV2PeripheryBorrowGivenPrincipal,
  NativeImmutableState,
  NativeWithdraws,
  UniswapV3CallbackWithOptionalNative,
  Multicall
{
  using UniswapV3PoolLibrary for address;
  using Math for uint256;
  using SafeERC20 for IERC20;

  constructor(
    address chosenOptionFactory,
    address chosenPoolFactory,
    address chosenTokens,
    address chosenUniswapV3Factory,
    address chosenNative
  )
    TimeswapV2PeripheryBorrowGivenPrincipal(chosenOptionFactory, chosenPoolFactory, chosenTokens)
    NativeImmutableState(chosenNative)
    UniswapImmutableState(chosenUniswapV3Factory)
  {}

  /// @inheritdoc ITimeswapV2PeripheryUniswapV3BorrowGivenPrincipal
  function borrowGivenPrincipal(
    TimeswapV2PeripheryUniswapV3BorrowGivenPrincipalParam calldata param
  ) external payable override returns (uint256 positionAmount) {
    if (param.deadline < block.timestamp) Error.deadlineReached(param.deadline);

    {
      (, address poolPair) = PoolFactoryLibrary.getWithCheck(optionFactory, poolFactory, param.token0, param.token1);

      (uint256 token0Balance, uint256 token1Balance) = ITimeswapV2Pool(poolPair).totalLongBalanceAdjustFees(
        param.strike,
        param.maturity
      );

      address pool = UniswapV3FactoryLibrary.getWithCheck(
        uniswapV3Factory,
        param.token0,
        param.token1,
        param.uniswapV3Fee
      );

      bool exactInput;
      bool removeStrikeLimit;
      uint256 tokenAmountIn;
      uint256 tokenAmountOut;
      bytes memory data = abi.encode(param.token0, param.token1, param.uniswapV3Fee);
      data = abi.encode(false, data);
      if ((param.isToken0 ? token1Balance : token0Balance) != 0) {
        (tokenAmountIn, tokenAmountOut) = pool.calculateSwap(
          UniswapV3CalculateSwapParam({
            zeroForOne: !param.isToken0,
            exactInput: false,
            amount: param.tokenAmount,
            strikeLimit: param.strike,
            data: data
          })
        );

        if (tokenAmountIn > (param.isToken0 ? token1Balance : token0Balance))
          (tokenAmountIn, tokenAmountOut) = pool.calculateSwap(
            UniswapV3CalculateSwapParam({
              zeroForOne: !param.isToken0,
              exactInput: (exactInput = true),
              amount: param.isToken0 ? token1Balance : token0Balance,
              strikeLimit: param.strike,
              data: data
            })
          );
      }

      if (param.tokenAmount - tokenAmountOut > (param.isToken0 ? token0Balance : token1Balance)) {
        removeStrikeLimit = true;

        UniswapV3CalculateSwapParam memory internalParam = UniswapV3CalculateSwapParam({
          zeroForOne: !param.isToken0,
          exactInput: (exactInput = false),
          amount: param.tokenAmount - (param.isToken0 ? token0Balance : token1Balance),
          strikeLimit: 0,
          data: data
        });

        (tokenAmountIn, tokenAmountOut) = pool.calculateSwap(internalParam);
      }

      data = abi.encode(
        msg.sender,
        param.uniswapV3Fee,
        param.tokenTo,
        param.isToken0,
        exactInput,
        removeStrikeLimit,
        tokenAmountOut
      );

      (positionAmount, ) = borrowGivenPrincipal(
        TimeswapV2PeripheryBorrowGivenPrincipalParam({
          token0: param.token0,
          token1: param.token1,
          strike: param.strike,
          maturity: param.maturity,
          tokenTo: param.isToken0 == param.isLong0 ? address(this) : param.tokenTo,
          longTo: param.longTo,
          isLong0: param.isLong0,
          token0Amount: param.isToken0 ? param.tokenAmount - tokenAmountOut : tokenAmountIn,
          token1Amount: param.isToken0 ? tokenAmountIn : param.tokenAmount - tokenAmountOut,
          data: data
        })
      );
    }

    if (positionAmount > param.maxPositionAmount) revert MaxPositionReached(positionAmount, param.maxPositionAmount);

    emit BorrowGivenPrincipal(
      param.token0,
      param.token1,
      param.strike,
      param.maturity,
      param.uniswapV3Fee,
      msg.sender,
      param.tokenTo,
      param.longTo,
      param.isToken0,
      param.isLong0,
      param.tokenAmount,
      positionAmount
    );
  }

  function timeswapV2PeripheryBorrowGivenPrincipalInternal(
    TimeswapV2PeripheryBorrowGivenPrincipalInternalParam memory param
  ) internal override returns (bytes memory data) {
    (
      address msgSender,
      uint24 uniswapV3Fee,
      address tokenTo,
      bool isToken0,
      bool exactInput,
      bool removeStrikeLimit,
      uint256 tokenAmountOut
    ) = abi.decode(param.data, (address, uint24, address, bool, bool, bool, uint256));

    if ((exactInput ? (isToken0 ? param.token1Amount : param.token0Amount) : tokenAmountOut) != 0) {
      address pool = UniswapV3FactoryLibrary.get(uniswapV3Factory, param.token0, param.token1, uniswapV3Fee);

      data = abi.encode(
        isToken0 == param.isLong0 ? address(this) : msgSender,
        param.token0,
        param.token1,
        uniswapV3Fee
      );
      data = abi.encode(true, data);

      (, tokenAmountOut) = pool.swap(
        UniswapV3SwapParam({
          recipient: isToken0 == param.isLong0 ? address(this) : tokenTo,
          zeroForOne: !isToken0,
          exactInput: exactInput,
          amount: exactInput ? (isToken0 ? param.token1Amount : param.token0Amount) : tokenAmountOut,
          strikeLimit: removeStrikeLimit ? 0 : param.strike,
          data: data
        })
      );

      data = bytes("");
    }

    if (isToken0 == param.isLong0) {
      if ((param.isLong0 ? param.token0Amount : param.token1Amount) > tokenAmountOut)
        pay(
          param.isLong0 ? param.token0 : param.token1,
          msgSender,
          address(this),
          (param.isLong0 ? param.token0Amount : param.token1Amount).unsafeSub(tokenAmountOut)
        );
      else if ((param.isLong0 ? param.token0Amount : param.token1Amount) < tokenAmountOut)
        IERC20(param.isLong0 ? param.token0 : param.token1).safeTransfer(
          tokenTo,
          tokenAmountOut.unsafeSub(param.isLong0 ? param.token0Amount : param.token1Amount)
        );

      IERC20(param.isLong0 ? param.token0 : param.token1).safeTransfer(
        param.optionPair,
        param.isLong0 ? param.token0Amount : param.token1Amount
      );
    } else
      pay(
        param.isLong0 ? param.token0 : param.token1,
        msgSender,
        param.optionPair,
        param.isLong0 ? param.token0Amount : param.token1Amount
      );
  }
}

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

pragma solidity ^0.8.0;

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

/**
 * @dev Required interface of an ERC1155 compliant contract, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1155[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155 is IERC165 {
    /**
     * @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`.
     */
    event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);

    /**
     * @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
     * transfers.
     */
    event TransferBatch(
        address indexed operator,
        address indexed from,
        address indexed to,
        uint256[] ids,
        uint256[] values
    );

    /**
     * @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
     * `approved`.
     */
    event ApprovalForAll(address indexed account, address indexed operator, bool approved);

    /**
     * @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
     *
     * If an {URI} event was emitted for `id`, the standard
     * https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
     * returned by {IERC1155MetadataURI-uri}.
     */
    event URI(string value, uint256 indexed id);

    /**
     * @dev Returns the amount of tokens of token type `id` owned by `account`.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id) external view returns (uint256);

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
        external
        view
        returns (uint256[] memory);

    /**
     * @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
     *
     * Emits an {ApprovalForAll} event.
     *
     * Requirements:
     *
     * - `operator` cannot be the caller.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
     *
     * See {setApprovalForAll}.
     */
    function isApprovedForAll(address account, address operator) external view returns (bool);

    /**
     * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If the caller is not `from`, it must have been approved to spend ``from``'s tokens via {setApprovalForAll}.
     * - `from` must have a balance of tokens of type `id` of at least `amount`.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes calldata data
    ) external;

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] calldata ids,
        uint256[] calldata amounts,
        bytes calldata data
    ) external;
}

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

pragma solidity ^0.8.0;

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

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

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

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

pragma solidity ^0.8.0;

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

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

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

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

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

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

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

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

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

pragma solidity ^0.8.0;

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

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

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

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

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

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

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

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

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

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

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

pragma solidity ^0.8.1;

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

        return account.code.length > 0;
    }

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

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

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

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

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

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

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

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

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

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

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

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

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

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

// SPDX-License-Identifier: 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);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/*
 * @title Solidity Bytes Arrays Utils
 * @author Gonçalo Sá <goncalo.sa@consensys.net>
 *
 * @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
 *      The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
 */
library BytesLib {
  /// @dev Slices the given byte stream
  /// @param _bytes the bytes stream
  /// @param _start the start position
  /// @param _length the length required

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

    bytes memory tempBytes;

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

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

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

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

        mstore(tempBytes, _length)

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

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

    return tempBytes;
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

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

/// @title Library for catchError
/// @author Timeswap Labs
library CatchError {
  /// @dev Get the data passed from a given custom error.
  /// @dev It checks that the first four bytes of the reason has the same selector.
  /// @notice Will simply revert with the original error if the first four bytes is not the given selector.
  /// @param reason The data being inquired upon.
  /// @param selector The given conditional selector.
  function catchError(bytes memory reason, bytes4 selector) internal pure returns (bytes memory) {
    uint256 length = reason.length;

    if ((length - 4) % 32 == 0 && bytes4(reason) == selector) return BytesLib.slice(reason, 4, length - 4);

    assembly {
      revert(add(32, reason), mload(reason))
    }
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title Library for errors
/// @author Timeswap Labs
/// @dev Common error messages
library Error {
  /// @dev Reverts when input is zero.
  error ZeroInput();

  /// @dev Reverts when output is zero.
  error ZeroOutput();

  /// @dev Reverts when a value cannot be zero.
  error CannotBeZero();

  /// @dev Reverts when a pool already have liquidity.
  /// @param liquidity The liquidity amount that already existed in the pool.
  error AlreadyHaveLiquidity(uint160 liquidity);

  /// @dev Reverts when a pool requires liquidity.
  error RequireLiquidity();

  /// @dev Reverts when a given address is the zero address.
  error ZeroAddress();

  /// @dev Reverts when the maturity given is not withing uint96.
  /// @param maturity The maturity being inquired.
  error IncorrectMaturity(uint256 maturity);

  /// @dev Reverts when an option of given strike and maturity is still inactive.
  /// @param strike The chosen strike.
  /// @param maturity The chosen maturity.
  error InactiveOption(uint256 strike, uint256 maturity);

  /// @dev Reverts when a pool of given strike and maturity is still inactive.
  /// @param strike The chosen strike.
  /// @param maturity The chosen maturity.
  error InactivePool(uint256 strike, uint256 maturity);

  /// @dev Reverts when a liquidity token is inactive.
  error InactiveLiquidityTokenChoice();

  /// @dev Reverts when the square root interest rate is zero.
  /// @param strike The chosen strike.
  /// @param maturity The chosen maturity.
  error ZeroSqrtInterestRate(uint256 strike, uint256 maturity);

  /// @dev Reverts when the maturity is already matured.
  /// @param maturity The maturity.
  /// @param blockTimestamp The current block timestamp.
  error AlreadyMatured(uint256 maturity, uint96 blockTimestamp);

  /// @dev Reverts when the maturity is still active.
  /// @param maturity The maturity.
  /// @param blockTimestamp The current block timestamp.
  error StillActive(uint256 maturity, uint96 blockTimestamp);

  /// @dev Token amount not received.
  /// @param minuend The amount being subtracted.
  /// @param subtrahend The amount subtracting.
  error NotEnoughReceived(uint256 minuend, uint256 subtrahend);

  /// @dev The deadline of a transaction has been reached.
  /// @param deadline The deadline set.
  error DeadlineReached(uint256 deadline);

  /// @dev Reverts when input is zero.
  function zeroInput() internal pure {
    revert ZeroInput();
  }

  /// @dev Reverts when output is zero.
  function zeroOutput() internal pure {
    revert ZeroOutput();
  }

  /// @dev Reverts when a value cannot be zero.
  function cannotBeZero() internal pure {
    revert CannotBeZero();
  }

  /// @dev Reverts when a pool already have liquidity.
  /// @param liquidity The liquidity amount that already existed in the pool.
  function alreadyHaveLiquidity(uint160 liquidity) internal pure {
    revert AlreadyHaveLiquidity(liquidity);
  }

  /// @dev Reverts when a pool requires liquidity.
  function requireLiquidity() internal pure {
    revert RequireLiquidity();
  }

  /// @dev Reverts when a given address is the zero address.
  function zeroAddress() internal pure {
    revert ZeroAddress();
  }

  /// @dev Reverts when the maturity given is not withing uint96.
  /// @param maturity The maturity being inquired.
  function incorrectMaturity(uint256 maturity) internal pure {
    revert IncorrectMaturity(maturity);
  }

  /// @dev Reverts when the maturity is already matured.
  /// @param maturity The maturity.
  /// @param blockTimestamp The current block timestamp.
  function alreadyMatured(uint256 maturity, uint96 blockTimestamp) internal pure {
    revert AlreadyMatured(maturity, blockTimestamp);
  }

  /// @dev Reverts when the maturity is still active.
  /// @param maturity The maturity.
  /// @param blockTimestamp The current block timestamp.
  function stillActive(uint256 maturity, uint96 blockTimestamp) internal pure {
    revert StillActive(maturity, blockTimestamp);
  }

  /// @dev The deadline of a transaction has been reached.
  /// @param deadline The deadline set.
  function deadlineReached(uint256 deadline) internal pure {
    revert DeadlineReached(deadline);
  }

  /// @dev Reverts when an option of given strike and maturity is still inactive.
  /// @param strike The chosen strike.
  function inactiveOptionChoice(uint256 strike, uint256 maturity) internal pure {
    revert InactiveOption(strike, maturity);
  }

  /// @dev Reverts when a pool of given strike and maturity is still inactive.
  /// @param strike The chosen strike.
  /// @param maturity The chosen maturity.
  function inactivePoolChoice(uint256 strike, uint256 maturity) internal pure {
    revert InactivePool(strike, maturity);
  }

  /// @dev Reverts when the square root interest rate is zero.
  /// @param strike The chosen strike.
  /// @param maturity The chosen maturity.
  function zeroSqrtInterestRate(uint256 strike, uint256 maturity) internal pure {
    revert ZeroSqrtInterestRate(strike, maturity);
  }

  /// @dev Reverts when a liquidity token is inactive.
  function inactiveLiquidityTokenChoice() internal pure {
    revert InactiveLiquidityTokenChoice();
  }

  /// @dev Reverts when token amount not received.
  /// @param balance The balance amount being subtracted.
  /// @param balanceTarget The amount target.
  function checkEnough(uint256 balance, uint256 balanceTarget) internal pure {
    if (balance < balanceTarget) revert NotEnoughReceived(balance, balanceTarget);
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

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

/// @title Library for math utils for uint512
/// @author Timeswap Labs
library FullMath {
  using Math for uint256;

  /// @dev Reverts when modulo by zero.
  error ModuloByZero();

  /// @dev Reverts when add512 overflows over uint512.
  /// @param addendA0 The least significant part of first addend.
  /// @param addendA1 The most significant part of first addend.
  /// @param addendB0 The least significant part of second addend.
  /// @param addendB1 The most significant part of second addend.
  error AddOverflow(uint256 addendA0, uint256 addendA1, uint256 addendB0, uint256 addendB1);

  /// @dev Reverts when sub512 underflows.
  /// @param minuend0 The least significant part of minuend.
  /// @param minuend1 The most significant part of minuend.
  /// @param subtrahend0 The least significant part of subtrahend.
  /// @param subtrahend1 The most significant part of subtrahend.
  error SubUnderflow(uint256 minuend0, uint256 minuend1, uint256 subtrahend0, uint256 subtrahend1);

  /// @dev Reverts when div512To256 overflows over uint256.
  /// @param dividend0 The least significant part of dividend.
  /// @param dividend1 The most significant part of dividend.
  /// @param divisor The divisor.
  error DivOverflow(uint256 dividend0, uint256 dividend1, uint256 divisor);

  /// @dev Reverts when mulDiv overflows over uint256.
  /// @param multiplicand The multiplicand.
  /// @param multiplier The multiplier.
  /// @param divisor The divisor.
  error MulDivOverflow(uint256 multiplicand, uint256 multiplier, uint256 divisor);

  /// @dev Calculates the sum of two uint512 numbers.
  /// @notice Reverts on overflow over uint512.
  /// @param addendA0 The least significant part of addendA.
  /// @param addendA1 The most significant part of addendA.
  /// @param addendB0 The least significant part of addendB.
  /// @param addendB1 The most significant part of addendB.
  /// @return sum0 The least significant part of sum.
  /// @return sum1 The most significant part of sum.
  function add512(
    uint256 addendA0,
    uint256 addendA1,
    uint256 addendB0,
    uint256 addendB1
  ) internal pure returns (uint256 sum0, uint256 sum1) {
    uint256 carry;
    assembly {
      sum0 := add(addendA0, addendB0)
      carry := lt(sum0, addendA0)
      sum1 := add(add(addendA1, addendB1), carry)
    }

    if (carry == 0 ? addendA1 > sum1 : (sum1 == 0 || addendA1 > sum1 - 1))
      revert AddOverflow(addendA0, addendA1, addendB0, addendB1);
  }

  /// @dev Calculates the difference of two uint512 numbers.
  /// @notice Reverts on underflow.
  /// @param minuend0 The least significant part of minuend.
  /// @param minuend1 The most significant part of minuend.
  /// @param subtrahend0 The least significant part of subtrahend.
  /// @param subtrahend1 The most significant part of subtrahend.
  /// @return difference0 The least significant part of difference.
  /// @return difference1 The most significant part of difference.
  function sub512(
    uint256 minuend0,
    uint256 minuend1,
    uint256 subtrahend0,
    uint256 subtrahend1
  ) internal pure returns (uint256 difference0, uint256 difference1) {
    assembly {
      difference0 := sub(minuend0, subtrahend0)
      difference1 := sub(sub(minuend1, subtrahend1), lt(minuend0, subtrahend0))
    }

    if (subtrahend1 > minuend1 || (subtrahend1 == minuend1 && subtrahend0 > minuend0))
      revert SubUnderflow(minuend0, minuend1, subtrahend0, subtrahend1);
  }

  /// @dev Calculate the product of two uint256 numbers that may result to uint512 product.
  /// @notice Can never overflow.
  /// @param multiplicand The multiplicand.
  /// @param multiplier The multiplier.
  /// @return product0 The least significant part of product.
  /// @return product1 The most significant part of product.
  function mul512(uint256 multiplicand, uint256 multiplier) internal pure returns (uint256 product0, uint256 product1) {
    assembly {
      let mm := mulmod(multiplicand, multiplier, not(0))
      product0 := mul(multiplicand, multiplier)
      product1 := sub(sub(mm, product0), lt(mm, product0))
    }
  }

  /// @dev Divide 2 to 256 power by the divisor.
  /// @dev Rounds down the result.
  /// @notice Reverts when divide by zero.
  /// @param divisor The divisor.
  /// @return quotient The quotient.
  function div256(uint256 divisor) private pure returns (uint256 quotient) {
    if (divisor == 0) revert Math.DivideByZero();
    assembly {
      quotient := add(div(sub(0, divisor), divisor), 1)
    }
  }

  /// @dev Compute 2 to 256 power modulo the given value.
  /// @notice Reverts when modulo by zero.
  /// @param value The given value.
  /// @return result The result.
  function mod256(uint256 value) private pure returns (uint256 result) {
    if (value == 0) revert ModuloByZero();
    assembly {
      result := mod(sub(0, value), value)
    }
  }

  /// @dev Divide a uint512 number by uint256 number to return a uint512 number.
  /// @dev Rounds down the result.
  /// @param dividend0 The least significant part of dividend.
  /// @param dividend1 The most significant part of dividend.
  /// @param divisor The divisor.
  /// @param quotient0 The least significant part of quotient.
  /// @param quotient1 The most significant part of quotient.
  function div512(
    uint256 dividend0,
    uint256 dividend1,
    uint256 divisor
  ) private pure returns (uint256 quotient0, uint256 quotient1) {
    if (dividend1 == 0) quotient0 = dividend0.div(divisor, false);
    else {
      uint256 q = div256(divisor);
      uint256 r = mod256(divisor);
      while (dividend1 != 0) {
        (uint256 t0, uint256 t1) = mul512(dividend1, q);
        (quotient0, quotient1) = add512(quotient0, quotient1, t0, t1);
        (t0, t1) = mul512(dividend1, r);
        (dividend0, dividend1) = add512(t0, t1, dividend0, 0);
      }
      (quotient0, quotient1) = add512(quotient0, quotient1, dividend0.div(divisor, false), 0);
    }
  }

  /// @dev Divide a uint512 number by a uint256 number.
  /// @dev Reverts when result is greater than uint256.
  /// @notice Skips div512 if dividend1 is zero.
  /// @param dividend0 The least significant part of dividend.
  /// @param dividend1 The most significant part of dividend.
  /// @param divisor The divisor.
  /// @param roundUp Round up the result when true. Round down if false.
  /// @param quotient The quotient.
  function div512To256(
    uint256 dividend0,
    uint256 dividend1,
    uint256 divisor,
    bool roundUp
  ) internal pure returns (uint256 quotient) {
    uint256 quotient1;
    (quotient, quotient1) = div512(dividend0, dividend1, divisor);

    if (quotient1 != 0) revert DivOverflow(dividend0, dividend1, divisor);

    if (roundUp) {
      (uint256 productA0, uint256 productA1) = mul512(quotient, divisor);
      if (dividend1 > productA1 || dividend0 > productA0) quotient++;
    }
  }

  /// @dev Divide a uint512 number by a uint256 number.
  /// @notice Skips div512 if dividend1 is zero.
  /// @param dividend0 The least significant part of dividend.
  /// @param dividend1 The most significant part of dividend.
  /// @param divisor The divisor.
  /// @param roundUp Round up the result when true. Round down if false.
  /// @param quotient0 The least significant part of quotient.
  /// @param quotient1 The most significant part of quotient.
  function div512(
    uint256 dividend0,
    uint256 dividend1,
    uint256 divisor,
    bool roundUp
  ) internal pure returns (uint256 quotient0, uint256 quotient1) {
    (quotient0, quotient1) = div512(dividend0, dividend1, divisor);

    if (roundUp) {
      (uint256 productA0, uint256 productA1) = mul512(quotient0, divisor);
      productA1 += (quotient1 * divisor);
      if (dividend1 > productA1 || dividend0 > productA0) {
        if (quotient0 == type(uint256).max) {
          quotient0 = 0;
          quotient1++;
        } else quotient0++;
      }
    }
  }

  /// @dev Multiply two uint256 number then divide it by a uint256 number.
  /// @notice Skips mulDiv if product of multiplicand and multiplier is uint256 number.
  /// @dev Reverts when result is greater than uint256.
  /// @param multiplicand The multiplicand.
  /// @param multiplier The multiplier.
  /// @param divisor The divisor.
  /// @param roundUp Round up the result when true. Round down if false.
  /// @return result The result.
  function mulDiv(
    uint256 multiplicand,
    uint256 multiplier,
    uint256 divisor,
    bool roundUp
  ) internal pure returns (uint256 result) {
    (uint256 product0, uint256 product1) = mul512(multiplicand, multiplier);

    // Handle non-overflow cases, 256 by 256 division
    if (product1 == 0) return result = product0.div(divisor, roundUp);

    // Make sure the result is less than 2**256.
    // Also prevents divisor == 0
    if (divisor <= product1) revert MulDivOverflow(multiplicand, multiplier, divisor);

    unchecked {
      ///////////////////////////////////////////////
      // 512 by 256 division.
      ///////////////////////////////////////////////

      // Make division exact by subtracting the remainder from [product1 product0]
      // Compute remainder using mulmod
      uint256 remainder;
      assembly {
        remainder := mulmod(multiplicand, multiplier, divisor)
      }
      // Subtract 256 bit number from 512 bit number
      assembly {
        product1 := sub(product1, gt(remainder, product0))
        product0 := sub(product0, remainder)
      }

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

      // Divide [product1 product0] by the factors of two
      assembly {
        product0 := div(product0, twos)
      }
      // Shift in bits from product1 into product0. 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)
      }
      product0 |= product1 * twos;

      // Invert divisor mod 2**256
      // Now that divisor is an odd number, it has an inverse
      // modulo 2**256 such that divisor * inv = 1 mod 2**256.
      // Compute the inverse by starting with a seed that is correct
      // correct for four bits. That is, divisor * inv = 1 mod 2**4
      uint256 inv;
      inv = (3 * divisor) ^ 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 - divisor * inv; // inverse mod 2**8
      inv *= 2 - divisor * inv; // inverse mod 2**16
      inv *= 2 - divisor * inv; // inverse mod 2**32
      inv *= 2 - divisor * inv; // inverse mod 2**64
      inv *= 2 - divisor * inv; // inverse mod 2**128
      inv *= 2 - divisor * inv; // inverse mod 2**256

      // Because the division is now exact we can divide by multiplying
      // with the modular inverse of divisor. 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 product1
      // is no longer required.
      result = product0 * inv;
    }

    if (roundUp && mulmod(multiplicand, multiplier, divisor) != 0) result++;
  }

  /// @dev Get the square root of a uint512 number.
  /// @param value0 The least significant of the number.
  /// @param value1 The most significant of the number.
  /// @param roundUp Round up the result when true. Round down if false.
  /// @return result The result.
  function sqrt512(uint256 value0, uint256 value1, bool roundUp) internal pure returns (uint256 result) {
    if (value1 == 0) result = value0.sqrt(roundUp);
    else {
      uint256 estimate = sqrt512Estimate(value0, value1, type(uint256).max);
      result = type(uint256).max;
      while (estimate < result) {
        result = estimate;
        estimate = sqrt512Estimate(value0, value1, estimate);
      }

      if (roundUp) {
        (uint256 product0, uint256 product1) = mul512(result, result);
        if (value1 > product1 || value0 > product0) result++;
      }
    }
  }

  /// @dev An iterative process of getting sqrt512 following Newtonian method.
  /// @param value0 The least significant of the number.
  /// @param value1 The most significant of the number.
  /// @param currentEstimate The current estimate of the iteration.
  /// @param estimate The new estimate of the iteration.
  function sqrt512Estimate(
    uint256 value0,
    uint256 value1,
    uint256 currentEstimate
  ) private pure returns (uint256 estimate) {
    uint256 r0 = div512To256(value0, value1, currentEstimate, false);
    uint256 r1;
    (r0, r1) = add512(r0, 0, currentEstimate, 0);
    estimate = div512To256(r0, r1, 2, false);
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title Library for math related utils
/// @author Timeswap Labs
library Math {
  /// @dev Reverts when divide by zero.
  error DivideByZero();
  error Overflow();

  /// @dev Add two uint256.
  /// @notice May overflow.
  /// @param addend1 The first addend.
  /// @param addend2 The second addend.
  /// @return sum The sum.
  function unsafeAdd(uint256 addend1, uint256 addend2) internal pure returns (uint256 sum) {
    unchecked {
      sum = addend1 + addend2;
    }
  }

  /// @dev Subtract two uint256.
  /// @notice May underflow.
  /// @param minuend The minuend.
  /// @param subtrahend The subtrahend.
  /// @return difference The difference.
  function unsafeSub(uint256 minuend, uint256 subtrahend) internal pure returns (uint256 difference) {
    unchecked {
      difference = minuend - subtrahend;
    }
  }

  /// @dev Multiply two uint256.
  /// @notice May overflow.
  /// @param multiplicand The multiplicand.
  /// @param multiplier The multiplier.
  /// @return product The product.
  function unsafeMul(uint256 multiplicand, uint256 multiplier) internal pure returns (uint256 product) {
    unchecked {
      product = multiplicand * multiplier;
    }
  }

  /// @dev Divide two uint256.
  /// @notice Reverts when divide by zero.
  /// @param dividend The dividend.
  /// @param divisor The divisor.
  //// @param roundUp Round up the result when true. Round down if false.
  /// @return quotient The quotient.
  function div(uint256 dividend, uint256 divisor, bool roundUp) internal pure returns (uint256 quotient) {
    quotient = dividend / divisor;

    if (roundUp && dividend % divisor != 0) quotient++;
  }

  /// @dev Shift right a uint256 number.
  /// @param dividend The dividend.
  /// @param divisorBit The divisor in bits.
  /// @param roundUp True if ceiling the result. False if floor the result.
  /// @return quotient The quotient.
  function shr(uint256 dividend, uint8 divisorBit, bool roundUp) internal pure returns (uint256 quotient) {
    quotient = dividend >> divisorBit;

    if (roundUp && dividend % (1 << divisorBit) != 0) quotient++;
  }

  /// @dev Gets the square root of a value.
  /// @param value The value being square rooted.
  /// @param roundUp Round up the result when true. Round down if false.
  /// @return result The resulting value of the square root.
  function sqrt(uint256 value, bool roundUp) internal pure returns (uint256 result) {
    if (value == type(uint256).max) return result = type(uint128).max;
    if (value == 0) return 0;
    unchecked {
      uint256 estimate = (value + 1) >> 1;
      result = value;
      while (estimate < result) {
        result = estimate;
        estimate = (value / estimate + estimate) >> 1;
      }
    }

    if (roundUp && result * result < value) result++;
  }

  /// @dev Gets the min of two uint256 number.
  /// @param value1 The first value to be compared.
  /// @param value2 The second value to be compared.
  /// @return result The min result.
  function min(uint256 value1, uint256 value2) internal pure returns (uint256 result) {
    return value1 < value2 ? value1 : value2;
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title Library for safecasting
/// @author Timeswap Labs
library SafeCast {
  /// @dev Reverts when overflows over uint16.
  error Uint16Overflow();

  /// @dev Reverts when overflows over uint96.
  error Uint96Overflow();

  /// @dev Reverts when overflows over uint160.
  error Uint160Overflow();

  /// @dev Safely cast a uint256 number to uint16.
  /// @dev Reverts when number is greater than uint16.
  /// @param value The uint256 number to be safecasted.
  /// @param result The uint16 result.
  function toUint16(uint256 value) internal pure returns (uint16 result) {
    if (value > type(uint16).max) revert Uint16Overflow();
    result = uint16(value);
  }

  /// @dev Safely cast a uint256 number to uint96.
  /// @dev Reverts when number is greater than uint96.
  /// @param value The uint256 number to be safecasted.
  /// @param result The uint96 result.
  function toUint96(uint256 value) internal pure returns (uint96 result) {
    if (value > type(uint96).max) revert Uint96Overflow();
    result = uint96(value);
  }

  /// @dev Safely cast a uint256 number to uint160.
  /// @dev Reverts when number is greater than uint160.
  /// @param value The uint256 number to be safecasted.
  /// @param result The uint160 result.
  function toUint160(uint256 value) internal pure returns (uint160 result) {
    if (value > type(uint160).max) revert Uint160Overflow();
    result = uint160(value);
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

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

/// @title library for converting strike prices.
/// @dev When strike is greater than uint128, the base token is denominated as token0 (which is the smaller address token).
/// @dev When strike is uint128, the base token is denominated as token1 (which is the larger address).
library StrikeConversion {
  /// @dev When zeroToOne, converts a number in multiple of strike.
  /// @dev When oneToZero, converts a number in multiple of 1 / strike.
  /// @param amount The amount to be converted.
  /// @param strike The strike multiple conversion.
  /// @param zeroToOne ZeroToOne if it is true. OneToZero if it is false.
  /// @param roundUp Round up the result when true. Round down if false.
  function convert(uint256 amount, uint256 strike, bool zeroToOne, bool roundUp) internal pure returns (uint256) {
    return
      zeroToOne
        ? FullMath.mulDiv(amount, strike, uint256(1) << 128, roundUp)
        : FullMath.mulDiv(amount, uint256(1) << 128, strike, roundUp);
  }

  /// @dev When toOne, converts a base denomination to token1 denomination.
  /// @dev When toZero, converts a base denomination to token0 denomination.
  /// @param amount The amount ot be converted. Token0 amount when zeroToOne. Token1 amount when oneToZero.
  /// @param strike The strike multiple conversion.
  /// @param toOne ToOne if it is true, ToZero if it is false.
  /// @param roundUp Round up the result when true. Round down if false.
  function turn(uint256 amount, uint256 strike, bool toOne, bool roundUp) internal pure returns (uint256) {
    return
      strike > type(uint128).max
        ? (toOne ? convert(amount, strike, true, roundUp) : amount)
        : (toOne ? amount : convert(amount, strike, false, roundUp));
  }

  /// @dev Combine and add token0Amount and token1Amount into base token amount.
  /// @param amount0 The token0 amount to be combined.
  /// @param amount1 The token1 amount to be combined.
  /// @param strike The strike multiple conversion.
  /// @param roundUp Round up the result when true. Round down if false.
  function combine(uint256 amount0, uint256 amount1, uint256 strike, bool roundUp) internal pure returns (uint256) {
    return
      strike > type(uint128).max
        ? amount0 + convert(amount1, strike, false, roundUp)
        : amount1 + convert(amount0, strike, true, roundUp);
  }

  /// @dev When zeroToOne, given a larger base amount, and token0 amount, get the difference token1 amount.
  /// @dev When oneToZero, given a larger base amount, and toekn1 amount, get the difference token0 amount.
  /// @param base The larger base amount.
  /// @param amount The token0 amount when zeroToOne, the token1 amount when oneToZero.
  /// @param strike The strike multiple conversion.
  /// @param zeroToOne ZeroToOne if it is true. OneToZero if it is false.
  /// @param roundUp Round up the result when true. Round down if false.
  function dif(
    uint256 base,
    uint256 amount,
    uint256 strike,
    bool zeroToOne,
    bool roundUp
  ) internal pure returns (uint256) {
    return
      strike > type(uint128).max
        ? (zeroToOne ? convert(base - amount, strike, true, roundUp) : base - convert(amount, strike, false, !roundUp))
        : (zeroToOne ? base - convert(amount, strike, true, !roundUp) : convert(base - amount, strike, false, roundUp));
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @dev The three type of native token positions.
/// @dev Long0 is denominated as the underlying Token0.
/// @dev Long1 is denominated as the underlying Token1.
/// @dev When strike greater than uint128 then Short is denominated as Token0 (the base token denomination).
/// @dev When strike is uint128 then Short is denominated as Token1 (the base token denomination).
enum TimeswapV2OptionPosition {
  Long0,
  Long1,
  Short
}

/// @title library for position utils
/// @author Timeswap Labs
/// @dev Helper functions for the TimeswapOptionPosition enum.
library PositionLibrary {
  /// @dev Reverts when the given type of position is invalid.
  error InvalidPosition();

  /// @dev Checks that the position input is correct.
  /// @param position The position input.
  function check(TimeswapV2OptionPosition position) internal pure {
    if (uint256(position) >= 3) revert InvalidPosition();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @dev The different input for the mint transaction.
enum TimeswapV2OptionMint {
  GivenTokensAndLongs,
  GivenShorts
}

/// @dev The different input for the burn transaction.
enum TimeswapV2OptionBurn {
  GivenTokensAndLongs,
  GivenShorts
}

/// @dev The different input for the swap transaction.
enum TimeswapV2OptionSwap {
  GivenToken0AndLong0,
  GivenToken1AndLong1
}

/// @dev The different input for the collect transaction.
enum TimeswapV2OptionCollect {
  GivenShort,
  GivenToken0,
  GivenToken1
}

/// @title library for transaction checks
/// @author Timeswap Labs
/// @dev Helper functions for the all enums in this module.
library TransactionLibrary {
  /// @dev Reverts when the given type of transaction is invalid.
  error InvalidTransaction();

  /// @dev checks that the given input is correct.
  /// @param transaction the mint transaction input.
  function check(TimeswapV2OptionMint transaction) internal pure {
    if (uint256(transaction) >= 2) revert InvalidTransaction();
  }

  /// @dev checks that the given input is correct.
  /// @param transaction the burn transaction input.
  function check(TimeswapV2OptionBurn transaction) internal pure {
    if (uint256(transaction) >= 2) revert InvalidTransaction();
  }

  /// @dev checks that the given input is correct.
  /// @param transaction the swap transaction input.
  function check(TimeswapV2OptionSwap transaction) internal pure {
    if (uint256(transaction) >= 2) revert InvalidTransaction();
  }

  /// @dev checks that the given input is correct.
  /// @param transaction the collect transaction input.
  function check(TimeswapV2OptionCollect transaction) internal pure {
    if (uint256(transaction) >= 3) revert InvalidTransaction();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {TimeswapV2OptionMintCallbackParam} from "../../structs/CallbackParam.sol";

/// @title Callback for ITimeswapV2Option#mint
/// @notice Any contract that calls ITimeswapV2Option#mint must implement this interface.
interface ITimeswapV2OptionMintCallback {
  /// @notice Called to `msg.sender` after initiating a mint from ITimeswapV2Option#mint.
  /// @dev In the implementation, you must transfer token0 and token1 for the mint transaction.
  /// The caller of this method must be checked to be a Timeswap V2 Option pair deployed by the canonical Timeswap V2 Factory.
  /// @dev The long0 positions, long1 positions, and/or short positions will already minted to the recipients.
  /// @param param The parameter of the callback.
  /// @return data The bytes code returned from the callback.
  function timeswapV2OptionMintCallback(
    TimeswapV2OptionMintCallbackParam calldata param
  ) external returns (bytes memory data);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {TimeswapV2OptionSwapCallbackParam} from "../../structs/CallbackParam.sol";

/// @title Callback for ITimeswapV2Option#swap
/// @notice Any contract that calls ITimeswapV2Option#swap must implement this interface.
interface ITimeswapV2OptionSwapCallback {
  /// @notice Called to `msg.sender` after initiating a swap from ITimeswapV2Option#swap.
  /// @dev In the implementation, you must transfer token0 for the swap transaction.
  /// The caller of this method must be checked to be a Timeswap V2 Option pair deployed by the canonical Timeswap V2 Factory.
  /// @dev The long0 positions or long1 positions will already minted to the recipients.
  /// @param param The param of the swap callback.
  /// @return data The bytes code returned from the callback.
  function timeswapV2OptionSwapCallback(
    TimeswapV2OptionSwapCallbackParam calldata param
  ) external returns (bytes memory data);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {TimeswapV2OptionPosition} from "../enums/Position.sol";
import {TimeswapV2OptionMintParam, TimeswapV2OptionBurnParam, TimeswapV2OptionSwapParam, TimeswapV2OptionCollectParam} from "../structs/Param.sol";
import {StrikeAndMaturity} from "../structs/StrikeAndMaturity.sol";

/// @title An interface for a contract that deploys Timeswap V2 Option pair contracts
/// @notice A Timeswap V2 Option pair facilitates option mechanics between any two assets that strictly conform
/// to the ERC20 specification.
interface ITimeswapV2Option {
  /* ===== EVENT ===== */

  /// @dev Emits when a position is transferred.
  /// @param strike The strike ratio of token1 per token0 of the position.
  /// @param maturity The maturity of the position.
  /// @param from The address of the caller of the transferPosition function.
  /// @param to The address of the recipient of the position.
  /// @param position The type of position transferred. More information in the Position module.
  /// @param amount The amount of balance transferred.
  event TransferPosition(
    uint256 indexed strike,
    uint256 indexed maturity,
    address from,
    address to,
    TimeswapV2OptionPosition position,
    uint256 amount
  );

  /// @dev Emits when a mint transaction is called.
  /// @param strike The strike ratio of token1 per token0 of the option.
  /// @param maturity The maturity of the option.
  /// @param caller The address of the caller of the mint function.
  /// @param long0To The address of the recipient of long token0 position.
  /// @param long1To The address of the recipient of long token1 position.
  /// @param shortTo The address of the recipient of short position.
  /// @param token0AndLong0Amount The amount of token0 deposited and long0 minted.
  /// @param token1AndLong1Amount The amount of token1 deposited and long1 minted.
  /// @param shortAmount The amount of short minted.
  event Mint(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address long0To,
    address long1To,
    address shortTo,
    uint256 token0AndLong0Amount,
    uint256 token1AndLong1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when a burn transaction is called.
  /// @param strike The strike ratio of token1 per token0 of the option.
  /// @param maturity The maturity of the option.
  /// @param caller The address of the caller of the mint function.
  /// @param token0To The address of the recipient of token0.
  /// @param token1To The address of the recipient of token1.
  /// @param token0AndLong0Amount The amount of token0 withdrawn and long0 burnt.
  /// @param token1AndLong1Amount The amount of token1 withdrawn and long1 burnt.
  /// @param shortAmount The amount of short burnt.
  event Burn(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address token0To,
    address token1To,
    uint256 token0AndLong0Amount,
    uint256 token1AndLong1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when a swap transaction is called.
  /// @param strike The strike ratio of token1 per token0 of the option.
  /// @param maturity The maturity of the option.
  /// @param caller The address of the caller of the mint function.
  /// @param tokenTo The address of the recipient of token0 or token1.
  /// @param longTo The address of the recipient of long token0 or long token1.
  /// @param isLong0toLong1 The direction of the swap. More information in the Transaction module.
  /// @param token0AndLong0Amount If the direction is from long0 to long1, the amount of token0 withdrawn and long0 burnt.
  /// If the direction is from long1 to long0, the amount of token0 deposited and long0 minted.
  /// @param token1AndLong1Amount If the direction is from long0 to long1, the amount of token1 deposited and long1 minted.
  /// If the direction is from long1 to long0, the amount of token1 withdrawn and long1 burnt.
  event Swap(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address tokenTo,
    address longTo,
    bool isLong0toLong1,
    uint256 token0AndLong0Amount,
    uint256 token1AndLong1Amount
  );

  /// @dev Emits when a collect transaction is called.
  /// @param strike The strike ratio of token1 per token0 of the option.
  /// @param maturity The maturity of the option.
  /// @param caller The address of the caller of the mint function.
  /// @param token0To The address of the recipient of token0.
  /// @param token1To The address of the recipient of token1.
  /// @param long0AndToken0Amount The amount of token0 withdrawn.
  /// @param long1Amount The sum of long0AndToken0Amount and this amount is the total short amount burnt.
  /// @param token1Amount The amount of token1 withdrawn.
  event Collect(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address token0To,
    address token1To,
    uint256 long0AndToken0Amount,
    uint256 long1Amount,
    uint256 token1Amount
  );

  /* ===== VIEW ===== */

  /// @dev Returns the factory address that deployed this contract.
  function optionFactory() external view returns (address);

  /// @dev Returns the first ERC20 token address of the pair.
  function token0() external view returns (address);

  /// @dev Returns the second ERC20 token address of the pair.
  function token1() external view returns (address);

  /// @dev Get the strike and maturity of the option in the option enumeration list.
  /// @param id The chosen index.
  function getByIndex(uint256 id) external view returns (StrikeAndMaturity memory);

  /// @dev Number of options being interacted.
  function numberOfOptions() external view returns (uint256);

  /// @dev Returns the total position of the option.
  /// @param strike The strike ratio of token1 per token0 of the position.
  /// @param maturity The maturity of the position.
  /// @param position The type of position inquired. More information in the Position module.
  /// @return balance The total position.
  function totalPosition(
    uint256 strike,
    uint256 maturity,
    TimeswapV2OptionPosition position
  ) external view returns (uint256 balance);

  /// @dev Returns the position of an owner of the option.
  /// @param strike The strike ratio of token1 per token0 of the position.
  /// @param maturity The maturity of the position.
  /// @param owner The address of the owner of the position.
  /// @param position The type of position inquired. More information in the Position module.
  /// @return balance The user position.
  function positionOf(
    uint256 strike,
    uint256 maturity,
    address owner,
    TimeswapV2OptionPosition position
  ) external view returns (uint256 balance);

  /* ===== UPDATE ===== */

  /// @dev Transfer position to another address.
  /// @param strike The strike ratio of token1 per token0 of the position.
  /// @param maturity The maturity of the position.
  /// @param to The address of the recipient of the position.
  /// @param position The type of position transferred. More information in the Position module.
  /// @param amount The amount of balance transferred.
  function transferPosition(
    uint256 strike,
    uint256 maturity,
    address to,
    TimeswapV2OptionPosition position,
    uint256 amount
  ) external;

  /// @dev Mint position.
  /// Mint long token0 position when token0 is deposited.
  /// Mint long token1 position when token1 is deposited.
  /// @dev Can only be called before the maturity of the pool.
  /// @param param The parameters for the mint function.
  /// @return token0AndLong0Amount The amount of token0 deposited and long0 minted.
  /// @return token1AndLong1Amount The amount of token1 deposited and long1 minted.
  /// @return shortAmount The amount of short minted.
  /// @return data The additional data return.
  function mint(
    TimeswapV2OptionMintParam calldata param
  )
    external
    returns (uint256 token0AndLong0Amount, uint256 token1AndLong1Amount, uint256 shortAmount, bytes memory data);

  /// @dev Burn short position.
  /// Withdraw token0, when long token0 is burnt.
  /// Withdraw token1, when long token1 is burnt.
  /// @dev Can only be called before the maturity of the pool.
  /// @param param The parameters for the burn function.
  /// @return token0AndLong0Amount The amount of token0 withdrawn and long0 burnt.
  /// @return token1AndLong1Amount The amount of token1 withdrawn and long1 burnt.
  /// @return shortAmount The amount of short burnt.
  function burn(
    TimeswapV2OptionBurnParam calldata param
  )
    external
    returns (uint256 token0AndLong0Amount, uint256 token1AndLong1Amount, uint256 shortAmount, bytes memory data);

  /// @dev If the direction is from long token0 to long token1, burn long token0 and mint equivalent long token1,
  /// also deposit token1 and withdraw token0.
  /// If the direction is from long token1 to long token0, burn long token1 and mint equivalent long token0,
  /// also deposit token0 and withdraw token1.
  /// @dev Can only be called before the maturity of the pool.
  /// @param param The parameters for the swap function.
  /// @return token0AndLong0Amount If direction is Long0ToLong1, the amount of token0 withdrawn and long0 burnt.
  /// If direction is Long1ToLong0, the amount of token0 deposited and long0 minted.
  /// @return token1AndLong1Amount If direction is Long0ToLong1, the amount of token1 deposited and long1 minted.
  /// If direction is Long1ToLong0, the amount of token1 withdrawn and long1 burnt.
  /// @return data The additional data return.
  function swap(
    TimeswapV2OptionSwapParam calldata param
  ) external returns (uint256 token0AndLong0Amount, uint256 token1AndLong1Amount, bytes memory data);

  /// @dev Burn short position, withdraw token0 and token1.
  /// @dev Can only be called after the maturity of the pool.
  /// @param param The parameters for the collect function.
  /// @return token0Amount The amount of token0 withdrawn.
  /// @return token1Amount The amount of token1 withdrawn.
  /// @return shortAmount The amount of short burnt.
  function collect(
    TimeswapV2OptionCollectParam calldata param
  ) external returns (uint256 token0Amount, uint256 token1Amount, uint256 shortAmount, bytes memory data);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title The interface for the contract that deploys Timeswap V2 Option pair contracts
/// @notice The Timeswap V2 Option Factory facilitates creation of Timeswap V2 Options pair.
interface ITimeswapV2OptionFactory {
  /* ===== EVENT ===== */

  /// @dev Emits when a new Timeswap V2 Option contract is created.
  /// @param caller The address of the caller of create function.
  /// @param token0 The first ERC20 token address of the pair.
  /// @param token1 The second ERC20 token address of the pair.
  /// @param optionPair The address of the Timeswap V2 Option contract created.
  event Create(address indexed caller, address indexed token0, address indexed token1, address optionPair);

  /* ===== VIEW ===== */

  /// @dev Returns the address of a Timeswap V2 Option.
  /// @dev Returns a zero address if the Timeswap V2 Option does not exist.
  /// @notice The token0 address must be smaller than token1 address.
  /// @param token0 The first ERC20 token address of the pair.
  /// @param token1 The second ERC20 token address of the pair.
  /// @return optionPair The address of the Timeswap V2 Option contract or a zero address.
  function get(address token0, address token1) external view returns (address optionPair);

  /// @dev Get the address of the option pair in the option pair enumeration list.
  /// @param id The chosen index.
  function getByIndex(uint256 id) external view returns (address optionPair);

  /// @dev The number of option pairs deployed.
  function numberOfPairs() external view returns (uint256);

  /* ===== UPDATE ===== */

  /// @dev Creates a Timeswap V2 Option based on pair parameters.
  /// @dev Cannot create a duplicate Timeswap V2 Option with the same pair parameters.
  /// @notice The token0 address must be smaller than token1 address.
  /// @param token0 The first ERC20 token address of the pair.
  /// @param token1 The second ERC20 token address of the pair.
  /// @param optionPair The address of the Timeswap V2 Option contract created.
  function create(address token0, address token1) external returns (address optionPair);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {Error} from "@timeswap-labs/v2-library/contracts/Error.sol";

import {OptionPairLibrary} from "./OptionPair.sol";

import {ITimeswapV2OptionFactory} from "../interfaces/ITimeswapV2OptionFactory.sol";

/// @title library for option utils
/// @author Timeswap Labs
library OptionFactoryLibrary {
  using OptionPairLibrary for address;

  /// @dev reverts if the factory is the zero address.
  error ZeroFactoryAddress();

  /// @dev check if the factory address is not zero.
  /// @param optionFactory The factory address.
  function checkNotZeroFactory(address optionFactory) internal pure {
    if (optionFactory == address(0)) revert ZeroFactoryAddress();
  }

  /// @dev Helper function to get the option pair address.
  /// @param optionFactory The address of the option factory.
  /// @param token0 The smaller ERC20 address of the pair.
  /// @param token1 The larger ERC20 address of the pair.
  /// @return optionPair The result option pair address.
  function get(address optionFactory, address token0, address token1) internal view returns (address optionPair) {
    optionPair = ITimeswapV2OptionFactory(optionFactory).get(token0, token1);
  }

  /// @dev Helper function to get the option pair address.
  /// @notice reverts when the option pair does not exist.
  /// @param optionFactory The address of the option factory.
  /// @param token0 The smaller ERC20 address of the pair.
  /// @param token1 The larger ERC20 address of the pair.
  /// @return optionPair The result option pair address.
  function getWithCheck(
    address optionFactory,
    address token0,
    address token1
  ) internal view returns (address optionPair) {
    optionPair = get(optionFactory, token0, token1);
    if (optionPair == address(0)) Error.zeroAddress();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title library for optionPair utils
/// @author Timeswap Labs
library OptionPairLibrary {
  /// @dev Reverts when option address is zero.
  error ZeroOptionAddress();

  /// @dev Reverts when the pair has incorrect format.
  /// @param token0 The first ERC20 token address of the pair.
  /// @param token1 The second ERC20 token address of the pair.
  error InvalidOptionPair(address token0, address token1);

  /// @dev Reverts when the Timeswap V2 Option already exist.
  /// @param token0 The first ERC20 token address of the pair.
  /// @param token1 The second ERC20 token address of the pair.
  /// @param optionPair The address of the existed Pair contract.
  error OptionPairAlreadyExisted(address token0, address token1, address optionPair);

  /// @dev Checks if option address is not zero.
  /// @param optionPair The option pair address being inquired.
  function checkNotZeroAddress(address optionPair) internal pure {
    if (optionPair == address(0)) revert ZeroOptionAddress();
  }

  /// @dev Check if the pair tokens is in correct format.
  /// @notice Reverts if token0 is greater than or equal token1.
  /// @param token0 The first ERC20 token address of the pair.
  /// @param token1 The second ERC20 token address of the pair.
  function checkCorrectFormat(address token0, address token1) internal pure {
    if (token0 >= token1) revert InvalidOptionPair(token0, token1);
  }

  /// @dev Check if the pair already existed.
  /// @notice Reverts if the pair is not a zero address.
  /// @param token0 The first ERC20 token address of the pair.
  /// @param token1 The second ERC20 token address of the pair.
  /// @param optionPair The address of the existed Pair contract.
  function checkDoesNotExist(address token0, address token1, address optionPair) internal pure {
    if (optionPair != address(0)) revert OptionPairAlreadyExisted(token0, token1, optionPair);
  }
}

File 23 of 69 : CallbackParam.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @dev Parameter for the mint callback.
/// @param strike The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param token0AndLong0Amount The token0 amount to be deposited and the long0 amount minted.
/// @param token1AndLong1Amount The token1 amount to be deposited and the long1 amount minted.
/// @param shortAmount The short amount minted.
/// @param data The bytes code data sent to the callback.
struct TimeswapV2OptionMintCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 token0AndLong0Amount;
  uint256 token1AndLong1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @dev Parameter for the burn callback.
/// @param strike The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param token0AndLong0Amount The token0 amount to be withdrawn and the long0 amount burnt.
/// @param token1AndLong1Amount The token1 amount to be withdrawn and the long1 amount burnt.
/// @param shortAmount The short amount burnt.
/// @param data The bytes code data sent to the callback.
struct TimeswapV2OptionBurnCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 token0AndLong0Amount;
  uint256 token1AndLong1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @dev Parameter for the swap callback.
/// @param strike The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param isLong0ToLong1 True when swapping long0 for long1. False when swapping long1 for long0.
/// @param token0AndLong0Amount If isLong0ToLong1 is true, the amount of long0 burnt and token0 to be withdrawn.
/// If isLong0ToLong1 is false, the amount of long0 minted and token0 to be deposited.
/// @param token1AndLong1Amount If isLong0ToLong1 is true, the amount of long1 withdrawn and token0 to be deposited.
/// If isLong0ToLong1 is false, the amount of long1 burnt and token1 to be withdrawn.
/// @param data The bytes code data sent to the callback.
struct TimeswapV2OptionSwapCallbackParam {
  uint256 strike;
  uint256 maturity;
  bool isLong0ToLong1;
  uint256 token0AndLong0Amount;
  uint256 token1AndLong1Amount;
  bytes data;
}

/// @dev Parameter for the collect callback.
/// @param strike The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param token0Amount The token0 amount to be withdrawn.
/// @param token1Amount The token1 amount to be withdrawn.
/// @param shortAmount The short amount burnt.
/// @param data The bytes code data sent to the callback.
struct TimeswapV2OptionCollectCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 shortAmount;
  bytes data;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {Error} from "@timeswap-labs/v2-library/contracts/Error.sol";

import {TimeswapV2OptionMint, TimeswapV2OptionBurn, TimeswapV2OptionSwap, TimeswapV2OptionCollect, TransactionLibrary} from "../enums/Transaction.sol";

/// @dev The parameter to call the mint function.
/// @param strike The strike of the option.
/// @param maturity The maturity of the option.
/// @param long0To The recipient of long0 positions.
/// @param long1To The recipient of long1 positions.
/// @param shortTo The recipient of short positions.
/// @param transaction The type of mint transaction, more information in Transaction module.
/// @param amount0 If transaction is givenTokensAndLongs, the amount of token0 deposited, and amount of long0 position minted.
/// If transaction is givenShorts, the amount of short minted, where the equivalent strike converted amount is long0 positions.
/// @param amount1 If transaction is givenTokensAndLongs, the amount of token1 deposited, and amount of long1 position minted.
/// If transaction is givenShorts, the amount of short minted, where the equivalent strike converted amount is long1 positions.
/// @param data The data to be sent to the function, which will go to the mint callback.
struct TimeswapV2OptionMintParam {
  uint256 strike;
  uint256 maturity;
  address long0To;
  address long1To;
  address shortTo;
  TimeswapV2OptionMint transaction;
  uint256 amount0;
  uint256 amount1;
  bytes data;
}

/// @dev The parameter to call the burn function.
/// @param strike The strike of the option.
/// @param maturity The maturity of the option.
/// @param token0To The recipient of token0 withdrawn.
/// @param token1To The recipient of token1 withdrawn.
/// @param transaction The type of burn transaction, more information in Transaction module.
/// @param amount0 If transaction is givenTokensAndLongs, the amount of token0 withdrawn, and amount of long0 position burnt.
/// If transaction is givenShorts, the amount of short burnt, where the equivalent strike converted amount is long0 positions.
/// @param amount1 If transaction is givenTokensAndLongs, the amount of token1 withdrawn, and amount of long1 position burnt.
/// If transaction is givenShorts, the amount of short burnt, where the equivalent strike converted amount is long1 positions.
/// @param data The data to be sent to the function, which will go to the burn callback.
/// @notice If data length is zero, skips the callback.
struct TimeswapV2OptionBurnParam {
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  TimeswapV2OptionBurn transaction;
  uint256 amount0;
  uint256 amount1;
  bytes data;
}

/// @dev The parameter to call the swap function.
/// @param strike The strike of the option.
/// @param maturity The maturity of the option.
/// @param tokenTo The recipient of token0 when isLong0ToLong1 or token1 when isLong1ToLong0.
/// @param longTo The recipient of long1 positions when isLong0ToLong1 or long0 when isLong1ToLong0.
/// @param isLong0ToLong1 Transform long0 positions to long1 positions when true. Transform long1 positions to long0 positions when false.
/// @param transaction The type of swap transaction, more information in Transaction module.
/// @param amount If isLong0ToLong1 and transaction is GivenToken0AndLong0, this is the amount of token0 withdrawn, and the amount of long0 position burnt.
/// If isLong1ToLong0 and transaction is GivenToken0AndLong0, this is the amount of token0 to be deposited, and the amount of long0 position minted.
/// If isLong0ToLong1 and transaction is GivenToken1AndLong1, this is the amount of token1 to be deposited, and the amount of long1 position minted.
/// If isLong1ToLong0 and transaction is GivenToken1AndLong1, this is the amount of token1 withdrawn, and the amount of long1 position burnt.
/// @param data The data to be sent to the function, which will go to the swap callback.
struct TimeswapV2OptionSwapParam {
  uint256 strike;
  uint256 maturity;
  address tokenTo;
  address longTo;
  bool isLong0ToLong1;
  TimeswapV2OptionSwap transaction;
  uint256 amount;
  bytes data;
}

/// @dev The parameter to call the collect function.
/// @param strike The strike of the option.
/// @param maturity The maturity of the option.
/// @param token0To The recipient of token0 withdrawn.
/// @param token1To The recipient of token1 withdrawn.
/// @param transaction The type of collect transaction, more information in Transaction module.
/// @param amount If transaction is GivenShort, the amount of short position burnt.
/// If transaction is GivenToken0, the amount of token0 withdrawn.
/// If transaction is GivenToken1, the amount of token1 withdrawn.
/// @param data The data to be sent to the function, which will go to the collect callback.
/// @notice If data length is zero, skips the callback.
struct TimeswapV2OptionCollectParam {
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  TimeswapV2OptionCollect transaction;
  uint256 amount;
  bytes data;
}

library ParamLibrary {
  /// @dev Sanity checks
  /// @param param the parameter for mint transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2OptionMintParam memory param, uint96 blockTimestamp) internal pure {
    if (param.strike == 0) Error.zeroInput();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.shortTo == address(0)) Error.zeroAddress();
    if (param.long0To == address(0)) Error.zeroAddress();
    if (param.long1To == address(0)) Error.zeroAddress();
    TransactionLibrary.check(param.transaction);
    if (param.amount0 == 0 && param.amount1 == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for burn transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2OptionBurnParam memory param, uint96 blockTimestamp) internal pure {
    if (param.strike == 0) Error.zeroInput();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.token0To == address(0)) Error.zeroAddress();
    if (param.token1To == address(0)) Error.zeroAddress();
    TransactionLibrary.check(param.transaction);
    if (param.amount0 == 0 && param.amount1 == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for swap transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2OptionSwapParam memory param, uint96 blockTimestamp) internal pure {
    if (param.strike == 0) Error.zeroInput();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.tokenTo == address(0)) Error.zeroAddress();
    if (param.longTo == address(0)) Error.zeroAddress();
    TransactionLibrary.check(param.transaction);
    if (param.amount == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for collect transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2OptionCollectParam memory param, uint96 blockTimestamp) internal pure {
    if (param.strike == 0) Error.zeroInput();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity >= blockTimestamp) Error.stillActive(param.maturity, blockTimestamp);
    if (param.token0To == address(0)) Error.zeroAddress();
    if (param.token1To == address(0)) Error.zeroAddress();
    TransactionLibrary.check(param.transaction);
    if (param.amount == 0) Error.zeroInput();
  }
}

File 25 of 69 : StrikeAndMaturity.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @dev A data with strike and maturity data.
/// @param strike The strike.
/// @param maturity The maturity.
struct StrikeAndMaturity {
  uint256 strike;
  uint256 maturity;
}

File 26 of 69 : Multicall.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.8.8;

import "../interfaces/IMulticall.sol";

/// @title Multicall
/// @notice Enables calling multiple methods in a single call to the contract
abstract contract Multicall is IMulticall {
  /// @inheritdoc IMulticall
  function multicall(bytes[] calldata data) public payable override returns (bytes[] memory results) {
    results = new bytes[](data.length);
    for (uint256 i = 0; i < data.length; i++) {
      (bool success, bytes memory result) = address(this).delegatecall(data[i]);

      if (!success) {
        // Next 5 lines from https://ethereum.stackexchange.com/a/83577
        if (result.length < 68) revert MulticallFailed("Invalid Result");
        assembly {
          result := add(result, 0x04)
        }
        revert MulticallFailed(abi.decode(result, (string)));
      }

      results[i] = result;
    }
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {IWrappedNative} from "../interfaces/external/IWrappedNative.sol";
import {INativeImmutableState} from "../interfaces/INativeImmutableState.sol";
import {INativeWithdraws} from "../interfaces/INativeWithdraws.sol";
import {INativePayments} from "../interfaces/INativePayments.sol";
import {NativeTransfer} from "../libraries/NativeTransfer.sol";

abstract contract NativeImmutableState is INativeImmutableState {
  /// @inheritdoc INativeImmutableState
  address public immutable override wrappedNativeToken;

  constructor(address chosenWrappedNativeToken) {
    wrappedNativeToken = chosenWrappedNativeToken;
  }
}

abstract contract NativeWithdraws is INativeWithdraws, NativeImmutableState {
  error CallerNotWrappedNative(address from);

  error InsufficientWrappedNative(uint256 value);

  receive() external payable {
    if (msg.sender != wrappedNativeToken) revert CallerNotWrappedNative(msg.sender);
  }

  /// @inheritdoc INativeWithdraws
  function unwrapWrappedNatives(uint256 amountMinimum, address recipient) external payable override {
    uint256 balanceWrappedNative = IWrappedNative(wrappedNativeToken).balanceOf(address(this));

    if (balanceWrappedNative < amountMinimum) revert InsufficientWrappedNative(balanceWrappedNative);

    if (balanceWrappedNative != 0) {
      IWrappedNative(wrappedNativeToken).withdraw(balanceWrappedNative);

      NativeTransfer.safeTransferNatives(recipient, balanceWrappedNative);
    }
  }
}

abstract contract NativePayments is INativePayments, NativeImmutableState {
  using SafeERC20 for IERC20;

  /// @inheritdoc INativePayments
  function refundNatives() external payable override {
    if (address(this).balance > 0) NativeTransfer.safeTransferNatives(msg.sender, address(this).balance);
  }

  /// @param token The token to pay
  /// @param payer The entity that must pay
  /// @param recipient The entity that will receive payment
  /// @param value The amount to pay
  function pay(address token, address payer, address recipient, uint256 value) internal {
    if (token == wrappedNativeToken && address(this).balance >= value) {
      // pay with WrappedNative
      IWrappedNative(wrappedNativeToken).deposit{value: value}();

      IERC20(token).safeTransfer(recipient, value);
    } else {
      // pull payment
      IERC20(token).safeTransferFrom(payer, recipient, value);
    }
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {IUniswapV3SwapCallback} from "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";

import {IUniswapImmutableState} from "../interfaces/IUniswapImmutableState.sol";

import {Verify} from "../libraries/Verify.sol";
import {UniswapV3PoolLibrary} from "../libraries/UniswapV3Pool.sol";

import {NativePayments} from "./Native.sol";

abstract contract UniswapImmutableState is IUniswapImmutableState {
  /// @inheritdoc IUniswapImmutableState
  address public immutable override uniswapV3Factory;

  constructor(address chosenUniswapV3Factory) {
    uniswapV3Factory = chosenUniswapV3Factory;
  }
}

abstract contract UniswapCalculate is UniswapImmutableState {
  function uniswapCalculate(int256 amount0Delta, int256 amount1Delta, bytes memory data) internal view {
    (address token0, address token1, uint24 uniswapV3Fee) = abi.decode(data, (address, address, uint24));

    Verify.uniswapV3Pool(uniswapV3Factory, token0, token1, uniswapV3Fee);

    UniswapV3PoolLibrary.passCalculateInfo(amount0Delta, amount1Delta);
  }
}

abstract contract UniswapV3Callback is UniswapCalculate, IUniswapV3SwapCallback {
  using SafeERC20 for IERC20;

  function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external override {
    (bool hasStateChange, bytes memory innerData) = abi.decode(data, (bool, bytes));

    if (hasStateChange) {
      (address token0, address token1, uint24 uniswapV3Fee) = abi.decode(innerData, (address, address, uint24));

      Verify.uniswapV3Pool(uniswapV3Factory, token0, token1, uniswapV3Fee);

      IERC20(amount0Delta > 0 ? token0 : token1).safeTransfer(
        msg.sender,
        uint256(amount0Delta > 0 ? amount0Delta : amount1Delta)
      );
    } else uniswapCalculate(amount0Delta, amount1Delta, innerData);
  }
}

abstract contract UniswapV3CallbackWithNative is UniswapCalculate, IUniswapV3SwapCallback, NativePayments {
  using SafeERC20 for IERC20;

  function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external override {
    (bool hasStateChange, bytes memory innerData) = abi.decode(data, (bool, bytes));

    if (hasStateChange) {
      (address msgSender, address token0, address token1, uint24 uniswapV3Fee) = abi.decode(
        innerData,
        (address, address, address, uint24)
      );

      Verify.uniswapV3Pool(uniswapV3Factory, token0, token1, uniswapV3Fee);

      pay(
        amount0Delta > 0 ? token0 : token1,
        msgSender,
        msg.sender,
        uint256(amount0Delta > 0 ? amount0Delta : amount1Delta)
      );
    } else uniswapCalculate(amount0Delta, amount1Delta, innerData);
  }
}

abstract contract UniswapV3CallbackWithOptionalNative is UniswapCalculate, IUniswapV3SwapCallback, NativePayments {
  using SafeERC20 for IERC20;

  function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external override {
    (bool hasStateChange, bytes memory innerData) = abi.decode(data, (bool, bytes));

    if (hasStateChange) {
      (address msgSender, address token0, address token1, uint24 uniswapV3Fee) = abi.decode(
        innerData,
        (address, address, address, uint24)
      );

      Verify.uniswapV3Pool(uniswapV3Factory, token0, token1, uniswapV3Fee);

      if (msgSender == address(this))
        IERC20(amount0Delta > 0 ? token0 : token1).safeTransfer(
          msg.sender,
          uint256(amount0Delta > 0 ? amount0Delta : amount1Delta)
        );
      else
        pay(
          amount0Delta > 0 ? token0 : token1,
          msgSender,
          msg.sender,
          uint256(amount0Delta > 0 ? amount0Delta : amount1Delta)
        );
    } else uniswapCalculate(amount0Delta, amount1Delta, innerData);
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

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

/// @title Interface for Native token
/// @dev This interface is used to interact with the native token
/// @dev The native token could be ETH for ethereum or BNB for Binance Smart Chain or MATIC for Polygon
interface IWrappedNative is IERC20 {
  /// @notice Deposit native token to get wrapped native token
  function deposit() external payable;

  /// @notice Withdraw wrapped native token to get native token
  function withdraw(uint256) external;
}

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

/// @title Multicall interface
/// @notice Enables calling multiple methods in a single call to the contract
interface IMulticall {
  /// @notice Call multiple functions in the current contract and return the data from all of them if they all succeed
  /// @dev The `msg.value` should not be trusted for any method callable from multicall.
  /// @param data The encoded function data for each of the calls to make to this contract
  /// @return results The results from each of the calls passed in via data
  function multicall(bytes[] calldata data) external payable returns (bytes[] memory results);

  error MulticallFailed(string revertString);
}

File 31 of 69 : INativeImmutableState.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title NativeImmutableState interface
interface INativeImmutableState {
  /// @return Returns the address of Wrapped Native token
  function wrappedNativeToken() external view returns (address);
}

File 32 of 69 : INativePayments.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title NativePayments interface
/// @notice Functions to ease payments of native tokens
interface INativePayments {
  /// @notice Refunds any Native Token balance held by this contract to the `msg.sender`
  function refundNatives() external payable;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title NativeWithdraws interface

interface INativeWithdraws {
  /// @notice Unwraps the contract's Wrapped Native token balance and sends it to recipient as Native token.
  /// @dev The amountMinimum parameter prevents malicious contracts from stealing Wrapped Native from users.
  /// @param amountMinimum The minimum amount of Wrapped Native to unwrap
  /// @param recipient The address receiving Native token
  function unwrapWrappedNatives(uint256 amountMinimum, address recipient) external payable;
}

File 34 of 69 : ITimeswapV2PeripheryUniswapV3BorrowGivenPrincipal.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {IUniswapV3SwapCallback} from "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";

import {ITimeswapV2PeripheryBorrowGivenPrincipal} from "@timeswap-labs/v2-periphery/contracts/interfaces/ITimeswapV2PeripheryBorrowGivenPrincipal.sol";

import {TimeswapV2PeripheryUniswapV3BorrowGivenPrincipalParam} from "../structs/Param.sol";

import {INativeWithdraws} from "./INativeWithdraws.sol";
import {INativePayments} from "./INativePayments.sol";
import {IMulticall} from "./IMulticall.sol";

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

/// @title An interface for TS-V2 Periphery UniswapV3 Borrow Given Pricipal.
interface ITimeswapV2PeripheryUniswapV3BorrowGivenPrincipal is
  ITimeswapV2PeripheryBorrowGivenPrincipal,
  IUniswapImmutableState,
  IUniswapV3SwapCallback,
  INativeWithdraws,
  INativePayments,
  IMulticall
{
  event BorrowGivenPrincipal(
    address indexed token0,
    address indexed token1,
    uint256 strike,
    uint256 indexed maturity,
    uint24 uniswapV3Fee,
    address from,
    address tokenTo,
    address longTo,
    bool isToken0,
    bool isLong0,
    uint256 tokenAmount,
    uint256 positionAmount
  );

  error MaxPositionReached(uint256 positionAmount, uint256 maxPositionAmount);

  /// @dev The borrow given principal function.
  /// @param param Borrow given principal param.
  /// @return positionAmount
  function borrowGivenPrincipal(
    TimeswapV2PeripheryUniswapV3BorrowGivenPrincipalParam calldata param
  ) external payable returns (uint256 positionAmount);
}

File 35 of 69 : IUniswapImmutableState.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @title UniswapImmutableState interface
interface IUniswapImmutableState {
  /// @return Returns the address of Uniswap Factory contract address
  function uniswapV3Factory() external view returns (address);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

library NativeTransfer {
  error NativeTransferFailed(address to, uint256 value);

  /// @notice Transfers Natives to the recipient address
  /// @dev Reverts if the transfer fails
  /// @param to The destination of the transfer
  /// @param value The value to be transferred
  function safeTransferNatives(address to, uint256 value) internal {
    (bool success, ) = to.call{value: value}(new bytes(0));
    if (!success) {
      revert NativeTransferFailed(to, value);
    }
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {SafeCast} from "@timeswap-labs/v2-library/contracts/SafeCast.sol";
import {Math} from "@timeswap-labs/v2-library/contracts/Math.sol";
import {FullMath} from "@timeswap-labs/v2-library/contracts/FullMath.sol";

library PriceConversion {
  using SafeCast for uint256;

  function convertTsToUni(uint256 strike) internal pure returns (uint160 sqrtRatioX96) {
    if (strike <= type(uint192).max) return sqrtRatioX96 = Math.sqrt(strike << 64, false).toUint160();

    (uint256 value0, uint256 value1) = FullMath.mul512(strike, 1 << 64);
    sqrtRatioX96 = FullMath.sqrt512(value0, value1, false).toUint160();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {UniswapV3PoolLibrary} from "./UniswapV3Pool.sol";

import {IUniswapV3Factory} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol";

library UniswapV3FactoryLibrary {
  using UniswapV3PoolLibrary for address;

  function get(
    address uniswapV3Factory,
    address token0,
    address token1,
    uint24 uniswapV3Fee
  ) internal view returns (address uniswapV3Pool) {
    uniswapV3Pool = IUniswapV3Factory(uniswapV3Factory).getPool(token0, token1, uniswapV3Fee);
  }

  function getWithCheck(
    address uniswapV3Factory,
    address token0,
    address token1,
    uint24 uniswapV3Fee
  ) internal view returns (address uniswapV3Pool) {
    uniswapV3Pool = get(uniswapV3Factory, token0, token1, uniswapV3Fee);

    uniswapV3Pool.checkNotZeroAddress();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {IUniswapV3PoolActions} from "@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolActions.sol";
import {IUniswapV3PoolState} from "@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolState.sol";
import {IUniswapV3PoolImmutables} from "@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolImmutables.sol";
import {LowGasSafeMath} from "@uniswap/v3-core/contracts/libraries/LowGasSafeMath.sol";
import {LiquidityMath} from "@uniswap/v3-core/contracts/libraries/LiquidityMath.sol";
import {SafeCast} from "@uniswap/v3-core/contracts/libraries/SafeCast.sol";

import {FullMath} from "@timeswap-labs/v2-library/contracts/FullMath.sol";
import {Math} from "@timeswap-labs/v2-library/contracts/Math.sol";
import {CatchError} from "@timeswap-labs/v2-library/contracts/CatchError.sol";

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

import {UniswapV3SwapParam, UniswapV3SwapForRebalanceParam, UniswapV3CalculateSwapParam, UniswapV3CalculateSwapForRebalanceParam} from "../structs/SwapParam.sol";

library UniswapV3PoolLibrary {
  using LowGasSafeMath for int256;
  using SafeCast for uint256;
  using PriceConversion for uint256;
  using Math for uint256;
  using CatchError for bytes;

  error ZeroPoolAddress();

  error PassCalculateInfo(int256 amount0, int256 amount1);

  function checkNotZeroAddress(address pool) internal pure {
    if (pool == address(0)) revert ZeroPoolAddress();
  }

  function passCalculateInfo(int256 amount0, int256 amount1) internal pure {
    revert PassCalculateInfo(amount0, amount1);
  }

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

  function swap(
    address pool,
    UniswapV3SwapParam memory param
  ) internal returns (uint256 tokenAmountIn, uint256 tokenAmountOut) {
    (uint160 sqrtPrice, , , , , , ) = IUniswapV3PoolState(pool).slot0();

    uint160 sqrtStrike = param.strikeLimit != 0
      ? param.strikeLimit.convertTsToUni()
      : (param.zeroForOne ? MIN_SQRT_RATIO + 1 : MAX_SQRT_RATIO - 1);

    if (sqrtStrike <= MIN_SQRT_RATIO) sqrtStrike = MIN_SQRT_RATIO + 1;
    if (sqrtStrike >= MAX_SQRT_RATIO) sqrtStrike = MAX_SQRT_RATIO - 1;

    if (param.zeroForOne ? (sqrtPrice > sqrtStrike) : (sqrtPrice < sqrtStrike)) {
      (int256 amount0, int256 amount1) = IUniswapV3PoolActions(pool).swap(
        param.recipient,
        param.zeroForOne,
        param.exactInput ? param.amount.toInt256() : -param.amount.toInt256(),
        sqrtStrike,
        param.data
      );

      (tokenAmountIn, tokenAmountOut) = param.zeroForOne
        ? (uint256(amount0), uint256(-amount1))
        : (uint256(amount1), uint256(-amount0));
    }
  }

  function swapForRebalance(
    address pool,
    UniswapV3SwapForRebalanceParam memory param
  ) internal returns (uint256 tokenAmountIn, uint256 tokenAmountOut) {
    uint160 sqrtStrike = (
      param.zeroForOne
        ? FullMath.mulDiv(
          param.strikeLimit.convertTsToUni(),
          1 << 16,
          (uint256(1) << 16).unsafeSub(param.transactionFee),
          true
        )
        : FullMath.mulDiv(
          param.strikeLimit.convertTsToUni(),
          (uint256(1) << 16).unsafeSub(param.transactionFee),
          1 << 16,
          false
        )
    ).toUint160();

    if (sqrtStrike <= MIN_SQRT_RATIO) sqrtStrike = MIN_SQRT_RATIO + 1;
    if (sqrtStrike >= MAX_SQRT_RATIO) sqrtStrike = MAX_SQRT_RATIO - 1;

    (int256 amount0, int256 amount1) = IUniswapV3PoolActions(pool).swap(
      param.recipient,
      param.zeroForOne,
      param.exactInput ? param.amount.toInt256() : -param.amount.toInt256(),
      sqrtStrike,
      param.data
    );

    (tokenAmountIn, tokenAmountOut) = param.zeroForOne
      ? (uint256(amount0), uint256(-amount1))
      : (uint256(amount1), uint256(-amount0));
  }

  function calculateSwap(
    address pool,
    UniswapV3CalculateSwapParam memory param
  ) internal returns (uint256 amountIn, uint256 amountOut) {
    (uint160 sqrtPriceX96, , , , , , ) = IUniswapV3PoolState(pool).slot0();

    uint160 sqrtRatioTargetX96 = param.strikeLimit != 0
      ? param.strikeLimit.convertTsToUni()
      : (param.zeroForOne ? MIN_SQRT_RATIO + 1 : MAX_SQRT_RATIO - 1);

    if (sqrtRatioTargetX96 <= MIN_SQRT_RATIO) sqrtRatioTargetX96 = MIN_SQRT_RATIO + 1;
    if (sqrtRatioTargetX96 >= MAX_SQRT_RATIO) sqrtRatioTargetX96 = MAX_SQRT_RATIO - 1;

    if (param.zeroForOne ? sqrtRatioTargetX96 < sqrtPriceX96 : sqrtRatioTargetX96 > sqrtPriceX96) {
      int256 amount0;
      int256 amount1;

      try
        IUniswapV3PoolActions(pool).swap(
          address(this),
          param.zeroForOne,
          param.exactInput ? param.amount.toInt256() : -param.amount.toInt256(),
          sqrtRatioTargetX96,
          param.data
        )
      {} catch (bytes memory reason) {
        (amount0, amount1) = handleRevert(reason);
      }

      (amountIn, amountOut) = param.zeroForOne
        ? (uint256(amount0), uint256(-amount1))
        : (uint256(amount1), uint256(-amount0));
    }
  }

  function calculateSwapForRebalance(
    address pool,
    UniswapV3CalculateSwapForRebalanceParam memory param
  ) internal returns (bool zeroForOne, uint256 amountIn, uint256 amountOut) {
    (uint160 sqrtPriceX96, , , , , , ) = IUniswapV3PoolState(pool).slot0();

    uint160 sqrtRatioTargetX96;
    uint256 amount;
    {
      uint160 baseSqrtRatioTargetX96 = param.strikeLimit.convertTsToUni();

      if (param.token0Amount != 0) {
        uint160 adjustedSqrtRatioTargetX96 = FullMath
          .mulDiv(baseSqrtRatioTargetX96, 1 << 16, (uint256(1) << 16).unsafeSub(param.transactionFee), true)
          .toUint160();

        if (adjustedSqrtRatioTargetX96 < sqrtPriceX96) {
          sqrtRatioTargetX96 = adjustedSqrtRatioTargetX96;
          amount = param.token0Amount;
          zeroForOne = true;
        }
      }

      if (param.token1Amount != 0) {
        uint160 adjustedSqrtRatioTargetX96 = FullMath
          .mulDiv(baseSqrtRatioTargetX96, (uint256(1) << 16).unsafeSub(param.transactionFee), 1 << 16, false)
          .toUint160();

        if (adjustedSqrtRatioTargetX96 > sqrtPriceX96) {
          sqrtRatioTargetX96 = adjustedSqrtRatioTargetX96;
          amount = param.token1Amount;
        }
      }
    }

    if (amount != 0) {
      int256 amount0;
      int256 amount1;

      if (sqrtRatioTargetX96 <= MIN_SQRT_RATIO) sqrtRatioTargetX96 = MIN_SQRT_RATIO + 1;
      if (sqrtRatioTargetX96 >= MAX_SQRT_RATIO) sqrtRatioTargetX96 = MAX_SQRT_RATIO - 1;

      try
        IUniswapV3PoolActions(pool).swap(address(this), zeroForOne, amount.toInt256(), sqrtRatioTargetX96, param.data)
      {} catch (bytes memory reason) {
        (amount0, amount1) = handleRevert(reason);
      }

      (amountIn, amountOut) = zeroForOne
        ? (uint256(amount0), uint256(-amount1))
        : (uint256(amount1), uint256(-amount0));
    }
  }

  function handleRevert(bytes memory reason) private pure returns (int256 amount0, int256 amount1) {
    return abi.decode(reason.catchError(PassCalculateInfo.selector), (int256, int256));
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {UniswapV3FactoryLibrary} from "./UniswapV3Factory.sol";

library Verify {
  error CanOnlyBeCalledByUniswapV3Contract();

  function uniswapV3Pool(address uniswapV3Factory, address token0, address token1, uint24 uniswapV3Fee) internal view {
    address pool = UniswapV3FactoryLibrary.get(uniswapV3Factory, token0, token1, uniswapV3Fee);

    if (pool != msg.sender) revert CanOnlyBeCalledByUniswapV3Contract();
  }
}

File 41 of 69 : Param.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {TimeswapV2PeripheryLongChoice} from "@timeswap-labs/v2-periphery/contracts/enums/Transaction.sol";

struct TimeswapV2PeripheryUniswapV3CollectProtocolFeesParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address tokenTo;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  bool isToken0;
  uint256 long0Requested;
  uint256 long1Requested;
  uint256 shortRequested;
  uint256 minTokenAmount;
  uint256 minExcessLong0Amount;
  uint256 minExcessLong1Amount;
  uint256 minExcessShortAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3CollectTransactionFeesParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address tokenTo;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  bool isToken0;
  uint256 long0Requested;
  uint256 long1Requested;
  uint256 shortRequested;
  uint256 minTokenAmount;
  uint256 minExcessLong0Amount;
  uint256 minExcessLong1Amount;
  uint256 minExcessShortAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3CollectTransactionFeesAfterMaturityParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address to;
  bool isToken0;
  uint256 shortRequested;
  uint256 minTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3AddLiquidityGivenPrincipalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address liquidityTo;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  bool isToken0;
  bool preferLong0Excess;
  uint256 tokenAmount;
  uint160 minLiquidityAmount;
  uint256 minExcessLong0Amount;
  uint256 minExcessLong1Amount;
  uint256 minExcessShortAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3RemoveLiquidityGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address tokenTo;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  bool isToken0;
  bool preferLong0Excess;
  uint160 liquidityAmount;
  uint256 minTokenAmount;
  uint256 minExcessLong0Amount;
  uint256 minExcessLong1Amount;
  uint256 minExcessShortAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3LendGivenPrincipalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address to;
  bool isToken0;
  uint256 tokenAmount;
  uint256 minReturnAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3LendGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address to;
  bool isToken0;
  uint256 positionAmount;
  uint256 maxTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3CloseBorrowGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address to;
  bool isToken0;
  bool isLong0;
  uint256 positionAmount;
  uint256 maxTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3BorrowGivenPrincipalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address tokenTo;
  address longTo;
  bool isToken0;
  bool isLong0;
  uint256 tokenAmount;
  uint256 maxPositionAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3BorrowGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address tokenTo;
  address longTo;
  bool isToken0;
  bool isLong0;
  uint256 positionAmount;
  uint256 minTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3CloseLendGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address to;
  bool isToken0;
  uint256 positionAmount;
  uint256 minTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3RedeemParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address to;
  bool isToken0;
  uint256 token0AndLong0Amount;
  uint256 token1AndLong1Amount;
  uint256 minTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3TransformParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address tokenTo;
  address longTo;
  bool isToken0;
  bool isLong0ToLong1;
  uint256 positionAmount;
  bool isMaxTokenDeposit;
  uint256 minOrMaxTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3WithdrawParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address to;
  bool isToken0;
  uint256 positionAmount;
  uint256 minTokenAmount;
  uint256 deadline;
}

struct TimeswapV2PeripheryUniswapV3RebalanceParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint24 uniswapV3Fee;
  address tokenTo;
  address excessShortTo;
  bool isToken0;
  uint256 minTokenAmount;
  uint256 minExcessShortAmount;
  uint256 deadline;
}

File 42 of 69 : SwapParam.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

struct UniswapV3SwapParam {
  address recipient;
  bool zeroForOne;
  bool exactInput;
  uint256 amount;
  uint256 strikeLimit;
  bytes data;
}

struct UniswapV3SwapForRebalanceParam {
  address recipient;
  bool zeroForOne;
  bool exactInput;
  uint256 amount;
  uint256 strikeLimit;
  uint256 transactionFee;
  bytes data;
}

struct UniswapV3CalculateSwapParam {
  bool zeroForOne;
  bool exactInput;
  uint256 amount;
  uint256 strikeLimit;
  bytes data;
}

struct UniswapV3CalculateSwapGivenBalanceLimitParam {
  address token0;
  address token1;
  uint256 strike;
  uint24 uniswapV3Fee;
  bool isToken0;
  uint256 token0Balance;
  uint256 token1Balance;
  uint256 tokenAmount;
}

struct UniswapV3CalculateSwapForRebalanceParam {
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 strikeLimit;
  uint256 transactionFee;
  bytes data;
}

struct UniswapV3SwapCalculationParam {
  uint24 uniswapV3Fee;
  uint160 sqrtPriceX96;
  uint160 sqrtRatioTargetX96;
  int24 tick;
  int256 amountSpecified;
  bool zeroForOne;
  bool exactInput;
}

File 43 of 69 : Transaction.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

enum TimeswapV2PeripheryLongChoice {
  Long0,
  Long1
}

enum TimeswapV2PeripheryTokenChoice {
  Token0,
  Token1
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {ITimeswapV2OptionMintCallback} from "@timeswap-labs/v2-option/contracts/interfaces/callbacks/ITimeswapV2OptionMintCallback.sol";
import {ITimeswapV2OptionSwapCallback} from "@timeswap-labs/v2-option/contracts/interfaces/callbacks/ITimeswapV2OptionSwapCallback.sol";

import {ITimeswapV2PoolLeverageCallback} from "@timeswap-labs/v2-pool/contracts/interfaces/callbacks/ITimeswapV2PoolLeverageCallback.sol";

import {ITimeswapV2TokenMintCallback} from "@timeswap-labs/v2-token/contracts/interfaces/callbacks/ITimeswapV2TokenMintCallback.sol";

/// @title An interface for TS-V2 Periphery Borrow Given Position
interface ITimeswapV2PeripheryBorrowGivenPrincipal is
  ITimeswapV2OptionMintCallback,
  ITimeswapV2OptionSwapCallback,
  ITimeswapV2PoolLeverageCallback,
  ITimeswapV2TokenMintCallback
{
  /// @dev Returns the option factory address.
  /// @return optionFactory The option factory address.
  function optionFactory() external returns (address);

  /// @dev Returns the pool factory address.
  /// @return poolFactory The pool factory address.
  function poolFactory() external returns (address);

  /// @dev Return the tokens address
  function tokens() external returns (address);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {ITimeswapV2OptionFactory} from "@timeswap-labs/v2-option/contracts/interfaces/ITimeswapV2OptionFactory.sol";

import {ITimeswapV2PoolFactory} from "@timeswap-labs/v2-pool/contracts/interfaces/ITimeswapV2PoolFactory.sol";

library Verify {
  error CanOnlyBeCalledByOptionContract();

  error CanOnlyBeCalledByPoolContract();

  error CanOnlyBeCalledByTokensContract();

  error CanOnlyBeCalledByLiquidityTokensContract();

  function timeswapV2Option(address optionFactory, address token0, address token1) internal view {
    address optionPair = ITimeswapV2OptionFactory(optionFactory).get(token0, token1);

    if (optionPair != msg.sender) revert CanOnlyBeCalledByOptionContract();
  }

  function timeswapV2Pool(
    address optionFactory,
    address poolFactory,
    address token0,
    address token1
  ) internal view returns (address optionPair) {
    optionPair = ITimeswapV2OptionFactory(optionFactory).get(token0, token1);

    address poolPair = ITimeswapV2PoolFactory(poolFactory).get(optionPair);

    if (poolPair != msg.sender) revert CanOnlyBeCalledByPoolContract();
  }

  function timeswapV2Token(address tokens) internal view {
    if (tokens != msg.sender) revert CanOnlyBeCalledByTokensContract();
  }

  function timeswapV2LiquidityToken(address liquidityTokens) internal view {
    if (liquidityTokens != msg.sender) revert CanOnlyBeCalledByLiquidityTokensContract();
  }
}

File 46 of 69 : InternalParam.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

struct TimeswapV2PeripheryCollectProtocolFeesExcessLongChoiceInternalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 token0Balance;
  uint256 token1Balance;
  uint256 tokenAmount;
  bytes data;
}

struct TimeswapV2PeripheryCollectTransactionFeesExcessLongChoiceInternalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 token0Balance;
  uint256 token1Balance;
  uint256 tokenAmount;
  bytes data;
}

struct TimeswapV2PeripheryAddLiquidityGivenPrincipalInternalParam {
  address optionPair;
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 liquidityAmount;
  uint256 excessLong0Amount;
  uint256 excessLong1Amount;
  uint256 excessShortAmount;
  bytes data;
}

struct TimeswapV2PeripheryRemoveLiquidityGivenPositionChoiceInternalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 token0Balance;
  uint256 token1Balance;
  uint256 tokenAmount;
  bytes data;
}

struct TimeswapV2PeripheryLendGivenPrincipalInternalParam {
  address optionPair;
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryCloseBorrowGivenPositionChoiceInternalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  bool isLong0;
  uint256 tokenAmount;
  bytes data;
}

struct TimeswapV2PeripheryCloseBorrowGivenPositionInternalParam {
  address optionPair;
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  bool isLong0;
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryBorrowGivenPrincipalInternalParam {
  address optionPair;
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  bool isLong0;
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryBorrowGivenPositionChoiceInternalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  bool isLong0;
  uint256 token0Balance;
  uint256 token1Balance;
  uint256 tokenAmount;
  bytes data;
}

struct TimeswapV2PeripheryBorrowGivenPositionInternalParam {
  address optionPair;
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  bool isLong0;
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryCloseLendGivenPositionChoiceInternalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 token0Balance;
  uint256 token1Balance;
  uint256 tokenAmount;
  bytes data;
}

struct TimeswapV2PeripheryTransformInternalParam {
  address optionPair;
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  bool isLong0ToLong1;
  uint256 token0AndLong0Amount;
  uint256 token1AndLong1Amount;
  bytes data;
}

struct TimeswapV2PeripheryRebalanceInternalParam {
  address optionPair;
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  bool isLong0ToLong1;
  uint256 token0Amount;
  uint256 token1Amount;
  uint256 excessShortAmount;
  bytes data;
}

File 47 of 69 : Param.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

struct TimeswapV2PeripheryCollectProtocolFeesParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  uint256 long0Requested;
  uint256 long1Requested;
  uint256 shortRequested;
  bytes data;
}

struct TimeswapV2PeripheryCollectTransactionFeesParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  uint256 long0Requested;
  uint256 long1Requested;
  uint256 shortRequested;
  bytes data;
}

struct TimeswapV2PeripheryCollectTransactionFeesAfterMaturityParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  uint256 shortRequested;
}

struct TimeswapV2PeripheryAddLiquidityGivenPrincipalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address liquidityTo;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  uint256 token0Amount;
  uint256 token1Amount;
  bool preferLong0Excess;
  bytes data;
}

struct TimeswapV2PeripheryRemoveLiquidityGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  address excessLong0To;
  address excessLong1To;
  address excessShortTo;
  uint160 liquidityAmount;
  bool preferLong0Excess;
  bytes data;
}

struct TimeswapV2PeripheryLendGivenPrincipalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address to;
  uint256 token0Amount;
  uint256 token1Amount;
  bytes data;
}

struct TimeswapV2PeripheryCloseBorrowGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address to;
  bool isLong0;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryBorrowGivenPrincipalParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address tokenTo;
  address longTo;
  bool isLong0;
  uint256 token0Amount;
  uint256 token1Amount;
  bytes data;
}

struct TimeswapV2PeripheryBorrowGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address tokenTo;
  address longTo;
  bool isLong0;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryCloseLendGivenPositionParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryRebalanceParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address tokenTo;
  address excessShortTo;
  bool isLong0ToLong1;
  bool givenLong0;
  uint256 tokenAmount;
  bytes data;
}

struct TimeswapV2PeripheryRedeemParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  uint256 token0AndLong0Amount;
  uint256 token1AndLong1Amount;
}

struct TimeswapV2PeripheryTransformParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address tokenTo;
  address longTo;
  bool isLong0ToLong1;
  uint256 positionAmount;
  bytes data;
}

struct TimeswapV2PeripheryWithdrawParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address token0To;
  address token1To;
  uint256 positionAmount;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {StrikeConversion} from "@timeswap-labs/v2-library/contracts/StrikeConversion.sol";

import {ITimeswapV2OptionFactory} from "@timeswap-labs/v2-option/contracts/interfaces/ITimeswapV2OptionFactory.sol";
import {ITimeswapV2Option} from "@timeswap-labs/v2-option/contracts/interfaces/ITimeswapV2Option.sol";
import {TimeswapV2OptionMintParam, TimeswapV2OptionSwapParam} from "@timeswap-labs/v2-option/contracts/structs/Param.sol";
import {TimeswapV2OptionMintCallbackParam, TimeswapV2OptionSwapCallbackParam} from "@timeswap-labs/v2-option/contracts/structs/CallbackParam.sol";
import {TimeswapV2OptionSwap} from "@timeswap-labs/v2-option/contracts/enums/Transaction.sol";
import {TimeswapV2OptionMint} from "@timeswap-labs/v2-option/contracts/enums/Transaction.sol";
import {TimeswapV2OptionPosition} from "@timeswap-labs/v2-option/contracts/enums/Position.sol";

import {OptionFactoryLibrary} from "@timeswap-labs/v2-option/contracts/libraries/OptionFactory.sol";

import {ITimeswapV2PoolFactory} from "@timeswap-labs/v2-pool/contracts/interfaces/ITimeswapV2PoolFactory.sol";
import {ITimeswapV2Pool} from "@timeswap-labs/v2-pool/contracts/interfaces/ITimeswapV2Pool.sol";

import {TimeswapV2PoolLeverageParam} from "@timeswap-labs/v2-pool/contracts/structs/Param.sol";
import {TimeswapV2PoolLeverageChoiceCallbackParam, TimeswapV2PoolLeverageCallbackParam} from "@timeswap-labs/v2-pool/contracts/structs/CallbackParam.sol";
import {PoolFactoryLibrary} from "@timeswap-labs/v2-pool/contracts/libraries/PoolFactory.sol";

import {TimeswapV2PoolLeverage} from "@timeswap-labs/v2-pool/contracts/enums/Transaction.sol";

import {ITimeswapV2Token} from "@timeswap-labs/v2-token/contracts/interfaces/ITimeswapV2Token.sol";

import {TimeswapV2TokenMintParam} from "@timeswap-labs/v2-token/contracts/structs/Param.sol";
import {TimeswapV2TokenMintCallbackParam} from "@timeswap-labs/v2-token/contracts/structs/CallbackParam.sol";

import {ITimeswapV2PeripheryBorrowGivenPrincipal} from "./interfaces/ITimeswapV2PeripheryBorrowGivenPrincipal.sol";

import {TimeswapV2PeripheryBorrowGivenPrincipalParam} from "./structs/Param.sol";
import {TimeswapV2PeripheryBorrowGivenPrincipalInternalParam} from "./structs/InternalParam.sol";

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

/// @title Abstract contract which specifies functions that are required for  borrow given principal which are to be inherited for a specific DEX/Aggregator implementation
abstract contract TimeswapV2PeripheryBorrowGivenPrincipal is ITimeswapV2PeripheryBorrowGivenPrincipal {
  /* ===== MODEL ===== */
  /// @inheritdoc ITimeswapV2PeripheryBorrowGivenPrincipal
  address public immutable override optionFactory;
  /// @inheritdoc ITimeswapV2PeripheryBorrowGivenPrincipal
  address public immutable override poolFactory;
  /// @inheritdoc ITimeswapV2PeripheryBorrowGivenPrincipal
  address public immutable override tokens;
  ///@dev data recieved from optionMintCallback
  struct CacheForTimeswapV2OptionMintCallback {
    address token0;
    address token1;
    address tokenTo;
    address longTo;
    bool isLong0;
    uint256 swapAmount;
    uint256 positionAmount;
  }

  /* ===== INIT ===== */

  constructor(address chosenOptionFactory, address chosenPoolFactory, address chosenTokens) {
    optionFactory = chosenOptionFactory;
    poolFactory = chosenPoolFactory;
    tokens = chosenTokens;
  }

  /// @notice the abstract implementation for borrowGivenPrincipal function
  /// @param param params for  borrowGivenPrincipal as mentioned in the TimeswapV2PeripheryBorrowGivenPrincipalParam struct
  /// @return positionAmount resulting borrowPosition amount
  /// @return data data passed as bytes in the param
  function borrowGivenPrincipal(
    TimeswapV2PeripheryBorrowGivenPrincipalParam memory param
  ) internal returns (uint256 positionAmount, bytes memory data) {
    (, address poolPair) = PoolFactoryLibrary.getWithCheck(optionFactory, poolFactory, param.token0, param.token1);

    data = abi.encode(
      param.token0,
      param.token1,
      param.tokenTo,
      param.longTo,
      param.isLong0,
      param.token0Amount,
      param.token1Amount,
      param.data
    );

    (, , , data) = ITimeswapV2Pool(poolPair).leverage(
      TimeswapV2PoolLeverageParam({
        strike: param.strike,
        maturity: param.maturity,
        long0To: address(this),
        long1To: address(this),
        transaction: TimeswapV2PoolLeverage.GivenLong,
        delta: StrikeConversion.combine(param.token0Amount, param.token1Amount, param.strike, true),
        data: data
      })
    );

    (positionAmount, data) = abi.decode(data, (uint256, bytes));
  }

  /// @notice the abstract implementation for leverageCallback function
  /// @param param params for  leverageChoiceCallback as mentioned in the TimeswapV2PoolLeverageChoiceCallbackParam struct
  /// @return long0Amount the amount of long0 chosen
  /// @return long1Amount the amount of long1 chosen
  /// @return data data passed as bytes in the param
  function timeswapV2PoolLeverageChoiceCallback(
    TimeswapV2PoolLeverageChoiceCallbackParam calldata param
  ) external view override returns (uint256 long0Amount, uint256 long1Amount, bytes memory data) {
    address token0;
    address token1;
    address tokenTo;
    address longTo;
    bool isLong0;
    (token0, token1, tokenTo, longTo, isLong0, long0Amount, long1Amount, data) = abi.decode(
      param.data,
      (address, address, address, address, bool, uint256, uint256, bytes)
    );

    Verify.timeswapV2Pool(optionFactory, poolFactory, token0, token1);

    data = abi.encode(token0, token1, tokenTo, longTo, isLong0, data);
  }

  /// @notice the abstract implementation for leverageCallback function
  /// @param param params for  leverageCallback as mentioned in the TimeswapV2PoolLeverageCallbackParam struct
  /// @return data data passed as bytes in the param
  function timeswapV2PoolLeverageCallback(
    TimeswapV2PoolLeverageCallbackParam calldata param
  ) external override returns (bytes memory data) {
    address token0;
    address token1;
    address tokenTo;
    address longTo;
    bool isLong0;
    (token0, token1, tokenTo, longTo, isLong0, data) = abi.decode(
      param.data,
      (address, address, address, address, bool, bytes)
    );

    address optionPair = Verify.timeswapV2Pool(optionFactory, poolFactory, token0, token1);

    data = abi.encode(
      CacheForTimeswapV2OptionMintCallback(
        token0,
        token1,
        tokenTo,
        longTo,
        isLong0,
        isLong0 ? param.long1Amount : param.long0Amount,
        isLong0 ? param.long0Amount : param.long1Amount
      ),
      data
    );

    (, , , data) = ITimeswapV2Option(optionPair).mint(
      TimeswapV2OptionMintParam({
        strike: param.strike,
        maturity: param.maturity,
        long0To: address(this),
        long1To: address(this),
        shortTo: msg.sender,
        transaction: TimeswapV2OptionMint.GivenShorts,
        amount0: isLong0 ? param.shortAmount : 0,
        amount1: isLong0 ? 0 : param.shortAmount,
        data: data
      })
    );
  }

  /// @notice the abstract implementation for TimeswapV2OptionMintCallback
  /// @param param params for mintCallBack from TimeswapV2Option
  /// @return data data passed in bytes in the param passed back
  function timeswapV2OptionMintCallback(
    TimeswapV2OptionMintCallbackParam memory param
  ) external override returns (bytes memory data) {
    CacheForTimeswapV2OptionMintCallback memory cache;
    (cache, data) = abi.decode(param.data, (CacheForTimeswapV2OptionMintCallback, bytes));

    Verify.timeswapV2Option(optionFactory, cache.token0, cache.token1);

    cache.positionAmount += cache.isLong0 ? param.token0AndLong0Amount : param.token1AndLong1Amount;

    if (cache.swapAmount != 0) {
      data = abi.encode(
        cache.token0,
        cache.token1,
        cache.longTo,
        cache.isLong0 ? param.token0AndLong0Amount : param.token1AndLong1Amount,
        cache.positionAmount,
        data
      );

      (, , data) = ITimeswapV2Option(msg.sender).swap(
        TimeswapV2OptionSwapParam({
          strike: param.strike,
          maturity: param.maturity,
          tokenTo: cache.tokenTo,
          longTo: address(this),
          isLong0ToLong1: !cache.isLong0,
          transaction: cache.isLong0
            ? TimeswapV2OptionSwap.GivenToken1AndLong1
            : TimeswapV2OptionSwap.GivenToken0AndLong0,
          amount: cache.swapAmount,
          data: data
        })
      );
    } else {
      ITimeswapV2Token(tokens).mint(
        TimeswapV2TokenMintParam({
          token0: cache.token0,
          token1: cache.token1,
          strike: param.strike,
          maturity: param.maturity,
          long0To: cache.isLong0 ? cache.longTo : address(this),
          long1To: cache.isLong0 ? address(this) : cache.longTo,
          shortTo: address(this),
          long0Amount: cache.isLong0 ? cache.positionAmount : 0,
          long1Amount: cache.isLong0 ? 0 : cache.positionAmount,
          shortAmount: 0,
          data: bytes("")
        })
      );

      data = timeswapV2PeripheryBorrowGivenPrincipalInternal(
        TimeswapV2PeripheryBorrowGivenPrincipalInternalParam({
          optionPair: msg.sender,
          token0: cache.token0,
          token1: cache.token1,
          strike: param.strike,
          maturity: param.maturity,
          isLong0: cache.isLong0,
          token0Amount: cache.isLong0 ? param.token0AndLong0Amount : 0,
          token1Amount: cache.isLong0 ? 0 : param.token1AndLong1Amount,
          positionAmount: cache.positionAmount,
          data: data
        })
      );

      data = abi.encode(cache.positionAmount, data);
    }
  }

  /// @notice the abstract implementation for TimeswapV2OptionSwapCallback
  /// @param param params for swapCallBack from TimeswapV2Option
  /// @return data data passed in bytes in the param passed back
  function timeswapV2OptionSwapCallback(
    TimeswapV2OptionSwapCallbackParam calldata param
  ) external override returns (bytes memory data) {
    address token0;
    address token1;
    address longTo;
    uint256 depositAmount;
    uint256 positionAmount;
    (token0, token1, longTo, depositAmount, positionAmount, data) = abi.decode(
      param.data,
      (address, address, address, uint256, uint256, bytes)
    );

    Verify.timeswapV2Option(optionFactory, token0, token1);

    positionAmount += param.isLong0ToLong1 ? param.token1AndLong1Amount : param.token0AndLong0Amount;

    ITimeswapV2Token(tokens).mint(
      TimeswapV2TokenMintParam({
        token0: token0,
        token1: token1,
        strike: param.strike,
        maturity: param.maturity,
        long0To: param.isLong0ToLong1 ? address(this) : longTo,
        long1To: param.isLong0ToLong1 ? longTo : address(this),
        shortTo: address(this),
        long0Amount: param.isLong0ToLong1 ? 0 : positionAmount,
        long1Amount: param.isLong0ToLong1 ? positionAmount : 0,
        shortAmount: 0,
        data: bytes("")
      })
    );

    data = timeswapV2PeripheryBorrowGivenPrincipalInternal(
      TimeswapV2PeripheryBorrowGivenPrincipalInternalParam({
        optionPair: msg.sender,
        token0: token0,
        token1: token1,
        strike: param.strike,
        maturity: param.maturity,
        isLong0: !param.isLong0ToLong1,
        token0Amount: param.token0AndLong0Amount + (param.isLong0ToLong1 ? 0 : depositAmount),
        token1Amount: param.token1AndLong1Amount + (param.isLong0ToLong1 ? depositAmount : 0),
        positionAmount: positionAmount,
        data: data
      })
    );

    data = abi.encode(positionAmount, data);
  }

  /// @notice the abstract implementation for TimeswapV2TokenMintCallback
  /// @param param params for mintCallBack from TimeswapV2Token
  /// @return data data passed in bytes in the param passed back
  function timeswapV2TokenMintCallback(
    TimeswapV2TokenMintCallbackParam calldata param
  ) external override returns (bytes memory data) {
    Verify.timeswapV2Token(tokens);

    address optionPair = OptionFactoryLibrary.get(optionFactory, param.token0, param.token1);

    ITimeswapV2Option(optionPair).transferPosition(
      param.strike,
      param.maturity,
      msg.sender,
      param.long0Amount != 0 ? TimeswapV2OptionPosition.Long0 : TimeswapV2OptionPosition.Long1,
      param.long0Amount != 0 ? param.long0Amount : param.long1Amount
    );

    data = bytes("");
  }

  /// @notice the implementation which is to be overriden for DEX/Aggregator specific logic for TimeswapV2BorrowGivenPrincipal
  /// @param param params for calling the implementation specfic borrowGivenPrincipal to be overriden
  /// @return data data passed in bytes in the param passed back
  function timeswapV2PeripheryBorrowGivenPrincipalInternal(
    TimeswapV2PeripheryBorrowGivenPrincipalInternalParam memory param
  ) internal virtual returns (bytes memory data);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @dev The different kind of mint transactions.
enum TimeswapV2PoolMint {
  GivenLiquidity,
  GivenLong,
  GivenShort,
  GivenLarger
}

/// @dev The different kind of burn transactions.
enum TimeswapV2PoolBurn {
  GivenLiquidity,
  GivenLong,
  GivenShort,
  GivenSmaller
}

/// @dev The different kind of deleverage transactions.
enum TimeswapV2PoolDeleverage {
  GivenDeltaSqrtInterestRate,
  GivenLong,
  GivenShort,
  GivenSum
}

/// @dev The different kind of leverage transactions.
enum TimeswapV2PoolLeverage {
  GivenDeltaSqrtInterestRate,
  GivenLong,
  GivenShort,
  GivenSum
}

/// @dev The different kind of rebalance transactions.
enum TimeswapV2PoolRebalance {
  GivenLong0,
  GivenLong1
}

library TransactionLibrary {
  /// @dev Reverts when the given type of transaction is invalid.
  error InvalidTransaction();

  /// @dev Function to revert with the error InvalidTransaction.
  function invalidTransaction() internal pure {
    revert InvalidTransaction();
  }

  /// @dev Sanity checks for the mint parameters.
  function check(TimeswapV2PoolMint transaction) internal pure {
    if (uint256(transaction) >= 4) revert InvalidTransaction();
  }

  /// @dev Sanity checks for the burn parameters.
  function check(TimeswapV2PoolBurn transaction) internal pure {
    if (uint256(transaction) >= 4) revert InvalidTransaction();
  }

  /// @dev Sanity checks for the deleverage parameters.
  function check(TimeswapV2PoolDeleverage transaction) internal pure {
    if (uint256(transaction) >= 4) revert InvalidTransaction();
  }

  /// @dev Sanity checks for the leverage parameters.
  function check(TimeswapV2PoolLeverage transaction) internal pure {
    if (uint256(transaction) >= 4) revert InvalidTransaction();
  }

  /// @dev Sanity checks for the rebalance parameters.
  function check(TimeswapV2PoolRebalance transaction) internal pure {
    if (uint256(transaction) >= 2) revert InvalidTransaction();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {TimeswapV2PoolLeverageChoiceCallbackParam, TimeswapV2PoolLeverageCallbackParam} from "../../structs/CallbackParam.sol";

/// @dev The interface that needs to be implemented by a contract calling the leverage function.
interface ITimeswapV2PoolLeverageCallback {
  /// @dev Returns the amount of long0 position and long1 positions chosen to be withdrawn.
  /// @notice The StrikeConversion.combine of long0 position and long1 position must be less than or equal to long amount.
  /// @dev The long0 positions and long1 positions will already be minted to the recipients.
  /// @return long0Amount Amount of long0 position to be withdrawn.
  /// @return long1Amount Amount of long1 position to be withdrawn.
  /// @param data The bytes of data to be sent to msg.sender.
  function timeswapV2PoolLeverageChoiceCallback(
    TimeswapV2PoolLeverageChoiceCallbackParam calldata param
  ) external returns (uint256 long0Amount, uint256 long1Amount, bytes memory data);

  /// @dev Require the transfer of short position into the pool.
  /// @param data The bytes of data to be sent to msg.sender.
  function timeswapV2PoolLeverageCallback(
    TimeswapV2PoolLeverageCallbackParam calldata param
  ) external returns (bytes memory data);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

interface IOwnableTwoSteps {
  /// @dev Emits when the pending owner is chosen.
  /// @param pendingOwner The new pending owner.
  event SetOwner(address pendingOwner);

  /// @dev Emits when the pending owner accepted and become the new owner.
  /// @param owner The new owner.
  event AcceptOwner(address owner);

  /// @dev The address of the current owner.
  /// @return address
  function owner() external view returns (address);

  /// @dev The address of the current pending owner.
  /// @notice The address can be zero which signifies no pending owner.
  /// @return address
  function pendingOwner() external view returns (address);

  /// @dev The owner sets the new pending owner.
  /// @notice Can only be called by the owner.
  /// @param chosenPendingOwner The newly chosen pending owner.
  function setPendingOwner(address chosenPendingOwner) external;

  /// @dev The pending owner accepts being the new owner.
  /// @notice Can only be called by the pending owner.
  function acceptOwner() external;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {StrikeAndMaturity} from "@timeswap-labs/v2-option/contracts/structs/StrikeAndMaturity.sol";

import {TimeswapV2PoolAddFeesParam, TimeswapV2PoolCollectParam, TimeswapV2PoolMintParam, TimeswapV2PoolBurnParam, TimeswapV2PoolDeleverageParam, TimeswapV2PoolLeverageParam, TimeswapV2PoolRebalanceParam} from "../structs/Param.sol";

/// @title An interface for Timeswap V2 Pool contract.
interface ITimeswapV2Pool {
  /// @dev Emits when liquidity position is transferred.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param from The sender of liquidity position.
  /// @param to The receipeint of liquidity position.
  /// @param liquidityAmount The amount of liquidity position transferred.
  event TransferLiquidity(
    uint256 indexed strike,
    uint256 indexed maturity,
    address from,
    address to,
    uint160 liquidityAmount
  );

  /// @dev Emits when fees is transferred.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param from The sender of fees position.
  /// @param to The receipeint of fees position.
  /// @param long0Fees The amount of long0 position fees transferred.
  /// @param long1Fees The amount of long1 position fees transferred.
  /// @param shortFees The amount of short position fees transferred.
  event TransferFees(
    uint256 indexed strike,
    uint256 indexed maturity,
    address from,
    address to,
    uint256 long0Fees,
    uint256 long1Fees,
    uint256 shortFees
  );

  /// @dev Emits when fees is added from a user.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param to The receipeint of fees position.
  /// @param long0Fees The amount of long0 position fees received.
  /// @param long1Fees The amount of long1 position fees received.
  /// @param shortFees The amount of short position fees received.
  event AddFees(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address to,
    uint256 long0Fees,
    uint256 long1Fees,
    uint256 shortFees
  );

  /// @dev Emits when protocol fees are withdrawn by the factory contract owner.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param caller The caller of the collectProtocolFees function.
  /// @param long0To The recipient of long0 position fees.
  /// @param long1To The recipient of long1 position fees.
  /// @param shortTo The recipient of short position fees.
  /// @param long0Amount The amount of long0 position fees withdrawn.
  /// @param long1Amount The amount of long1 position fees withdrawn.
  /// @param shortAmount The amount of short position fees withdrawn.
  event CollectProtocolFees(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address long0To,
    address long1To,
    address shortTo,
    uint256 long0Amount,
    uint256 long1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when transaction fees are withdrawn by a liquidity provider.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param caller The caller of the collectTransactionFees function.
  /// @param long0To The recipient of long0 position fees.
  /// @param long1To The recipient of long1 position fees.
  /// @param shortTo The recipient of short position fees.
  /// @param long0Amount The amount of long0 position fees withdrawn.
  /// @param long1Amount The amount of long1 position fees withdrawn.
  /// @param shortAmount The amount of short position fees withdrawn.
  event CollectTransactionFee(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address long0To,
    address long1To,
    address shortTo,
    uint256 long0Amount,
    uint256 long1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when the mint transaction is called.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param caller The caller of the mint function.
  /// @param to The recipient of liquidity positions.
  /// @param liquidityAmount The amount of liquidity positions minted.
  /// @param long0Amount The amount of long0 positions deposited.
  /// @param long1Amount The amount of long1 positions deposited.
  /// @param shortAmount The amount of short positions deposited.
  event Mint(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address to,
    uint160 liquidityAmount,
    uint256 long0Amount,
    uint256 long1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when the burn transaction is called.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param caller The caller of the burn function.
  /// @param long0To The recipient of long0 positions.
  /// @param long1To The recipient of long1 positions.
  /// @param shortTo The recipient of short positions.
  /// @param liquidityAmount The amount of liquidity positions burnt.
  /// @param long0Amount The amount of long0 positions withdrawn.
  /// @param long1Amount The amount of long1 positions withdrawn.
  /// @param shortAmount The amount of short positions withdrawn.
  event Burn(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address long0To,
    address long1To,
    address shortTo,
    uint160 liquidityAmount,
    uint256 long0Amount,
    uint256 long1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when deleverage transaction is called.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param caller The caller of the deleverage function.
  /// @param to The recipient of short positions.
  /// @param long0Amount The amount of long0 positions deposited.
  /// @param long1Amount The amount of long1 positions deposited.
  /// @param shortAmount The amount of short positions withdrawn.
  event Deleverage(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address to,
    uint256 long0Amount,
    uint256 long1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when leverage transaction is called.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param caller The caller of the leverage function.
  /// @param long0To The recipient of long0 positions.
  /// @param long1To The recipient of long1 positions.
  /// @param long0Amount The amount of long0 positions withdrawn.
  /// @param long1Amount The amount of long1 positions withdrawn.
  /// @param shortAmount The amount of short positions deposited.
  event Leverage(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address long0To,
    address long1To,
    uint256 long0Amount,
    uint256 long1Amount,
    uint256 shortAmount
  );

  /// @dev Emits when rebalance transaction is called.
  /// @param strike The strike of the option and pool.
  /// @param maturity The maturity of the option and pool.
  /// @param caller The caller of the rebalance function.
  /// @param to If isLong0ToLong1 then recipient of long0 positions, ekse recipient of long1 positions.
  /// @param isLong0ToLong1 Long0ToLong1 if true. Long1ToLong0 if false.
  /// @param long0Amount If isLong0ToLong1, amount of long0 positions deposited.
  /// If isLong1ToLong0, amount of long0 positions withdrawn.
  /// @param long1Amount If isLong0ToLong1, amount of long1 positions withdrawn.
  /// If isLong1ToLong0, amount of long1 positions deposited.
  event Rebalance(
    uint256 indexed strike,
    uint256 indexed maturity,
    address indexed caller,
    address to,
    bool isLong0ToLong1,
    uint256 long0Amount,
    uint256 long1Amount
  );

  error Quote();

  /* ===== VIEW ===== */

  /// @dev Returns the factory address that deployed this contract.
  function poolFactory() external view returns (address);

  /// @dev Returns the Timeswap V2 Option of the pair.
  function optionPair() external view returns (address);

  /// @dev Returns the transaction fee earned by the liquidity providers.
  function transactionFee() external view returns (uint256);

  /// @dev Returns the protocol fee earned by the protocol.
  function protocolFee() external view returns (uint256);

  /// @dev Get the strike and maturity of the pool in the pool enumeration list.
  /// @param id The chosen index.
  function getByIndex(uint256 id) external view returns (StrikeAndMaturity memory);

  /// @dev Get the number of pools being interacted.
  function numberOfPools() external view returns (uint256);

  /// @dev Returns the total amount of liquidity in the pool.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @return liquidityAmount The liquidity amount of the pool.
  function totalLiquidity(uint256 strike, uint256 maturity) external view returns (uint160 liquidityAmount);

  /// @dev Returns the square root of the interest rate of the pool.
  /// @dev the square root of interest rate is z/(x+y) where z is the short amount, x+y is the long0 amount, and y is the long1 amount.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @return rate The square root of the interest rate of the pool.
  function sqrtInterestRate(uint256 strike, uint256 maturity) external view returns (uint160 rate);

  /// @dev Returns the amount of liquidity owned by the given address.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @param owner The address to query the liquidity of.
  /// @return liquidityAmount The amount of liquidity owned by the given address.
  function liquidityOf(uint256 strike, uint256 maturity, address owner) external view returns (uint160 liquidityAmount);

  /// @dev It calculates the global fee growth, which is fee increased per unit of liquidity token.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @return long0FeeGrowth The global fee increased per unit of liquidity token for long0.
  /// @return long1FeeGrowth The global fee increased per unit of liquidity token for long1.
  /// @return  shortFeeGrowth The global fee increased per unit of liquidity token for short.
  function feeGrowth(
    uint256 strike,
    uint256 maturity
  ) external view returns (uint256 long0FeeGrowth, uint256 long1FeeGrowth, uint256 shortFeeGrowth);

  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @param owner The address to query the fees earned of.
  /// @return long0Fees The amount of long0 fees owned by the given address.
  /// @return long1Fees The amount of long1 fees owned by the given address.
  /// @return shortFees The amount of short fees owned by the given address.
  function feesEarnedOf(
    uint256 strike,
    uint256 maturity,
    address owner
  ) external view returns (uint256 long0Fees, uint256 long1Fees, uint256 shortFees);

  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @return long0ProtocolFees The amount of long0 protocol fees owned by the owner of the factory contract.
  /// @return long1ProtocolFees The amount of long1 protocol fees owned by the owner of the factory contract.
  /// @return shortProtocolFees The amount of short protocol fees owned by the owner of the factory contract.
  function protocolFeesEarned(
    uint256 strike,
    uint256 maturity
  ) external view returns (uint256 long0ProtocolFees, uint256 long1ProtocolFees, uint256 shortProtocolFees);

  /// @dev Returns the amount of long0 and long1 in the pool.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @return long0Amount The amount of long0 in the pool.
  /// @return long1Amount The amount of long1 in the pool.
  function totalLongBalance(
    uint256 strike,
    uint256 maturity
  ) external view returns (uint256 long0Amount, uint256 long1Amount);

  /// @dev Returns the amount of long0 and long1 adjusted for the protocol and transaction fee.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @return long0Amount The amount of long0 in the pool, adjusted for the protocol and transaction fee.
  /// @return long1Amount The amount of long1 in the pool, adjusted for the protocol and transaction fee.
  function totalLongBalanceAdjustFees(
    uint256 strike,
    uint256 maturity
  ) external view returns (uint256 long0Amount, uint256 long1Amount);

  /// @dev Returns the amount of sum of long0 and long1 converted to base denomination in the pool.
  /// @dev Returns the amount of short positions in the pool.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @return longAmount The amount of sum of long0 and long1 converted to base denomination in the pool.
  /// @return shortAmount The amount of short in the pool.
  function totalPositions(
    uint256 strike,
    uint256 maturity
  ) external view returns (uint256 longAmount, uint256 shortAmount);

  /* ===== UPDATE ===== */

  /// @dev Updates the fee growth.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  function update(uint256 strike, uint256 maturity) external;

  /// @dev Transfer liquidity positions to another address.
  /// @notice Does not transfer the transaction fees earned by the sender.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @param to The recipient of the liquidity positions.
  /// @param liquidityAmount The amount of liquidity positions transferred
  function transferLiquidity(uint256 strike, uint256 maturity, address to, uint160 liquidityAmount) external;

  /// @dev Transfer fees earned of the sender to another address.
  /// @notice Does not transfer the liquidity positions of the sender.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @param to The recipient of the transaction fees.
  /// @param long0Fees The amount of long0 position fees transferred.
  /// @param long1Fees The amount of long1 position fees transferred.
  /// @param shortFees The amount of short position fees transferred.
  function transferFees(
    uint256 strike,
    uint256 maturity,
    address to,
    uint256 long0Fees,
    uint256 long1Fees,
    uint256 shortFees
  ) external;

  /// @dev Transfer long0, long1, and/or short to fees storage.
  /// @param param The parameters of addFees.
  /// @return data the data used for the callbacks.
  function addFees(TimeswapV2PoolAddFeesParam calldata param) external returns (bytes memory data);

  /// @dev initializes the pool with the given parameters.
  /// @param strike The strike price of the pool.
  /// @param maturity The maturity of the pool.
  /// @param rate The square root of the interest rate of the pool.
  function initialize(uint256 strike, uint256 maturity, uint160 rate) external;

  /// @dev Collects the protocol fees of the pool.
  /// @dev only protocol owner can call this function.
  /// @dev if the owner enters an amount which is greater than the fee amount they have earned, withdraw only the amount they have.
  /// @param param The parameters of the collectProtocolFees.
  /// @return long0Amount The amount of long0 collected.
  /// @return long1Amount The amount of long1 collected.
  /// @return shortAmount The amount of short collected.
  function collectProtocolFees(
    TimeswapV2PoolCollectParam calldata param
  ) external returns (uint256 long0Amount, uint256 long1Amount, uint256 shortAmount);

  /// @dev Collects the transaction fees of the pool.
  /// @dev only liquidity provider can call this function.
  /// @dev if the owner enters an amount which is greater than the fee amount they have earned, withdraw only the amount they have.
  /// @param param The parameters of the collectTransactionFee.
  /// @return long0Amount The amount of long0 collected.
  /// @return long1Amount The amount of long1 collected.
  /// @return shortAmount The amount of short collected.
  function collectTransactionFees(
    TimeswapV2PoolCollectParam calldata param
  ) external returns (uint256 long0Amount, uint256 long1Amount, uint256 shortAmount);

  /// @dev deposit Short and Long tokens and mints Liquidity
  /// @dev can be only called before the maturity.
  /// @param param it is a struct that contains the parameters of the mint function
  /// @return liquidityAmount The amount of liquidity minted.
  /// @return long0Amount The amount of long0 deposited.
  /// @return long1Amount The amount of long1 deposited.
  /// @return shortAmount The amount of short deposited.
  /// @return data the data used for the callbacks.
  function mint(
    TimeswapV2PoolMintParam calldata param
  )
    external
    returns (uint160 liquidityAmount, uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev deposit Short and Long tokens and mints Liquidity
  /// @dev can be only called before the maturity.
  /// @notice Will always revert with error Quote after the final callback.
  /// @param param it is a struct that contains the parameters of the mint function.
  /// @param durationForward The duration of time moved forward.
  /// @return liquidityAmount The amount of liquidity minted.
  /// @return long0Amount The amount of long0 deposited.
  /// @return long1Amount The amount of long1 deposited.
  /// @return shortAmount The amount of short deposited.
  /// @return data the data used for the callbacks.
  function mint(
    TimeswapV2PoolMintParam calldata param,
    uint96 durationForward
  )
    external
    returns (uint160 liquidityAmount, uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev burn Liquidity and receive Short and Long tokens
  /// @dev can be only called before the maturity.
  /// @dev after the maturity of the pool, the long0 and long1 tokens are zero. And the short tokens are added into the transaction fees.
  /// @dev if the user wants to burn the liquidity after the maturity, they should call the collectTransactionFee function.
  /// @param param it is a struct that contains the parameters of the burn function
  /// @return liquidityAmount The amount of liquidity burned.
  /// @return long0Amount The amount of long0 withdrawn.
  /// @return long1Amount The amount of long1 withdrawn.
  /// @return shortAmount The amount of short withdrawn.
  /// @return data the data used for the callbacks.
  function burn(
    TimeswapV2PoolBurnParam calldata param
  )
    external
    returns (uint160 liquidityAmount, uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev burn Liquidity and receive Short and Long tokens
  /// @dev can be only called before the maturity.
  /// @dev after the maturity of the pool, the long0 and long1 tokens are zero. And the short tokens are added into the transaction fees.
  /// @dev if the user wants to burn the liquidity after the maturity, they should call the collectTransactionFee function.
  /// @notice Will always revert with error Quote after the final callback.
  /// @param param it is a struct that contains the parameters of the burn function.
  /// @param durationForward The duration of time moved forward.
  /// @return liquidityAmount The amount of liquidity burned.
  /// @return long0Amount The amount of long0 withdrawn.
  /// @return long1Amount The amount of long1 withdrawn.
  /// @return shortAmount The amount of short withdrawn.
  /// @return data the data used for the callbacks.
  function burn(
    TimeswapV2PoolBurnParam calldata param,
    uint96 durationForward
  )
    external
    returns (uint160 liquidityAmount, uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev deposit Long tokens and receive Short tokens
  /// @dev can be only called before the maturity.
  /// @param param it is a struct that contains the parameters of the deleverage function
  /// @return long0Amount The amount of long0 deposited.
  /// @return long1Amount The amount of long1 deposited.
  /// @return shortAmount The amount of short received.
  /// @return data the data used for the callbacks.
  function deleverage(
    TimeswapV2PoolDeleverageParam calldata param
  ) external returns (uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev deposit Long tokens and receive Short tokens
  /// @dev can be only called before the maturity.
  /// @notice Will always revert with error Quote after the final callback.
  /// @param param it is a struct that contains the parameters of the deleverage function.
  /// @param durationForward The duration of time moved forward.
  /// @return long0Amount The amount of long0 deposited.
  /// @return long1Amount The amount of long1 deposited.
  /// @return shortAmount The amount of short received.
  /// @return data the data used for the callbacks.
  function deleverage(
    TimeswapV2PoolDeleverageParam calldata param,
    uint96 durationForward
  ) external returns (uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev deposit Short tokens and receive Long tokens
  /// @dev can be only called before the maturity.
  /// @param param it is a struct that contains the parameters of the leverage function.
  /// @return long0Amount The amount of long0 received.
  /// @return long1Amount The amount of long1 received.
  /// @return shortAmount The amount of short deposited.
  /// @return data the data used for the callbacks.
  function leverage(
    TimeswapV2PoolLeverageParam calldata param
  ) external returns (uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev deposit Short tokens and receive Long tokens
  /// @dev can be only called before the maturity.
  /// @notice Will always revert with error Quote after the final callback.
  /// @param param it is a struct that contains the parameters of the leverage function.
  /// @param durationForward The duration of time moved forward.
  /// @return long0Amount The amount of long0 received.
  /// @return long1Amount The amount of long1 received.
  /// @return shortAmount The amount of short deposited.
  /// @return data the data used for the callbacks.
  function leverage(
    TimeswapV2PoolLeverageParam calldata param,
    uint96 durationForward
  ) external returns (uint256 long0Amount, uint256 long1Amount, uint256 shortAmount, bytes memory data);

  /// @dev Deposit Long0 to receive Long1 or deposit Long1 to receive Long0.
  /// @dev can be only called before the maturity.
  /// @param param it is a struct that contains the parameters of the rebalance function
  /// @return long0Amount The amount of long0 received/deposited.
  /// @return long1Amount The amount of long1 deposited/received.
  /// @return data the data used for the callbacks.
  function rebalance(
    TimeswapV2PoolRebalanceParam calldata param
  ) external returns (uint256 long0Amount, uint256 long1Amount, bytes memory data);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

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

/// @title The interface for the contract that deploys Timeswap V2 Pool pair contracts
/// @notice The Timeswap V2 Pool Factory facilitates creation of Timeswap V2 Pool pair.
interface ITimeswapV2PoolFactory is IOwnableTwoSteps {
  /* ===== EVENT ===== */

  /// @dev Emits when a new Timeswap V2 Pool contract is created.
  /// @param caller The address of the caller of create function.
  /// @param option The address of the option contract used by the pool.
  /// @param poolPair The address of the Timeswap V2 Pool contract created.
  event Create(address caller, address option, address poolPair);

  /* ===== VIEW ===== */

  /// @dev Returns the fixed transaction fee used by all created Timeswap V2 Pool contract.
  function transactionFee() external view returns (uint256);

  /// @dev Returns the fixed protocol fee used by all created Timeswap V2 Pool contract.
  function protocolFee() external view returns (uint256);

  /// @dev Returns the address of a Timeswap V2 Pool.
  /// @dev Returns a zero address if the Timeswap V2 Pool does not exist.
  /// @param option The address of the option contract used by the pool.
  /// @return poolPair The address of the Timeswap V2 Pool contract or a zero address.
  function get(address option) external view returns (address poolPair);

  function getByIndex(uint256 id) external view returns (address optionPair);

  function numberOfPairs() external view returns (uint256);

  /* ===== UPDATE ===== */

  /// @dev Creates a Timeswap V2 Pool based on option parameter.
  /// @dev Cannot create a duplicate Timeswap V2 Pool with the same option parameter.
  /// @param option The address of the option contract used by the pool.
  /// @param poolPair The address of the Timeswap V2 Pool contract created.
  function create(address option) external returns (address poolPair);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {OptionFactoryLibrary} from "@timeswap-labs/v2-option/contracts/libraries/OptionFactory.sol";

import {ITimeswapV2PoolFactory} from "../interfaces/ITimeswapV2PoolFactory.sol";

import {Error} from "@timeswap-labs/v2-library/contracts/Error.sol";

/// @title library for calculating poolFactory functions
/// @author Timeswap Labs
library PoolFactoryLibrary {
  using OptionFactoryLibrary for address;

  /// @dev Reverts when pool factory address is zero.
  error ZeroFactoryAddress();

  /// @dev Checks if the pool factory address is zero.
  /// @param poolFactory The pool factory address which is needed to be checked.
  function checkNotZeroFactory(address poolFactory) internal pure {
    if (poolFactory == address(0)) revert ZeroFactoryAddress();
  }

  /// @dev Get the option pair address and pool pair address.
  /// @param optionFactory The option factory contract address.
  /// @param poolFactory The pool factory contract address.
  /// @param token0 The address of the smaller address ERC20 token contract.
  /// @param token1 The address of the larger address ERC20 token contract.
  /// @return optionPair The retrieved option pair address. Zero address if not deployed.
  /// @return poolPair The retrieved pool pair address. Zero address if not deployed.
  function get(
    address optionFactory,
    address poolFactory,
    address token0,
    address token1
  ) internal view returns (address optionPair, address poolPair) {
    optionPair = optionFactory.get(token0, token1);

    poolPair = ITimeswapV2PoolFactory(poolFactory).get(optionPair);
  }

  /// @dev Get the option pair address and pool pair address.
  /// @notice Reverts when the option or the pool is not deployed.
  /// @param optionFactory The option factory contract address.
  /// @param poolFactory The pool factory contract address.
  /// @param token0 The address of the smaller address ERC20 token contract.
  /// @param token1 The address of the larger address ERC20 token contract.
  /// @return optionPair The retrieved option pair address.
  /// @return poolPair The retrieved pool pair address.
  function getWithCheck(
    address optionFactory,
    address poolFactory,
    address token0,
    address token1
  ) internal view returns (address optionPair, address poolPair) {
    optionPair = optionFactory.getWithCheck(token0, token1);

    poolPair = ITimeswapV2PoolFactory(poolFactory).get(optionPair);

    if (poolPair == address(0)) Error.zeroAddress();
  }
}

File 55 of 69 : CallbackParam.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @dev The parameters for the add fees callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0Fees The amount of long0 position required by the pool from msg.sender.
/// @param long1Fees The amount of long1 position required by the pool from msg.sender.
/// @param shortFees The amount of short position required by the pool from msg.sender.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolAddFeesCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 long0Fees;
  uint256 long1Fees;
  uint256 shortFees;
  bytes data;
}

/// @dev The parameters for the mint choice callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param longAmount The amount of long position in base denomination required by the pool from msg.sender.
/// @param shortAmount The amount of short position required by the pool from msg.sender.
/// @param liquidityAmount The amount of liquidity position minted.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolMintChoiceCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 longAmount;
  uint256 shortAmount;
  uint160 liquidityAmount;
  bytes data;
}

/// @dev The parameters for the mint callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0Amount The amount of long0 position required by the pool from msg.sender.
/// @param long1Amount The amount of long1 position required by the pool from msg.sender.
/// @param shortAmount The amount of short position required by the pool from msg.sender.
/// @param liquidityAmount The amount of liquidity position minted.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolMintCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  uint160 liquidityAmount;
  bytes data;
}

/// @dev The parameters for the burn choice callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0Balance The amount of long0 position that can be withdrawn from the pool.
/// @param long1Balance The amount of long1 position that can be withdrawn from the pool.
/// @param longAmount The amount of long position in base denomination that will be withdrawn.
/// @param shortAmount The amount of short position that will be withdrawn.
/// @param liquidityAmount The amount of liquidity position burnt.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolBurnChoiceCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 long0Balance;
  uint256 long1Balance;
  uint256 longAmount;
  uint256 shortAmount;
  uint160 liquidityAmount;
  bytes data;
}

/// @dev The parameters for the burn callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0Amount The amount of long0 position that will be withdrawn.
/// @param long1Amount The amount of long1 position that will be withdrawn.
/// @param shortAmount The amount of short position that will be withdrawn.
/// @param liquidityAmount The amount of liquidity position burnt.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolBurnCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  uint160 liquidityAmount;
  bytes data;
}

/// @dev The parameters for the deleverage choice callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0Amount The amount of long0 position required by the pool from msg.sender.
/// @param long1Amount The amount of long1 position required by the pool from msg.sender.
/// @param shortAmount The amount of short position that will be withdrawn.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolDeleverageChoiceCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 longAmount;
  uint256 shortAmount;
  bytes data;
}

/// @dev The parameters for the deleverage callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param longAmount The amount of long position in base denomination required by the pool from msg.sender.
/// @param shortAmount The amount of short position that will be withdrawn.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolDeleverageCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @dev The parameters for the leverage choice callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0Balance The amount of long0 position that can be withdrawn from the pool.
/// @param long1Balance The amount of long1 position that can be withdrawn from the pool.
/// @param longAmount The amount of long position in base denomination that will be withdrawn.
/// @param shortAmount The amount of short position required by the pool from msg.sender.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolLeverageChoiceCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 long0Balance;
  uint256 long1Balance;
  uint256 longAmount;
  uint256 shortAmount;
  bytes data;
}

/// @dev The parameters for the leverage choice callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0Amount The amount of long0 position that can be withdrawn.
/// @param long1Amount The amount of long1 position that can be withdrawn.
/// @param shortAmount The amount of short position required by the pool from msg.sender.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolLeverageCallbackParam {
  uint256 strike;
  uint256 maturity;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @dev The parameters for the rebalance callback.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param isLong0ToLong1 Long0ToLong1 when true. Long1ToLong0 when false.
/// @param long0Amount When Long0ToLong1, the amount of long0 position required by the pool from msg.sender.
/// When Long1ToLong0, the amount of long0 position that can be withdrawn.
/// @param long1Amount When Long0ToLong1, the amount of long1 position that can be withdrawn.
/// When Long1ToLong0, the amount of long1 position required by the pool from msg.sender.
/// @param data The bytes of data to be sent to msg.sender.
struct TimeswapV2PoolRebalanceCallbackParam {
  uint256 strike;
  uint256 maturity;
  bool isLong0ToLong1;
  uint256 long0Amount;
  uint256 long1Amount;
  bytes data;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {Error} from "@timeswap-labs/v2-library/contracts/Error.sol";

import {TimeswapV2PoolMint, TimeswapV2PoolBurn, TimeswapV2PoolDeleverage, TimeswapV2PoolLeverage, TimeswapV2PoolRebalance, TransactionLibrary} from "../enums/Transaction.sol";

/// @dev The parameter for addFees functions.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param to The owner of the fees received.
/// @param long0Fees The amount of long0 position to be received.
/// @param long1Fees The amount of long1 position to be received.
/// @param shortFees The amount of short position to be received.
/// @param data The data to be sent to the function, which will go to the add fees callback.
struct TimeswapV2PoolAddFeesParam {
  uint256 strike;
  uint256 maturity;
  address to;
  uint256 long0Fees;
  uint256 long1Fees;
  uint256 shortFees;
  bytes data;
}

/// @dev The parameter for collectTransactionFees and collectProtocolFees functions.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0To The recipient of long0 positions.
/// @param long1To The recipient of long1 positions.
/// @param shortTo The recipient of short positions.
/// @param long0Requested The maximum amount of long0 positions wanted.
/// @param long1Requested The maximum amount of long1 positions wanted.
/// @param shortRequested The maximum amount of short positions wanted.
struct TimeswapV2PoolCollectParam {
  uint256 strike;
  uint256 maturity;
  address long0To;
  address long1To;
  address shortTo;
  uint256 long0Requested;
  uint256 long1Requested;
  uint256 shortRequested;
}

/// @dev The parameter for mint function.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param to The recipient of liquidity positions.
/// @param transaction The type of mint transaction, more information in Transaction module.
/// @param delta If transaction is GivenLiquidity, the amount of liquidity minted. Note that this value must be uint160.
/// If transaction is GivenLong, the amount of long position in base denomination to be deposited.
/// If transaction is GivenShort, the amount of short position to be deposited.
/// @param data The data to be sent to the function, which will go to the mint choice callback.
struct TimeswapV2PoolMintParam {
  uint256 strike;
  uint256 maturity;
  address to;
  TimeswapV2PoolMint transaction;
  uint256 delta;
  bytes data;
}

/// @dev The parameter for burn function.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0To The recipient of long0 positions.
/// @param long1To The recipient of long1 positions.
/// @param shortTo The recipient of short positions.
/// @param transaction The type of burn transaction, more information in Transaction module.
/// @param delta If transaction is GivenLiquidity, the amount of liquidity burnt. Note that this value must be uint160.
/// If transaction is GivenLong, the amount of long position in base denomination to be withdrawn.
/// If transaction is GivenShort, the amount of short position to be withdrawn.
/// @param data The data to be sent to the function, which will go to the burn choice callback.
struct TimeswapV2PoolBurnParam {
  uint256 strike;
  uint256 maturity;
  address long0To;
  address long1To;
  address shortTo;
  TimeswapV2PoolBurn transaction;
  uint256 delta;
  bytes data;
}

/// @dev The parameter for deleverage function.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param to The recipient of short positions.
/// @param transaction The type of deleverage transaction, more information in Transaction module.
/// @param delta If transaction is GivenDeltaSqrtInterestRate, the decrease in square root interest rate.
/// If transaction is GivenLong, the amount of long position in base denomination to be deposited.
/// If transaction is GivenShort, the amount of short position to be withdrawn.
/// If transaction is  GivenSum, the sum amount of long position in base denomination to be deposited, and short position to be withdrawn.
/// @param data The data to be sent to the function, which will go to the deleverage choice callback.
struct TimeswapV2PoolDeleverageParam {
  uint256 strike;
  uint256 maturity;
  address to;
  TimeswapV2PoolDeleverage transaction;
  uint256 delta;
  bytes data;
}

/// @dev The parameter for leverage function.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param long0To The recipient of long0 positions.
/// @param long1To The recipient of long1 positions.
/// @param transaction The type of leverage transaction, more information in Transaction module.
/// @param delta If transaction is GivenDeltaSqrtInterestRate, the increase in square root interest rate.
/// If transaction is GivenLong, the amount of long position in base denomination to be withdrawn.
/// If transaction is GivenShort, the amount of short position to be deposited.
/// If transaction is  GivenSum, the sum amount of long position in base denomination to be withdrawn, and short position to be deposited.
/// @param data The data to be sent to the function, which will go to the leverage choice callback.
struct TimeswapV2PoolLeverageParam {
  uint256 strike;
  uint256 maturity;
  address long0To;
  address long1To;
  TimeswapV2PoolLeverage transaction;
  uint256 delta;
  bytes data;
}

/// @dev The parameter for rebalance function.
/// @param strike The strike price of the pool.
/// @param maturity The maturity of the pool.
/// @param to When Long0ToLong1, the recipient of long1 positions.
/// When Long1ToLong0, the recipient of long0 positions.
/// @param isLong0ToLong1 Long0ToLong1 when true. Long1ToLong0 when false.
/// @param transaction The type of rebalance transaction, more information in Transaction module.
/// @param delta If transaction is GivenLong0 and Long0ToLong1, the amount of long0 positions to be deposited.
/// If transaction is GivenLong0 and Long1ToLong0, the amount of long1 positions to be withdrawn.
/// If transaction is GivenLong1 and Long0ToLong1, the amount of long1 positions to be withdrawn.
/// If transaction is GivenLong1 and Long1ToLong0, the amount of long1 positions to be deposited.
/// @param data The data to be sent to the function, which will go to the rebalance callback.
struct TimeswapV2PoolRebalanceParam {
  uint256 strike;
  uint256 maturity;
  address to;
  bool isLong0ToLong1;
  TimeswapV2PoolRebalance transaction;
  uint256 delta;
  bytes data;
}

library ParamLibrary {
  /// @dev Sanity checks
  /// @param param the parameter for addFees transaction.
  function check(TimeswapV2PoolAddFeesParam memory param) internal pure {
    if (param.to == address(0)) Error.zeroAddress();
    if (param.long0Fees == 0 && param.long1Fees == 0 && param.shortFees == 0) Error.zeroInput();
    if (param.strike == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for collectTransactionFees or collectProtocolFees transaction.
  function check(TimeswapV2PoolCollectParam memory param) internal pure {
    if (param.long0To == address(0) || param.long1To == address(0) || param.shortTo == address(0)) Error.zeroAddress();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.long0Requested == 0 && param.long1Requested == 0 && param.shortRequested == 0 && param.strike == 0)
      Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for mint transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2PoolMintParam memory param, uint96 blockTimestamp) internal pure {
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.to == address(0)) Error.zeroAddress();
    TransactionLibrary.check(param.transaction);
    if (param.delta == 0 || param.strike == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for burn transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2PoolBurnParam memory param, uint96 blockTimestamp) internal pure {
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.long0To == address(0) || param.long1To == address(0) || param.shortTo == address(0)) Error.zeroAddress();

    TransactionLibrary.check(param.transaction);
    if (param.delta == 0 || param.strike == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for deleverage transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2PoolDeleverageParam memory param, uint96 blockTimestamp) internal pure {
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.to == address(0)) Error.zeroAddress();
    TransactionLibrary.check(param.transaction);
    if (param.delta == 0 || param.strike == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for leverage transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2PoolLeverageParam memory param, uint96 blockTimestamp) internal pure {
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.long0To == address(0) || param.long1To == address(0)) Error.zeroAddress();

    TransactionLibrary.check(param.transaction);
    if (param.delta == 0 || param.strike == 0) Error.zeroInput();
  }

  /// @dev Sanity checks
  /// @param param the parameter for rebalance transaction.
  /// @param blockTimestamp the current block timestamp.
  function check(TimeswapV2PoolRebalanceParam memory param, uint96 blockTimestamp) internal pure {
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.maturity < blockTimestamp) Error.alreadyMatured(param.maturity, blockTimestamp);
    if (param.to == address(0)) Error.zeroAddress();
    TransactionLibrary.check(param.transaction);
    if (param.delta == 0 || param.strike == 0) Error.zeroInput();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {TimeswapV2TokenMintCallbackParam} from "../../structs/CallbackParam.sol";

interface ITimeswapV2TokenMintCallback {
  /// @dev Callback for `ITimeswapV2Token.mint`
  function timeswapV2TokenMintCallback(
    TimeswapV2TokenMintCallbackParam calldata param
  ) external returns (bytes memory data);
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";

import {TimeswapV2TokenPosition} from "../structs/Position.sol";
import {TimeswapV2TokenMintParam, TimeswapV2TokenBurnParam} from "../structs/Param.sol";

/// @title An interface for TS-V2 token system
/// @notice This interface is used to interact with TS-V2 positions
interface ITimeswapV2Token is IERC1155 {
  /// @dev Returns the factory address that deployed this contract.
  function optionFactory() external view returns (address);

  /// @dev Returns the position Balance of the owner
  /// @param owner The owner of the token
  /// @param position type of option position (long0, long1, short)
  function positionOf(address owner, TimeswapV2TokenPosition calldata position) external view returns (uint256 amount);

  /// @dev Transfers position token TimeswapV2Token from `from` to `to`
  /// @param from The address to transfer position token from
  /// @param to The address to transfer position token to
  /// @param position The TimeswapV2Token Position to transfer
  /// @param amount The amount of TimeswapV2Token Position to transfer
  function transferTokenPositionFrom(
    address from,
    address to,
    TimeswapV2TokenPosition calldata position,
    uint256 amount
  ) external;

  /// @dev mints TimeswapV2Token as per postion and amount
  /// @param param The TimeswapV2TokenMintParam
  /// @return data Arbitrary data
  function mint(TimeswapV2TokenMintParam calldata param) external returns (bytes memory data);

  /// @dev burns TimeswapV2Token as per postion and amount
  /// @param param The TimeswapV2TokenBurnParam
  function burn(TimeswapV2TokenBurnParam calldata param) external returns (bytes memory data);
}

File 59 of 69 : CallbackParam.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

/// @dev parameter for minting Timeswap V2 Tokens
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param long0Amount The amount of long0 deposited.
/// @param long1Amount The amount of long1 deposited.
/// @param shortAmount The amount of short deposited.
/// @param data Arbitrary data passed to the callback.
struct TimeswapV2TokenMintCallbackParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @dev parameter for burning Timeswap V2 Tokens
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param long0Amount The amount of long0 withdrawn.
/// @param long1Amount The amount of long1 withdrawn.
/// @param shortAmount The amount of short withdrawn.
/// @param data Arbitrary data passed to the callback, initalize as empty if not required.
struct TimeswapV2TokenBurnCallbackParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param liquidity The amount of liquidity increase.
/// @param data data
struct TimeswapV2LiquidityTokenMintCallbackParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint160 liquidityAmount;
  bytes data;
}

/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param liquidity The amount of liquidity decrease.
/// @param data data
struct TimeswapV2LiquidityTokenBurnCallbackParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint160 liquidityAmount;
  bytes data;
}

/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param long0Fees The amount of long0 required by the contract from msg.sender.
/// @param long1Fees The amount of long1 required by the contract from msg.sender.
/// @param shortFees The amount of short required by the contract from msg.sender.
/// @param data data
struct TimeswapV2LiquidityTokenAddFeesCallbackParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 long0Fees;
  uint256 long1Fees;
  uint256 shortFees;
  bytes data;
}

/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param long0Fees The amount of long0 fees withdrawn.
/// @param long1Fees The amount of long1 fees withdrawn.
/// @param shortFees The amount of short fees withdrawn.
/// @param data data
struct TimeswapV2LiquidityTokenCollectCallbackParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  uint256 long0Fees;
  uint256 long1Fees;
  uint256 shortFees;
  bytes data;
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {Error} from "@timeswap-labs/v2-library/contracts/Error.sol";

/// @dev parameter for minting Timeswap V2 Tokens
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param long0To The address of the recipient of TimeswapV2Token representing long0 position.
/// @param long1To The address of the recipient of TimeswapV2Token representing long1 position.
/// @param shortTo The address of the recipient of TimeswapV2Token representing short position.
/// @param long0Amount The amount of long0 deposited.
/// @param long1Amount The amount of long1 deposited.
/// @param shortAmount The amount of short deposited.
/// @param data Arbitrary data passed to the callback.
struct TimeswapV2TokenMintParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address long0To;
  address long1To;
  address shortTo;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @dev parameter for burning Timeswap V2 Tokens
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param long0To  The address of the recipient of long token0 position.
/// @param long1To The address of the recipient of long token1 position.
/// @param shortTo The address of the recipient of short position.
/// @param long0Amount  The amount of TimeswapV2Token long0  deposited and equivalent long0 position is withdrawn.
/// @param long1Amount The amount of TimeswapV2Token long1 deposited and equivalent long1 position is withdrawn.
/// @param shortAmount The amount of TimeswapV2Token short deposited and equivalent short position is withdrawn,
/// @param data Arbitrary data passed to the callback, initalize as empty if not required.
struct TimeswapV2TokenBurnParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address long0To;
  address long1To;
  address shortTo;
  uint256 long0Amount;
  uint256 long1Amount;
  uint256 shortAmount;
  bytes data;
}

/// @dev parameter for minting Timeswap V2 Liquidity Tokens
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param to The address of the recipient of TimeswapV2LiquidityToken.
/// @param liquidityAmount The amount of liquidity token deposited.
/// @param data Arbitrary data passed to the callback.
struct TimeswapV2LiquidityTokenMintParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address to;
  uint160 liquidityAmount;
  bytes data;
}

/// @dev parameter for burning Timeswap V2 Liquidity Tokens
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param to The address of the recipient of the liquidity token.
/// @param liquidityAmount The amount of liquidity token withdrawn.
/// @param data Arbitrary data passed to the callback, initalize as empty if not required.
struct TimeswapV2LiquidityTokenBurnParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address to;
  uint160 liquidityAmount;
  bytes data;
}

/// @dev parameter for adding long0, long1, and short into fees storage.
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param to The address of the recipient of the fees.
/// @param long0Fees The amount of long0 to be deposited.
/// @param long1Fees The amount of long1 to be deposited.
/// @param shortFees The amount of short to be deposited.
/// @param data Arbitrary data passed to the callback, initalize as empty if not required.
struct TimeswapV2LiquidityTokenAddFeesParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address to;
  uint256 long0Fees;
  uint256 long1Fees;
  uint256 shortFees;
  bytes data;
}

/// @dev parameter for collecting fees from Timeswap V2 Liquidity Tokens
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param to The address of the recipient of the fees.
/// @param long0FeesDesired The maximum amount of long0Fees desired to be withdrawn.
/// @param long1FeesDesired The maximum amount of long1Fees desired to be withdrawn.
/// @param shortFeesDesired The maximum amount of shortFees desired to be withdrawn.
/// @param data Arbitrary data passed to the callback, initalize as empty if not required.
struct TimeswapV2LiquidityTokenCollectParam {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  address to;
  uint256 long0FeesDesired;
  uint256 long1FeesDesired;
  uint256 shortFeesDesired;
  bytes data;
}

library ParamLibrary {
  /// @dev Sanity checks for token mint.
  function check(TimeswapV2TokenMintParam memory param) internal pure {
    if (param.long0To == address(0) || param.long1To == address(0) || param.shortTo == address(0)) Error.zeroAddress();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.long0Amount == 0 && param.long1Amount == 0 && param.shortAmount == 0) Error.zeroInput();
  }

  /// @dev Sanity checks for token burn.
  function check(TimeswapV2TokenBurnParam memory param) internal pure {
    if (param.long0To == address(0) || param.long1To == address(0) || param.shortTo == address(0)) Error.zeroAddress();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.long0Amount == 0 && param.long1Amount == 0 && param.shortAmount == 0) Error.zeroInput();
  }

  /// @dev Sanity checks for liquidity token mint.
  function check(TimeswapV2LiquidityTokenMintParam memory param) internal pure {
    if (param.to == address(0)) Error.zeroAddress();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.liquidityAmount == 0) Error.zeroInput();
  }

  /// @dev Sanity checks for liquidity token burn.
  function check(TimeswapV2LiquidityTokenBurnParam memory param) internal pure {
    if (param.to == address(0)) Error.zeroAddress();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.liquidityAmount == 0) Error.zeroInput();
  }

  /// @dev Sanity checks for liquidity add fees.
  function check(TimeswapV2LiquidityTokenAddFeesParam memory param) internal pure {
    if (param.to == address(0)) Error.zeroAddress();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.long0Fees == 0 && param.long1Fees == 0 && param.shortFees == 0) Error.zeroInput();
  }

  /// @dev Sanity checks for liquidity token collect.
  function check(TimeswapV2LiquidityTokenCollectParam memory param) internal pure {
    if (param.to == address(0)) Error.zeroAddress();
    if (param.maturity > type(uint96).max) Error.incorrectMaturity(param.maturity);
    if (param.long0FeesDesired == 0 && param.long1FeesDesired == 0 && param.shortFeesDesired == 0) Error.zeroInput();
  }
}

// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.8;

import {TimeswapV2OptionPosition} from "@timeswap-labs/v2-option/contracts/enums/Position.sol";

/// @dev Struct for Token
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
/// @param position The position of the option.
struct TimeswapV2TokenPosition {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
  TimeswapV2OptionPosition position;
}

/// @dev Struct for Liquidity Token
/// @param token0 The first ERC20 token address of the pair.
/// @param token1 The second ERC20 token address of the pair.
/// @param strike  The strike ratio of token1 per token0 of the option.
/// @param maturity The maturity of the option.
struct TimeswapV2LiquidityTokenPosition {
  address token0;
  address token1;
  uint256 strike;
  uint256 maturity;
}

library PositionLibrary {
  /// @dev return keccak for key management for Token.
  function toKey(TimeswapV2TokenPosition memory timeswapV2TokenPosition) internal pure returns (bytes32) {
    return keccak256(abi.encode(timeswapV2TokenPosition));
  }

  /// @dev return keccak for key management for Liquidity Token.
  function toKey(
    TimeswapV2LiquidityTokenPosition memory timeswapV2LiquidityTokenPosition
  ) internal pure returns (bytes32) {
    return keccak256(abi.encode(timeswapV2LiquidityTokenPosition));
  }
}

File 62 of 69 : 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: 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.5.0;

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

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

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

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

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

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

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

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

/// @title Pool state that 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 Math library for liquidity
library LiquidityMath {
    /// @notice Add a signed liquidity delta to liquidity and revert if it overflows or underflows
    /// @param x The liquidity before change
    /// @param y The delta by which liquidity should be changed
    /// @return z The liquidity delta
    function addDelta(uint128 x, int128 y) internal pure returns (uint128 z) {
        if (y < 0) {
            require((z = x - uint128(-y)) < x, 'LS');
        } else {
            require((z = x + uint128(y)) >= x, 'LA');
        }
    }
}

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

/// @title Optimized overflow and underflow safe math operations
/// @notice Contains methods for doing math operations that revert on overflow or underflow for minimal gas cost
library LowGasSafeMath {
    /// @notice Returns x + y, reverts if sum overflows uint256
    /// @param x The augend
    /// @param y The addend
    /// @return z The sum of x and y
    function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
        require((z = x + y) >= x);
    }

    /// @notice Returns x - y, reverts if underflows
    /// @param x The minuend
    /// @param y The subtrahend
    /// @return z The difference of x and y
    function sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
        require((z = x - y) <= x);
    }

    /// @notice Returns x * y, reverts if overflows
    /// @param x The multiplicand
    /// @param y The multiplier
    /// @return z The product of x and y
    function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
        require(x == 0 || (z = x * y) / x == y);
    }

    /// @notice Returns x + y, reverts if overflows or underflows
    /// @param x The augend
    /// @param y The addend
    /// @return z The sum of x and y
    function add(int256 x, int256 y) internal pure returns (int256 z) {
        require((z = x + y) >= x == (y >= 0));
    }

    /// @notice Returns x - y, reverts if overflows or underflows
    /// @param x The minuend
    /// @param y The subtrahend
    /// @return z The difference of x and y
    function sub(int256 x, int256 y) internal pure returns (int256 z) {
        require((z = x - y) <= x == (y >= 0));
    }
}

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

Settings
{
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "metadata": {
    "useLiteralContent": true
  },
  "libraries": {}
}

Contract Security Audit

Contract ABI

API
[{"inputs":[{"internalType":"address","name":"chosenOptionFactory","type":"address"},{"internalType":"address","name":"chosenPoolFactory","type":"address"},{"internalType":"address","name":"chosenTokens","type":"address"},{"internalType":"address","name":"chosenUniswapV3Factory","type":"address"},{"internalType":"address","name":"chosenNative","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"addendA0","type":"uint256"},{"internalType":"uint256","name":"addendA1","type":"uint256"},{"internalType":"uint256","name":"addendB0","type":"uint256"},{"internalType":"uint256","name":"addendB1","type":"uint256"}],"name":"AddOverflow","type":"error"},{"inputs":[{"internalType":"address","name":"from","type":"address"}],"name":"CallerNotWrappedNative","type":"error"},{"inputs":[],"name":"CanOnlyBeCalledByOptionContract","type":"error"},{"inputs":[],"name":"CanOnlyBeCalledByPoolContract","type":"error"},{"inputs":[],"name":"CanOnlyBeCalledByTokensContract","type":"error"},{"inputs":[],"name":"CanOnlyBeCalledByUniswapV3Contract","type":"error"},{"inputs":[{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"DeadlineReached","type":"error"},{"inputs":[{"internalType":"uint256","name":"dividend0","type":"uint256"},{"internalType":"uint256","name":"dividend1","type":"uint256"},{"internalType":"uint256","name":"divisor","type":"uint256"}],"name":"DivOverflow","type":"error"},{"inputs":[],"name":"DivideByZero","type":"error"},{"inputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"name":"InsufficientWrappedNative","type":"error"},{"inputs":[{"internalType":"uint256","name":"positionAmount","type":"uint256"},{"internalType":"uint256","name":"maxPositionAmount","type":"uint256"}],"name":"MaxPositionReached","type":"error"},{"inputs":[],"name":"ModuloByZero","type":"error"},{"inputs":[{"internalType":"uint256","name":"multiplicand","type":"uint256"},{"internalType":"uint256","name":"multiplier","type":"uint256"},{"internalType":"uint256","name":"divisor","type":"uint256"}],"name":"MulDivOverflow","type":"error"},{"inputs":[{"internalType":"string","name":"revertString","type":"string"}],"name":"MulticallFailed","type":"error"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"NativeTransferFailed","type":"error"},{"inputs":[{"internalType":"int256","name":"amount0","type":"int256"},{"internalType":"int256","name":"amount1","type":"int256"}],"name":"PassCalculateInfo","type":"error"},{"inputs":[],"name":"Uint160Overflow","type":"error"},{"inputs":[],"name":"ZeroAddress","type":"error"},{"inputs":[],"name":"ZeroPoolAddress","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"uint256","name":"strike","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"maturity","type":"uint256"},{"indexed":false,"internalType":"uint24","name":"uniswapV3Fee","type":"uint24"},{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"address","name":"tokenTo","type":"address"},{"indexed":false,"internalType":"address","name":"longTo","type":"address"},{"indexed":false,"internalType":"bool","name":"isToken0","type":"bool"},{"indexed":false,"internalType":"bool","name":"isLong0","type":"bool"},{"indexed":false,"internalType":"uint256","name":"tokenAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"positionAmount","type":"uint256"}],"name":"BorrowGivenPrincipal","type":"event"},{"inputs":[{"components":[{"internalType":"address","name":"token0","type":"address"},{"internalType":"address","name":"token1","type":"address"},{"internalType":"uint256","name":"strike","type":"uint256"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint24","name":"uniswapV3Fee","type":"uint24"},{"internalType":"address","name":"tokenTo","type":"address"},{"internalType":"address","name":"longTo","type":"address"},{"internalType":"bool","name":"isToken0","type":"bool"},{"internalType":"bool","name":"isLong0","type":"bool"},{"internalType":"uint256","name":"tokenAmount","type":"uint256"},{"internalType":"uint256","name":"maxPositionAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"internalType":"struct TimeswapV2PeripheryUniswapV3BorrowGivenPrincipalParam","name":"param","type":"tuple"}],"name":"borrowGivenPrincipal","outputs":[{"internalType":"uint256","name":"positionAmount","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes[]","name":"data","type":"bytes[]"}],"name":"multicall","outputs":[{"internalType":"bytes[]","name":"results","type":"bytes[]"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"optionFactory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"poolFactory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"refundNatives","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"strike","type":"uint256"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"token0AndLong0Amount","type":"uint256"},{"internalType":"uint256","name":"token1AndLong1Amount","type":"uint256"},{"internalType":"uint256","name":"shortAmount","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct TimeswapV2OptionMintCallbackParam","name":"param","type":"tuple"}],"name":"timeswapV2OptionMintCallback","outputs":[{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"strike","type":"uint256"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"bool","name":"isLong0ToLong1","type":"bool"},{"internalType":"uint256","name":"token0AndLong0Amount","type":"uint256"},{"internalType":"uint256","name":"token1AndLong1Amount","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct TimeswapV2OptionSwapCallbackParam","name":"param","type":"tuple"}],"name":"timeswapV2OptionSwapCallback","outputs":[{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"strike","type":"uint256"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"long0Amount","type":"uint256"},{"internalType":"uint256","name":"long1Amount","type":"uint256"},{"internalType":"uint256","name":"shortAmount","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct TimeswapV2PoolLeverageCallbackParam","name":"param","type":"tuple"}],"name":"timeswapV2PoolLeverageCallback","outputs":[{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"strike","type":"uint256"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"long0Balance","type":"uint256"},{"internalType":"uint256","name":"long1Balance","type":"uint256"},{"internalType":"uint256","name":"longAmount","type":"uint256"},{"internalType":"uint256","name":"shortAmount","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct TimeswapV2PoolLeverageChoiceCallbackParam","name":"param","type":"tuple"}],"name":"timeswapV2PoolLeverageChoiceCallback","outputs":[{"internalType":"uint256","name":"long0Amount","type":"uint256"},{"internalType":"uint256","name":"long1Amount","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"token0","type":"address"},{"internalType":"address","name":"token1","type":"address"},{"internalType":"uint256","name":"strike","type":"uint256"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"long0Amount","type":"uint256"},{"internalType":"uint256","name":"long1Amount","type":"uint256"},{"internalType":"uint256","name":"shortAmount","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct TimeswapV2TokenMintCallbackParam","name":"param","type":"tuple"}],"name":"timeswapV2TokenMintCallback","outputs":[{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"uniswapV3Factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int256","name":"amount0Delta","type":"int256"},{"internalType":"int256","name":"amount1Delta","type":"int256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"uniswapV3SwapCallback","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountMinimum","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"unwrapWrappedNatives","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"wrappedNativeToken","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]

6101206040523480156200001257600080fd5b50604051620049cf380380620049cf833981016040819052620000359162000089565b6001600160601b0319606092831b811660805294821b851660a05292811b841660c05290811b831660e0521b1661010052620000f9565b80516001600160a01b03811681146200008457600080fd5b919050565b600080600080600060a08688031215620000a257600080fd5b620000ad866200006c565b9450620000bd602087016200006c565b9350620000cd604087016200006c565b9250620000dd606087016200006c565b9150620000ed608087016200006c565b90509295509295909350565b60805160601c60a05160601c60c05160601c60e05160601c6101005160601c6147e2620001ed6000396000818160fc0152818161015a015281816113ba0152818161147e015281816126ad01526126f30152600081816102b001528181610cd70152818161157e015261183b0152600081816101cc015281816103e601528181610f9c015281816110530152611e28015260008181610304015281816103c501528181610b2f01528181610f7b0152818161103201528181611526015281816118660152611e0701526000818161022d015281816104c001528181611997015281816120bb01526127b901526147e26000f3fe6080604052600436106100ec5760003560e01c80639d63848a1161008a578063dc4a484711610059578063dc4a484714610339578063dfef25d814610359578063e13c022c14610361578063fa461e331461038157600080fd5b80639d63848a1461029e578063ac9650d8146102d2578063b3461c87146102f2578063b7f68a0e1461032657600080fd5b80634a358d65116100c65780634a358d65146101ee5780635b5491821461021b578063797481d01461024f5780638662fa6a1461027e57600080fd5b806317fcb39b146101485780632d780db4146101995780634219dc40146101ba57600080fd5b3661014357336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146101415760405163631b2c4f60e11b81523360048201526024015b60405180910390fd5b005b600080fd5b34801561015457600080fd5b5061017c7f000000000000000000000000000000000000000000000000000000000000000081565b6040516001600160a01b0390911681526020015b60405180910390f35b6101ac6101a7366004613533565b6103a1565b604051908152602001610190565b3480156101c657600080fd5b5061017c7f000000000000000000000000000000000000000000000000000000000000000081565b3480156101fa57600080fd5b5061020e610209366004613653565b610aca565b6040516101909190613749565b34801561022757600080fd5b5061017c7f000000000000000000000000000000000000000000000000000000000000000081565b34801561025b57600080fd5b5061026f61026a36600461375c565b610f3f565b60405161019093929190613796565b34801561028a57600080fd5b5061020e6102993660046137c7565b610ffb565b3480156102aa57600080fd5b5061017c7f000000000000000000000000000000000000000000000000000000000000000081565b6102e56102e03660046137fb565b611215565b604051610190919061386f565b3480156102fe57600080fd5b5061017c7f000000000000000000000000000000000000000000000000000000000000000081565b6101416103343660046138e6565b6113a2565b34801561034557600080fd5b5061020e6103543660046137c7565b6114f1565b610141611822565b34801561036d57600080fd5b5061020e61037c366004613916565b611834565b34801561038d57600080fd5b5061014161039c366004613951565b611957565b60004282610160013510156103be576103be826101600135611a4e565b60006104277f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000061041260208701876139d0565b61042260408801602089016139d0565b611a6a565b60408051633a9d71e760e01b8152908601356004820152606086013560248201529092506000915081906001600160a01b03841690633a9d71e790604401604080518083038186803b15801561047c57600080fd5b505afa158015610490573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104b491906139ed565b909250905060006105117f00000000000000000000000000000000000000000000000000000000000000006104ec60208901896139d0565b6104fc60408a0160208b016139d0565b61050c60a08b0160808c01613a22565b611b1f565b905060008080808061052660208c018c6139d0565b61053660408d0160208e016139d0565b61054660a08e0160808f01613a22565b604080516001600160a01b039485166020820152939092169183019190915262ffffff1660608201526080016040516020818303038152906040529050600081604051602001610597929190613a3f565b60408051601f1981840301815291905290506105ba6101008c0160e08d01613a68565b6105c457876105c6565b865b156106c7576106236040518060a001604052808d60e00160208101906105ec9190613a68565b158152600060208201526101208e01356040808301919091528e013560608201526080018390526001600160a01b03881690611b49565b90935091506106396101008c0160e08d01613a68565b6106435787610645565b865b8311156106c7576106c16040518060a001604052808d60e001602081019061066d9190613a68565b15151581526020016001975087151581526020018d60e00160208101906106949190613a68565b61069e578a6106a0565b895b81526040808f01356020830152018390526001600160a01b03881690611b49565b90935091505b6106d86101008c0160e08d01613a68565b6106e257866106e4565b875b6106f3836101208e0135613a9b565b111561078b576001935060006040518060a001604052808d60e001602081019061071d9190613a68565b15151581526020016000975087151581526020018d60e00160208101906107449190613a68565b61074e5789610750565b8a5b8e61012001356107609190613a9b565b81526000602082015260400183905290506107846001600160a01b03881682611b49565b9094509250505b3361079c60a08d0160808e01613a22565b6107ac60c08e0160a08f016139d0565b8d60e00160208101906107bf9190613a68565b604080516001600160a01b03958616602082015262ffffff9094169084015292166060820152901515608082015285151560a082015284151560c082015260e081018390526101000160408051601f1981840301815261014083019091529150610977908061083160208f018f6139d0565b6001600160a01b031681526020018d602001602081019061085291906139d0565b6001600160a01b031681526020018d6040013581526020018d6060013581526020018d6101000160208101906108889190613a68565b15158e60e001602081019061089d9190613a68565b1515146108bc578d60a00160208101906108b791906139d0565b6108be565b305b6001600160a01b031681526020018d60c00160208101906108df91906139d0565b6001600160a01b031681526020018d6101000160208101906109019190613a68565b151581526020018d60e001602081019061091b9190613a68565b6109255785610936565b848e61012001356109369190613a9b565b81526020018d60e001602081019061094e9190613a68565b61096857848e61012001356109639190613a9b565b61096a565b855b8152602001839052611dfc565b50809a50505050505050505050508161014001358111156109b957604051633d14758360e11b8152600481018290526101408301356024820152604401610138565b60608201356109ce60408401602085016139d0565b6001600160a01b03166109e460208501856139d0565b6001600160a01b03167f319869ce99e1a1f0954078ca235aeb1867c1a21d61430b7ff0951b503488e80a6040860135610a2360a0880160808901613a22565b33610a3460c08a0160a08b016139d0565b610a4460e08b0160c08c016139d0565b610a556101008c0160e08d01613a68565b610a676101208d016101008e01613a68565b6040805197885262ffffff9690961660208801526001600160a01b039485168787015292841660608701529216608085015290151560a0840152151560c08301526101208881013560e084015261010083018890529051918290030190a4919050565b6040805160e08101825260008082526020820181905291810182905260608181018390526080820183905260a0820183905260c08201929092528260a00151806020019051810190610b1c9190613b12565b81516020830151919450919250610b54917f000000000000000000000000000000000000000000000000000000000000000091611fa5565b8060800151610b67578260600151610b6d565b82604001515b8160c001818151610b7e9190613bdc565b90525060a081015115610cd5578051602082015160608301516080840151610baa578560600151610bb0565b85604001515b8460c0015186604051602001610bcb96959493929190613bf4565b60408051601f19818403018152610100830182528551835260208681015190840152838201516001600160a01b03169183019190915230606083015260808084018051159184019190915251909350339163b084c2529160a0820190610c32576000610c35565b60015b6001811115610c4657610c46613c42565b81526020018460a001518152602001858152506040518263ffffffff1660e01b8152600401610c759190613c6c565b600060405180830381600087803b158015610c8f57600080fd5b505af1158015610ca3573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610ccb9190810190613cf2565b9350610f39915050565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316634fd991ea60405180610160016040528084600001516001600160a01b0316815260200184602001516001600160a01b0316815260200186600001518152602001866020015181526020018460800151610d595730610d5f565b84606001515b6001600160a01b031681526020018460800151610d80578460600151610d82565b305b6001600160a01b03168152602001306001600160a01b031681526020018460800151610daf576000610db5565b8460c001515b81526020018460800151610dcd578460c00151610dd0565b60005b815260200160008152602001604051806020016040528060008152508152506040518263ffffffff1660e01b8152600401610e0b9190613d41565b600060405180830381600087803b158015610e2557600080fd5b505af1158015610e39573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610e619190810190613e12565b50610f0e604051806101400160405280336001600160a01b0316815260200183600001516001600160a01b0316815260200183602001516001600160a01b0316815260200185600001518152602001856020015181526020018360800151151581526020018360800151610ed6576000610edc565b85604001515b81526020018360800151610ef4578560600151610ef7565b60005b815260c0840151602082015260400184905261205b565b91508060c0015182604051602001610f27929190613e46565b60405160208183030381529060405291505b50919050565b60008060608180808080610f5660c08a018a613e5f565b810190610f639190613eac565b919e509c509a50939850919650945092509050610fc27f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000878761237f565b5084848484848a604051602001610fde96959493929190613f59565b604051602081830303815290604052955050505050509193909250565b606060008080808061101060a0880188613e5f565b81019061101d9190613f9d565b9a5093985091965094509250905060006110797f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000888861237f565b90506040518060e00160405280876001600160a01b03168152602001866001600160a01b03168152602001856001600160a01b03168152602001846001600160a01b031681526020018315158152602001836110d95789604001356110df565b89606001355b8152602001836110f35789606001356110f9565b89604001355b905260405161110d91908990602001614035565b60408051601f19818403018152610120830182528a35835260208b8101359084015230918301829052606083019190915233608083015297506001600160a01b038216906380e066799060a08101600181526020018561116e576000611174565b8b608001355b815260200185611188578b6080013561118b565b60005b81526020018a8152506040518263ffffffff1660e01b81526004016111b0919061409e565b600060405180830381600087803b1580156111ca57600080fd5b505af11580156111de573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526112069190810190614140565b9b9a5050505050505050505050565b6060816001600160401b0381111561122f5761122f613546565b60405190808252806020026020018201604052801561126257816020015b606081526020019060019003908161124d5790505b50905060005b8281101561139b576000803086868581811061128657611286614199565b90506020028101906112989190613e5f565b6040516112a69291906141af565b600060405180830381855af49150503d80600081146112e1576040519150601f19603f3d011682016040523d82523d6000602084013e6112e6565b606091505b5091509150816113685760448151101561133457604051635d2f436f60e11b815260206004820152600e60248201526d125b9d985b1a590814995cdd5b1d60921b6044820152606401610138565b6004810190508080602001905181019061134e91906141bf565b604051635d2f436f60e11b81526004016101389190613749565b8084848151811061137b5761137b614199565b60200260200101819052505050808061139390614207565b915050611268565b5092915050565b6040516370a0823160e01b81523060048201526000907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906370a082319060240160206040518083038186803b15801561140457600080fd5b505afa158015611418573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061143c9190614222565b90508281101561146257604051636b8c4dab60e01b815260048101829052602401610138565b80156114ec57604051632e1a7d4d60e01b8152600481018290527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690632e1a7d4d90602401600060405180830381600087803b1580156114ca57600080fd5b505af11580156114de573d6000803e3d6000fd5b505050506114ec82826124b7565b505050565b606060008080808061150660a0880188613e5f565b810190611513919061423b565b9a5093985091965094509250905061154c7f00000000000000000000000000000000000000000000000000000000000000008686611fa5565b61155c6060880160408901613a68565b61156a578660600135611570565b86608001355b61157a9082613bdc565b90507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316634fd991ea604051806101600160405280886001600160a01b03168152602001876001600160a01b031681526020018a6000013581526020018a6020013581526020018a60400160208101906115fc9190613a68565b6116065786611608565b305b6001600160a01b0316815260200161162660608c0160408d01613a68565b6116305730611632565b865b6001600160a01b031681523060208201526040908101906116599060608d01908d01613a68565b6116635784611666565b60005b815260200161167b60608c0160408d01613a68565b611686576000611688565b845b815260200160008152602001604051806020016040528060008152508152506040518263ffffffff1660e01b81526004016116c39190613d41565b600060405180830381600087803b1580156116dd57600080fd5b505af11580156116f1573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526117199190810190613e12565b506117f2604051806101400160405280336001600160a01b03168152602001876001600160a01b03168152602001866001600160a01b03168152602001896000013581526020018960200135815260200189604001602081019061177d9190613a68565b15815260200161179360608b0160408c01613a68565b61179d57846117a0565b60005b6117ae9060608c0135613bdc565b81526020016117c360608b0160408c01613a68565b6117ce5760006117d0565b845b6117de9060808c0135613bdc565b81526020018381526020018881525061205b565b95508086604051602001611807929190613e46565b60405160208183030381529060405295505050505050919050565b47156118325761183233476124b7565b565b606061185f7f0000000000000000000000000000000000000000000000000000000000000000612557565b60006118a77f000000000000000000000000000000000000000000000000000000000000000061189260208601866139d0565b6118a260408701602088016139d0565b612583565b90506001600160a01b03811663b2ceca77604085013560608601353360808801356118d35760016118d6565b60005b60808901356118e9578860a001356118ef565b88608001355b6040518663ffffffff1660e01b815260040161190f9594939291906142a8565b600060405180830381600087803b15801561192957600080fd5b505af115801561193d573d6000803e3d6000fd5b505060408051602081019091526000815295945050505050565b600080611966838501856142e8565b915091508115611a3b576000806000808480602001905181019061198a919061432d565b93509350935093506119be7f0000000000000000000000000000000000000000000000000000000000000000848484612608565b6001600160a01b038416301415611a0857611a033360008c136119e1578a6119e3565b8b5b60008d136119f157846119f3565b855b6001600160a01b03169190612648565b611a32565b611a3260008b13611a195782611a1b565b835b853360008e13611a2b578c6126ab565b8d5b6126ab565b50505050611a46565b611a46868683612795565b505050505050565b60405163070b57d960e41b815260048101829052602401610138565b600080611a816001600160a01b03871685856127ea565b6040516330af0bbf60e21b81526001600160a01b0380831660048301529193509086169063c2bc2efc9060240160206040518083038186803b158015611ac657600080fd5b505afa158015611ada573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611afe919061438c565b90506001600160a01b038116611b1657611b16612816565b94509492505050565b6000611b2d8585858561282f565b9050611b41816001600160a01b03166128c8565b949350505050565b6000806000846001600160a01b0316633850c7bd6040518163ffffffff1660e01b815260040160e06040518083038186803b158015611b8757600080fd5b505afa158015611b9b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611bbf91906143bb565b50505050505090506000846060015160001415611c10578451611c0057611bfb600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b611c1d565b611bfb6401000276a36001614482565b611c1d85606001516128ef565b90506401000276a36001600160a01b03821611611c4757611c446401000276a36001614482565b90505b73fffd8963efd1fc6a506488495d951d5263988d266001600160a01b03821610611c8d57611c8a600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b90505b8451611cad57816001600160a01b0316816001600160a01b031611611cc3565b816001600160a01b0316816001600160a01b0316105b15611df357600080876001600160a01b031663128acb083089600001518a60200151611d0457611cf68b60400151612945565b611cff906144ad565b611d11565b611d118b60400151612945565b878c608001516040518663ffffffff1660e01b8152600401611d379594939291906144ca565b6040805180830381600087803b158015611d5057600080fd5b505af1925050508015611d80575060408051601f3d908101601f19168201909252611d7d918101906139ed565b60015b611dc9573d808015611dae576040519150601f19603f3d011682016040523d82523d6000602084013e611db3565b606091505b50611dbd8161295b565b9093509150611dcc9050565b50505b8651611de15780611ddc836144ad565b611deb565b81611deb826144ad565b909650945050505b50509250929050565b600060606000611e567f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000086600001518760200151611a6a565b9150508360000151846020015185608001518660a001518760c001518860e001518961010001518a6101200151604051602001611e9a989796959493929190614505565b60408051601f1981840301815260e08301825286820151835260608088015160208501523092840183905283019190915292506001600160a01b0382169063e0260d11906080810160018152602001611f038860e001518961010001518a60400151600161298b565b8152602001858152506040518263ffffffff1660e01b8152600401611f289190614558565b600060405180830381600087803b158015611f4257600080fd5b505af1158015611f56573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611f7e9190810190614140565b8051909550611f9a9350850160209081019250850190506145d0565b909590945092505050565b60405163d81e842360e01b81526001600160a01b03838116600483015282811660248301526000919085169063d81e84239060440160206040518083038186803b158015611ff257600080fd5b505afa158015612006573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061202a919061438c565b90506001600160a01b03811633146120555760405163a5fbb7d960e01b815260040160405180910390fd5b50505050565b606060008060008060008060008861012001518060200190518101906120819190614600565b96509650965096509650965096508261209a57806120af565b836120a9578860c001516120af565b8860e001515b1561221e5760006120ea7f00000000000000000000000000000000000000000000000000000000000000008b602001518c604001518a61282f565b90508960a001511515851515146121015787612103565b305b60208b8101516040808e015181516001600160a01b03958616948101949094529184169083015291909116606082015262ffffff8816608082015260a0016040516020818303038152906040529850600189604051602001612166929190613a3f565b60405160208183030381529060405298506122086040518060c001604052808c60a0015115158815151461219a578861219c565b305b6001600160a01b03168152871560208201528615156040820152606001866121c457846121d9565b876121d3578c60c001516121d9565b8c60e001515b8152602001856121ed578c606001516121f0565b60005b81526020018b90526001600160a01b038316906129ce565b6040805160208101909152600081529a50925050505b8860a001511515841515141561233457808960a00151612242578960e00151612248565b8960c001515b11156122905761228b8960a0015161226457896040015161226a565b89602001515b8830611a2d858e60a00151612282578e60e001510390565b8e60c001510390565b6122fc565b808960a001516122a4578960e001516122aa565b8960c001515b10156122fc576122fc856122d48b60a001516122ca578b60e00151840390565b8b60c00151840390565b8b60a001516122e7578b604001516119f3565b8b602001516001600160a01b03169190612648565b61232f89600001518a60a00151612317578a60e001516122d4565b8a60c001518b60a001516122e7578b604001516119f3565b612373565b6123738960a0015161234a578960400151612350565b89602001515b888b600001518c60a00151612369578c60e001516126ab565b8c60c001516126ab565b50505050505050919050565b60405163d81e842360e01b81526001600160a01b03838116600483015282811660248301526000919086169063d81e84239060440160206040518083038186803b1580156123cc57600080fd5b505afa1580156123e0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612404919061438c565b6040516330af0bbf60e21b81526001600160a01b03808316600483015291925060009186169063c2bc2efc9060240160206040518083038186803b15801561244b57600080fd5b505afa15801561245f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612483919061438c565b90506001600160a01b03811633146124ae5760405163863b47e760e01b815260040160405180910390fd5b50949350505050565b604080516000808252602082019092526001600160a01b0384169083906040516124e19190614692565b60006040518083038185875af1925050503d806000811461251e576040519150601f19603f3d011682016040523d82523d6000602084013e612523565b606091505b50509050806114ec5760405163296c17bb60e21b81526001600160a01b038416600482015260248101839052604401610138565b6001600160a01b03811633146125805760405163d2bc8fc960e01b815260040160405180910390fd5b50565b60405163d81e842360e01b81526001600160a01b03838116600483015282811660248301526000919085169063d81e84239060440160206040518083038186803b1580156125d057600080fd5b505afa1580156125e4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611b41919061438c565b60006126168585858561282f565b90506001600160a01b0381163314612641576040516307c1698f60e01b815260040160405180910390fd5b5050505050565b6040516001600160a01b0383166024820152604481018290526114ec90849063a9059cbb60e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b031990931692909217909152612c2e565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316846001600160a01b03161480156126ec5750804710155b15612780577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663d0e30db0826040518263ffffffff1660e01b81526004016000604051808303818588803b15801561274c57600080fd5b505af1158015612760573d6000803e3d6000fd5b5061277b935050506001600160a01b03861690508383612648565b612055565b6120556001600160a01b038516848484612d00565b6000806000838060200190518101906127ae91906146ae565b9250925092506127e07f0000000000000000000000000000000000000000000000000000000000000000848484612608565b611a468686612d38565b60006127f7848484612583565b90506001600160a01b03811661280f5761280f612816565b9392505050565b60405163d92e233d60e01b815260040160405180910390fd5b604051630b4c774160e11b81526001600160a01b038481166004830152838116602483015262ffffff8316604483015260009190861690631698ee829060640160206040518083038186803b15801561288757600080fd5b505afa15801561289b573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128bf919061438c565b95945050505050565b6001600160a01b0381166125805760405163037064f160e01b815260040160405180910390fd5b60006001600160c01b03821161291c57612916612911604084901b6000612d5b565b612dd9565b92915050565b6000806129328468010000000000000000612e03565b91509150611b4161291183836000612e22565b6000600160ff1b821061295757600080fd5b5090565b60008061296f836377760aaf60e11b612eb0565b80602001905181019061298291906139ed565b91509150915091565b60006001600160801b0383116129b7576129a88584600185612f1e565b6129b29085613bdc565b6128bf565b6129c48484600085612f1e565b6128bf9086613bdc565b6000806000846001600160a01b0316633850c7bd6040518163ffffffff1660e01b815260040160e06040518083038186803b158015612a0c57600080fd5b505afa158015612a20573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612a4491906143bb565b50505050505090506000846080015160001415612a98578460200151612a8857612a83600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b612aa5565b612a836401000276a36001614482565b612aa585608001516128ef565b90506401000276a36001600160a01b03821611612acf57612acc6401000276a36001614482565b90505b73fffd8963efd1fc6a506488495d951d5263988d266001600160a01b03821610612b1557612b12600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b90505b8460200151612b3857806001600160a01b0316826001600160a01b031610612b4e565b806001600160a01b0316826001600160a01b0316115b15611df357600080876001600160a01b031663128acb08886000015189602001518a60400151612b9357612b858b60600151612945565b612b8e906144ad565b612ba0565b612ba08b60600151612945565b878c60a001516040518663ffffffff1660e01b8152600401612bc69594939291906144ca565b6040805180830381600087803b158015612bdf57600080fd5b505af1158015612bf3573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612c1791906139ed565b915091508660200151611de15780611ddc836144ad565b6000612c83826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316612f459092919063ffffffff16565b8051909150156114ec5780806020019051810190612ca191906146fb565b6114ec5760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610138565b6040516001600160a01b03808516602483015283166044820152606481018290526120559085906323b872dd60e01b90608401612674565b6040516377760aaf60e11b81526004810183905260248101829052604401610138565b6000600019831415612d7557506001600160801b03612916565b82612d8257506000612916565b50816001818101901c5b81811015612db457809150600181828681612da957612da9614718565b0401901c9050612d8c565b50818015612dca575082612dc8828061472e565b105b156129165780611b4181614207565b60006001600160a01b0382111561295757604051630ba006e160e41b815260040160405180910390fd5b6000806000198385098385029250828110838203039150509250929050565b600082612e3a57612e338483612d5b565b905061280f565b6000612e498585600019612f54565b905060001991505b81811015612e6e57809150612e67858583612f54565b9050612e51565b8215612ea857600080612e818485612e03565b9150915080861180612e9257508187115b15612ea55783612ea181614207565b9450505b50505b509392505050565b81516060906020612ec2600483613a9b565b612ecc919061474d565b158015612ef457506001600160e01b03198316612ee885614761565b6001600160e01b031916145b15612f1657612f0e846004612f098185613a9b565b612f93565b915050612916565b835184602001fd5b600082612f35576129b285600160801b868561309f565b6128bf8585600160801b8561309f565b6060611b41848460008561319b565b600080612f6485858560006132cc565b90506000612f76826000866000613351565b9092509050612f898282600260006132cc565b9695505050505050565b606081612fa181601f613bdc565b1015612fe05760405162461bcd60e51b815260206004820152600e60248201526d736c6963655f6f766572666c6f7760901b6044820152606401610138565b612fea8284613bdc565b8451101561302e5760405162461bcd60e51b8152602060048201526011602482015270736c6963655f6f75744f66426f756e647360781b6044820152606401610138565b60608215801561304d57604051915060008252602082016040526124ae565b6040519150601f8416801560200281840101858101878315602002848b0101015b8183101561308657805183526020928301920161306e565b5050858452601f01601f19166040525050949350505050565b60008060006130ae8787612e03565b9150915080600014156130cf576130c68286866133c2565b92505050611b41565b8085116131005760405163362ced0960e11b8152600481018890526024810187905260448101869052606401610138565b600085878909600087810388169788900497600260038a028118808b02820302808b02820302808b02820302808b02820302808b02820302808b0290910302918190038190046001018684119095039485029290950394909404179283029350849050801561317e5750848061317857613178614718565b86880915155b15613191578261318d81614207565b9350505b5050949350505050565b6060824710156131fc5760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b6064820152608401610138565b6001600160a01b0385163b6132535760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610138565b600080866001600160a01b0316858760405161326f9190614692565b60006040518083038185875af1925050503d80600081146132ac576040519150601f19603f3d011682016040523d82523d6000602084013e6132b1565b606091505b50915091506132c18282866133f4565b979650505050505050565b6000806132da86868661342d565b9092509050801561330f57604051636aa917ab60e01b8152600481018790526024810186905260448101859052606401610138565b82156124ae576000806133228487612e03565b915091508087118061333357508188115b15613346578361334281614207565b9450505b505050949350505050565b818401848110828501810190801561337e578115806133795750613376600183613a9b565b86115b613382565b8186115b156133b8576040516347b1893f60e01b815260048101889052602481018790526044810186905260648101859052608401610138565b5094509492505050565b60006133ce8385614798565b90508180156133e557506133e2838561474d565b15155b1561280f57806128bf81614207565b6060831561340357508161280f565b8251156134135782518084602001fd5b8160405162461bcd60e51b81526004016101389190613749565b6000808361344857613441858460006133c2565b91506134d4565b6000613453846134dc565b9050600061346085613509565b90505b85156134b4576000806134768885612e03565b9150915061348686868484613351565b90965094506134958884612e03565b90925090506134a782828b6000613351565b9099509750613463915050565b6134cc84846134c58a8960006133c2565b6000613351565b909450925050505b935093915050565b6000816134fc576040516301de42a960e41b815260040160405180910390fd5b5060008190030460010190565b600081613529576040516319dbad9f60e01b815260040160405180910390fd5b5060008190030690565b60006101808284031215610f3957600080fd5b634e487b7160e01b600052604160045260246000fd5b60405160c081016001600160401b038111828210171561357e5761357e613546565b60405290565b60405160e081016001600160401b038111828210171561357e5761357e613546565b604051601f8201601f191681016001600160401b03811182821017156135ce576135ce613546565b604052919050565b60006001600160401b038211156135ef576135ef613546565b50601f01601f191660200190565b600082601f83011261360e57600080fd5b813561362161361c826135d6565b6135a6565b81815284602083860101111561363657600080fd5b816020850160208301376000918101602001919091529392505050565b60006020828403121561366557600080fd5b81356001600160401b038082111561367c57600080fd5b9083019060c0828603121561369057600080fd5b61369861355c565b823581526020830135602082015260408301356040820152606083013560608201526080830135608082015260a0830135828111156136d657600080fd5b6136e2878286016135fd565b60a08301525095945050505050565b60005b8381101561370c5781810151838201526020016136f4565b838111156120555750506000910152565b600081518084526137358160208601602086016136f1565b601f01601f19169290920160200192915050565b60208152600061280f602083018461371d565b60006020828403121561376e57600080fd5b81356001600160401b0381111561378457600080fd5b820160e0818503121561280f57600080fd5b8381528260208201526060604082015260006128bf606083018461371d565b600060c08284031215610f3957600080fd5b6000602082840312156137d957600080fd5b81356001600160401b038111156137ef57600080fd5b611b41848285016137b5565b6000806020838503121561380e57600080fd5b82356001600160401b038082111561382557600080fd5b818501915085601f83011261383957600080fd5b81358181111561384857600080fd5b8660208260051b850101111561385d57600080fd5b60209290920196919550909350505050565b6000602080830181845280855180835260408601915060408160051b870101925083870160005b828110156138c457603f198886030184526138b285835161371d565b94509285019290850190600101613896565b5092979650505050505050565b6001600160a01b038116811461258057600080fd5b600080604083850312156138f957600080fd5b82359150602083013561390b816138d1565b809150509250929050565b60006020828403121561392857600080fd5b81356001600160401b0381111561393e57600080fd5b8201610100818503121561280f57600080fd5b6000806000806060858703121561396757600080fd5b843593506020850135925060408501356001600160401b038082111561398c57600080fd5b818701915087601f8301126139a057600080fd5b8135818111156139af57600080fd5b8860208285010111156139c157600080fd5b95989497505060200194505050565b6000602082840312156139e257600080fd5b813561280f816138d1565b60008060408385031215613a0057600080fd5b505080516020909101519092909150565b62ffffff8116811461258057600080fd5b600060208284031215613a3457600080fd5b813561280f81613a11565b8215158152604060208201526000611b41604083018461371d565b801515811461258057600080fd5b600060208284031215613a7a57600080fd5b813561280f81613a5a565b634e487b7160e01b600052601160045260246000fd5b600082821015613aad57613aad613a85565b500390565b8051613abd81613a5a565b919050565b6000613ad061361c846135d6565b9050828152838383011115613ae457600080fd5b61280f8360208301846136f1565b600082601f830112613b0357600080fd5b61280f83835160208501613ac2565b600080828403610100811215613b2757600080fd5b60e0811215613b3557600080fd5b50613b3e613584565b8351613b49816138d1565b81526020840151613b59816138d1565b60208201526040840151613b6c816138d1565b60408201526060840151613b7f816138d1565b6060820152613b9060808501613ab2565b608082015260a084015160a082015260c084015160c08201528092505060e08301516001600160401b03811115613bc657600080fd5b613bd285828601613af2565b9150509250929050565b60008219821115613bef57613bef613a85565b500190565b6001600160a01b038781168252868116602083015285166040820152606081018490526080810183905260c060a08201819052600090613c369083018461371d565b98975050505050505050565b634e487b7160e01b600052602160045260246000fd5b60028110613c6857613c68613c42565b9052565b6020815281516020820152602082015160408201526000604083015160018060a01b03808216606085015280606086015116608085015250506080830151613cb860a084018215159052565b5060a0830151613ccb60c0840182613c58565b5060c083015160e083015260e0830151610100808185015250611b4161012084018261371d565b600080600060608486031215613d0757600080fd5b835192506020840151915060408401516001600160401b03811115613d2b57600080fd5b613d3786828701613af2565b9150509250925092565b60208152613d5b6020820183516001600160a01b03169052565b60006020830151613d7760408401826001600160a01b03169052565b5060408301516060830152606083015160808301526080830151613da660a08401826001600160a01b03169052565b5060a08301516001600160a01b03811660c08401525060c08301516001600160a01b03811660e08401525060e083015161010083810191909152830151610120808401919091528301516101408084019190915283015161016080840152611b4161018084018261371d565b600060208284031215613e2457600080fd5b81516001600160401b03811115613e3a57600080fd5b611b4184828501613af2565b828152604060208201526000611b41604083018461371d565b6000808335601e19843603018112613e7657600080fd5b8301803591506001600160401b03821115613e9057600080fd5b602001915036819003821315613ea557600080fd5b9250929050565b600080600080600080600080610100898b031215613ec957600080fd5b8835613ed4816138d1565b97506020890135613ee4816138d1565b96506040890135613ef4816138d1565b95506060890135613f04816138d1565b94506080890135613f1481613a5a565b935060a0890135925060c0890135915060e08901356001600160401b03811115613f3d57600080fd5b613f498b828c016135fd565b9150509295985092959890939650565b6001600160a01b0387811682528681166020830152858116604083015284166060820152821515608082015260c060a08201819052600090613c369083018461371d565b60008060008060008060c08789031215613fb657600080fd5b8635613fc1816138d1565b95506020870135613fd1816138d1565b94506040870135613fe1816138d1565b93506060870135613ff1816138d1565b9250608087013561400181613a5a565b915060a08701356001600160401b0381111561401c57600080fd5b61402889828a016135fd565b9150509295509295509295565b600061010060018060a01b038086511684528060208701511660208501528060408701511660408501528060608701511660608501525060808501511515608084015260a085015160a084015260c085015160c08401528060e08401526128bf8184018561371d565b602081528151602082015260208201516040820152600060408301516140cf60608401826001600160a01b03169052565b5060608301516001600160a01b03811660808401525060808301516001600160a01b03811660a08401525060a083015161410c60c0840182613c58565b5060c083015160e0838101919091528301516101008084019190915283015161012080840152611b4161014084018261371d565b6000806000806080858703121561415657600080fd5b84519350602085015192506040850151915060608501516001600160401b0381111561418157600080fd5b61418d87828801613af2565b91505092959194509250565b634e487b7160e01b600052603260045260246000fd5b8183823760009101908152919050565b6000602082840312156141d157600080fd5b81516001600160401b038111156141e757600080fd5b8201601f810184136141f857600080fd5b611b4184825160208401613ac2565b600060001982141561421b5761421b613a85565b5060010190565b60006020828403121561423457600080fd5b5051919050565b60008060008060008060c0878903121561425457600080fd5b863561425f816138d1565b9550602087013561426f816138d1565b9450604087013561427f816138d1565b9350606087013592506080870135915060a08701356001600160401b0381111561401c57600080fd5b858152602081018590526001600160a01b038416604082015260a08101600384106142d5576142d5613c42565b6060820193909352608001529392505050565b600080604083850312156142fb57600080fd5b823561430681613a5a565b915060208301356001600160401b0381111561432157600080fd5b613bd2858286016135fd565b6000806000806080858703121561434357600080fd5b845161434e816138d1565b602086015190945061435f816138d1565b6040860151909350614370816138d1565b606086015190925061438181613a11565b939692955090935050565b60006020828403121561439e57600080fd5b815161280f816138d1565b805161ffff81168114613abd57600080fd5b600080600080600080600060e0888a0312156143d657600080fd5b87516143e1816138d1565b8097505060208801518060020b81146143f957600080fd5b9550614407604089016143a9565b9450614415606089016143a9565b9350614423608089016143a9565b925060a088015160ff8116811461443957600080fd5b60c089015190925061444a81613a5a565b8091505092959891949750929550565b60006001600160a01b038381169083168181101561447a5761447a613a85565b039392505050565b60006001600160a01b038281168482168083038211156144a4576144a4613a85565b01949350505050565b6000600160ff1b8214156144c3576144c3613a85565b5060000390565b6001600160a01b0386811682528515156020830152604082018590528316606082015260a0608082018190526000906132c19083018461371d565b6001600160a01b0389811682528881166020830152878116604083015286166060820152841515608082015260a0810184905260c0810183905261010060e082018190526000906112068382018561371d565b6020815281516020820152602082015160408201526000604083015160018060a01b03808216606085015280606086015116608085015250506080830151600481106145a6576145a6613c42565b8060a08401525060a083015160c083015260c083015160e080840152611b4161010084018261371d565b600080604083850312156145e357600080fd5b8251915060208301516001600160401b03811115613bc657600080fd5b600080600080600080600060e0888a03121561461b57600080fd5b8751614626816138d1565b602089015190975061463781613a11565b6040890151909650614648816138d1565b606089015190955061465981613a5a565b608089015190945061466a81613a5a565b60a089015190935061467b81613a5a565b8092505060c0880151905092959891949750929550565b600082516146a48184602087016136f1565b9190910192915050565b6000806000606084860312156146c357600080fd5b83516146ce816138d1565b60208501519093506146df816138d1565b60408501519092506146f081613a11565b809150509250925092565b60006020828403121561470d57600080fd5b815161280f81613a5a565b634e487b7160e01b600052601260045260246000fd5b600081600019048311821515161561474857614748613a85565b500290565b60008261475c5761475c614718565b500690565b805160208201516001600160e01b031980821692919060048310156147905780818460040360031b1b83161693505b505050919050565b6000826147a7576147a7614718565b50049056fea26469706673582212207e340c5d07de4a2f4d50a93036b1b11ee50099351f121629cca1b1c9e229efd964736f6c63430008080033000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe80000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f0000000000000000000000001f741e2b12eb99aab61d737bd8b8951b641036470000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f984000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2

Deployed Bytecode

0x6080604052600436106100ec5760003560e01c80639d63848a1161008a578063dc4a484711610059578063dc4a484714610339578063dfef25d814610359578063e13c022c14610361578063fa461e331461038157600080fd5b80639d63848a1461029e578063ac9650d8146102d2578063b3461c87146102f2578063b7f68a0e1461032657600080fd5b80634a358d65116100c65780634a358d65146101ee5780635b5491821461021b578063797481d01461024f5780638662fa6a1461027e57600080fd5b806317fcb39b146101485780632d780db4146101995780634219dc40146101ba57600080fd5b3661014357336001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc216146101415760405163631b2c4f60e11b81523360048201526024015b60405180910390fd5b005b600080fd5b34801561015457600080fd5b5061017c7f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc281565b6040516001600160a01b0390911681526020015b60405180910390f35b6101ac6101a7366004613533565b6103a1565b604051908152602001610190565b3480156101c657600080fd5b5061017c7f0000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f81565b3480156101fa57600080fd5b5061020e610209366004613653565b610aca565b6040516101909190613749565b34801561022757600080fd5b5061017c7f0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f98481565b34801561025b57600080fd5b5061026f61026a36600461375c565b610f3f565b60405161019093929190613796565b34801561028a57600080fd5b5061020e6102993660046137c7565b610ffb565b3480156102aa57600080fd5b5061017c7f0000000000000000000000001f741e2b12eb99aab61d737bd8b8951b6410364781565b6102e56102e03660046137fb565b611215565b604051610190919061386f565b3480156102fe57600080fd5b5061017c7f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe881565b6101416103343660046138e6565b6113a2565b34801561034557600080fd5b5061020e6103543660046137c7565b6114f1565b610141611822565b34801561036d57600080fd5b5061020e61037c366004613916565b611834565b34801561038d57600080fd5b5061014161039c366004613951565b611957565b60004282610160013510156103be576103be826101600135611a4e565b60006104277f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe87f0000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f61041260208701876139d0565b61042260408801602089016139d0565b611a6a565b60408051633a9d71e760e01b8152908601356004820152606086013560248201529092506000915081906001600160a01b03841690633a9d71e790604401604080518083038186803b15801561047c57600080fd5b505afa158015610490573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104b491906139ed565b909250905060006105117f0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f9846104ec60208901896139d0565b6104fc60408a0160208b016139d0565b61050c60a08b0160808c01613a22565b611b1f565b905060008080808061052660208c018c6139d0565b61053660408d0160208e016139d0565b61054660a08e0160808f01613a22565b604080516001600160a01b039485166020820152939092169183019190915262ffffff1660608201526080016040516020818303038152906040529050600081604051602001610597929190613a3f565b60408051601f1981840301815291905290506105ba6101008c0160e08d01613a68565b6105c457876105c6565b865b156106c7576106236040518060a001604052808d60e00160208101906105ec9190613a68565b158152600060208201526101208e01356040808301919091528e013560608201526080018390526001600160a01b03881690611b49565b90935091506106396101008c0160e08d01613a68565b6106435787610645565b865b8311156106c7576106c16040518060a001604052808d60e001602081019061066d9190613a68565b15151581526020016001975087151581526020018d60e00160208101906106949190613a68565b61069e578a6106a0565b895b81526040808f01356020830152018390526001600160a01b03881690611b49565b90935091505b6106d86101008c0160e08d01613a68565b6106e257866106e4565b875b6106f3836101208e0135613a9b565b111561078b576001935060006040518060a001604052808d60e001602081019061071d9190613a68565b15151581526020016000975087151581526020018d60e00160208101906107449190613a68565b61074e5789610750565b8a5b8e61012001356107609190613a9b565b81526000602082015260400183905290506107846001600160a01b03881682611b49565b9094509250505b3361079c60a08d0160808e01613a22565b6107ac60c08e0160a08f016139d0565b8d60e00160208101906107bf9190613a68565b604080516001600160a01b03958616602082015262ffffff9094169084015292166060820152901515608082015285151560a082015284151560c082015260e081018390526101000160408051601f1981840301815261014083019091529150610977908061083160208f018f6139d0565b6001600160a01b031681526020018d602001602081019061085291906139d0565b6001600160a01b031681526020018d6040013581526020018d6060013581526020018d6101000160208101906108889190613a68565b15158e60e001602081019061089d9190613a68565b1515146108bc578d60a00160208101906108b791906139d0565b6108be565b305b6001600160a01b031681526020018d60c00160208101906108df91906139d0565b6001600160a01b031681526020018d6101000160208101906109019190613a68565b151581526020018d60e001602081019061091b9190613a68565b6109255785610936565b848e61012001356109369190613a9b565b81526020018d60e001602081019061094e9190613a68565b61096857848e61012001356109639190613a9b565b61096a565b855b8152602001839052611dfc565b50809a50505050505050505050508161014001358111156109b957604051633d14758360e11b8152600481018290526101408301356024820152604401610138565b60608201356109ce60408401602085016139d0565b6001600160a01b03166109e460208501856139d0565b6001600160a01b03167f319869ce99e1a1f0954078ca235aeb1867c1a21d61430b7ff0951b503488e80a6040860135610a2360a0880160808901613a22565b33610a3460c08a0160a08b016139d0565b610a4460e08b0160c08c016139d0565b610a556101008c0160e08d01613a68565b610a676101208d016101008e01613a68565b6040805197885262ffffff9690961660208801526001600160a01b039485168787015292841660608701529216608085015290151560a0840152151560c08301526101208881013560e084015261010083018890529051918290030190a4919050565b6040805160e08101825260008082526020820181905291810182905260608181018390526080820183905260a0820183905260c08201929092528260a00151806020019051810190610b1c9190613b12565b81516020830151919450919250610b54917f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe891611fa5565b8060800151610b67578260600151610b6d565b82604001515b8160c001818151610b7e9190613bdc565b90525060a081015115610cd5578051602082015160608301516080840151610baa578560600151610bb0565b85604001515b8460c0015186604051602001610bcb96959493929190613bf4565b60408051601f19818403018152610100830182528551835260208681015190840152838201516001600160a01b03169183019190915230606083015260808084018051159184019190915251909350339163b084c2529160a0820190610c32576000610c35565b60015b6001811115610c4657610c46613c42565b81526020018460a001518152602001858152506040518263ffffffff1660e01b8152600401610c759190613c6c565b600060405180830381600087803b158015610c8f57600080fd5b505af1158015610ca3573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610ccb9190810190613cf2565b9350610f39915050565b7f0000000000000000000000001f741e2b12eb99aab61d737bd8b8951b641036476001600160a01b0316634fd991ea60405180610160016040528084600001516001600160a01b0316815260200184602001516001600160a01b0316815260200186600001518152602001866020015181526020018460800151610d595730610d5f565b84606001515b6001600160a01b031681526020018460800151610d80578460600151610d82565b305b6001600160a01b03168152602001306001600160a01b031681526020018460800151610daf576000610db5565b8460c001515b81526020018460800151610dcd578460c00151610dd0565b60005b815260200160008152602001604051806020016040528060008152508152506040518263ffffffff1660e01b8152600401610e0b9190613d41565b600060405180830381600087803b158015610e2557600080fd5b505af1158015610e39573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610e619190810190613e12565b50610f0e604051806101400160405280336001600160a01b0316815260200183600001516001600160a01b0316815260200183602001516001600160a01b0316815260200185600001518152602001856020015181526020018360800151151581526020018360800151610ed6576000610edc565b85604001515b81526020018360800151610ef4578560600151610ef7565b60005b815260c0840151602082015260400184905261205b565b91508060c0015182604051602001610f27929190613e46565b60405160208183030381529060405291505b50919050565b60008060608180808080610f5660c08a018a613e5f565b810190610f639190613eac565b919e509c509a50939850919650945092509050610fc27f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe87f0000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f878761237f565b5084848484848a604051602001610fde96959493929190613f59565b604051602081830303815290604052955050505050509193909250565b606060008080808061101060a0880188613e5f565b81019061101d9190613f9d565b9a5093985091965094509250905060006110797f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe87f0000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f888861237f565b90506040518060e00160405280876001600160a01b03168152602001866001600160a01b03168152602001856001600160a01b03168152602001846001600160a01b031681526020018315158152602001836110d95789604001356110df565b89606001355b8152602001836110f35789606001356110f9565b89604001355b905260405161110d91908990602001614035565b60408051601f19818403018152610120830182528a35835260208b8101359084015230918301829052606083019190915233608083015297506001600160a01b038216906380e066799060a08101600181526020018561116e576000611174565b8b608001355b815260200185611188578b6080013561118b565b60005b81526020018a8152506040518263ffffffff1660e01b81526004016111b0919061409e565b600060405180830381600087803b1580156111ca57600080fd5b505af11580156111de573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526112069190810190614140565b9b9a5050505050505050505050565b6060816001600160401b0381111561122f5761122f613546565b60405190808252806020026020018201604052801561126257816020015b606081526020019060019003908161124d5790505b50905060005b8281101561139b576000803086868581811061128657611286614199565b90506020028101906112989190613e5f565b6040516112a69291906141af565b600060405180830381855af49150503d80600081146112e1576040519150601f19603f3d011682016040523d82523d6000602084013e6112e6565b606091505b5091509150816113685760448151101561133457604051635d2f436f60e11b815260206004820152600e60248201526d125b9d985b1a590814995cdd5b1d60921b6044820152606401610138565b6004810190508080602001905181019061134e91906141bf565b604051635d2f436f60e11b81526004016101389190613749565b8084848151811061137b5761137b614199565b60200260200101819052505050808061139390614207565b915050611268565b5092915050565b6040516370a0823160e01b81523060048201526000907f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b0316906370a082319060240160206040518083038186803b15801561140457600080fd5b505afa158015611418573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061143c9190614222565b90508281101561146257604051636b8c4dab60e01b815260048101829052602401610138565b80156114ec57604051632e1a7d4d60e01b8152600481018290527f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b031690632e1a7d4d90602401600060405180830381600087803b1580156114ca57600080fd5b505af11580156114de573d6000803e3d6000fd5b505050506114ec82826124b7565b505050565b606060008080808061150660a0880188613e5f565b810190611513919061423b565b9a5093985091965094509250905061154c7f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe88686611fa5565b61155c6060880160408901613a68565b61156a578660600135611570565b86608001355b61157a9082613bdc565b90507f0000000000000000000000001f741e2b12eb99aab61d737bd8b8951b641036476001600160a01b0316634fd991ea604051806101600160405280886001600160a01b03168152602001876001600160a01b031681526020018a6000013581526020018a6020013581526020018a60400160208101906115fc9190613a68565b6116065786611608565b305b6001600160a01b0316815260200161162660608c0160408d01613a68565b6116305730611632565b865b6001600160a01b031681523060208201526040908101906116599060608d01908d01613a68565b6116635784611666565b60005b815260200161167b60608c0160408d01613a68565b611686576000611688565b845b815260200160008152602001604051806020016040528060008152508152506040518263ffffffff1660e01b81526004016116c39190613d41565b600060405180830381600087803b1580156116dd57600080fd5b505af11580156116f1573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526117199190810190613e12565b506117f2604051806101400160405280336001600160a01b03168152602001876001600160a01b03168152602001866001600160a01b03168152602001896000013581526020018960200135815260200189604001602081019061177d9190613a68565b15815260200161179360608b0160408c01613a68565b61179d57846117a0565b60005b6117ae9060608c0135613bdc565b81526020016117c360608b0160408c01613a68565b6117ce5760006117d0565b845b6117de9060808c0135613bdc565b81526020018381526020018881525061205b565b95508086604051602001611807929190613e46565b60405160208183030381529060405295505050505050919050565b47156118325761183233476124b7565b565b606061185f7f0000000000000000000000001f741e2b12eb99aab61d737bd8b8951b64103647612557565b60006118a77f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe861189260208601866139d0565b6118a260408701602088016139d0565b612583565b90506001600160a01b03811663b2ceca77604085013560608601353360808801356118d35760016118d6565b60005b60808901356118e9578860a001356118ef565b88608001355b6040518663ffffffff1660e01b815260040161190f9594939291906142a8565b600060405180830381600087803b15801561192957600080fd5b505af115801561193d573d6000803e3d6000fd5b505060408051602081019091526000815295945050505050565b600080611966838501856142e8565b915091508115611a3b576000806000808480602001905181019061198a919061432d565b93509350935093506119be7f0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f984848484612608565b6001600160a01b038416301415611a0857611a033360008c136119e1578a6119e3565b8b5b60008d136119f157846119f3565b855b6001600160a01b03169190612648565b611a32565b611a3260008b13611a195782611a1b565b835b853360008e13611a2b578c6126ab565b8d5b6126ab565b50505050611a46565b611a46868683612795565b505050505050565b60405163070b57d960e41b815260048101829052602401610138565b600080611a816001600160a01b03871685856127ea565b6040516330af0bbf60e21b81526001600160a01b0380831660048301529193509086169063c2bc2efc9060240160206040518083038186803b158015611ac657600080fd5b505afa158015611ada573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611afe919061438c565b90506001600160a01b038116611b1657611b16612816565b94509492505050565b6000611b2d8585858561282f565b9050611b41816001600160a01b03166128c8565b949350505050565b6000806000846001600160a01b0316633850c7bd6040518163ffffffff1660e01b815260040160e06040518083038186803b158015611b8757600080fd5b505afa158015611b9b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611bbf91906143bb565b50505050505090506000846060015160001415611c10578451611c0057611bfb600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b611c1d565b611bfb6401000276a36001614482565b611c1d85606001516128ef565b90506401000276a36001600160a01b03821611611c4757611c446401000276a36001614482565b90505b73fffd8963efd1fc6a506488495d951d5263988d266001600160a01b03821610611c8d57611c8a600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b90505b8451611cad57816001600160a01b0316816001600160a01b031611611cc3565b816001600160a01b0316816001600160a01b0316105b15611df357600080876001600160a01b031663128acb083089600001518a60200151611d0457611cf68b60400151612945565b611cff906144ad565b611d11565b611d118b60400151612945565b878c608001516040518663ffffffff1660e01b8152600401611d379594939291906144ca565b6040805180830381600087803b158015611d5057600080fd5b505af1925050508015611d80575060408051601f3d908101601f19168201909252611d7d918101906139ed565b60015b611dc9573d808015611dae576040519150601f19603f3d011682016040523d82523d6000602084013e611db3565b606091505b50611dbd8161295b565b9093509150611dcc9050565b50505b8651611de15780611ddc836144ad565b611deb565b81611deb826144ad565b909650945050505b50509250929050565b600060606000611e567f000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe87f0000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f86600001518760200151611a6a565b9150508360000151846020015185608001518660a001518760c001518860e001518961010001518a6101200151604051602001611e9a989796959493929190614505565b60408051601f1981840301815260e08301825286820151835260608088015160208501523092840183905283019190915292506001600160a01b0382169063e0260d11906080810160018152602001611f038860e001518961010001518a60400151600161298b565b8152602001858152506040518263ffffffff1660e01b8152600401611f289190614558565b600060405180830381600087803b158015611f4257600080fd5b505af1158015611f56573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611f7e9190810190614140565b8051909550611f9a9350850160209081019250850190506145d0565b909590945092505050565b60405163d81e842360e01b81526001600160a01b03838116600483015282811660248301526000919085169063d81e84239060440160206040518083038186803b158015611ff257600080fd5b505afa158015612006573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061202a919061438c565b90506001600160a01b03811633146120555760405163a5fbb7d960e01b815260040160405180910390fd5b50505050565b606060008060008060008060008861012001518060200190518101906120819190614600565b96509650965096509650965096508261209a57806120af565b836120a9578860c001516120af565b8860e001515b1561221e5760006120ea7f0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f9848b602001518c604001518a61282f565b90508960a001511515851515146121015787612103565b305b60208b8101516040808e015181516001600160a01b03958616948101949094529184169083015291909116606082015262ffffff8816608082015260a0016040516020818303038152906040529850600189604051602001612166929190613a3f565b60405160208183030381529060405298506122086040518060c001604052808c60a0015115158815151461219a578861219c565b305b6001600160a01b03168152871560208201528615156040820152606001866121c457846121d9565b876121d3578c60c001516121d9565b8c60e001515b8152602001856121ed578c606001516121f0565b60005b81526020018b90526001600160a01b038316906129ce565b6040805160208101909152600081529a50925050505b8860a001511515841515141561233457808960a00151612242578960e00151612248565b8960c001515b11156122905761228b8960a0015161226457896040015161226a565b89602001515b8830611a2d858e60a00151612282578e60e001510390565b8e60c001510390565b6122fc565b808960a001516122a4578960e001516122aa565b8960c001515b10156122fc576122fc856122d48b60a001516122ca578b60e00151840390565b8b60c00151840390565b8b60a001516122e7578b604001516119f3565b8b602001516001600160a01b03169190612648565b61232f89600001518a60a00151612317578a60e001516122d4565b8a60c001518b60a001516122e7578b604001516119f3565b612373565b6123738960a0015161234a578960400151612350565b89602001515b888b600001518c60a00151612369578c60e001516126ab565b8c60c001516126ab565b50505050505050919050565b60405163d81e842360e01b81526001600160a01b03838116600483015282811660248301526000919086169063d81e84239060440160206040518083038186803b1580156123cc57600080fd5b505afa1580156123e0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612404919061438c565b6040516330af0bbf60e21b81526001600160a01b03808316600483015291925060009186169063c2bc2efc9060240160206040518083038186803b15801561244b57600080fd5b505afa15801561245f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612483919061438c565b90506001600160a01b03811633146124ae5760405163863b47e760e01b815260040160405180910390fd5b50949350505050565b604080516000808252602082019092526001600160a01b0384169083906040516124e19190614692565b60006040518083038185875af1925050503d806000811461251e576040519150601f19603f3d011682016040523d82523d6000602084013e612523565b606091505b50509050806114ec5760405163296c17bb60e21b81526001600160a01b038416600482015260248101839052604401610138565b6001600160a01b03811633146125805760405163d2bc8fc960e01b815260040160405180910390fd5b50565b60405163d81e842360e01b81526001600160a01b03838116600483015282811660248301526000919085169063d81e84239060440160206040518083038186803b1580156125d057600080fd5b505afa1580156125e4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611b41919061438c565b60006126168585858561282f565b90506001600160a01b0381163314612641576040516307c1698f60e01b815260040160405180910390fd5b5050505050565b6040516001600160a01b0383166024820152604481018290526114ec90849063a9059cbb60e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b031990931692909217909152612c2e565b7f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b0316846001600160a01b03161480156126ec5750804710155b15612780577f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b031663d0e30db0826040518263ffffffff1660e01b81526004016000604051808303818588803b15801561274c57600080fd5b505af1158015612760573d6000803e3d6000fd5b5061277b935050506001600160a01b03861690508383612648565b612055565b6120556001600160a01b038516848484612d00565b6000806000838060200190518101906127ae91906146ae565b9250925092506127e07f0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f984848484612608565b611a468686612d38565b60006127f7848484612583565b90506001600160a01b03811661280f5761280f612816565b9392505050565b60405163d92e233d60e01b815260040160405180910390fd5b604051630b4c774160e11b81526001600160a01b038481166004830152838116602483015262ffffff8316604483015260009190861690631698ee829060640160206040518083038186803b15801561288757600080fd5b505afa15801561289b573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128bf919061438c565b95945050505050565b6001600160a01b0381166125805760405163037064f160e01b815260040160405180910390fd5b60006001600160c01b03821161291c57612916612911604084901b6000612d5b565b612dd9565b92915050565b6000806129328468010000000000000000612e03565b91509150611b4161291183836000612e22565b6000600160ff1b821061295757600080fd5b5090565b60008061296f836377760aaf60e11b612eb0565b80602001905181019061298291906139ed565b91509150915091565b60006001600160801b0383116129b7576129a88584600185612f1e565b6129b29085613bdc565b6128bf565b6129c48484600085612f1e565b6128bf9086613bdc565b6000806000846001600160a01b0316633850c7bd6040518163ffffffff1660e01b815260040160e06040518083038186803b158015612a0c57600080fd5b505afa158015612a20573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612a4491906143bb565b50505050505090506000846080015160001415612a98578460200151612a8857612a83600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b612aa5565b612a836401000276a36001614482565b612aa585608001516128ef565b90506401000276a36001600160a01b03821611612acf57612acc6401000276a36001614482565b90505b73fffd8963efd1fc6a506488495d951d5263988d266001600160a01b03821610612b1557612b12600173fffd8963efd1fc6a506488495d951d5263988d2661445a565b90505b8460200151612b3857806001600160a01b0316826001600160a01b031610612b4e565b806001600160a01b0316826001600160a01b0316115b15611df357600080876001600160a01b031663128acb08886000015189602001518a60400151612b9357612b858b60600151612945565b612b8e906144ad565b612ba0565b612ba08b60600151612945565b878c60a001516040518663ffffffff1660e01b8152600401612bc69594939291906144ca565b6040805180830381600087803b158015612bdf57600080fd5b505af1158015612bf3573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612c1791906139ed565b915091508660200151611de15780611ddc836144ad565b6000612c83826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316612f459092919063ffffffff16565b8051909150156114ec5780806020019051810190612ca191906146fb565b6114ec5760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610138565b6040516001600160a01b03808516602483015283166044820152606481018290526120559085906323b872dd60e01b90608401612674565b6040516377760aaf60e11b81526004810183905260248101829052604401610138565b6000600019831415612d7557506001600160801b03612916565b82612d8257506000612916565b50816001818101901c5b81811015612db457809150600181828681612da957612da9614718565b0401901c9050612d8c565b50818015612dca575082612dc8828061472e565b105b156129165780611b4181614207565b60006001600160a01b0382111561295757604051630ba006e160e41b815260040160405180910390fd5b6000806000198385098385029250828110838203039150509250929050565b600082612e3a57612e338483612d5b565b905061280f565b6000612e498585600019612f54565b905060001991505b81811015612e6e57809150612e67858583612f54565b9050612e51565b8215612ea857600080612e818485612e03565b9150915080861180612e9257508187115b15612ea55783612ea181614207565b9450505b50505b509392505050565b81516060906020612ec2600483613a9b565b612ecc919061474d565b158015612ef457506001600160e01b03198316612ee885614761565b6001600160e01b031916145b15612f1657612f0e846004612f098185613a9b565b612f93565b915050612916565b835184602001fd5b600082612f35576129b285600160801b868561309f565b6128bf8585600160801b8561309f565b6060611b41848460008561319b565b600080612f6485858560006132cc565b90506000612f76826000866000613351565b9092509050612f898282600260006132cc565b9695505050505050565b606081612fa181601f613bdc565b1015612fe05760405162461bcd60e51b815260206004820152600e60248201526d736c6963655f6f766572666c6f7760901b6044820152606401610138565b612fea8284613bdc565b8451101561302e5760405162461bcd60e51b8152602060048201526011602482015270736c6963655f6f75744f66426f756e647360781b6044820152606401610138565b60608215801561304d57604051915060008252602082016040526124ae565b6040519150601f8416801560200281840101858101878315602002848b0101015b8183101561308657805183526020928301920161306e565b5050858452601f01601f19166040525050949350505050565b60008060006130ae8787612e03565b9150915080600014156130cf576130c68286866133c2565b92505050611b41565b8085116131005760405163362ced0960e11b8152600481018890526024810187905260448101869052606401610138565b600085878909600087810388169788900497600260038a028118808b02820302808b02820302808b02820302808b02820302808b02820302808b0290910302918190038190046001018684119095039485029290950394909404179283029350849050801561317e5750848061317857613178614718565b86880915155b15613191578261318d81614207565b9350505b5050949350505050565b6060824710156131fc5760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b6064820152608401610138565b6001600160a01b0385163b6132535760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610138565b600080866001600160a01b0316858760405161326f9190614692565b60006040518083038185875af1925050503d80600081146132ac576040519150601f19603f3d011682016040523d82523d6000602084013e6132b1565b606091505b50915091506132c18282866133f4565b979650505050505050565b6000806132da86868661342d565b9092509050801561330f57604051636aa917ab60e01b8152600481018790526024810186905260448101859052606401610138565b82156124ae576000806133228487612e03565b915091508087118061333357508188115b15613346578361334281614207565b9450505b505050949350505050565b818401848110828501810190801561337e578115806133795750613376600183613a9b565b86115b613382565b8186115b156133b8576040516347b1893f60e01b815260048101889052602481018790526044810186905260648101859052608401610138565b5094509492505050565b60006133ce8385614798565b90508180156133e557506133e2838561474d565b15155b1561280f57806128bf81614207565b6060831561340357508161280f565b8251156134135782518084602001fd5b8160405162461bcd60e51b81526004016101389190613749565b6000808361344857613441858460006133c2565b91506134d4565b6000613453846134dc565b9050600061346085613509565b90505b85156134b4576000806134768885612e03565b9150915061348686868484613351565b90965094506134958884612e03565b90925090506134a782828b6000613351565b9099509750613463915050565b6134cc84846134c58a8960006133c2565b6000613351565b909450925050505b935093915050565b6000816134fc576040516301de42a960e41b815260040160405180910390fd5b5060008190030460010190565b600081613529576040516319dbad9f60e01b815260040160405180910390fd5b5060008190030690565b60006101808284031215610f3957600080fd5b634e487b7160e01b600052604160045260246000fd5b60405160c081016001600160401b038111828210171561357e5761357e613546565b60405290565b60405160e081016001600160401b038111828210171561357e5761357e613546565b604051601f8201601f191681016001600160401b03811182821017156135ce576135ce613546565b604052919050565b60006001600160401b038211156135ef576135ef613546565b50601f01601f191660200190565b600082601f83011261360e57600080fd5b813561362161361c826135d6565b6135a6565b81815284602083860101111561363657600080fd5b816020850160208301376000918101602001919091529392505050565b60006020828403121561366557600080fd5b81356001600160401b038082111561367c57600080fd5b9083019060c0828603121561369057600080fd5b61369861355c565b823581526020830135602082015260408301356040820152606083013560608201526080830135608082015260a0830135828111156136d657600080fd5b6136e2878286016135fd565b60a08301525095945050505050565b60005b8381101561370c5781810151838201526020016136f4565b838111156120555750506000910152565b600081518084526137358160208601602086016136f1565b601f01601f19169290920160200192915050565b60208152600061280f602083018461371d565b60006020828403121561376e57600080fd5b81356001600160401b0381111561378457600080fd5b820160e0818503121561280f57600080fd5b8381528260208201526060604082015260006128bf606083018461371d565b600060c08284031215610f3957600080fd5b6000602082840312156137d957600080fd5b81356001600160401b038111156137ef57600080fd5b611b41848285016137b5565b6000806020838503121561380e57600080fd5b82356001600160401b038082111561382557600080fd5b818501915085601f83011261383957600080fd5b81358181111561384857600080fd5b8660208260051b850101111561385d57600080fd5b60209290920196919550909350505050565b6000602080830181845280855180835260408601915060408160051b870101925083870160005b828110156138c457603f198886030184526138b285835161371d565b94509285019290850190600101613896565b5092979650505050505050565b6001600160a01b038116811461258057600080fd5b600080604083850312156138f957600080fd5b82359150602083013561390b816138d1565b809150509250929050565b60006020828403121561392857600080fd5b81356001600160401b0381111561393e57600080fd5b8201610100818503121561280f57600080fd5b6000806000806060858703121561396757600080fd5b843593506020850135925060408501356001600160401b038082111561398c57600080fd5b818701915087601f8301126139a057600080fd5b8135818111156139af57600080fd5b8860208285010111156139c157600080fd5b95989497505060200194505050565b6000602082840312156139e257600080fd5b813561280f816138d1565b60008060408385031215613a0057600080fd5b505080516020909101519092909150565b62ffffff8116811461258057600080fd5b600060208284031215613a3457600080fd5b813561280f81613a11565b8215158152604060208201526000611b41604083018461371d565b801515811461258057600080fd5b600060208284031215613a7a57600080fd5b813561280f81613a5a565b634e487b7160e01b600052601160045260246000fd5b600082821015613aad57613aad613a85565b500390565b8051613abd81613a5a565b919050565b6000613ad061361c846135d6565b9050828152838383011115613ae457600080fd5b61280f8360208301846136f1565b600082601f830112613b0357600080fd5b61280f83835160208501613ac2565b600080828403610100811215613b2757600080fd5b60e0811215613b3557600080fd5b50613b3e613584565b8351613b49816138d1565b81526020840151613b59816138d1565b60208201526040840151613b6c816138d1565b60408201526060840151613b7f816138d1565b6060820152613b9060808501613ab2565b608082015260a084015160a082015260c084015160c08201528092505060e08301516001600160401b03811115613bc657600080fd5b613bd285828601613af2565b9150509250929050565b60008219821115613bef57613bef613a85565b500190565b6001600160a01b038781168252868116602083015285166040820152606081018490526080810183905260c060a08201819052600090613c369083018461371d565b98975050505050505050565b634e487b7160e01b600052602160045260246000fd5b60028110613c6857613c68613c42565b9052565b6020815281516020820152602082015160408201526000604083015160018060a01b03808216606085015280606086015116608085015250506080830151613cb860a084018215159052565b5060a0830151613ccb60c0840182613c58565b5060c083015160e083015260e0830151610100808185015250611b4161012084018261371d565b600080600060608486031215613d0757600080fd5b835192506020840151915060408401516001600160401b03811115613d2b57600080fd5b613d3786828701613af2565b9150509250925092565b60208152613d5b6020820183516001600160a01b03169052565b60006020830151613d7760408401826001600160a01b03169052565b5060408301516060830152606083015160808301526080830151613da660a08401826001600160a01b03169052565b5060a08301516001600160a01b03811660c08401525060c08301516001600160a01b03811660e08401525060e083015161010083810191909152830151610120808401919091528301516101408084019190915283015161016080840152611b4161018084018261371d565b600060208284031215613e2457600080fd5b81516001600160401b03811115613e3a57600080fd5b611b4184828501613af2565b828152604060208201526000611b41604083018461371d565b6000808335601e19843603018112613e7657600080fd5b8301803591506001600160401b03821115613e9057600080fd5b602001915036819003821315613ea557600080fd5b9250929050565b600080600080600080600080610100898b031215613ec957600080fd5b8835613ed4816138d1565b97506020890135613ee4816138d1565b96506040890135613ef4816138d1565b95506060890135613f04816138d1565b94506080890135613f1481613a5a565b935060a0890135925060c0890135915060e08901356001600160401b03811115613f3d57600080fd5b613f498b828c016135fd565b9150509295985092959890939650565b6001600160a01b0387811682528681166020830152858116604083015284166060820152821515608082015260c060a08201819052600090613c369083018461371d565b60008060008060008060c08789031215613fb657600080fd5b8635613fc1816138d1565b95506020870135613fd1816138d1565b94506040870135613fe1816138d1565b93506060870135613ff1816138d1565b9250608087013561400181613a5a565b915060a08701356001600160401b0381111561401c57600080fd5b61402889828a016135fd565b9150509295509295509295565b600061010060018060a01b038086511684528060208701511660208501528060408701511660408501528060608701511660608501525060808501511515608084015260a085015160a084015260c085015160c08401528060e08401526128bf8184018561371d565b602081528151602082015260208201516040820152600060408301516140cf60608401826001600160a01b03169052565b5060608301516001600160a01b03811660808401525060808301516001600160a01b03811660a08401525060a083015161410c60c0840182613c58565b5060c083015160e0838101919091528301516101008084019190915283015161012080840152611b4161014084018261371d565b6000806000806080858703121561415657600080fd5b84519350602085015192506040850151915060608501516001600160401b0381111561418157600080fd5b61418d87828801613af2565b91505092959194509250565b634e487b7160e01b600052603260045260246000fd5b8183823760009101908152919050565b6000602082840312156141d157600080fd5b81516001600160401b038111156141e757600080fd5b8201601f810184136141f857600080fd5b611b4184825160208401613ac2565b600060001982141561421b5761421b613a85565b5060010190565b60006020828403121561423457600080fd5b5051919050565b60008060008060008060c0878903121561425457600080fd5b863561425f816138d1565b9550602087013561426f816138d1565b9450604087013561427f816138d1565b9350606087013592506080870135915060a08701356001600160401b0381111561401c57600080fd5b858152602081018590526001600160a01b038416604082015260a08101600384106142d5576142d5613c42565b6060820193909352608001529392505050565b600080604083850312156142fb57600080fd5b823561430681613a5a565b915060208301356001600160401b0381111561432157600080fd5b613bd2858286016135fd565b6000806000806080858703121561434357600080fd5b845161434e816138d1565b602086015190945061435f816138d1565b6040860151909350614370816138d1565b606086015190925061438181613a11565b939692955090935050565b60006020828403121561439e57600080fd5b815161280f816138d1565b805161ffff81168114613abd57600080fd5b600080600080600080600060e0888a0312156143d657600080fd5b87516143e1816138d1565b8097505060208801518060020b81146143f957600080fd5b9550614407604089016143a9565b9450614415606089016143a9565b9350614423608089016143a9565b925060a088015160ff8116811461443957600080fd5b60c089015190925061444a81613a5a565b8091505092959891949750929550565b60006001600160a01b038381169083168181101561447a5761447a613a85565b039392505050565b60006001600160a01b038281168482168083038211156144a4576144a4613a85565b01949350505050565b6000600160ff1b8214156144c3576144c3613a85565b5060000390565b6001600160a01b0386811682528515156020830152604082018590528316606082015260a0608082018190526000906132c19083018461371d565b6001600160a01b0389811682528881166020830152878116604083015286166060820152841515608082015260a0810184905260c0810183905261010060e082018190526000906112068382018561371d565b6020815281516020820152602082015160408201526000604083015160018060a01b03808216606085015280606086015116608085015250506080830151600481106145a6576145a6613c42565b8060a08401525060a083015160c083015260c083015160e080840152611b4161010084018261371d565b600080604083850312156145e357600080fd5b8251915060208301516001600160401b03811115613bc657600080fd5b600080600080600080600060e0888a03121561461b57600080fd5b8751614626816138d1565b602089015190975061463781613a11565b6040890151909650614648816138d1565b606089015190955061465981613a5a565b608089015190945061466a81613a5a565b60a089015190935061467b81613a5a565b8092505060c0880151905092959891949750929550565b600082516146a48184602087016136f1565b9190910192915050565b6000806000606084860312156146c357600080fd5b83516146ce816138d1565b60208501519093506146df816138d1565b60408501519092506146f081613a11565b809150509250925092565b60006020828403121561470d57600080fd5b815161280f81613a5a565b634e487b7160e01b600052601260045260246000fd5b600081600019048311821515161561474857614748613a85565b500290565b60008261475c5761475c614718565b500690565b805160208201516001600160e01b031980821692919060048310156147905780818460040360031b1b83161693505b505050919050565b6000826147a7576147a7614718565b50049056fea26469706673582212207e340c5d07de4a2f4d50a93036b1b11ee50099351f121629cca1b1c9e229efd964736f6c63430008080033

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

000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe80000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f0000000000000000000000001f741e2b12eb99aab61d737bd8b8951b641036470000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f984000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2

-----Decoded View---------------
Arg [0] : chosenOptionFactory (address): 0xcf0aca5c5b7e1bF63514D362243b6c50d5761FE8
Arg [1] : chosenPoolFactory (address): 0x8E3fcda01FCA533989C3CeABdcF2A6E0A776585F
Arg [2] : chosenTokens (address): 0x1f741e2b12eb99AAb61d737Bd8b8951b64103647
Arg [3] : chosenUniswapV3Factory (address): 0x1F98431c8aD98523631AE4a59f267346ea31F984
Arg [4] : chosenNative (address): 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2

-----Encoded View---------------
5 Constructor Arguments found :
Arg [0] : 000000000000000000000000cf0aca5c5b7e1bf63514d362243b6c50d5761fe8
Arg [1] : 0000000000000000000000008e3fcda01fca533989c3ceabdcf2a6e0a776585f
Arg [2] : 0000000000000000000000001f741e2b12eb99aab61d737bd8b8951b64103647
Arg [3] : 0000000000000000000000001f98431c8ad98523631ae4a59f267346ea31f984
Arg [4] : 000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2


Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading
Loading...
Loading
[ Download: CSV Export  ]
[ Download: CSV Export  ]

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