{
    "Function": "redeemCollateral",
    "File": "contracts/src/CollateralRegistry.sol",
    "Parent Contracts": [
        "contracts/src/Interfaces/ICollateralRegistry.sol",
        "contracts/src/Dependencies/Owned.sol",
        "contracts/src/Interfaces/IOwned.sol"
    ],
    "High-Level Calls": [
        "IBoldToken",
        "ITroveManager",
        "ITroveManager",
        "ITroveManager",
        "ITroveManager",
        "ITroveManager",
        "IBoldToken",
        "ITroveManager"
    ],
    "Internal Calls": [
        "require(bool,string)",
        "_updateBaseRateAndGetRedemptionRate",
        "getTroveManager",
        "_calcRedemptionRate",
        "_requireAmountGreaterThanZero",
        "getTroveManager",
        "getTroveManager",
        "_getUpdatedBaseRateFromRedemption",
        "_requireValidMaxFeePercentage"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function redeemCollateral(\n        uint256 _boldAmount,\n        uint256 _maxIterationsPerCollateral,\n        uint256 _maxFeePercentage\n    ) external {\n        _requireValidMaxFeePercentage(_maxFeePercentage);\n        _requireAmountGreaterThanZero(_boldAmount);\n\n        RedemptionTotals memory totals;\n\n        totals.numCollaterals = totalCollaterals;\n        uint256[] memory unbackedPortions = new uint256[](totals.numCollaterals);\n        uint256[] memory prices = new uint256[](totals.numCollaterals);\n\n        totals.boldSupplyAtStart = boldToken.totalSupply();\n        // Decay the baseRate due to time passed, and then increase it according to the size of this redemption.\n        // Use the saved total Bold supply value, from before it was reduced by the redemption.\n        // We only compute it here, and update it at the end,\n        // because the final redeemed amount may be less than the requested amount\n        // Redeemers should take this into account in order to request the optimal amount to not overpay\n        uint256 redemptionRate = _calcRedemptionRate(\n            _getUpdatedBaseRateFromRedemption(_boldAmount, totals.boldSupplyAtStart)\n        );\n        require(redemptionRate <= _maxFeePercentage, \"CR: Fee exceeded provided maximum\");\n        // Implicit by the above and the _requireValidMaxFeePercentage checks\n        //require(newBaseRate < DECIMAL_PRECISION, \"CR: Fee would eat up all collateral\");\n\n        // Gather and accumulate unbacked portions\n        for (uint256 index = 0; index < totals.numCollaterals; index++) {\n            ITroveManager troveManager = getTroveManager(index);\n            (uint256 unbackedPortion, uint256 price, bool redeemable) = troveManager\n                .getUnbackedPortionPriceAndRedeemability();\n            prices[index] = price;\n            if (redeemable && troveManager.isWhitelisted(msg.sender)) {\n                totals.unbacked += unbackedPortion;\n                unbackedPortions[index] = unbackedPortion;\n            }\n        }\n\n        // There\u2019s an unlikely scenario where all the normally redeemable branches (i.e. having TCR > SCR) have 0 unbacked\n        // In that case, we redeem proportinally to branch size\n        if (totals.unbacked == 0) {\n            unbackedPortions = new uint256[](totals.numCollaterals);\n            for (uint256 index = 0; index < totals.numCollaterals; index++) {\n                ITroveManager troveManager = getTroveManager(index);\n                (, , bool redeemable) = troveManager.getUnbackedPortionPriceAndRedeemability();\n                if (redeemable && troveManager.isWhitelisted(msg.sender)) {\n                    uint256 unbackedPortion = troveManager.getEntireBranchDebt();\n                    totals.unbacked += unbackedPortion;\n                    unbackedPortions[index] = unbackedPortion;\n                }\n            }\n        }\n\n        // Compute redemption amount for each collateral and redeem against the corresponding TroveManager\n        for (uint256 index = 0; index < totals.numCollaterals; index++) {\n            //uint256 unbackedPortion = unbackedPortions[index];\n            if (unbackedPortions[index] > 0) {\n                uint256 redeemAmount = (_boldAmount * unbackedPortions[index]) / totals.unbacked;\n                if (redeemAmount > 0) {\n                    ITroveManager troveManager = getTroveManager(index);\n                    uint256 redeemedAmount = troveManager.redeemCollateral(\n                        msg.sender,\n                        redeemAmount,\n                        prices[index],\n                        redemptionRate,\n                        _maxIterationsPerCollateral\n                    );\n                    totals.redeemedAmount += redeemedAmount;\n                }\n            }\n        }\n\n        _updateBaseRateAndGetRedemptionRate(totals.redeemedAmount, totals.boldSupplyAtStart);\n\n        // Burn the total Bold that is cancelled with debt\n        if (totals.redeemedAmount > 0) {\n            boldToken.burn(msg.sender, totals.redeemedAmount);\n        }\n    }"
}