// File: contracts/addrs_and_selectors.sol



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

library Addrs {
  address internal constant UNISWAP_V2_FACTORY = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f; // ON ETHER OR EVERYWHERE ?
  address internal constant UNISWAP_V3_FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984; // ON ETHER
  address internal constant SUSHI_FACTORY = 0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac; // ON ETHER
  address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; // ON ETHER from v3-periphery
  address internal constant BALANCER_VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
  address internal constant CURVE_USDT_WBTC_WETH_POOL = 0xD51a44d3FaE010294C616388b506AcdA1bfAAE46;
  address internal constant CURVE_DAI_USDC_USDT_POOL = 0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7;
//  address internal constant SUSHI_FACTORY = 0xc35DADB65012eC5796536bD9864eD8773aBc74C4; // ON MOVR
//  address internal constant ANYSWAP_ETH_TO_MOVR_BRIDGE = 0x10c6b61DbF44a083Aec3780aCF769C77BE747E23;
//  address internal constant ANYCALLPROXY_ON_ETHER = 0x37414a8662bC1D25be3ee51Fb27C2686e2490A89;
}

library Selectors {
  bytes4 internal constant UNISWAP_V2_GETPAIR_SELECTOR = bytes4(keccak256("getPair(address,address)"));
  bytes4 internal constant UNISWAP_V2_GETRESERVES_SELECTOR = bytes4(keccak256("getReserves()"));

  bytes4 internal constant UNISWAP_V2_PAIR_SWAP_SELECTOR = bytes4(keccak256("swap(uint256,uint256,address,bytes)"));

  uint internal constant SLIPPAGE_LIMIT = 200;
//  bytes4 internal constant ANYCALLPROXY_ANYCALL_SELECTOR = bytes4(keccak256("anyCall(address,bytes,address,uint256)"));

  bytes4 internal constant TRANSFER_SELECTOR = bytes4(keccak256("transfer(address,uint256)"));
  bytes4 internal constant TRANSFERFROM_SELECTOR = bytes4(keccak256("transferFrom(address,address,uint256)"));
  bytes4 internal constant BALANCEOF_SELECTOR = bytes4(keccak256("balanceOf(address)"));
  bytes4 internal constant ALLOWANCE_SELECTOR = bytes4(keccak256("allowance(address,address)"));
}

// File: contracts/CalldataLoader.sol



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

//import "../libraries/openzeppelin-contracts/contracts/utils/math/SafeMath.sol";

library CalldataLoader {
//  using SafeMath for uint;

  function loadUint8(uint self) pure internal returns (uint x) {
    assembly {
      x := shr(248, calldataload(self))
    }
  }
  function loadUint16(uint self) pure internal returns (uint x) {
    assembly {
      x := shr(240, calldataload(self))
    }
  }
  function loadUint24(uint self) pure internal returns (uint x) {
    assembly {
      x := shr(232, calldataload(self))
    }
  }
  function loadUint256(uint self) pure internal returns (uint x) {
    assembly {
      x := calldataload(self)
    }
  }
  function loadAddress(uint self) pure internal returns (address x) {
    assembly {
      x := shr(96, calldataload(self)) // 12 * 8 = 96
    }
  }
  function loadTokenFromArray(uint self) pure internal returns (address x) {
    assembly {
      x := shr(96, calldataload(add(73, mul(20, self)))) // 73 = 68 + 5
    }
  }
  function loadTokenFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (address x) {
    assembly {
      x := shr(96, calldataload(add(start_ind, mul(20, arr_idx))))
    }
  }
//  function loadTotalsFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (uint x) {
//    assembly {
//      x := calldataload(add(start_ind, mul(32, arr_idx)))
//    }
//  }
//  function loadExpectedFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (uint x) {
//    assembly {
//      x := calldataload(add(start_ind, mul(32, arr_idx)))
//    }
//  }
//  function loadBalancesFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (uint x) {
//    assembly {
//      x := calldataload(add(start_ind, mul(32, arr_idx)))
//    }
//  }
  function loadVariableUint(uint self, uint len) pure internal returns (uint x) {
    uint extra = (uint(32) - len) << 3;
    assembly {
      x := shr(extra, calldataload(self))
    }
  }
}

//library BytesLoader {
////  using SafeMath for uint;
//
//  function mloadUint8(uint self) pure internal returns (uint x) {
//    assembly {
//      x := shr(248, mload(self))
//    }
//  }
//  function mloadUint16(uint self) pure internal returns (uint x) {
//    assembly {
//      x := shr(240, mload(self))
//    }
//  }
//  function mloadUint24(uint self) pure internal returns (uint x) {
//    assembly {
//      x := shr(232, mload(self))
//    }
//  }
//  function mloadUint256(uint self) pure internal returns (uint x) {
//    assembly {
//      x := mload(self)
//    }
//  }
//  function mloadAddress(uint self) pure internal returns (address x) {
//    assembly {
//      x := shr(96, mload(self)) // 12 * 8 = 96
//    }
//  }
////  function mloadTokenFromArray(uint self) pure internal returns (address x) {
////    assembly {
////      x := shr(96, mload(add(73, mul(20, self)))) // 73 = 68 + 5
////    }
////  }
//  function mloadTokenFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (address x) {
//    assembly {
//      x := shr(96, mload(add(start_ind, mul(20, arr_idx))))
//    }
//  }
////  function mloadTotalsFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (uint x) {
////    assembly {
////      x := mload(add(start_ind, mul(32, arr_idx)))
////    }
////  }
////  function mtopUpTotalsAtArrayV4(uint arr_idx, uint start_ind, uint extra) pure internal returns (uint x) {
////    uint addr = start_ind + arr_idx * 32;
////    uint v;
////    assembly {
////      v := mload(addr)
////    }
////    v += extra;
////    assembly {
////      mstore(addr, v)
////    }
////  }
////  function mloadExpectedFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (uint x) {
////    assembly {
////      x := mload(add(start_ind, mul(32, arr_idx)))
////    }
////  }
////  function mtopUpExpectedAtArrayV4(uint arr_idx, uint start_ind, uint extra) pure internal returns (uint x) {
////    uint addr = start_ind + arr_idx * 32;
////    uint v;
////    assembly {
////      v := mload(addr)
////    }
////    v += extra;
////    assembly {
////      mstore(addr, v)
////    }
////  }
////  function mloadBalancesFromArrayV4(uint arr_idx, uint start_ind) pure internal returns (uint x) {
////    assembly {
////      x := mload(add(start_ind, mul(32, arr_idx)))
////    }
////  }
////  function mtopUpBalancesAtArrayV4(uint arr_idx, uint start_ind, uint extra) pure internal returns (uint x) {
////    uint addr = start_ind + arr_idx * 32;
////    uint v;
////    assembly {
////      v := mload(addr)
////    }
////    v += extra;
////    assembly {
////      mstore(addr, v)
////    }
////  }
////  function mwithdrawBalancesAtArrayV4(uint arr_idx, uint start_ind, uint extra) pure internal returns (uint x) {
////    uint addr = start_ind + arr_idx * 32;
////    uint v;
////    assembly {
////      v := mload(addr)
////    }
////    v -= extra;
////    assembly {
////      mstore(addr, v)
////    }
////  }
////  function mtopUpTotalFrom(uint ind, uint extra) pure internal returns (uint x) {
////    uint v;
////    assembly {
////      v := mload(ind)
////    }
////    v += extra;
////    assembly {
////      mstore(ind, v)
////    }
////  }
//  function mloadVariableUint(uint self, uint len) pure internal returns (uint x) {
//    uint extra = (uint(32) - len) << 3;
//    assembly {
//      x := shr(extra, mload(self))
//    }
//  }
//}

// File: contracts/ETHRouter_V5_FromInput.sol



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

import "hardhat/console.sol";

//i mport "./libraries/BytesLib.sol";
import "../libraries/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "../libraries/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
//import "../libraries/openzeppelin-contracts/contracts/utils/math/SafeMath.sol";
import "../libraries/v2-core/contracts/interfaces/IUniswapV2Callee.sol";
import "../libraries/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
import "../libraries/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
//import "../libraries/v2-core/contracts/interfaces/IERC20.sol";
//import "../libraries/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
//import "../libraries/openzeppelin-contracts/contracts/interfaces/IERC2612.sol";
//import "./libraries/anyswap-v1-core/contracts/AnyswapV4CallProxy.sol";

//import "../libraries/v3-core/contracts/libraries/TickMath.sol";
import "./TickMath_0.8.sol";

import "../libraries/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
import "./interfaces/IWETH9.sol";
import "../libraries/TransferHelper.sol";

//import "../libraries/balancer-v2-monorepo/pkg/interfaces/contracts/vault/IVault.sol";
//import "../libraries/balancer-v2-monorepo/pkg/interfaces/contracts/vault/IBasePool.sol";
//import "../libraries/balancer-v2-monorepo/pkg/interfaces/contracts/vault/IAsset.sol";

import "./CalldataLoader.sol";
//import "./ETHRouter_V4_types.sol";
import "./ETHRouter_V5_types.sol";

import "./addrs_and_selectors.sol";

contract ETHRouter_V5_FromInput is IUniswapV3SwapCallback {
//  using SafeMath for uint;
//  using SafeMath for int;
  using CalldataLoader for uint;
//  using BytesLoader for uint;

  using V5CallType_basic_lib for V5CallType_basic_lib.CallType_basic_vars;
//  using V5CallType_1_lib for V5CallType_1_lib.CallType_1_vars;
  using V5CallType_5_lib for V5CallType_5_lib.CallType_5_vars;
  using V5CallType_6_lib for V5CallType_6_lib.CallType_6_vars;
  using V5CallType_7_lib for V5CallType_7_lib.CallType_7_vars;
  using V5ExecHeader for V5ExecHeader.Header;
//  address current_pool;

  address public owner;
  address public current_caller;
  address private callback_to_check;
  uint public value_to_pay;
  uint public constant network = 1;

  uint public constant SLIPPAGE_LIMIT = 200;

  constructor() {
    owner = msg.sender;
  }

  modifier ownerOnly {
    require(owner == msg.sender, 'ownerOnly');
    _;
  }

  modifier isLocked {
    require(current_caller != address(0), 'isLocked: false');
    _;
  }

  modifier isUnlocked {
    require(current_caller == address(0), 'isUnlocked: false');
    _;
  }

  modifier isCallbackChecked {
    require(callback_to_check == msg.sender, 'callback_to_check failure');
    _;
  }

  function setOwner(address newOwner) external ownerOnly {
    owner = newOwner;
  }


  function exec(bytes calldata data) isUnlocked() external payable returns (uint ind) {
    current_caller = msg.sender;
    (V5ExecHeader.Header memory h, uint ind_) = V5ExecHeader.load(data);
    uint tmp;
    assembly {
      tmp := data.offset
    }
//    uint old_balance = IERC20(uint(h.targetArrInd).loadTokenFromArrayV4(h.tokensOffset + tmp)).balanceOf(msg.sender);
    if (h.fromInput) {
      ind = execFromInputExternalHandler(h, ind_, data);
    } else {
//      ind = execFromOutputExternalHandler(h, ind_, data);
      revert("execFromOutput is not implemented");
    }
//    uint new_balance = IERC20(uint(h.targetArrInd).loadTokenFromArrayV4(h.tokensOffset + tmp)).balanceOf(msg.sender);
//    console.log("balance diff: ", new_balance - old_balance);
    current_caller = address(0);
  }

  function execFromInputExternalHandler(V5ExecHeader.Header memory h, uint ind_, bytes calldata data) internal returns (uint ind) {
    V5ExecState.V5ExecState memory st = h.newState();
    ind = ind_;

    {
      uint tmp;
      assembly {
        tmp := data.offset
      }
      if (st.header.doAcquireInputERC20Token) {
        if (uint256(st.header.sourceArrInd).loadTokenFromArrayV4(st.header.tokensOffset + tmp) == Addrs.WETH9) {
          if (st.msgValueLeft > 0) {
            WETH9(Addrs.WETH9).deposit{value: st.msgValueLeft}();
            st.balances[st.header.sourceArrInd] += st.msgValueLeft;
            st.msgValueLeft = 0;
          }
        } else {
          revert('doAcquireInputERC20Token: only WETH9 tokens are supported');
        }
      }
    }

    while (st.callCounter < st.header.numOfCalls) {
      uint calltype = ind.loadUint8();
      ind++;
////      console.log("i: ", st.callCounter, " | calltype: ", calltype);
      if (calltype == 1) { // transfer to univ2-like pair and swap
        V5CallType_basic_lib.CallType_basic_vars memory basic_vars;
        {
          uint tmp;
          assembly {
            tmp := data.offset
          }
          ind = basic_vars.load(ind, st.header.tokensOffset + tmp, st.header.tokensNum);
        }

        V5CallType_1_extra_lib.do_1_FromInput(basic_vars, st);
      } else if (calltype == 5) { // univ3
        V5CallType_5_lib.CallType_5_vars memory vars;
        {
          uint tmp;
          assembly {
            tmp := data.offset
          }
          ind = vars.load(ind, st.header.tokensOffset + tmp, st.header.tokensNum);
        }
        vars.prepare(st);
        callback_to_check = vars.univ3_pool;
        vars.doItFromInput(st, data);
        callback_to_check = address(0x0);
        vars.afterFromInput(st);
      } else if (calltype == 6) { // balancer
        V5CallType_6_lib.CallType_6_vars memory vars;
        {
          uint tmp;
          assembly {
            tmp := data.offset
          }
          ind = vars.load(ind, st.header.tokensOffset + tmp, st.header.tokensNum);
        }
        vars.prepare(st);
        vars.doItFromInput(st);
//        vars.afterFromInput(st);
      } else if (calltype == 7) { // curve
        V5CallType_7_lib.CallType_7_vars memory vars;
        {
          uint tmp;
          assembly {
            tmp := data.offset
          }
          ind = vars.load(ind, st.header.tokensOffset + tmp, st.header.tokensNum);
        }
        vars.prepare(st);
        vars.doItFromInput(st);
//        vars.afterFromInput(st);
      } else {
        revert("CT");
      }
      st.callCounter++;
    }

//    console.log('st.totals[st.header.targetArrInd]:', st.totals[st.header.targetArrInd]);
//    console.log('treshold:', st.expected[st.header.targetArrInd] * (SLIPPAGE_LIMIT - st.header.slippage)/SLIPPAGE_LIMIT);
    require(st.totals[st.header.targetArrInd] > st.expected[st.header.targetArrInd] * (SLIPPAGE_LIMIT - st.header.slippage)/SLIPPAGE_LIMIT, "slippage is above the threshold");
    uint tmp;
    assembly {
      tmp := data.offset
    }
    if (uint256(st.header.targetArrInd).loadTokenFromArrayV4(st.header.tokensOffset + tmp) == Addrs.WETH9) {
      if (st.header.getNativeETH) {
        if (st.ethAmountToUnwrap > 0) {
//          console.log('WETH9.balanceOf:', IERC20(Addrs.WETH9).balanceOf(address(this)));
//          console.log('st.ethAmountToUnwrap:', st.ethAmountToUnwrap);
          WETH9(Addrs.WETH9).withdraw(st.ethAmountToUnwrap);
        }
        payable(msg.sender).call{value: (st.ethAmountToUnwrap + st.balances[st.header.targetArrInd])}("");
      } else {
        TransferHelper.safeTransfer(Addrs.WETH9, msg.sender, st.balances[st.header.targetArrInd]);
      }
    } else {
      if (st.balances[st.header.targetArrInd] != 0) {
        TransferHelper.safeTransfer(uint256(st.header.targetArrInd).loadTokenFromArrayV4(st.header.tokensOffset + tmp), msg.sender, st.balances[st.header.targetArrInd]);
      }
    }
  }

  struct UniswapV3SwapCallbackData {
    address tokenIn;
    address tokenOut;
    uint amountIn;
    uint amountOut;
//    address payer;
  }

  function uniswapV3SwapCallback(
      int256 amount0Delta,
      int256 amount1Delta,
      bytes calldata _data
  ) external isLocked() isCallbackChecked() override {
      require(amount0Delta > 0 || amount1Delta > 0, 'zero amounts'); // swaps entirely within 0-liquidity regions are not supported

//    console.log("amount0Delta:");
//    console.logInt(amount0Delta);
//    console.log("amount1Delta");
//    console.logInt(amount1Delta);
//    (V5ExecState.V5ExecState memory st, V5CallType_5_lib.CallType_5_vars memory vars, bytes memory data) = abi.decode(_data, (V5ExecState.V5ExecState, V5CallType_5_lib.CallType_5_vars, bytes));
//    console.log("CT_5: st.callCounter:", st.callCounter);

    (address tokenIn, address tokenOut, bool isNative, bool fromSender) = abi.decode(_data, (address, address, bool, bool));
    (uint amountIn, uint amountOut) = tokenIn < tokenOut ? (
      uint256(amount0Delta),
      uint256(-amount1Delta)
    ) : (
      uint256(amount1Delta),
      uint256(-amount0Delta)
    );

//    V5Utils.pay(tokenIn, fromSender ? current_caller : address(this), msg.sender, amountIn);
    if (fromSender) {
      TransferHelper.safeTransferFrom(tokenIn, current_caller, msg.sender, amountIn);
    } else {
      if (isNative) {
        WETH9(Addrs.WETH9).deposit{value: amountIn}();
      }
      TransferHelper.safeTransfer(tokenIn, msg.sender, amountIn);
    }
  }

  fallback() external payable {
//    console.log('get paid');
  }
}

// File: contracts/ETHRouter_V5_types.sol



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

import "hardhat/console.sol";

//import "./libraries/BytesLib.sol";
import "../libraries/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "../libraries/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
//import "../libraries/openzeppelin-contracts/contracts/utils/math/SafeMath.sol";
import "../libraries/v2-core/contracts/interfaces/IUniswapV2Callee.sol";
import "../libraries/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
import "../libraries/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import "../libraries/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
//import "../libraries/v2-core/contracts/interfaces/IERC20.sol";
//import "../libraries/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
//import "../libraries/openzeppelin-contracts/contracts/interfaces/IERC2612.sol";
//import "./libraries/anyswap-v1-core/contracts/AnyswapV5CallProxy.sol";

//import "../libraries/v3-core/contracts/libraries/TickMath.sol";
import "./TickMath_0.8.sol";

import "../libraries/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
import "./interfaces/IWETH9.sol";

import "../libraries/TransferHelper.sol";

import "./interfaces/IVault.sol";

import "./CalldataLoader.sol";
import "./ETHRouter_V5_types.sol";

import "./addrs_and_selectors.sol";

library V5CallType_basic_lib {
  using CalldataLoader for uint;
  
  struct CallType_basic_vars {
    uint flags;
    uint token_source_ind;
    uint token_target_ind;
    address token_source;
    address token_target;
    uint amount_in_expected;
    uint amount_out_expected;
    uint amount_to_be_sent;
    uint amount_out;
  }

  using V5CallType_basic_lib for CallType_basic_vars;

  uint internal constant CT_BASIC_FROM_SENDER = 1;
  uint internal constant CT_BASIC_TO_SENDER = 2;

  function load(CallType_basic_vars memory self, uint ind, uint tokens_start_ind, uint tokens_num) internal pure returns(uint new_ind) {
    self.flags = ind.loadUint8();
    ind++;
////        console.log("self.flags:", self.flags);

    self.token_source_ind = ind.loadUint8();// = uint8(data[ind]);
    ind++;
////        console.log("self.token_source_ind:", self.token_source_ind);
    require(self.token_source_ind < tokens_num, "1LSI");
    self.token_source = self.token_source_ind.loadTokenFromArrayV4(tokens_start_ind);
////        console.log("self.token_source:", self.token_source);

    self.token_target_ind = ind.loadUint8(); //= uint8(data[ind]);
    ind++;
////        console.log("self.token_target_ind:", self.token_target_ind);
    require(self.token_target_ind < tokens_num, "1LTI");
    self.token_target = self.token_target_ind.loadTokenFromArrayV4(tokens_start_ind);
////        console.log("self.token_target:", self.token_target);

    {
      uint amount_in_len = ind.loadUint8();// = uint(uint8(data[ind]));
      ind++;
      self.amount_in_expected = ind.loadVariableUint(amount_in_len);
      ind += amount_in_len;
////          console.log("self.amount_in_expected:", self.amount_in_expected);
    }

    {
      uint amount_out_len = ind.loadUint8();
      ind++;
      self.amount_out_expected = ind.loadVariableUint(amount_out_len);
      ind += amount_out_len;
////          console.log("self.amount_out_expected:", self.amount_out_expected);
    }

    return ind;
  }
}

library V5CallType_1_extra_lib {
  using CalldataLoader for uint;

  uint internal constant CT_1_UNISWAP_OR_SUSHISWAP = 4; // false == uniswap, true == sushiswap
  uint internal constant CT_1_SUSHISWAP = 4;

  function do_1_FromInput(V5CallType_basic_lib.CallType_basic_vars memory self, V5ExecState.V5ExecState memory st) internal {
    if (self.token_source_ind == st.header.sourceArrInd) {
      self.amount_to_be_sent = self.amount_in_expected;
    } else {
      self.amount_to_be_sent = ((self.amount_in_expected * st.totals[self.token_source_ind]) / st.expected[self.token_source_ind]);
    }

    address v2pair;
    {
      address factory = (self.flags & CT_1_SUSHISWAP) > 0 ? Addrs.SUSHI_FACTORY : Addrs.UNISWAP_V2_FACTORY ;
      (address t0, address t1) = self.token_source < self.token_target ? (self.token_source, self.token_target) : (self.token_target, self.token_source) ;
      v2pair =  IUniswapV2Factory(factory).getPair(t0, t1);
    }
    (uint112 reserve_0, uint112 reserve_1, ) = IUniswapV2Pair(v2pair).getReserves();
    uint amount_out = self.token_source < self.token_target ?
      calcUniswapV2Out(reserve_0, reserve_1, self.amount_to_be_sent) :
      calcUniswapV2Out(reserve_1, reserve_0, self.amount_to_be_sent) ;
    if (amount_out == 0) {
      return;
    }

    if (self.token_source_ind == st.header.sourceArrInd) {
      if (st.header.doAcquireInputERC20Token) {
        TransferHelper.safeTransfer(self.token_source, v2pair, self.amount_to_be_sent);
        st.balances[self.token_source_ind] -= self.amount_to_be_sent;
      } else {
        if (self.token_source == Addrs.WETH9) {
          if (st.msgValueLeft >= self.amount_to_be_sent) {
            WETH9(Addrs.WETH9).deposit{value: self.amount_to_be_sent}();
            TransferHelper.safeTransfer(self.token_source, v2pair, self.amount_to_be_sent);
            st.msgValueLeft -= self.amount_to_be_sent;
          } else {
            revert('CT1: msg.value insufficient');
          }
        } else {
          TransferHelper.safeTransferFrom(self.token_source, msg.sender, v2pair, self.amount_to_be_sent);
        }
      }
    } else {
      TransferHelper.safeTransfer(self.token_source, v2pair, self.amount_to_be_sent);
      st.balances[self.token_source_ind] -= self.amount_to_be_sent;
    }

//    V5Utils.pay(self.token_source, (self.token_source_ind != st.header.sourceArrInd || (self.token_source == Addrs.WETH9 && msg.value != 0) || st.header.doPrefetchInputToken) ? address(this) : msg.sender, v2pair, self.amount_to_be_sent);

    (uint256 a0, uint256 a1) = (self.token_source < self.token_target) ?
      (uint256(0), amount_out) :
      (amount_out, uint256(0)) ;

    IUniswapV2Pair(v2pair).swap(a0, a1, (self.token_target_ind != st.header.targetArrInd || (self.token_target == Addrs.WETH9 && st.header.getNativeETH)) ? address(this): msg.sender, new bytes(0));

//    if (self.token_source_ind == st.header.sourceArrInd) {
//      if (self.token_source == Addrs.WETH9 && msg.value != 0 || st.header.doAcquireInputERC20Token) {
//        st.balances[self.token_source_ind] -= self.amount_to_be_sent;
//      }
//    } else {
//      st.balances[self.token_source_ind] -= self.amount_to_be_sent;
//    }
    if (self.token_target_ind == st.header.targetArrInd) {
      if (self.token_target == Addrs.WETH9 && st.header.getNativeETH) {
        st.ethAmountToUnwrap += amount_out;
      }
    } else {
      st.balances[self.token_target_ind] += amount_out;
    }
//    if (self.token_target_ind == st.header.targetArrInd && self.token_target == Addrs.WETH9 && st.header.getNativeEth) {
//      st.ethAmountToUnwrap += amount_out;
//    }
    st.totals[self.token_target_ind] += amount_out;
    st.expected[self.token_target_ind] += self.amount_out_expected;
  }

  function calcUniswapV2Out(uint r0, uint r1, uint a0) pure private returns (uint a1) {
    uint numer = r1 * a0 * 997;
    uint denom = r0 * 1000 + a0 * 997;
    a1 = numer / denom; // to round down
  }
}

library V5CallType_6_lib {
  using CalldataLoader for uint;
//  using BytesLoader for uint;

  struct CallType_6_vars {
    uint flags;
    uint token_source_ind;
    uint token_target_ind;
    address token_source;
    address token_target;
    uint amount_in_expected;
    uint amount_out_expected;
    bytes32 balancer_pool_id;
    uint amount_to_be_sent;
    uint amount_out;

    bool isNative;
  }

  using V5CallType_6_lib for CallType_6_vars;

  uint internal constant CT_6_FROM_SENDER = 1;
  uint internal constant CT_6_TO_SENDER = 2;

  function load(CallType_6_vars memory self, uint ind, uint tokens_start_ind, uint tokens_num) internal pure returns (uint new_ind) {
    self.flags = ind.loadUint8();
    ind++;
////        console.log("self.flags:", self.flags);

    self.token_source_ind = ind.loadUint8();// = uint8(data[ind]);
    ind++;
////        console.log("self.token_source_ind:", self.token_source_ind);
    require(self.token_source_ind < tokens_num, "1LSI");
    self.token_source = self.token_source_ind.loadTokenFromArrayV4(tokens_start_ind);
////        console.log("self.token_source:", self.token_source);

    self.token_target_ind = ind.loadUint8(); //= uint8(data[ind]);
    ind++;
////        console.log("self.token_target_ind:", self.token_target_ind);
    require(self.token_target_ind < tokens_num, "1LTI");
    self.token_target = self.token_target_ind.loadTokenFromArrayV4(tokens_start_ind);
////        console.log("self.token_target:", self.token_target);

    {
      uint amount_in_len = ind.loadUint8();// = uint(uint8(data[ind]));
      ind++;
      self.amount_in_expected = ind.loadVariableUint(amount_in_len);
      ind += amount_in_len;
////          console.log("self.amount_in_expected:", self.amount_in_expected);
    }

    {
      uint amount_out_len = ind.loadUint8();
      ind++;
      self.amount_out_expected = ind.loadVariableUint(amount_out_len);
      ind += amount_out_len;
////          console.log("self.amount_out_expected:", self.amount_out_expected);
    }

    self.balancer_pool_id = bytes32(ind.loadUint256());
    ind += 32;

//    console.log('pool_id:');
//    console.logBytes32(self.balancer_pool_id);

    return ind;
  }

  function prepare(CallType_6_vars memory self, V5ExecState.V5ExecState memory st) internal {
    if (self.token_source_ind == st.header.sourceArrInd) {
      self.amount_to_be_sent = self.amount_in_expected;
      if (st.header.doAcquireInputERC20Token) {
        TransferHelper.safeApprove(self.token_source, Addrs.BALANCER_VAULT, self.amount_to_be_sent);
        st.balances[self.token_source_ind] -= self.amount_to_be_sent;
        self.isNative = false;
      } else {
        if (self.token_source == Addrs.WETH9) {
          if (st.msgValueLeft >= self.amount_to_be_sent) {
            self.isNative = true;
            st.msgValueLeft -= self.amount_to_be_sent;
          } else {
            revert('CT6: msg.value insufficient');
          }
        } else {
          TransferHelper.safeTransferFrom(self.token_source, msg.sender, address(this), self.amount_to_be_sent);
          TransferHelper.safeApprove(self.token_source, Addrs.BALANCER_VAULT, self.amount_to_be_sent);
          self.isNative = false;
        }
      }
    } else {
      self.amount_to_be_sent = (self.amount_in_expected * st.totals[self.token_source_ind]) / st.expected[self.token_source_ind];
      TransferHelper.safeApprove(self.token_source, Addrs.BALANCER_VAULT, self.amount_to_be_sent);
      self.isNative = false;
      st.balances[self.token_source_ind] -= self.amount_to_be_sent;
    }
  }

  function doItFromInput(CallType_6_vars memory self, V5ExecState.V5ExecState memory st) internal {
//    console.log('pool_id:');
//    console.logBytes32(self.balancer_pool_id);
//    console.log('self.amount_to_be_sent:', self.amount_to_be_sent);
    (IAsset assetOut, address payable recipient) = self.token_target_ind == st.header.targetArrInd ? 
      (self.token_target == Addrs.WETH9 && st.header.getNativeETH ?
        ((self.flags & CT_6_TO_SENDER > 0) ?  (IAsset(address(0x0)), payable(msg.sender)) : (IAsset(self.token_target), payable(address(this)))) :
        (IAsset(self.token_target), payable(msg.sender))
      ) :
      (IAsset(self.token_target), payable(address(this)))
      ;
    uint256 output = IVault(Addrs.BALANCER_VAULT).swap{value: self.isNative ? self.amount_to_be_sent : 0}(IVault.SingleSwap({
      poolId: self.balancer_pool_id, //bytes32 poolId;
      kind: IVault.SwapKind.GIVEN_IN, //SwapKind kind;
//      assetIn: IAsset(self.token_source == Addrs.WETH9 ? address(0x0) : self.token_source), //IAsset assetIn;
      assetIn: IAsset(self.isNative ? address(0x0) : self.token_source), //IAsset assetIn;
//      assetOut: IAsset(self.token_target == Addrs.WETH9 ? address(0x0) : self.token_target), //IAsset assetOut;
      assetOut: assetOut, //IAsset(self.token_target), //IAsset assetOut;
      amount: self.amount_to_be_sent,//uint256 amount;
      userData: new bytes(0) //bytes userData;
    }),
    IVault.FundManagement({
      sender: address(this), //address sender;
      fromInternalBalance: false, // bool fromInternalBalance;
      recipient: recipient, // self.token_target == Addrs.WETH9 && st.header.getNativeETH ? self.flags & CT_6_TO_SENDER > 0 ? payable(msg.sender) : payable(address(this)), //address payable recipient;
      toInternalBalance: false //bool toInternalBalance;
    }),
      0,
      block.timestamp + 1000
    );
//    if (self.token_source_ind != st.header.sourceArrInd || (self.token_source == Addrs.WETH9 && (msg.value != 0 || st.header.doPrefetchInputToken))) {
//      st.balances[self.token_source_ind] -= self.amount_to_be_sent;
//    }
    if (self.token_target_ind == st.header.targetArrInd) {
//      st.totalOutput += output;
      if (self.token_target == Addrs.WETH9 && st.header.getNativeETH && self.flags & CT_6_TO_SENDER == 0) { // !!!!! FLAG VAR for flexability
        st.ethAmountToUnwrap += output;
      } else {
        // skip
      }
    } else {
      st.balances[self.token_target_ind] += output;
    }
    st.totals[self.token_target_ind] += output;
    st.expected[self.token_target_ind] += self.amount_out_expected;
    self.amount_out = output;
  }

//  function afterFromInput(CallType_6_vars memory self, V5ExecState.V5ExecState memory st) internal {
////    if (self.flags & CT_6_TO_SENDER > 0) {
////      TransferHelper.safeTransfer(self.token_target, msg.sender, self.amount_out);
//////      st.totalTo += uint128(self.amount_out);
////    }
//  }
}

library V5CallType_7_lib {
//  using SafeMath for uint;
  using CalldataLoader for uint;
//  using BytesLoader for uint;

  struct CallType_7_vars {
    uint flags;
    uint token_source_ind;
    uint token_target_ind;
    address token_source;
    address token_target;
    uint amount_in_expected;
    uint amount_out_expected;
    address curve_pool;
    uint curve_pool_source_ind;
    uint curve_pool_target_ind;
    uint amount_to_be_sent;
    uint amount_out;

    bool isNative;
  }

  using V5CallType_7_lib for CallType_7_vars;

  uint internal constant CT_7_FROM_SENDER = 1;
  uint internal constant CT_7_TO_SENDER = 2;

  function load(CallType_7_vars memory self, uint ind, uint tokens_start_ind, uint tokens_num) internal pure returns (uint new_ind) {
    self.flags = ind.loadUint8();
    ind++;
////        console.log("self.flags:", self.flags);

    self.token_source_ind = ind.loadUint8();// = uint8(data[ind]);
    ind++;
////        console.log("self.token_source_ind:", self.token_source_ind);
    require(self.token_source_ind < tokens_num, "1LSI");
    self.token_source = self.token_source_ind.loadTokenFromArrayV4(tokens_start_ind);
////        console.log("self.token_source:", self.token_source);

    self.token_target_ind = ind.loadUint8(); //= uint8(data[ind]);
    ind++;
////        console.log("self.token_target_ind:", self.token_target_ind);
    require(self.token_target_ind < tokens_num, "1LTI");
    self.token_target = self.token_target_ind.loadTokenFromArrayV4(tokens_start_ind);
////        console.log("self.token_target:", self.token_target);

    {
      uint amount_in_len = ind.loadUint8();// = uint(uint8(data[ind]));
      ind++;
      self.amount_in_expected = ind.loadVariableUint(amount_in_len);
      ind += amount_in_len;
////          console.log("self.amount_in_expected:", self.amount_in_expected);
    }

    {
      uint amount_out_len = ind.loadUint8();
      ind++;
      self.amount_out_expected = ind.loadVariableUint(amount_out_len);
      ind += amount_out_len;
////          console.log("self.amount_out_expected:", self.amount_out_expected);
    }

    self.curve_pool = ind.loadAddress();
    ind += 20;

    self.curve_pool_source_ind = ind.loadUint8();
    ind++;

    self.curve_pool_target_ind = ind.loadUint8();
    ind++;

    return ind;
  }

  function prepare(CallType_7_vars memory self, V5ExecState.V5ExecState memory st) internal {
    if (self.token_source_ind == st.header.sourceArrInd) {
      self.amount_to_be_sent = self.amount_in_expected;
      if (st.header.doAcquireInputERC20Token) {
        TransferHelper.safeApprove(self.token_source, self.curve_pool, self.amount_to_be_sent);
        self.isNative = false;
        st.balances[self.token_source_ind] -= self.amount_to_be_sent;
      } else {
        if (self.token_source == Addrs.WETH9) {
          if (st.msgValueLeft >= self.amount_to_be_sent) {
            self.isNative = true;
            st.msgValueLeft -= self.amount_to_be_sent;
          } else {
            revert('CT7: msg.value insufficient');
          }
        } else {
          TransferHelper.safeTransferFrom(self.token_source, msg.sender, address(this), self.amount_to_be_sent);
          TransferHelper.safeApprove(self.token_source, self.curve_pool, self.amount_to_be_sent);
          self.isNative = false;
        }
      }
    } else {
      self.amount_to_be_sent = (self.amount_in_expected * st.totals[self.token_source_ind]) / st.expected[self.token_source_ind];
      TransferHelper.safeApprove(self.token_source, self.curve_pool, self.amount_to_be_sent);
      self.isNative = false;
      st.balances[self.token_source_ind] -= self.amount_to_be_sent;
    }
  }

  function doItFromInput(CallType_7_vars memory self, V5ExecState.V5ExecState memory st) internal returns (bool res) {
//    console.log('self.token_target:', self.token_target);
    uint balanceBefore;
    uint balanceAfter;
    if (self.curve_pool == Addrs.CURVE_USDT_WBTC_WETH_POOL) {
      if (self.token_target == Addrs.WETH9 && self.token_target_ind == st.header.targetArrInd && st.header.getNativeETH) {
        balanceBefore = address(this).balance;
        CurvePool_USDT_WBTC_WETH(self.curve_pool).exchange(self.curve_pool_source_ind, self.curve_pool_target_ind, self.amount_to_be_sent, 0, true);
        balanceAfter = address(this).balance;
      } else {
        balanceBefore = IERC20(self.token_target).balanceOf(address(this));
        CurvePool_USDT_WBTC_WETH(self.curve_pool).exchange{value: self.isNative ? self.amount_to_be_sent : 0}(self.curve_pool_source_ind, self.curve_pool_target_ind, self.amount_to_be_sent, 0, self.isNative);
        balanceAfter = IERC20(self.token_target).balanceOf(address(this));
      }
    } else if (self.curve_pool == Addrs.CURVE_DAI_USDC_USDT_POOL) {
      balanceBefore = IERC20(self.token_target).balanceOf(address(this));
      CurvePool_DAI_USDC_USDT(self.curve_pool).exchange(int128(uint128(self.curve_pool_source_ind)), int128(uint128(self.curve_pool_target_ind)), self.amount_to_be_sent, 0);
      balanceAfter = IERC20(self.token_target).balanceOf(address(this));
    } else {
      revert('unsupported CurvePool');
    }
    uint diff = balanceAfter - balanceBefore;
    // sent in the aftermath
    self.amount_out = diff;
//    console.log('diff:', diff);
//    if (self.token_source_ind != st.header.sourceArrInd || (self.token_source == Addrs.WETH9 && (msg.value != 0 || st.header.doPrefetchInputToken))) {
//      st.balances[self.token_source_ind] -= self.amount_to_be_sent;
//    }
    st.balances[self.token_target_ind] += diff;
    st.totals[self.token_target_ind] += diff;
//    console.log('self.token_target_ind:', self.token_target_ind);
    st.expected[self.token_target_ind] += self.amount_out_expected;
  }

//  function afterFromInput(CallType_7_vars memory self, V5ExecState.V5ExecState memory st) internal {
//    if (self.isNative) {
//
//    } else {
//    }
//    if (self.flags & CT_7_TO_SENDER > 0) {
//      TransferHelper.safeTransfer(self.token_target, msg.sender, self.amount_out);
////      st.totalTo += uint128(self.amount_out);
//    }
//  }
}

interface CurvePool_DAI_USDC_USDT {
  function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy) external;
}

interface CurvePool_USDT_WBTC_WETH {
  function coins(uint256 i) external returns(address);
  function exchange(uint256 i, uint256 j, uint256 dx, uint256 min_dy, bool use_eth) external payable;
}

interface WETH9 {
  function deposit() external payable;
  function withdraw(uint256 amount) external;
}

library V5CallType_5_lib {
//  using SafeMath for uint;
  using CalldataLoader for uint;
//  using BytesLoader for uint;

  struct CallType_5_vars {
    uint flags;
    uint token_source_ind;
    uint token_target_ind;
    uint pool_fee;
    uint amount_in_expected;
    uint amount_out_expected;

    int specifiedAmount;

    bool isNative;
    bool fromSender;

    address token_source;
    address token_target;
    address univ3_pool;

    int dsa;
    int dta;
  }

  uint internal constant CT_5_FROM_SENDER = 1;
  uint internal constant CT_5_TO_SENDER = 2;

  function load(CallType_5_vars memory self, uint ind, uint tokens_start_ind, uint tokens_num) internal view returns(uint new_ind) {
    self.flags = ind.loadUint8();
    ind++;
//    console.log("flags:", self.flags);
    self.token_source_ind = ind.loadUint8();
    ind++;
//    console.log("self.token_source_ind:", self.token_source_ind);
    require(self.token_source_ind < tokens_num, "5SI");
    self.token_source = self.token_source_ind.loadTokenFromArrayV4(tokens_start_ind);

    self.token_target_ind = ind.loadUint8();
    ind++;
    require(self.token_target_ind < tokens_num, "5TI");
    self.token_target = self.token_target_ind.loadTokenFromArrayV4(tokens_start_ind);
    self.pool_fee = ind.loadUint24();
//    console.log("pool_fee:", self.pool_fee);
    ind += 3;
    {
      uint amount_in_len = ind.loadUint8();
      ind++;
      self.amount_in_expected = ind.loadVariableUint(amount_in_len);
      ind += amount_in_len;
    }
    {
      uint amount_out_len = ind.loadUint8();
      ind++;
      self.amount_out_expected = ind.loadVariableUint(amount_out_len);
      ind += amount_out_len;
    }
//    console.log("token_source:", self.token_source);
//    console.log("token_target:", self.token_target);
//    console.log("pool_fee:", self.pool_fee);
//    console.log("amount_in_expected:", self.amount_in_expected);
//    console.log("amount_out_expected:", self.amount_out_expected);
    return ind;
  }

  function prepare(CallType_5_vars memory self, V5ExecState.V5ExecState memory st) internal {
    self.univ3_pool = IUniswapV3Factory(Addrs.UNISWAP_V3_FACTORY).getPool(self.token_source, self.token_target, uint24(self.pool_fee)); 
//    console.log("univ3_pool:", self.univ3_pool);
    require(self.univ3_pool != address(0), '5ZPA');
    if (self.token_source_ind == st.header.sourceArrInd) {
      self.specifiedAmount = int256(self.amount_in_expected);
      if (st.header.doAcquireInputERC20Token) {
        self.isNative == false;
        self.fromSender = false;
        st.balances[self.token_source_ind] -= uint(self.specifiedAmount);
      } else {
        if (self.token_source == Addrs.WETH9) {
          if (st.msgValueLeft >= uint256(self.specifiedAmount)) {
            self.isNative = true;
            self.fromSender = false;
            st.msgValueLeft -= uint256(self.specifiedAmount);
          } else {
            revert('CT5: msg.value insufficient');
          }
        } else {
          self.isNative = false;
          self.fromSender = true;
        }
      }
    } else {
      self.specifiedAmount = int256((self.amount_in_expected * st.totals[self.token_source_ind]) / st.expected[self.token_source_ind]);
      self.isNative = false;
      self.fromSender = false;
      st.balances[self.token_source_ind] -= uint256(self.specifiedAmount);
    }
  }

  function doItFromInput(CallType_5_vars memory self, V5ExecState.V5ExecState memory st, bytes calldata data) internal {
//    self.univ3_pool = IUniswapV3Factory(Addrs.UNISWAP_V3_FACTORY).getPool(self.token_source, self.token_target, uint24(self.pool_fee)); 
////    console.log("univ3_pool:", self.univ3_pool);
//    require(self.univ3_pool != address(0), '5ZPA');
    bool zeroForOne = self.token_source < self.token_target;
    {
//      if (self.flags & V5CallType_5_lib.CT_5_FROM_SENDER != 0) {
//        st.totalFrom += uint128(self.amount_in_expected);
////        console.log("amount from msg.sender:", self.amount_in_expected);
//        self.specifiedAmount = int256(self.amount_in_expected);
//      } else {
//        self.specifiedAmount = int256((self.amount_in_expected * st.totals[self.token_source_ind]) / st.expected[self.token_source_ind]);
//      }
//      console.log("specifiedAmount:");
//      console.logInt(self.specifiedAmount);
//      console.log("total:     ", st.totals[self.token_target_ind]);
//      console.log("expected:  ", st.expected[self.token_target_ind]);
//      console.log("recipient: ", (self.flags & V5CallType_5_lib.CT_5_TO_SENDER) == 0 ? address(this) : st.msgsender);
      try IUniswapV3Pool(self.univ3_pool).swap(
//          (self.flags & V5CallType_5_lib.CT_5_TO_SENDER) == 0 ? address(this) : st.msgsender ,
          (self.token_target_ind != st.header.targetArrInd || (self.token_target == Addrs.WETH9 && st.header.getNativeETH)) ? address(this) : msg.sender,
          zeroForOne,
          self.specifiedAmount,
          zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1,
//          abi.encode(st, self, data)
          abi.encode(self.token_source, self.token_target, self.isNative, self.fromSender)
      ) returns (int256 da0, int256 da1) {
        if (zeroForOne) {
          (self.dsa, self.dta) = (da0, da1);
        } else {
          (self.dsa, self.dta) = (da1, da0);
        }
      } catch Error(string memory s) {
//        console.log("transfer failed");
////            console.log("expected balance: ", value_to_pay);
//        console.log("actual balance: ", uint256(st.balances[self.token_source_ind]));
        revert(s);
      }
    }
  }

  function afterFromInput(CallType_5_vars memory self, V5ExecState.V5ExecState memory st) internal {
//    console.log('dsa');
//    console.logInt(self.dsa);
//    console.log('dta');
//    console.logInt(self.dta);
//    if (self.token_source_ind != st.header.sourceArrInd || msg.value != 0 || st.header.doPrefetchInputToken) {
//      st.balances[self.token_source_ind] -= uint256(self.dsa);
//    }

    if (self.specifiedAmount < self.dsa) {
      uint diff = uint256(self.dsa - self.specifiedAmount);
      if (self.token_source_ind == st.header.sourceArrInd) {
        if (st.header.doAcquireInputERC20Token) {
          st.balances[self.token_source_ind] -= diff;
        } else {
          if (self.token_source == Addrs.WETH9) {
            if (st.msgValueLeft >= diff) {
              st.msgValueLeft -= diff;
            } else {
              revert('CT5: msg.value insufficient (after)');
            }
          } else {
            // skip
          }
        }
      } else {
        st.balances[self.token_source_ind] -= diff;
      }
    }

    if (self.token_target_ind == st.header.targetArrInd) {
      if (self.token_target == Addrs.WETH9 && st.header.getNativeETH) {
        st.ethAmountToUnwrap += uint256(-self.dta);
//        st.balances[st.token_target_ind] += uint256(-self.dta); // in case of ETH target, balance logs only the native ETH, i.e. the output of Curve only
      }
    } else {
      st.balances[self.token_target_ind] += uint256(-self.dta);
    }
    st.totals[self.token_target_ind] += uint256(-self.dta);
    st.expected[self.token_target_ind] += self.amount_out_expected;
  }
}

library V5ExecHeader {
  using CalldataLoader for uint;

  struct Header {
    uint8 slippage;
    uint8 tokensNum;
    uint8 numOfCalls;
    uint8 sourceArrInd;
    uint8 targetArrInd;
    bool fromInput;
    uint32 callArrOffset;
    uint32 tokensOffset;

    bool getNativeETH;
    bool doAcquireInputERC20Token;
  }

  function load(bytes calldata cdata) view internal returns (Header memory h, uint ind) {
    assembly {
      ind := cdata.offset
    }
    {
      uint _network = ind.loadUint16();
      ind += 2;
//      console.log("_network:", _network);
      require(_network == 1, "WRONGNETWORK");
      uint version = ind.loadUint8();
//    console.log("version:", version);
      ind++;
      require(version == 5, "WRONGVERSION");
    }
    h.fromInput = ind.loadUint8() > 0;
    ind++;
//    console.log("fromInput:", h.fromInput);
    h.slippage = uint8(ind.loadUint8());
    ind++;
//    console.log("slippage:", h.slippage);
    h.getNativeETH = bool(ind.loadUint8() != 0); //true;
    ind++;
    h.doAcquireInputERC20Token = bool(ind.loadUint8() != 0);//false;
    ind++;

    h.sourceArrInd = uint8(ind.loadUint8());
    ind++;
//    console.log("sourceArrInd:", h.sourceArrInd);
    h.targetArrInd = uint8(ind.loadUint8());
    ind++;

//    console.log("targetArrInd:", h.targetArrInd);
    h.tokensNum = uint8(ind.loadUint8());
    ind++;
    {
      uint tmp;
      assembly {
        tmp := sub(ind, cdata.offset)
      }
      h.tokensOffset = uint32(tmp);
    }
    ind += h.tokensNum * 20;
    h.numOfCalls = uint8(ind.loadUint8());// = uint8(data[ind]);
    ind++;
    {
      uint tmp;
      assembly {
        tmp := sub(ind, cdata.offset)
      }
      h.callArrOffset = uint32(tmp);
    }

//    h.msgValueLeft = msg.value;

  }

  function newState(Header memory h) view internal returns(V5ExecState.V5ExecState memory st) {
    st = V5ExecState.V5ExecState({
      totals: new uint256[](h.tokensNum),
      expected: new uint256[](h.tokensNum),
      balances: new uint256[](h.tokensNum),
//      totalFrom: 0,
//      totalTo: 0,
//      currentCallOffset: h.callArrOffset,
//      nextCallOffset: h.callArrOffset,
      callCounter: 0,
      msgsender: msg.sender,
      msgValueLeft: msg.value,
      ethAmountToUnwrap: 0,
      header: h
    });
  }
}

library V5ExecState {
  struct V5ExecState {
    uint256[] totals;
    uint256[] expected;
    uint256[] balances;
//    uint256 totalFrom;
//    uint256 totalTo;
// no need for FromInput
//    uint32 currentCallOffset;
//    uint32 nextCallOffset;
    uint callCounter;
    address msgsender;
    uint msgValueLeft;
    uint ethAmountToUnwrap;
    V5ExecHeader.Header header;
  }
}

library  V5Utils {
  function pay(
      address token,
      address payer,
      address recipient,
      uint256 value
  ) internal {
//      console.log("going to pay ... ");
      if (token == Addrs.WETH9 && address(this).balance >= value) {
          // pay with WETH9
          IWETH9(Addrs.WETH9).deposit{value: value}(); // wrap only what is needed to pay
          IWETH9(Addrs.WETH9).transfer(recipient, value);
      } else if (payer == address(this)) {
          // pay with tokens already in the contract (for the exact input multihop case)
//          console.log("from router");
//          console.log("token:", token);
//          console.log("value to pay: ", value);
//          console.log("actual balance: ", IERC20(token).balanceOf(address(this)));
//          try TransferHelper.safeTransfer(token, recipient, value) {
//          } catch Error(string memory s) {
//            value_to_pay = value;
//            revert(s);
//          }
          TransferHelper.safeTransfer(token, recipient, value);
      } else {
          // pull payment
//          console.log("from msg.sender");
//          console.log("token:", token);
//          console.log("payer:", payer);
//          console.log("value to pay: ", value);
//          (bool success_, bytes memory b_allowance) = token.staticcall(abi.encodeWithSelector(Selectors.ALLOWANCE_SELECTOR, payer, address(this)));
//          uint256 allowance = abi.decode(b_allowance, (uint256));
//          console.log('token:', token);
//          console.log('payer:', payer);
//          console.log('address(this):', address(this));
//          console.log("allowance:    ", allowance);
//          console.log('going to trasferFrom');
          TransferHelper.safeTransferFrom(token, payer, recipient, value);
//          console.log("trasfered: ", value);
      }
  }
}

// File: contracts/interfaces/IVault.sol



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

interface IVault {
    function swap(
        SingleSwap memory singleSwap,
        FundManagement memory funds,
        uint256 limit,
        uint256 deadline
    ) external payable returns (uint256);

    struct SingleSwap {
        bytes32 poolId;
        SwapKind kind;
        IAsset assetIn;
        IAsset assetOut;
        uint256 amount;
        bytes userData;
    }

    struct FundManagement {
        address sender;
        bool fromInternalBalance;
        address payable recipient;
        bool toInternalBalance;
    }

    enum SwapKind { GIVEN_IN, GIVEN_OUT }
}

interface IAsset {
    // solhint-disable-previous-line no-empty-blocks
}

// File: contracts/interfaces/IWETH9.sol



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

import "../../libraries/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";

/// @title Interface for WETH9
interface IWETH9 is IERC20 {
    /// @notice Deposit ether to get wrapped ether
    function deposit() external payable;

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


// File: contracts/TickMath_0.8.sol



// 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 = -887272;
    /// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
    int24 internal constant MAX_TICK = -MIN_TICK;

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

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

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

          if gt(tick, 0) { ratio := div(not(0), ratio) }

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

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

        uint256 r = ratio;
        uint256 msb = 0;

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

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

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

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

        int256 log_sqrt10001;// = log_2 * 255738958999603826347141; // 128.128 number
        assembly {
          log_sqrt10001 := mul(log_2, 255738958999603826347141) // 128.128 number
        }

        int24 tickLow;// = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128);
        int24 tickHi;// = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128);
        assembly {
          tickLow := sar(128, sub(log_sqrt10001, 3402992956809132418596140100660247210))
          tickHi := sar(128, add(log_sqrt10001, 291339464771989622907027621153398088495))
        }

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

// File: hardhat/console.sol



// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

library console {
    address constant CONSOLE_ADDRESS =
        0x000000000000000000636F6e736F6c652e6c6f67;

    function _sendLogPayloadImplementation(bytes memory payload) internal view {
        address consoleAddress = CONSOLE_ADDRESS;
        /// @solidity memory-safe-assembly
        assembly {
            pop(
                staticcall(
                    gas(),
                    consoleAddress,
                    add(payload, 32),
                    mload(payload),
                    0,
                    0
                )
            )
        }
    }

    function _castToPure(
      function(bytes memory) internal view fnIn
    ) internal pure returns (function(bytes memory) pure fnOut) {
        assembly {
            fnOut := fnIn
        }
    }

    function _sendLogPayload(bytes memory payload) internal pure {
        _castToPure(_sendLogPayloadImplementation)(payload);
    }

    function log() internal pure {
        _sendLogPayload(abi.encodeWithSignature("log()"));
    }
    function logInt(int256 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(int256)", p0));
    }

    function logUint(uint256 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256)", p0));
    }

    function logString(string memory p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string)", p0));
    }

    function logBool(bool p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool)", p0));
    }

    function logAddress(address p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address)", p0));
    }

    function logBytes(bytes memory p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes)", p0));
    }

    function logBytes1(bytes1 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes1)", p0));
    }

    function logBytes2(bytes2 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes2)", p0));
    }

    function logBytes3(bytes3 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes3)", p0));
    }

    function logBytes4(bytes4 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes4)", p0));
    }

    function logBytes5(bytes5 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes5)", p0));
    }

    function logBytes6(bytes6 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes6)", p0));
    }

    function logBytes7(bytes7 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes7)", p0));
    }

    function logBytes8(bytes8 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes8)", p0));
    }

    function logBytes9(bytes9 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes9)", p0));
    }

    function logBytes10(bytes10 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes10)", p0));
    }

    function logBytes11(bytes11 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes11)", p0));
    }

    function logBytes12(bytes12 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes12)", p0));
    }

    function logBytes13(bytes13 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes13)", p0));
    }

    function logBytes14(bytes14 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes14)", p0));
    }

    function logBytes15(bytes15 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes15)", p0));
    }

    function logBytes16(bytes16 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes16)", p0));
    }

    function logBytes17(bytes17 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes17)", p0));
    }

    function logBytes18(bytes18 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes18)", p0));
    }

    function logBytes19(bytes19 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes19)", p0));
    }

    function logBytes20(bytes20 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes20)", p0));
    }

    function logBytes21(bytes21 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes21)", p0));
    }

    function logBytes22(bytes22 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes22)", p0));
    }

    function logBytes23(bytes23 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes23)", p0));
    }

    function logBytes24(bytes24 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes24)", p0));
    }

    function logBytes25(bytes25 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes25)", p0));
    }

    function logBytes26(bytes26 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes26)", p0));
    }

    function logBytes27(bytes27 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes27)", p0));
    }

    function logBytes28(bytes28 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes28)", p0));
    }

    function logBytes29(bytes29 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes29)", p0));
    }

    function logBytes30(bytes30 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes30)", p0));
    }

    function logBytes31(bytes31 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes31)", p0));
    }

    function logBytes32(bytes32 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bytes32)", p0));
    }

    function log(uint256 p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256)", p0));
    }

    function log(string memory p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string)", p0));
    }

    function log(bool p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool)", p0));
    }

    function log(address p0) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address)", p0));
    }

    function log(uint256 p0, uint256 p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256)", p0, p1));
    }

    function log(uint256 p0, string memory p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string)", p0, p1));
    }

    function log(uint256 p0, bool p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool)", p0, p1));
    }

    function log(uint256 p0, address p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address)", p0, p1));
    }

    function log(string memory p0, uint256 p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256)", p0, p1));
    }

    function log(string memory p0, string memory p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string)", p0, p1));
    }

    function log(string memory p0, bool p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool)", p0, p1));
    }

    function log(string memory p0, address p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address)", p0, p1));
    }

    function log(bool p0, uint256 p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256)", p0, p1));
    }

    function log(bool p0, string memory p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string)", p0, p1));
    }

    function log(bool p0, bool p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool)", p0, p1));
    }

    function log(bool p0, address p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address)", p0, p1));
    }

    function log(address p0, uint256 p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256)", p0, p1));
    }

    function log(address p0, string memory p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string)", p0, p1));
    }

    function log(address p0, bool p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool)", p0, p1));
    }

    function log(address p0, address p1) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address)", p0, p1));
    }

    function log(uint256 p0, uint256 p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256)", p0, p1, p2));
    }

    function log(uint256 p0, uint256 p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string)", p0, p1, p2));
    }

    function log(uint256 p0, uint256 p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool)", p0, p1, p2));
    }

    function log(uint256 p0, uint256 p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address)", p0, p1, p2));
    }

    function log(uint256 p0, string memory p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256)", p0, p1, p2));
    }

    function log(uint256 p0, string memory p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,string)", p0, p1, p2));
    }

    function log(uint256 p0, string memory p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool)", p0, p1, p2));
    }

    function log(uint256 p0, string memory p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,address)", p0, p1, p2));
    }

    function log(uint256 p0, bool p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256)", p0, p1, p2));
    }

    function log(uint256 p0, bool p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string)", p0, p1, p2));
    }

    function log(uint256 p0, bool p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool)", p0, p1, p2));
    }

    function log(uint256 p0, bool p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address)", p0, p1, p2));
    }

    function log(uint256 p0, address p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256)", p0, p1, p2));
    }

    function log(uint256 p0, address p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,string)", p0, p1, p2));
    }

    function log(uint256 p0, address p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool)", p0, p1, p2));
    }

    function log(uint256 p0, address p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,address)", p0, p1, p2));
    }

    function log(string memory p0, uint256 p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256)", p0, p1, p2));
    }

    function log(string memory p0, uint256 p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,string)", p0, p1, p2));
    }

    function log(string memory p0, uint256 p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool)", p0, p1, p2));
    }

    function log(string memory p0, uint256 p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,address)", p0, p1, p2));
    }

    function log(string memory p0, string memory p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,uint256)", p0, p1, p2));
    }

    function log(string memory p0, string memory p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,string)", p0, p1, p2));
    }

    function log(string memory p0, string memory p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,bool)", p0, p1, p2));
    }

    function log(string memory p0, string memory p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,address)", p0, p1, p2));
    }

    function log(string memory p0, bool p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256)", p0, p1, p2));
    }

    function log(string memory p0, bool p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,string)", p0, p1, p2));
    }

    function log(string memory p0, bool p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,bool)", p0, p1, p2));
    }

    function log(string memory p0, bool p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,address)", p0, p1, p2));
    }

    function log(string memory p0, address p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,uint256)", p0, p1, p2));
    }

    function log(string memory p0, address p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,string)", p0, p1, p2));
    }

    function log(string memory p0, address p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,bool)", p0, p1, p2));
    }

    function log(string memory p0, address p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,address)", p0, p1, p2));
    }

    function log(bool p0, uint256 p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256)", p0, p1, p2));
    }

    function log(bool p0, uint256 p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string)", p0, p1, p2));
    }

    function log(bool p0, uint256 p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool)", p0, p1, p2));
    }

    function log(bool p0, uint256 p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address)", p0, p1, p2));
    }

    function log(bool p0, string memory p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256)", p0, p1, p2));
    }

    function log(bool p0, string memory p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,string)", p0, p1, p2));
    }

    function log(bool p0, string memory p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,bool)", p0, p1, p2));
    }

    function log(bool p0, string memory p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,address)", p0, p1, p2));
    }

    function log(bool p0, bool p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256)", p0, p1, p2));
    }

    function log(bool p0, bool p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,string)", p0, p1, p2));
    }

    function log(bool p0, bool p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool)", p0, p1, p2));
    }

    function log(bool p0, bool p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,address)", p0, p1, p2));
    }

    function log(bool p0, address p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256)", p0, p1, p2));
    }

    function log(bool p0, address p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,string)", p0, p1, p2));
    }

    function log(bool p0, address p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,bool)", p0, p1, p2));
    }

    function log(bool p0, address p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,address)", p0, p1, p2));
    }

    function log(address p0, uint256 p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256)", p0, p1, p2));
    }

    function log(address p0, uint256 p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,string)", p0, p1, p2));
    }

    function log(address p0, uint256 p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool)", p0, p1, p2));
    }

    function log(address p0, uint256 p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,address)", p0, p1, p2));
    }

    function log(address p0, string memory p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,uint256)", p0, p1, p2));
    }

    function log(address p0, string memory p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,string)", p0, p1, p2));
    }

    function log(address p0, string memory p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,bool)", p0, p1, p2));
    }

    function log(address p0, string memory p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,address)", p0, p1, p2));
    }

    function log(address p0, bool p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256)", p0, p1, p2));
    }

    function log(address p0, bool p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,string)", p0, p1, p2));
    }

    function log(address p0, bool p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,bool)", p0, p1, p2));
    }

    function log(address p0, bool p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,address)", p0, p1, p2));
    }

    function log(address p0, address p1, uint256 p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,uint256)", p0, p1, p2));
    }

    function log(address p0, address p1, string memory p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,string)", p0, p1, p2));
    }

    function log(address p0, address p1, bool p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,bool)", p0, p1, p2));
    }

    function log(address p0, address p1, address p2) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,address)", p0, p1, p2));
    }

    function log(uint256 p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,uint256,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,string,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,bool,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, uint256 p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,uint256,address,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,uint256,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,string,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,bool,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, string memory p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,string,address,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,uint256,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,string,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,bool,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, bool p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,bool,address,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,uint256,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,string,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,bool,address)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,uint256)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,string)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,bool)", p0, p1, p2, p3));
    }

    function log(uint256 p0, address p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(uint256,address,address,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,uint256,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,string,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,bool,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, uint256 p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,uint256,address,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,uint256,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,string,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,string,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,string,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,string,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,bool,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,bool,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,bool,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,bool,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,address,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,address,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,address,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, string memory p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,string,address,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,uint256,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,string,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,string,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,string,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,string,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,bool,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,address,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,address,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,address,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, bool p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,bool,address,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,uint256,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,string,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,string,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,string,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,string,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,bool,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,bool,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,bool,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,bool,address)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,address,uint256)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,address,string)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,address,bool)", p0, p1, p2, p3));
    }

    function log(string memory p0, address p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(string,address,address,address)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,string)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,uint256,address)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,string)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,string,address)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,string)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,bool,address)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,string)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, uint256 p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,uint256,address,address)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,string)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,uint256,address)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,string,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,string,string)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,string,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,string,address)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,string)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,bool,address)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,address,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,address,string)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,address,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, string memory p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,string,address,address)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,string)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,uint256,address)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,string)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,string,address)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,string)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,bool,address)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,string)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, bool p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,bool,address,address)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,string)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,uint256,address)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,string,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,string,string)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,string,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,string,address)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,string)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,bool,address)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,address,uint256)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,address,string)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,address,bool)", p0, p1, p2, p3));
    }

    function log(bool p0, address p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(bool,address,address,address)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,string)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,bool)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,uint256,address)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,string)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,bool)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,string,address)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,string)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,bool)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,bool,address)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,string)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,bool)", p0, p1, p2, p3));
    }

    function log(address p0, uint256 p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,uint256,address,address)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,string)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,bool)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,uint256,address)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,string,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,string,string)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,string,bool)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,string,address)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,bool,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,bool,string)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,bool,bool)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,bool,address)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,address,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,address,string)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,address,bool)", p0, p1, p2, p3));
    }

    function log(address p0, string memory p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,string,address,address)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,string)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,bool)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,uint256,address)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,string,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,string,string)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,string,bool)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,string,address)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,string)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,bool)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,bool,address)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,address,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,address,string)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,address,bool)", p0, p1, p2, p3));
    }

    function log(address p0, bool p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,bool,address,address)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, uint256 p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, uint256 p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,string)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, uint256 p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,bool)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, uint256 p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,uint256,address)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, string memory p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,string,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, string memory p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,string,string)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, string memory p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,string,bool)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, string memory p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,string,address)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, bool p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,bool,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, bool p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,bool,string)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, bool p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,bool,bool)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, bool p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,bool,address)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, address p2, uint256 p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,address,uint256)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, address p2, string memory p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,address,string)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, address p2, bool p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,address,bool)", p0, p1, p2, p3));
    }

    function log(address p0, address p1, address p2, address p3) internal pure {
        _sendLogPayload(abi.encodeWithSignature("log(address,address,address,address)", p0, p1, p2, p3));
    }

}

// File: libraries/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol



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

pragma solidity ^0.8.19;

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

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

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

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

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

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

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

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

// File: libraries/TransferHelper.sol



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

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

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

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

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

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


// File: libraries/v2-core/contracts/interfaces/IUniswapV2Callee.sol



pragma solidity >=0.5.0;

interface IUniswapV2Callee {
    function uniswapV2Call(address sender, uint amount0, uint amount1, bytes calldata data) external;
}

// File: libraries/v2-core/contracts/interfaces/IUniswapV2Factory.sol



pragma solidity >=0.5.0;

interface IUniswapV2Factory {
    event PairCreated(address indexed token0, address indexed token1, address pair, uint);

    function feeTo() external view returns (address);
    function feeToSetter() external view returns (address);

    function getPair(address tokenA, address tokenB) external view returns (address pair);
    function allPairs(uint) external view returns (address pair);
    function allPairsLength() external view returns (uint);

    function createPair(address tokenA, address tokenB) external returns (address pair);

    function setFeeTo(address) external;
    function setFeeToSetter(address) external;
}

// File: libraries/v2-core/contracts/interfaces/IUniswapV2Pair.sol



pragma solidity >=0.5.0;

interface IUniswapV2Pair {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external pure returns (string memory);
    function symbol() external pure returns (string memory);
    function decimals() external pure returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);

    function DOMAIN_SEPARATOR() external view returns (bytes32);
    function PERMIT_TYPEHASH() external pure returns (bytes32);
    function nonces(address owner) external view returns (uint);

    function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

    event Mint(address indexed sender, uint amount0, uint amount1);
    event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
    event Swap(
        address indexed sender,
        uint amount0In,
        uint amount1In,
        uint amount0Out,
        uint amount1Out,
        address indexed to
    );
    event Sync(uint112 reserve0, uint112 reserve1);

    function MINIMUM_LIQUIDITY() external pure returns (uint);
    function factory() external view returns (address);
    function token0() external view returns (address);
    function token1() external view returns (address);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
    function price0CumulativeLast() external view returns (uint);
    function price1CumulativeLast() external view returns (uint);
    function kLast() external view returns (uint);

    function mint(address to) external returns (uint liquidity);
    function burn(address to) external returns (uint amount0, uint amount1);
    function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
    function skim(address to) external;
    function sync() external;

    function initialize(address, address) external;
}

// File: libraries/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol



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

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

// File: libraries/v3-core/contracts/interfaces/IUniswapV3Factory.sol



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

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

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

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

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

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

    /// @notice Returns the pool address for a given pair of tokens and a fee, or address 0 if it does not exist
    /// @dev tokenA and tokenB may be passed in either token0/token1 or token1/token0 order
    /// @param tokenA The contract address of either token0 or token1
    /// @param tokenB The contract address of the other token
    /// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
    /// @return pool The pool address
    function getPool(
        address tokenA,
        address tokenB,
        uint24 fee
    ) external view returns (address pool);

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

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

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

// File: libraries/v3-core/contracts/interfaces/IUniswapV3Pool.sol



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

import './pool/IUniswapV3PoolImmutables.sol';
import './pool/IUniswapV3PoolState.sol';
import './pool/IUniswapV3PoolDerivedState.sol';
import './pool/IUniswapV3PoolActions.sol';
import './pool/IUniswapV3PoolOwnerActions.sol';
import './pool/IUniswapV3PoolEvents.sol';

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

}

// File: libraries/v3-core/contracts/interfaces/pool/IUniswapV3PoolActions.sol



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

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

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

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

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

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

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

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

// File: libraries/v3-core/contracts/interfaces/pool/IUniswapV3PoolDerivedState.sol



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

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

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

// File: libraries/v3-core/contracts/interfaces/pool/IUniswapV3PoolEvents.sol



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

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

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

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

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

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

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

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

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

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

// File: libraries/v3-core/contracts/interfaces/pool/IUniswapV3PoolImmutables.sol



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

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

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

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

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

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

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

// File: libraries/v3-core/contracts/interfaces/pool/IUniswapV3PoolOwnerActions.sol



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

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

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

// File: libraries/v3-core/contracts/interfaces/pool/IUniswapV3PoolState.sol



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

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

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

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

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

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

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

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

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

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