ETH Price: $2,375.11 (+8.32%)
 

Overview

ETH Balance

0 ETH

ETH Value

$0.00

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Block
From
To

There are no matching entries

1 Internal Transaction found.

Latest 1 internal transaction

Parent Transaction Hash Block From To
417362402026-02-05 3:23:4768 days ago1770261827  Contract Creation0 ETH

Cross-Chain Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
KSRemoveLiquidityPancakeV4CLHook

Compiler Version
v0.8.30+commit.73712a01

Optimization Enabled:
Yes with 44444444 runs

Other Settings:
prague EvmVersion
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

import {TokenHelper} from 'ks-common-sc/src/libraries/token/TokenHelper.sol';

import {ICLPoolManager} from '../../interfaces/pancakev4/ICLPoolManager.sol';
import {ICLPositionManager} from '../../interfaces/pancakev4/ICLPositionManager.sol';
import {PoolId, PoolKey, TickInfo} from '../../interfaces/pancakev4/Types.sol';
import {LiquidityAmounts} from '../../libraries/uniswapv4/LiquidityAmounts.sol';
import {TickMath} from '../../libraries/uniswapv4/TickMath.sol';
import {BaseTickBasedRemoveLiquidityHook} from '../base/BaseTickBasedRemoveLiquidityHook.sol';

import {ActionData} from '../../types/ActionData.sol';
import {IntentData} from '../../types/IntentData.sol';

import {Math} from 'openzeppelin-contracts/contracts/utils/math/Math.sol';

contract KSRemoveLiquidityPancakeV4CLHook is BaseTickBasedRemoveLiquidityHook {
  using TokenHelper for address;

  /**
   * @notice Parameters used for remove liquidity validation of a pancake v4 CL position
   * @param clPoolManager The cl pool manager contract
   * @param poolId The pool ID
   * @param removeLiqParams The params used to remove liquidity
   * @param outputParams The params used to validate output after execution
   */
  struct PancakeV4CLParams {
    ICLPoolManager clPoolManager;
    PoolId poolId;
    RemoveLiquidityParams removeLiqParams;
    OutputValidationParams outputParams;
  }

  constructor(address _weth) BaseTickBasedRemoveLiquidityHook(_weth) {}

  function _validateBeforeExecution(IntentData calldata intentData, ActionData calldata actionData)
    internal
    view
    override
    returns (bytes memory beforeExecutionData)
  {
    PancakeV4CLParams memory pancakeCL;

    RemoveLiquidityHookData calldata validationData =
      _decodeHookData(intentData.coreData.hookIntentData);

    _cacheValidationData(pancakeCL, validationData, actionData.hookActionData);

    _validateERC721Data(
      pancakeCL.removeLiqParams.positionInfo.nftAddress,
      pancakeCL.removeLiqParams.positionInfo.nftId,
      intentData.tokenData.erc721Data[actionData.erc721Ids[0]].token,
      intentData.tokenData.erc721Data[actionData.erc721Ids[0]].tokenId
    );

    _validateConditions(
      validationData.nodes[pancakeCL.removeLiqParams.index],
      pancakeCL.removeLiqParams.positionInfo.feesGenerated[0],
      pancakeCL.removeLiqParams.positionInfo.feesGenerated[1],
      pancakeCL.removeLiqParams.sqrtPriceX96
    );

    beforeExecutionData = abi.encode(pancakeCL.removeLiqParams, pancakeCL.outputParams);
  }

  function _cacheValidationData(
    PancakeV4CLParams memory pancakeCL,
    RemoveLiquidityHookData calldata validationData,
    bytes calldata hookActionData
  ) internal view {
    OutputValidationParams memory outputParams = pancakeCL.outputParams;
    RemoveLiquidityParams memory removeLiqParams = pancakeCL.removeLiqParams;

    _cacheBaseData(validationData, hookActionData, removeLiqParams, outputParams);

    ICLPositionManager positionManager = ICLPositionManager(removeLiqParams.positionInfo.nftAddress);
    PoolKey memory poolKey;
    (
      poolKey,
      removeLiqParams.positionInfo.ticks[0],
      removeLiqParams.positionInfo.ticks[1],
      removeLiqParams.positionInfo.liquidity,
      removeLiqParams.positionInfo.feesGrowthInsideLast[0],
      removeLiqParams.positionInfo.feesGrowthInsideLast[1],
    ) = positionManager.positions(removeLiqParams.positionInfo.nftId);
    pancakeCL.poolId = _toId(poolKey);

    (removeLiqParams.sqrtPriceX96, removeLiqParams.currentTick,,) =
      (pancakeCL.clPoolManager = positionManager.clPoolManager()).getSlot0(pancakeCL.poolId);
    outputParams.tokens = [_toNative(poolKey.currency0), _toNative(poolKey.currency1)];

    if (removeLiqParams.wrapOrUnwrap) {
      outputParams.tokens = _adjustTokens(outputParams.tokens);
    }

    (outputParams.balancesBefore[0], outputParams.balancesBefore[1]) =
      _recordRouterBalances(msg.sender, outputParams.tokens);
    _computePositionValues(pancakeCL);
  }

  function _computePositionValues(PancakeV4CLParams memory pancakeCL) internal view {
    PositionInfo memory positionInfo = pancakeCL.removeLiqParams.positionInfo;
    RemoveLiquidityParams memory removeLiqParams = pancakeCL.removeLiqParams;

    int24 tickLower = positionInfo.ticks[0];
    int24 tickCurrent = removeLiqParams.currentTick;
    int24 tickUpper = positionInfo.ticks[1];

    if (removeLiqParams.liquidityToRemove != 0) {
      uint160 sqrtPriceLower = TickMath.getSqrtRatioAtTick(tickLower);
      uint160 sqrtPriceUpper = TickMath.getSqrtRatioAtTick(tickUpper);
      (positionInfo.amounts[0], positionInfo.amounts[1]) = LiquidityAmounts.getAmountsForLiquidity(
        removeLiqParams.sqrtPriceX96,
        sqrtPriceLower,
        sqrtPriceUpper,
        uint128(removeLiqParams.liquidityToRemove)
      );
    }

    (uint256 feeGrowthInside0, uint256 feeGrowthInside1) = _getFeeGrowthInside(
      pancakeCL.clPoolManager, pancakeCL.poolId, tickLower, tickCurrent, tickUpper
    );

    unchecked {
      positionInfo.unclaimedFees[0] = Math.mulDiv(
        feeGrowthInside0 - positionInfo.feesGrowthInsideLast[0], positionInfo.liquidity, Q128
      );
      positionInfo.unclaimedFees[1] = Math.mulDiv(
        feeGrowthInside1 - positionInfo.feesGrowthInsideLast[1], positionInfo.liquidity, Q128
      );
    }
  }

  function _getFeeGrowthInside(
    ICLPoolManager clPoolManager,
    PoolId poolId,
    int24 tickLower,
    int24 tickCurrent,
    int24 tickUpper
  ) internal view returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) {
    (uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128) =
      clPoolManager.getFeeGrowthGlobals(poolId);

    TickInfo memory lower = clPoolManager.getPoolTickInfo(poolId, tickLower);
    TickInfo memory upper = clPoolManager.getPoolTickInfo(poolId, tickUpper);

    uint256 feeGrowthBelow0X128;
    uint256 feeGrowthBelow1X128;
    unchecked {
      if (tickCurrent >= tickLower) {
        feeGrowthBelow0X128 = lower.feeGrowthOutside0X128;
        feeGrowthBelow1X128 = lower.feeGrowthOutside1X128;
      } else {
        feeGrowthBelow0X128 = feeGrowthGlobal0X128 - lower.feeGrowthOutside0X128;
        feeGrowthBelow1X128 = feeGrowthGlobal1X128 - lower.feeGrowthOutside1X128;
      }

      uint256 feeGrowthAbove0X128;
      uint256 feeGrowthAbove1X128;
      if (tickCurrent < tickUpper) {
        feeGrowthAbove0X128 = upper.feeGrowthOutside0X128;
        feeGrowthAbove1X128 = upper.feeGrowthOutside1X128;
      } else {
        feeGrowthAbove0X128 = feeGrowthGlobal0X128 - upper.feeGrowthOutside0X128;
        feeGrowthAbove1X128 = feeGrowthGlobal1X128 - upper.feeGrowthOutside1X128;
      }

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

  function _toId(PoolKey memory poolKey) internal pure returns (PoolId poolId) {
    assembly ('memory-safe') {
      poolId := keccak256(poolKey, 0xc0)
    }
  }
}

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

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

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

/// @title Library for transferring, approving and holding native tokens and ERC20 tokens
/// @dev This library is based on CurrencyLibrary.sol from Uniswap/v4-core and SafeTransferLib.sol from transmissions11/solmate
library TokenHelper {
  /// @notice Additional context for ERC-7751 wrapped error when a native transfer fails
  error NativeTransferFailed();

  /// @notice Additional context for ERC-7751 wrapped error when an ERC20 transfer fails
  error ERC20TransferFailed();

  /// @notice Additional context for ERC-7751 wrapped error when an ERC20 approve fails
  error ERC20ApproveFailed();

  address internal constant NATIVE_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

  function isNative(address token) internal pure returns (bool) {
    return token == NATIVE_ADDRESS || token == address(0);
  }

  function safeTransferNative(address to, uint256 amount) internal {
    if (amount == 0) return;

    bool success;
    assembly ('memory-safe') {
      // Transfer the ETH and revert if it fails.
      success := call(gas(), to, amount, 0, 0, 0, 0)
    }
    // revert with NativeTransferFailed, containing the bubbled up error as an argument
    if (!success) {
      CustomRevert.bubbleUpAndRevertWith(to, bytes4(0), NativeTransferFailed.selector);
    }
  }

  function safeTransferERC20(address token, address to, uint256 amount) internal {
    if (amount == 0) return;

    bool success;
    assembly ('memory-safe') {
      // Get a pointer to some free memory.
      let fmp := mload(0x40)

      // Write the abi-encoded calldata into memory, beginning with the function selector.
      mstore(fmp, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
      mstore(add(fmp, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "to" argument.
      mstore(add(fmp, 36), amount) // Append the "amount" argument. Masking not required as it's a full 32 byte type.

      success := and(
        // Set success to whether the call reverted, if not we check it either
        // returned exactly 1 (can't just be non-zero data), or had no return data.
        or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
        // We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
        // We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
        // Counterintuitively, this call must be positioned second to the or() call in the
        // surrounding and() call or else returndatasize() will be zero during the computation.
        call(gas(), token, 0, fmp, 68, 0, 32)
      )

      // Now clean the memory we used
      mstore(fmp, 0) // 4 byte `selector` and 28 bytes of `to` were stored here
      mstore(add(fmp, 0x20), 0) // 4 bytes of `to` and 28 bytes of `amount` were stored here
      mstore(add(fmp, 0x40), 0) // 4 bytes of `amount` were stored here
    }
    // revert with ERC20TransferFailed, containing the bubbled up error as an argument
    if (!success) {
      CustomRevert.bubbleUpAndRevertWith(
        token, IERC20.transfer.selector, ERC20TransferFailed.selector
      );
    }
  }

  function safeTransfer(address token, address to, uint256 amount) internal {
    if (isNative(token)) {
      safeTransferNative(to, amount);
    } else {
      safeTransferERC20(token, to, amount);
    }
  }

  function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
    if (amount == 0) return;

    bool success;
    assembly ('memory-safe') {
      // Get a pointer to some free memory.
      let fmp := mload(0x40)

      // Write the abi-encoded calldata into memory, beginning with the function selector.
      mstore(fmp, 0x23b872dd00000000000000000000000000000000000000000000000000000000)
      mstore(add(fmp, 4), and(from, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "from" argument.
      mstore(add(fmp, 36), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "to" argument.
      mstore(add(fmp, 68), amount) // Append the "amount" argument. Masking not required as it's a full 32 byte type.

      success := and(
        // Set success to whether the call reverted, if not we check it either
        // returned exactly 1 (can't just be non-zero data), or had no return data.
        or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
        // We use 100 because the length of our calldata totals up like so: 4 + 32 * 3.
        // We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
        // Counterintuitively, this call must be positioned second to the or() call in the
        // surrounding and() call or else returndatasize() will be zero during the computation.
        call(gas(), token, 0, fmp, 100, 0, 32)
      )

      // Now clean the memory we used
      mstore(fmp, 0) // 4 byte `selector` and 28 bytes of `from` were stored here
      mstore(add(fmp, 0x20), 0) // 4 bytes of `from` and 28 bytes of `to` were stored here
      mstore(add(fmp, 0x40), 0) // 4 bytes of `to` and 28 bytes of `amount` were stored here
      mstore(add(fmp, 0x60), 0) // 4 bytes of `amount` were stored here
    }
    // revert with ERC20TransferFailed, containing the bubbled up error as an argument
    if (!success) {
      CustomRevert.bubbleUpAndRevertWith(
        token, IERC20.transferFrom.selector, ERC20TransferFailed.selector
      );
    }
  }

  function trySafeApprove(address token, address spender, uint256 amount)
    internal
    returns (bool success)
  {
    assembly ('memory-safe') {
      // Get a pointer to some free memory.
      let fmp := mload(0x40)

      // Write the abi-encoded calldata into memory, beginning with the function selector.
      mstore(fmp, 0x095ea7b300000000000000000000000000000000000000000000000000000000)
      mstore(add(fmp, 4), and(spender, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "spender" argument.
      mstore(add(fmp, 36), amount) // Append the "amount" argument. Masking not required as it's a full 32 byte type.

      success := and(
        // Set success to whether the call reverted, if not we check it either
        // returned exactly 1 (can't just be non-zero data), or had no return data.
        or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
        // We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
        // We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
        // Counterintuitively, this call must be positioned second to the or() call in the
        // surrounding and() call or else returndatasize() will be zero during the computation.
        call(gas(), token, 0, fmp, 68, 0, 32)
      )

      // Now clean the memory we used
      mstore(fmp, 0) // 4 byte `selector` and 28 bytes of `to` were stored here
      mstore(add(fmp, 0x20), 0) // 4 bytes of `to` and 28 bytes of `amount` were stored here
      mstore(add(fmp, 0x40), 0) // 4 bytes of `amount` were stored here
    }
  }

  function safeApprove(address token, address spender, uint256 amount) internal {
    // revert with ERC20ApproveFailed, containing the bubbled up error as an argument
    if (!trySafeApprove(token, spender, amount)) {
      CustomRevert.bubbleUpAndRevertWith(
        token, IERC20.approve.selector, ERC20ApproveFailed.selector
      );
    }
  }

  function forceApprove(address token, address spender, uint256 amount) internal {
    // meant to be used with tokens that require the approval to be set to zero before setting it to a non-zero value, such as USDT
    if (!trySafeApprove(token, spender, amount)) {
      safeApprove(token, spender, 0);
      safeApprove(token, spender, amount);
    }
  }

  function balanceOf(address token, address account) internal view returns (uint256) {
    if (isNative(token)) {
      return account.balance;
    } else {
      return IERC20(token).balanceOf(account);
    }
  }

  function selfBalance(address token) internal view returns (uint256) {
    if (isNative(token)) {
      return address(this).balance;
    } else {
      return IERC20(token).balanceOf(address(this));
    }
  }
}

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

import {BalanceDelta, PoolId, PoolKey, TickInfo} from './Types.sol';

interface ICLPoolManager {
  /// @notice Thrown when trying to interact with a non-initialized pool
  error PoolNotInitialized();

  /// @notice PoolKey must have currencies where address(currency0) < address(currency1)
  error CurrenciesInitializedOutOfOrder(address currency0, address currency1);

  /// @notice Thrown when a call to updateDynamicLPFee is made by an address that is not the hook,
  /// or on a pool is not a dynamic fee pool.
  error UnauthorizedDynamicLPFeeUpdate();

  /// @notice Emitted when lp fee is updated
  /// @dev The event is emitted even if the updated fee value is the same as previous one
  event DynamicLPFeeUpdated(PoolId indexed id, uint24 dynamicLPFee);

  /// @notice Updates lp fee for a dyanmic fee pool
  /// @dev Some of the use case could be:
  ///   1) when hook#beforeSwap() is called and hook call this function to update the lp fee
  ///   2) For BinPool only, when hook#beforeMint() is called and hook call this function to update the lp fee
  ///   3) other use case where the hook might want to on an ad-hoc basis increase/reduce lp fee
  function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicLPFee) external;

  /// @notice Return PoolKey for a given PoolId
  function poolIdToPoolKey(PoolId id) external view returns (PoolKey memory key);

  /// @notice PoolManagerMismatch is thrown when pool manager specified in the pool key does not match current contract
  error PoolManagerMismatch();
  /// @notice Pools are limited to type(int16).max tickSpacing in #initialize, to prevent overflow
  error TickSpacingTooLarge(int24 tickSpacing);
  /// @notice Pools must have a positive non-zero tickSpacing passed to #initialize
  error TickSpacingTooSmall(int24 tickSpacing);
  /// @notice Error thrown when add liquidity is called when paused()
  error PoolPaused();
  /// @notice Thrown when trying to swap amount of 0
  error SwapAmountCannotBeZero();

  /// @notice Emitted when a liquidity position is modified
  /// @param id The abi encoded hash of the pool key struct for the pool that was modified
  /// @param sender The address that modified the pool
  /// @param tickLower The lower tick of the position
  /// @param tickUpper The upper tick of the position
  /// @param liquidityDelta The amount of liquidity that was added or removed
  /// @param salt The value used to create a unique liquidity position
  event ModifyLiquidity(
    PoolId indexed id,
    address indexed sender,
    int24 tickLower,
    int24 tickUpper,
    int256 liquidityDelta,
    bytes32 salt
  );

  /// @notice Emitted for swaps between currency0 and currency1
  /// @param id The abi encoded hash of the pool key struct for the pool that was modified
  /// @param sender The address that initiated the swap call, and that received the callback
  /// @param amount0 The delta of the currency0 balance of the pool
  /// @param amount1 The delta of the currency1 balance of the pool
  /// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
  /// @param liquidity The liquidity of the pool after the swap
  /// @param tick The log base 1.0001 of the price of the pool after the swap
  /// @param fee The fee collected upon every swap in the pool (including protocol fee and LP fee), denominated in hundredths of a bip
  /// @param protocolFee Single direction protocol fee from the swap, also denominated in hundredths of a bip
  event Swap(
    PoolId indexed id,
    address indexed sender,
    int128 amount0,
    int128 amount1,
    uint160 sqrtPriceX96,
    uint128 liquidity,
    int24 tick,
    uint24 fee,
    uint16 protocolFee
  );

  /// @notice Emitted when donate happen
  /// @param id The abi encoded hash of the pool key struct for the pool that was modified
  /// @param sender The address that modified the pool
  /// @param amount0 The delta of the currency0 balance of the pool
  /// @param amount1 The delta of the currency1 balance of the pool
  /// @param tick The donated tick
  event Donate(
    PoolId indexed id, address indexed sender, uint256 amount0, uint256 amount1, int24 tick
  );

  /// @notice Get the current value in slot0 of the given pool
  function getSlot0(PoolId id)
    external
    view
    returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee);

  /// @notice Get the current value of liquidity of the given pool
  function getLiquidity(PoolId id) external view returns (uint128 liquidity);

  /// @notice Get the current value of liquidity for the specified pool and position
  function getLiquidity(PoolId id, address owner, int24 tickLower, int24 tickUpper, bytes32 salt)
    external
    view
    returns (uint128 liquidity);

  /// @notice Get the tick info about a specific tick in the pool
  function getPoolTickInfo(PoolId id, int24 tick) external view returns (TickInfo memory tickInfo);

  /// @notice Get the tick bitmap info about a specific range (a word range) in the pool
  function getPoolBitmapInfo(PoolId id, int16 word) external view returns (uint256 tickBitmap);

  /// @notice Get the fee growth global for the given pool
  function getFeeGrowthGlobals(PoolId id)
    external
    view
    returns (uint256 feeGrowthGlobal0x128, uint256 feeGrowthGlobal1x128);

  /// @notice Initialize the state for a given pool ID
  function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);

  struct ModifyLiquidityParams {
    // the lower and upper tick of the position
    int24 tickLower;
    int24 tickUpper;
    // how to modify the liquidity
    int256 liquidityDelta;
    // a value to set if you want unique liquidity positions at the same range
    bytes32 salt;
  }

  /// @notice Modify the position for the given pool
  /// @return delta The total balance delta of the caller of modifyLiquidity.
  /// @return feeDelta The balance delta of the fees generated in the liquidity range.
  function modifyLiquidity(
    PoolKey memory key,
    ModifyLiquidityParams memory params,
    bytes calldata hookData
  ) external returns (BalanceDelta delta, BalanceDelta feeDelta);

  struct SwapParams {
    bool zeroForOne;
    int256 amountSpecified;
    uint160 sqrtPriceLimitX96;
  }

  /// @notice Swap against the given pool
  /// @param key The pool to swap in
  /// @param params The parameters for swapping
  /// @param hookData Any data to pass to the callback
  /// @return delta The balance delta of the address swapping
  /// @dev Swapping on low liquidity pools may cause unexpected swap amounts when liquidity available is less than amountSpecified.
  /// Additionally note that if interacting with hooks that have the BEFORE_SWAP_RETURNS_DELTA_FLAG or AFTER_SWAP_RETURNS_DELTA_FLAG
  /// the hook may alter the swap input/output. Integrators should perform checks on the returned swapDelta.
  function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData)
    external
    returns (BalanceDelta delta);
}

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

import {ICLPoolManager} from './ICLPoolManager.sol';
import {BalanceDelta, CLPositionInfo, PoolKey} from './Types.sol';

interface ICLPositionManager {
  /// @notice Thrown when the block.timestamp exceeds the user-provided deadline
  error DeadlinePassed(uint256 deadline);

  /// @notice Thrown when calling transfer, subscribe, or unsubscribe on CLPositionManager
  /// or batchTransferFrom on BinPositionManager when the vault is locked.
  /// @dev This is to prevent hooks from being able to trigger actions or notifications at the same time the position is being modified.
  error VaultMustBeUnlocked();

  /// @notice Thrown when the token ID is bind to an unexisting pool
  error InvalidTokenID();

  /// @notice Unlocks Vault and batches actions for modifying liquidity
  /// @dev This is the standard entrypoint for the PositionManager
  /// @param payload is an encoding of actions, and parameters for those actions
  /// @param deadline is the deadline for the batched actions to be executed
  function modifyLiquidities(bytes calldata payload, uint256 deadline) external payable;

  /// @notice Batches actions for modifying liquidity without getting a lock from vault
  /// @dev This must be called by a contract that has already locked the vault
  /// @param actions the actions to perform
  /// @param params the parameters to provide for the actions
  function modifyLiquiditiesWithoutLock(bytes calldata actions, bytes[] calldata params)
    external
    payable;

  /// @notice Thrown when the caller is not approved to modify a position
  error NotApproved(address caller);

  /// @notice Emitted when a new liquidity position is minted
  event MintPosition(uint256 indexed tokenId);

  /// @notice Emitted when liquidity is modified
  /// @param tokenId the tokenId of the position that was modified
  /// @param liquidityChange the change in liquidity of the position
  /// @param feesAccrued the fees collected from the liquidity change
  event ModifyLiquidity(uint256 indexed tokenId, int256 liquidityChange, BalanceDelta feesAccrued);

  /// @notice Get the clPoolManager
  function clPoolManager() external view returns (ICLPoolManager);

  /// @notice Initialize a v4 PCS cl pool
  /// @param key the PoolKey of the pool to initialize
  /// @param sqrtPriceX96 the initial sqrtPriceX96 of the pool
  function initializePool(PoolKey calldata key, uint160 sqrtPriceX96)
    external
    payable
    returns (int24);

  /// @notice Used to get the ID that will be used for the next minted liquidity position
  /// @return uint256 The next token ID
  function nextTokenId() external view returns (uint256);

  /// @param tokenId the ERC721 tokenId
  /// @return liquidity the position's liquidity, as a liquidityAmount
  /// @dev this value can be processed as an amount0 and amount1 by using the LiquidityAmounts library
  function getPositionLiquidity(uint256 tokenId) external view returns (uint128 liquidity);

  /// @notice Get the detailed information for a specified position
  /// @param tokenId the ERC721 tokenId
  /// @return poolKey the pool key of the position
  /// @return tickLower the lower tick of the position
  /// @return tickUpper the upper tick of the position
  /// @return liquidity the liquidity of the position
  /// @return feeGrowthInside0LastX128 the fee growth count of token0 since last time updated
  /// @return feeGrowthInside1LastX128 the fee growth count of token1 since last time updated
  /// @return _subscriber the address of the subscriber, if not set, it returns address(0)
  function positions(uint256 tokenId)
    external
    view
    returns (
      PoolKey memory poolKey,
      int24 tickLower,
      int24 tickUpper,
      uint128 liquidity,
      uint256 feeGrowthInside0LastX128,
      uint256 feeGrowthInside1LastX128,
      address _subscriber
    );

  /// @param tokenId the ERC721 tokenId
  /// @return poolKey the pool key of the position
  /// @return CLPositionInfo a uint256 packed value holding information about the position including the range (tickLower, tickUpper)
  function getPoolAndPositionInfo(uint256 tokenId)
    external
    view
    returns (PoolKey memory, CLPositionInfo);

  function approve(address to, uint256 tokenId) external;

  function ownerOf(uint256 _tokenId) external view returns (address);

  function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;

  function transferFrom(address _from, address _to, uint256 _tokenId) external payable;

  function poolKeys(bytes25 poolId) external view returns (PoolKey memory);
}

File 5 of 43 : Types.sol
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

type PoolId is bytes32;

type BalanceDelta is int256;

type CLPositionInfo is uint256;

// info stored for each initialized individual tick
struct TickInfo {
  // the total position liquidity that references this tick
  uint128 liquidityGross;
  // amount of net liquidity added (subtracted) when tick is crossed from left to right (right to left),
  int128 liquidityNet;
  // fee growth per unit of liquidity on the _other_ side of this tick (relative to the current tick)
  // only has relative meaning, not absolute — the value depends on when the tick is initialized
  uint256 feeGrowthOutside0X128;
  uint256 feeGrowthOutside1X128;
}

/// @notice Returns the key for identifying a pool
struct PoolKey {
  /// @notice The lower currency of the pool, sorted numerically
  address currency0;
  /// @notice The higher currency of the pool, sorted numerically
  address currency1;
  /// @notice The hooks of the pool, won't have a general interface because hooks interface vary on pool type
  address hooks;
  /// @notice The pool manager of the pool
  address poolManager;
  /// @notice The pool lp fee, capped at 1_000_000. If the pool has a dynamic fee then it must be exactly equal to 0x800000
  uint24 fee;
  /// @notice Hooks callback and pool specific parameters, i.e. tickSpacing for CL, binStep for bin
  bytes32 parameters;
}

/// @notice Library to define different pool actions.
/// @dev These are suggested common commands, however additional commands should be defined as required
library Actions {
  // cl-pool actions
  // liquidity actions
  uint256 constant CL_INCREASE_LIQUIDITY = 0x00;
  uint256 constant CL_DECREASE_LIQUIDITY = 0x01;
  uint256 constant CL_MINT_POSITION = 0x02;
  uint256 constant CL_BURN_POSITION = 0x03;
  uint256 constant CL_INCREASE_LIQUIDITY_FROM_DELTAS = 0x04;
  uint256 constant CL_MINT_POSITION_FROM_DELTAS = 0x05;

  // swapping
  uint256 constant CL_SWAP_EXACT_IN_SINGLE = 0x06;
  uint256 constant CL_SWAP_EXACT_IN = 0x07;
  uint256 constant CL_SWAP_EXACT_OUT_SINGLE = 0x08;
  uint256 constant CL_SWAP_EXACT_OUT = 0x09;

  // donate
  /// @dev this is not supported in the position manager or router
  uint256 constant CL_DONATE = 0x0a;

  // closing deltas on the pool manager
  // settling
  uint256 constant SETTLE = 0x0b;
  uint256 constant SETTLE_ALL = 0x0c;
  uint256 constant SETTLE_PAIR = 0x0d;
  // taking
  uint256 constant TAKE = 0x0e;
  uint256 constant TAKE_ALL = 0x0f;
  uint256 constant TAKE_PORTION = 0x10;
  uint256 constant TAKE_PAIR = 0x11;

  uint256 constant CLOSE_CURRENCY = 0x12;
  uint256 constant CLEAR_OR_TAKE = 0x13;
  uint256 constant SWEEP = 0x14;
  uint256 constant WRAP = 0x15;
  uint256 constant UNWRAP = 0x16;

  // minting/burning 6909s to close deltas
  /// @dev this is not supported in the position manager or router
  uint256 constant MINT_6909 = 0x17;
  uint256 constant BURN_6909 = 0x18;

  // bin-pool actions
  // liquidity actions
  uint256 constant BIN_ADD_LIQUIDITY = 0x19;
  uint256 constant BIN_REMOVE_LIQUIDITY = 0x1a;
  uint256 constant BIN_ADD_LIQUIDITY_FROM_DELTAS = 0x1b;
  // swapping
  uint256 constant BIN_SWAP_EXACT_IN_SINGLE = 0x1c;
  uint256 constant BIN_SWAP_EXACT_IN = 0x1d;
  uint256 constant BIN_SWAP_EXACT_OUT_SINGLE = 0x1e;
  uint256 constant BIN_SWAP_EXACT_OUT = 0x1f;
  // donate
  uint256 constant BIN_DONATE = 0x20;
}

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

import {FixedPoint96} from './FixedPoint96.sol';
import {Math} from 'openzeppelin-contracts/contracts/utils/math/Math.sol';

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

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

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

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

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

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

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

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

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

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

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

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

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

/// @title Math library for computing sqrt prices from ticks and vice versa
/// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports
/// prices between 2**-128 and 2**128
library TickMath {
  /// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
  int24 internal constant MIN_TICK = -887_272;
  /// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
  int24 internal constant MAX_TICK = -MIN_TICK;

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

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

      // do bitwise comparison, if i-th bit is turned on,
      // multiply ratio by hardcoded values of sqrt(1.0001^-(2^i)) * 2^128
      // where 0 <= i <= 19
      uint256 ratio = (absTick & 0x1 != 0)
        ? 0xfffcb933bd6fad37aa2d162d1a594001
        : 0x100000000000000000000000000000000;
      if (absTick & 0x2 != 0) {
        ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128;
      }
      if (absTick & 0x4 != 0) {
        ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128;
      }
      if (absTick & 0x8 != 0) {
        ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128;
      }
      if (absTick & 0x10 != 0) {
        ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128;
      }
      if (absTick & 0x20 != 0) {
        ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128;
      }
      if (absTick & 0x40 != 0) {
        ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128;
      }
      if (absTick & 0x80 != 0) {
        ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128;
      }
      if (absTick & 0x100 != 0) {
        ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128;
      }
      if (absTick & 0x200 != 0) {
        ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128;
      }
      if (absTick & 0x400 != 0) {
        ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128;
      }
      if (absTick & 0x800 != 0) {
        ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128;
      }
      if (absTick & 0x1000 != 0) {
        ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
      }
      if (absTick & 0x2000 != 0) {
        ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128;
      }
      if (absTick & 0x4000 != 0) {
        ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128;
      }
      if (absTick & 0x8000 != 0) {
        ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128;
      }
      if (absTick & 0x10000 != 0) {
        ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128;
      }
      if (absTick & 0x20000 != 0) {
        ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128;
      }
      if (absTick & 0x40000 != 0) {
        ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128;
      }
      if (absTick & 0x80000 != 0) {
        ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128;
      }

      // take reciprocal for positive tick values
      if (tick > 0) {
        ratio = type(uint256).max / ratio;
      }

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

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

    uint256 r = ratio;
    uint256 msb = 0;

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

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

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

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

      int256 log_sqrt10001 = log_2 * 255_738_958_999_603_826_347_141; // 128.128 number

      int24 tickLow =
        int24((log_sqrt10001 - 3_402_992_956_809_132_418_596_140_100_660_247_210) >> 128);
      int24 tickHi =
        int24((log_sqrt10001 + 291_339_464_771_989_622_907_027_621_153_398_088_495) >> 128);

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

  function getMaxNumberTicks(int24 _tickDistance) internal pure returns (uint24 numTicks) {
    return uint24(TickMath.MAX_TICK / _tickDistance) * 2;
  }
}

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

import {BaseConditionalHook} from '../../hooks/base/BaseConditionalHook.sol';
import {IKSSmartIntentHook} from '../../interfaces/hooks/IKSSmartIntentHook.sol';

import {TokenHelper} from 'ks-common-sc/src/libraries/token/TokenHelper.sol';

import {IPositionManager} from '../../interfaces/uniswapv4/IPositionManager.sol';
import {IERC721} from 'openzeppelin-contracts/contracts/token/ERC721/IERC721.sol';

import {ActionData} from '../../types/ActionData.sol';
import {ConditionTree, Node} from '../../types/ConditionTree.sol';
import {IntentData} from '../../types/IntentData.sol';

abstract contract BaseTickBasedRemoveLiquidityHook is BaseConditionalHook {
  using TokenHelper for address;

  event LiquidityRemoved(address nftAddress, uint256 nftId, uint256 liquidity);

  error InvalidOwner();
  error InvalidLiquidity();
  error NotEnoughOutputAmount();
  error NotEnoughFeesReceived();
  error ExceedMaxFeesPercent();
  error InvalidERC721Data();

  uint256 public constant Q128 = 1 << 128;
  address public immutable WETH;

  modifier checkTokenLengths(ActionData calldata actionData) override {
    require(actionData.erc20Ids.length == 0, InvalidTokenData());
    require(actionData.erc721Ids.length == 1, InvalidTokenData());
    _;
  }

  constructor(address _weth) {
    WETH = _weth;
  }

  /**
   * @notice Data structure for remove liquidity validation
   * @param nftAddresses The NFT addresses
   * @param nftIds The NFT IDs
   * @param nodes The nodes of conditions (used to build the condition tree)
   * @param maxFees The max fee percents for each output token (1e6 = 100%), [128 bits token0 max fee, 128 bits token1 max fee]
   * @param recipient The recipient
   * @param additionalData The additional data
   */
  struct RemoveLiquidityHookData {
    address[] nftAddresses;
    uint256[] nftIds;
    Node[][] nodes;
    uint256[] maxFees;
    address recipient;
    bytes additionalData;
  }

  /**
   * @notice Data structure for remove liquidity params
   * @param index The index of validation data in RemoveLiquidityHookData struct
   * @param liquidityToRemove The liquidity to remove
   * @param wrapOrUnwrap Whether to wrap or unwrap the tokens after removing liquidity
   * @param recipient The recipient of the output tokens
   * @param poolPrice The price of the pool
   * @param currentTick The current tick of the pool
   * @param positionInfo The position info of the NFT
   */
  struct RemoveLiquidityParams {
    uint256 index;
    uint256 liquidityToRemove;
    bool wrapOrUnwrap;
    address recipient;
    uint160 sqrtPriceX96;
    int24 currentTick;
    PositionInfo positionInfo;
  }

  /**
   * @notice Data structure for output validation params
   * @param router The router address that receives the output tokens after removing liquidity
   * @param balancesBefore The token0, token1 balances of the router before removing liquidity
   * @param maxFees The max fee percents for each output token (1e6 = 100%)
   * @param intentFeesPercent The intent fees percents for each output token (1e6 = 100%)
   * @param tokens The token0, token1 of the pool
   * @param amounts The expected amounts of tokens received from removing the specified liquidity
   * @param unclaimedFees The unclaimed fees of the position
   */
  struct OutputValidationParams {
    address router;
    uint256[2] balancesBefore;
    uint256[2] maxFees;
    uint256[2] intentFeesPercent;
    address[2] tokens;
  }

  /**
   * @notice Data structure for position info
   * @param nftAddress The NFT address
   * @param nftId The NFT ID
   * @param liquidity The liquidity of the position before removing liquidity
   * @param feesGrowthInsideLast The fees growth count of token0 and token1 since last time updated
   * @param feesGenerated The fees generated of token0 and token1
   * @param ticks Position tick range [tickLower, tickUpper]
   * @param amounts The expected amounts of tokens received from removing the specified liquidity
   * @param unclaimedFees The unclaimed fees of the position
   */
  struct PositionInfo {
    address nftAddress;
    uint256 nftId;
    uint256 liquidity;
    int24[2] ticks;
    uint256[2] feesGrowthInsideLast;
    uint256[2] feesGenerated;
    uint256[2] amounts;
    uint256[2] unclaimedFees;
  }

  /// @inheritdoc IKSSmartIntentHook
  function beforeExecution(bytes32, IntentData calldata intentData, ActionData calldata actionData)
    external
    view
    override
    checkTokenLengths(actionData)
    returns (uint256[] memory, bytes memory beforeExecutionData)
  {
    // not collect fees before execution
    beforeExecutionData = _validateBeforeExecution(intentData, actionData);
  }

  /// @inheritdoc IKSSmartIntentHook
  function afterExecution(
    bytes32,
    IntentData calldata intentData,
    bytes calldata beforeExecutionData,
    bytes calldata
  )
    external
    override
    returns (
      address[] memory tokens,
      uint256[] memory fees,
      uint256[] memory amounts,
      address recipient
    )
  {
    (tokens, fees, amounts, recipient) = _validateAfterExecution(intentData, beforeExecutionData);
  }

  function _validateBeforeExecution(IntentData calldata intentData, ActionData calldata actionData)
    internal
    view
    virtual
    returns (bytes memory beforeExecutionData)
  {}

  function _validateAfterExecution(
    IntentData calldata intentData,
    bytes calldata beforeExecutionData
  )
    internal
    virtual
    returns (
      address[] memory tokens,
      uint256[] memory fees,
      uint256[] memory amounts,
      address recipient
    )
  {
    (RemoveLiquidityParams calldata removeLiqParams, OutputValidationParams calldata outputParams) =
      _decodeBeforeExecutionData(beforeExecutionData);

    _validateTokenOwner(
      removeLiqParams.positionInfo.nftAddress,
      removeLiqParams.positionInfo.nftId,
      intentData.coreData.mainAddress
    );
    _validateLiquidity(removeLiqParams);
    (fees, amounts) = _validateOutput(outputParams, removeLiqParams.positionInfo);

    tokens = new address[](2);
    tokens[0] = outputParams.tokens[0];
    tokens[1] = outputParams.tokens[1];
    recipient = removeLiqParams.recipient;

    emit LiquidityRemoved(
      removeLiqParams.positionInfo.nftAddress,
      removeLiqParams.positionInfo.nftId,
      removeLiqParams.liquidityToRemove
    );
  }

  /**
   * @notice Validate the output after removing liquidity
   * @param outputParams The params used to validate output after execution
   * @return fees The fees will be charged
   * @return userReceived The amounts of tokens user will receive after removing liquidity
   */
  function _validateOutput(
    OutputValidationParams calldata outputParams,
    PositionInfo calldata positionInfo
  ) internal view virtual returns (uint256[] memory fees, uint256[] memory userReceived) {
    (uint256 routerReceived0, uint256 routerReceived1) =
      _recordRouterBalances(outputParams.router, outputParams.tokens);

    routerReceived0 -= outputParams.balancesBefore[0];
    routerReceived1 -= outputParams.balancesBefore[1];

    require(
      routerReceived0 >= positionInfo.unclaimedFees[0]
        && routerReceived1 >= positionInfo.unclaimedFees[1],
      NotEnoughFeesReceived()
    );

    uint256 amount0ReceivedForLiquidity = routerReceived0 - positionInfo.unclaimedFees[0];
    uint256 amount1ReceivedForLiquidity = routerReceived1 - positionInfo.unclaimedFees[1];

    // not charge fee on the user's unclaimed fees
    fees = new uint256[](2);
    fees[0] = amount0ReceivedForLiquidity * outputParams.intentFeesPercent[0] / PRECISION;
    fees[1] = amount1ReceivedForLiquidity * outputParams.intentFeesPercent[1] / PRECISION;

    userReceived = new uint256[](2);
    userReceived[0] = routerReceived0 - fees[0];
    userReceived[1] = routerReceived1 - fees[1];

    uint256 minReceived0 = positionInfo.unclaimedFees[0]
      + (positionInfo.amounts[0] * (PRECISION - outputParams.maxFees[0])) / PRECISION;
    uint256 minReceived1 = positionInfo.unclaimedFees[1]
      + (positionInfo.amounts[1] * (PRECISION - outputParams.maxFees[1])) / PRECISION;

    require(
      userReceived[0] >= minReceived0 && userReceived[1] >= minReceived1, NotEnoughOutputAmount()
    );
  }

  function _cacheBaseData(
    RemoveLiquidityHookData calldata validationData,
    bytes calldata hookActionData,
    RemoveLiquidityParams memory removeLiqParams,
    OutputValidationParams memory outputParams
  ) internal view virtual {
    (
      removeLiqParams.index,
      removeLiqParams.positionInfo.feesGenerated[0],
      removeLiqParams.positionInfo.feesGenerated[1],
      removeLiqParams.liquidityToRemove,
      removeLiqParams.wrapOrUnwrap,
      outputParams.intentFeesPercent
    ) = _decodeHookActionData(hookActionData);
    removeLiqParams.recipient = validationData.recipient;
    removeLiqParams.positionInfo.nftAddress = validationData.nftAddresses[removeLiqParams.index];
    removeLiqParams.positionInfo.nftId = validationData.nftIds[removeLiqParams.index];

    outputParams.router = msg.sender;
    outputParams.maxFees = [
      validationData.maxFees[removeLiqParams.index] >> 128,
      uint128(validationData.maxFees[removeLiqParams.index])
    ];

    require(
      outputParams.intentFeesPercent[0] <= outputParams.maxFees[0]
        && outputParams.intentFeesPercent[1] <= outputParams.maxFees[1],
      ExceedMaxFeesPercent()
    );
  }

  /**
   * @notice Validate the conditions of the liquidity removal operation
   * @param nodes The nodes of conditions (used to build the condition tree)
   * @param fee0Generated The fee0 generated - an offchain component that could contain claimed fees, unclaimed fees, and yield-based fees
   * @param fee1Generated The fee1 generated - an offchain component that could contain claimed fees, unclaimed fees, and yield-based fees
   * @param poolPrice The price of the pool
   */
  function _validateConditions(
    Node[] calldata nodes,
    uint256 fee0Generated,
    uint256 fee1Generated,
    uint256 poolPrice
  ) internal view virtual {
    this.validateConditionTree(
      _buildConditionTree(nodes, fee0Generated, fee1Generated, poolPrice), 0
    );
  }

  function _validateTokenOwner(address nftAddress, uint256 nftId, address owner)
    internal
    view
    virtual
  {
    require(IERC721(nftAddress).ownerOf(nftId) == owner, InvalidOwner());
  }

  function _validateLiquidity(RemoveLiquidityParams calldata removeLiquidityParams)
    internal
    view
    virtual
  {
    require(
      _getPositionLiquidity(
        removeLiquidityParams.positionInfo.nftAddress, removeLiquidityParams.positionInfo.nftId
      ) == removeLiquidityParams.positionInfo.liquidity - removeLiquidityParams.liquidityToRemove,
      InvalidLiquidity()
    );
  }

  function _validateERC721Data(
    address nftAddress,
    uint256 nftId,
    address tokenAddress,
    uint256 tokenId
  ) internal view virtual {
    require(nftAddress == tokenAddress && nftId == tokenId, InvalidERC721Data());
  }

  function _getPositionLiquidity(address nftAddress, uint256 nftId)
    internal
    view
    virtual
    returns (uint256 liquidity)
  {
    liquidity = IPositionManager(nftAddress).getPositionLiquidity(nftId);
  }

  function _recordRouterBalances(address router, address[2] memory tokens)
    internal
    view
    returns (uint256 balance0, uint256 balance1)
  {
    balance0 = tokens[0].balanceOf(router);
    balance1 = tokens[1].balanceOf(router);
  }

  function _adjustTokens(address[2] memory tokens)
    internal
    view
    returns (address[2] memory adjustedTokens)
  {
    adjustedTokens[0] = _adjustToken(tokens[0]);
    adjustedTokens[1] = _adjustToken(tokens[1]);
  }

  function _buildConditionTree(
    Node[] calldata nodes,
    uint256 fee0Generated,
    uint256 fee1Generated,
    uint256 poolPrice
  ) internal pure virtual returns (ConditionTree memory conditionTree) {
    conditionTree.nodes = nodes;
    conditionTree.additionalData = new bytes[](nodes.length);
    for (uint256 i; i < nodes.length; ++i) {
      if (!nodes[i].isLeaf() || nodes[i].condition.isType(TIME_BASED)) {
        continue;
      }
      if (nodes[i].condition.isType(YIELD_BASED)) {
        conditionTree.additionalData[i] = abi.encode(fee0Generated, fee1Generated, poolPrice);
      } else if (nodes[i].condition.isType(PRICE_BASED)) {
        conditionTree.additionalData[i] = abi.encode(poolPrice);
      }
    }
  }

  // @dev: equivalent to abi.decode(data, (RemoveLiquidityHookData))
  function _decodeHookData(bytes calldata data)
    internal
    pure
    returns (RemoveLiquidityHookData calldata validationData)
  {
    assembly ('memory-safe') {
      validationData := add(data.offset, calldataload(data.offset))
    }
  }

  // @dev: equivalent to abi.decode(data, (uint256 index, uint256 fee0Generated, uint256 fee1Generated, uint256 liquidity, bool wrapOrUnwrap, uint256 packedFees))
  function _decodeHookActionData(bytes calldata data)
    internal
    pure
    virtual
    returns (
      uint256 index,
      uint256 fee0Generated,
      uint256 fee1Generated,
      uint256 liquidity,
      bool wrapOrUnwrap,
      uint256[2] memory intentFeesPercent
    )
  {
    uint256 packedFees;
    assembly ('memory-safe') {
      index := calldataload(data.offset)
      fee0Generated := calldataload(add(data.offset, 0x20))
      fee1Generated := calldataload(add(data.offset, 0x40))
      liquidity := calldataload(add(data.offset, 0x60))
      wrapOrUnwrap := calldataload(add(data.offset, 0x80))
      packedFees := calldataload(add(data.offset, 0xa0))
    }

    intentFeesPercent = [packedFees >> 128, uint128(packedFees)];
  }

  // @dev: equivalent to abi.decode(data, (RemoveLiquidityParams, OutputValidationParams))
  function _decodeBeforeExecutionData(bytes calldata data)
    internal
    pure
    virtual
    returns (
      RemoveLiquidityParams calldata removeLiqParams,
      OutputValidationParams calldata outputParams
    )
  {
    assembly ('memory-safe') {
      removeLiqParams := data.offset
      outputParams := add(data.offset, 0x260) // (outputParams starts at slot 19 (608th byte))
    }
  }

  function _adjustToken(address token) internal view returns (address adjustedToken) {
    if (token != WETH && token != TokenHelper.NATIVE_ADDRESS) {
      return token;
    }

    return token == WETH ? TokenHelper.NATIVE_ADDRESS : WETH;
  }

  function _toNative(address token) internal pure returns (address nativeToken) {
    return token == address(0) ? TokenHelper.NATIVE_ADDRESS : token;
  }
}

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

import {FeeInfo} from './FeeInfo.sol';
import {IntentCoreData} from './IntentCoreData.sol';
import {TokenData} from './TokenData.sol';

/**
 * @notice Data structure for action
 * @param erc20Ids The IDs of the ERC20 tokens in the intent data
 * @param erc20Amounts The amounts of the ERC20 tokens
 * @param erc721Ids The IDs of the ERC721 tokens in the intent data
 * @param feeInfo The fee info for the action
 * @param approvalFlags The approval flags for the tokens
 * @param actionSelectorId The ID of the action selector
 * @param actionCalldata The calldata for the action
 * @param hookActionData The action data for the hook
 * @param extraData The extra data for the action
 * @param deadline The deadline for the action
 * @param nonce The nonce for the action
 */
struct ActionData {
  uint256[] erc20Ids;
  uint256[] erc20Amounts;
  uint256[] erc721Ids;
  FeeInfo feeInfo;
  uint256 approvalFlags;
  uint256 actionSelectorId;
  bytes actionCalldata;
  bytes hookActionData;
  bytes extraData;
  uint256 deadline;
  uint256 nonce;
}

using ActionDataLibrary for ActionData global;

library ActionDataLibrary {
  bytes32 constant ACTION_DATA_TYPE_HASH = keccak256(
    abi.encodePacked(
      'ActionData(uint256[] erc20Ids,uint256[] erc20Amounts,uint256[] erc721Ids,FeeInfo feeInfo,uint256 approvalFlags,uint256 actionSelectorId,bytes actionCalldata,bytes hookActionData,bytes extraData,uint256 deadline,uint256 nonce)FeeInfo(address protocolRecipient,uint256[][] partnerFeeConfigs)'
    )
  );

  function hash(ActionData calldata self) internal pure returns (bytes32) {
    return keccak256(
      abi.encode(
        ACTION_DATA_TYPE_HASH,
        keccak256(abi.encodePacked(self.erc20Ids)),
        keccak256(abi.encodePacked(self.erc20Amounts)),
        keccak256(abi.encodePacked(self.erc721Ids)),
        self.feeInfo.hash(),
        self.approvalFlags,
        self.actionSelectorId,
        keccak256(self.actionCalldata),
        keccak256(self.hookActionData),
        keccak256(self.extraData),
        self.deadline,
        self.nonce
      )
    );
  }
}

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

import {IntentCoreData} from './IntentCoreData.sol';
import {TokenData} from './TokenData.sol';

/**
 * @notice Data structure for intent data
 * @param coreData The core data for the intent
 * @param tokenData The token data for the intent
 * @param extraData The extra data for the intent
 */
struct IntentData {
  IntentCoreData coreData;
  TokenData tokenData;
  bytes extraData;
}

using IntentDataLibrary for IntentData global;

library IntentDataLibrary {
  bytes32 constant INTENT_DATA_TYPE_HASH = keccak256(
    abi.encodePacked(
      'IntentData(IntentCoreData coreData,TokenData tokenData,bytes extraData)ERC20Data(address token,uint256 amount,bytes permitData)ERC721Data(address token,uint256 tokenId,bytes permitData)IntentCoreData(address mainAddress,address signatureVerifier,bytes delegatedKey,address[] actionContracts,bytes4[] actionSelectors,address hook,bytes hookIntentData)TokenData(ERC20Data[] erc20Data,ERC721Data[] erc721Data)'
    )
  );

  function hash(IntentData calldata self) internal pure returns (bytes32) {
    return keccak256(
      abi.encode(
        INTENT_DATA_TYPE_HASH,
        self.coreData.hash(),
        self.tokenData.hash(),
        keccak256(self.extraData)
      )
    );
  }
}

File 11 of 43 : Math.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/math/Math.sol)

pragma solidity ^0.8.20;

import {Panic} from "../Panic.sol";
import {SafeCast} from "./SafeCast.sol";

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    enum Rounding {
        Floor, // Toward negative infinity
        Ceil, // Toward positive infinity
        Trunc, // Toward zero
        Expand // Away from zero
    }

    /**
     * @dev Return the 512-bit addition of two uint256.
     *
     * The result is stored in two 256 variables such that sum = high * 2²⁵⁶ + low.
     */
    function add512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
        assembly ("memory-safe") {
            low := add(a, b)
            high := lt(low, a)
        }
    }

    /**
     * @dev Return the 512-bit multiplication of two uint256.
     *
     * The result is stored in two 256 variables such that product = high * 2²⁵⁶ + low.
     */
    function mul512(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
        // 512-bit multiply [high low] = x * y. Compute the product mod 2²⁵⁶ and mod 2²⁵⁶ - 1, then use
        // the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
        // variables such that product = high * 2²⁵⁶ + low.
        assembly ("memory-safe") {
            let mm := mulmod(a, b, not(0))
            low := mul(a, b)
            high := sub(sub(mm, low), lt(mm, low))
        }
    }

    /**
     * @dev Returns the addition of two unsigned integers, with a success flag (no overflow).
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            uint256 c = a + b;
            success = c >= a;
            result = c * SafeCast.toUint(success);
        }
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, with a success flag (no overflow).
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            uint256 c = a - b;
            success = c <= a;
            result = c * SafeCast.toUint(success);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with a success flag (no overflow).
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            uint256 c = a * b;
            assembly ("memory-safe") {
                // Only true when the multiplication doesn't overflow
                // (c / a == b) || (a == 0)
                success := or(eq(div(c, a), b), iszero(a))
            }
            // equivalent to: success ? c : 0
            result = c * SafeCast.toUint(success);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a success flag (no division by zero).
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            success = b > 0;
            assembly ("memory-safe") {
                // The `DIV` opcode returns zero when the denominator is 0.
                result := div(a, b)
            }
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero).
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
        unchecked {
            success = b > 0;
            assembly ("memory-safe") {
                // The `MOD` opcode returns zero when the denominator is 0.
                result := mod(a, b)
            }
        }
    }

    /**
     * @dev Unsigned saturating addition, bounds to `2²⁵⁶ - 1` instead of overflowing.
     */
    function saturatingAdd(uint256 a, uint256 b) internal pure returns (uint256) {
        (bool success, uint256 result) = tryAdd(a, b);
        return ternary(success, result, type(uint256).max);
    }

    /**
     * @dev Unsigned saturating subtraction, bounds to zero instead of overflowing.
     */
    function saturatingSub(uint256 a, uint256 b) internal pure returns (uint256) {
        (, uint256 result) = trySub(a, b);
        return result;
    }

    /**
     * @dev Unsigned saturating multiplication, bounds to `2²⁵⁶ - 1` instead of overflowing.
     */
    function saturatingMul(uint256 a, uint256 b) internal pure returns (uint256) {
        (bool success, uint256 result) = tryMul(a, b);
        return ternary(success, result, type(uint256).max);
    }

    /**
     * @dev Branchless ternary evaluation for `condition ? a : b`. Gas costs are constant.
     *
     * IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
     * However, the compiler may optimize Solidity ternary operations (i.e. `condition ? a : b`) to only compute
     * one branch when needed, making this function more expensive.
     */
    function ternary(bool condition, uint256 a, uint256 b) internal pure returns (uint256) {
        unchecked {
            // branchless ternary works because:
            // b ^ (a ^ b) == a
            // b ^ 0 == b
            return b ^ ((a ^ b) * SafeCast.toUint(condition));
        }
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return ternary(a > b, a, b);
    }

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

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow.
        return (a & b) + (a ^ b) / 2;
    }

    /**
     * @dev Returns the ceiling of the division of two numbers.
     *
     * This differs from standard division with `/` in that it rounds towards infinity instead
     * of rounding towards zero.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        if (b == 0) {
            // Guarantee the same behavior as in a regular Solidity division.
            Panic.panic(Panic.DIVISION_BY_ZERO);
        }

        // The following calculation ensures accurate ceiling division without overflow.
        // Since a is non-zero, (a - 1) / b will not overflow.
        // The largest possible result occurs when (a - 1) / b is type(uint256).max,
        // but the largest value we can obtain is type(uint256).max - 1, which happens
        // when a = type(uint256).max and b = 1.
        unchecked {
            return SafeCast.toUint(a > 0) * ((a - 1) / b + 1);
        }
    }

    /**
     * @dev Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or
     * denominator == 0.
     *
     * Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
     * Uniswap Labs also under MIT license.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
        unchecked {
            (uint256 high, uint256 low) = mul512(x, y);

            // Handle non-overflow cases, 256 by 256 division.
            if (high == 0) {
                // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                // The surrounding unchecked block does not change this fact.
                // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                return low / denominator;
            }

            // Make sure the result is less than 2²⁵⁶. Also prevents denominator == 0.
            if (denominator <= high) {
                Panic.panic(ternary(denominator == 0, Panic.DIVISION_BY_ZERO, Panic.UNDER_OVERFLOW));
            }

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

            // Make division exact by subtracting the remainder from [high low].
            uint256 remainder;
            assembly ("memory-safe") {
                // Compute remainder using mulmod.
                remainder := mulmod(x, y, denominator)

                // Subtract 256 bit number from 512 bit number.
                high := sub(high, gt(remainder, low))
                low := sub(low, remainder)
            }

            // Factor powers of two out of denominator and compute largest power of two divisor of denominator.
            // Always >= 1. See https://cs.stackexchange.com/q/138556/92363.

            uint256 twos = denominator & (0 - denominator);
            assembly ("memory-safe") {
                // Divide denominator by twos.
                denominator := div(denominator, twos)

                // Divide [high low] by twos.
                low := div(low, twos)

                // Flip twos such that it is 2²⁵⁶ / twos. If twos is zero, then it becomes one.
                twos := add(div(sub(0, twos), twos), 1)
            }

            // Shift in bits from high into low.
            low |= high * twos;

            // Invert denominator mod 2²⁵⁶. Now that denominator is an odd number, it has an inverse modulo 2²⁵⁶ such
            // that denominator * inv ≡ 1 mod 2²⁵⁶. Compute the inverse by starting with a seed that is correct for
            // four bits. That is, denominator * inv ≡ 1 mod 2⁴.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the 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.
            inverse *= 2 - denominator * inverse; // inverse mod 2⁸
            inverse *= 2 - denominator * inverse; // inverse mod 2¹⁶
            inverse *= 2 - denominator * inverse; // inverse mod 2³²
            inverse *= 2 - denominator * inverse; // inverse mod 2⁶⁴
            inverse *= 2 - denominator * inverse; // inverse mod 2¹²⁸
            inverse *= 2 - denominator * inverse; // inverse mod 2²⁵⁶

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

    /**
     * @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
        return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
    }

    /**
     * @dev Calculates floor(x * y >> n) with full precision. Throws if result overflows a uint256.
     */
    function mulShr(uint256 x, uint256 y, uint8 n) internal pure returns (uint256 result) {
        unchecked {
            (uint256 high, uint256 low) = mul512(x, y);
            if (high >= 1 << n) {
                Panic.panic(Panic.UNDER_OVERFLOW);
            }
            return (high << (256 - n)) | (low >> n);
        }
    }

    /**
     * @dev Calculates x * y >> n with full precision, following the selected rounding direction.
     */
    function mulShr(uint256 x, uint256 y, uint8 n, Rounding rounding) internal pure returns (uint256) {
        return mulShr(x, y, n) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, 1 << n) > 0);
    }

    /**
     * @dev Calculate the modular multiplicative inverse of a number in Z/nZ.
     *
     * If n is a prime, then Z/nZ is a field. In that case all elements are inversible, except 0.
     * If n is not a prime, then Z/nZ is not a field, and some elements might not be inversible.
     *
     * If the input value is not inversible, 0 is returned.
     *
     * NOTE: If you know for sure that n is (big) a prime, it may be cheaper to use Fermat's little theorem and get the
     * inverse using `Math.modExp(a, n - 2, n)`. See {invModPrime}.
     */
    function invMod(uint256 a, uint256 n) internal pure returns (uint256) {
        unchecked {
            if (n == 0) return 0;

            // The inverse modulo is calculated using the Extended Euclidean Algorithm (iterative version)
            // Used to compute integers x and y such that: ax + ny = gcd(a, n).
            // When the gcd is 1, then the inverse of a modulo n exists and it's x.
            // ax + ny = 1
            // ax = 1 + (-y)n
            // ax ≡ 1 (mod n) # x is the inverse of a modulo n

            // If the remainder is 0 the gcd is n right away.
            uint256 remainder = a % n;
            uint256 gcd = n;

            // Therefore the initial coefficients are:
            // ax + ny = gcd(a, n) = n
            // 0a + 1n = n
            int256 x = 0;
            int256 y = 1;

            while (remainder != 0) {
                uint256 quotient = gcd / remainder;

                (gcd, remainder) = (
                    // The old remainder is the next gcd to try.
                    remainder,
                    // Compute the next remainder.
                    // Can't overflow given that (a % gcd) * (gcd // (a % gcd)) <= gcd
                    // where gcd is at most n (capped to type(uint256).max)
                    gcd - remainder * quotient
                );

                (x, y) = (
                    // Increment the coefficient of a.
                    y,
                    // Decrement the coefficient of n.
                    // Can overflow, but the result is casted to uint256 so that the
                    // next value of y is "wrapped around" to a value between 0 and n - 1.
                    x - y * int256(quotient)
                );
            }

            if (gcd != 1) return 0; // No inverse exists.
            return ternary(x < 0, n - uint256(-x), uint256(x)); // Wrap the result if it's negative.
        }
    }

    /**
     * @dev Variant of {invMod}. More efficient, but only works if `p` is known to be a prime greater than `2`.
     *
     * From https://en.wikipedia.org/wiki/Fermat%27s_little_theorem[Fermat's little theorem], we know that if p is
     * prime, then `a**(p-1) ≡ 1 mod p`. As a consequence, we have `a * a**(p-2) ≡ 1 mod p`, which means that
     * `a**(p-2)` is the modular multiplicative inverse of a in Fp.
     *
     * NOTE: this function does NOT check that `p` is a prime greater than `2`.
     */
    function invModPrime(uint256 a, uint256 p) internal view returns (uint256) {
        unchecked {
            return Math.modExp(a, p - 2, p);
        }
    }

    /**
     * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m)
     *
     * Requirements:
     * - modulus can't be zero
     * - underlying staticcall to precompile must succeed
     *
     * IMPORTANT: The result is only valid if the underlying call succeeds. When using this function, make
     * sure the chain you're using it on supports the precompiled contract for modular exponentiation
     * at address 0x05 as specified in https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise,
     * the underlying function will succeed given the lack of a revert, but the result may be incorrectly
     * interpreted as 0.
     */
    function modExp(uint256 b, uint256 e, uint256 m) internal view returns (uint256) {
        (bool success, uint256 result) = tryModExp(b, e, m);
        if (!success) {
            Panic.panic(Panic.DIVISION_BY_ZERO);
        }
        return result;
    }

    /**
     * @dev Returns the modular exponentiation of the specified base, exponent and modulus (b ** e % m).
     * It includes a success flag indicating if the operation succeeded. Operation will be marked as failed if trying
     * to operate modulo 0 or if the underlying precompile reverted.
     *
     * IMPORTANT: The result is only valid if the success flag is true. When using this function, make sure the chain
     * you're using it on supports the precompiled contract for modular exponentiation at address 0x05 as specified in
     * https://eips.ethereum.org/EIPS/eip-198[EIP-198]. Otherwise, the underlying function will succeed given the lack
     * of a revert, but the result may be incorrectly interpreted as 0.
     */
    function tryModExp(uint256 b, uint256 e, uint256 m) internal view returns (bool success, uint256 result) {
        if (m == 0) return (false, 0);
        assembly ("memory-safe") {
            let ptr := mload(0x40)
            // | Offset    | Content    | Content (Hex)                                                      |
            // |-----------|------------|--------------------------------------------------------------------|
            // | 0x00:0x1f | size of b  | 0x0000000000000000000000000000000000000000000000000000000000000020 |
            // | 0x20:0x3f | size of e  | 0x0000000000000000000000000000000000000000000000000000000000000020 |
            // | 0x40:0x5f | size of m  | 0x0000000000000000000000000000000000000000000000000000000000000020 |
            // | 0x60:0x7f | value of b | 0x<.............................................................b> |
            // | 0x80:0x9f | value of e | 0x<.............................................................e> |
            // | 0xa0:0xbf | value of m | 0x<.............................................................m> |
            mstore(ptr, 0x20)
            mstore(add(ptr, 0x20), 0x20)
            mstore(add(ptr, 0x40), 0x20)
            mstore(add(ptr, 0x60), b)
            mstore(add(ptr, 0x80), e)
            mstore(add(ptr, 0xa0), m)

            // Given the result < m, it's guaranteed to fit in 32 bytes,
            // so we can use the memory scratch space located at offset 0.
            success := staticcall(gas(), 0x05, ptr, 0xc0, 0x00, 0x20)
            result := mload(0x00)
        }
    }

    /**
     * @dev Variant of {modExp} that supports inputs of arbitrary length.
     */
    function modExp(bytes memory b, bytes memory e, bytes memory m) internal view returns (bytes memory) {
        (bool success, bytes memory result) = tryModExp(b, e, m);
        if (!success) {
            Panic.panic(Panic.DIVISION_BY_ZERO);
        }
        return result;
    }

    /**
     * @dev Variant of {tryModExp} that supports inputs of arbitrary length.
     */
    function tryModExp(
        bytes memory b,
        bytes memory e,
        bytes memory m
    ) internal view returns (bool success, bytes memory result) {
        if (_zeroBytes(m)) return (false, new bytes(0));

        uint256 mLen = m.length;

        // Encode call args in result and move the free memory pointer
        result = abi.encodePacked(b.length, e.length, mLen, b, e, m);

        assembly ("memory-safe") {
            let dataPtr := add(result, 0x20)
            // Write result on top of args to avoid allocating extra memory.
            success := staticcall(gas(), 0x05, dataPtr, mload(result), dataPtr, mLen)
            // Overwrite the length.
            // result.length > returndatasize() is guaranteed because returndatasize() == m.length
            mstore(result, mLen)
            // Set the memory pointer after the returned data.
            mstore(0x40, add(dataPtr, mLen))
        }
    }

    /**
     * @dev Returns whether the provided byte array is zero.
     */
    function _zeroBytes(bytes memory byteArray) private pure returns (bool) {
        for (uint256 i = 0; i < byteArray.length; ++i) {
            if (byteArray[i] != 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
     * towards zero.
     *
     * This method is based on Newton's method for computing square roots; the algorithm is restricted to only
     * using integer operations.
     */
    function sqrt(uint256 a) internal pure returns (uint256) {
        unchecked {
            // Take care of easy edge cases when a == 0 or a == 1
            if (a <= 1) {
                return a;
            }

            // In this function, we use Newton's method to get a root of `f(x) := x² - a`. It involves building a
            // sequence x_n that converges toward sqrt(a). For each iteration x_n, we also define the error between
            // the current value as `ε_n = | x_n - sqrt(a) |`.
            //
            // For our first estimation, we consider `e` the smallest power of 2 which is bigger than the square root
            // of the target. (i.e. `2**(e-1) ≤ sqrt(a) < 2**e`). We know that `e ≤ 128` because `(2¹²⁸)² = 2²⁵⁶` is
            // bigger than any uint256.
            //
            // By noticing that
            // `2**(e-1) ≤ sqrt(a) < 2**e → (2**(e-1))² ≤ a < (2**e)² → 2**(2*e-2) ≤ a < 2**(2*e)`
            // we can deduce that `e - 1` is `log2(a) / 2`. We can thus compute `x_n = 2**(e-1)` using a method similar
            // to the msb function.
            uint256 aa = a;
            uint256 xn = 1;

            if (aa >= (1 << 128)) {
                aa >>= 128;
                xn <<= 64;
            }
            if (aa >= (1 << 64)) {
                aa >>= 64;
                xn <<= 32;
            }
            if (aa >= (1 << 32)) {
                aa >>= 32;
                xn <<= 16;
            }
            if (aa >= (1 << 16)) {
                aa >>= 16;
                xn <<= 8;
            }
            if (aa >= (1 << 8)) {
                aa >>= 8;
                xn <<= 4;
            }
            if (aa >= (1 << 4)) {
                aa >>= 4;
                xn <<= 2;
            }
            if (aa >= (1 << 2)) {
                xn <<= 1;
            }

            // We now have x_n such that `x_n = 2**(e-1) ≤ sqrt(a) < 2**e = 2 * x_n`. This implies ε_n ≤ 2**(e-1).
            //
            // We can refine our estimation by noticing that the middle of that interval minimizes the error.
            // If we move x_n to equal 2**(e-1) + 2**(e-2), then we reduce the error to ε_n ≤ 2**(e-2).
            // This is going to be our x_0 (and ε_0)
            xn = (3 * xn) >> 1; // ε_0 := | x_0 - sqrt(a) | ≤ 2**(e-2)

            // From here, Newton's method give us:
            // x_{n+1} = (x_n + a / x_n) / 2
            //
            // One should note that:
            // x_{n+1}² - a = ((x_n + a / x_n) / 2)² - a
            //              = ((x_n² + a) / (2 * x_n))² - a
            //              = (x_n⁴ + 2 * a * x_n² + a²) / (4 * x_n²) - a
            //              = (x_n⁴ + 2 * a * x_n² + a² - 4 * a * x_n²) / (4 * x_n²)
            //              = (x_n⁴ - 2 * a * x_n² + a²) / (4 * x_n²)
            //              = (x_n² - a)² / (2 * x_n)²
            //              = ((x_n² - a) / (2 * x_n))²
            //              ≥ 0
            // Which proves that for all n ≥ 1, sqrt(a) ≤ x_n
            //
            // This gives us the proof of quadratic convergence of the sequence:
            // ε_{n+1} = | x_{n+1} - sqrt(a) |
            //         = | (x_n + a / x_n) / 2 - sqrt(a) |
            //         = | (x_n² + a - 2*x_n*sqrt(a)) / (2 * x_n) |
            //         = | (x_n - sqrt(a))² / (2 * x_n) |
            //         = | ε_n² / (2 * x_n) |
            //         = ε_n² / | (2 * x_n) |
            //
            // For the first iteration, we have a special case where x_0 is known:
            // ε_1 = ε_0² / | (2 * x_0) |
            //     ≤ (2**(e-2))² / (2 * (2**(e-1) + 2**(e-2)))
            //     ≤ 2**(2*e-4) / (3 * 2**(e-1))
            //     ≤ 2**(e-3) / 3
            //     ≤ 2**(e-3-log2(3))
            //     ≤ 2**(e-4.5)
            //
            // For the following iterations, we use the fact that, 2**(e-1) ≤ sqrt(a) ≤ x_n:
            // ε_{n+1} = ε_n² / | (2 * x_n) |
            //         ≤ (2**(e-k))² / (2 * 2**(e-1))
            //         ≤ 2**(2*e-2*k) / 2**e
            //         ≤ 2**(e-2*k)
            xn = (xn + a / xn) >> 1; // ε_1 := | x_1 - sqrt(a) | ≤ 2**(e-4.5)  -- special case, see above
            xn = (xn + a / xn) >> 1; // ε_2 := | x_2 - sqrt(a) | ≤ 2**(e-9)    -- general case with k = 4.5
            xn = (xn + a / xn) >> 1; // ε_3 := | x_3 - sqrt(a) | ≤ 2**(e-18)   -- general case with k = 9
            xn = (xn + a / xn) >> 1; // ε_4 := | x_4 - sqrt(a) | ≤ 2**(e-36)   -- general case with k = 18
            xn = (xn + a / xn) >> 1; // ε_5 := | x_5 - sqrt(a) | ≤ 2**(e-72)   -- general case with k = 36
            xn = (xn + a / xn) >> 1; // ε_6 := | x_6 - sqrt(a) | ≤ 2**(e-144)  -- general case with k = 72

            // Because e ≤ 128 (as discussed during the first estimation phase), we know have reached a precision
            // ε_6 ≤ 2**(e-144) < 1. Given we're operating on integers, then we can ensure that xn is now either
            // sqrt(a) or sqrt(a) + 1.
            return xn - SafeCast.toUint(xn > a / xn);
        }
    }

    /**
     * @dev Calculates sqrt(a), following the selected rounding direction.
     */
    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = sqrt(a);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
        }
    }

    /**
     * @dev Return the log in base 2 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log2(uint256 x) internal pure returns (uint256 r) {
        // If value has upper 128 bits set, log2 result is at least 128
        r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
        // If upper 64 bits of 128-bit half set, add 64 to result
        r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
        // If upper 32 bits of 64-bit half set, add 32 to result
        r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
        // If upper 16 bits of 32-bit half set, add 16 to result
        r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
        // If upper 8 bits of 16-bit half set, add 8 to result
        r |= SafeCast.toUint((x >> r) > 0xff) << 3;
        // If upper 4 bits of 8-bit half set, add 4 to result
        r |= SafeCast.toUint((x >> r) > 0xf) << 2;

        // Shifts value right by the current result and use it as an index into this lookup table:
        //
        // | x (4 bits) |  index  | table[index] = MSB position |
        // |------------|---------|-----------------------------|
        // |    0000    |    0    |        table[0] = 0         |
        // |    0001    |    1    |        table[1] = 0         |
        // |    0010    |    2    |        table[2] = 1         |
        // |    0011    |    3    |        table[3] = 1         |
        // |    0100    |    4    |        table[4] = 2         |
        // |    0101    |    5    |        table[5] = 2         |
        // |    0110    |    6    |        table[6] = 2         |
        // |    0111    |    7    |        table[7] = 2         |
        // |    1000    |    8    |        table[8] = 3         |
        // |    1001    |    9    |        table[9] = 3         |
        // |    1010    |   10    |        table[10] = 3        |
        // |    1011    |   11    |        table[11] = 3        |
        // |    1100    |   12    |        table[12] = 3        |
        // |    1101    |   13    |        table[13] = 3        |
        // |    1110    |   14    |        table[14] = 3        |
        // |    1111    |   15    |        table[15] = 3        |
        //
        // The lookup table is represented as a 32-byte value with the MSB positions for 0-15 in the last 16 bytes.
        assembly ("memory-safe") {
            r := or(r, byte(shr(r, x), 0x0000010102020202030303030303030300000000000000000000000000000000))
        }
    }

    /**
     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log2(value);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
        }
    }

    /**
     * @dev Return the log in base 10 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log10(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >= 10 ** 64) {
                value /= 10 ** 64;
                result += 64;
            }
            if (value >= 10 ** 32) {
                value /= 10 ** 32;
                result += 32;
            }
            if (value >= 10 ** 16) {
                value /= 10 ** 16;
                result += 16;
            }
            if (value >= 10 ** 8) {
                value /= 10 ** 8;
                result += 8;
            }
            if (value >= 10 ** 4) {
                value /= 10 ** 4;
                result += 4;
            }
            if (value >= 10 ** 2) {
                value /= 10 ** 2;
                result += 2;
            }
            if (value >= 10 ** 1) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log10(value);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
        }
    }

    /**
     * @dev Return the log in base 256 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     *
     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
     */
    function log256(uint256 x) internal pure returns (uint256 r) {
        // If value has upper 128 bits set, log2 result is at least 128
        r = SafeCast.toUint(x > 0xffffffffffffffffffffffffffffffff) << 7;
        // If upper 64 bits of 128-bit half set, add 64 to result
        r |= SafeCast.toUint((x >> r) > 0xffffffffffffffff) << 6;
        // If upper 32 bits of 64-bit half set, add 32 to result
        r |= SafeCast.toUint((x >> r) > 0xffffffff) << 5;
        // If upper 16 bits of 32-bit half set, add 16 to result
        r |= SafeCast.toUint((x >> r) > 0xffff) << 4;
        // Add 1 if upper 8 bits of 16-bit half set, and divide accumulated result by 8
        return (r >> 3) | SafeCast.toUint((x >> r) > 0xff);
    }

    /**
     * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log256(value);
            return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
        }
    }

    /**
     * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
     */
    function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
        return uint8(rounding) % 2 == 1;
    }

    /**
     * @dev Counts the number of leading zero bits in a uint256.
     */
    function clz(uint256 x) internal pure returns (uint256) {
        return ternary(x == 0, 256, 255 - log2(x));
    }
}

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

/// @title Library for reverting with custom errors efficiently
/// @notice Contains functions for reverting with custom errors with different argument types efficiently
/// @dev To use this library, declare `using CustomRevert for bytes4;` and replace `revert CustomError()` with
/// `CustomError.selector.revertWith()`
/// @dev The functions may tamper with the free memory pointer but it is fine since the call context is exited immediately
library CustomRevert {
  /// @dev ERC-7751 error for wrapping bubbled up reverts
  error WrappedError(address target, bytes4 selector, bytes reason, bytes details);

  /// @dev Reverts with the selector of a custom error in the scratch space
  function revertWith(bytes4 selector) internal pure {
    assembly ('memory-safe') {
      mstore(0, selector)
      revert(0, 0x04)
    }
  }

  /// @dev Reverts with a custom error with an address argument in the scratch space
  function revertWith(bytes4 selector, address addr) internal pure {
    assembly ('memory-safe') {
      mstore(0, selector)
      mstore(0x04, and(addr, 0xffffffffffffffffffffffffffffffffffffffff))
      revert(0, 0x24)
    }
  }

  /// @dev Reverts with a custom error with an int24 argument in the scratch space
  function revertWith(bytes4 selector, int24 value) internal pure {
    assembly ('memory-safe') {
      mstore(0, selector)
      mstore(0x04, signextend(2, value))
      revert(0, 0x24)
    }
  }

  /// @dev Reverts with a custom error with a uint160 argument in the scratch space
  function revertWith(bytes4 selector, uint160 value) internal pure {
    assembly ('memory-safe') {
      mstore(0, selector)
      mstore(0x04, and(value, 0xffffffffffffffffffffffffffffffffffffffff))
      revert(0, 0x24)
    }
  }

  /// @dev Reverts with a custom error with two int24 arguments
  function revertWith(bytes4 selector, int24 value1, int24 value2) internal pure {
    assembly ('memory-safe') {
      let fmp := mload(0x40)
      mstore(fmp, selector)
      mstore(add(fmp, 0x04), signextend(2, value1))
      mstore(add(fmp, 0x24), signextend(2, value2))
      revert(fmp, 0x44)
    }
  }

  /// @dev Reverts with a custom error with two uint160 arguments
  function revertWith(bytes4 selector, uint160 value1, uint160 value2) internal pure {
    assembly ('memory-safe') {
      let fmp := mload(0x40)
      mstore(fmp, selector)
      mstore(add(fmp, 0x04), and(value1, 0xffffffffffffffffffffffffffffffffffffffff))
      mstore(add(fmp, 0x24), and(value2, 0xffffffffffffffffffffffffffffffffffffffff))
      revert(fmp, 0x44)
    }
  }

  /// @dev Reverts with a custom error with two address arguments
  function revertWith(bytes4 selector, address value1, address value2) internal pure {
    assembly ('memory-safe') {
      let fmp := mload(0x40)
      mstore(fmp, selector)
      mstore(add(fmp, 0x04), and(value1, 0xffffffffffffffffffffffffffffffffffffffff))
      mstore(add(fmp, 0x24), and(value2, 0xffffffffffffffffffffffffffffffffffffffff))
      revert(fmp, 0x44)
    }
  }

  /// @notice bubble up the revert message returned by a call and revert with a wrapped ERC-7751 error
  /// @dev this method can be vulnerable to revert data bombs
  function bubbleUpAndRevertWith(
    address revertingContract,
    bytes4 revertingFunctionSelector,
    bytes4 additionalContext
  ) internal pure {
    bytes4 wrappedErrorSelector = WrappedError.selector;
    assembly ('memory-safe') {
      // Ensure the size of the revert data is a multiple of 32 bytes
      let encodedDataSize := mul(div(add(returndatasize(), 31), 32), 32)

      let fmp := mload(0x40)

      // Encode wrapped error selector, address, function selector, offset, additional context, size, revert reason
      mstore(fmp, wrappedErrorSelector)
      mstore(add(fmp, 0x04), and(revertingContract, 0xffffffffffffffffffffffffffffffffffffffff))
      mstore(
        add(fmp, 0x24),
        and(
          revertingFunctionSelector,
          0xffffffff00000000000000000000000000000000000000000000000000000000
        )
      )
      // offset revert reason
      mstore(add(fmp, 0x44), 0x80)
      // offset additional context
      mstore(add(fmp, 0x64), add(0xa0, encodedDataSize))
      // size revert reason
      mstore(add(fmp, 0x84), returndatasize())
      // revert reason
      returndatacopy(add(fmp, 0xa4), 0, returndatasize())
      // size additional context
      mstore(add(fmp, add(0xa4, encodedDataSize)), 0x04)
      // additional context
      mstore(
        add(fmp, add(0xc4, encodedDataSize)),
        and(additionalContext, 0xffffffff00000000000000000000000000000000000000000000000000000000)
      )
      revert(fmp, add(0xe4, encodedDataSize))
    }
  }
}

File 13 of 43 : IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC20.sol)

pragma solidity >=0.4.16;

import {IERC20} from "../token/ERC20/IERC20.sol";

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

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

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

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

import {IKSConditionalHook} from '../../interfaces/hooks/IKSConditionalHook.sol';
import 'src/hooks/base/BaseHook.sol';

import {
  Condition,
  ConditionTree,
  ConditionTreeLibrary,
  ConditionType
} from '../../types/ConditionTree.sol';

import {Math} from 'openzeppelin-contracts/contracts/utils/math/Math.sol';

/**
 * @param startTimestamp the start timestamp of the condition
 * @param endTimestamp the end timestamp of the condition
 */
struct TimeCondition {
  uint256 startTimestamp;
  uint256 endTimestamp;
}

/**
 * @param targetYield the target yield threshold (1e6 precision)
 * @param initialAmounts the initial amounts of the tokens
 */
struct YieldCondition {
  uint256 targetYield;
  uint256 initialAmounts; // [token0, token1]
}

/**
 * @param minPrice the minimum price of the token (would be in sqrtPriceX96 if uni v3 pool type)
 * @param maxPrice the maximum price of the token (would be in sqrtPriceX96 if uni v3 pool type)
 */
struct PriceCondition {
  uint256 minPrice;
  uint256 maxPrice;
}

abstract contract BaseConditionalHook is BaseHook, IKSConditionalHook {
  error WrongConditionType();

  ConditionType public constant PRICE_BASED = ConditionType.wrap(keccak256('PRICE_BASED'));
  ConditionType public constant TIME_BASED = ConditionType.wrap(keccak256('TIME_BASED'));
  ConditionType public constant YIELD_BASED = ConditionType.wrap(keccak256('YIELD_BASED'));
  uint256 public constant PRECISION = 1_000_000;
  uint256 public constant Q96 = 1 << 96;

  /// @inheritdoc IKSConditionalHook
  function validateConditionTree(ConditionTree calldata tree, uint256 curIndex)
    external
    view
    virtual
  {
    require(
      ConditionTreeLibrary.evaluateConditionTree(tree, curIndex, evaluateCondition),
      ConditionsNotMet()
    );
  }

  /// @inheritdoc IKSConditionalHook
  function evaluateCondition(Condition calldata condition, bytes calldata additionalData)
    public
    view
    virtual
    returns (bool isSatisfied)
  {
    if (condition.isType(TIME_BASED)) {
      isSatisfied = _evaluateTimeCondition(condition);
    } else if (condition.isType(PRICE_BASED)) {
      isSatisfied = _evaluatePriceCondition(condition, additionalData);
    } else if (condition.isType(YIELD_BASED)) {
      isSatisfied = _evaluateYieldCondition(condition, additionalData);
    } else {
      revert WrongConditionType();
    }
  }

  /**
   * @notice helper function to evaluate time condition
   * @param condition the condition to evaluate
   * @return true if the condition is satisfied, false otherwise
   */
  function _evaluateTimeCondition(Condition calldata condition)
    internal
    view
    virtual
    returns (bool)
  {
    TimeCondition calldata timeCondition = _decodeTimeCondition(condition.data);

    return timeCondition.startTimestamp <= block.timestamp
      && timeCondition.endTimestamp >= block.timestamp;
  }

  /**
   * @notice helper function to evaluate price condition
   * @param condition the price condition to evaluate
   * @param additionalData the abi encoded data of the current price
   * @return true if the condition is satisfied, false otherwise
   */
  function _evaluatePriceCondition(Condition calldata condition, bytes calldata additionalData)
    internal
    pure
    virtual
    returns (bool)
  {
    PriceCondition calldata priceCondition = _decodePriceCondition(condition.data);

    uint256 currentPrice;
    assembly ('memory-safe') {
      currentPrice := calldataload(additionalData.offset)
    }

    return priceCondition.minPrice <= currentPrice && priceCondition.maxPrice >= currentPrice;
  }

  /**
   * @notice helper function to evaluate whether the yield condition is satisfied
   * @dev Calculates yield as: (fees_in_token0_terms) / (initial_amounts_in_token0_terms)
   * @param condition The yield condition containing target yield and initial amounts
   * @param additionalData Encoded fee0, fee1, and poolPrice (sqrtPriceX96 if uni v3 pool type) values
   * @return true if actual yield >= target yield, false otherwise
   */
  function _evaluateYieldCondition(Condition calldata condition, bytes calldata additionalData)
    internal
    pure
    virtual
    returns (bool)
  {
    uint256 fee0;
    uint256 fee1;
    uint256 poolPrice;

    assembly ('memory-safe') {
      fee0 := calldataload(additionalData.offset)
      fee1 := calldataload(add(additionalData.offset, 0x20))
      poolPrice := calldataload(add(additionalData.offset, 0x40))
    }

    YieldCondition calldata yieldCondition = _decodeYieldCondition(condition.data);

    uint256 initialAmount0 = yieldCondition.initialAmounts >> 128;
    uint256 initialAmount1 = uint256(uint128(yieldCondition.initialAmounts));

    uint256 numerator = fee0 + _convertToken1ToToken0(poolPrice, fee1);
    uint256 denominator = initialAmount0 + _convertToken1ToToken0(poolPrice, initialAmount1);
    if (denominator == 0) return false;

    uint256 yield = (numerator * PRECISION) / denominator;

    return yield >= yieldCondition.targetYield;
  }

  /**
   * @notice Converts token1 amount to equivalent token0 amount using current price
   * @dev formula: amount0 = amount1 * Q192 / sqrtPriceX96^2
   * @param sqrtPriceX96 The pool's sqrt price
   * @param amount1 Amount of token1 to convert
   * @return amount0 Equivalent amount in token0 terms
   */
  function _convertToken1ToToken0(uint256 sqrtPriceX96, uint256 amount1)
    internal
    pure
    virtual
    returns (uint256 amount0)
  {
    amount0 = Math.mulDiv(Math.mulDiv(amount1, Q96, sqrtPriceX96), Q96, sqrtPriceX96);
  }

  function _decodePriceCondition(bytes calldata data)
    internal
    pure
    returns (PriceCondition calldata priceCondition)
  {
    assembly ('memory-safe') {
      priceCondition := data.offset
    }
  }

  function _decodeTimeCondition(bytes calldata data)
    internal
    pure
    returns (TimeCondition calldata timeCondition)
  {
    assembly ('memory-safe') {
      timeCondition := data.offset
    }
  }

  function _decodeYieldCondition(bytes calldata data)
    internal
    pure
    returns (YieldCondition calldata yieldCondition)
  {
    assembly ('memory-safe') {
      yieldCondition := data.offset
    }
  }
}

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

import {ActionData} from '../../types/ActionData.sol';
import {IntentData} from '../../types/IntentData.sol';
import {IKSSmartIntentRouter} from '../IKSSmartIntentRouter.sol';

interface IKSSmartIntentHook {
  /**
   * @notice Before execution hook
   * @param intentData the intent data
   * @param actionData the data of the action
   * @return fees the amount of fees to be taken
   * @return beforeExecutionData the data representing the state before execution
   */
  function beforeExecution(
    bytes32 intentHash,
    IntentData calldata intentData,
    ActionData calldata actionData
  ) external returns (uint256[] memory fees, bytes memory beforeExecutionData);

  /**
   * @notice After execution hook
   * @param intentData the intent data
   * @param beforeExecutionData the data returned from `beforeExecution`
   * @param actionResult the result of the action
   * @return tokens the tokens to be taken fees from and to be returned to the recipient
   * @return fees the fees to be taken
   * @return amounts the amounts of the tokens to be returned to the recipient
   * @return recipient the address of the recipient
   */
  function afterExecution(
    bytes32 intentHash,
    IntentData calldata intentData,
    bytes calldata beforeExecutionData,
    bytes calldata actionResult
  )
    external
    returns (
      address[] memory tokens,
      uint256[] memory fees,
      uint256[] memory amounts,
      address recipient
    );
}

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

import {IPoolManager} from './IPoolManager.sol';
import {PoolKey} from './Types.sol';

import {IERC721} from 'openzeppelin-contracts/contracts/token/ERC721/IERC721.sol';

/// @title IPositionManager
/// @notice Interface for the PositionManager contract
interface IPositionManager is IERC721 {
  /// @notice Thrown when the caller is not approved to modify a position
  error NotApproved(address caller);
  /// @notice Thrown when the block.timestamp exceeds the user-provided deadline
  error DeadlinePassed(uint256 deadline);
  /// @notice Thrown when calling transfer, subscribe, or unsubscribe when the PoolManager is unlocked.
  /// @dev This is to prevent hooks from being able to trigger notifications at the same time the position is being modified.
  error PoolManagerMustBeLocked();

  /// @param tokenId the ERC721 tokenId
  /// @return liquidity the position's liquidity, as a liquidityAmount
  /// @dev this value can be processed as an amount0 and amount1 by using the LiquidityAmounts library
  function getPositionLiquidity(uint256 tokenId) external view returns (uint128 liquidity);

  /// @param tokenId the ERC721 tokenId
  /// @return PositionInfo a uint256 packed value holding information about the position including the range (tickLower, tickUpper)
  /// @return poolKey the pool key of the position
  function getPoolAndPositionInfo(uint256 tokenId) external view returns (PoolKey memory, uint256);

  function poolManager() external view returns (IPoolManager);

  function modifyLiquidities(bytes calldata unlockData, uint256 deadline) external payable;
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC721/IERC721.sol)

pragma solidity >=0.6.2;

import {IERC165} from "../../utils/introspection/IERC165.sol";

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

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

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

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

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

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

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

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

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

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

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

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

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

type ConditionType is bytes32;

enum OperationType {
  AND,
  OR
}

/**
 * @param conditionType the type of the condition
 * @param data the data of the condition
 */
struct Condition {
  ConditionType conditionType;
  bytes data;
}

/**
 * @param operationType the type of the operation (AND or OR)
 * @param condition the condition to be validated
 * @param childrenIndexes the indexes of the children nodes (if the node is a leaf, this is empty)
 */
struct Node {
  OperationType operationType;
  Condition condition;
  uint256[] childrenIndexes;
}

/**
 * @param nodes the nodes of the condition tree
 * @param additionalData the additional data to be validated or used for validation for each node (should be empty for non-leaf nodes)
 */
struct ConditionTree {
  Node[] nodes;
  bytes[] additionalData;
}

using ConditionTreeLibrary for ConditionTree global;
using ConditionTreeLibrary for Node global;
using ConditionTreeLibrary for Condition global;

/**
 * @notice Library for condition tree evaluation
 */
library ConditionTreeLibrary {
  error InvalidNodeIndex();
  error WrongOperationType();

  OperationType public constant AND = OperationType.AND;
  OperationType public constant OR = OperationType.OR;

  /**
   * @notice Recursively evaluates a node in a condition tree
   * @dev The algorithm assumes that the condition tree structure is valid, meaning:
   *      - No cycle paths exist in the tree
   *      - Each node is only visited once during traversal
   *      - All childrenIndexes point to valid nodes within the array bounds
   *      Invalid tree structures could lead to revert, or invalid results.
   * @param tree the condition tree to be evaluated
   * @param curIndex index of current node to evaluate (must be < nodes.length and != childIndex)
   * @param evaluateCondition the custom function holding the logic for evaluating the condition of the leaf node
   * @return true if the condition tree is satisfied, false otherwise
   */
  function evaluateConditionTree(
    ConditionTree calldata tree,
    uint256 curIndex,
    function(Condition calldata, bytes calldata) view returns (bool) evaluateCondition
  ) internal view returns (bool) {
    require(curIndex < tree.nodes.length, InvalidNodeIndex());
    Node calldata node = tree.nodes[curIndex];

    if (node.isLeaf()) {
      return evaluateCondition(node.condition, tree.additionalData[curIndex]);
    }

    // non-leaf node
    uint256 length = node.childrenIndexes.length;
    uint256 childIndex;
    if (node.operationType == AND) {
      for (uint256 i; i < length; ++i) {
        childIndex = node.childrenIndexes[i];
        if (!tree.evaluateConditionTree(childIndex, evaluateCondition)) {
          return false;
        }
      }
      return true;
    } else if (node.operationType == OR) {
      for (uint256 i; i < length; ++i) {
        childIndex = node.childrenIndexes[i];
        if (tree.evaluateConditionTree(childIndex, evaluateCondition)) {
          return true;
        }
      }
      return false;
    } else {
      revert WrongOperationType();
    }
  }

  /**
   * @notice Checks if a node is a leaf node
   * @param node the node to check
   * @return true if the node is a leaf node, false otherwise
   */
  function isLeaf(Node calldata node) internal pure returns (bool) {
    return node.childrenIndexes.length == 0;
  }

  /**
   * @notice Checks if a condition is of a specific type
   * @param condition the condition to check
   * @param conditionType the type to check against
   * @return true if the condition is of the specified type, false otherwise
   */
  function isType(Condition calldata condition, ConditionType conditionType)
    internal
    pure
    returns (bool)
  {
    return ConditionType.unwrap(condition.conditionType) == ConditionType.unwrap(conditionType);
  }
}

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

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

import '../libraries/BitMask.sol';
/**
 * @notice FeeConfig is packed version of solidity structure.
 *
 * Layout: 1 bit feeMode | 24 bits partnerFee | 160 bits partnerRecipient
 */

type FeeConfig is uint256;

/**
 * @notice FeeInfo is a struct that contains the protocol recipient and the fee configs for the partners
 * @param protocolRecipient The protocol recipient
 * @param partnerFeeConfigs The fee configs for the partners
 */
struct FeeInfo {
  address protocolRecipient;
  FeeConfig[][] partnerFeeConfigs;
}

using FeeInfoLibrary for FeeInfo global;
using FeeInfoLibrary for FeeConfig global;

library FeeInfoLibrary {
  uint256 internal constant PROTOCOL_BPS_OFFSET = 160;
  uint256 internal constant FEE_MODE_OFFSET = 184;
  uint256 internal constant FEE_DENOMINATOR = 1_000_000;

  bytes32 constant FEE_INFO_TYPE_HASH =
    keccak256(abi.encodePacked('FeeInfo(address protocolRecipient,uint256[][] partnerFeeConfigs)'));

  function feeMode(FeeConfig self) internal pure returns (bool _feeMode) {
    assembly ('memory-safe') {
      _feeMode := and(shr(FEE_MODE_OFFSET, self), MASK_1_BIT)
    }
  }

  function partnerFee(FeeConfig self) internal pure returns (uint24 _partnerFee) {
    assembly ('memory-safe') {
      _partnerFee := and(shr(PROTOCOL_BPS_OFFSET, self), MASK_24_BITS)
    }
  }

  function partnerRecipient(FeeConfig self) internal pure returns (address _partnerRecipient) {
    assembly ('memory-safe') {
      _partnerRecipient := and(self, MASK_160_BITS)
    }
  }

  function computeFees(FeeConfig[] calldata self, uint256 totalAmount)
    internal
    pure
    returns (uint256 protocolFeeAmount, uint256[] memory partnersFeeAmounts)
  {
    unchecked {
      partnersFeeAmounts = new uint256[](self.length);
      uint256 _totalPartnerFeePrecision;
      uint256 _totalPartnerFeeAmount;
      uint256 _feeAmount;
      uint24 _partnerFee;

      for (uint256 i = 0; i < self.length; i++) {
        _partnerFee = self[i].partnerFee();
        _feeAmount = (totalAmount * _partnerFee) / FEE_DENOMINATOR;
        partnersFeeAmounts[i] = _feeAmount;

        _totalPartnerFeePrecision += _partnerFee;
        _totalPartnerFeeAmount += _feeAmount;
      }
      protocolFeeAmount = totalAmount - _totalPartnerFeeAmount;

      require(_totalPartnerFeePrecision <= FEE_DENOMINATOR, IKSSmartIntentRouter.InvalidFeeConfig());
    }
  }

  function hash(FeeInfo calldata self) internal pure returns (bytes32) {
    bytes32[] memory partnersFeeConfigsHashes = new bytes32[](self.partnerFeeConfigs.length);
    for (uint256 i = 0; i < self.partnerFeeConfigs.length; i++) {
      partnersFeeConfigsHashes[i] = keccak256(abi.encodePacked(self.partnerFeeConfigs[i]));
    }
    return keccak256(
      abi.encode(
        FEE_INFO_TYPE_HASH,
        self.protocolRecipient,
        keccak256(abi.encodePacked(partnersFeeConfigsHashes))
      )
    );
  }
}

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

/**
 * @notice Data structure for core components of intent
 * @param mainAddress The main address
 * @param signatureVerifier The address of the signature verifier
 * @param delegatedKey The delegated key
 * @param actionContracts The addresses of the action contracts
 * @param actionSelectors The selectors of the action functions
 * @param hook The address of the hook
 * @param hookIntentData The intent data for the hook
 */
struct IntentCoreData {
  address mainAddress;
  address signatureVerifier;
  bytes delegatedKey;
  address[] actionContracts;
  bytes4[] actionSelectors;
  address hook;
  bytes hookIntentData;
}

using IntentCoreDataLibrary for IntentCoreData global;

library IntentCoreDataLibrary {
  bytes32 constant INTENT_CORE_DATA_TYPE_HASH = keccak256(
    abi.encodePacked(
      'IntentCoreData(address mainAddress,address signatureVerifier,bytes delegatedKey,address[] actionContracts,bytes4[] actionSelectors,address hook,bytes hookIntentData)'
    )
  );

  function hash(IntentCoreData calldata self) internal pure returns (bytes32) {
    return keccak256(
      abi.encode(
        INTENT_CORE_DATA_TYPE_HASH,
        self.mainAddress,
        self.signatureVerifier,
        keccak256(self.delegatedKey),
        keccak256(abi.encodePacked(self.actionContracts)),
        keccak256(abi.encodePacked(self.actionSelectors)),
        self.hook,
        keccak256(self.hookIntentData)
      )
    );
  }
}

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

import {ERC20Data} from './ERC20Data.sol';
import {ERC721Data} from './ERC721Data.sol';

struct TokenData {
  ERC20Data[] erc20Data;
  ERC721Data[] erc721Data;
}

using TokenDataLibrary for TokenData global;

library TokenDataLibrary {
  bytes32 constant TOKEN_DATA_TYPE_HASH = keccak256(
    abi.encodePacked(
      'TokenData(ERC20Data[] erc20Data,ERC721Data[] erc721Data)ERC20Data(address token,uint256 amount,bytes permitData)ERC721Data(address token,uint256 tokenId,bytes permitData)'
    )
  );

  function hash(TokenData calldata self) internal pure returns (bytes32) {
    bytes32[] memory erc20DataHashes = new bytes32[](self.erc20Data.length);
    for (uint256 i = 0; i < self.erc20Data.length; i++) {
      erc20DataHashes[i] = self.erc20Data[i].hash();
    }

    bytes32[] memory erc721DataHashes = new bytes32[](self.erc721Data.length);
    for (uint256 i = 0; i < self.erc721Data.length; i++) {
      erc721DataHashes[i] = self.erc721Data[i].hash();
    }

    return keccak256(
      abi.encode(
        TOKEN_DATA_TYPE_HASH,
        keccak256(abi.encodePacked(erc20DataHashes)),
        keccak256(abi.encodePacked(erc721DataHashes))
      )
    );
  }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/Panic.sol)

pragma solidity ^0.8.20;

/**
 * @dev Helper library for emitting standardized panic codes.
 *
 * ```solidity
 * contract Example {
 *      using Panic for uint256;
 *
 *      // Use any of the declared internal constants
 *      function foo() { Panic.GENERIC.panic(); }
 *
 *      // Alternatively
 *      function foo() { Panic.panic(Panic.GENERIC); }
 * }
 * ```
 *
 * Follows the list from https://github.com/ethereum/solidity/blob/v0.8.24/libsolutil/ErrorCodes.h[libsolutil].
 *
 * _Available since v5.1._
 */
// slither-disable-next-line unused-state
library Panic {
    /// @dev generic / unspecified error
    uint256 internal constant GENERIC = 0x00;
    /// @dev used by the assert() builtin
    uint256 internal constant ASSERT = 0x01;
    /// @dev arithmetic underflow or overflow
    uint256 internal constant UNDER_OVERFLOW = 0x11;
    /// @dev division or modulo by zero
    uint256 internal constant DIVISION_BY_ZERO = 0x12;
    /// @dev enum conversion error
    uint256 internal constant ENUM_CONVERSION_ERROR = 0x21;
    /// @dev invalid encoding in storage
    uint256 internal constant STORAGE_ENCODING_ERROR = 0x22;
    /// @dev empty array pop
    uint256 internal constant EMPTY_ARRAY_POP = 0x31;
    /// @dev array out of bounds access
    uint256 internal constant ARRAY_OUT_OF_BOUNDS = 0x32;
    /// @dev resource error (too large allocation or too large array)
    uint256 internal constant RESOURCE_ERROR = 0x41;
    /// @dev calling invalid internal function
    uint256 internal constant INVALID_INTERNAL_FUNCTION = 0x51;

    /// @dev Reverts with a panic code. Recommended to use with
    /// the internal constants with predefined codes.
    function panic(uint256 code) internal pure {
        assembly ("memory-safe") {
            mstore(0x00, 0x4e487b71)
            mstore(0x20, code)
            revert(0x1c, 0x24)
        }
    }
}

File 24 of 43 : SafeCast.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.

pragma solidity ^0.8.20;

/**
 * @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow
 * checks.
 *
 * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
 * easily result in undesired exploitation or bugs, since developers usually
 * assume that overflows raise errors. `SafeCast` restores this intuition by
 * reverting the transaction when such an operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeCast {
    /**
     * @dev Value doesn't fit in an uint of `bits` size.
     */
    error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);

    /**
     * @dev An int value doesn't fit in an uint of `bits` size.
     */
    error SafeCastOverflowedIntToUint(int256 value);

    /**
     * @dev Value doesn't fit in an int of `bits` size.
     */
    error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);

    /**
     * @dev An uint value doesn't fit in an int of `bits` size.
     */
    error SafeCastOverflowedUintToInt(uint256 value);

    /**
     * @dev Returns the downcasted uint248 from uint256, reverting on
     * overflow (when the input is greater than largest uint248).
     *
     * Counterpart to Solidity's `uint248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     */
    function toUint248(uint256 value) internal pure returns (uint248) {
        if (value > type(uint248).max) {
            revert SafeCastOverflowedUintDowncast(248, value);
        }
        return uint248(value);
    }

    /**
     * @dev Returns the downcasted uint240 from uint256, reverting on
     * overflow (when the input is greater than largest uint240).
     *
     * Counterpart to Solidity's `uint240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     */
    function toUint240(uint256 value) internal pure returns (uint240) {
        if (value > type(uint240).max) {
            revert SafeCastOverflowedUintDowncast(240, value);
        }
        return uint240(value);
    }

    /**
     * @dev Returns the downcasted uint232 from uint256, reverting on
     * overflow (when the input is greater than largest uint232).
     *
     * Counterpart to Solidity's `uint232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     */
    function toUint232(uint256 value) internal pure returns (uint232) {
        if (value > type(uint232).max) {
            revert SafeCastOverflowedUintDowncast(232, value);
        }
        return uint232(value);
    }

    /**
     * @dev Returns the downcasted uint224 from uint256, reverting on
     * overflow (when the input is greater than largest uint224).
     *
     * Counterpart to Solidity's `uint224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     */
    function toUint224(uint256 value) internal pure returns (uint224) {
        if (value > type(uint224).max) {
            revert SafeCastOverflowedUintDowncast(224, value);
        }
        return uint224(value);
    }

    /**
     * @dev Returns the downcasted uint216 from uint256, reverting on
     * overflow (when the input is greater than largest uint216).
     *
     * Counterpart to Solidity's `uint216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     */
    function toUint216(uint256 value) internal pure returns (uint216) {
        if (value > type(uint216).max) {
            revert SafeCastOverflowedUintDowncast(216, value);
        }
        return uint216(value);
    }

    /**
     * @dev Returns the downcasted uint208 from uint256, reverting on
     * overflow (when the input is greater than largest uint208).
     *
     * Counterpart to Solidity's `uint208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     */
    function toUint208(uint256 value) internal pure returns (uint208) {
        if (value > type(uint208).max) {
            revert SafeCastOverflowedUintDowncast(208, value);
        }
        return uint208(value);
    }

    /**
     * @dev Returns the downcasted uint200 from uint256, reverting on
     * overflow (when the input is greater than largest uint200).
     *
     * Counterpart to Solidity's `uint200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     */
    function toUint200(uint256 value) internal pure returns (uint200) {
        if (value > type(uint200).max) {
            revert SafeCastOverflowedUintDowncast(200, value);
        }
        return uint200(value);
    }

    /**
     * @dev Returns the downcasted uint192 from uint256, reverting on
     * overflow (when the input is greater than largest uint192).
     *
     * Counterpart to Solidity's `uint192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     */
    function toUint192(uint256 value) internal pure returns (uint192) {
        if (value > type(uint192).max) {
            revert SafeCastOverflowedUintDowncast(192, value);
        }
        return uint192(value);
    }

    /**
     * @dev Returns the downcasted uint184 from uint256, reverting on
     * overflow (when the input is greater than largest uint184).
     *
     * Counterpart to Solidity's `uint184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     */
    function toUint184(uint256 value) internal pure returns (uint184) {
        if (value > type(uint184).max) {
            revert SafeCastOverflowedUintDowncast(184, value);
        }
        return uint184(value);
    }

    /**
     * @dev Returns the downcasted uint176 from uint256, reverting on
     * overflow (when the input is greater than largest uint176).
     *
     * Counterpart to Solidity's `uint176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     */
    function toUint176(uint256 value) internal pure returns (uint176) {
        if (value > type(uint176).max) {
            revert SafeCastOverflowedUintDowncast(176, value);
        }
        return uint176(value);
    }

    /**
     * @dev Returns the downcasted uint168 from uint256, reverting on
     * overflow (when the input is greater than largest uint168).
     *
     * Counterpart to Solidity's `uint168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     */
    function toUint168(uint256 value) internal pure returns (uint168) {
        if (value > type(uint168).max) {
            revert SafeCastOverflowedUintDowncast(168, value);
        }
        return uint168(value);
    }

    /**
     * @dev Returns the downcasted uint160 from uint256, reverting on
     * overflow (when the input is greater than largest uint160).
     *
     * Counterpart to Solidity's `uint160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     */
    function toUint160(uint256 value) internal pure returns (uint160) {
        if (value > type(uint160).max) {
            revert SafeCastOverflowedUintDowncast(160, value);
        }
        return uint160(value);
    }

    /**
     * @dev Returns the downcasted uint152 from uint256, reverting on
     * overflow (when the input is greater than largest uint152).
     *
     * Counterpart to Solidity's `uint152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     */
    function toUint152(uint256 value) internal pure returns (uint152) {
        if (value > type(uint152).max) {
            revert SafeCastOverflowedUintDowncast(152, value);
        }
        return uint152(value);
    }

    /**
     * @dev Returns the downcasted uint144 from uint256, reverting on
     * overflow (when the input is greater than largest uint144).
     *
     * Counterpart to Solidity's `uint144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     */
    function toUint144(uint256 value) internal pure returns (uint144) {
        if (value > type(uint144).max) {
            revert SafeCastOverflowedUintDowncast(144, value);
        }
        return uint144(value);
    }

    /**
     * @dev Returns the downcasted uint136 from uint256, reverting on
     * overflow (when the input is greater than largest uint136).
     *
     * Counterpart to Solidity's `uint136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     */
    function toUint136(uint256 value) internal pure returns (uint136) {
        if (value > type(uint136).max) {
            revert SafeCastOverflowedUintDowncast(136, value);
        }
        return uint136(value);
    }

    /**
     * @dev Returns the downcasted uint128 from uint256, reverting on
     * overflow (when the input is greater than largest uint128).
     *
     * Counterpart to Solidity's `uint128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     */
    function toUint128(uint256 value) internal pure returns (uint128) {
        if (value > type(uint128).max) {
            revert SafeCastOverflowedUintDowncast(128, value);
        }
        return uint128(value);
    }

    /**
     * @dev Returns the downcasted uint120 from uint256, reverting on
     * overflow (when the input is greater than largest uint120).
     *
     * Counterpart to Solidity's `uint120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     */
    function toUint120(uint256 value) internal pure returns (uint120) {
        if (value > type(uint120).max) {
            revert SafeCastOverflowedUintDowncast(120, value);
        }
        return uint120(value);
    }

    /**
     * @dev Returns the downcasted uint112 from uint256, reverting on
     * overflow (when the input is greater than largest uint112).
     *
     * Counterpart to Solidity's `uint112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     */
    function toUint112(uint256 value) internal pure returns (uint112) {
        if (value > type(uint112).max) {
            revert SafeCastOverflowedUintDowncast(112, value);
        }
        return uint112(value);
    }

    /**
     * @dev Returns the downcasted uint104 from uint256, reverting on
     * overflow (when the input is greater than largest uint104).
     *
     * Counterpart to Solidity's `uint104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     */
    function toUint104(uint256 value) internal pure returns (uint104) {
        if (value > type(uint104).max) {
            revert SafeCastOverflowedUintDowncast(104, value);
        }
        return uint104(value);
    }

    /**
     * @dev Returns the downcasted uint96 from uint256, reverting on
     * overflow (when the input is greater than largest uint96).
     *
     * Counterpart to Solidity's `uint96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     */
    function toUint96(uint256 value) internal pure returns (uint96) {
        if (value > type(uint96).max) {
            revert SafeCastOverflowedUintDowncast(96, value);
        }
        return uint96(value);
    }

    /**
     * @dev Returns the downcasted uint88 from uint256, reverting on
     * overflow (when the input is greater than largest uint88).
     *
     * Counterpart to Solidity's `uint88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     */
    function toUint88(uint256 value) internal pure returns (uint88) {
        if (value > type(uint88).max) {
            revert SafeCastOverflowedUintDowncast(88, value);
        }
        return uint88(value);
    }

    /**
     * @dev Returns the downcasted uint80 from uint256, reverting on
     * overflow (when the input is greater than largest uint80).
     *
     * Counterpart to Solidity's `uint80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     */
    function toUint80(uint256 value) internal pure returns (uint80) {
        if (value > type(uint80).max) {
            revert SafeCastOverflowedUintDowncast(80, value);
        }
        return uint80(value);
    }

    /**
     * @dev Returns the downcasted uint72 from uint256, reverting on
     * overflow (when the input is greater than largest uint72).
     *
     * Counterpart to Solidity's `uint72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     */
    function toUint72(uint256 value) internal pure returns (uint72) {
        if (value > type(uint72).max) {
            revert SafeCastOverflowedUintDowncast(72, value);
        }
        return uint72(value);
    }

    /**
     * @dev Returns the downcasted uint64 from uint256, reverting on
     * overflow (when the input is greater than largest uint64).
     *
     * Counterpart to Solidity's `uint64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     */
    function toUint64(uint256 value) internal pure returns (uint64) {
        if (value > type(uint64).max) {
            revert SafeCastOverflowedUintDowncast(64, value);
        }
        return uint64(value);
    }

    /**
     * @dev Returns the downcasted uint56 from uint256, reverting on
     * overflow (when the input is greater than largest uint56).
     *
     * Counterpart to Solidity's `uint56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     */
    function toUint56(uint256 value) internal pure returns (uint56) {
        if (value > type(uint56).max) {
            revert SafeCastOverflowedUintDowncast(56, value);
        }
        return uint56(value);
    }

    /**
     * @dev Returns the downcasted uint48 from uint256, reverting on
     * overflow (when the input is greater than largest uint48).
     *
     * Counterpart to Solidity's `uint48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     */
    function toUint48(uint256 value) internal pure returns (uint48) {
        if (value > type(uint48).max) {
            revert SafeCastOverflowedUintDowncast(48, value);
        }
        return uint48(value);
    }

    /**
     * @dev Returns the downcasted uint40 from uint256, reverting on
     * overflow (when the input is greater than largest uint40).
     *
     * Counterpart to Solidity's `uint40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     */
    function toUint40(uint256 value) internal pure returns (uint40) {
        if (value > type(uint40).max) {
            revert SafeCastOverflowedUintDowncast(40, value);
        }
        return uint40(value);
    }

    /**
     * @dev Returns the downcasted uint32 from uint256, reverting on
     * overflow (when the input is greater than largest uint32).
     *
     * Counterpart to Solidity's `uint32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     */
    function toUint32(uint256 value) internal pure returns (uint32) {
        if (value > type(uint32).max) {
            revert SafeCastOverflowedUintDowncast(32, value);
        }
        return uint32(value);
    }

    /**
     * @dev Returns the downcasted uint24 from uint256, reverting on
     * overflow (when the input is greater than largest uint24).
     *
     * Counterpart to Solidity's `uint24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     */
    function toUint24(uint256 value) internal pure returns (uint24) {
        if (value > type(uint24).max) {
            revert SafeCastOverflowedUintDowncast(24, value);
        }
        return uint24(value);
    }

    /**
     * @dev Returns the downcasted uint16 from uint256, reverting on
     * overflow (when the input is greater than largest uint16).
     *
     * Counterpart to Solidity's `uint16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     */
    function toUint16(uint256 value) internal pure returns (uint16) {
        if (value > type(uint16).max) {
            revert SafeCastOverflowedUintDowncast(16, value);
        }
        return uint16(value);
    }

    /**
     * @dev Returns the downcasted uint8 from uint256, reverting on
     * overflow (when the input is greater than largest uint8).
     *
     * Counterpart to Solidity's `uint8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     */
    function toUint8(uint256 value) internal pure returns (uint8) {
        if (value > type(uint8).max) {
            revert SafeCastOverflowedUintDowncast(8, value);
        }
        return uint8(value);
    }

    /**
     * @dev Converts a signed int256 into an unsigned uint256.
     *
     * Requirements:
     *
     * - input must be greater than or equal to 0.
     */
    function toUint256(int256 value) internal pure returns (uint256) {
        if (value < 0) {
            revert SafeCastOverflowedIntToUint(value);
        }
        return uint256(value);
    }

    /**
     * @dev Returns the downcasted int248 from int256, reverting on
     * overflow (when the input is less than smallest int248 or
     * greater than largest int248).
     *
     * Counterpart to Solidity's `int248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     */
    function toInt248(int256 value) internal pure returns (int248 downcasted) {
        downcasted = int248(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(248, value);
        }
    }

    /**
     * @dev Returns the downcasted int240 from int256, reverting on
     * overflow (when the input is less than smallest int240 or
     * greater than largest int240).
     *
     * Counterpart to Solidity's `int240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     */
    function toInt240(int256 value) internal pure returns (int240 downcasted) {
        downcasted = int240(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(240, value);
        }
    }

    /**
     * @dev Returns the downcasted int232 from int256, reverting on
     * overflow (when the input is less than smallest int232 or
     * greater than largest int232).
     *
     * Counterpart to Solidity's `int232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     */
    function toInt232(int256 value) internal pure returns (int232 downcasted) {
        downcasted = int232(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(232, value);
        }
    }

    /**
     * @dev Returns the downcasted int224 from int256, reverting on
     * overflow (when the input is less than smallest int224 or
     * greater than largest int224).
     *
     * Counterpart to Solidity's `int224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     */
    function toInt224(int256 value) internal pure returns (int224 downcasted) {
        downcasted = int224(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(224, value);
        }
    }

    /**
     * @dev Returns the downcasted int216 from int256, reverting on
     * overflow (when the input is less than smallest int216 or
     * greater than largest int216).
     *
     * Counterpart to Solidity's `int216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     */
    function toInt216(int256 value) internal pure returns (int216 downcasted) {
        downcasted = int216(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(216, value);
        }
    }

    /**
     * @dev Returns the downcasted int208 from int256, reverting on
     * overflow (when the input is less than smallest int208 or
     * greater than largest int208).
     *
     * Counterpart to Solidity's `int208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     */
    function toInt208(int256 value) internal pure returns (int208 downcasted) {
        downcasted = int208(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(208, value);
        }
    }

    /**
     * @dev Returns the downcasted int200 from int256, reverting on
     * overflow (when the input is less than smallest int200 or
     * greater than largest int200).
     *
     * Counterpart to Solidity's `int200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     */
    function toInt200(int256 value) internal pure returns (int200 downcasted) {
        downcasted = int200(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(200, value);
        }
    }

    /**
     * @dev Returns the downcasted int192 from int256, reverting on
     * overflow (when the input is less than smallest int192 or
     * greater than largest int192).
     *
     * Counterpart to Solidity's `int192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     */
    function toInt192(int256 value) internal pure returns (int192 downcasted) {
        downcasted = int192(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(192, value);
        }
    }

    /**
     * @dev Returns the downcasted int184 from int256, reverting on
     * overflow (when the input is less than smallest int184 or
     * greater than largest int184).
     *
     * Counterpart to Solidity's `int184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     */
    function toInt184(int256 value) internal pure returns (int184 downcasted) {
        downcasted = int184(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(184, value);
        }
    }

    /**
     * @dev Returns the downcasted int176 from int256, reverting on
     * overflow (when the input is less than smallest int176 or
     * greater than largest int176).
     *
     * Counterpart to Solidity's `int176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     */
    function toInt176(int256 value) internal pure returns (int176 downcasted) {
        downcasted = int176(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(176, value);
        }
    }

    /**
     * @dev Returns the downcasted int168 from int256, reverting on
     * overflow (when the input is less than smallest int168 or
     * greater than largest int168).
     *
     * Counterpart to Solidity's `int168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     */
    function toInt168(int256 value) internal pure returns (int168 downcasted) {
        downcasted = int168(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(168, value);
        }
    }

    /**
     * @dev Returns the downcasted int160 from int256, reverting on
     * overflow (when the input is less than smallest int160 or
     * greater than largest int160).
     *
     * Counterpart to Solidity's `int160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     */
    function toInt160(int256 value) internal pure returns (int160 downcasted) {
        downcasted = int160(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(160, value);
        }
    }

    /**
     * @dev Returns the downcasted int152 from int256, reverting on
     * overflow (when the input is less than smallest int152 or
     * greater than largest int152).
     *
     * Counterpart to Solidity's `int152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     */
    function toInt152(int256 value) internal pure returns (int152 downcasted) {
        downcasted = int152(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(152, value);
        }
    }

    /**
     * @dev Returns the downcasted int144 from int256, reverting on
     * overflow (when the input is less than smallest int144 or
     * greater than largest int144).
     *
     * Counterpart to Solidity's `int144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     */
    function toInt144(int256 value) internal pure returns (int144 downcasted) {
        downcasted = int144(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(144, value);
        }
    }

    /**
     * @dev Returns the downcasted int136 from int256, reverting on
     * overflow (when the input is less than smallest int136 or
     * greater than largest int136).
     *
     * Counterpart to Solidity's `int136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     */
    function toInt136(int256 value) internal pure returns (int136 downcasted) {
        downcasted = int136(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(136, value);
        }
    }

    /**
     * @dev Returns the downcasted int128 from int256, reverting on
     * overflow (when the input is less than smallest int128 or
     * greater than largest int128).
     *
     * Counterpart to Solidity's `int128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     */
    function toInt128(int256 value) internal pure returns (int128 downcasted) {
        downcasted = int128(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(128, value);
        }
    }

    /**
     * @dev Returns the downcasted int120 from int256, reverting on
     * overflow (when the input is less than smallest int120 or
     * greater than largest int120).
     *
     * Counterpart to Solidity's `int120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     */
    function toInt120(int256 value) internal pure returns (int120 downcasted) {
        downcasted = int120(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(120, value);
        }
    }

    /**
     * @dev Returns the downcasted int112 from int256, reverting on
     * overflow (when the input is less than smallest int112 or
     * greater than largest int112).
     *
     * Counterpart to Solidity's `int112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     */
    function toInt112(int256 value) internal pure returns (int112 downcasted) {
        downcasted = int112(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(112, value);
        }
    }

    /**
     * @dev Returns the downcasted int104 from int256, reverting on
     * overflow (when the input is less than smallest int104 or
     * greater than largest int104).
     *
     * Counterpart to Solidity's `int104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     */
    function toInt104(int256 value) internal pure returns (int104 downcasted) {
        downcasted = int104(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(104, value);
        }
    }

    /**
     * @dev Returns the downcasted int96 from int256, reverting on
     * overflow (when the input is less than smallest int96 or
     * greater than largest int96).
     *
     * Counterpart to Solidity's `int96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     */
    function toInt96(int256 value) internal pure returns (int96 downcasted) {
        downcasted = int96(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(96, value);
        }
    }

    /**
     * @dev Returns the downcasted int88 from int256, reverting on
     * overflow (when the input is less than smallest int88 or
     * greater than largest int88).
     *
     * Counterpart to Solidity's `int88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     */
    function toInt88(int256 value) internal pure returns (int88 downcasted) {
        downcasted = int88(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(88, value);
        }
    }

    /**
     * @dev Returns the downcasted int80 from int256, reverting on
     * overflow (when the input is less than smallest int80 or
     * greater than largest int80).
     *
     * Counterpart to Solidity's `int80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     */
    function toInt80(int256 value) internal pure returns (int80 downcasted) {
        downcasted = int80(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(80, value);
        }
    }

    /**
     * @dev Returns the downcasted int72 from int256, reverting on
     * overflow (when the input is less than smallest int72 or
     * greater than largest int72).
     *
     * Counterpart to Solidity's `int72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     */
    function toInt72(int256 value) internal pure returns (int72 downcasted) {
        downcasted = int72(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(72, value);
        }
    }

    /**
     * @dev Returns the downcasted int64 from int256, reverting on
     * overflow (when the input is less than smallest int64 or
     * greater than largest int64).
     *
     * Counterpart to Solidity's `int64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     */
    function toInt64(int256 value) internal pure returns (int64 downcasted) {
        downcasted = int64(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(64, value);
        }
    }

    /**
     * @dev Returns the downcasted int56 from int256, reverting on
     * overflow (when the input is less than smallest int56 or
     * greater than largest int56).
     *
     * Counterpart to Solidity's `int56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     */
    function toInt56(int256 value) internal pure returns (int56 downcasted) {
        downcasted = int56(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(56, value);
        }
    }

    /**
     * @dev Returns the downcasted int48 from int256, reverting on
     * overflow (when the input is less than smallest int48 or
     * greater than largest int48).
     *
     * Counterpart to Solidity's `int48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     */
    function toInt48(int256 value) internal pure returns (int48 downcasted) {
        downcasted = int48(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(48, value);
        }
    }

    /**
     * @dev Returns the downcasted int40 from int256, reverting on
     * overflow (when the input is less than smallest int40 or
     * greater than largest int40).
     *
     * Counterpart to Solidity's `int40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     */
    function toInt40(int256 value) internal pure returns (int40 downcasted) {
        downcasted = int40(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(40, value);
        }
    }

    /**
     * @dev Returns the downcasted int32 from int256, reverting on
     * overflow (when the input is less than smallest int32 or
     * greater than largest int32).
     *
     * Counterpart to Solidity's `int32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     */
    function toInt32(int256 value) internal pure returns (int32 downcasted) {
        downcasted = int32(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(32, value);
        }
    }

    /**
     * @dev Returns the downcasted int24 from int256, reverting on
     * overflow (when the input is less than smallest int24 or
     * greater than largest int24).
     *
     * Counterpart to Solidity's `int24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     */
    function toInt24(int256 value) internal pure returns (int24 downcasted) {
        downcasted = int24(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(24, value);
        }
    }

    /**
     * @dev Returns the downcasted int16 from int256, reverting on
     * overflow (when the input is less than smallest int16 or
     * greater than largest int16).
     *
     * Counterpart to Solidity's `int16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     */
    function toInt16(int256 value) internal pure returns (int16 downcasted) {
        downcasted = int16(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(16, value);
        }
    }

    /**
     * @dev Returns the downcasted int8 from int256, reverting on
     * overflow (when the input is less than smallest int8 or
     * greater than largest int8).
     *
     * Counterpart to Solidity's `int8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     */
    function toInt8(int256 value) internal pure returns (int8 downcasted) {
        downcasted = int8(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(8, value);
        }
    }

    /**
     * @dev Converts an unsigned uint256 into a signed int256.
     *
     * Requirements:
     *
     * - input must be less than or equal to maxInt256.
     */
    function toInt256(uint256 value) internal pure returns (int256) {
        // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
        if (value > uint256(type(int256).max)) {
            revert SafeCastOverflowedUintToInt(value);
        }
        return int256(value);
    }

    /**
     * @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
     */
    function toUint(bool b) internal pure returns (uint256 u) {
        assembly ("memory-safe") {
            u := iszero(iszero(b))
        }
    }
}

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

pragma solidity >=0.4.16;

/**
 * @dev Interface of the ERC-20 standard as defined in the ERC.
 */
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 value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

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

    /**
     * @dev Moves a `value` amount of 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 value) 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 a `value` amount of tokens 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 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` 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 value) external returns (bool);
}

File 26 of 43 : BaseHook.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

import {IKSSmartIntentHook} from '../../interfaces/hooks/IKSSmartIntentHook.sol';

import {ActionData} from '../../types/ActionData.sol';

abstract contract BaseHook is IKSSmartIntentHook {
  error InvalidTokenData();

  modifier checkTokenLengths(ActionData calldata actionData) virtual;
}

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

import {Condition, ConditionTree} from '../../types/ConditionTree.sol';

interface IKSConditionalHook {
  error ConditionsNotMet();

  /**
   * @notice Validates a condition tree starting from the specified root node
   * @dev Reverts with ConditionsNotMet() if the conditions are not met
   * @param conditionTree The hierarchical structure of conditions to evaluate
   * @param rootIndex The index of the root node to start evaluation from
   */
  function validateConditionTree(ConditionTree calldata conditionTree, uint256 rootIndex)
    external
    view;

  /**
   * @param condition the condition to be evaluated
   * @param additionalData the additional data to be used for evaluation
   * @return true if the condition is met, false otherwise
   */
  function evaluateCondition(Condition calldata condition, bytes calldata additionalData)
    external
    view
    returns (bool);
}

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

import {ActionData} from '../types/ActionData.sol';
import {FeeConfig} from '../types/FeeInfo.sol';
import {IntentData} from '../types/IntentData.sol';

interface IKSSmartIntentRouter {
  /// @notice Thrown when total partner fee is greater than 100%
  error InvalidFeeConfig();

  /// @notice Thrown when the caller is not the main address
  error NotMainAddress();

  /// @notice Thrown when the action is expired
  error ActionExpired();

  /// @notice Thrown when the intent has not been delegated
  error IntentNotDelegated();

  /// @notice Thrown when the intent has already been delegated
  error IntentDelegated();

  /// @notice Thrown when the intent has already been revoked
  error IntentRevoked();

  /// @notice Thrown when the signature is not from the main address
  error InvalidMainAddressSignature();

  /// @notice Thrown when the signature is not from the delegated key
  error InvalidDelegatedKeySignature();

  /// @notice Thrown when the signature is not from the guardian
  error InvalidGuardianSignature();

  /// @notice Thrown when the action contract and selector not found in intent
  error InvalidActionSelectorId(uint256 actionSelectorId);

  /// @notice Thrown when a nonce has already been used
  error NonceAlreadyUsed(bytes32 intentHash, uint256 nonce);

  /// @notice Thrown when collecting more than the intent allowance for ERC20
  error ERC20InsufficientIntentAllowance(
    bytes32 intentHash, address token, uint256 allowance, uint256 needed
  );

  /// @notice Emitted when the forwarder is updated
  event UpdateForwarder(address newForwarder);

  /// @notice Emitted when an intent is delegated
  event DelegateIntent(address indexed mainAddress, bytes delegatedKey, IntentData intentData);

  /// @notice Emitted when an intent is revoked
  event RevokeIntent(bytes32 indexed intentHash);

  /// @notice Emitted when an intent is executed
  event ExecuteIntent(bytes32 indexed intentHash, ActionData actionData, bytes actionResult);

  /// @notice Emitted when a nonce is consumed
  event UseNonce(bytes32 indexed intentHash, uint256 nonce);

  /// @notice Emitted when the fee is collected before execution
  event RecordVolumeAndFees(
    address indexed token,
    address indexed protocolRecipient,
    FeeConfig[] partnerFeeConfigs,
    uint256 protocolFeeAmount,
    uint256[] partnersFeeAmounts,
    bool beforeExecution,
    uint256 totalAmount
  );

  enum IntentStatus {
    NOT_DELEGATED,
    DELEGATED,
    REVOKED
  }

  /**
   * @notice Delegate the intent to the delegated key
   * @param intentData The data for the intent
   */
  function delegate(IntentData calldata intentData) external;

  /**
   * @notice Revoke the delegated intent
   * @param intentData The intent data to revoke
   */
  function revoke(IntentData calldata intentData) external;

  /**
   * @notice Execute the intent
   * @param intentData The data for the intent
   * @param dkSignature The signature of the delegated key
   * @param guardian The address of the guardian
   * @param gdSignature The signature of the guardian
   * @param actionData The data for the action
   */
  function execute(
    IntentData calldata intentData,
    bytes calldata dkSignature,
    address guardian,
    bytes calldata gdSignature,
    ActionData calldata actionData
  ) external;

  /**
   * @notice Execute the intent with the signed data and main address signature
   * @param intentData The data for the intent
   * @param maSignature The signature of the main address
   * @param dkSignature The signature of the delegated key
   * @param guardian The address of the guardian
   * @param gdSignature The signature of the guardian
   * @param actionData The data for the action
   */
  function executeWithSignedIntent(
    IntentData calldata intentData,
    bytes calldata maSignature,
    bytes calldata dkSignature,
    address guardian,
    bytes calldata gdSignature,
    ActionData calldata actionData
  ) external;

  /**
   * @notice Return the ERC20 allowance for a specific intent
   * @param intentHash The hash of the intent
   * @param token The address of the ERC20 token
   * @return allowance The allowance for the specified token
   */
  function erc20Allowances(bytes32 intentHash, address token)
    external
    view
    returns (uint256 allowance);

  /**
   * @notice Update the forwarder address
   * @param newForwarder The new forwarder address
   */
  function updateForwarder(address newForwarder) external;

  /// @notice mapping of nonces consumed by each intent, where a nonce is a single bit on the 256-bit bitmap
  /// @dev word is at most type(uint248).max
  function nonces(bytes32 intentHash, uint256 word) external view returns (uint256 bitmap);

  /// @notice Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
  function DOMAIN_SEPARATOR() external view returns (bytes32);
}

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

/// @notice Interface for the PoolManager
interface IPoolManager {
  /// @notice Called by external contracts to access granular pool state
  /// @param slot Key of slot to sload
  /// @return value The value of the slot as bytes32
  function extsload(bytes32 slot) external view returns (bytes32 value);

  /// @notice Called by external contracts to access granular pool state
  /// @param startSlot Key of slot to start sloading from
  /// @param nSlots Number of slots to load into return value
  /// @return values List of loaded values.
  function extsload(bytes32 startSlot, uint256 nSlots)
    external
    view
    returns (bytes32[] memory values);

  /// @notice Called by external contracts to access sparse pool state
  /// @param slots List of slots to SLOAD from.
  /// @return values List of loaded values.
  function extsload(bytes32[] calldata slots) external view returns (bytes32[] memory values);

  /// @notice Called by external contracts to access transient storage of the contract
  /// @param slot Key of slot to tload
  /// @return value The value of the slot as bytes32
  function exttload(bytes32 slot) external view returns (bytes32 value);

  /// @notice Called by external contracts to access sparse transient pool state
  /// @param slots List of slots to tload
  /// @return values List of loaded values
  function exttload(bytes32[] calldata slots) external view returns (bytes32[] memory values);

  /// @notice Thrown when a currency is not netted out after the contract is unlocked
  error CurrencyNotSettled();

  /// @notice Thrown when trying to interact with a non-initialized pool
  error PoolNotInitialized();

  /// @notice Thrown when unlock is called, but the contract is already unlocked
  error AlreadyUnlocked();

  /// @notice Thrown when a function is called that requires the contract to be unlocked, but it is not
  error ManagerLocked();

  /// @notice Pools are limited to type(int16).max tickSpacing in #initialize, to prevent overflow
  error TickSpacingTooLarge(int24 tickSpacing);

  /// @notice Pools must have a positive non-zero tickSpacing passed to #initialize
  error TickSpacingTooSmall(int24 tickSpacing);

  /// @notice PoolKey must have currencies where address(currency0) < address(currency1)
  error CurrenciesOutOfOrderOrEqual(address currency0, address currency1);

  /// @notice Thrown when a call to updateDynamicLPFee is made by an address that is not the hook,
  /// or on a pool that does not have a dynamic swap fee.
  error UnauthorizedDynamicLPFeeUpdate();

  /// @notice Thrown when trying to swap amount of 0
  error SwapAmountCannotBeZero();

  ///@notice Thrown when native currency is passed to a non native settlement
  error NonzeroNativeValue();

  /// @notice Thrown when `clear` is called with an amount that is not exactly equal to the open currency delta.
  error MustClearExactPositiveDelta();
}

File 30 of 43 : Types.sol
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

type PoolId is bytes32;

type BalanceDelta is int256;

type PositionInfo is uint256;

/// @notice Returns the key for identifying a pool
struct PoolKey {
  /// @notice The lower currency of the pool, sorted numerically
  address currency0;
  /// @notice The higher currency of the pool, sorted numerically
  address currency1;
  /// @notice The pool LP fee, capped at 1_000_000. If the highest bit is 1, the pool has a dynamic fee and must be exactly equal to 0x800000
  uint24 fee;
  /// @notice Ticks that involve positions must be a multiple of tick spacing
  int24 tickSpacing;
  /// @notice The hooks of the pool
  address hooks;
}

/// @notice Library to define different pool actions.
/// @dev These are suggested common commands, however additional commands should be defined as required
library Actions {
  // pool actions
  // liquidity actions
  uint256 constant INCREASE_LIQUIDITY = 0x00;
  uint256 constant DECREASE_LIQUIDITY = 0x01;
  uint256 constant MINT_POSITION = 0x02;
  uint256 constant BURN_POSITION = 0x03;
  uint256 constant INCREASE_LIQUIDITY_FROM_DELTAS = 0x04;
  uint256 constant MINT_POSITION_FROM_DELTAS = 0x05;

  // swapping
  uint256 constant SWAP_EXACT_IN_SINGLE = 0x06;
  uint256 constant SWAP_EXACT_IN = 0x07;
  uint256 constant SWAP_EXACT_OUT_SINGLE = 0x08;
  uint256 constant SWAP_EXACT_OUT = 0x09;
  // donate
  uint256 constant DONATE = 0x0a;

  // closing deltas on the pool manager
  // settling
  uint256 constant SETTLE = 0x0b;
  uint256 constant SETTLE_ALL = 0x0c;
  uint256 constant SETTLE_PAIR = 0x0d;
  // taking
  uint256 constant TAKE = 0x0e;
  uint256 constant TAKE_ALL = 0x0f;
  uint256 constant TAKE_PORTION = 0x10;
  uint256 constant TAKE_PAIR = 0x11;

  uint256 constant CLOSE_CURRENCY = 0x12;
  uint256 constant CLEAR_OR_TAKE = 0x13;
  uint256 constant SWEEP = 0x14;

  uint256 constant WRAP = 0x15;
  uint256 constant UNWRAP = 0x16;

  // minting/burning 6909s to close deltas
  uint256 constant MINT_6909 = 0x17;
  uint256 constant BURN_6909 = 0x18;
}

/// @title Commands
/// @notice Command Flags used to decode commands
library Commands {
  uint256 constant SWEEP = 0x04;
  uint256 constant V4_SWAP = 0x10;
}

struct ExactInputSingleParams {
  PoolKey poolKey;
  bool zeroForOne;
  uint128 amountIn;
  uint128 amountOutMinimum;
  bytes hookData;
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (utils/introspection/IERC165.sol)

pragma solidity >=0.4.16;

/**
 * @dev Interface of the ERC-165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[ERC].
 *
 * 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[ERC 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);
}

File 32 of 43 : BitMask.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

uint256 constant MASK_1_BIT = 0x1;
uint256 constant MASK_8_BITS = 0xff;
uint256 constant MASK_24_BITS = 0xffffff;
uint256 constant MASK_127_BITS = 0x7fffffffffffffffffffffffffffffff;
uint256 constant MASK_128_BITS = 0xffffffffffffffffffffffffffffffff;
uint160 constant MASK_160_BITS = 0x00ffffffffffffffffffffffffffffffffffffffff;

uint256 constant MASK_BYTES_4 = 0xffffffff00000000000000000000000000000000000000000000000000000000;

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

import {IKSGenericForwarder} from 'ks-common-sc/src/interfaces/IKSGenericForwarder.sol';
import {PermitHelper} from 'ks-common-sc/src/libraries/token/PermitHelper.sol';
import {TokenHelper} from 'ks-common-sc/src/libraries/token/TokenHelper.sol';

import {IKSSmartIntentRouter} from '../interfaces/IKSSmartIntentRouter.sol';
import {FeeConfig, FeeInfo, FeeInfoLibrary} from './FeeInfo.sol';

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

/**
 * @notice Data structure for ERC20 token
 * @param token The address of the ERC20 token
 * @param amount The amount of the ERC20 token
 * @param permitData The permit data for the ERC20 token
 */
struct ERC20Data {
  address token;
  uint256 amount;
  bytes permitData;
}

using ERC20DataLibrary for ERC20Data global;

library ERC20DataLibrary {
  using PermitHelper for address;
  using TokenHelper for address;

  bytes32 constant ERC20_DATA_TYPE_HASH =
    keccak256(abi.encodePacked('ERC20Data(address token,uint256 amount,bytes permitData)'));

  function hash(ERC20Data calldata self) internal pure returns (bytes32) {
    return keccak256(
      abi.encode(ERC20_DATA_TYPE_HASH, self.token, self.amount, keccak256(self.permitData))
    );
  }

  function collect(
    address token,
    uint256 amount,
    address mainAddress,
    address actionContract,
    uint256 fee,
    bool approvalFlag,
    IKSGenericForwarder forwarder,
    FeeConfig[] calldata partnerFeeConfigs,
    address protocolRecipient
  ) internal {
    if (address(forwarder) == address(0)) {
      token.safeTransferFrom(mainAddress, address(this), amount - fee);
      if (approvalFlag) {
        token.forceApprove(actionContract, type(uint256).max);
      }
    } else {
      token.safeTransferFrom(mainAddress, address(forwarder), amount - fee);
      if (approvalFlag) {
        _forwardApproveInf(forwarder, token, actionContract);
      }
    }

    (uint256 protocolFeeAmount, uint256[] memory partnersFeeAmounts) =
      FeeInfoLibrary.computeFees(partnerFeeConfigs, fee);

    uint256 protocolTotalReceived = protocolFeeAmount;
    for (uint256 i = 0; i < partnersFeeAmounts.length; i++) {
      if (partnerFeeConfigs[i].feeMode()) {
        protocolTotalReceived += partnersFeeAmounts[i];
      } else {
        token.safeTransferFrom(
          mainAddress, partnerFeeConfigs[i].partnerRecipient(), partnersFeeAmounts[i]
        );
      }
    }
    token.safeTransferFrom(mainAddress, protocolRecipient, protocolTotalReceived);

    emit IKSSmartIntentRouter.RecordVolumeAndFees(
      token,
      protocolRecipient,
      partnerFeeConfigs,
      protocolFeeAmount,
      partnersFeeAmounts,
      true,
      amount
    );
  }

  function collectFeeAfterExecution(
    address token,
    uint256 amount,
    uint256 fee,
    FeeConfig[] calldata partnerFeeConfigs,
    address protocolRecipient
  ) internal {
    (uint256 protocolFeeAmount, uint256[] memory partnersFeeAmounts) =
      FeeInfoLibrary.computeFees(partnerFeeConfigs, fee);

    uint256 protocolTotalReceived = protocolFeeAmount;
    for (uint256 i = 0; i < partnersFeeAmounts.length; i++) {
      if (partnerFeeConfigs[i].feeMode()) {
        protocolTotalReceived += partnersFeeAmounts[i];
      } else {
        token.safeTransfer(partnerFeeConfigs[i].partnerRecipient(), partnersFeeAmounts[i]);
      }
    }
    token.safeTransfer(protocolRecipient, protocolTotalReceived);

    emit IKSSmartIntentRouter.RecordVolumeAndFees(
      token,
      protocolRecipient,
      partnerFeeConfigs,
      protocolFeeAmount,
      partnersFeeAmounts,
      false,
      amount
    );
  }

  function _forwardApproveInf(IKSGenericForwarder forwarder, address token, address spender)
    internal
  {
    bytes memory approveCalldata = abi.encodeCall(IERC20.approve, (spender, type(uint256).max));
    try forwarder.forward(token, approveCalldata) {}
    catch {
      approveCalldata = abi.encodeCall(IERC20.approve, (spender, 0));
      forwarder.forward(token, approveCalldata);
      approveCalldata = abi.encodeCall(IERC20.approve, (spender, type(uint256).max));
      forwarder.forward(token, approveCalldata);
    }
  }
}

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

import {IKSGenericForwarder} from 'ks-common-sc/src/interfaces/IKSGenericForwarder.sol';
import {PermitHelper} from 'ks-common-sc/src/libraries/token/PermitHelper.sol';

import {IERC721} from 'openzeppelin-contracts/contracts/interfaces/IERC721.sol';

/**
 * @notice Data structure for ERC721 token
 * @param token The address of the ERC721 token
 * @param tokenId The ID of the ERC721 token
 * @param permitData The permit data for the ERC721 token
 */
struct ERC721Data {
  address token;
  uint256 tokenId;
  bytes permitData;
}

using ERC721DataLibrary for ERC721Data global;

library ERC721DataLibrary {
  using PermitHelper for address;

  bytes32 constant ERC721_DATA_TYPE_HASH =
    keccak256(abi.encodePacked('ERC721Data(address token,uint256 tokenId,bytes permitData)'));

  function hash(ERC721Data calldata self) internal pure returns (bytes32) {
    return keccak256(
      abi.encode(ERC721_DATA_TYPE_HASH, self.token, self.tokenId, keccak256(self.permitData))
    );
  }

  function collect(
    address token,
    uint256 tokenId,
    address mainAddress,
    address actionContract,
    IKSGenericForwarder forwarder,
    bool approvalFlag
  ) internal {
    if (address(forwarder) == address(0)) {
      IERC721(token).safeTransferFrom(mainAddress, address(this), tokenId);
      if (approvalFlag) {
        IERC721(token).approve(actionContract, tokenId);
      }
    } else {
      IERC721(token).safeTransferFrom(mainAddress, address(forwarder), tokenId);
      if (approvalFlag) {
        bytes memory approveCalldata = abi.encodeCall(IERC721.approve, (actionContract, tokenId));
        forwarder.forward(token, approveCalldata);
      }
    }
  }
}

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

interface IKSGenericForwarder {
  function forward(address target, bytes calldata data) external payable returns (bytes memory);

  function forwardValue(address target, bytes calldata data, uint256 value)
    external
    payable
    returns (bytes memory);

  function forwardBatch(address[] calldata targets, bytes[] calldata data)
    external
    returns (bytes[] memory);

  function forwardBatchValue(
    address[] calldata targets,
    bytes[] calldata data,
    uint256[] calldata value
  ) external payable returns (bytes[] memory);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import {IAllowanceTransfer} from '../../interfaces/IAllowanceTransfer.sol';
import {IDaiLikePermit} from '../../interfaces/IDaiLikePermit.sol';
import {IERC721Permit_v3} from '../../interfaces/IERC721Permit_v3.sol';
import {IERC721Permit_v4} from '../../interfaces/IERC721Permit_v4.sol';
import {CalldataDecoder} from '../calldata/CalldataDecoder.sol';

import {
  IERC20Permit
} from 'openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Permit.sol';

library PermitHelper {
  using CalldataDecoder for bytes;

  function callERC20Permit(address token, address owner, bytes calldata permitData)
    internal
    returns (bool)
  {
    if (permitData.length == 32 * 5) {
      uint256 value = permitData.decodeUint256(0);
      uint256 deadline = permitData.decodeUint256(1);
      uint8 v = uint8(permitData.decodeUint256(2));
      bytes32 r = permitData.decodeBytes32(3);
      bytes32 s = permitData.decodeBytes32(4);

      try IERC20Permit(token).permit(owner, address(this), value, deadline, v, r, s) {
        return true;
      } catch {}
    } else if (permitData.length == 32 * 6) {
      uint256 nonce = permitData.decodeUint256(0);
      uint256 expiry = permitData.decodeUint256(1);
      bool allowed = permitData.decodeBool(2);
      uint8 v = uint8(permitData.decodeUint256(3));
      bytes32 r = permitData.decodeBytes32(4);
      bytes32 s = permitData.decodeBytes32(5);

      try IDaiLikePermit(token).permit(owner, address(this), nonce, expiry, allowed, v, r, s) {
        return true;
      } catch {}
    }
  }

  function callERC721Permit(address token, uint256 tokenId, bytes calldata permitData)
    internal
    returns (bool)
  {
    if (permitData.length == 32 * 4) {
      uint256 deadline = permitData.decodeUint256(0);
      uint8 v = uint8(permitData.decodeUint256(1));
      bytes32 r = permitData.decodeBytes32(2);
      bytes32 s = permitData.decodeBytes32(3);

      try IERC721Permit_v3(token).permit(address(this), tokenId, deadline, v, r, s) {
        return true;
      } catch {}
    } else if (permitData.length == 32 * 7) {
      uint256 deadline = permitData.decodeUint256(0);
      uint256 nonce = permitData.decodeUint256(1);
      bytes calldata signature = permitData.decodeBytes(2);

      try IERC721Permit_v4(token).permit(address(this), tokenId, deadline, nonce, signature) {
        return true;
      } catch {}
    }
  }

  function callPermit2(IAllowanceTransfer permit2, address owner, bytes calldata permit2Data)
    internal
    returns (bool)
  {
    IAllowanceTransfer.PermitBatch calldata permitBatch;
    assembly ('memory-safe') {
      permitBatch := add(permit2Data.offset, calldataload(permit2Data.offset))
    }
    bytes calldata signature = permit2Data.decodeBytes(1);

    try permit2.permit(owner, permitBatch, signature) {
      return true;
    } catch {}
  }
}

File 37 of 43 : IERC721.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC721.sol)

pragma solidity >=0.6.2;

import {IERC721} from "../token/ERC721/IERC721.sol";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/// @notice Interface for DAI-style permits
interface IDaiLikePermit {
  /// @notice Permit a spender to a given amount of the holder's token via the holder's EIP-712 signature
  /// @dev May fail if the holder's nonce was invalidated in-flight by invalidateNonce
  /// @param holder The holder of the tokens being approved
  /// @param spender The address permissioned on the allowed tokens
  /// @param nonce The nonce of the holder
  /// @param expiry The expiry of the permit
  /// @param allowed Whether the permit is allowed
  /// @param v The v component of the signature
  /// @param r The r component of the signature
  /// @param s The s component of the signature
  function permit(
    address holder,
    address spender,
    uint256 nonce,
    uint256 expiry,
    bool allowed,
    uint8 v,
    bytes32 r,
    bytes32 s
  ) external;

  function PERMIT_TYPEHASH() external pure returns (bytes32);
}

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

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

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

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

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

/// @title IERC721Permit_v4
/// @notice Interface for the ERC721Permit_v4 contract
interface IERC721Permit_v4 {
  error SignatureDeadlineExpired();
  error NoSelfPermit();
  error Unauthorized();

  /// @notice Approve of a specific token ID for spending by spender via signature
  /// @param spender The account that is being approved
  /// @param tokenId The ID of the token that is being approved for spending
  /// @param deadline The deadline timestamp by which the call must be mined for the approve to work
  /// @param nonce a unique value, for an owner, to prevent replay attacks; an unordered nonce where the top 248 bits correspond to a word and the bottom 8 bits calculate the bit position of the word
  /// @param signature Concatenated data from a valid secp256k1 signature from the holder, i.e. abi.encodePacked(r, s, v)
  /// @dev payable so it can be multicalled with NATIVE related actions
  function permit(
    address spender,
    uint256 tokenId,
    uint256 deadline,
    uint256 nonce,
    bytes calldata signature
  ) external payable;

  /// @notice Set an operator with full permission to an owner's tokens via signature
  /// @param owner The address that is setting the operator
  /// @param operator The address that will be set as an operator for the owner
  /// @param approved The permission to set on the operator
  /// @param deadline The deadline timestamp by which the call must be mined for the approve to work
  /// @param nonce a unique value, for an owner, to prevent replay attacks; an unordered nonce where the top 248 bits correspond to a word and the bottom 8 bits calculate the bit position of the word
  /// @param signature Concatenated data from a valid secp256k1 signature from the holder, i.e. abi.encodePacked(r, s, v)
  /// @dev payable so it can be multicalled with NATIVE related actions
  function permitForAll(
    address owner,
    address operator,
    bool approved,
    uint256 deadline,
    uint256 nonce,
    bytes calldata signature
  ) external payable;
}

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

/// @title Library for abi decoding in calldata
library CalldataDecoder {
  error SliceOutOfBounds();

  /// @notice mask used for offsets and lengths to ensure no overflow
  /// @dev no sane abi encoding will pass in an offset or length greater than type(uint32).max
  ///      (note that this does deviate from standard solidity behavior and offsets/lengths will
  ///      be interpreted as mod type(uint32).max which will only impact malicious/buggy callers)
  uint256 internal constant OFFSET_OR_LENGTH_MASK = 0xffffffff;
  uint256 internal constant OFFSET_OR_LENGTH_MASK_AND_WORD_ALIGN = 0xffffffe0;

  /// @notice equivalent to SliceOutOfBounds.selector, stored in least-significant bits
  uint256 internal constant SLICE_ERROR_SELECTOR = 0x3b99b53d;

  function decodeAddress(bytes calldata _bytes) internal pure returns (address value) {
    assembly ('memory-safe') {
      value := calldataload(_bytes.offset)
    }
  }

  function decodeAddress(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (address value)
  {
    assembly ('memory-safe') {
      value := calldataload(add(_bytes.offset, shl(5, _arg)))
    }
  }

  function decodeUint256(bytes calldata _bytes) internal pure returns (uint256 value) {
    assembly ('memory-safe') {
      value := calldataload(_bytes.offset)
    }
  }

  function decodeUint256(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (uint256 value)
  {
    assembly ('memory-safe') {
      value := calldataload(add(_bytes.offset, shl(5, _arg)))
    }
  }

  function decodeBool(bytes calldata _bytes) internal pure returns (bool value) {
    assembly ('memory-safe') {
      value := calldataload(_bytes.offset)
    }
  }

  function decodeBool(bytes calldata _bytes, uint256 _arg) internal pure returns (bool value) {
    assembly ('memory-safe') {
      value := calldataload(add(_bytes.offset, shl(5, _arg)))
    }
  }

  function decodeBytes32(bytes calldata _bytes) internal pure returns (bytes32 value) {
    assembly ('memory-safe') {
      value := calldataload(_bytes.offset)
    }
  }

  function decodeBytes32(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (bytes32 value)
  {
    assembly ('memory-safe') {
      value := calldataload(add(_bytes.offset, shl(5, _arg)))
    }
  }

  /// @notice Decode the `_arg`-th element in `_bytes` as a dynamic array
  /// @dev The decoding of `length` and `offset` is universal,
  /// whereas the type declaration of `res` instructs the compiler how to read it.
  /// @param _bytes The input bytes string to slice
  /// @param _arg The index of the argument to extract
  /// @return length Length of the array
  /// @return offset Pointer to the data part of the array
  function decodeLengthOffset(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (uint256 length, uint256 offset)
  {
    assembly ('memory-safe') {
      // The offset of the `_arg`-th element is `32 * arg`, which stores the offset of the length pointer.
      // shl(5, x) is equivalent to mul(32, x)
      let lengthPtr := add(_bytes.offset, calldataload(add(_bytes.offset, shl(5, _arg))))
      length := calldataload(lengthPtr)
      offset := add(lengthPtr, 0x20)

      // if the provided bytes string isnt as long as the encoding says, revert
      if lt(add(_bytes.length, _bytes.offset), add(length, offset)) {
        mstore(0, SLICE_ERROR_SELECTOR)
        revert(0x1c, 4)
      }
    }
  }

  /// @notice Decode the `_arg`-th element in `_bytes` as `bytes`
  /// @param _bytes The input bytes string to extract a bytes string from
  /// @param _arg The index of the argument to extract
  function decodeBytes(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (bytes calldata res)
  {
    (uint256 length, uint256 offset) = decodeLengthOffset(_bytes, _arg);
    assembly ('memory-safe') {
      res.length := length
      res.offset := offset
    }
  }

  /// @notice Decode the `_arg`-th element in `_bytes` as `uint256[]`
  /// @param _bytes The input bytes string to extract a uint256 array from
  /// @param _arg The index of the argument to extract
  function decodeUint256Array(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (uint256[] calldata res)
  {
    (uint256 length, uint256 offset) = decodeLengthOffset(_bytes, _arg);
    assembly ('memory-safe') {
      res.length := length
      res.offset := offset
    }
  }

  /// @notice Decode the `_arg`-th element in `_bytes` as `address[]`
  /// @param _bytes The input bytes string to extract an address array from
  /// @param _arg The index of the argument to extract
  function decodeAddressArray(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (address[] calldata res)
  {
    (uint256 length, uint256 offset) = decodeLengthOffset(_bytes, _arg);
    assembly ('memory-safe') {
      res.length := length
      res.offset := offset
    }
  }

  /// @notice Decode the `_arg`-th element in `_bytes` as `bytes32[]`
  /// @param _bytes The input bytes string to extract a bytes32 array from
  /// @param _arg The index of the argument to extract
  function decodeBytes32Array(bytes calldata _bytes, uint256 _arg)
    internal
    pure
    returns (bytes32[] calldata res)
  {
    (uint256 length, uint256 offset) = decodeLengthOffset(_bytes, _arg);
    assembly ('memory-safe') {
      res.length := length
      res.offset := offset
    }
  }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (token/ERC20/extensions/IERC20Permit.sol)

pragma solidity >=0.4.16;

/**
 * @dev Interface of the ERC-20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[ERC-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC-20 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.
 *
 * ==== Security Considerations
 *
 * There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
 * expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
 * considered as an intention to spend the allowance in any specific way. The second is that because permits have
 * built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
 * take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
 * generally recommended is:
 *
 * ```solidity
 * function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
 *     try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
 *     doThing(..., value);
 * }
 *
 * function doThing(..., uint256 value) public {
 *     token.safeTransferFrom(msg.sender, address(this), value);
 *     ...
 * }
 * ```
 *
 * Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
 * `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
 * {SafeERC20-safeTransferFrom}).
 *
 * Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
 * contracts should have entry points that don't rely on permit.
 */
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 applies 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].
     *
     * CAUTION: See Security Considerations above.
     */
    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);
}

Settings
{
  "remappings": [
    "forge-std/=lib/ks-common-sc/lib/forge-std/src/",
    "ks-common-sc/=lib/ks-common-sc/",
    "@openzeppelin/contracts/=lib/ks-common-sc/lib/openzeppelin-contracts/contracts/",
    "erc4626-tests/=lib/ks-common-sc/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "halmos-cheatcodes/=lib/ks-common-sc/lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/ks-common-sc/lib/openzeppelin-contracts/"
  ],
  "optimizer": {
    "enabled": true,
    "runs": 44444444
  },
  "metadata": {
    "useLiteralContent": false,
    "bytecodeHash": "ipfs",
    "appendCBOR": true
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "evmVersion": "prague",
  "viaIR": true
}

Contract Security Audit

Contract ABI

API
[{"inputs":[{"internalType":"address","name":"_weth","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ConditionsNotMet","type":"error"},{"inputs":[],"name":"ExceedMaxFeesPercent","type":"error"},{"inputs":[],"name":"InvalidERC721Data","type":"error"},{"inputs":[],"name":"InvalidLiquidity","type":"error"},{"inputs":[],"name":"InvalidNodeIndex","type":"error"},{"inputs":[],"name":"InvalidOwner","type":"error"},{"inputs":[],"name":"InvalidTokenData","type":"error"},{"inputs":[],"name":"NotEnoughFeesReceived","type":"error"},{"inputs":[],"name":"NotEnoughOutputAmount","type":"error"},{"inputs":[],"name":"WrongConditionType","type":"error"},{"inputs":[],"name":"WrongOperationType","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"nftAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"nftId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"liquidity","type":"uint256"}],"name":"LiquidityRemoved","type":"event"},{"inputs":[],"name":"PRECISION","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PRICE_BASED","outputs":[{"internalType":"ConditionType","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"Q128","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"Q96","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"TIME_BASED","outputs":[{"internalType":"ConditionType","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"YIELD_BASED","outputs":[{"internalType":"ConditionType","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"},{"components":[{"components":[{"internalType":"address","name":"mainAddress","type":"address"},{"internalType":"address","name":"signatureVerifier","type":"address"},{"internalType":"bytes","name":"delegatedKey","type":"bytes"},{"internalType":"address[]","name":"actionContracts","type":"address[]"},{"internalType":"bytes4[]","name":"actionSelectors","type":"bytes4[]"},{"internalType":"address","name":"hook","type":"address"},{"internalType":"bytes","name":"hookIntentData","type":"bytes"}],"internalType":"struct IntentCoreData","name":"coreData","type":"tuple"},{"components":[{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes","name":"permitData","type":"bytes"}],"internalType":"struct ERC20Data[]","name":"erc20Data","type":"tuple[]"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"permitData","type":"bytes"}],"internalType":"struct ERC721Data[]","name":"erc721Data","type":"tuple[]"}],"internalType":"struct TokenData","name":"tokenData","type":"tuple"},{"internalType":"bytes","name":"extraData","type":"bytes"}],"internalType":"struct IntentData","name":"intentData","type":"tuple"},{"internalType":"bytes","name":"beforeExecutionData","type":"bytes"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"afterExecution","outputs":[{"internalType":"address[]","name":"tokens","type":"address[]"},{"internalType":"uint256[]","name":"fees","type":"uint256[]"},{"internalType":"uint256[]","name":"amounts","type":"uint256[]"},{"internalType":"address","name":"recipient","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"},{"components":[{"components":[{"internalType":"address","name":"mainAddress","type":"address"},{"internalType":"address","name":"signatureVerifier","type":"address"},{"internalType":"bytes","name":"delegatedKey","type":"bytes"},{"internalType":"address[]","name":"actionContracts","type":"address[]"},{"internalType":"bytes4[]","name":"actionSelectors","type":"bytes4[]"},{"internalType":"address","name":"hook","type":"address"},{"internalType":"bytes","name":"hookIntentData","type":"bytes"}],"internalType":"struct IntentCoreData","name":"coreData","type":"tuple"},{"components":[{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes","name":"permitData","type":"bytes"}],"internalType":"struct ERC20Data[]","name":"erc20Data","type":"tuple[]"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"permitData","type":"bytes"}],"internalType":"struct ERC721Data[]","name":"erc721Data","type":"tuple[]"}],"internalType":"struct TokenData","name":"tokenData","type":"tuple"},{"internalType":"bytes","name":"extraData","type":"bytes"}],"internalType":"struct IntentData","name":"intentData","type":"tuple"},{"components":[{"internalType":"uint256[]","name":"erc20Ids","type":"uint256[]"},{"internalType":"uint256[]","name":"erc20Amounts","type":"uint256[]"},{"internalType":"uint256[]","name":"erc721Ids","type":"uint256[]"},{"components":[{"internalType":"address","name":"protocolRecipient","type":"address"},{"internalType":"FeeConfig[][]","name":"partnerFeeConfigs","type":"uint256[][]"}],"internalType":"struct FeeInfo","name":"feeInfo","type":"tuple"},{"internalType":"uint256","name":"approvalFlags","type":"uint256"},{"internalType":"uint256","name":"actionSelectorId","type":"uint256"},{"internalType":"bytes","name":"actionCalldata","type":"bytes"},{"internalType":"bytes","name":"hookActionData","type":"bytes"},{"internalType":"bytes","name":"extraData","type":"bytes"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"nonce","type":"uint256"}],"internalType":"struct ActionData","name":"actionData","type":"tuple"}],"name":"beforeExecution","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"bytes","name":"beforeExecutionData","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"ConditionType","name":"conditionType","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct Condition","name":"condition","type":"tuple"},{"internalType":"bytes","name":"additionalData","type":"bytes"}],"name":"evaluateCondition","outputs":[{"internalType":"bool","name":"isSatisfied","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"components":[{"internalType":"enum OperationType","name":"operationType","type":"uint8"},{"components":[{"internalType":"ConditionType","name":"conditionType","type":"bytes32"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct Condition","name":"condition","type":"tuple"},{"internalType":"uint256[]","name":"childrenIndexes","type":"uint256[]"}],"internalType":"struct Node[]","name":"nodes","type":"tuple[]"},{"internalType":"bytes[]","name":"additionalData","type":"bytes[]"}],"internalType":"struct ConditionTree","name":"tree","type":"tuple"},{"internalType":"uint256","name":"curIndex","type":"uint256"}],"name":"validateConditionTree","outputs":[],"stateMutability":"view","type":"function"}]

60a034606d57601f61346638819003918201601f19168301916001600160401b03831184841017607157808492602094604052833981010312606d57516001600160a01b0381168103606d576080526040516133e0908161008682396080518181816101dc0152612d760152f35b5f80fd5b634e487b7160e01b5f52604160045260245ffdfe60e06040526004361015610011575f80fd5b5f5f3560e01c80630e63b5101461219057806327a83116146121385780633b15ba39146120925780633f0195c6146119b1578063407924651461196c578063a55b716a1461023c578063aaf5eb6814610200578063ad5c464814610191578063b4b15f7114610147578063eeb8594a146100ee5763f8f3ad8d14610093575f80fd5b346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb5760206040517fab478f0811200e2fe5d52563d5a2104400f89e38425964dd6083cc49d15388d28152f35b80fd5b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb5760206040517fa4146dd1eb46e4bd1d6977ffec765cfcc6f8c6d8ecb3bf903f3af4131e31fd018152f35b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb5760206040517001000000000000000000000000000000008152f35b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb57602060405173ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152f35b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb576020604051620f42408152f35b503461111d5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5767ffffffffffffffff6024351161111d5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc6024353603011261111d5760443567ffffffffffffffff811161111d576101607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc823603011261111d576102f760048201806123e9565b905061194457600161030f60448301836004016123e9565b9050036119445760405160a05261032760a05161280e565b5f60a051525f602060a051015260405160e0810181811067ffffffffffffffff821117611456576040525f81525f60208201525f60408201525f60608201525f60808201525f60a0820152604051610100810181811067ffffffffffffffff821117611456576040525f81525f60208201525f60408201525f60c052604060c05260c051516103b860c05182612862565b60c051368237606082015260c0518051906103d39082612862565b60c051368237608082015260c0518051906103ee9082612862565b60c05136823760a082015260c0518051906104099082612862565b60c05136823760c082015260c0515060c0515161042860c05182612862565b60c05136823760e082015260c0820152604060a051015260c051519060a0820182811067ffffffffffffffff8211176114565760c051525f825260c0515060c0515161047660c05182612862565b60c051368237602083015260c051805181840193916104959082612862565b60c051368237835260c05180516060830194916104b29082612862565b60c051368237845260c0518051906104ca9082612862565b60c051368237608083015260a0516060018290526104fa6104f0600460243501806127db565b60c08101906124df565b509361050c60e48501856004016124df565b50916105e06105db604060a05101519460c0515060c0515161053060c05182612862565b60c0519036903760c051518135916020906fffffffffffffffffffffffffffffffff60a08201356105608561282a565b8060801c8552168284015260a060c08a015101519288526080810135151560c0518a01526060810135828a015260c051810135828401520135905280865273ffffffffffffffffffffffffffffffffffffffff6105c260808b358c01016127ba565b1660608701526105d589358a01806123e9565b90612562565b6127ba565b73ffffffffffffffffffffffffffffffffffffffff60c08501519116905261061c61061460208835890101883589016123e9565b855191612562565b35602060c0850151015233845260c051516106368161282a565b6fffffffffffffffffffffffffffffffff61068361067b60608a358b010161066c610664828d8035016123e9565b8a5191612562565b3560801c85528a358b016123e9565b875191612562565b3516602082015281528151518151511015918261192e575b5050156119065773ffffffffffffffffffffffffffffffffffffffff60c08201515116905f60a060c051516106cf81612846565b8281528260208201528260c0518201528260608201528260808201520152602060c082015101519160c05151927f99fbab88000000000000000000000000000000000000000000000000000000008452600484015261018083602481845afa8015610fd8575f935f905f905f5f915f95611801575b509160049593916fffffffffffffffffffffffffffffffff6020969460c08a0151926060840151966080850151908a82015252169060c051015260020b8483015260020b905260c085208260a051015260c05151928380927f406793610000000000000000000000000000000000000000000000000000000082525afa8015610fd8575f9061179e575b73ffffffffffffffffffffffffffffffffffffffff9150168060a051526080602060a0510151602460c05151809481937fc815641c00000000000000000000000000000000000000000000000000000000835260048301525afa8015610fd8575f905f90611710575b73ffffffffffffffffffffffffffffffffffffffff925060020b60a084015216608082015261089c73ffffffffffffffffffffffffffffffffffffffff602060c05151946108848661282a565b8261089181835116612c6c565b168652015116612c6c565b9073ffffffffffffffffffffffffffffffffffffffff602084019216825282608085015260c05101516116b3575b505060206108dc608083015133612c38565b919092015190602082015252604060a051015160c0810151906060820180515160020b90602060a084015160020b9151015160020b92602081019081516115a2575b505073ffffffffffffffffffffffffffffffffffffffff60a051511690602060a05101519260c051517f9ec538c800000000000000000000000000000000000000000000000000000000815284600482015260c05181602481875afa928315610fd8575f915f94611565575b5060c051517f5aa208a400000000000000000000000000000000000000000000000000000000815260048101879052600284900b6024820152608081604481895afa958615610fd857610a2c9789925f9861153f575b50906080929160c05151809a819482937f5aa208a4000000000000000000000000000000000000000000000000000000008452600484019092916020906040830194835260020b910152565b03915afa918215610fd857602097610aa7975f9461150c575b5092939192889284126114f757606060c051880151970151935b12156114e257606060c051850151940151935b60808a0196875151920303039460e0610a9260c0518b0197885190612990565b99019889515251015192030303905190612990565b9151015260c0604060a0510151015190602073ffffffffffffffffffffffffffffffffffffffff83511692015191602480350191610af7610aed846024356004016124ac565b60208101906123e9565b90610b0860448401846004016123e9565b9190911561148357610b296105db610aed92610b4595610b3595359161246c565b956024356004016124ac565b91909260448101906004016123e9565b156114835773ffffffffffffffffffffffffffffffffffffffff92602092610b6e92359161246c565b013592161491826114d8575b5050156114b05760c051610b9491803501908101906123e9565b90604060a0510151519182101561148357610bb49160051b8101906123e9565b60a060c06040825101510151015151602060a060c060408251015101510151015173ffffffffffffffffffffffffffffffffffffffff6080604060a05101510151169260c05151608052610c0960805161282a565b606060805152606060206080510152610c21816128a3565b610c2f60c051519182612862565b81815260208101368360051b88011161111d5786905b8360051b8801821061127a57505060805152610c60816128a3565b610c6e60c051519182612862565b8181527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0610c9b836128a3565b015f5b818110611269575050602060805101525f5b8181106111215786303b1561111d5760c051517f0e63b51000000000000000000000000000000000000000000000000000000000815260c05160048201528060848101608051519060c0516044840152815180915260a4830190602060a48260051b8601019301915f905b828210611033575050505060206080510151907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbc838203016064840152815180825260208201916020808360051b8301019401925f915b838310610fe45788875f81808a8360248301520381305afa8015610fd857610fc5575b50604060a0510151606060a051015160c080515192805160208501526020810151825185015281518101511515606085015273ffffffffffffffffffffffffffffffffffffffff606082015116608085015273ffffffffffffffffffffffffffffffffffffffff60808201511660a085015260a081015160020b82850152015173ffffffffffffffffffffffffffffffffffffffff81511660e0840152602081015161010084015260c0518101516101208401526060810151610140840185905b60028210610fac5750505090610eaf60e083610e7c6080809601516101808801906128ec565b610e8f60a08201516101c08801906128ec565b610ea260c08201516102008801906128ec565b01516102408501906128ec565b73ffffffffffffffffffffffffffffffffffffffff815116610280840152610ee060208201516102a08501906128ec565b610ef460c0518201516102e08501906128ec565b610f0760608201516103208501906128ec565b01518261036083015b60028210610f80575050506103808152610f2c6103a082612862565b60c051519060c05182019260c051835260605180945260608301936080915b818110610f6a578480610f668887838203602085015261229f565b0390f35b8251865260209586019590920191600101610f4b565b60208060019273ffffffffffffffffffffffffffffffffffffffff865116815201930191019091610f10565b602080600192855160020b815201930191019091610e56565b610fd191505f90612862565b5f81610d95565b60c051513d5f823e3d90fd5b919395509193602080611021837fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08660019603018752895161229f565b97019301930190928695949293610d72565b91939092947fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5c9082030182528451805160028110156110f057825261109860208083015160608286015280516060860152015160c051608085015260a084019061229f565b9060c0510151918082039060c0510152602080835192838152019201905f905b8082106110d8575050506020806001929601920192018594939192610d1b565b909192602080600192865181520194019201906110b8565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602160045260245ffd5b5f80fd5b8061113d611132600193858a61246c565b60c0518101906123e9565b1580159150611232575b61122d577fab478f0811200e2fe5d52563d5a2104400f89e38425964dd6083cc49d15388d261118461117a83868b61246c565b60208101906124ac565b35036111d4576111cc8160c051518760208201528660c051820152886060820152606081526111b4608082612862565b60206080510151906111c683836128d8565b526128d8565b505b01610cb0565b7faa4ed6eba3df66352fbab5103a7bde08f635eb0728a4a4889f8ef6f7e34b4c2a61120361117a83868b61246c565b35036111ce576112278160c05151886020820152602081526111b460c05182612862565b506111ce565b6111ce565b507fa4146dd1eb46e4bd1d6977ffec765cfcc6f8c6d8ecb3bf903f3af4131e31fd0161126261117a83868b61246c565b3514611147565b806060602080938601015201610c9e565b813567ffffffffffffffff811161111d576060818a0136031261111d5760c051519081606081011067ffffffffffffffff606084011117611456576060820160c05152808a0135600281101561111d57825267ffffffffffffffff6020828c0101351161111d5760c0518a820160208101350136031261111d5760c051516113018161282a565b6020828c01818101350180358352013567ffffffffffffffff811161111d578b83016020810135010136601f8201121561111d57803567ffffffffffffffff81116114565760c051519161137d60207fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f8501160184612862565b818352366020838301011161111d57815f92602080930183860137830101526020820152602083015260c051818b0101359067ffffffffffffffff821161111d5736818c018301601f01121561111d5781818c010135906113dd826128a3565b926113ec60c051519485612862565b82845260208401918d6020369184848860051b92010101011161111d578d926020838386010101905b60208660051b858588010101018210611446575050505050509160209291839260c051820152815201910190610c45565b8135815260209182019101611415565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b7f53093dd4000000000000000000000000000000000000000000000000000000005f5260045ffd5b1490505f80610b7a565b606060c0518501518203940151850393610a72565b606060c0518801518303970151860393610a5f565b899394506115319060803d608011611538575b6115298183612862565b810190613225565b9392610a45565b503d61151f565b6080939291985061155c90843d8611611538576115298183612862565b979091926109e0565b9150925060c0513d60c0511161159b575b6115808183612862565b8160c051918101031261111d5760208151910151925f61098a565b503d611576565b6115ab84612e1a565b906fffffffffffffffffffffffffffffffff73ffffffffffffffffffffffffffffffffffffffff60806115dd89612e1a565b93015116935116908083905f905f9573ffffffffffffffffffffffffffffffffffffffff821673ffffffffffffffffffffffffffffffffffffffff8216116116a8575b505073ffffffffffffffffffffffffffffffffffffffff82168611611660575061164b9394506132fb565b905b60c0860151906020820152525f8061091e565b90935073ffffffffffffffffffffffffffffffffffffffff821685101561169d575092611692826116989495836132fb565b936132ae565b61164d565b9350611698926132ae565b935091505f80611620565b6116fe73ffffffffffffffffffffffffffffffffffffffff809260c05150816116f48160c05151976116e760c0518a612862565b60c051368a375116612d74565b1685525116612d74565b16602082015260808201525f806108ca565b50506080813d608011611796575b8161172b60809383612862565b8101031261111d5780519073ffffffffffffffffffffffffffffffffffffffff8216820361111d5773ffffffffffffffffffffffffffffffffffffffff9161178f606061177a60208501612965565b9361178860c0518201612955565b5001612955565b5090610837565b3d915061171e565b506020813d6020116117f9575b816117b860209383612862565b8101031261111d575173ffffffffffffffffffffffffffffffffffffffff8116810361111d5773ffffffffffffffffffffffffffffffffffffffff906107ce565b3d91506117ab565b97505050505050826101803d81116118ff575b61181e8183612862565b810103610180811261111d5760c01361111d57602060049160c0515161184381612846565b61184c86612927565b8152611859838701612927565b8382015261186a60c0518701612927565b60c05182015261187c60608701612927565b606082015261188d60808701612955565b608082015260a086015160a08201526118a860c08701612965565b956118b560e08201612965565b906118c36101008201612973565b906fffffffffffffffffffffffffffffffff610120820151946118ef6101606101408501519401612927565b5099959791965092939250610744565b503d611814565b7f9c373586000000000000000000000000000000000000000000000000000000005f5260045ffd5b5160209081015191510151101590505f8061069b565b7fafb5d0c3000000000000000000000000000000000000000000000000000000005f5260045ffd5b3461111d575f7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760206040516c010000000000000000000000008152f35b3461111d5760807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760243567ffffffffffffffff811161111d5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc82600401923603011261111d5760443567ffffffffffffffff811161111d57611a4290369060040161223e565b5060643567ffffffffffffffff811161111d57611a6390369060040161223e565b505060c0810173ffffffffffffffffffffffffffffffffffffffff611a87826127ba565b6020611a9b6105db60e087013597806127db565b916024604051809581937f6352211e0000000000000000000000000000000000000000000000000000000083528a6004840152165afa91821561200c575f9261203f575b5073ffffffffffffffffffffffffffffffffffffffff80911691160361201757602492602073ffffffffffffffffffffffffffffffffffffffff611b22846127ba565b16604051958680927f1efeed330000000000000000000000000000000000000000000000000000000082528560048301525afa93841561200c575f94611fd0575b506020830135936fffffffffffffffffffffffffffffffff611b8a86610100870135612948565b911603611fa857611b9e61026084016127ba565b610340840193604091825190611bb48483612862565b81610380840136811161111d5788915b818310611f7b57505050611bdf611bed9392611bfa92612c38565b939061028084013590612948565b926102a083013590612948565b93610220820135918284101580611f6c575b15611f4457611c1b8385612948565b98610240820135611c2c8189612948565b94606098885196611c3d8b89612862565b600288527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08b019d8e3660208b0137610300870135611c7b916126d4565b620f42409004611c8a896128bb565b52610320860135611c9a916126d4565b620f42409004611ca9886128c8565b52885197611cb78b8a612862565b600289528d3660208b0137611ccb886128bb565b51611cd591612948565b611cde896128bb565b52611ce8876128c8565b51611cf291612948565b611cfb886128c8565b526102c0840135620f42400390620f42408211611f1757620f4240611d28611d2f936101e08801356126d4565b04906126c7565b906102e0840135620f42400390620f42408211611f1757620f4240611d28611d5c936102008801356126d4565b90611d66876128bb565b5110159081611f03575b5015611edb5786809287519a611d86838d612862565b60028c5260208c019c368e37611d9b906127ba565b611da48c6128bb565b9073ffffffffffffffffffffffffffffffffffffffff1690526103608101611dcb906127ba565b611dd48c6128c8565b9073ffffffffffffffffffffffffffffffffffffffff16905201611df7906127ba565b97611e01906127ba565b9286519373ffffffffffffffffffffffffffffffffffffffff1684526020840152858301527f96cd817c6329656790ef8fba7675405193677d39619571282f5e21f3a98cd05991a18251956080870190608088525180915260a0870197905f5b818110611eaf5750505091611ea591611e988873ffffffffffffffffffffffffffffffffffffffff9589809b0360208b015261226c565b918783039088015261226c565b9216908301520390f35b825173ffffffffffffffffffffffffffffffffffffffff168a526020998a019990920191600101611e61565b7f215ff8e5000000000000000000000000000000000000000000000000000000005f5260045ffd5b9050611f0e866128c8565b5110158b611d70565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b7f86a6861f000000000000000000000000000000000000000000000000000000005f5260045ffd5b50610240810135861015611c0c565b823573ffffffffffffffffffffffffffffffffffffffff8116810361111d57815260209283019201611bc4565b7f1fff9681000000000000000000000000000000000000000000000000000000005f5260045ffd5b9093506020813d602011612004575b81611fec60209383612862565b8101031261111d57611ffd90612973565b9284611b63565b3d9150611fdf565b6040513d5f823e3d90fd5b7f49e27cff000000000000000000000000000000000000000000000000000000005f5260045ffd5b9091506020813d60201161208a575b8161205b60209383612862565b8101031261111d5773ffffffffffffffffffffffffffffffffffffffff6120828192612927565b929150611adf565b3d915061204e565b3461111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760043567ffffffffffffffff811161111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc823603011261111d5760243567ffffffffffffffff811161111d5760209161212561212e92369060040161223e565b916004016122e2565b6040519015158152f35b3461111d575f7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760206040517faa4ed6eba3df66352fbab5103a7bde08f635eb0728a4a4889f8ef6f7e34b4c2a8152f35b3461111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760043567ffffffffffffffff811161111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc823603011261111d57600161220f9160243590600401612572565b1561221657005b7f936ac6b8000000000000000000000000000000000000000000000000000000005f5260045ffd5b9181601f8401121561111d5782359167ffffffffffffffff831161111d576020838186019501011161111d57565b90602080835192838152019201905f5b8181106122895750505090565b825184526020938401939092019160010161227c565b907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f602080948051918291828752018686015e5f8582860101520116010190565b915081357fa4146dd1eb46e4bd1d6977ffec765cfcc6f8c6d8ecb3bf903f3af4131e31fd01810361233a57505080602061231d9201906124df565b504281351115908161232d575090565b9050602042910135101590565b7faa4ed6eba3df66352fbab5103a7bde08f635eb0728a4a4889f8ef6f7e34b4c2a810361238f5750908060206123719201906124df565b5090358082351115918261238457505090565b602001351015905090565b7fab478f0811200e2fe5d52563d5a2104400f89e38425964dd6083cc49d15388d2036123c1576123be9161271e565b90565b7f4ce5c936000000000000000000000000000000000000000000000000000000005f5260045ffd5b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe18136030182121561111d570180359067ffffffffffffffff821161111d57602001918160051b3603831361111d57565b1561244457565b7f940aba73000000000000000000000000000000000000000000000000000000005f5260045ffd5b91908110156114835760051b810135907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa18136030182121561111d570190565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc18136030182121561111d570190565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe18136030182121561111d570180359067ffffffffffffffff821161111d5760200191813603831361111d57565b90821015611483576125479160051b8101906124df565b9091565b600211156110f057565b35600281101561111d5790565b91908110156114835760051b0190565b909161258a61258183806123e9565b9050841061243d565b61259e8361259884806123e9565b9061246c565b926125a884612913565b612696575060408301916125bc83856123e9565b9490506125c881612555565b6125d18161254b565b612619575f5b8581106125e957505050505050600190565b612602846125fb836105d589876123e9565b3585612572565b1561260f576001016125d7565b5050505050505f90565b600161262482612555565b61262d8161254b565b0361266e575f5b858110612645575050505050505f90565b612657846125fb836105d589876123e9565b61266357600101612634565b505050505050600190565b7fb0e35b9c000000000000000000000000000000000000000000000000000000005f5260045ffd5b6126bf906126b96126ae8660206123be9801906124ac565b9460208101906123e9565b90612530565b929091613369565b91908201809211611f1757565b81810292918115918404141715611f1757565b81156126f1570490565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b61278661273460408401359260208101906124df565b509261277d61276060208601359261275986612754816020850135612a28565b612a28565b90356126c7565b93612754816fffffffffffffffffffffffffffffffff8516612a28565b9060801c6126c7565b80156127b357620f4240820291808304620f42401490151715611f17576127ac916126e7565b9035111590565b5050505f90565b3573ffffffffffffffffffffffffffffffffffffffff8116810361111d5790565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff218136030182121561111d570190565b6080810190811067ffffffffffffffff82111761145657604052565b6040810190811067ffffffffffffffff82111761145657604052565b60c0810190811067ffffffffffffffff82111761145657604052565b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff82111761145657604052565b67ffffffffffffffff81116114565760051b60200190565b8051156114835760200190565b8051600110156114835760400190565b80518210156114835760209160051b010190565b905f905b600282106128fd57505050565b60208060019285518152019301910190916128f0565b6129219060408101906123e9565b90501590565b519073ffffffffffffffffffffffffffffffffffffffff8216820361111d57565b91908203918211611f1757565b519062ffffff8216820361111d57565b51908160020b820361111d57565b51906fffffffffffffffffffffffffffffffff8216820361111d57565b5f917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8183099181810293848085109403938085039414612a1e57837001000000000000000000000000000000001115612a0c575090700100000000000000000000000000000000910990828211900360801b910360801c1790565b634e487b71905260116020526024601cfd5b5050505060801c90565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6c010000000000000000000000008209918160601b91828085109403938085039414612af15783821115612ad9576c01000000000000000000000000829109815f0382168092046002816003021880820260020302808202600203028082026002030280820260020302808202600203028091026002030293600183805f03040190848311900302920304170290565b50634e487b715f52156003026011186020526024601cfd5b50906123be92506126e7565b5f917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8183099181810293848085109403938085039414612b7157836c010000000000000000000000001115612a0c5750906c01000000000000000000000000910990828211900360a01b910360601c1790565b5050505060601c90565b90917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8383099280830292838086109503948086039514612c2b5784831115612c135790829109815f0382168092046002816003021880820260020302808202600203028082026002030280820260020302808202600203028091026002030293600183805f03040190848311900302920304170290565b82634e487b715f52156003026011186020526024601cfd5b5050906123be92506126e7565b6123be9092919273ffffffffffffffffffffffffffffffffffffffff6020612c638383885116612ca0565b95015116612ca0565b73ffffffffffffffffffffffffffffffffffffffff81166123be575073eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee90565b73ffffffffffffffffffffffffffffffffffffffff1673eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee81148015612d6c575b15612cde57503190565b9073ffffffffffffffffffffffffffffffffffffffff602460209260405194859384927f70a082310000000000000000000000000000000000000000000000000000000084521660048301525afa90811561200c575f91612d3d575090565b90506020813d602011612d64575b81612d5860209383612862565b8101031261111d575190565b3d9150612d4b565b508015612cd4565b7f00000000000000000000000000000000000000000000000000000000000000009073ffffffffffffffffffffffffffffffffffffffff81169073ffffffffffffffffffffffffffffffffffffffff831682149182159081612dfb575b50612df55750156123be575073eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee90565b91505090565b73eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee915014155f612dd1565b60020b5f81121561321f57805f03905b620d89e882116131c15760018216156131985770ffffffffffffffffffffffffffffffffff6ffffcb933bd6fad37aa2d162d1a5940015b16916002811661317c575b60048116613160575b60088116613144575b60108116613128575b6020811661310c575b604081166130f0575b608081166130d4575b61010081166130b8575b610200811661309c575b6104008116613080575b6108008116613064575b6110008116613048575b612000811661302c575b6140008116613010575b6180008116612ff4575b620100008116612fd8575b620200008116612fbd575b620400008116612fa2575b6208000016612f89575b5f12612f5c575b73ffffffffffffffffffffffffffffffffffffffff9063ffffffff8116612f535760ff5f5b169060201c011690565b60ff6001612f49565b80156126f1577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff04612f24565b6b048a170391f7dc42444e8fa290910260801c90612f1d565b6d2216e584f5fa1ea926041bedfe9890920260801c91612f13565b916e5d6af8dedb81196699c329225ee6040260801c91612f08565b916f09aa508b5b7a84e1c677de54f3e99bc90260801c91612efd565b916f31be135f97d08fd981231505542fcfa60260801c91612ef2565b916f70d869a156d2a1b890bb3df62baf32f70260801c91612ee8565b916fa9f746462d870fdf8a65dc1f90e061e50260801c91612ede565b916fd097f3bdfd2022b8845ad8f792aa58250260801c91612ed4565b916fe7159475a2c29b7443b29c7fa6e889d90260801c91612eca565b916ff3392b0822b70005940c7a398e4b70f30260801c91612ec0565b916ff987a7253ac413176f2b074cf7815e540260801c91612eb6565b916ffcbe86c7900a88aedcffc83b479aa3a40260801c91612eac565b916ffe5dee046a99a2a811c461f1969c30530260801c91612ea2565b916fff2ea16466c96a3843ec78b326b528610260801c91612e99565b916fff973b41fa98c081472e6896dfb254c00260801c91612e90565b916fffcb9843d60f6159c9db58835c9266440260801c91612e87565b916fffe5caca7e10e4e61c3624eaa0941cd00260801c91612e7e565b916ffff2e50f5f656932ef12357cf3c7fdcc0260801c91612e75565b916ffff97272373d413259a46990580e213a0260801c91612e6c565b70ffffffffffffffffffffffffffffffffff700100000000000000000000000000000000612e61565b60646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600160248201527f54000000000000000000000000000000000000000000000000000000000000006044820152fd5b80612e2a565b9081608091031261111d576040519061323d8261280e565b61324681612973565b825260208101519081600f0b820361111d576060916020840152604081015160408401520151606082015290565b9073ffffffffffffffffffffffffffffffffffffffff8091169116039073ffffffffffffffffffffffffffffffffffffffff8211611f1757565b73ffffffffffffffffffffffffffffffffffffffff6132ed6fffffffffffffffffffffffffffffffff926123be9594838116848316116132f557613274565b169116612afd565b90613274565b9061335b6123be9373ffffffffffffffffffffffffffffffffffffffff9283811684861611613363575b7bffffffffffffffffffffffffffffffff000000000000000000000000848061334e8885613274565b1692169260601b16612b7b565b9116906126e7565b93613325565b929190926001146133a1577f4e487b71000000000000000000000000000000000000000000000000000000005f52605160045260245ffd5b6123be926122e256fea2646970667358221220ebf21fdbb2b0c9d5ba5aff2f4ef40fc16a91aa921aab56809c33323cac7e539464736f6c634300081e00330000000000000000000000004200000000000000000000000000000000000006

Deployed Bytecode

0x60e06040526004361015610011575f80fd5b5f5f3560e01c80630e63b5101461219057806327a83116146121385780633b15ba39146120925780633f0195c6146119b1578063407924651461196c578063a55b716a1461023c578063aaf5eb6814610200578063ad5c464814610191578063b4b15f7114610147578063eeb8594a146100ee5763f8f3ad8d14610093575f80fd5b346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb5760206040517fab478f0811200e2fe5d52563d5a2104400f89e38425964dd6083cc49d15388d28152f35b80fd5b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb5760206040517fa4146dd1eb46e4bd1d6977ffec765cfcc6f8c6d8ecb3bf903f3af4131e31fd018152f35b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb5760206040517001000000000000000000000000000000008152f35b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb57602060405173ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000004200000000000000000000000000000000000006168152f35b50346100eb57807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126100eb576020604051620f42408152f35b503461111d5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5767ffffffffffffffff6024351161111d5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc6024353603011261111d5760443567ffffffffffffffff811161111d576101607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc823603011261111d576102f760048201806123e9565b905061194457600161030f60448301836004016123e9565b9050036119445760405160a05261032760a05161280e565b5f60a051525f602060a051015260405160e0810181811067ffffffffffffffff821117611456576040525f81525f60208201525f60408201525f60608201525f60808201525f60a0820152604051610100810181811067ffffffffffffffff821117611456576040525f81525f60208201525f60408201525f60c052604060c05260c051516103b860c05182612862565b60c051368237606082015260c0518051906103d39082612862565b60c051368237608082015260c0518051906103ee9082612862565b60c05136823760a082015260c0518051906104099082612862565b60c05136823760c082015260c0515060c0515161042860c05182612862565b60c05136823760e082015260c0820152604060a051015260c051519060a0820182811067ffffffffffffffff8211176114565760c051525f825260c0515060c0515161047660c05182612862565b60c051368237602083015260c051805181840193916104959082612862565b60c051368237835260c05180516060830194916104b29082612862565b60c051368237845260c0518051906104ca9082612862565b60c051368237608083015260a0516060018290526104fa6104f0600460243501806127db565b60c08101906124df565b509361050c60e48501856004016124df565b50916105e06105db604060a05101519460c0515060c0515161053060c05182612862565b60c0519036903760c051518135916020906fffffffffffffffffffffffffffffffff60a08201356105608561282a565b8060801c8552168284015260a060c08a015101519288526080810135151560c0518a01526060810135828a015260c051810135828401520135905280865273ffffffffffffffffffffffffffffffffffffffff6105c260808b358c01016127ba565b1660608701526105d589358a01806123e9565b90612562565b6127ba565b73ffffffffffffffffffffffffffffffffffffffff60c08501519116905261061c61061460208835890101883589016123e9565b855191612562565b35602060c0850151015233845260c051516106368161282a565b6fffffffffffffffffffffffffffffffff61068361067b60608a358b010161066c610664828d8035016123e9565b8a5191612562565b3560801c85528a358b016123e9565b875191612562565b3516602082015281528151518151511015918261192e575b5050156119065773ffffffffffffffffffffffffffffffffffffffff60c08201515116905f60a060c051516106cf81612846565b8281528260208201528260c0518201528260608201528260808201520152602060c082015101519160c05151927f99fbab88000000000000000000000000000000000000000000000000000000008452600484015261018083602481845afa8015610fd8575f935f905f905f5f915f95611801575b509160049593916fffffffffffffffffffffffffffffffff6020969460c08a0151926060840151966080850151908a82015252169060c051015260020b8483015260020b905260c085208260a051015260c05151928380927f406793610000000000000000000000000000000000000000000000000000000082525afa8015610fd8575f9061179e575b73ffffffffffffffffffffffffffffffffffffffff9150168060a051526080602060a0510151602460c05151809481937fc815641c00000000000000000000000000000000000000000000000000000000835260048301525afa8015610fd8575f905f90611710575b73ffffffffffffffffffffffffffffffffffffffff925060020b60a084015216608082015261089c73ffffffffffffffffffffffffffffffffffffffff602060c05151946108848661282a565b8261089181835116612c6c565b168652015116612c6c565b9073ffffffffffffffffffffffffffffffffffffffff602084019216825282608085015260c05101516116b3575b505060206108dc608083015133612c38565b919092015190602082015252604060a051015160c0810151906060820180515160020b90602060a084015160020b9151015160020b92602081019081516115a2575b505073ffffffffffffffffffffffffffffffffffffffff60a051511690602060a05101519260c051517f9ec538c800000000000000000000000000000000000000000000000000000000815284600482015260c05181602481875afa928315610fd8575f915f94611565575b5060c051517f5aa208a400000000000000000000000000000000000000000000000000000000815260048101879052600284900b6024820152608081604481895afa958615610fd857610a2c9789925f9861153f575b50906080929160c05151809a819482937f5aa208a4000000000000000000000000000000000000000000000000000000008452600484019092916020906040830194835260020b910152565b03915afa918215610fd857602097610aa7975f9461150c575b5092939192889284126114f757606060c051880151970151935b12156114e257606060c051850151940151935b60808a0196875151920303039460e0610a9260c0518b0197885190612990565b99019889515251015192030303905190612990565b9151015260c0604060a0510151015190602073ffffffffffffffffffffffffffffffffffffffff83511692015191602480350191610af7610aed846024356004016124ac565b60208101906123e9565b90610b0860448401846004016123e9565b9190911561148357610b296105db610aed92610b4595610b3595359161246c565b956024356004016124ac565b91909260448101906004016123e9565b156114835773ffffffffffffffffffffffffffffffffffffffff92602092610b6e92359161246c565b013592161491826114d8575b5050156114b05760c051610b9491803501908101906123e9565b90604060a0510151519182101561148357610bb49160051b8101906123e9565b60a060c06040825101510151015151602060a060c060408251015101510151015173ffffffffffffffffffffffffffffffffffffffff6080604060a05101510151169260c05151608052610c0960805161282a565b606060805152606060206080510152610c21816128a3565b610c2f60c051519182612862565b81815260208101368360051b88011161111d5786905b8360051b8801821061127a57505060805152610c60816128a3565b610c6e60c051519182612862565b8181527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0610c9b836128a3565b015f5b818110611269575050602060805101525f5b8181106111215786303b1561111d5760c051517f0e63b51000000000000000000000000000000000000000000000000000000000815260c05160048201528060848101608051519060c0516044840152815180915260a4830190602060a48260051b8601019301915f905b828210611033575050505060206080510151907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbc838203016064840152815180825260208201916020808360051b8301019401925f915b838310610fe45788875f81808a8360248301520381305afa8015610fd857610fc5575b50604060a0510151606060a051015160c080515192805160208501526020810151825185015281518101511515606085015273ffffffffffffffffffffffffffffffffffffffff606082015116608085015273ffffffffffffffffffffffffffffffffffffffff60808201511660a085015260a081015160020b82850152015173ffffffffffffffffffffffffffffffffffffffff81511660e0840152602081015161010084015260c0518101516101208401526060810151610140840185905b60028210610fac5750505090610eaf60e083610e7c6080809601516101808801906128ec565b610e8f60a08201516101c08801906128ec565b610ea260c08201516102008801906128ec565b01516102408501906128ec565b73ffffffffffffffffffffffffffffffffffffffff815116610280840152610ee060208201516102a08501906128ec565b610ef460c0518201516102e08501906128ec565b610f0760608201516103208501906128ec565b01518261036083015b60028210610f80575050506103808152610f2c6103a082612862565b60c051519060c05182019260c051835260605180945260608301936080915b818110610f6a578480610f668887838203602085015261229f565b0390f35b8251865260209586019590920191600101610f4b565b60208060019273ffffffffffffffffffffffffffffffffffffffff865116815201930191019091610f10565b602080600192855160020b815201930191019091610e56565b610fd191505f90612862565b5f81610d95565b60c051513d5f823e3d90fd5b919395509193602080611021837fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08660019603018752895161229f565b97019301930190928695949293610d72565b91939092947fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5c9082030182528451805160028110156110f057825261109860208083015160608286015280516060860152015160c051608085015260a084019061229f565b9060c0510151918082039060c0510152602080835192838152019201905f905b8082106110d8575050506020806001929601920192018594939192610d1b565b909192602080600192865181520194019201906110b8565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602160045260245ffd5b5f80fd5b8061113d611132600193858a61246c565b60c0518101906123e9565b1580159150611232575b61122d577fab478f0811200e2fe5d52563d5a2104400f89e38425964dd6083cc49d15388d261118461117a83868b61246c565b60208101906124ac565b35036111d4576111cc8160c051518760208201528660c051820152886060820152606081526111b4608082612862565b60206080510151906111c683836128d8565b526128d8565b505b01610cb0565b7faa4ed6eba3df66352fbab5103a7bde08f635eb0728a4a4889f8ef6f7e34b4c2a61120361117a83868b61246c565b35036111ce576112278160c05151886020820152602081526111b460c05182612862565b506111ce565b6111ce565b507fa4146dd1eb46e4bd1d6977ffec765cfcc6f8c6d8ecb3bf903f3af4131e31fd0161126261117a83868b61246c565b3514611147565b806060602080938601015201610c9e565b813567ffffffffffffffff811161111d576060818a0136031261111d5760c051519081606081011067ffffffffffffffff606084011117611456576060820160c05152808a0135600281101561111d57825267ffffffffffffffff6020828c0101351161111d5760c0518a820160208101350136031261111d5760c051516113018161282a565b6020828c01818101350180358352013567ffffffffffffffff811161111d578b83016020810135010136601f8201121561111d57803567ffffffffffffffff81116114565760c051519161137d60207fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f8501160184612862565b818352366020838301011161111d57815f92602080930183860137830101526020820152602083015260c051818b0101359067ffffffffffffffff821161111d5736818c018301601f01121561111d5781818c010135906113dd826128a3565b926113ec60c051519485612862565b82845260208401918d6020369184848860051b92010101011161111d578d926020838386010101905b60208660051b858588010101018210611446575050505050509160209291839260c051820152815201910190610c45565b8135815260209182019101611415565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b7f53093dd4000000000000000000000000000000000000000000000000000000005f5260045ffd5b1490505f80610b7a565b606060c0518501518203940151850393610a72565b606060c0518801518303970151860393610a5f565b899394506115319060803d608011611538575b6115298183612862565b810190613225565b9392610a45565b503d61151f565b6080939291985061155c90843d8611611538576115298183612862565b979091926109e0565b9150925060c0513d60c0511161159b575b6115808183612862565b8160c051918101031261111d5760208151910151925f61098a565b503d611576565b6115ab84612e1a565b906fffffffffffffffffffffffffffffffff73ffffffffffffffffffffffffffffffffffffffff60806115dd89612e1a565b93015116935116908083905f905f9573ffffffffffffffffffffffffffffffffffffffff821673ffffffffffffffffffffffffffffffffffffffff8216116116a8575b505073ffffffffffffffffffffffffffffffffffffffff82168611611660575061164b9394506132fb565b905b60c0860151906020820152525f8061091e565b90935073ffffffffffffffffffffffffffffffffffffffff821685101561169d575092611692826116989495836132fb565b936132ae565b61164d565b9350611698926132ae565b935091505f80611620565b6116fe73ffffffffffffffffffffffffffffffffffffffff809260c05150816116f48160c05151976116e760c0518a612862565b60c051368a375116612d74565b1685525116612d74565b16602082015260808201525f806108ca565b50506080813d608011611796575b8161172b60809383612862565b8101031261111d5780519073ffffffffffffffffffffffffffffffffffffffff8216820361111d5773ffffffffffffffffffffffffffffffffffffffff9161178f606061177a60208501612965565b9361178860c0518201612955565b5001612955565b5090610837565b3d915061171e565b506020813d6020116117f9575b816117b860209383612862565b8101031261111d575173ffffffffffffffffffffffffffffffffffffffff8116810361111d5773ffffffffffffffffffffffffffffffffffffffff906107ce565b3d91506117ab565b97505050505050826101803d81116118ff575b61181e8183612862565b810103610180811261111d5760c01361111d57602060049160c0515161184381612846565b61184c86612927565b8152611859838701612927565b8382015261186a60c0518701612927565b60c05182015261187c60608701612927565b606082015261188d60808701612955565b608082015260a086015160a08201526118a860c08701612965565b956118b560e08201612965565b906118c36101008201612973565b906fffffffffffffffffffffffffffffffff610120820151946118ef6101606101408501519401612927565b5099959791965092939250610744565b503d611814565b7f9c373586000000000000000000000000000000000000000000000000000000005f5260045ffd5b5160209081015191510151101590505f8061069b565b7fafb5d0c3000000000000000000000000000000000000000000000000000000005f5260045ffd5b3461111d575f7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760206040516c010000000000000000000000008152f35b3461111d5760807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760243567ffffffffffffffff811161111d5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc82600401923603011261111d5760443567ffffffffffffffff811161111d57611a4290369060040161223e565b5060643567ffffffffffffffff811161111d57611a6390369060040161223e565b505060c0810173ffffffffffffffffffffffffffffffffffffffff611a87826127ba565b6020611a9b6105db60e087013597806127db565b916024604051809581937f6352211e0000000000000000000000000000000000000000000000000000000083528a6004840152165afa91821561200c575f9261203f575b5073ffffffffffffffffffffffffffffffffffffffff80911691160361201757602492602073ffffffffffffffffffffffffffffffffffffffff611b22846127ba565b16604051958680927f1efeed330000000000000000000000000000000000000000000000000000000082528560048301525afa93841561200c575f94611fd0575b506020830135936fffffffffffffffffffffffffffffffff611b8a86610100870135612948565b911603611fa857611b9e61026084016127ba565b610340840193604091825190611bb48483612862565b81610380840136811161111d5788915b818310611f7b57505050611bdf611bed9392611bfa92612c38565b939061028084013590612948565b926102a083013590612948565b93610220820135918284101580611f6c575b15611f4457611c1b8385612948565b98610240820135611c2c8189612948565b94606098885196611c3d8b89612862565b600288527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08b019d8e3660208b0137610300870135611c7b916126d4565b620f42409004611c8a896128bb565b52610320860135611c9a916126d4565b620f42409004611ca9886128c8565b52885197611cb78b8a612862565b600289528d3660208b0137611ccb886128bb565b51611cd591612948565b611cde896128bb565b52611ce8876128c8565b51611cf291612948565b611cfb886128c8565b526102c0840135620f42400390620f42408211611f1757620f4240611d28611d2f936101e08801356126d4565b04906126c7565b906102e0840135620f42400390620f42408211611f1757620f4240611d28611d5c936102008801356126d4565b90611d66876128bb565b5110159081611f03575b5015611edb5786809287519a611d86838d612862565b60028c5260208c019c368e37611d9b906127ba565b611da48c6128bb565b9073ffffffffffffffffffffffffffffffffffffffff1690526103608101611dcb906127ba565b611dd48c6128c8565b9073ffffffffffffffffffffffffffffffffffffffff16905201611df7906127ba565b97611e01906127ba565b9286519373ffffffffffffffffffffffffffffffffffffffff1684526020840152858301527f96cd817c6329656790ef8fba7675405193677d39619571282f5e21f3a98cd05991a18251956080870190608088525180915260a0870197905f5b818110611eaf5750505091611ea591611e988873ffffffffffffffffffffffffffffffffffffffff9589809b0360208b015261226c565b918783039088015261226c565b9216908301520390f35b825173ffffffffffffffffffffffffffffffffffffffff168a526020998a019990920191600101611e61565b7f215ff8e5000000000000000000000000000000000000000000000000000000005f5260045ffd5b9050611f0e866128c8565b5110158b611d70565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b7f86a6861f000000000000000000000000000000000000000000000000000000005f5260045ffd5b50610240810135861015611c0c565b823573ffffffffffffffffffffffffffffffffffffffff8116810361111d57815260209283019201611bc4565b7f1fff9681000000000000000000000000000000000000000000000000000000005f5260045ffd5b9093506020813d602011612004575b81611fec60209383612862565b8101031261111d57611ffd90612973565b9284611b63565b3d9150611fdf565b6040513d5f823e3d90fd5b7f49e27cff000000000000000000000000000000000000000000000000000000005f5260045ffd5b9091506020813d60201161208a575b8161205b60209383612862565b8101031261111d5773ffffffffffffffffffffffffffffffffffffffff6120828192612927565b929150611adf565b3d915061204e565b3461111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760043567ffffffffffffffff811161111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc823603011261111d5760243567ffffffffffffffff811161111d5760209161212561212e92369060040161223e565b916004016122e2565b6040519015158152f35b3461111d575f7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760206040517faa4ed6eba3df66352fbab5103a7bde08f635eb0728a4a4889f8ef6f7e34b4c2a8152f35b3461111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261111d5760043567ffffffffffffffff811161111d5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc823603011261111d57600161220f9160243590600401612572565b1561221657005b7f936ac6b8000000000000000000000000000000000000000000000000000000005f5260045ffd5b9181601f8401121561111d5782359167ffffffffffffffff831161111d576020838186019501011161111d57565b90602080835192838152019201905f5b8181106122895750505090565b825184526020938401939092019160010161227c565b907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f602080948051918291828752018686015e5f8582860101520116010190565b915081357fa4146dd1eb46e4bd1d6977ffec765cfcc6f8c6d8ecb3bf903f3af4131e31fd01810361233a57505080602061231d9201906124df565b504281351115908161232d575090565b9050602042910135101590565b7faa4ed6eba3df66352fbab5103a7bde08f635eb0728a4a4889f8ef6f7e34b4c2a810361238f5750908060206123719201906124df565b5090358082351115918261238457505090565b602001351015905090565b7fab478f0811200e2fe5d52563d5a2104400f89e38425964dd6083cc49d15388d2036123c1576123be9161271e565b90565b7f4ce5c936000000000000000000000000000000000000000000000000000000005f5260045ffd5b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe18136030182121561111d570180359067ffffffffffffffff821161111d57602001918160051b3603831361111d57565b1561244457565b7f940aba73000000000000000000000000000000000000000000000000000000005f5260045ffd5b91908110156114835760051b810135907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa18136030182121561111d570190565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc18136030182121561111d570190565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe18136030182121561111d570180359067ffffffffffffffff821161111d5760200191813603831361111d57565b90821015611483576125479160051b8101906124df565b9091565b600211156110f057565b35600281101561111d5790565b91908110156114835760051b0190565b909161258a61258183806123e9565b9050841061243d565b61259e8361259884806123e9565b9061246c565b926125a884612913565b612696575060408301916125bc83856123e9565b9490506125c881612555565b6125d18161254b565b612619575f5b8581106125e957505050505050600190565b612602846125fb836105d589876123e9565b3585612572565b1561260f576001016125d7565b5050505050505f90565b600161262482612555565b61262d8161254b565b0361266e575f5b858110612645575050505050505f90565b612657846125fb836105d589876123e9565b61266357600101612634565b505050505050600190565b7fb0e35b9c000000000000000000000000000000000000000000000000000000005f5260045ffd5b6126bf906126b96126ae8660206123be9801906124ac565b9460208101906123e9565b90612530565b929091613369565b91908201809211611f1757565b81810292918115918404141715611f1757565b81156126f1570490565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b61278661273460408401359260208101906124df565b509261277d61276060208601359261275986612754816020850135612a28565b612a28565b90356126c7565b93612754816fffffffffffffffffffffffffffffffff8516612a28565b9060801c6126c7565b80156127b357620f4240820291808304620f42401490151715611f17576127ac916126e7565b9035111590565b5050505f90565b3573ffffffffffffffffffffffffffffffffffffffff8116810361111d5790565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff218136030182121561111d570190565b6080810190811067ffffffffffffffff82111761145657604052565b6040810190811067ffffffffffffffff82111761145657604052565b60c0810190811067ffffffffffffffff82111761145657604052565b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff82111761145657604052565b67ffffffffffffffff81116114565760051b60200190565b8051156114835760200190565b8051600110156114835760400190565b80518210156114835760209160051b010190565b905f905b600282106128fd57505050565b60208060019285518152019301910190916128f0565b6129219060408101906123e9565b90501590565b519073ffffffffffffffffffffffffffffffffffffffff8216820361111d57565b91908203918211611f1757565b519062ffffff8216820361111d57565b51908160020b820361111d57565b51906fffffffffffffffffffffffffffffffff8216820361111d57565b5f917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8183099181810293848085109403938085039414612a1e57837001000000000000000000000000000000001115612a0c575090700100000000000000000000000000000000910990828211900360801b910360801c1790565b634e487b71905260116020526024601cfd5b5050505060801c90565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6c010000000000000000000000008209918160601b91828085109403938085039414612af15783821115612ad9576c01000000000000000000000000829109815f0382168092046002816003021880820260020302808202600203028082026002030280820260020302808202600203028091026002030293600183805f03040190848311900302920304170290565b50634e487b715f52156003026011186020526024601cfd5b50906123be92506126e7565b5f917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8183099181810293848085109403938085039414612b7157836c010000000000000000000000001115612a0c5750906c01000000000000000000000000910990828211900360a01b910360601c1790565b5050505060601c90565b90917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8383099280830292838086109503948086039514612c2b5784831115612c135790829109815f0382168092046002816003021880820260020302808202600203028082026002030280820260020302808202600203028091026002030293600183805f03040190848311900302920304170290565b82634e487b715f52156003026011186020526024601cfd5b5050906123be92506126e7565b6123be9092919273ffffffffffffffffffffffffffffffffffffffff6020612c638383885116612ca0565b95015116612ca0565b73ffffffffffffffffffffffffffffffffffffffff81166123be575073eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee90565b73ffffffffffffffffffffffffffffffffffffffff1673eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee81148015612d6c575b15612cde57503190565b9073ffffffffffffffffffffffffffffffffffffffff602460209260405194859384927f70a082310000000000000000000000000000000000000000000000000000000084521660048301525afa90811561200c575f91612d3d575090565b90506020813d602011612d64575b81612d5860209383612862565b8101031261111d575190565b3d9150612d4b565b508015612cd4565b7f00000000000000000000000042000000000000000000000000000000000000069073ffffffffffffffffffffffffffffffffffffffff81169073ffffffffffffffffffffffffffffffffffffffff831682149182159081612dfb575b50612df55750156123be575073eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee90565b91505090565b73eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee915014155f612dd1565b60020b5f81121561321f57805f03905b620d89e882116131c15760018216156131985770ffffffffffffffffffffffffffffffffff6ffffcb933bd6fad37aa2d162d1a5940015b16916002811661317c575b60048116613160575b60088116613144575b60108116613128575b6020811661310c575b604081166130f0575b608081166130d4575b61010081166130b8575b610200811661309c575b6104008116613080575b6108008116613064575b6110008116613048575b612000811661302c575b6140008116613010575b6180008116612ff4575b620100008116612fd8575b620200008116612fbd575b620400008116612fa2575b6208000016612f89575b5f12612f5c575b73ffffffffffffffffffffffffffffffffffffffff9063ffffffff8116612f535760ff5f5b169060201c011690565b60ff6001612f49565b80156126f1577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff04612f24565b6b048a170391f7dc42444e8fa290910260801c90612f1d565b6d2216e584f5fa1ea926041bedfe9890920260801c91612f13565b916e5d6af8dedb81196699c329225ee6040260801c91612f08565b916f09aa508b5b7a84e1c677de54f3e99bc90260801c91612efd565b916f31be135f97d08fd981231505542fcfa60260801c91612ef2565b916f70d869a156d2a1b890bb3df62baf32f70260801c91612ee8565b916fa9f746462d870fdf8a65dc1f90e061e50260801c91612ede565b916fd097f3bdfd2022b8845ad8f792aa58250260801c91612ed4565b916fe7159475a2c29b7443b29c7fa6e889d90260801c91612eca565b916ff3392b0822b70005940c7a398e4b70f30260801c91612ec0565b916ff987a7253ac413176f2b074cf7815e540260801c91612eb6565b916ffcbe86c7900a88aedcffc83b479aa3a40260801c91612eac565b916ffe5dee046a99a2a811c461f1969c30530260801c91612ea2565b916fff2ea16466c96a3843ec78b326b528610260801c91612e99565b916fff973b41fa98c081472e6896dfb254c00260801c91612e90565b916fffcb9843d60f6159c9db58835c9266440260801c91612e87565b916fffe5caca7e10e4e61c3624eaa0941cd00260801c91612e7e565b916ffff2e50f5f656932ef12357cf3c7fdcc0260801c91612e75565b916ffff97272373d413259a46990580e213a0260801c91612e6c565b70ffffffffffffffffffffffffffffffffff700100000000000000000000000000000000612e61565b60646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600160248201527f54000000000000000000000000000000000000000000000000000000000000006044820152fd5b80612e2a565b9081608091031261111d576040519061323d8261280e565b61324681612973565b825260208101519081600f0b820361111d576060916020840152604081015160408401520151606082015290565b9073ffffffffffffffffffffffffffffffffffffffff8091169116039073ffffffffffffffffffffffffffffffffffffffff8211611f1757565b73ffffffffffffffffffffffffffffffffffffffff6132ed6fffffffffffffffffffffffffffffffff926123be9594838116848316116132f557613274565b169116612afd565b90613274565b9061335b6123be9373ffffffffffffffffffffffffffffffffffffffff9283811684861611613363575b7bffffffffffffffffffffffffffffffff000000000000000000000000848061334e8885613274565b1692169260601b16612b7b565b9116906126e7565b93613325565b929190926001146133a1577f4e487b71000000000000000000000000000000000000000000000000000000005f52605160045260245ffd5b6123be926122e256fea2646970667358221220ebf21fdbb2b0c9d5ba5aff2f4ef40fc16a91aa921aab56809c33323cac7e539464736f6c634300081e0033

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

0000000000000000000000004200000000000000000000000000000000000006

-----Decoded View---------------
Arg [0] : _weth (address): 0x4200000000000000000000000000000000000006

-----Encoded View---------------
1 Constructor Arguments found :
Arg [0] : 0000000000000000000000004200000000000000000000000000000000000006


Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading
Loading...
Loading

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading
[ Download: CSV Export  ]

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