{
    "Function": "_settleProtocolDebt",
    "File": "contracts/managers/SherlockProtocolManager.sol",
    "Parent Contracts": [
        "contracts/managers/Manager.sol",
        "node_modules/@openzeppelin/contracts/security/Pausable.sol",
        "node_modules/@openzeppelin/contracts/access/Ownable.sol",
        "node_modules/@openzeppelin/contracts/utils/Context.sol",
        "contracts/interfaces/managers/ISherlockProtocolManager.sol",
        "contracts/interfaces/managers/IManager.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [
        "_settleTotalDebt",
        "_calcIncrementalProtocolDebt"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function _settleProtocolDebt(bytes32 _protocol) internal returns (uint256 _nonStakerPercentage) {\n    // This calcs the accrued debt of the protocol since it was last updated\n    uint256 debt = _calcIncrementalProtocolDebt(_protocol);\n    // This pulls the percentage that is sent to nonstakers\n    _nonStakerPercentage = nonStakersPercentage[_protocol];\n    // In case the protocol has accrued debt, this code block will ensure the debt is settled properly\n    if (debt != 0) {\n      // Pulls the stored active balance of the protocol\n      uint256 balance = activeBalances[_protocol];\n      // This is the start of handling an edge case where arbitragers don't remove this protocol before debt becomes greater than active balance\n      // Economically speaking, this point should never be reached as arbs will get rewarded for removing the protocol before this point\n      // The arb would use forceRemoveByActiveBalance and forceRemoveBySecondsOfCoverage\n      // However, if arbs don't come in, the premium for this protocol should be set to 0 asap otherwise accounting for stakers/nonstakers gets messed up\n      if (debt > balance) {\n        // This error amount represents the magnitude of the mistake\n        uint256 error = debt - balance;\n        // Gets the latest value of claimable premiums for stakers\n        _settleTotalDebt();\n        // @note to production, set premium first to zero before solving accounting issue.\n        // otherwise the accounting error keeps increasing\n        uint256 lastClaimablePremiumsForStakers_ = lastClaimablePremiumsForStakers;\n\n        // Figures out the amount due to stakers by subtracting the nonstaker percentage from 100%\n        uint256 claimablePremiumError = ((HUNDRED_PERCENT - _nonStakerPercentage) * error) /\n          HUNDRED_PERCENT;\n\n        // This insufficient tokens var is simply how we know (emitted as an event) how many tokens the protocol is short\n        uint256 insufficientTokens;\n\n        // The idea here is that lastClaimablePremiumsForStakers has gotten too big accidentally\n        // We need to decrease the balance of lastClaimablePremiumsForStakers by the amount that was added in error\n        // This first line can be true if claimPremiumsForStakers() has been called and\n        // lastClaimablePremiumsForStakers would be 0 but a faulty protocol could cause claimablePremiumError to be >0 still\n        if (claimablePremiumError > lastClaimablePremiumsForStakers_) {\n          insufficientTokens = claimablePremiumError - lastClaimablePremiumsForStakers_;\n          lastClaimablePremiumsForStakers = 0;\n        } else {\n          // If the error is not bigger than the claimable premiums, then we just decrease claimable premiums\n          // By the amount that was added in error (error) and insufficientTokens = 0\n          lastClaimablePremiumsForStakers =\n            lastClaimablePremiumsForStakers_ -\n            claimablePremiumError;\n        }\n\n        // If two events are thrown, the values need to be summed up for the actual state.\n        // This means an error of this type will continue until it is handled\n        emit AccountingError(_protocol, claimablePremiumError, insufficientTokens);\n        // We set the debt equal to the balance, and in the next line we effectively set the protocol's active balance to 0 in this case\n        debt = balance;\n      }\n      // Subtracts the accrued debt (since last update) from the protocol's active balance and updates active balance\n      activeBalances[_protocol] = balance - debt;\n      // Adds the requisite amount of the debt to the balance claimable by nonstakers for this protocol\n      nonStakersClaimableByProtocol[_protocol] += (_nonStakerPercentage * debt) / HUNDRED_PERCENT;\n    }\n    // Updates the last accounted timestamp for this protocol\n    lastAccountedEachProtocol[_protocol] = block.timestamp;\n  }"
}