{
    "Function": "execute",
    "File": "src/automators/AutoExit.sol",
    "Parent Contracts": [
        "src/automators/Automator.sol",
        "lib/openzeppelin-contracts/contracts/access/Ownable.sol",
        "lib/openzeppelin-contracts/contracts/utils/Context.sol",
        "src/utils/Swapper.sol",
        "src/interfaces/IErrors.sol",
        "lib/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol"
    ],
    "High-Level Calls": [
        "INonfungiblePositionManager",
        "IUniswapV3Pool",
        "INonfungiblePositionManager"
    ],
    "Internal Calls": [
        "revert NoLiquidity()",
        "revert NotReady()",
        "_validateSwap",
        "revert ExceedsMaxReward()",
        "revert MissingSwapData()",
        "_validateSwap",
        "_routerSwap",
        "_decreaseFullLiquidityAndCollect",
        "_transferToken",
        "_transferToken",
        "revert Unauthorized()",
        "revert NotConfigured()",
        "_routerSwap",
        "revert LiquidityChanged()",
        "_getPool"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function execute(ExecuteParams calldata params) external {\n        if (!operators[msg.sender]) {\n            revert Unauthorized();\n        }\n\n        ExecuteState memory state;\n        PositionConfig memory config = positionConfigs[params.tokenId];\n\n        if (!config.isActive) {\n            revert NotConfigured();\n        }\n\n        if (\n            config.onlyFees && params.rewardX64 > config.maxRewardX64\n                || !config.onlyFees && params.rewardX64 > config.maxRewardX64\n        ) {\n            revert ExceedsMaxReward();\n        }\n\n        // get position info\n        (,, state.token0, state.token1, state.fee, state.tickLower, state.tickUpper, state.liquidity,,,,) =\n            nonfungiblePositionManager.positions(params.tokenId);\n\n        // so can be executed only once\n        if (state.liquidity == 0) {\n            revert NoLiquidity();\n        }\n        if (state.liquidity != params.liquidity) {\n            revert LiquidityChanged();\n        }\n\n        state.pool = _getPool(state.token0, state.token1, state.fee);\n        (, state.tick,,,,,) = state.pool.slot0();\n\n        // not triggered\n        if (config.token0TriggerTick <= state.tick && state.tick < config.token1TriggerTick) {\n            revert NotReady();\n        }\n\n        state.isAbove = state.tick >= config.token1TriggerTick;\n        state.isSwap = !state.isAbove && config.token0Swap || state.isAbove && config.token1Swap;\n\n        // decrease full liquidity for given position - and return fees as well\n        (state.amount0, state.amount1, state.feeAmount0, state.feeAmount1) = _decreaseFullLiquidityAndCollect(\n            params.tokenId, state.liquidity, params.amountRemoveMin0, params.amountRemoveMin1, params.deadline\n        );\n\n        // swap to other token\n        if (state.isSwap) {\n            if (params.swapData.length == 0) {\n                revert MissingSwapData();\n            }\n\n            // reward is taken before swap - if from fees only\n            if (config.onlyFees) {\n                state.amount0 -= state.feeAmount0 * params.rewardX64 / Q64;\n                state.amount1 -= state.feeAmount1 * params.rewardX64 / Q64;\n            }\n\n            state.swapAmount = state.isAbove ? state.amount1 : state.amount0;\n\n            // checks if price in valid oracle range and calculates amountOutMin\n            (state.amountOutMin,,,) = _validateSwap(\n                !state.isAbove,\n                state.swapAmount,\n                state.pool,\n                TWAPSeconds,\n                maxTWAPTickDifference,\n                state.isAbove ? config.token1SlippageX64 : config.token0SlippageX64\n            );\n\n            (state.amountInDelta, state.amountOutDelta) = _routerSwap(\n                Swapper.RouterSwapParams(\n                    state.isAbove ? IERC20(state.token1) : IERC20(state.token0),\n                    state.isAbove ? IERC20(state.token0) : IERC20(state.token1),\n                    state.swapAmount,\n                    state.amountOutMin,\n                    params.swapData\n                )\n            );\n\n            state.amount0 = state.isAbove ? state.amount0 + state.amountOutDelta : state.amount0 - state.amountInDelta;\n            state.amount1 = state.isAbove ? state.amount1 - state.amountInDelta : state.amount1 + state.amountOutDelta;\n\n            // when swap and !onlyFees - protocol reward is removed only from target token (to incentivize optimal swap done by operator)\n            if (!config.onlyFees) {\n                if (state.isAbove) {\n                    state.amount0 -= state.amount0 * params.rewardX64 / Q64;\n                } else {\n                    state.amount1 -= state.amount1 * params.rewardX64 / Q64;\n                }\n            }\n        } else {\n            // reward is taken as configured\n            state.amount0 -= (config.onlyFees ? state.feeAmount0 : state.amount0) * params.rewardX64 / Q64;\n            state.amount1 -= (config.onlyFees ? state.feeAmount1 : state.amount1) * params.rewardX64 / Q64;\n        }\n\n        state.owner = nonfungiblePositionManager.ownerOf(params.tokenId);\n        if (state.amount0 > 0) {\n            _transferToken(state.owner, IERC20(state.token0), state.amount0, true);\n        }\n        if (state.amount1 > 0) {\n            _transferToken(state.owner, IERC20(state.token1), state.amount1, true);\n        }\n\n        // delete config for position\n        delete positionConfigs[params.tokenId];\n        emit PositionConfigured(params.tokenId, false, false, false, 0, 0, 0, 0, false, 0);\n\n        // log event\n        emit Executed(\n            params.tokenId, msg.sender, state.isSwap, state.amount0, state.amount1, state.token0, state.token1\n        );\n    }"
}