{
    "Function": "redeemCollateral",
    "File": "contracts/src/TroveManager.sol",
    "Parent Contracts": [
        "contracts/src/Interfaces/ITroveEvents.sol",
        "contracts/src/Interfaces/ITroveManager.sol",
        "contracts/src/Dependencies/LiquityBase.sol",
        "contracts/src/Interfaces/ILiquityBase.sol"
    ],
    "High-Level Calls": [
        "IActivePool",
        "ISortedTroves",
        "ISortedTroves",
        "ISortedTroves",
        "IActivePool"
    ],
    "Internal Calls": [
        "_getBatchManager",
        "_updateBatchInterestPriorToRedemption",
        "_requireCallerIsCollateralRegistry",
        "getCurrentICR",
        "_redeemCollateralFromTrove"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function redeemCollateral(\n        address _redeemer,\n        uint256 _boldamount,\n        uint256 _price,\n        uint256 _redemptionRate,\n        uint256 _maxIterations\n    ) external override returns (uint256 _redemeedAmount) {\n        _requireCallerIsCollateralRegistry();\n\n        IActivePool activePoolCached = activePool;\n        ISortedTroves sortedTrovesCached = sortedTroves;\n\n        TroveChange memory totalsTroveChange;\n        uint256 totalCollFee;\n\n        uint256 remainingBold = _boldamount;\n\n        SingleRedemptionValues memory singleRedemption;\n        // Let\u2019s check if there\u2019s a pending zombie trove from previous redemption\n        if (lastZombieTroveId != 0) {\n            singleRedemption.troveId = lastZombieTroveId;\n            singleRedemption.isZombieTrove = true;\n        } else {\n            singleRedemption.troveId = sortedTrovesCached.getLast();\n        }\n        address lastBatchUpdatedInterest = address(0);\n\n        // Loop through the Troves starting from the one with lowest interest rate until _amount of Bold is exchanged for collateral\n        if (_maxIterations == 0) _maxIterations = type(uint256).max;\n        while (singleRedemption.troveId != 0 && remainingBold > 0 && _maxIterations > 0) {\n            _maxIterations--;\n            // Save the uint256 of the Trove preceding the current one\n            uint256 nextUserToCheck;\n            if (singleRedemption.isZombieTrove) {\n                nextUserToCheck = sortedTrovesCached.getLast();\n            } else {\n                nextUserToCheck = sortedTrovesCached.getPrev(singleRedemption.troveId);\n            }\n\n            // Skip if ICR < 100%, to make sure that redemptions don\u2019t decrease the CR of hit Troves\n            if (getCurrentICR(singleRedemption.troveId, _price) < _100pct) {\n                singleRedemption.troveId = nextUserToCheck;\n                singleRedemption.isZombieTrove = false;\n                continue;\n            }\n\n            // If it\u2019s in a batch, we need to update interest first\n            // We do it here outside, to avoid repeating for each trove in the same batch\n            singleRedemption.batchAddress = _getBatchManager(singleRedemption.troveId);\n            if (\n                singleRedemption.batchAddress != address(0) && singleRedemption.batchAddress != lastBatchUpdatedInterest\n            ) {\n                _updateBatchInterestPriorToRedemption(activePoolCached, singleRedemption.batchAddress);\n                lastBatchUpdatedInterest = singleRedemption.batchAddress;\n            }\n\n            _redeemCollateralFromTrove(defaultPool, singleRedemption, remainingBold, _price, _redemptionRate);\n\n            totalsTroveChange.collDecrease += singleRedemption.collLot;\n            totalsTroveChange.debtDecrease += singleRedemption.boldLot;\n            totalsTroveChange.appliedRedistBoldDebtGain += singleRedemption.appliedRedistBoldDebtGain;\n            // For recorded and weighted recorded debt totals, we need to capture the increases and decreases,\n            // since the net debt change for a given Trove could be positive or negative: redemptions decrease a Trove's recorded\n            // (and weighted recorded) debt, but the accrued interest increases it.\n            totalsTroveChange.newWeightedRecordedDebt += singleRedemption.newWeightedRecordedDebt;\n            totalsTroveChange.oldWeightedRecordedDebt += singleRedemption.oldWeightedRecordedDebt;\n            totalCollFee += singleRedemption.collFee;\n\n            remainingBold -= singleRedemption.boldLot;\n            singleRedemption.troveId = nextUserToCheck;\n            singleRedemption.isZombieTrove = false;\n        }\n\n        // We are removing this condition to prevent blocking redemptions\n        //require(totals.totalCollDrawn > 0, \"TroveManager: Unable to redeem any amount\");\n\n        emit Redemption(\n            _boldamount,\n            totalsTroveChange.debtDecrease,\n            totalsTroveChange.collDecrease,\n            totalCollFee,\n            _price\n        );\n\n        activePoolCached.mintAggInterestAndAccountForTroveChange(totalsTroveChange, address(0));\n\n        // Send the redeemed Coll to sender\n        activePoolCached.sendColl(_redeemer, totalsTroveChange.collDecrease);\n        // We\u2019ll burn all the Bold together out in the CollateralRegistry, to save gas\n\n        return totalsTroveChange.debtDecrease;\n    }"
}