{
    "Function": "_updateBatchShares",
    "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": [],
    "Internal Calls": [
        "_requireBelowMaxSharesRatio"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function _updateBatchShares(\n        uint256 _troveId,\n        address _batchAddress,\n        TroveChange memory _troveChange,\n        uint256 _newTroveDebt, // entire, with interest, batch fee and redistribution\n        uint256 _batchColl, // without trove change\n        uint256 _batchDebt, // entire (with interest, batch fee), but without trove change, nor upfront fee nor redist\n        bool _checkBatchSharesRatio // whether we do the check on the resulting ratio inside the func call\n    ) internal {\n        // Debt\n        uint256 currentBatchDebtShares = batches[_batchAddress].totalDebtShares;\n        uint256 batchDebtSharesDelta;\n        uint256 debtIncrease = _troveChange.debtIncrease +\n            _troveChange.upfrontFee +\n            _troveChange.appliedRedistBoldDebtGain;\n        uint256 debtDecrease;\n        if (debtIncrease > _troveChange.debtDecrease) {\n            debtIncrease -= _troveChange.debtDecrease;\n        } else {\n            debtDecrease = _troveChange.debtDecrease - debtIncrease;\n            debtIncrease = 0;\n        }\n\n        if (debtIncrease == 0 && debtDecrease == 0) {\n            batches[_batchAddress].debt = _batchDebt;\n        } else {\n            if (debtIncrease > 0) {\n                // Add debt\n                if (_batchDebt == 0) {\n                    batchDebtSharesDelta = debtIncrease;\n                } else {\n                    // To avoid rebasing issues, let\u2019s make sure the ratio debt / shares is not too high\n                    _requireBelowMaxSharesRatio(currentBatchDebtShares, _batchDebt, _checkBatchSharesRatio);\n\n                    batchDebtSharesDelta = (currentBatchDebtShares * debtIncrease) / _batchDebt;\n                }\n\n                Troves[_troveId].batchDebtShares += batchDebtSharesDelta;\n                batches[_batchAddress].debt = _batchDebt + debtIncrease;\n                batches[_batchAddress].totalDebtShares = currentBatchDebtShares + batchDebtSharesDelta;\n            } else if (debtDecrease > 0) {\n                // Subtract debt\n                // We make sure that if final trove debt is zero, shares are too (avoiding rounding issues)\n                // This can only happen from redemptions, as otherwise we would be using _removeTroveSharesFromBatch\n                // In redemptions we don\u2019t do that because we don\u2019t want to kick the trove out of the batch (it\u2019d be bad UX)\n                if (_newTroveDebt == 0) {\n                    batches[_batchAddress].debt = _batchDebt - debtDecrease;\n                    batches[_batchAddress].totalDebtShares = currentBatchDebtShares - Troves[_troveId].batchDebtShares;\n                    Troves[_troveId].batchDebtShares = 0;\n                } else {\n                    batchDebtSharesDelta = (currentBatchDebtShares * debtDecrease) / _batchDebt;\n\n                    Troves[_troveId].batchDebtShares -= batchDebtSharesDelta;\n                    batches[_batchAddress].debt = _batchDebt - debtDecrease;\n                    batches[_batchAddress].totalDebtShares = currentBatchDebtShares - batchDebtSharesDelta;\n                }\n            }\n        }\n        // Update debt checkpoint\n        batches[_batchAddress].lastDebtUpdateTime = uint64(block.timestamp);\n\n        // Collateral\n        uint256 collIncrease = _troveChange.collIncrease + _troveChange.appliedRedistCollGain;\n        uint256 collDecrease;\n        if (collIncrease > _troveChange.collDecrease) {\n            collIncrease -= _troveChange.collDecrease;\n        } else {\n            collDecrease = _troveChange.collDecrease - collIncrease;\n            collIncrease = 0;\n        }\n\n        if (collIncrease == 0 && collDecrease == 0) {\n            batches[_batchAddress].coll = _batchColl;\n        } else {\n            if (collIncrease > 0) {\n                // Add coll\n                batches[_batchAddress].coll = _batchColl + collIncrease;\n            } else if (collDecrease > 0) {\n                // Subtract coll\n                batches[_batchAddress].coll = _batchColl - collDecrease;\n            }\n        }\n    }"
}