224:        uint256 issuance = LendingTerm(gauge).issuance();
225:        if (issuance != 0) {
226:            uint256 debtCeilingAfterDecrement = LendingTerm(gauge).debtCeiling(-int256(weight));
227:            require(
228:                issuance <= debtCeilingAfterDecrement,
229:                "GuildToken: debt ceiling used"
230:            );
231:        }
232:
233:        super._decrementGaugeWeight(user, gauge, weight);
274:        uint256 gaugeWeight = GuildToken(_guildToken).getGaugeWeight(
275:            address(this)
276:        );
277:        gaugeWeight = uint256(int256(gaugeWeight) + gaugeWeightDelta);
278:        uint256 gaugeType = GuildToken(_guildToken).gaugeType(address(this));
279:        uint256 totalWeight = GuildToken(_guildToken).totalTypeWeight(
280:            gaugeType
281:        );
282:        uint256 creditMinterBuffer = RateLimitedMinter(refs.creditMinter)
283:            .buffer();
284:        uint256 _hardCap = params.hardCap; // cached SLOAD
285:        if (gaugeWeight == 0) {
286:            return 0; // no gauge vote, 0 debt ceiling
287:        } else if (gaugeWeight == totalWeight) {
288:            // one gauge, unlimited debt ceiling
289:            // returns min(hardCap, creditMinterBuffer)
290:            return
291:                _hardCap < creditMinterBuffer ? _hardCap : creditMinterBuffer;
293:        uint256 _issuance = issuance; // cached SLOAD
294:        uint256 totalBorrowedCredit = ProfitManager(refs.profitManager)
295:            .totalBorrowedCredit();
296:        uint256 gaugeWeightTolerance = ProfitManager(refs.profitManager)
297:            .gaugeWeightTolerance();
298:        if (totalBorrowedCredit == 0 && gaugeWeight != 0) {
299:            // first-ever CREDIT mint on a non-zero gauge weight term
300:            // does not check the relative debt ceilings
301:            // returns min(hardCap, creditMinterBuffer)
302:            return
303:                _hardCap < creditMinterBuffer ? _hardCap : creditMinterBuffer;
305:        uint256 toleratedGaugeWeight = (gaugeWeight * gaugeWeightTolerance) /
306:            1e18;
307:        uint256 debtCeilingBefore = (totalBorrowedCredit *
308:            toleratedGaugeWeight) / totalWeight;
309:        if (_issuance >= debtCeilingBefore) {
310:            return debtCeilingBefore; // no more borrows allowed
// use chisel console

// state
uint256 issuance = 2000000000000000000000000;
uint256 totalBorrwedCredit = 2000000000000000000000000;
uint256 tolerance = 1.2e18;
uint256 totalWeight = 2000000000000000000000000

// ideal unstakeAmount = totalWeight * .1666666666...
// solidity does not offer a lot of precision right out of the box so I used python to acquire the necessary precision

uint256 unstakeAmount = 333333333333333333333333;

// calculations
uint256 gaugeWeight = totalWeight - unstakeAmount;
// gaugeWeight == 1666666666666666666666667
uint256 toleratedGaugeWeight = (gaugeWeight * tolerance) / 1e18
// toleratedGaugeWeight == 2000000000000000000000000
toleratedGaugeWeight == totalWeight

uint256 debtCeilingBefore = (totalBorrowedCredit * toleratedGaugeWeight) / totalWeight;
// debtCeilingBefore == 2000000000000000000000000
debtCeilingBefore == issuance
