{
    "Function": "execute",
    "File": "src/transformers/AutoCompound.sol",
    "Parent Contracts": [
        "lib/openzeppelin-contracts/contracts/security/ReentrancyGuard.sol",
        "lib/openzeppelin-contracts/contracts/utils/Multicall.sol",
        "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": [
        "IUniswapV3Pool",
        "INonfungiblePositionManager",
        "INonfungiblePositionManager",
        "INonfungiblePositionManager"
    ],
    "Internal Calls": [
        "_setBalance",
        "_increaseBalance",
        "_poolSwap",
        "_increaseBalance",
        "_getPool",
        "_checkApprovals",
        "_hasMaxTWAPTickDifference",
        "nonReentrant",
        "revert Unauthorized()",
        "_setBalance"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function execute(ExecuteParams calldata params) external nonReentrant {\n        if (!operators[msg.sender] && !vaults[msg.sender]) {\n            revert Unauthorized();\n        }\n        ExecuteState memory state;\n\n        // collect fees - if the position doesn't have operator set or is called from vault - it won't work\n        (state.amount0, state.amount1) = nonfungiblePositionManager.collect(\n            INonfungiblePositionManager.CollectParams(\n                params.tokenId, address(this), type(uint128).max, type(uint128).max\n            )\n        );\n\n        // get position info\n        (,, state.token0, state.token1, state.fee, state.tickLower, state.tickUpper,,,,,) =\n            nonfungiblePositionManager.positions(params.tokenId);\n\n        // add previous balances from given tokens\n        state.amount0 = state.amount0 + positionBalances[params.tokenId][state.token0];\n        state.amount1 = state.amount1 + positionBalances[params.tokenId][state.token1];\n\n        // only if there are balances to work with - start autocompounding process\n        if (state.amount0 > 0 || state.amount1 > 0) {\n            uint256 amountIn = params.amountIn;\n\n            // if a swap is requested - check TWAP oracle\n            if (amountIn > 0) {\n                IUniswapV3Pool pool = _getPool(state.token0, state.token1, state.fee);\n                (state.sqrtPriceX96, state.tick,,,,,) = pool.slot0();\n\n                // how many seconds are needed for TWAP protection\n                uint32 tSecs = TWAPSeconds;\n                if (tSecs > 0) {\n                    if (!_hasMaxTWAPTickDifference(pool, tSecs, state.tick, maxTWAPTickDifference)) {\n                        // if there is no valid TWAP - disable swap\n                        amountIn = 0;\n                    }\n                }\n                // if still needed - do swap\n                if (amountIn > 0) {\n                    // no slippage check done - because protected by TWAP check\n                    (state.amountInDelta, state.amountOutDelta) = _poolSwap(\n                        Swapper.PoolSwapParams(\n                            pool, IERC20(state.token0), IERC20(state.token1), state.fee, params.swap0To1, amountIn, 0\n                        )\n                    );\n                    state.amount0 =\n                        params.swap0To1 ? state.amount0 - state.amountInDelta : state.amount0 + state.amountOutDelta;\n                    state.amount1 =\n                        params.swap0To1 ? state.amount1 + state.amountOutDelta : state.amount1 - state.amountInDelta;\n                }\n            }\n\n            uint256 rewardX64 = totalRewardX64;\n\n            state.maxAddAmount0 = state.amount0 * Q64 / (rewardX64 + Q64);\n            state.maxAddAmount1 = state.amount1 * Q64 / (rewardX64 + Q64);\n\n            // deposit liquidity into tokenId\n            if (state.maxAddAmount0 > 0 || state.maxAddAmount1 > 0) {\n                _checkApprovals(state.token0, state.token1);\n\n                (, state.compounded0, state.compounded1) = nonfungiblePositionManager.increaseLiquidity(\n                    INonfungiblePositionManager.IncreaseLiquidityParams(\n                        params.tokenId, state.maxAddAmount0, state.maxAddAmount1, 0, 0, block.timestamp\n                    )\n                );\n\n                // fees are always calculated based on added amount (to incentivize optimal swap)\n                state.amount0Fees = state.compounded0 * rewardX64 / Q64;\n                state.amount1Fees = state.compounded1 * rewardX64 / Q64;\n            }\n\n            // calculate remaining tokens for owner\n            _setBalance(params.tokenId, state.token0, state.amount0 - state.compounded0 - state.amount0Fees);\n            _setBalance(params.tokenId, state.token1, state.amount1 - state.compounded1 - state.amount1Fees);\n\n            // add reward to protocol balance (token 0)\n            _increaseBalance(0, state.token0, state.amount0Fees);\n            _increaseBalance(0, state.token1, state.amount1Fees);\n        }\n\n        emit AutoCompounded(\n            msg.sender,\n            params.tokenId,\n            state.compounded0,\n            state.compounded1,\n            state.amount0Fees,\n            state.amount1Fees,\n            state.token0,\n            state.token1\n        );\n    }"
}