{
    "Function": "slitherConstructorConstantVariables",
    "File": "src/contracts/middleware/PaymentManager.sol",
    "Parent Contracts": [
        "src/contracts/permissions/Pausable.sol",
        "src/contracts/interfaces/IPausable.sol",
        "src/contracts/interfaces/IPaymentManager.sol",
        "lib/openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "abstract contract PaymentManager is Initializable, IPaymentManager, Pausable {\n    using SafeERC20 for IERC20;\n\n    uint8 constant internal PAUSED_NEW_PAYMENT_COMMIT = 0;\n    uint8 constant internal PAUSED_REDEEM_PAYMENT = 1;\n\n    // DATA STRUCTURES\n\n    /**\n     * @notice Challenge window for submitting fraudproof in the case of an incorrect payment claim by a registered operator.\n     */\n    uint256 public constant paymentFraudproofInterval = 7 days;\n    /// @notice Constant used as a divisor in dealing with BIPS amounts\n    uint256 internal constant MAX_BIPS = 10000;\n    /// @notice Gas budget provided in calls to DelegationTerms contracts\n    uint256 internal constant LOW_LEVEL_GAS_BUDGET = 1e5;\n\n    /**\n     * @notice The global EigenLayer Delegation contract, which is primarily used by\n     * stakers to delegate their stake to operators who serve as middleware nodes.\n     * @dev For more details, see DelegationManager.sol.\n     */\n    IDelegationManager public immutable delegationManager;\n\n    /// @notice The ServiceManager contract for this middleware, where tasks are created / initiated.\n    IServiceManager public immutable serviceManager;\n\n    /// @notice The Registry contract for this middleware, where operators register and deregister.\n    IQuorumRegistry public immutable registry;\n\n    /// @notice the ERC20 token that will be used by the disperser to pay the service fees to middleware nodes.\n    IERC20 public immutable paymentToken;\n\n    /// @notice Token used for placing a guarantee on challenges & payment commits\n    IERC20 public immutable paymentChallengeToken;\n\n    /**\n     * @notice Specifies the payment that has to be made as a guarantee for fraudproof during payment challenges.\n     */\n    uint256 public paymentChallengeAmount;\n\n    /// @notice mapping between the operator and its current committed payment or last redeemed payment\n    mapping(address => Payment) public operatorToPayment;\n\n    /// @notice mapping from operator => PaymentChallenge\n    mapping(address => PaymentChallenge) public operatorToPaymentChallenge;\n\n    /// @notice Deposits of future fees to be drawn against when paying for service from the middleware\n    mapping(address => uint256) public depositsOf;\n\n    /// @notice depositors => addresses approved to spend deposits => allowance\n    mapping(address => mapping(address => uint256)) public allowances;\n\n    // EVENTS\n    /// @notice Emitted when the `paymentChallengeAmount` variable is modified\n    event PaymentChallengeAmountSet(uint256 previousValue, uint256 newValue);\n\n    /// @notice Emitted when an operator commits to a payment by calling the `commitPayment` function\n    event PaymentCommit(address operator, uint32 fromTaskNumber, uint32 toTaskNumber, uint256 fee);\n\n    /// @notice Emitted when a new challenge is created through a call to the `initPaymentChallenge` function\n    event PaymentChallengeInit(address indexed operator, address challenger);\n\n    /// @notice Emitted when an operator redeems a payment by calling the `redeemPayment` function\n    event PaymentRedemption(address indexed operator, uint256 fee);\n\n    /// @notice Emitted when a bisection step is performed in a challenge, through a call to the `performChallengeBisectionStep` function\n    event PaymentBreakdown(\n        address indexed operator, uint32 fromTaskNumber, uint32 toTaskNumber, uint96 amount1, uint96 amount2\n    );\n\n    /// @notice Emitted upon successful resolution of a payment challenge, within a call to `resolveChallenge`\n    event PaymentChallengeResolution(address indexed operator, bool operatorWon);\n\n    /// @dev Emitted when a low-level call to `delegationTerms.payForService` fails, returning `returnData`\n    event OnPayForServiceCallFailure(IDelegationTerms indexed delegationTerms, bytes32 returnData);\n\n    /// @notice when applied to a function, ensures that the function is only callable by the `serviceManager`\n    modifier onlyServiceManager() {\n        require(msg.sender == address(serviceManager), \"onlyServiceManager\");\n        _;\n    }\n\n    /// @notice when applied to a function, ensures that the function is only callable by the `registry`\n    modifier onlyRegistry() {\n        require(msg.sender == address(registry), \"onlyRegistry\");\n        _;\n    }\n\n    /// @notice when applied to a function, ensures that the function is only callable by the owner of the `serviceManager`\n    modifier onlyServiceManagerOwner() {\n        require(msg.sender == serviceManager.owner(), \"onlyServiceManagerOwner\");\n        _;\n    }\n\n    constructor(\n        IDelegationManager _delegationManager,\n        IServiceManager _serviceManager,\n        IQuorumRegistry _registry,\n        IERC20 _paymentToken,\n        IERC20 _paymentChallengeToken\n    ) {\n        delegationManager = _delegationManager;\n        serviceManager = _serviceManager;\n        registry = _registry;\n        paymentToken = _paymentToken;\n        paymentChallengeToken = _paymentChallengeToken;\n        _disableInitializers();\n    }\n\n    function initialize(IPauserRegistry _pauserReg, uint256 _paymentChallengeAmount) public initializer {\n        _initializePauser(_pauserReg, UNPAUSE_ALL);\n        _setPaymentChallengeAmount(_paymentChallengeAmount);\n    }\n\n    /**\n     * @notice deposit one-time fees by the `msg.sender` with this contract to pay for future tasks of this middleware\n     * @param depositFor could be the `msg.sender` themselves, or a different address for whom `msg.sender` is depositing these future fees\n     * @param amount is amount of futures fees being deposited\n     */\n    function depositFutureFees(address depositFor, uint256 amount) external {\n        paymentToken.safeTransferFrom(msg.sender, address(this), amount);\n        depositsOf[depositFor] += amount;\n    }\n\n    /// @notice Allows the `allowed` address to spend up to `amount` of the `msg.sender`'s funds that have been deposited in this contract\n    function setAllowance(address allowed, uint256 amount) external {\n        allowances[msg.sender][allowed] = amount;\n    }\n\n    /**\n     * @notice Modifies the `paymentChallengeAmount` amount.\n     * @param _paymentChallengeAmount The new value for `paymentChallengeAmount` to take.\n     */\n    function setPaymentChallengeAmount(uint256 _paymentChallengeAmount) external virtual onlyServiceManagerOwner {\n        _setPaymentChallengeAmount(_paymentChallengeAmount);\n    }\n\n    /// @notice Used for deducting the fees from the payer to the middleware\n    function takeFee(address initiator, address payer, uint256 feeAmount) external virtual onlyServiceManager {\n        if (initiator != payer) {\n            if (allowances[payer][initiator] != type(uint256).max) {\n                allowances[payer][initiator] -= feeAmount;\n            }\n        }\n\n        // decrement `payer`'s stored deposits\n        depositsOf[payer] -= feeAmount;\n    }\n\n    /**\n     * @notice This is used by an operator to make a claim on the amount that they deserve for their service from their last payment until `toTaskNumber`\n     * @dev Once this payment is recorded, a fraud proof period commences during which a challenger can dispute the proposed payment.\n     */\n    function commitPayment(uint32 toTaskNumber, uint96 amount) external onlyWhenNotPaused(PAUSED_NEW_PAYMENT_COMMIT) {\n        // only active operators can call\n        require(\n            registry.isActiveOperator(msg.sender),\n            \"PaymentManager.commitPayment: Only registered operators can call this function\"\n        );\n\n        require(toTaskNumber <= _taskNumber(), \"PaymentManager.commitPayment: Cannot claim future payments\");\n\n        // can only claim for a payment after redeeming the last payment\n        require(\n            operatorToPayment[msg.sender].status == PaymentStatus.REDEEMED,\n            \"PaymentManager.commitPayment: Require last payment is redeemed\"\n        );\n\n        // operator puts up tokens which can be slashed in case of wrongful payment claim\n        paymentChallengeToken.safeTransferFrom(msg.sender, address(this), paymentChallengeAmount);\n\n        // recording payment claims for the operator\n        uint32 fromTaskNumber;\n\n        // calculate the UTC timestamp at which the payment claim will be optimistically confirmed\n        uint32 confirmAt = uint32(block.timestamp + paymentFraudproofInterval);\n\n        /**\n         * @notice For the special case of this being the first payment that is being claimed by the operator,\n         * the operator must be claiming payment starting from when they registered.\n         */\n        if (operatorToPayment[msg.sender].fromTaskNumber == 0) {\n            // get the taskNumber when the operator registered\n            fromTaskNumber = registry.getFromTaskNumberForOperator(msg.sender);\n        } else {\n            // you have to redeem starting from the last task you previously redeemed up to\n            fromTaskNumber = operatorToPayment[msg.sender].toTaskNumber;\n        }\n\n        require(fromTaskNumber < toTaskNumber, \"invalid payment range\");\n\n        // update the record for the commitment to payment made by the operator\n        operatorToPayment[msg.sender] = Payment(\n            fromTaskNumber,\n            toTaskNumber,\n            confirmAt,\n            amount,\n            // set payment status as 1: committed\n            PaymentStatus.COMMITTED,\n            // storing guarantee amount deposited\n            paymentChallengeAmount\n        );\n\n        emit PaymentCommit(msg.sender, fromTaskNumber, toTaskNumber, amount);\n    }\n\n    /**\n     * @notice Called by an operator to redeem a payment that they previously 'committed' to by calling `commitPayment`.\n     * @dev This function can only be called after the challenge window for the payment claim has completed.\n     */\n    function redeemPayment() external onlyWhenNotPaused(PAUSED_REDEEM_PAYMENT) {\n        // verify that the `msg.sender` has a committed payment\n        require(\n            operatorToPayment[msg.sender].status == PaymentStatus.COMMITTED,\n            \"PaymentManager.redeemPayment: Payment Status is not 'COMMITTED'\"\n        );\n\n        // check that the fraudproof period has already transpired\n        require(\n            block.timestamp > operatorToPayment[msg.sender].confirmAt,\n            \"PaymentManager.redeemPayment: Payment still eligible for fraudproof\"\n        );\n\n        // update the status to show that operator's payment is getting redeemed\n        operatorToPayment[msg.sender].status = PaymentStatus.REDEEMED;\n\n        // Transfer back the challengeAmount to the operator as there was no successful challenge to the payment commitment made by the operator.\n        paymentChallengeToken.safeTransfer(msg.sender, operatorToPayment[msg.sender].challengeAmount);\n\n        // look up payment amount and delegation terms address for the `msg.sender`\n        uint256 amount = operatorToPayment[msg.sender].amount;\n        IDelegationTerms dt = delegationManager.delegationTerms(msg.sender);\n\n        // transfer the amount due in the payment claim of the operator to its delegation terms contract, where the delegators can withdraw their rewards.\n        paymentToken.safeTransfer(address(dt), amount);\n\n        // emit event\n        emit PaymentRedemption(msg.sender, amount);\n\n        // inform the DelegationTerms contract of the payment, which will determine the rewards the operator and its delegators are eligible for\n        _payForServiceHook(dt, amount);\n    }\n\n    // inform the DelegationTerms contract of the payment, which will determine the rewards the operator and its delegators are eligible for\n    function _payForServiceHook(IDelegationTerms dt, uint256 amount) internal {\n        /**\n         * We use low-level call functionality here to ensure that an operator cannot maliciously make this function fail in order to prevent undelegation.\n         * In particular, in-line assembly is also used to prevent the copying of uncapped return data which is also a potential DoS vector.\n         */\n        // format calldata\n        bytes memory lowLevelCalldata = abi.encodeWithSelector(IDelegationTerms.payForService.selector, paymentToken, amount);\n        // Prepare memory for low-level call return data. We accept a max return data length of 32 bytes\n        bool success;\n        bytes32[1] memory returnData;\n        // actually make the call\n        assembly {\n            success := call(\n                // gas provided to this context\n                LOW_LEVEL_GAS_BUDGET,\n                // address to call\n                dt,\n                // value in wei for call\n                0,\n                // memory location to copy for calldata\n                add(lowLevelCalldata, 32),\n                // length of memory to copy for calldata\n                mload(lowLevelCalldata),\n                // memory location to copy return data\n                returnData,\n                // byte size of return data to copy to memory\n                32\n            )\n        }\n        // if the call fails, we emit a special event rather than reverting\n        if (!success) {\n            emit OnPayForServiceCallFailure(dt, returnData[0]);\n        }\n    }\n\n    /**\n     * @notice This function is called by a fraud prover to challenge a payment, initiating an interactive-type fraudproof.\n     * @param operator is the operator against whose payment claim the fraudproof is being made\n     * @param amount1 is the reward amount the challenger in that round claims is for the first half of tasks\n     * @param amount2 is the reward amount the challenger in that round claims is for the second half of tasks\n     *\n     */\n    function initPaymentChallenge(address operator, uint96 amount1, uint96 amount2) external {\n        require(\n            block.timestamp < operatorToPayment[operator].confirmAt\n                && operatorToPayment[operator].status == PaymentStatus.COMMITTED,\n            \"PaymentManager.initPaymentChallenge: Fraudproof interval has passed for payment\"\n        );\n\n        // store challenge details\n        operatorToPaymentChallenge[operator] = PaymentChallenge(\n            operator,\n            msg.sender,\n            address(serviceManager),\n            operatorToPayment[operator].fromTaskNumber,\n            operatorToPayment[operator].toTaskNumber,\n            amount1,\n            amount2,\n            // recording current timestamp plus the fraudproof interval as the `settleAt` timestamp for this challenge\n            uint32(block.timestamp + paymentFraudproofInterval),\n            // set the status for the operator to respond next\n            ChallengeStatus.OPERATOR_TURN\n        );\n\n        // move challengeAmount over\n        uint256 challengeAmount = operatorToPayment[operator].challengeAmount;\n        paymentChallengeToken.safeTransferFrom(msg.sender, address(this), challengeAmount);\n        // update the payment status and reset the fraudproof window for this payment\n        operatorToPayment[operator].status = PaymentStatus.CHALLENGED;\n        operatorToPayment[operator].confirmAt = uint32(block.timestamp + paymentFraudproofInterval);\n        emit PaymentChallengeInit(operator, msg.sender);\n    }\n\n    /**\n     * @notice Perform a single bisection step in an existing interactive payment challenge.\n     * @param operator The middleware operator who was challenged (used to look up challenge details)\n     * @param secondHalf If true, then the caller wishes to challenge the amount claimed as payment in the *second half* of the\n     * previous bisection step. If false then the *first half* is indicated instead.\n     * @param amount1 The amount that the caller asserts the operator is entitled to, for the first half *of the challenged half* of the previous bisection.\n     * @param amount2 The amount that the caller asserts the operator is entitled to, for the second half *of the challenged half* of the previous bisection.\n     */\n    function performChallengeBisectionStep(address operator, bool secondHalf, uint96 amount1, uint96 amount2)\n        external\n    {\n        // copy challenge struct to memory\n        PaymentChallenge memory challenge = operatorToPaymentChallenge[operator];\n\n        ChallengeStatus status = challenge.status;\n\n        require(\n            (status == ChallengeStatus.CHALLENGER_TURN && challenge.challenger == msg.sender)\n                || (status == ChallengeStatus.OPERATOR_TURN && challenge.operator == msg.sender),\n            \"PaymentManager.performChallengeBisectionStep: Must be challenger and their turn or operator and their turn\"\n        );\n\n        require(\n            block.timestamp < challenge.settleAt,\n            \"PaymentManager.performChallengeBisectionStep: Challenge has already settled\"\n        );\n\n        uint32 fromTaskNumber = challenge.fromTaskNumber;\n        uint32 toTaskNumber = challenge.toTaskNumber;\n        uint32 diff = (toTaskNumber - fromTaskNumber) / 2;\n\n        /**\n         * @notice Change the challenged interval to the one the challenger cares about.\n         * If the difference between the current start and end is even, then the new interval has an endpoint halfway in-between\n         * If the difference is odd = 2n + 1, the new interval has a \"from\" endpoint at (start + n = end - (n + 1)) if the second half is challenged,\n         * or a \"to\" endpoint at (end - (2n + 2)/2 = end - (n + 1) = start + n) if the first half is challenged\n         * In other words, it's simple when the difference is even, and when the difference is odd, we just always make the first half the smaller one.\n         */\n        if (secondHalf) {\n            challenge.fromTaskNumber = fromTaskNumber + diff;\n            _updateChallengeAmounts(operator, DissectionType.SECOND_HALF, amount1, amount2);\n        } else {\n            challenge.toTaskNumber = fromTaskNumber + diff;\n            _updateChallengeAmounts(operator, DissectionType.FIRST_HALF, amount1, amount2);\n        }\n\n        // update who must respond next to the challenge\n        _updateStatus(operator, diff);\n\n        // extend the settlement time for the challenge, giving the next participant in the interactive fraudproof `paymentFraudproofInterval` to respond\n        challenge.settleAt = uint32(block.timestamp + paymentFraudproofInterval);\n\n        // update challenge struct in storage\n        operatorToPaymentChallenge[operator] = challenge;\n\n        emit PaymentBreakdown(\n            operator, challenge.fromTaskNumber, challenge.toTaskNumber, challenge.amount1, challenge.amount2\n            );\n    }\n\n    /**\n     * @notice This function is used for updating the status of the challenge in terms of who has to respon\n     * to the interactive challenge mechanism next -  is it going to be challenger or the operator.\n     * @param operator is the operator whose payment claim is being challenged\n     * @param diff is the number of tasks across which payment is being challenged in this iteration\n     * @dev If the challenge is over only one task, then the challenge is marked specially as a one step challenge \u2013\n     * the smallest unit over which a challenge can be proposed \u2013 and 'true' is returned.\n     * Otherwise status is updated normally and 'false' is returned.\n     */\n    function _updateStatus(address operator, uint32 diff) internal returns (bool) {\n        // payment challenge for one task\n        if (diff == 1) {\n            //set to one step turn of either challenger or operator\n            operatorToPaymentChallenge[operator].status =\n                msg.sender == operator\n                ? ChallengeStatus.CHALLENGER_TURN_ONE_STEP\n                : ChallengeStatus.OPERATOR_TURN_ONE_STEP;\n            return false;\n\n        // payment challenge across more than one task\n        } else {\n            // set to dissection turn of either challenger or operator\n            operatorToPaymentChallenge[operator].status =\n                msg.sender == operator ? ChallengeStatus.CHALLENGER_TURN : ChallengeStatus.OPERATOR_TURN;\n            return true;\n        }\n    }\n\n    /// @notice Used to update challenge amounts when the operator (or challenger) breaks down the challenged amount (single bisection step)\n    function _updateChallengeAmounts(address operator, DissectionType dissectionType, uint96 amount1, uint96 amount2)\n        internal\n    {\n        if (dissectionType == DissectionType.FIRST_HALF) {\n            // if first half is challenged, break the first half of the payment into two halves\n            require(\n                amount1 + amount2 != operatorToPaymentChallenge[operator].amount1,\n                \"PaymentManager._updateChallengeAmounts: Invalid amount breakdown\"\n            );\n        } else if (dissectionType == DissectionType.SECOND_HALF) {\n            // if second half is challenged, break the second half of the payment into two halves\n            require(\n                amount1 + amount2 != operatorToPaymentChallenge[operator].amount2,\n                \"PaymentManager._updateChallengeAmounts: Invalid amount breakdown\"\n            );\n        } else {\n            revert(\"PaymentManager._updateChallengeAmounts: invalid DissectionType\");\n        }\n        // update the stored payment halves\n        operatorToPaymentChallenge[operator].amount1 = amount1;\n        operatorToPaymentChallenge[operator].amount2 = amount2;\n    }\n\n    /// @notice resolve an existing PaymentChallenge for an operator\n    function resolveChallenge(address operator) external {\n        // copy challenge struct to memory\n        PaymentChallenge memory challenge = operatorToPaymentChallenge[operator];\n\n        require(\n            block.timestamp > challenge.settleAt,\n            \"PaymentManager.resolveChallenge: challenge has not yet reached settlement time\"\n        );\n        ChallengeStatus status = challenge.status;\n        // if operator did not respond\n        if (status == ChallengeStatus.OPERATOR_TURN || status == ChallengeStatus.OPERATOR_TURN_ONE_STEP) {\n            _resolve(challenge, challenge.challenger);\n            // if challenger did not respond\n        } else if (status == ChallengeStatus.CHALLENGER_TURN || status == ChallengeStatus.CHALLENGER_TURN_ONE_STEP) {\n            _resolve(challenge, challenge.operator);\n        }\n    }\n\n    /**\n     * @notice Resolves a single payment challenge, paying the winner.\n     * @param challenge The challenge that is being resolved.\n     * @param winner Address of the winner of the challenge.\n     * @dev If challenger is proven correct, then they are refunded their own challengeAmount plus the challengeAmount put up by the operator.\n     * If operator is proven correct, then the challenger's challengeAmount is transferred to them, since the operator still hasn't been\n     * proven right, and thus their challengeAmount is still required in case they are challenged again.\n     */\n    function _resolve(PaymentChallenge memory challenge, address winner) internal {\n        address operator = challenge.operator;\n        address challenger = challenge.challenger;\n        if (winner == operator) {\n            // operator was correct, allow for another challenge\n            operatorToPayment[operator].status = PaymentStatus.COMMITTED;\n            operatorToPayment[operator].confirmAt = uint32(block.timestamp + paymentFraudproofInterval);\n            /*\n            * Since the operator hasn't been proved right (only challenger has been proved wrong)\n            * transfer them only challengers challengeAmount, not their own challengeAmount (which is still\n            * locked up in this contract)\n             */\n            paymentChallengeToken.safeTransfer(operator, operatorToPayment[operator].challengeAmount);\n            emit PaymentChallengeResolution(operator, true);\n        } else {\n            // challeger was correct, reset payment\n            operatorToPayment[operator].status = PaymentStatus.REDEEMED;\n            //give them their challengeAmount and the operator's\n            paymentChallengeToken.safeTransfer(challenger, 2 * operatorToPayment[operator].challengeAmount);\n            emit PaymentChallengeResolution(operator, false);\n        }\n    }\n\n    /// @notice Returns the ChallengeStatus for the `operator`'s payment claim.\n    function getChallengeStatus(address operator) external view returns (ChallengeStatus) {\n        return operatorToPaymentChallenge[operator].status;\n    }\n\n    /// @notice Returns the 'amount1' for the `operator`'s payment claim.\n    function getAmount1(address operator) external view returns (uint96) {\n        return operatorToPaymentChallenge[operator].amount1;\n    }\n\n    /// @notice Returns the 'amount2' for the `operator`'s payment claim.\n    function getAmount2(address operator) external view returns (uint96) {\n        return operatorToPaymentChallenge[operator].amount2;\n    }\n\n    /// @notice Returns the 'toTaskNumber' for the `operator`'s payment claim.\n    function getToTaskNumber(address operator) external view returns (uint48) {\n        return operatorToPaymentChallenge[operator].toTaskNumber;\n    }\n\n    /// @notice Returns the 'fromTaskNumber' for the `operator`'s payment claim.\n    function getFromTaskNumber(address operator) external view returns (uint48) {\n        return operatorToPaymentChallenge[operator].fromTaskNumber;\n    }\n\n    /// @notice Returns the task number difference for the `operator`'s payment claim.\n    function getDiff(address operator) external view returns (uint48) {\n        return operatorToPaymentChallenge[operator].toTaskNumber - operatorToPaymentChallenge[operator].fromTaskNumber;\n    }\n\n    /// @notice Returns the active challengeAmount of the `operator` placed on their payment claim.\n    function getPaymentChallengeAmount(address operator) external view returns (uint256) {\n        return operatorToPayment[operator].challengeAmount;\n    }\n\n    /// @notice Convenience function for fetching the current taskNumber from the `serviceManager`\n    function _taskNumber() internal view returns (uint32) {\n        return serviceManager.taskNumber();\n    }\n\n    /**\n     * @notice Modifies the `paymentChallengeAmount` amount.\n     * @param _paymentChallengeAmount The new value for `paymentChallengeAmount` to take.\n     */\n    function _setPaymentChallengeAmount(uint256 _paymentChallengeAmount) internal {\n        emit PaymentChallengeAmountSet(paymentChallengeAmount, _paymentChallengeAmount);\n        paymentChallengeAmount = _paymentChallengeAmount;\n    }\n}"
}