Sonic Blaze Testnet

Contract

0xeDa79f7F84aE971bF28c501cE3e56847f8BB1BC6

Overview

S Balance

Sonic Blaze LogoSonic Blaze LogoSonic Blaze Logo0 S

Multichain Info

No addresses found
Transaction Hash
Method
Block
From
To

There are no matching entries

Please try again later

Latest 21 internal transactions

Parent Transaction Hash Block From To
43778002024-12-15 21:11:143 days ago1734297074
0xeDa79f7F...7f8BB1BC6
0 S
43778002024-12-15 21:11:143 days ago1734297074
0xeDa79f7F...7f8BB1BC6
0 S
43778002024-12-15 21:11:143 days ago1734297074
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200
0xeDa79f7F...7f8BB1BC6
0 S
43749682024-12-15 20:56:403 days ago1734296200  Contract Creation0 S
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
TroveManager

Compiler Version
v0.8.24+commit.e11b9ed9

Optimization Enabled:
Yes with 200 runs

Other Settings:
cancun EvmVersion

Contract Source Code (Solidity Standard Json-Input format)

File 1 of 44 : TroveManager.sol
// SPDX-License-Identifier: BUSL-1.1

pragma solidity 0.8.24;

import "./Interfaces/ITroveManager.sol";
import "./Interfaces/IAddressesRegistry.sol";
import "./Interfaces/IStabilityPool.sol";
import "./Interfaces/ICollSurplusPool.sol";
import "./Interfaces/IBoldToken.sol";
import "./Interfaces/ISortedTroves.sol";
import "./Interfaces/ITroveEvents.sol";
import "./Interfaces/ITroveNFT.sol";
import "./Interfaces/ICollateralRegistry.sol";
import "./Interfaces/IWETH.sol";
import "./Dependencies/LiquityBase.sol";

contract TroveManager is LiquityBase, ITroveManager, ITroveEvents {
    // --- Connected contract declarations ---

    ITroveNFT public troveNFT;
    IBorrowerOperations public borrowerOperations;
    IStabilityPool public stabilityPool;
    address internal gasPoolAddress;
    ICollSurplusPool internal collSurplusPool;
    IBoldToken internal boldToken;
    // A doubly linked list of Troves, sorted by their interest rate
    ISortedTroves public sortedTroves;
    ICollateralRegistry internal collateralRegistry;
    // Wrapped ETH for liquidation reserve (gas compensation)
    IWETH internal immutable WETH;

    // Critical system collateral ratio. If the system's total collateral ratio (TCR) falls below the CCR, some borrowing operation restrictions are applied
    uint256 public immutable CCR;

    // Minimum collateral ratio for individual troves
    uint256 internal immutable MCR;
    // Shutdown system collateral ratio. If the system's total collateral ratio (TCR) for a given collateral falls below the SCR,
    // the protocol triggers the shutdown of the borrow market and permanently disables all borrowing operations except for closing Troves.
    uint256 internal immutable SCR;

    // Liquidation penalty for troves offset to the SP
    uint256 internal immutable LIQUIDATION_PENALTY_SP;
    // Liquidation penalty for troves redistributed
    uint256 internal immutable LIQUIDATION_PENALTY_REDISTRIBUTION;

    // --- Data structures ---

    // Store the necessary data for a trove
    struct Trove {
        uint256 debt;
        uint256 coll;
        uint256 stake;
        Status status;
        uint64 arrayIndex;
        uint64 lastDebtUpdateTime;
        uint64 lastInterestRateAdjTime;
        uint256 annualInterestRate;
        address interestBatchManager;
        uint256 batchDebtShares;
    }

    mapping(uint256 => Trove) public Troves;

    // Store the necessary data for an interest batch manager. We treat each batch as a “big trove”.
    // Each trove has a share of the debt of the global batch. Collateral is stored per trove (as CRs are different)
    // Still the total amount of batch collateral is stored for informational purposes
    struct Batch {
        uint256 debt;
        uint256 coll;
        uint64 arrayIndex;
        uint64 lastDebtUpdateTime;
        uint64 lastInterestRateAdjTime;
        uint256 annualInterestRate;
        uint256 annualManagementFee;
        uint256 totalDebtShares;
    }

    mapping(address => Batch) internal batches;

    uint256 internal totalStakes;

    // Snapshot of the value of totalStakes, taken immediately after the latest liquidation
    uint256 internal totalStakesSnapshot;

    // Snapshot of the total collateral across the ActivePool and DefaultPool, immediately after the latest liquidation.
    uint256 internal totalCollateralSnapshot;

    /*
    * L_coll and L_boldDebt track the sums of accumulated liquidation rewards per unit staked. During its lifetime, each stake earns:
    *
    * An Coll gain of ( stake * [L_coll - L_coll(0)] )
    * A boldDebt increase  of ( stake * [L_boldDebt - L_boldDebt(0)] )
    *
    * Where L_coll(0) and L_boldDebt(0) are snapshots of L_coll and L_boldDebt for the active Trove taken at the instant the stake was made
    */
    uint256 internal L_coll;
    uint256 internal L_boldDebt;

    // Map active troves to their RewardSnapshot
    mapping(uint256 => RewardSnapshot) public rewardSnapshots;

    // Object containing the Coll and Bold snapshots for a given active trove
    struct RewardSnapshot {
        uint256 coll;
        uint256 boldDebt;
    }

    // Array of all active trove addresses - used to compute an approximate hint off-chain, for the sorted list insertion
    uint256[] internal TroveIds;
    // Array of all batch managers - used to fetch them off-chain
    address[] public batchIds;

    uint256 public lastZombieTroveId;

    // Error trackers for the trove redistribution calculation
    uint256 internal lastCollError_Redistribution;
    uint256 internal lastBoldDebtError_Redistribution;

    // Timestamp at which branch was shut down. 0 if not shut down.
    uint256 public shutdownTime;

    /*
    * --- Variable container structs for liquidations ---
    *
    * These structs are used to hold, return and assign variables inside the liquidation functions,
    * in order to avoid the error: "CompilerError: Stack too deep".
    **/

    struct LiquidationValues {
        uint256 collGasCompensation;
        uint256 debtToOffset;
        uint256 collToSendToSP;
        uint256 debtToRedistribute;
        uint256 collToRedistribute;
        uint256 collSurplus;
        uint256 ETHGasCompensation;
        uint256 oldWeightedRecordedDebt;
        uint256 newWeightedRecordedDebt;
    }

    // --- Variable container structs for redemptions ---

    struct SingleRedemptionValues {
        uint256 troveId;
        address batchAddress;
        uint256 boldLot;
        uint256 collLot;
        uint256 collFee;
        uint256 appliedRedistBoldDebtGain;
        uint256 oldWeightedRecordedDebt;
        uint256 newWeightedRecordedDebt;
        uint256 newStake;
        bool isZombieTrove;
        LatestTroveData trove;
        LatestBatchData batch;
    }

    // --- Errors ---

    error EmptyData();
    error NothingToLiquidate();
    error CallerNotBorrowerOperations();
    error CallerNotCollateralRegistry();
    error OnlyOneTroveLeft();
    error NotShutDown();
    error ZeroAmount();
    error NotEnoughBoldBalance();
    error MinCollNotReached(uint256 _coll);
    error BatchSharesRatioTooHigh();

    // --- Events ---

    event TroveNFTAddressChanged(address _newTroveNFTAddress);
    event BorrowerOperationsAddressChanged(address _newBorrowerOperationsAddress);
    event BoldTokenAddressChanged(address _newBoldTokenAddress);
    event StabilityPoolAddressChanged(address _stabilityPoolAddress);
    event GasPoolAddressChanged(address _gasPoolAddress);
    event CollSurplusPoolAddressChanged(address _collSurplusPoolAddress);
    event SortedTrovesAddressChanged(address _sortedTrovesAddress);
    event CollateralRegistryAddressChanged(address _collateralRegistryAddress);

    constructor(IAddressesRegistry _addressesRegistry) LiquityBase(_addressesRegistry) {
        CCR = _addressesRegistry.CCR();
        MCR = _addressesRegistry.MCR();
        SCR = _addressesRegistry.SCR();
        LIQUIDATION_PENALTY_SP = _addressesRegistry.LIQUIDATION_PENALTY_SP();
        LIQUIDATION_PENALTY_REDISTRIBUTION = _addressesRegistry.LIQUIDATION_PENALTY_REDISTRIBUTION();

        troveNFT = _addressesRegistry.troveNFT();
        borrowerOperations = _addressesRegistry.borrowerOperations();
        stabilityPool = _addressesRegistry.stabilityPool();
        gasPoolAddress = _addressesRegistry.gasPoolAddress();
        collSurplusPool = _addressesRegistry.collSurplusPool();
        boldToken = _addressesRegistry.boldToken();
        sortedTroves = _addressesRegistry.sortedTroves();
        WETH = _addressesRegistry.WETH();
        collateralRegistry = _addressesRegistry.collateralRegistry();

        emit TroveNFTAddressChanged(address(troveNFT));
        emit BorrowerOperationsAddressChanged(address(borrowerOperations));
        emit StabilityPoolAddressChanged(address(stabilityPool));
        emit GasPoolAddressChanged(gasPoolAddress);
        emit CollSurplusPoolAddressChanged(address(collSurplusPool));
        emit BoldTokenAddressChanged(address(boldToken));
        emit SortedTrovesAddressChanged(address(sortedTroves));
        emit CollateralRegistryAddressChanged(address(collateralRegistry));
    }

    // --- Getters ---

    function getTroveIdsCount() external view override returns (uint256) {
        return TroveIds.length;
    }

    function getTroveFromTroveIdsArray(uint256 _index) external view override returns (uint256) {
        return TroveIds[_index];
    }

    // --- Trove Liquidation functions ---

    // --- Inner single liquidation functions ---

    // Liquidate one trove
    function _liquidate(
        IDefaultPool _defaultPool,
        uint256 _troveId,
        uint256 _boldInStabPool,
        uint256 _price,
        LatestTroveData memory trove,
        LiquidationValues memory singleLiquidation
    ) internal {
        address owner = troveNFT.ownerOf(_troveId);

        _getLatestTroveData(_troveId, trove);
        address batchAddress = _getBatchManager(_troveId);
        bool isTroveInBatch = batchAddress != address(0);
        LatestBatchData memory batch;
        if (isTroveInBatch) _getLatestBatchData(batchAddress, batch);

        _movePendingTroveRewardsToActivePool(_defaultPool, trove.redistBoldDebtGain, trove.redistCollGain);

        singleLiquidation.collGasCompensation = _getCollGasCompensation(trove.entireColl);
        uint256 collToLiquidate = trove.entireColl - singleLiquidation.collGasCompensation;

        (
            singleLiquidation.debtToOffset,
            singleLiquidation.collToSendToSP,
            singleLiquidation.debtToRedistribute,
            singleLiquidation.collToRedistribute,
            singleLiquidation.collSurplus
        ) = _getOffsetAndRedistributionVals(trove.entireDebt, collToLiquidate, _boldInStabPool, _price);

        TroveChange memory troveChange;
        troveChange.collDecrease = trove.entireColl;
        troveChange.debtDecrease = trove.entireDebt;
        troveChange.appliedRedistCollGain = trove.redistCollGain;
        troveChange.appliedRedistBoldDebtGain = trove.redistBoldDebtGain;
        _closeTrove(
            _troveId,
            troveChange,
            batchAddress,
            batch.entireCollWithoutRedistribution,
            batch.entireDebtWithoutRedistribution,
            Status.closedByLiquidation
        );

        if (isTroveInBatch) {
            singleLiquidation.oldWeightedRecordedDebt =
                batch.weightedRecordedDebt + (trove.entireDebt - trove.redistBoldDebtGain) * batch.annualInterestRate;
            singleLiquidation.newWeightedRecordedDebt = batch.entireDebtWithoutRedistribution * batch.annualInterestRate;
            // Mint batch management fee
            troveChange.batchAccruedManagementFee = batch.accruedManagementFee;
            troveChange.oldWeightedRecordedBatchManagementFee = batch.weightedRecordedBatchManagementFee
                + (trove.entireDebt - trove.redistBoldDebtGain) * batch.annualManagementFee;
            troveChange.newWeightedRecordedBatchManagementFee =
                batch.entireDebtWithoutRedistribution * batch.annualManagementFee;
            activePool.mintBatchManagementFeeAndAccountForChange(troveChange, batchAddress);
        } else {
            singleLiquidation.oldWeightedRecordedDebt = trove.weightedRecordedDebt;
        }

        // Differencen between liquidation penalty and liquidation threshold
        if (singleLiquidation.collSurplus > 0) {
            collSurplusPool.accountSurplus(owner, singleLiquidation.collSurplus);
        }

        // Wipe out state in BO
        borrowerOperations.onLiquidateTrove(_troveId);

        emit TroveUpdated({
            _troveId: _troveId,
            _debt: 0,
            _coll: 0,
            _stake: 0,
            _annualInterestRate: 0,
            _snapshotOfTotalCollRedist: 0,
            _snapshotOfTotalDebtRedist: 0
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.liquidate,
            _annualInterestRate: 0,
            _debtIncreaseFromRedist: trove.redistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: 0,
            _debtChangeFromOperation: -int256(trove.entireDebt),
            _collIncreaseFromRedist: trove.redistCollGain,
            _collChangeFromOperation: -int256(trove.entireColl)
        });

        if (isTroveInBatch) {
            emit BatchUpdated({
                _interestBatchManager: batchAddress,
                _operation: BatchOperation.exitBatch,
                _debt: batches[batchAddress].debt,
                _coll: batches[batchAddress].coll,
                _annualInterestRate: batch.annualInterestRate,
                _annualManagementFee: batch.annualManagementFee,
                _totalDebtShares: batches[batchAddress].totalDebtShares,
                _debtIncreaseFromUpfrontFee: 0
            });
        }
    }

    // Return the amount of Coll to be drawn from a trove's collateral and sent as gas compensation.
    function _getCollGasCompensation(uint256 _entireColl) internal pure returns (uint256) {
        return LiquityMath._min(_entireColl / COLL_GAS_COMPENSATION_DIVISOR, COLL_GAS_COMPENSATION_CAP);
    }

    /* In a full liquidation, returns the values for a trove's coll and debt to be offset, and coll and debt to be
    * redistributed to active troves.
    */
    function _getOffsetAndRedistributionVals(
        uint256 _entireTroveDebt,
        uint256 _collToLiquidate, // gas compensation is already subtracted
        uint256 _boldInStabPool,
        uint256 _price
    )
        internal
        view
        returns (
            uint256 debtToOffset,
            uint256 collToSendToSP,
            uint256 debtToRedistribute,
            uint256 collToRedistribute,
            uint256 collSurplus
        )
    {
        uint256 collSPPortion;
        /*
         * Offset as much debt & collateral as possible against the Stability Pool, and redistribute the remainder
         * between all active troves.
         *
         *  If the trove's debt is larger than the deposited Bold in the Stability Pool:
         *
         *  - Offset an amount of the trove's debt equal to the Bold in the Stability Pool
         *  - Send a fraction of the trove's collateral to the Stability Pool, equal to the fraction of its offset debt
         *
         */
        if (_boldInStabPool > 0) {
            debtToOffset = LiquityMath._min(_entireTroveDebt, _boldInStabPool);
            collSPPortion = _collToLiquidate * debtToOffset / _entireTroveDebt;
            (collToSendToSP, collSurplus) =
                _getCollPenaltyAndSurplus(collSPPortion, debtToOffset, LIQUIDATION_PENALTY_SP, _price);
        }

        // Redistribution
        debtToRedistribute = _entireTroveDebt - debtToOffset;
        if (debtToRedistribute > 0) {
            uint256 collRedistributionPortion = _collToLiquidate - collSPPortion;
            if (collRedistributionPortion > 0) {
                (collToRedistribute, collSurplus) = _getCollPenaltyAndSurplus(
                    collRedistributionPortion + collSurplus, // Coll surplus from offset can be eaten up by red. penalty
                    debtToRedistribute,
                    LIQUIDATION_PENALTY_REDISTRIBUTION, // _penaltyRatio
                    _price
                );
            }
        }
        // assert(_collToLiquidate == collToSendToSP + collToRedistribute + collSurplus);
    }

    function _getCollPenaltyAndSurplus(
        uint256 _collToLiquidate,
        uint256 _debtToLiquidate,
        uint256 _penaltyRatio,
        uint256 _price
    ) internal pure returns (uint256 seizedColl, uint256 collSurplus) {
        uint256 maxSeizedColl = _debtToLiquidate * (DECIMAL_PRECISION + _penaltyRatio) / _price;
        if (_collToLiquidate > maxSeizedColl) {
            seizedColl = maxSeizedColl;
            collSurplus = _collToLiquidate - maxSeizedColl;
        } else {
            seizedColl = _collToLiquidate;
            collSurplus = 0;
        }
    }

    /*
     * Attempt to liquidate a custom list of troves provided by the caller.
     */
    function batchLiquidateTroves(uint256[] memory _troveArray) public override {
        if (_troveArray.length == 0) {
            revert EmptyData();
        }

        IActivePool activePoolCached = activePool;
        IDefaultPool defaultPoolCached = defaultPool;
        IStabilityPool stabilityPoolCached = stabilityPool;

        TroveChange memory troveChange;
        LiquidationValues memory totals;

        (uint256 price,) = priceFeed.fetchPrice();
        uint256 boldInStabPool = stabilityPoolCached.getTotalBoldDeposits();

        // Perform the appropriate liquidation sequence - tally values and obtain their totals.
        _batchLiquidateTroves(defaultPoolCached, price, boldInStabPool, _troveArray, totals, troveChange);

        if (troveChange.debtDecrease == 0) {
            revert NothingToLiquidate();
        }

        activePoolCached.mintAggInterestAndAccountForTroveChange(troveChange, address(0));

        // Move liquidated Coll and Bold to the appropriate pools
        if (totals.debtToOffset > 0 || totals.collToSendToSP > 0) {
            stabilityPoolCached.offset(totals.debtToOffset, totals.collToSendToSP);
        }
        // we check amount is not zero inside
        _redistributeDebtAndColl(
            activePoolCached, defaultPoolCached, totals.debtToRedistribute, totals.collToRedistribute
        );
        if (totals.collSurplus > 0) {
            activePoolCached.sendColl(address(collSurplusPool), totals.collSurplus);
        }

        // Update system snapshots
        _updateSystemSnapshots_excludeCollRemainder(activePoolCached, totals.collGasCompensation);

        emit Liquidation(
            totals.debtToOffset,
            totals.debtToRedistribute,
            totals.ETHGasCompensation,
            totals.collGasCompensation,
            totals.collToSendToSP,
            totals.collToRedistribute,
            totals.collSurplus,
            L_coll,
            L_boldDebt,
            price
        );

        // Send gas compensation to caller
        _sendGasCompensation(activePoolCached, msg.sender, totals.ETHGasCompensation, totals.collGasCompensation);
    }

    function _isActiveOrZombie(Status _status) internal pure returns (bool) {
        return _status == Status.active || _status == Status.zombie;
    }

    function _batchLiquidateTroves(
        IDefaultPool _defaultPool,
        uint256 _price,
        uint256 _boldInStabPool,
        uint256[] memory _troveArray,
        LiquidationValues memory totals,
        TroveChange memory troveChange
    ) internal {
        uint256 remainingBoldInStabPool = _boldInStabPool;

        for (uint256 i = 0; i < _troveArray.length; i++) {
            uint256 troveId = _troveArray[i];

            // Skip non-liquidatable troves
            if (!_isActiveOrZombie(Troves[troveId].status)) continue;

            uint256 ICR = getCurrentICR(troveId, _price);

            if (ICR < MCR) {
                LiquidationValues memory singleLiquidation;
                LatestTroveData memory trove;

                _liquidate(_defaultPool, troveId, remainingBoldInStabPool, _price, trove, singleLiquidation);
                remainingBoldInStabPool -= singleLiquidation.debtToOffset;

                // Add liquidation values to their respective running totals
                _addLiquidationValuesToTotals(trove, singleLiquidation, totals, troveChange);
            }
        }
    }

    // --- Liquidation helper functions ---

    // Adds all values from `singleLiquidation` to their respective totals in `totals` in-place
    function _addLiquidationValuesToTotals(
        LatestTroveData memory _trove,
        LiquidationValues memory _singleLiquidation,
        LiquidationValues memory totals,
        TroveChange memory troveChange
    ) internal pure {
        // Tally all the values with their respective running totals
        totals.collGasCompensation += _singleLiquidation.collGasCompensation;
        totals.ETHGasCompensation += ETH_GAS_COMPENSATION;
        troveChange.debtDecrease += _trove.entireDebt;
        troveChange.collDecrease += _trove.entireColl;
        troveChange.appliedRedistBoldDebtGain += _trove.redistBoldDebtGain;
        troveChange.oldWeightedRecordedDebt += _singleLiquidation.oldWeightedRecordedDebt;
        troveChange.newWeightedRecordedDebt += _singleLiquidation.newWeightedRecordedDebt;
        totals.debtToOffset += _singleLiquidation.debtToOffset;
        totals.collToSendToSP += _singleLiquidation.collToSendToSP;
        totals.debtToRedistribute += _singleLiquidation.debtToRedistribute;
        totals.collToRedistribute += _singleLiquidation.collToRedistribute;
        totals.collSurplus += _singleLiquidation.collSurplus;
    }

    function _sendGasCompensation(IActivePool _activePool, address _liquidator, uint256 _eth, uint256 _coll) internal {
        if (_eth > 0) {
            WETH.transferFrom(gasPoolAddress, _liquidator, _eth);
        }

        if (_coll > 0) {
            _activePool.sendColl(_liquidator, _coll);
        }
    }

    // Move a Trove's pending debt and collateral rewards from distributions, from the Default Pool to the Active Pool
    function _movePendingTroveRewardsToActivePool(IDefaultPool _defaultPool, uint256 _bold, uint256 _coll) internal {
        if (_bold > 0) {
            _defaultPool.decreaseBoldDebt(_bold);
        }

        if (_coll > 0) {
            _defaultPool.sendCollToActivePool(_coll);
        }
    }

    // --- Redemption functions ---

    function _applySingleRedemption(
        IDefaultPool _defaultPool,
        SingleRedemptionValues memory _singleRedemption,
        bool _isTroveInBatch
    ) internal returns (uint256) {
        // Decrease the debt and collateral of the current Trove according to the Bold lot and corresponding ETH to send
        uint256 newDebt = _singleRedemption.trove.entireDebt - _singleRedemption.boldLot;
        uint256 newColl = _singleRedemption.trove.entireColl - _singleRedemption.collLot;

        _singleRedemption.appliedRedistBoldDebtGain = _singleRedemption.trove.redistBoldDebtGain;

        if (_isTroveInBatch) {
            _getLatestBatchData(_singleRedemption.batchAddress, _singleRedemption.batch);
            // We know boldLot <= trove entire debt, so this subtraction is safe
            uint256 newAmountForWeightedDebt = _singleRedemption.batch.entireDebtWithoutRedistribution
                + _singleRedemption.trove.redistBoldDebtGain - _singleRedemption.boldLot;
            _singleRedemption.oldWeightedRecordedDebt = _singleRedemption.batch.weightedRecordedDebt;
            _singleRedemption.newWeightedRecordedDebt =
                newAmountForWeightedDebt * _singleRedemption.batch.annualInterestRate;

            TroveChange memory troveChange;
            troveChange.debtDecrease = _singleRedemption.boldLot;
            troveChange.collDecrease = _singleRedemption.collLot;
            troveChange.appliedRedistBoldDebtGain = _singleRedemption.trove.redistBoldDebtGain;
            troveChange.appliedRedistCollGain = _singleRedemption.trove.redistCollGain;
            // batchAccruedManagementFee is handled in the outer function
            troveChange.oldWeightedRecordedBatchManagementFee =
                _singleRedemption.batch.weightedRecordedBatchManagementFee;
            troveChange.newWeightedRecordedBatchManagementFee =
                newAmountForWeightedDebt * _singleRedemption.batch.annualManagementFee;

            activePool.mintBatchManagementFeeAndAccountForChange(troveChange, _singleRedemption.batchAddress);

            Troves[_singleRedemption.troveId].coll = newColl;
            // interest and fee were updated in the outer function
            // This call could revert due to BatchSharesRatioTooHigh if trove.redistCollGain > boldLot
            // so we skip that check to avoid blocking redemptions
            _updateBatchShares(
                _singleRedemption.troveId,
                _singleRedemption.batchAddress,
                troveChange,
                newDebt,
                _singleRedemption.batch.entireCollWithoutRedistribution,
                _singleRedemption.batch.entireDebtWithoutRedistribution,
                false // _checkBatchSharesRatio
            );
        } else {
            _singleRedemption.oldWeightedRecordedDebt = _singleRedemption.trove.weightedRecordedDebt;
            _singleRedemption.newWeightedRecordedDebt = newDebt * _singleRedemption.trove.annualInterestRate;
            Troves[_singleRedemption.troveId].debt = newDebt;
            Troves[_singleRedemption.troveId].coll = newColl;
            Troves[_singleRedemption.troveId].lastDebtUpdateTime = uint64(block.timestamp);
        }

        _singleRedemption.newStake = _updateStakeAndTotalStakes(_singleRedemption.troveId, newColl);
        _movePendingTroveRewardsToActivePool(
            _defaultPool, _singleRedemption.trove.redistBoldDebtGain, _singleRedemption.trove.redistCollGain
        );
        _updateTroveRewardSnapshots(_singleRedemption.troveId);

        if (_isTroveInBatch) {
            emit BatchedTroveUpdated({
                _troveId: _singleRedemption.troveId,
                _interestBatchManager: _singleRedemption.batchAddress,
                _batchDebtShares: Troves[_singleRedemption.troveId].batchDebtShares,
                _coll: newColl,
                _stake: _singleRedemption.newStake,
                _snapshotOfTotalCollRedist: L_coll,
                _snapshotOfTotalDebtRedist: L_boldDebt
            });
        } else {
            emit TroveUpdated({
                _troveId: _singleRedemption.troveId,
                _debt: newDebt,
                _coll: newColl,
                _stake: _singleRedemption.newStake,
                _annualInterestRate: _singleRedemption.trove.annualInterestRate,
                _snapshotOfTotalCollRedist: L_coll,
                _snapshotOfTotalDebtRedist: L_boldDebt
            });
        }

        emit TroveOperation({
            _troveId: _singleRedemption.troveId,
            _operation: Operation.redeemCollateral,
            _annualInterestRate: _singleRedemption.trove.annualInterestRate,
            _debtIncreaseFromRedist: _singleRedemption.trove.redistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: 0,
            _debtChangeFromOperation: -int256(_singleRedemption.boldLot),
            _collIncreaseFromRedist: _singleRedemption.trove.redistCollGain,
            _collChangeFromOperation: -int256(_singleRedemption.collLot)
        });

        if (_isTroveInBatch) {
            emit BatchUpdated({
                _interestBatchManager: _singleRedemption.batchAddress,
                _operation: BatchOperation.troveChange,
                _debt: batches[_singleRedemption.batchAddress].debt,
                _coll: batches[_singleRedemption.batchAddress].coll,
                _annualInterestRate: _singleRedemption.batch.annualInterestRate,
                _annualManagementFee: _singleRedemption.batch.annualManagementFee,
                _totalDebtShares: batches[_singleRedemption.batchAddress].totalDebtShares,
                _debtIncreaseFromUpfrontFee: 0
            });
        }

        emit RedemptionFeePaidToTrove(_singleRedemption.troveId, _singleRedemption.collFee);

        return newDebt;
    }

    // Redeem as much collateral as possible from _borrower's Trove in exchange for Bold up to _maxBoldamount
    function _redeemCollateralFromTrove(
        IDefaultPool _defaultPool,
        SingleRedemptionValues memory _singleRedemption,
        uint256 _maxBoldamount,
        uint256 _price,
        uint256 _redemptionRate
    ) internal {
        _getLatestTroveData(_singleRedemption.troveId, _singleRedemption.trove);

        // Determine the remaining amount (lot) to be redeemed, capped by the entire debt of the Trove
        _singleRedemption.boldLot = LiquityMath._min(_maxBoldamount, _singleRedemption.trove.entireDebt);

        // Get the amount of Coll equal in USD value to the boldLot redeemed
        uint256 correspondingColl = _singleRedemption.boldLot * DECIMAL_PRECISION / _price;
        // Calculate the collFee separately (for events)
        _singleRedemption.collFee = correspondingColl * _redemptionRate / DECIMAL_PRECISION;
        // Get the final collLot to send to redeemer, leaving the fee in the Trove
        _singleRedemption.collLot = correspondingColl - _singleRedemption.collFee;

        bool isTroveInBatch = _singleRedemption.batchAddress != address(0);
        uint256 newDebt = _applySingleRedemption(_defaultPool, _singleRedemption, isTroveInBatch);

        // Make Trove zombie if it's tiny (and it wasn’t already), in order to prevent griefing future (normal, sequential) redemptions
        if (newDebt < MIN_DEBT) {
            if (!_singleRedemption.isZombieTrove) {
                Troves[_singleRedemption.troveId].status = Status.zombie;
                if (isTroveInBatch) {
                    sortedTroves.removeFromBatch(_singleRedemption.troveId);
                } else {
                    sortedTroves.remove(_singleRedemption.troveId);
                }
                // If it’s a partial redemption, let’s store a pointer to it so it’s used first in the next one
                if (newDebt > 0) {
                    lastZombieTroveId = _singleRedemption.troveId;
                }
            } else if (newDebt == 0) {
                // Reset last zombie trove pointer if the previous one was fully redeemed now
                lastZombieTroveId = 0;
            }
        }
        // Note: technically, it could happen that the Trove pointed to by `lastZombieTroveId` ends up with
        // newDebt >= MIN_DEBT thanks to BOLD debt redistribution, which means it _could_ be made active again,
        // however we don't do that here, as it would require hints for re-insertion into `SortedTroves`.
    }

    function _updateBatchInterestPriorToRedemption(IActivePool _activePool, address _batchAddress) internal {
        LatestBatchData memory batch;
        _getLatestBatchData(_batchAddress, batch);
        batches[_batchAddress].debt = batch.entireDebtWithoutRedistribution;
        batches[_batchAddress].lastDebtUpdateTime = uint64(block.timestamp);
        // As we are updating the batch, we update the ActivePool weighted sum too
        TroveChange memory batchTroveChange;
        batchTroveChange.oldWeightedRecordedDebt = batch.weightedRecordedDebt;
        batchTroveChange.newWeightedRecordedDebt = batch.entireDebtWithoutRedistribution * batch.annualInterestRate;
        batchTroveChange.batchAccruedManagementFee = batch.accruedManagementFee;
        batchTroveChange.oldWeightedRecordedBatchManagementFee = batch.weightedRecordedBatchManagementFee;
        batchTroveChange.newWeightedRecordedBatchManagementFee =
            batch.entireDebtWithoutRedistribution * batch.annualManagementFee;

        _activePool.mintAggInterestAndAccountForTroveChange(batchTroveChange, _batchAddress);
    }

    /* Send _boldamount Bold to the system and redeem the corresponding amount of collateral from as many Troves as are needed to fill the redemption
    * request.  Applies redistribution gains to a Trove before reducing its debt and coll.
    *
    * Note that if _amount is very large, this function can run out of gas, specially if traversed troves are small. This can be easily avoided by
    * splitting the total _amount in appropriate chunks and calling the function multiple times.
    *
    * Param `_maxIterations` can also be provided, so the loop through Troves is capped (if it’s zero, it will be ignored).This makes it easier to
    * avoid OOG for the frontend, as only knowing approximately the average cost of an iteration is enough, without needing to know the “topology”
    * of the trove list. It also avoids the need to set the cap in stone in the contract, nor doing gas calculations, as both gas price and opcode
    * costs can vary.
    *
    * All Troves that are redeemed from -- with the likely exception of the last one -- will end up with no debt left, and therefore in “zombie” state
    */
    function redeemCollateral(
        address _redeemer,
        uint256 _boldamount,
        uint256 _price,
        uint256 _redemptionRate,
        uint256 _maxIterations
    ) external override returns (uint256 _redemeedAmount) {
        _requireCallerIsCollateralRegistry();

        IActivePool activePoolCached = activePool;
        ISortedTroves sortedTrovesCached = sortedTroves;

        TroveChange memory totalsTroveChange;
        uint256 totalCollFee;

        uint256 remainingBold = _boldamount;

        SingleRedemptionValues memory singleRedemption;
        // Let’s check if there’s a pending zombie trove from previous redemption
        if (lastZombieTroveId != 0) {
            singleRedemption.troveId = lastZombieTroveId;
            singleRedemption.isZombieTrove = true;
        } else {
            singleRedemption.troveId = sortedTrovesCached.getLast();
        }
        address lastBatchUpdatedInterest = address(0);

        // Loop through the Troves starting from the one with lowest interest rate until _amount of Bold is exchanged for collateral
        if (_maxIterations == 0) _maxIterations = type(uint256).max;
        while (singleRedemption.troveId != 0 && remainingBold > 0 && _maxIterations > 0) {
            _maxIterations--;
            // Save the uint256 of the Trove preceding the current one
            uint256 nextUserToCheck;
            if (singleRedemption.isZombieTrove) {
                nextUserToCheck = sortedTrovesCached.getLast();
            } else {
                nextUserToCheck = sortedTrovesCached.getPrev(singleRedemption.troveId);
            }

            // Skip if ICR < 100%, to make sure that redemptions don’t decrease the CR of hit Troves
            if (getCurrentICR(singleRedemption.troveId, _price) < _100pct) {
                singleRedemption.troveId = nextUserToCheck;
                singleRedemption.isZombieTrove = false;
                continue;
            }

            // If it’s in a batch, we need to update interest first
            // We do it here outside, to avoid repeating for each trove in the same batch
            singleRedemption.batchAddress = _getBatchManager(singleRedemption.troveId);
            if (
                singleRedemption.batchAddress != address(0) && singleRedemption.batchAddress != lastBatchUpdatedInterest
            ) {
                _updateBatchInterestPriorToRedemption(activePoolCached, singleRedemption.batchAddress);
                lastBatchUpdatedInterest = singleRedemption.batchAddress;
            }

            _redeemCollateralFromTrove(defaultPool, singleRedemption, remainingBold, _price, _redemptionRate);

            totalsTroveChange.collDecrease += singleRedemption.collLot;
            totalsTroveChange.debtDecrease += singleRedemption.boldLot;
            totalsTroveChange.appliedRedistBoldDebtGain += singleRedemption.appliedRedistBoldDebtGain;
            // For recorded and weighted recorded debt totals, we need to capture the increases and decreases,
            // since the net debt change for a given Trove could be positive or negative: redemptions decrease a Trove's recorded
            // (and weighted recorded) debt, but the accrued interest increases it.
            totalsTroveChange.newWeightedRecordedDebt += singleRedemption.newWeightedRecordedDebt;
            totalsTroveChange.oldWeightedRecordedDebt += singleRedemption.oldWeightedRecordedDebt;
            totalCollFee += singleRedemption.collFee;

            remainingBold -= singleRedemption.boldLot;
            singleRedemption.troveId = nextUserToCheck;
            singleRedemption.isZombieTrove = false;
        }

        // We are removing this condition to prevent blocking redemptions
        //require(totals.totalCollDrawn > 0, "TroveManager: Unable to redeem any amount");

        emit Redemption(
            _boldamount, totalsTroveChange.debtDecrease, totalsTroveChange.collDecrease, totalCollFee, _price
        );

        activePoolCached.mintAggInterestAndAccountForTroveChange(totalsTroveChange, address(0));

        // Send the redeemed Coll to sender
        activePoolCached.sendColl(_redeemer, totalsTroveChange.collDecrease);
        // We’ll burn all the Bold together out in the CollateralRegistry, to save gas

        return totalsTroveChange.debtDecrease;
    }

    // Redeem as much collateral as possible from _borrower's Trove in exchange for Bold up to _maxBoldamount
    function _urgentRedeemCollateralFromTrove(
        IDefaultPool _defaultPool,
        uint256 _maxBoldamount,
        uint256 _price,
        SingleRedemptionValues memory _singleRedemption
    ) internal {
        // Determine the remaining amount (lot) to be redeemed, capped by the entire debt of the Trove minus the liquidation reserve
        _singleRedemption.boldLot = LiquityMath._min(_maxBoldamount, _singleRedemption.trove.entireDebt);

        // Get the amount of ETH equal in USD value to the BOLD lot redeemed
        _singleRedemption.collLot = _singleRedemption.boldLot * (DECIMAL_PRECISION + URGENT_REDEMPTION_BONUS) / _price;
        // As here we can redeem when CR < 101% (accounting for 1% bonus), we need to cap by collateral too
        if (_singleRedemption.collLot > _singleRedemption.trove.entireColl) {
            _singleRedemption.collLot = _singleRedemption.trove.entireColl;
            _singleRedemption.boldLot =
                _singleRedemption.trove.entireColl * _price / (DECIMAL_PRECISION + URGENT_REDEMPTION_BONUS);
        }

        bool isTroveInBatch = _singleRedemption.batchAddress != address(0);
        _applySingleRedemption(_defaultPool, _singleRedemption, isTroveInBatch);

        // No need to make this Trove zombie if it has tiny debt, since:
        // - This collateral branch has shut down and urgent redemptions are enabled
        // - Urgent redemptions aren't sequential, so they can't be griefed by tiny Troves.
    }

    function urgentRedemption(uint256 _boldAmount, uint256[] calldata _troveIds, uint256 _minCollateral) external {
        _requireIsShutDown();
        _requireAmountGreaterThanZero(_boldAmount);
        _requireBoldBalanceCoversRedemption(boldToken, msg.sender, _boldAmount);

        IActivePool activePoolCached = activePool;
        TroveChange memory totalsTroveChange;

        // Use the standard fetchPrice here, since if branch has shut down we don't worry about small redemption arbs
        (uint256 price,) = priceFeed.fetchPrice();

        uint256 remainingBold = _boldAmount;
        for (uint256 i = 0; i < _troveIds.length; i++) {
            if (remainingBold == 0) break;

            SingleRedemptionValues memory singleRedemption;
            singleRedemption.troveId = _troveIds[i];
            _getLatestTroveData(singleRedemption.troveId, singleRedemption.trove);

            if (!_isActiveOrZombie(Troves[singleRedemption.troveId].status) || singleRedemption.trove.entireDebt == 0) {
                continue;
            }

            // If it’s in a batch, we need to update interest first
            // As we don’t have them ordered now, we cannot avoid repeating for each trove in the same batch
            singleRedemption.batchAddress = _getBatchManager(singleRedemption.troveId);
            if (singleRedemption.batchAddress != address(0)) {
                _updateBatchInterestPriorToRedemption(activePoolCached, singleRedemption.batchAddress);
            }

            _urgentRedeemCollateralFromTrove(defaultPool, remainingBold, price, singleRedemption);

            totalsTroveChange.collDecrease += singleRedemption.collLot;
            totalsTroveChange.debtDecrease += singleRedemption.boldLot;
            totalsTroveChange.appliedRedistBoldDebtGain += singleRedemption.appliedRedistBoldDebtGain;
            // For recorded and weighted recorded debt totals, we need to capture the increases and decreases,
            // since the net debt change for a given Trove could be positive or negative: redemptions decrease a Trove's recorded
            // (and weighted recorded) debt, but the accrued interest increases it.
            totalsTroveChange.newWeightedRecordedDebt += singleRedemption.newWeightedRecordedDebt;
            totalsTroveChange.oldWeightedRecordedDebt += singleRedemption.oldWeightedRecordedDebt;

            remainingBold -= singleRedemption.boldLot;
        }

        if (totalsTroveChange.collDecrease < _minCollateral) {
            revert MinCollNotReached(totalsTroveChange.collDecrease);
        }

        emit Redemption(_boldAmount, totalsTroveChange.debtDecrease, totalsTroveChange.collDecrease, 0, price);

        // Since this branch is shut down, this will mint 0 interest.
        // We call this only to update the aggregate debt and weighted debt trackers.
        activePoolCached.mintAggInterestAndAccountForTroveChange(totalsTroveChange, address(0));

        // Send the redeemed coll to caller
        activePoolCached.sendColl(msg.sender, totalsTroveChange.collDecrease);
        // Burn bold
        boldToken.burn(msg.sender, totalsTroveChange.debtDecrease);
    }

    function shutdown() external {
        _requireCallerIsBorrowerOperations();
        shutdownTime = block.timestamp;
        activePool.setShutdownFlag();
    }

    // --- Helper functions ---

    // Return the current collateral ratio (ICR) of a given Trove. Takes a trove's pending coll and debt rewards from redistributions into account.
    function getCurrentICR(uint256 _troveId, uint256 _price) public view override returns (uint256) {
        LatestTroveData memory trove;
        _getLatestTroveData(_troveId, trove);
        return LiquityMath._computeCR(trove.entireColl, trove.entireDebt, _price);
    }

    function _updateTroveRewardSnapshots(uint256 _troveId) internal {
        rewardSnapshots[_troveId].coll = L_coll;
        rewardSnapshots[_troveId].boldDebt = L_boldDebt;
    }

    // Return the Troves entire debt and coll, including redistribution gains from redistributions.
    function _getLatestTroveData(uint256 _troveId, LatestTroveData memory trove) internal view {
        // If trove belongs to a batch, we fetch the batch and apply its share to obtained values
        address batchAddress = _getBatchManager(_troveId);
        if (batchAddress != address(0)) {
            LatestBatchData memory batch;
            _getLatestBatchData(batchAddress, batch);
            _getLatestTroveDataFromBatch(_troveId, batchAddress, trove, batch);
            return;
        }

        uint256 stake = Troves[_troveId].stake;
        trove.redistBoldDebtGain = stake * (L_boldDebt - rewardSnapshots[_troveId].boldDebt) / DECIMAL_PRECISION;
        trove.redistCollGain = stake * (L_coll - rewardSnapshots[_troveId].coll) / DECIMAL_PRECISION;

        trove.recordedDebt = Troves[_troveId].debt;
        trove.annualInterestRate = Troves[_troveId].annualInterestRate;
        trove.weightedRecordedDebt = trove.recordedDebt * trove.annualInterestRate;

        uint256 period = _getInterestPeriod(Troves[_troveId].lastDebtUpdateTime);
        trove.accruedInterest = _calcInterest(trove.weightedRecordedDebt, period);

        trove.entireDebt = trove.recordedDebt + trove.redistBoldDebtGain + trove.accruedInterest;
        trove.entireColl = Troves[_troveId].coll + trove.redistCollGain;
        trove.lastInterestRateAdjTime = Troves[_troveId].lastInterestRateAdjTime;
    }

    function _getLatestTroveDataFromBatch(
        uint256 _troveId,
        address _batchAddress,
        LatestTroveData memory _latestTroveData,
        LatestBatchData memory _latestBatchData
    ) internal view {
        Trove memory trove = Troves[_troveId];
        uint256 batchDebtShares = trove.batchDebtShares;
        uint256 totalDebtShares = batches[_batchAddress].totalDebtShares;

        uint256 stake = trove.stake;
        _latestTroveData.redistBoldDebtGain =
            stake * (L_boldDebt - rewardSnapshots[_troveId].boldDebt) / DECIMAL_PRECISION;
        _latestTroveData.redistCollGain = stake * (L_coll - rewardSnapshots[_troveId].coll) / DECIMAL_PRECISION;

        if (totalDebtShares > 0) {
            _latestTroveData.recordedDebt = _latestBatchData.recordedDebt * batchDebtShares / totalDebtShares;
            _latestTroveData.weightedRecordedDebt = _latestTroveData.recordedDebt * _latestBatchData.annualInterestRate;
            _latestTroveData.accruedInterest = _latestBatchData.accruedInterest * batchDebtShares / totalDebtShares;
            _latestTroveData.accruedBatchManagementFee =
                _latestBatchData.accruedManagementFee * batchDebtShares / totalDebtShares;
        }
        _latestTroveData.annualInterestRate = _latestBatchData.annualInterestRate;

        // We can’t do pro-rata batch entireDebt, because redist gains are proportional to coll, not to debt
        _latestTroveData.entireDebt = _latestTroveData.recordedDebt + _latestTroveData.redistBoldDebtGain
            + _latestTroveData.accruedInterest + _latestTroveData.accruedBatchManagementFee;
        _latestTroveData.entireColl = trove.coll + _latestTroveData.redistCollGain;
        _latestTroveData.lastInterestRateAdjTime =
            LiquityMath._max(_latestBatchData.lastInterestRateAdjTime, trove.lastInterestRateAdjTime);
    }

    function getLatestTroveData(uint256 _troveId) external view returns (LatestTroveData memory trove) {
        _getLatestTroveData(_troveId, trove);
    }

    function getTroveAnnualInterestRate(uint256 _troveId) external view returns (uint256) {
        Trove memory trove = Troves[_troveId];
        address batchAddress = _getBatchManager(trove);
        if (batchAddress != address(0)) {
            return batches[batchAddress].annualInterestRate;
        }
        return trove.annualInterestRate;
    }

    function _getBatchManager(uint256 _troveId) internal view returns (address) {
        return Troves[_troveId].interestBatchManager;
    }

    function _getBatchManager(Trove memory trove) internal pure returns (address) {
        return trove.interestBatchManager;
    }

    // Return the Batch entire debt and coll, including redistribution gains from redistributions.
    function _getLatestBatchData(address _batchAddress, LatestBatchData memory latestBatchData) internal view {
        Batch memory batch = batches[_batchAddress];

        latestBatchData.recordedDebt = batch.debt;
        latestBatchData.annualInterestRate = batch.annualInterestRate;
        latestBatchData.weightedRecordedDebt = latestBatchData.recordedDebt * latestBatchData.annualInterestRate;
        uint256 period = _getInterestPeriod(batch.lastDebtUpdateTime);
        latestBatchData.accruedInterest = _calcInterest(latestBatchData.weightedRecordedDebt, period);
        latestBatchData.annualManagementFee = batch.annualManagementFee;
        latestBatchData.weightedRecordedBatchManagementFee =
            latestBatchData.recordedDebt * latestBatchData.annualManagementFee;
        latestBatchData.accruedManagementFee = _calcInterest(latestBatchData.weightedRecordedBatchManagementFee, period);

        latestBatchData.entireDebtWithoutRedistribution =
            latestBatchData.recordedDebt + latestBatchData.accruedInterest + latestBatchData.accruedManagementFee;
        latestBatchData.entireCollWithoutRedistribution = batch.coll;
        latestBatchData.lastDebtUpdateTime = batch.lastDebtUpdateTime;
        latestBatchData.lastInterestRateAdjTime = batch.lastInterestRateAdjTime;
    }

    function getLatestBatchData(address _batchAddress) external view returns (LatestBatchData memory batch) {
        _getLatestBatchData(_batchAddress, batch);
    }

    // Update borrower's stake based on their latest collateral value
    function _updateStakeAndTotalStakes(uint256 _troveId, uint256 _coll) internal returns (uint256 newStake) {
        newStake = _computeNewStake(_coll);
        uint256 oldStake = Troves[_troveId].stake;
        Troves[_troveId].stake = newStake;

        totalStakes = totalStakes - oldStake + newStake;
    }

    // Calculate a new stake based on the snapshots of the totalStakes and totalCollateral taken at the last liquidation
    function _computeNewStake(uint256 _coll) internal view returns (uint256) {
        uint256 stake;
        if (totalCollateralSnapshot == 0) {
            stake = _coll;
        } else {
            /*
            * The following assert() holds true because:
            * - The system always contains >= 1 trove
            * - When we close or liquidate a trove, we redistribute the redistribution gains, so if all troves were closed/liquidated,
            * rewards would’ve been emptied and totalCollateralSnapshot would be zero too.
            */
            // assert(totalStakesSnapshot > 0);
            stake = _coll * totalStakesSnapshot / totalCollateralSnapshot;
        }
        return stake;
    }

    function _redistributeDebtAndColl(
        IActivePool _activePool,
        IDefaultPool _defaultPool,
        uint256 _debtToRedistribute,
        uint256 _collToRedistribute
    ) internal {
        if (_debtToRedistribute == 0) return; // Otherwise _collToRedistribute > 0 too

        /*
        * Add distributed coll and debt rewards-per-unit-staked to the running totals. Division uses a "feedback"
        * error correction, to keep the cumulative error low in the running totals L_coll and L_boldDebt:
        *
        * 1) Form numerators which compensate for the floor division errors that occurred the last time this
        * function was called.
        * 2) Calculate "per-unit-staked" ratios.
        * 3) Multiply each ratio back by its denominator, to reveal the current floor division error.
        * 4) Store these errors for use in the next correction when this function is called.
        * 5) Note: static analysis tools complain about this "division before multiplication", however, it is intended.
        */
        uint256 collNumerator = _collToRedistribute * DECIMAL_PRECISION + lastCollError_Redistribution;
        uint256 boldDebtNumerator = _debtToRedistribute * DECIMAL_PRECISION + lastBoldDebtError_Redistribution;

        // Get the per-unit-staked terms
        uint256 collRewardPerUnitStaked = collNumerator / totalStakes;
        uint256 boldDebtRewardPerUnitStaked = boldDebtNumerator / totalStakes;

        lastCollError_Redistribution = collNumerator - collRewardPerUnitStaked * totalStakes;
        lastBoldDebtError_Redistribution = boldDebtNumerator - boldDebtRewardPerUnitStaked * totalStakes;

        // Add per-unit-staked terms to the running totals
        L_coll = L_coll + collRewardPerUnitStaked;
        L_boldDebt = L_boldDebt + boldDebtRewardPerUnitStaked;

        _defaultPool.increaseBoldDebt(_debtToRedistribute);
        _activePool.sendCollToDefaultPool(_collToRedistribute);
    }

    /*
    * Updates snapshots of system total stakes and total collateral, excluding a given collateral remainder from the calculation.
    * Used in a liquidation sequence.
    */
    function _updateSystemSnapshots_excludeCollRemainder(IActivePool _activePool, uint256 _collRemainder) internal {
        totalStakesSnapshot = totalStakes;

        uint256 activeColl = _activePool.getCollBalance();
        uint256 liquidatedColl = defaultPool.getCollBalance();
        totalCollateralSnapshot = activeColl - _collRemainder + liquidatedColl;
    }

    /*
    * Remove a Trove owner from the TroveIds array, not preserving array order. Removing owner 'B' does the following:
    * [A B C D E] => [A E C D], and updates E's Trove struct to point to its new array index.
    */
    function _removeTroveId(uint256 _troveId, uint256 TroveIdsArrayLength) internal {
        uint64 index = Troves[_troveId].arrayIndex;
        uint256 idxLast = TroveIdsArrayLength - 1;

        // assert(index <= idxLast);

        uint256 idToMove = TroveIds[idxLast];

        TroveIds[index] = idToMove;
        Troves[idToMove].arrayIndex = index;

        TroveIds.pop();
    }

    function getTroveStatus(uint256 _troveId) external view override returns (Status) {
        return Troves[_troveId].status;
    }

    // --- Interest rate calculations ---

    function _getInterestPeriod(uint256 _lastDebtUpdateTime) internal view returns (uint256) {
        if (shutdownTime == 0) {
            // If branch is not shut down, interest is earned up to now.
            return block.timestamp - _lastDebtUpdateTime;
        } else if (shutdownTime > 0 && _lastDebtUpdateTime < shutdownTime) {
            // If branch is shut down and the Trove was not updated since shut down, interest is earned up to the shutdown time.
            return shutdownTime - _lastDebtUpdateTime;
        } else {
            // if (shutdownTime > 0 && _lastDebtUpdateTime >= shutdownTime)
            // If branch is shut down and the Trove was updated after shutdown, no interest is earned since.
            return 0;
        }
    }

    // --- 'require' wrapper functions ---

    function _requireCallerIsBorrowerOperations() internal view {
        if (msg.sender != address(borrowerOperations)) {
            revert CallerNotBorrowerOperations();
        }
    }

    function _requireCallerIsCollateralRegistry() internal view {
        if (msg.sender != address(collateralRegistry)) {
            revert CallerNotCollateralRegistry();
        }
    }

    function _requireMoreThanOneTroveInSystem(uint256 TroveIdsArrayLength) internal pure {
        if (TroveIdsArrayLength == 1) {
            revert OnlyOneTroveLeft();
        }
    }

    function _requireIsShutDown() internal view {
        if (shutdownTime == 0) {
            revert NotShutDown();
        }
    }

    function _requireAmountGreaterThanZero(uint256 _amount) internal pure {
        if (_amount == 0) {
            revert ZeroAmount();
        }
    }

    function _requireBoldBalanceCoversRedemption(IBoldToken _boldToken, address _redeemer, uint256 _amount)
        internal
        view
    {
        uint256 boldBalance = _boldToken.balanceOf(_redeemer);
        if (boldBalance < _amount) {
            revert NotEnoughBoldBalance();
        }
    }

    // --- Trove property getters ---

    function getUnbackedPortionPriceAndRedeemability() external returns (uint256, uint256, bool) {
        uint256 totalDebt = getEntireSystemDebt();
        uint256 spSize = stabilityPool.getTotalBoldDeposits();
        uint256 unbackedPortion = totalDebt > spSize ? totalDebt - spSize : 0;

        (uint256 price,) = priceFeed.fetchRedemptionPrice();
        // It's redeemable if the TCR is above the shutdown threshold, and branch has not been shut down
        bool redeemable = _getTCR(price) >= SCR && shutdownTime == 0;

        return (unbackedPortion, price, redeemable);
    }

    // --- Trove property setters, called by BorrowerOperations ---

    function onOpenTrove(address _owner, uint256 _troveId, TroveChange memory _troveChange, uint256 _annualInterestRate)
        external
    {
        _requireCallerIsBorrowerOperations();

        uint256 newStake = _computeNewStake(_troveChange.collIncrease);

        // Trove memory newTrove;
        Troves[_troveId].debt = _troveChange.debtIncrease + _troveChange.upfrontFee;
        Troves[_troveId].coll = _troveChange.collIncrease;
        Troves[_troveId].stake = newStake;
        Troves[_troveId].status = Status.active;
        Troves[_troveId].arrayIndex = uint64(TroveIds.length);
        Troves[_troveId].lastDebtUpdateTime = uint64(block.timestamp);
        Troves[_troveId].lastInterestRateAdjTime = uint64(block.timestamp);
        Troves[_troveId].annualInterestRate = _annualInterestRate;

        // Push the trove's id to the Trove list
        TroveIds.push(_troveId);

        uint256 newTotalStakes = totalStakes + newStake;
        totalStakes = newTotalStakes;

        // mint ERC721
        troveNFT.mint(_owner, _troveId);

        _updateTroveRewardSnapshots(_troveId);

        emit TroveUpdated({
            _troveId: _troveId,
            _debt: _troveChange.debtIncrease + _troveChange.upfrontFee,
            _coll: _troveChange.collIncrease,
            _stake: newStake,
            _annualInterestRate: _annualInterestRate,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.openTrove,
            _annualInterestRate: _annualInterestRate,
            _debtIncreaseFromRedist: 0,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: int256(_troveChange.debtIncrease),
            _collIncreaseFromRedist: 0,
            _collChangeFromOperation: int256(_troveChange.collIncrease)
        });
    }

    function onOpenTroveAndJoinBatch(
        address _owner,
        uint256 _troveId,
        TroveChange memory _troveChange,
        address _batchAddress,
        uint256 _batchColl,
        uint256 _batchDebt
    ) external {
        _requireCallerIsBorrowerOperations();
        // assert(batchIds[batches[_batchAddress].arrayIndex] == _batchAddress);

        uint256 newStake = _computeNewStake(_troveChange.collIncrease);

        // Trove memory newTrove;
        Troves[_troveId].coll = _troveChange.collIncrease;
        Troves[_troveId].stake = newStake;
        Troves[_troveId].status = Status.active;
        Troves[_troveId].arrayIndex = uint64(TroveIds.length);
        Troves[_troveId].interestBatchManager = _batchAddress;
        Troves[_troveId].lastInterestRateAdjTime = uint64(block.timestamp);

        _updateTroveRewardSnapshots(_troveId);

        // Push the trove's id to the Trove list
        TroveIds.push(_troveId);

        assert(_troveChange.debtIncrease > 0); // TODO: remove before deployment
        _updateBatchShares(
            _troveId, _batchAddress, _troveChange, _troveChange.debtIncrease, _batchColl, _batchDebt, true
        );

        uint256 newTotalStakes = totalStakes + newStake;
        totalStakes = newTotalStakes;

        // mint ERC721
        troveNFT.mint(_owner, _troveId);

        emit BatchedTroveUpdated({
            _troveId: _troveId,
            _interestBatchManager: _batchAddress,
            _batchDebtShares: Troves[_troveId].batchDebtShares,
            _coll: _troveChange.collIncrease,
            _stake: newStake,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.openTroveAndJoinBatch,
            _annualInterestRate: batches[_batchAddress].annualInterestRate,
            _debtIncreaseFromRedist: 0,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: int256(_troveChange.debtIncrease),
            _collIncreaseFromRedist: 0,
            _collChangeFromOperation: int256(_troveChange.collIncrease)
        });

        emit BatchUpdated({
            _interestBatchManager: _batchAddress,
            _operation: BatchOperation.joinBatch,
            _debt: batches[_batchAddress].debt,
            _coll: batches[_batchAddress].coll,
            _annualInterestRate: batches[_batchAddress].annualInterestRate,
            _annualManagementFee: batches[_batchAddress].annualManagementFee,
            _totalDebtShares: batches[_batchAddress].totalDebtShares,
            // Although the Trove joining the batch pays an upfront fee,
            // it is an individual fee, so we don't include it here
            _debtIncreaseFromUpfrontFee: 0
        });
    }

    function setTroveStatusToActive(uint256 _troveId) external {
        _requireCallerIsBorrowerOperations();
        Troves[_troveId].status = Status.active;
        if (lastZombieTroveId == _troveId) {
            lastZombieTroveId = 0;
        }
    }

    function onAdjustTroveInterestRate(
        uint256 _troveId,
        uint256 _newColl,
        uint256 _newDebt,
        uint256 _newAnnualInterestRate,
        TroveChange calldata _troveChange
    ) external {
        _requireCallerIsBorrowerOperations();

        Troves[_troveId].coll = _newColl;
        Troves[_troveId].debt = _newDebt;
        Troves[_troveId].annualInterestRate = _newAnnualInterestRate;
        Troves[_troveId].lastDebtUpdateTime = uint64(block.timestamp);
        Troves[_troveId].lastInterestRateAdjTime = uint64(block.timestamp);

        _movePendingTroveRewardsToActivePool(
            defaultPool, _troveChange.appliedRedistBoldDebtGain, _troveChange.appliedRedistCollGain
        );

        _updateTroveRewardSnapshots(_troveId);

        emit TroveUpdated({
            _troveId: _troveId,
            _debt: _newDebt,
            _coll: _newColl,
            _stake: Troves[_troveId].stake,
            _annualInterestRate: _newAnnualInterestRate,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.adjustTroveInterestRate,
            _annualInterestRate: _newAnnualInterestRate,
            _debtIncreaseFromRedist: _troveChange.appliedRedistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: 0,
            _collIncreaseFromRedist: _troveChange.appliedRedistCollGain,
            _collChangeFromOperation: 0
        });
    }

    function onAdjustTrove(uint256 _troveId, uint256 _newColl, uint256 _newDebt, TroveChange calldata _troveChange)
        external
    {
        _requireCallerIsBorrowerOperations();

        Troves[_troveId].coll = _newColl;
        Troves[_troveId].debt = _newDebt;
        Troves[_troveId].lastDebtUpdateTime = uint64(block.timestamp);

        _movePendingTroveRewardsToActivePool(
            defaultPool, _troveChange.appliedRedistBoldDebtGain, _troveChange.appliedRedistCollGain
        );

        uint256 newStake = _updateStakeAndTotalStakes(_troveId, _newColl);
        _updateTroveRewardSnapshots(_troveId);

        emit TroveUpdated({
            _troveId: _troveId,
            _debt: _newDebt,
            _coll: _newColl,
            _stake: newStake,
            _annualInterestRate: Troves[_troveId].annualInterestRate,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.adjustTrove,
            _annualInterestRate: Troves[_troveId].annualInterestRate,
            _debtIncreaseFromRedist: _troveChange.appliedRedistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: int256(_troveChange.debtIncrease) - int256(_troveChange.debtDecrease),
            _collIncreaseFromRedist: _troveChange.appliedRedistCollGain,
            _collChangeFromOperation: int256(_troveChange.collIncrease) - int256(_troveChange.collDecrease)
        });
    }

    function onCloseTrove(
        uint256 _troveId,
        TroveChange memory _troveChange, // decrease vars: entire, with interest, batch fee and redistribution
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt // entire, with interest and batch fee
    ) external override {
        _requireCallerIsBorrowerOperations();
        _closeTrove(_troveId, _troveChange, _batchAddress, _newBatchColl, _newBatchDebt, Status.closedByOwner);
        _movePendingTroveRewardsToActivePool(
            defaultPool, _troveChange.appliedRedistBoldDebtGain, _troveChange.appliedRedistCollGain
        );

        emit TroveUpdated({
            _troveId: _troveId,
            _debt: 0,
            _coll: 0,
            _stake: 0,
            _annualInterestRate: 0,
            _snapshotOfTotalCollRedist: 0,
            _snapshotOfTotalDebtRedist: 0
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.closeTrove,
            _annualInterestRate: 0,
            _debtIncreaseFromRedist: _troveChange.appliedRedistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: int256(_troveChange.debtIncrease) - int256(_troveChange.debtDecrease),
            _collIncreaseFromRedist: _troveChange.appliedRedistCollGain,
            _collChangeFromOperation: int256(_troveChange.collIncrease) - int256(_troveChange.collDecrease)
        });

        if (_batchAddress != address(0)) {
            emit BatchUpdated({
                _interestBatchManager: _batchAddress,
                _operation: BatchOperation.exitBatch,
                _debt: batches[_batchAddress].debt,
                _coll: batches[_batchAddress].coll,
                _annualInterestRate: batches[_batchAddress].annualInterestRate,
                _annualManagementFee: batches[_batchAddress].annualManagementFee,
                _totalDebtShares: batches[_batchAddress].totalDebtShares,
                _debtIncreaseFromUpfrontFee: 0
            });
        }
    }

    function _closeTrove(
        uint256 _troveId,
        TroveChange memory _troveChange, // decrease vars: entire, with interest, batch fee and redistribution
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt, // entire, with interest and batch fee
        Status closedStatus
    ) internal {
        // assert(closedStatus == Status.closedByLiquidation || closedStatus == Status.closedByOwner);

        uint256 TroveIdsArrayLength = TroveIds.length;
        _requireMoreThanOneTroveInSystem(TroveIdsArrayLength);

        _removeTroveId(_troveId, TroveIdsArrayLength);

        Trove memory trove = Troves[_troveId];

        // If trove belongs to a batch, remove from it
        if (_batchAddress != address(0)) {
            if (trove.status == Status.active) {
                sortedTroves.removeFromBatch(_troveId);
            } else if (trove.status == Status.zombie && lastZombieTroveId == _troveId) {
                lastZombieTroveId = 0;
            }

            _removeTroveSharesFromBatch(
                _troveId,
                _troveChange.collDecrease,
                _troveChange.debtDecrease,
                _troveChange,
                _batchAddress,
                _newBatchColl,
                _newBatchDebt
            );
        } else {
            if (trove.status == Status.active) {
                sortedTroves.remove(_troveId);
            } else if (trove.status == Status.zombie && lastZombieTroveId == _troveId) {
                lastZombieTroveId = 0;
            }
        }

        uint256 newTotalStakes = totalStakes - trove.stake;
        totalStakes = newTotalStakes;

        // Zero Trove properties
        delete Troves[_troveId];
        Troves[_troveId].status = closedStatus;

        // Zero Trove snapshots
        delete rewardSnapshots[_troveId];

        // burn ERC721
        troveNFT.burn(_troveId);
    }

    function onAdjustTroveInsideBatch(
        uint256 _troveId,
        uint256 _newTroveColl, // entire, with redistribution and trove change
        uint256 _newTroveDebt, // entire, with redistribution and trove change
        TroveChange memory _troveChange,
        address _batchAddress,
        uint256 _newBatchColl, // without trove change
        uint256 _newBatchDebt // entire (with interest, batch fee), but without trove change nor upfront fee nor redistribution
    ) external {
        _requireCallerIsBorrowerOperations();

        // Trove
        Troves[_troveId].coll = _newTroveColl;
        _updateTroveRewardSnapshots(_troveId);
        uint256 newStake = _updateStakeAndTotalStakes(_troveId, _newTroveColl);

        // Batch
        assert(_newTroveDebt > 0); // TODO: remove before deployment
        _updateBatchShares(_troveId, _batchAddress, _troveChange, _newTroveDebt, _newBatchColl, _newBatchDebt, true);

        _movePendingTroveRewardsToActivePool(
            defaultPool, _troveChange.appliedRedistBoldDebtGain, _troveChange.appliedRedistCollGain
        );

        emit BatchedTroveUpdated({
            _troveId: _troveId,
            _interestBatchManager: _batchAddress,
            _batchDebtShares: Troves[_troveId].batchDebtShares,
            _coll: _newTroveColl,
            _stake: newStake,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.adjustTrove,
            _annualInterestRate: batches[_batchAddress].annualInterestRate,
            _debtIncreaseFromRedist: _troveChange.appliedRedistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: int256(_troveChange.debtIncrease) - int256(_troveChange.debtDecrease),
            _collIncreaseFromRedist: _troveChange.appliedRedistCollGain,
            _collChangeFromOperation: int256(_troveChange.collIncrease) - int256(_troveChange.collDecrease)
        });

        emit BatchUpdated({
            _interestBatchManager: _batchAddress,
            _operation: BatchOperation.troveChange,
            _debt: batches[_batchAddress].debt,
            _coll: batches[_batchAddress].coll,
            _annualInterestRate: batches[_batchAddress].annualInterestRate,
            _annualManagementFee: batches[_batchAddress].annualManagementFee,
            _totalDebtShares: batches[_batchAddress].totalDebtShares,
            // Although the Trove being adjusted may pay an upfront fee,
            // it is an individual fee, so we don't include it here
            _debtIncreaseFromUpfrontFee: 0
        });
    }

    function onApplyTroveInterest(
        uint256 _troveId,
        uint256 _newTroveColl,
        uint256 _newTroveDebt,
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt,
        TroveChange calldata _troveChange
    ) external {
        _requireCallerIsBorrowerOperations();

        Troves[_troveId].coll = _newTroveColl;

        if (_batchAddress != address(0)) {
            assert(_newTroveDebt > 0); // TODO: remove before deployment
            _updateBatchShares(_troveId, _batchAddress, _troveChange, _newTroveDebt, _newBatchColl, _newBatchDebt, true);

            emit BatchUpdated({
                _interestBatchManager: _batchAddress,
                _operation: BatchOperation.applyBatchInterestAndFee,
                _debt: _newBatchDebt,
                _coll: _newBatchColl,
                _annualInterestRate: batches[_batchAddress].annualInterestRate,
                _annualManagementFee: batches[_batchAddress].annualManagementFee,
                _totalDebtShares: batches[_batchAddress].totalDebtShares,
                _debtIncreaseFromUpfrontFee: 0
            });
        } else {
            Troves[_troveId].debt = _newTroveDebt;
            Troves[_troveId].lastDebtUpdateTime = uint64(block.timestamp);
        }

        _movePendingTroveRewardsToActivePool(
            defaultPool, _troveChange.appliedRedistBoldDebtGain, _troveChange.appliedRedistCollGain
        );

        _updateTroveRewardSnapshots(_troveId);

        emit TroveUpdated({
            _troveId: _troveId,
            _debt: _newTroveDebt,
            _coll: _newTroveColl,
            _stake: Troves[_troveId].stake,
            _annualInterestRate: Troves[_troveId].annualInterestRate,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.applyPendingDebt,
            _annualInterestRate: Troves[_troveId].annualInterestRate,
            _debtIncreaseFromRedist: _troveChange.appliedRedistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: int256(_troveChange.debtIncrease) - int256(_troveChange.debtDecrease),
            _collIncreaseFromRedist: _troveChange.appliedRedistCollGain,
            _collChangeFromOperation: int256(_troveChange.collIncrease) - int256(_troveChange.collDecrease)
        });
    }

    function onRegisterBatchManager(address _account, uint256 _annualInterestRate, uint256 _annualManagementFee)
        external
    {
        _requireCallerIsBorrowerOperations();

        batches[_account].arrayIndex = uint64(batchIds.length);
        batches[_account].annualInterestRate = _annualInterestRate;
        batches[_account].annualManagementFee = _annualManagementFee;
        batches[_account].lastInterestRateAdjTime = uint64(block.timestamp);

        batchIds.push(_account);

        emit BatchUpdated({
            _interestBatchManager: _account,
            _operation: BatchOperation.registerBatchManager,
            _debt: 0,
            _coll: 0,
            _annualInterestRate: _annualInterestRate,
            _annualManagementFee: _annualManagementFee,
            _totalDebtShares: 0,
            _debtIncreaseFromUpfrontFee: 0
        });
    }

    function onLowerBatchManagerAnnualFee(
        address _batchAddress,
        uint256 _newColl,
        uint256 _newDebt,
        uint256 _newAnnualManagementFee
    ) external {
        _requireCallerIsBorrowerOperations();

        batches[_batchAddress].coll = _newColl;
        batches[_batchAddress].debt = _newDebt;
        batches[_batchAddress].annualManagementFee = _newAnnualManagementFee;
        batches[_batchAddress].lastDebtUpdateTime = uint64(block.timestamp);

        emit BatchUpdated({
            _interestBatchManager: _batchAddress,
            _operation: BatchOperation.lowerBatchManagerAnnualFee,
            _debt: _newDebt,
            _coll: _newColl,
            _annualInterestRate: batches[_batchAddress].annualInterestRate,
            _annualManagementFee: _newAnnualManagementFee,
            _totalDebtShares: batches[_batchAddress].totalDebtShares,
            _debtIncreaseFromUpfrontFee: 0
        });
    }

    function onSetBatchManagerAnnualInterestRate(
        address _batchAddress,
        uint256 _newColl,
        uint256 _newDebt,
        uint256 _newAnnualInterestRate,
        uint256 _upfrontFee
    ) external {
        _requireCallerIsBorrowerOperations();

        batches[_batchAddress].coll = _newColl;
        batches[_batchAddress].debt = _newDebt;
        batches[_batchAddress].annualInterestRate = _newAnnualInterestRate;
        batches[_batchAddress].lastDebtUpdateTime = uint64(block.timestamp);
        batches[_batchAddress].lastInterestRateAdjTime = uint64(block.timestamp);

        emit BatchUpdated({
            _interestBatchManager: _batchAddress,
            _operation: BatchOperation.setBatchManagerAnnualInterestRate,
            _debt: _newDebt,
            _coll: _newColl,
            _annualInterestRate: _newAnnualInterestRate,
            _annualManagementFee: batches[_batchAddress].annualManagementFee,
            _totalDebtShares: batches[_batchAddress].totalDebtShares,
            _debtIncreaseFromUpfrontFee: _upfrontFee
        });
    }

    function onSetInterestBatchManager(OnSetInterestBatchManagerParams calldata _params) external {
        _requireCallerIsBorrowerOperations();
        TroveChange memory _troveChange = _params.troveChange;

        // assert(batchIds[batches[_params.newBatchAddress].arrayIndex] == _params.newBatchAddress);

        _updateTroveRewardSnapshots(_params.troveId);

        // Clean Trove state
        Troves[_params.troveId].debt = 0;
        Troves[_params.troveId].annualInterestRate = 0;
        Troves[_params.troveId].lastDebtUpdateTime = 0;
        Troves[_params.troveId].coll = _params.troveColl;

        Troves[_params.troveId].interestBatchManager = _params.newBatchAddress;
        Troves[_params.troveId].lastInterestRateAdjTime = uint64(block.timestamp);

        _troveChange.collIncrease = _params.troveColl - _troveChange.appliedRedistCollGain;
        _troveChange.debtIncrease = _params.troveDebt - _troveChange.appliedRedistBoldDebtGain - _troveChange.upfrontFee;
        assert(_params.troveDebt > 0); // TODO: remove before deployment
        _updateBatchShares(
            _params.troveId,
            _params.newBatchAddress,
            _troveChange,
            _params.troveDebt,
            _params.newBatchColl,
            _params.newBatchDebt,
            true
        );

        _movePendingTroveRewardsToActivePool(
            defaultPool, _troveChange.appliedRedistBoldDebtGain, _troveChange.appliedRedistCollGain
        );

        emit BatchedTroveUpdated({
            _troveId: _params.troveId,
            _interestBatchManager: _params.newBatchAddress,
            _batchDebtShares: Troves[_params.troveId].batchDebtShares,
            _coll: _params.troveColl,
            _stake: Troves[_params.troveId].stake,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _params.troveId,
            _operation: Operation.setInterestBatchManager,
            _annualInterestRate: batches[_params.newBatchAddress].annualInterestRate,
            _debtIncreaseFromRedist: _troveChange.appliedRedistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: 0,
            _collIncreaseFromRedist: _troveChange.appliedRedistCollGain,
            _collChangeFromOperation: 0
        });

        emit BatchUpdated({
            _interestBatchManager: _params.newBatchAddress,
            _operation: BatchOperation.joinBatch,
            _debt: batches[_params.newBatchAddress].debt,
            _coll: batches[_params.newBatchAddress].coll,
            _annualInterestRate: batches[_params.newBatchAddress].annualInterestRate,
            _annualManagementFee: batches[_params.newBatchAddress].annualManagementFee,
            _totalDebtShares: batches[_params.newBatchAddress].totalDebtShares,
            // Although the Trove joining the batch may pay an upfront fee,
            // it is an individual fee, so we don't include it here
            _debtIncreaseFromUpfrontFee: 0
        });
    }

    // This function will revert if there’s a total debt increase and the ratio debt / shares has exceeded the max
    function _updateBatchShares(
        uint256 _troveId,
        address _batchAddress,
        TroveChange memory _troveChange,
        uint256 _newTroveDebt, // entire, with interest, batch fee and redistribution
        uint256 _batchColl, // without trove change
        uint256 _batchDebt, // entire (with interest, batch fee), but without trove change, nor upfront fee nor redist
        bool _checkBatchSharesRatio // whether we do the check on the resulting ratio inside the func call
    ) internal {
        // Debt
        uint256 currentBatchDebtShares = batches[_batchAddress].totalDebtShares;
        uint256 batchDebtSharesDelta;
        uint256 debtIncrease =
            _troveChange.debtIncrease + _troveChange.upfrontFee + _troveChange.appliedRedistBoldDebtGain;
        uint256 debtDecrease;
        if (debtIncrease > _troveChange.debtDecrease) {
            debtIncrease -= _troveChange.debtDecrease;
        } else {
            debtDecrease = _troveChange.debtDecrease - debtIncrease;
            debtIncrease = 0;
        }

        if (debtIncrease == 0 && debtDecrease == 0) {
            batches[_batchAddress].debt = _batchDebt;
        } else {
            if (debtIncrease > 0) {
                // Add debt
                if (_batchDebt == 0) {
                    batchDebtSharesDelta = debtIncrease;
                } else {
                    // To avoid rebasing issues, let’s make sure the ratio debt / shares is not too high
                    _requireBelowMaxSharesRatio(currentBatchDebtShares, _batchDebt, _checkBatchSharesRatio);

                    batchDebtSharesDelta = currentBatchDebtShares * debtIncrease / _batchDebt;
                }

                Troves[_troveId].batchDebtShares += batchDebtSharesDelta;
                batches[_batchAddress].debt = _batchDebt + debtIncrease;
                batches[_batchAddress].totalDebtShares = currentBatchDebtShares + batchDebtSharesDelta;
            } else if (debtDecrease > 0) {
                // Subtract debt
                // We make sure that if final trove debt is zero, shares are too (avoiding rounding issues)
                // This can only happen from redemptions, as otherwise we would be using _removeTroveSharesFromBatch
                // In redemptions we don’t do that because we don’t want to kick the trove out of the batch (it’d be bad UX)
                if (_newTroveDebt == 0) {
                    batches[_batchAddress].debt = _batchDebt - debtDecrease;
                    batches[_batchAddress].totalDebtShares = currentBatchDebtShares - Troves[_troveId].batchDebtShares;
                    Troves[_troveId].batchDebtShares = 0;
                } else {
                    batchDebtSharesDelta = currentBatchDebtShares * debtDecrease / _batchDebt;

                    Troves[_troveId].batchDebtShares -= batchDebtSharesDelta;
                    batches[_batchAddress].debt = _batchDebt - debtDecrease;
                    batches[_batchAddress].totalDebtShares = currentBatchDebtShares - batchDebtSharesDelta;
                }
            }
        }
        // Update debt checkpoint
        batches[_batchAddress].lastDebtUpdateTime = uint64(block.timestamp);

        // Collateral
        uint256 collIncrease = _troveChange.collIncrease + _troveChange.appliedRedistCollGain;
        uint256 collDecrease;
        if (collIncrease > _troveChange.collDecrease) {
            collIncrease -= _troveChange.collDecrease;
        } else {
            collDecrease = _troveChange.collDecrease - collIncrease;
            collIncrease = 0;
        }

        if (collIncrease == 0 && collDecrease == 0) {
            batches[_batchAddress].coll = _batchColl;
        } else {
            if (collIncrease > 0) {
                // Add coll
                batches[_batchAddress].coll = _batchColl + collIncrease;
            } else if (collDecrease > 0) {
                // Subtract coll
                batches[_batchAddress].coll = _batchColl - collDecrease;
            }
        }
    }

    // For the debt / shares ratio to increase by a factor 1e9
    // at a average annual debt increase (compounded interest + fees) of 10%, it would take more than 217 years (log(1e9)/log(1.1))
    // at a average annual debt increase (compounded interest + fees) of 50%, it would take more than 51 years (log(1e9)/log(1.5))
    // When that happens, no more debt can be manually added to the batch, so batch should be migrated to a new one
    function _requireBelowMaxSharesRatio(
        uint256 _currentBatchDebtShares,
        uint256 _batchDebt,
        bool _checkBatchSharesRatio
    ) internal pure {
        // debt / shares should be below MAX_BATCH_SHARES_RATIO
        if (_currentBatchDebtShares * MAX_BATCH_SHARES_RATIO < _batchDebt && _checkBatchSharesRatio) {
            revert BatchSharesRatioTooHigh();
        }
    }

    function onRemoveFromBatch(
        uint256 _troveId,
        uint256 _newTroveColl, // entire, with redistribution
        uint256 _newTroveDebt, // entire, with interest, batch fee and redistribution
        TroveChange memory _troveChange,
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt, // entire, with interest and batch fee
        uint256 _newAnnualInterestRate
    ) external {
        _requireCallerIsBorrowerOperations();
        // assert(batchIds[batches[_batchAddress].arrayIndex] == _batchAddress);

        // Subtract from batch
        _removeTroveSharesFromBatch(
            _troveId, _newTroveColl, _newTroveDebt, _troveChange, _batchAddress, _newBatchColl, _newBatchDebt
        );

        // Restore Trove state
        Troves[_troveId].debt = _newTroveDebt;
        Troves[_troveId].coll = _newTroveColl;
        Troves[_troveId].lastDebtUpdateTime = uint64(block.timestamp);
        Troves[_troveId].annualInterestRate = _newAnnualInterestRate;
        Troves[_troveId].lastInterestRateAdjTime = uint64(block.timestamp);

        _updateTroveRewardSnapshots(_troveId);
        _movePendingTroveRewardsToActivePool(
            defaultPool, _troveChange.appliedRedistBoldDebtGain, _troveChange.appliedRedistCollGain
        );

        emit TroveUpdated({
            _troveId: _troveId,
            _debt: _newTroveDebt,
            _coll: _newTroveColl,
            _stake: Troves[_troveId].stake,
            _annualInterestRate: _newAnnualInterestRate,
            _snapshotOfTotalCollRedist: L_coll,
            _snapshotOfTotalDebtRedist: L_boldDebt
        });

        emit TroveOperation({
            _troveId: _troveId,
            _operation: Operation.removeFromBatch,
            _annualInterestRate: _newAnnualInterestRate,
            _debtIncreaseFromRedist: _troveChange.appliedRedistBoldDebtGain,
            _debtIncreaseFromUpfrontFee: _troveChange.upfrontFee,
            _debtChangeFromOperation: 0,
            _collIncreaseFromRedist: _troveChange.appliedRedistCollGain,
            _collChangeFromOperation: 0
        });

        emit BatchUpdated({
            _interestBatchManager: _batchAddress,
            _operation: BatchOperation.exitBatch,
            _debt: batches[_batchAddress].debt,
            _coll: batches[_batchAddress].coll,
            _annualInterestRate: batches[_batchAddress].annualInterestRate,
            _annualManagementFee: batches[_batchAddress].annualManagementFee,
            _totalDebtShares: batches[_batchAddress].totalDebtShares,
            // Although the Trove leaving the batch may pay an upfront fee,
            // it is an individual fee, so we don't include it here
            _debtIncreaseFromUpfrontFee: 0
        });
    }

    function _removeTroveSharesFromBatch(
        uint256 _troveId,
        uint256 _newTroveColl, // entire, with redistribution
        uint256 _newTroveDebt, // entire, with interest, batch fee and redistribution
        TroveChange memory _troveChange,
        address _batchAddress,
        uint256 _newBatchColl, // without trove change
        uint256 _newBatchDebt // entire (with interest and batch fee), but without trove change
    ) internal {
        // As we are removing:
        // assert(_newBatchDebt > 0 || _newBatchColl > 0);

        Trove memory trove = Troves[_troveId];

        // We don’t need to increase the shares corresponding to redistribution first, because they would be subtracted immediately after
        // We don’t need to account for interest nor batch fee because it’s proportional to debt shares
        uint256 batchDebtDecrease = _newTroveDebt - _troveChange.upfrontFee - _troveChange.appliedRedistBoldDebtGain;
        uint256 batchCollDecrease = _newTroveColl - _troveChange.appliedRedistCollGain;

        batches[_batchAddress].totalDebtShares -= trove.batchDebtShares;
        batches[_batchAddress].debt = _newBatchDebt - batchDebtDecrease;
        batches[_batchAddress].coll = _newBatchColl - batchCollDecrease;
        batches[_batchAddress].lastDebtUpdateTime = uint64(block.timestamp);

        Troves[_troveId].interestBatchManager = address(0);
        Troves[_troveId].batchDebtShares = 0;
    }
}

File 2 of 44 : ITroveManager.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./ILiquityBase.sol";
import "./ITroveNFT.sol";
import "./IBorrowerOperations.sol";
import "./IStabilityPool.sol";
import "./IBoldToken.sol";
import "./ISortedTroves.sol";
import "../Types/LatestTroveData.sol";
import "../Types/LatestBatchData.sol";

// Common interface for the Trove Manager.
interface ITroveManager is ILiquityBase {
    enum Status {
        nonExistent,
        active,
        closedByOwner,
        closedByLiquidation,
        zombie
    }

    function shutdownTime() external view returns (uint256);

    function troveNFT() external view returns (ITroveNFT);
    function stabilityPool() external view returns (IStabilityPool);
    //function boldToken() external view returns (IBoldToken);
    function sortedTroves() external view returns (ISortedTroves);
    function borrowerOperations() external view returns (IBorrowerOperations);

    function Troves(uint256 _id)
        external
        view
        returns (
            uint256 debt,
            uint256 coll,
            uint256 stake,
            Status status,
            uint64 arrayIndex,
            uint64 lastDebtUpdateTime,
            uint64 lastInterestRateAdjTime,
            uint256 annualInterestRate,
            address interestBatchManager,
            uint256 batchDebtShares
        );

    function rewardSnapshots(uint256 _id) external view returns (uint256 coll, uint256 boldDebt);

    function getTroveIdsCount() external view returns (uint256);

    function getTroveFromTroveIdsArray(uint256 _index) external view returns (uint256);

    function getCurrentICR(uint256 _troveId, uint256 _price) external view returns (uint256);

    function lastZombieTroveId() external view returns (uint256);

    function batchLiquidateTroves(uint256[] calldata _troveArray) external;

    function redeemCollateral(
        address _sender,
        uint256 _boldAmount,
        uint256 _price,
        uint256 _redemptionRate,
        uint256 _maxIterations
    ) external returns (uint256 _redemeedAmount);

    function shutdown() external;
    function urgentRedemption(uint256 _boldAmount, uint256[] calldata _troveIds, uint256 _minCollateral) external;

    function getUnbackedPortionPriceAndRedeemability() external returns (uint256, uint256, bool);

    function getLatestTroveData(uint256 _troveId) external view returns (LatestTroveData memory);
    function getTroveAnnualInterestRate(uint256 _troveId) external view returns (uint256);

    function getTroveStatus(uint256 _troveId) external view returns (Status);

    function getLatestBatchData(address _batchAddress) external view returns (LatestBatchData memory);

    // -- permissioned functions called by BorrowerOperations

    function onOpenTrove(address _owner, uint256 _troveId, TroveChange memory _troveChange, uint256 _annualInterestRate)
        external;
    function onOpenTroveAndJoinBatch(
        address _owner,
        uint256 _troveId,
        TroveChange memory _troveChange,
        address _batchAddress,
        uint256 _batchColl,
        uint256 _batchDebt
    ) external;

    // Called from `adjustZombieTrove()`
    function setTroveStatusToActive(uint256 _troveId) external;

    function onAdjustTroveInterestRate(
        uint256 _troveId,
        uint256 _newColl,
        uint256 _newDebt,
        uint256 _newAnnualInterestRate,
        TroveChange calldata _troveChange
    ) external;

    function onAdjustTrove(uint256 _troveId, uint256 _newColl, uint256 _newDebt, TroveChange calldata _troveChange)
        external;

    function onAdjustTroveInsideBatch(
        uint256 _troveId,
        uint256 _newTroveColl,
        uint256 _newTroveDebt,
        TroveChange memory _troveChange,
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt
    ) external;

    function onApplyTroveInterest(
        uint256 _troveId,
        uint256 _newTroveColl,
        uint256 _newTroveDebt,
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt,
        TroveChange calldata _troveChange
    ) external;

    function onCloseTrove(
        uint256 _troveId,
        TroveChange memory _troveChange, // decrease vars: entire, with interest, batch fee and redistribution
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt // entire, with interest and batch fee
    ) external;

    // -- batches --
    function onRegisterBatchManager(address _batchAddress, uint256 _annualInterestRate, uint256 _annualFee) external;
    function onLowerBatchManagerAnnualFee(
        address _batchAddress,
        uint256 _newColl,
        uint256 _newDebt,
        uint256 _newAnnualManagementFee
    ) external;
    function onSetBatchManagerAnnualInterestRate(
        address _batchAddress,
        uint256 _newColl,
        uint256 _newDebt,
        uint256 _newAnnualInterestRate,
        uint256 _upfrontFee // needed by BatchUpdated event
    ) external;

    struct OnSetInterestBatchManagerParams {
        uint256 troveId;
        uint256 troveColl; // entire, with redistribution
        uint256 troveDebt; // entire, with interest, batch fee and redistribution
        TroveChange troveChange;
        address newBatchAddress;
        uint256 newBatchColl; // updated collateral for new batch manager
        uint256 newBatchDebt; // updated debt for new batch manager
    }

    function onSetInterestBatchManager(OnSetInterestBatchManagerParams calldata _params) external;
    function onRemoveFromBatch(
        uint256 _troveId,
        uint256 _newTroveColl, // entire, with redistribution
        uint256 _newTroveDebt, // entire, with interest, batch fee and redistribution
        TroveChange memory _troveChange,
        address _batchAddress,
        uint256 _newBatchColl,
        uint256 _newBatchDebt, // entire, with interest and batch fee
        uint256 _newAnnualInterestRate
    ) external;

    // -- end of permissioned functions --
}

File 3 of 44 : IAddressesRegistry.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IActivePool.sol";
import "./IBoldToken.sol";
import "./IBorrowerOperations.sol";
import "./ICollSurplusPool.sol";
import "./IDefaultPool.sol";
import "./IHintHelpers.sol";
import "./IMultiTroveGetter.sol";
import "./ISortedTroves.sol";
import "./IStabilityPool.sol";
import "./ITroveManager.sol";
import "./ITroveNFT.sol";
import {IMetadataNFT} from "../NFTMetadata/MetadataNFT.sol";
import "./ICollateralRegistry.sol";
import "./IInterestRouter.sol";
import "./IPriceFeed.sol";

interface IAddressesRegistry {
    struct AddressVars {
        IERC20Metadata collToken;
        IBorrowerOperations borrowerOperations;
        ITroveManager troveManager;
        ITroveNFT troveNFT;
        IMetadataNFT metadataNFT;
        IStabilityPool stabilityPool;
        IPriceFeed priceFeed;
        IActivePool activePool;
        IDefaultPool defaultPool;
        address gasPoolAddress;
        ICollSurplusPool collSurplusPool;
        ISortedTroves sortedTroves;
        IInterestRouter interestRouter;
        IHintHelpers hintHelpers;
        IMultiTroveGetter multiTroveGetter;
        ICollateralRegistry collateralRegistry;
        IBoldToken boldToken;
        IWETH WETH;
    }

    function CCR() external returns (uint256);
    function SCR() external returns (uint256);
    function MCR() external returns (uint256);
    function LIQUIDATION_PENALTY_SP() external returns (uint256);
    function LIQUIDATION_PENALTY_REDISTRIBUTION() external returns (uint256);

    function collToken() external view returns (IERC20Metadata);
    function borrowerOperations() external view returns (IBorrowerOperations);
    function troveManager() external view returns (ITroveManager);
    function troveNFT() external view returns (ITroveNFT);
    function metadataNFT() external view returns (IMetadataNFT);
    function stabilityPool() external view returns (IStabilityPool);
    function priceFeed() external view returns (IPriceFeed);
    function activePool() external view returns (IActivePool);
    function defaultPool() external view returns (IDefaultPool);
    function gasPoolAddress() external view returns (address);
    function collSurplusPool() external view returns (ICollSurplusPool);
    function sortedTroves() external view returns (ISortedTroves);
    function interestRouter() external view returns (IInterestRouter);
    function hintHelpers() external view returns (IHintHelpers);
    function multiTroveGetter() external view returns (IMultiTroveGetter);
    function collateralRegistry() external view returns (ICollateralRegistry);
    function boldToken() external view returns (IBoldToken);
    function WETH() external returns (IWETH);

    function setAddresses(AddressVars memory _vars) external;
}

File 4 of 44 : IStabilityPool.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IActivePool.sol";
import "./ILiquityBase.sol";
import "./IBoldToken.sol";
import "./ITroveManager.sol";
import "./IBoldRewardsReceiver.sol";

/*
 * The Stability Pool holds Bold tokens deposited by Stability Pool depositors.
 *
 * When a trove is liquidated, then depending on system conditions, some of its Bold debt gets offset with
 * Bold in the Stability Pool:  that is, the offset debt evaporates, and an equal amount of Bold tokens in the Stability Pool is burned.
 *
 * Thus, a liquidation causes each depositor to receive a Bold loss, in proportion to their deposit as a share of total deposits.
 * They also receive an Coll gain, as the collateral of the liquidated trove is distributed among Stability depositors,
 * in the same proportion.
 *
 * When a liquidation occurs, it depletes every deposit by the same fraction: for example, a liquidation that depletes 40%
 * of the total Bold in the Stability Pool, depletes 40% of each deposit.
 *
 * A deposit that has experienced a series of liquidations is termed a "compounded deposit": each liquidation depletes the deposit,
 * multiplying it by some factor in range ]0,1[
 *
 * Please see the implementation spec in the proof document, which closely follows on from the compounded deposit / Coll gain derivations:
 * https://github.com/liquity/liquity/blob/master/papers/Scalable_Reward_Distribution_with_Compounding_Stakes.pdf
 *
*/
interface IStabilityPool is ILiquityBase, IBoldRewardsReceiver {
    function boldToken() external view returns (IBoldToken);
    function troveManager() external view returns (ITroveManager);

    /*  provideToSP():
    * - Calculates depositor's Coll gain
    * - Calculates the compounded deposit
    * - Increases deposit, and takes new snapshots of accumulators P and S
    * - Sends depositor's accumulated Coll gains to depositor
    */
    function provideToSP(uint256 _amount, bool _doClaim) external;

    /*  withdrawFromSP():
    * - Calculates depositor's Coll gain
    * - Calculates the compounded deposit
    * - Sends the requested BOLD withdrawal to depositor
    * - (If _amount > userDeposit, the user withdraws all of their compounded deposit)
    * - Decreases deposit by withdrawn amount and takes new snapshots of accumulators P and S
    */
    function withdrawFromSP(uint256 _amount, bool doClaim) external;

    function claimAllCollGains() external;

    /*
     * Initial checks:
     * - Caller is TroveManager
     * ---
     * Cancels out the specified debt against the Bold contained in the Stability Pool (as far as possible)
     * and transfers the Trove's collateral from ActivePool to StabilityPool.
     * Only called by liquidation functions in the TroveManager.
     */
    function offset(uint256 _debt, uint256 _coll) external;

    function deposits(address _depositor) external view returns (uint256 initialValue);
    function stashedColl(address _depositor) external view returns (uint256);

    /*
     * Returns the total amount of Coll held by the pool, accounted in an internal variable instead of `balance`,
     * to exclude edge cases like Coll received from a self-destruct.
     */
    function getCollBalance() external view returns (uint256);

    /*
     * Returns Bold held in the pool. Changes when users deposit/withdraw, and when Trove debt is offset.
     */
    function getTotalBoldDeposits() external view returns (uint256);

    function getYieldGainsOwed() external view returns (uint256);
    function getYieldGainsPending() external view returns (uint256);

    /*
     * Calculates the Coll gain earned by the deposit since its last snapshots were taken.
     */
    function getDepositorCollGain(address _depositor) external view returns (uint256);

    /*
     * Calculates the BOLD yield gain earned by the deposit since its last snapshots were taken.
     */
    function getDepositorYieldGain(address _depositor) external view returns (uint256);

    /*
     * Calculates what `getDepositorYieldGain` will be if interest is minted now.
     */
    function getDepositorYieldGainWithPending(address _depositor) external view returns (uint256);

    /*
     * Return the user's compounded deposit.
     */
    function getCompoundedBoldDeposit(address _depositor) external view returns (uint256);

    function epochToScaleToS(uint128 _epoch, uint128 _scale) external view returns (uint256);

    function epochToScaleToB(uint128 _epoch, uint128 _scale) external view returns (uint256);

    function P() external view returns (uint256);
    function currentScale() external view returns (uint128);
    function currentEpoch() external view returns (uint128);
}

File 5 of 44 : ICollSurplusPool.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface ICollSurplusPool {
    function getCollBalance() external view returns (uint256);

    function getCollateral(address _account) external view returns (uint256);

    function accountSurplus(address _account, uint256 _amount) external;

    function claimColl(address _account) external;
}

File 6 of 44 : IBoldToken.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {IERC5267} from "openzeppelin-contracts/contracts/interfaces/IERC5267.sol";

interface IBoldToken is IERC20Metadata, IERC5267 {
    function setBranchAddresses(
        address _troveManagerAddress,
        address _stabilityPoolAddress,
        address _borrowerOperationsAddress,
        address _activePoolAddress
    ) external;

    function setCollateralRegistry(address _collateralRegistryAddress) external;

    function mint(address _account, uint256 _amount) external;

    function burn(address _account, uint256 _amount) external;

    function sendToPool(address _sender, address poolAddress, uint256 _amount) external;

    function returnFromPool(address poolAddress, address user, uint256 _amount) external;
}

File 7 of 44 : ISortedTroves.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./ITroveManager.sol";
import {BatchId, BATCH_ID_ZERO} from "../Types/BatchId.sol";

interface ISortedTroves {
    // -- Mutating functions (permissioned) --
    function insert(uint256 _id, uint256 _annualInterestRate, uint256 _prevId, uint256 _nextId) external;
    function insertIntoBatch(
        uint256 _troveId,
        BatchId _batchId,
        uint256 _annualInterestRate,
        uint256 _prevId,
        uint256 _nextId
    ) external;

    function remove(uint256 _id) external;
    function removeFromBatch(uint256 _id) external;

    function reInsert(uint256 _id, uint256 _newAnnualInterestRate, uint256 _prevId, uint256 _nextId) external;
    function reInsertBatch(BatchId _id, uint256 _newAnnualInterestRate, uint256 _prevId, uint256 _nextId) external;

    // -- View functions --

    function contains(uint256 _id) external view returns (bool);
    function isBatchedNode(uint256 _id) external view returns (bool);
    function isEmptyBatch(BatchId _id) external view returns (bool);

    function isEmpty() external view returns (bool);
    function getSize() external view returns (uint256);

    function getFirst() external view returns (uint256);
    function getLast() external view returns (uint256);
    function getNext(uint256 _id) external view returns (uint256);
    function getPrev(uint256 _id) external view returns (uint256);

    function validInsertPosition(uint256 _annualInterestRate, uint256 _prevId, uint256 _nextId)
        external
        view
        returns (bool);
    function findInsertPosition(uint256 _annualInterestRate, uint256 _prevId, uint256 _nextId)
        external
        view
        returns (uint256, uint256);

    // Public state variable getters
    function borrowerOperationsAddress() external view returns (address);
    function troveManager() external view returns (ITroveManager);
    function size() external view returns (uint256);
    function nodes(uint256 _id) external view returns (uint256 nextId, uint256 prevId, BatchId batchId, bool exists);
    function batches(BatchId _id) external view returns (uint256 head, uint256 tail);
}

File 8 of 44 : ITroveEvents.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface ITroveEvents {
    enum Operation {
        openTrove,
        closeTrove,
        adjustTrove,
        adjustTroveInterestRate,
        applyPendingDebt,
        liquidate,
        redeemCollateral,
        // batch management
        openTroveAndJoinBatch,
        setInterestBatchManager,
        removeFromBatch
    }

    event Liquidation(
        uint256 _debtOffsetBySP,
        uint256 _debtRedistributed,
        uint256 _boldGasCompensation,
        uint256 _collGasCompensation,
        uint256 _collSentToSP,
        uint256 _collRedistributed,
        uint256 _collSurplus,
        uint256 _L_ETH,
        uint256 _L_boldDebt,
        uint256 _price
    );

    event Redemption(
        uint256 _attemptedBoldAmount, uint256 _actualBoldAmount, uint256 _ETHSent, uint256 _ETHFee, uint256 _price
    );

    // A snapshot of the Trove's latest state on-chain
    event TroveUpdated(
        uint256 indexed _troveId,
        uint256 _debt,
        uint256 _coll,
        uint256 _stake,
        uint256 _annualInterestRate,
        uint256 _snapshotOfTotalCollRedist,
        uint256 _snapshotOfTotalDebtRedist
    );

    // Details of an operation that modifies a Trove
    event TroveOperation(
        uint256 indexed _troveId,
        Operation _operation,
        uint256 _annualInterestRate,
        uint256 _debtIncreaseFromRedist,
        uint256 _debtIncreaseFromUpfrontFee,
        int256 _debtChangeFromOperation,
        uint256 _collIncreaseFromRedist,
        int256 _collChangeFromOperation
    );

    event RedemptionFeePaidToTrove(uint256 indexed _troveId, uint256 _ETHFee);

    // Batch management

    enum BatchOperation {
        registerBatchManager,
        lowerBatchManagerAnnualFee,
        setBatchManagerAnnualInterestRate,
        applyBatchInterestAndFee,
        joinBatch,
        exitBatch,
        // used when the batch is updated as a result of a Trove change inside the batch
        troveChange
    }

    event BatchUpdated(
        address indexed _interestBatchManager,
        BatchOperation _operation,
        uint256 _debt,
        uint256 _coll,
        uint256 _annualInterestRate,
        uint256 _annualManagementFee,
        uint256 _totalDebtShares,
        uint256 _debtIncreaseFromUpfrontFee
    );

    event BatchedTroveUpdated(
        uint256 indexed _troveId,
        address _interestBatchManager,
        uint256 _batchDebtShares,
        uint256 _coll,
        uint256 _stake,
        uint256 _snapshotOfTotalCollRedist,
        uint256 _snapshotOfTotalDebtRedist
    );
}

File 9 of 44 : ITroveNFT.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/token/ERC721/extensions/IERC721Metadata.sol";

import "./ITroveManager.sol";

interface ITroveNFT is IERC721Metadata {
    function mint(address _owner, uint256 _troveId) external;
    function burn(uint256 _troveId) external;
}

File 10 of 44 : ICollateralRegistry.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "./IBoldToken.sol";
import "./ITroveManager.sol";

interface ICollateralRegistry {
    function baseRate() external view returns (uint256);
    function lastFeeOperationTime() external view returns (uint256);

    function redeemCollateral(uint256 _boldamount, uint256 _maxIterations, uint256 _maxFeePercentage) external;
    // getters
    function totalCollaterals() external view returns (uint256);
    function getToken(uint256 _index) external view returns (IERC20Metadata);
    function getTroveManager(uint256 _index) external view returns (ITroveManager);
    function boldToken() external view returns (IBoldToken);

    function getRedemptionRate() external view returns (uint256);
    function getRedemptionRateWithDecay() external view returns (uint256);
    function getRedemptionRateForRedeemedAmount(uint256 _redeemAmount) external view returns (uint256);

    function getRedemptionFeeWithDecay(uint256 _ETHDrawn) external view returns (uint256);
    function getEffectiveRedemptionFeeInBold(uint256 _redeemAmount) external view returns (uint256);
}

File 11 of 44 : IWETH.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";

interface IWETH is IERC20Metadata {
    function deposit() external payable;
    function withdraw(uint256 wad) external;
}

File 12 of 44 : LiquityBase.sol
// SPDX-License-Identifier: BUSL-1.1

pragma solidity 0.8.24;

import "./Constants.sol";
import "./LiquityMath.sol";
import "../Interfaces/IAddressesRegistry.sol";
import "../Interfaces/IActivePool.sol";
import "../Interfaces/IDefaultPool.sol";
import "../Interfaces/IPriceFeed.sol";
import "../Interfaces/ILiquityBase.sol";

/*
* Base contract for TroveManager, BorrowerOperations and StabilityPool. Contains global system constants and
* common functions.
*/
contract LiquityBase is ILiquityBase {
    IActivePool public activePool;
    IDefaultPool internal defaultPool;
    IPriceFeed internal priceFeed;

    event ActivePoolAddressChanged(address _newActivePoolAddress);
    event DefaultPoolAddressChanged(address _newDefaultPoolAddress);
    event PriceFeedAddressChanged(address _newPriceFeedAddress);

    constructor(IAddressesRegistry _addressesRegistry) {
        activePool = _addressesRegistry.activePool();
        defaultPool = _addressesRegistry.defaultPool();
        priceFeed = _addressesRegistry.priceFeed();

        emit ActivePoolAddressChanged(address(activePool));
        emit DefaultPoolAddressChanged(address(defaultPool));
        emit PriceFeedAddressChanged(address(priceFeed));
    }
    // --- Gas compensation functions ---

    function getEntireSystemColl() public view returns (uint256 entireSystemColl) {
        uint256 activeColl = activePool.getCollBalance();
        uint256 liquidatedColl = defaultPool.getCollBalance();

        return activeColl + liquidatedColl;
    }

    function getEntireSystemDebt() public view returns (uint256 entireSystemDebt) {
        uint256 activeDebt = activePool.getBoldDebt();
        uint256 closedDebt = defaultPool.getBoldDebt();

        return activeDebt + closedDebt;
    }

    function _getTCR(uint256 _price) internal view returns (uint256 TCR) {
        uint256 entireSystemColl = getEntireSystemColl();
        uint256 entireSystemDebt = getEntireSystemDebt();

        TCR = LiquityMath._computeCR(entireSystemColl, entireSystemDebt, _price);

        return TCR;
    }

    function _checkBelowCriticalThreshold(uint256 _price, uint256 _CCR) internal view returns (bool) {
        uint256 TCR = _getTCR(_price);

        return TCR < _CCR;
    }

    function _calcInterest(uint256 _weightedDebt, uint256 _period) internal pure returns (uint256) {
        return _weightedDebt * _period / ONE_YEAR / DECIMAL_PRECISION;
    }
}

File 13 of 44 : ILiquityBase.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IActivePool.sol";
import "./IDefaultPool.sol";
import "./IPriceFeed.sol";

interface ILiquityBase {
    function activePool() external view returns (IActivePool);
    function getEntireSystemDebt() external view returns (uint256);
    function getEntireSystemColl() external view returns (uint256);
}

File 14 of 44 : IBorrowerOperations.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./ILiquityBase.sol";
import "./IAddRemoveManagers.sol";
import "./IBoldToken.sol";
import "./IPriceFeed.sol";
import "./ISortedTroves.sol";
import "./ITroveManager.sol";
import "./IWETH.sol";

// Common interface for the Borrower Operations.
interface IBorrowerOperations is ILiquityBase, IAddRemoveManagers {
    function CCR() external view returns (uint256);
    function MCR() external view returns (uint256);
    function SCR() external view returns (uint256);

    function openTrove(
        address _owner,
        uint256 _ownerIndex,
        uint256 _ETHAmount,
        uint256 _boldAmount,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _annualInterestRate,
        uint256 _maxUpfrontFee,
        address _addManager,
        address _removeManager,
        address _receiver
    ) external returns (uint256);

    struct OpenTroveAndJoinInterestBatchManagerParams {
        address owner;
        uint256 ownerIndex;
        uint256 collAmount;
        uint256 boldAmount;
        uint256 upperHint;
        uint256 lowerHint;
        address interestBatchManager;
        uint256 maxUpfrontFee;
        address addManager;
        address removeManager;
        address receiver;
    }

    function openTroveAndJoinInterestBatchManager(OpenTroveAndJoinInterestBatchManagerParams calldata _params)
        external
        returns (uint256);

    function addColl(uint256 _troveId, uint256 _ETHAmount) external;

    function withdrawColl(uint256 _troveId, uint256 _amount) external;

    function withdrawBold(uint256 _troveId, uint256 _amount, uint256 _maxUpfrontFee) external;

    function repayBold(uint256 _troveId, uint256 _amount) external;

    function closeTrove(uint256 _troveId) external;

    function adjustTrove(
        uint256 _troveId,
        uint256 _collChange,
        bool _isCollIncrease,
        uint256 _debtChange,
        bool isDebtIncrease,
        uint256 _maxUpfrontFee
    ) external;

    function adjustZombieTrove(
        uint256 _troveId,
        uint256 _collChange,
        bool _isCollIncrease,
        uint256 _boldChange,
        bool _isDebtIncrease,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _maxUpfrontFee
    ) external;

    function adjustTroveInterestRate(
        uint256 _troveId,
        uint256 _newAnnualInterestRate,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _maxUpfrontFee
    ) external;

    function applyPendingDebt(uint256 _troveId, uint256 _lowerHint, uint256 _upperHint) external;

    function onLiquidateTrove(uint256 _troveId) external;

    function claimCollateral() external;

    function hasBeenShutDown() external view returns (bool);
    function shutdown() external;
    function shutdownFromOracleFailure() external;

    function checkBatchManagerExists(address _batchMananger) external view returns (bool);

    // -- individual delegation --
    struct InterestIndividualDelegate {
        address account;
        uint128 minInterestRate;
        uint128 maxInterestRate;
        uint256 minInterestRateChangePeriod;
    }

    function getInterestIndividualDelegateOf(uint256 _troveId)
        external
        view
        returns (InterestIndividualDelegate memory);
    function setInterestIndividualDelegate(
        uint256 _troveId,
        address _delegate,
        uint128 _minInterestRate,
        uint128 _maxInterestRate,
        // only needed if trove was previously in a batch:
        uint256 _newAnnualInterestRate,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _maxUpfrontFee,
        uint256 _minInterestRateChangePeriod
    ) external;
    function removeInterestIndividualDelegate(uint256 _troveId) external;

    // -- batches --
    struct InterestBatchManager {
        uint128 minInterestRate;
        uint128 maxInterestRate;
        uint256 minInterestRateChangePeriod;
    }

    function registerBatchManager(
        uint128 minInterestRate,
        uint128 maxInterestRate,
        uint128 currentInterestRate,
        uint128 fee,
        uint128 minInterestRateChangePeriod
    ) external;
    function lowerBatchManagementFee(uint256 _newAnnualFee) external;
    function setBatchManagerAnnualInterestRate(
        uint128 _newAnnualInterestRate,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _maxUpfrontFee
    ) external;
    function interestBatchManagerOf(uint256 _troveId) external view returns (address);
    function getInterestBatchManager(address _account) external view returns (InterestBatchManager memory);
    function setInterestBatchManager(
        uint256 _troveId,
        address _newBatchManager,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _maxUpfrontFee
    ) external;
    function removeFromBatch(
        uint256 _troveId,
        uint256 _newAnnualInterestRate,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _maxUpfrontFee
    ) external;
    function switchBatchManager(
        uint256 _troveId,
        uint256 _removeUpperHint,
        uint256 _removeLowerHint,
        address _newBatchManager,
        uint256 _addUpperHint,
        uint256 _addLowerHint,
        uint256 _maxUpfrontFee
    ) external;
}

File 15 of 44 : LatestTroveData.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

struct LatestTroveData {
    uint256 entireDebt;
    uint256 entireColl;
    uint256 redistBoldDebtGain;
    uint256 redistCollGain;
    uint256 accruedInterest;
    uint256 recordedDebt;
    uint256 annualInterestRate;
    uint256 weightedRecordedDebt;
    uint256 accruedBatchManagementFee;
    uint256 lastInterestRateAdjTime;
}

File 16 of 44 : LatestBatchData.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

struct LatestBatchData {
    uint256 entireDebtWithoutRedistribution;
    uint256 entireCollWithoutRedistribution;
    uint256 accruedInterest;
    uint256 recordedDebt;
    uint256 annualInterestRate;
    uint256 weightedRecordedDebt;
    uint256 annualManagementFee;
    uint256 accruedManagementFee;
    uint256 weightedRecordedBatchManagementFee;
    uint256 lastDebtUpdateTime;
    uint256 lastInterestRateAdjTime;
}

File 17 of 44 : IActivePool.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IInterestRouter.sol";
import "./IBoldRewardsReceiver.sol";
import "../Types/TroveChange.sol";

interface IActivePool {
    function defaultPoolAddress() external view returns (address);
    function borrowerOperationsAddress() external view returns (address);
    function troveManagerAddress() external view returns (address);
    function interestRouter() external view returns (IInterestRouter);
    // We avoid IStabilityPool here in order to prevent creating a dependency cycle that would break flattening
    function stabilityPool() external view returns (IBoldRewardsReceiver);

    function getCollBalance() external view returns (uint256);
    function getBoldDebt() external view returns (uint256);
    function lastAggUpdateTime() external view returns (uint256);
    function aggRecordedDebt() external view returns (uint256);
    function aggWeightedDebtSum() external view returns (uint256);
    function aggBatchManagementFees() external view returns (uint256);
    function aggWeightedBatchManagementFeeSum() external view returns (uint256);
    function calcPendingAggInterest() external view returns (uint256);
    function calcPendingSPYield() external view returns (uint256);
    function calcPendingAggBatchManagementFee() external view returns (uint256);
    function getNewApproxAvgInterestRateFromTroveChange(TroveChange calldata _troveChange)
        external
        view
        returns (uint256);

    function mintAggInterest() external;
    function mintAggInterestAndAccountForTroveChange(TroveChange calldata _troveChange, address _batchManager)
        external;
    function mintBatchManagementFeeAndAccountForChange(TroveChange calldata _troveChange, address _batchAddress)
        external;

    function setShutdownFlag() external;
    function hasBeenShutDown() external view returns (bool);
    function shutdownTime() external view returns (uint256);

    function sendColl(address _account, uint256 _amount) external;
    function sendCollToDefaultPool(uint256 _amount) external;
    function receiveColl(uint256 _amount) external;
    function accountForReceivedColl(uint256 _amount) external;
}

File 18 of 44 : IDefaultPool.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IDefaultPool {
    function troveManagerAddress() external view returns (address);
    function activePoolAddress() external view returns (address);
    // --- Functions ---
    function getCollBalance() external view returns (uint256);
    function getBoldDebt() external view returns (uint256);
    function sendCollToActivePool(uint256 _amount) external;
    function receiveColl(uint256 _amount) external;

    function increaseBoldDebt(uint256 _amount) external;
    function decreaseBoldDebt(uint256 _amount) external;
}

File 19 of 44 : IHintHelpers.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IHintHelpers {
    function getApproxHint(uint256 _collIndex, uint256 _interestRate, uint256 _numTrials, uint256 _inputRandomSeed)
        external
        view
        returns (uint256 hintId, uint256 diff, uint256 latestRandomSeed);

    function predictOpenTroveUpfrontFee(uint256 _collIndex, uint256 _borrowedAmount, uint256 _interestRate)
        external
        view
        returns (uint256);

    function predictAdjustInterestRateUpfrontFee(uint256 _collIndex, uint256 _troveId, uint256 _newInterestRate)
        external
        view
        returns (uint256);

    function forcePredictAdjustInterestRateUpfrontFee(uint256 _collIndex, uint256 _troveId, uint256 _newInterestRate)
        external
        view
        returns (uint256);

    function predictAdjustTroveUpfrontFee(uint256 _collIndex, uint256 _troveId, uint256 _debtIncrease)
        external
        view
        returns (uint256);

    function predictAdjustBatchInterestRateUpfrontFee(
        uint256 _collIndex,
        address _batchAddress,
        uint256 _newInterestRate
    ) external view returns (uint256);

    function predictJoinBatchInterestRateUpfrontFee(uint256 _collIndex, uint256 _troveId, address _batchAddress)
        external
        view
        returns (uint256);
}

File 20 of 44 : IMultiTroveGetter.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IMultiTroveGetter {
    struct CombinedTroveData {
        uint256 id;
        uint256 debt;
        uint256 coll;
        uint256 stake;
        uint256 annualInterestRate;
        uint256 lastDebtUpdateTime;
        uint256 lastInterestRateAdjTime;
        address interestBatchManager;
        uint256 batchDebtShares;
        uint256 batchCollShares;
        uint256 snapshotETH;
        uint256 snapshotBoldDebt;
    }

    struct DebtPerInterestRate {
        address interestBatchManager;
        uint256 interestRate;
        uint256 debt;
    }

    function getMultipleSortedTroves(uint256 _collIndex, int256 _startIdx, uint256 _count)
        external
        view
        returns (CombinedTroveData[] memory _troves);

    function getDebtPerInterestRateAscending(uint256 _collIndex, uint256 _startId, uint256 _maxIterations)
        external
        view
        returns (DebtPerInterestRate[] memory, uint256 currId);
}

File 21 of 44 : MetadataNFT.sol
//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "lib/Solady/src/utils/SSTORE2.sol";
import "./utils/JSON.sol";

import "./utils/baseSVG.sol";
import "./utils/bauhaus.sol";

import "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import {ITroveManager} from "src/Interfaces/ITroveManager.sol";

interface IMetadataNFT {
    struct TroveData {
        uint256 _tokenId;
        address _owner;
        address _collToken;
        address _boldToken;
        uint256 _collAmount;
        uint256 _debtAmount;
        uint256 _interestRate;
        ITroveManager.Status _status;
    }

    function uri(TroveData memory _troveData) external view returns (string memory);
}

contract MetadataNFT is IMetadataNFT {
    FixedAssetReader public immutable assetReader;

    string public constant name = "Liquity V2 Trove";
    string public constant description = "Liquity V2 Trove position";

    constructor(FixedAssetReader _assetReader) {
        assetReader = _assetReader;
    }

    function uri(TroveData memory _troveData) public view returns (string memory) {
        string memory attr = attributes(_troveData);
        return json.formattedMetadata(name, description, renderSVGImage(_troveData), attr);
    }

    function renderSVGImage(TroveData memory _troveData) internal view returns (string memory) {
        return svg._svg(
            baseSVG._svgProps(),
            string.concat(
                baseSVG._baseElements(assetReader),
                bauhaus._bauhaus(IERC20Metadata(_troveData._collToken).symbol(), _troveData._tokenId),
                dynamicTextComponents(_troveData)
            )
        );
    }

    function attributes(TroveData memory _troveData) public pure returns (string memory) {
        //include: collateral token address, collateral amount, debt token address, debt amount, interest rate, status
        return string.concat(
            '[{"trait_type": "Collateral Token", "value": "',
            LibString.toHexString(_troveData._collToken),
            '"}, {"trait_type": "Collateral Amount", "value": "',
            LibString.toString(_troveData._collAmount),
            '"}, {"trait_type": "Debt Token", "value": "',
            LibString.toHexString(_troveData._boldToken),
            '"}, {"trait_type": "Debt Amount", "value": "',
            LibString.toString(_troveData._debtAmount),
            '"}, {"trait_type": "Interest Rate", "value": "',
            LibString.toString(_troveData._interestRate),
            '"}, {"trait_type": "Status", "value": "',
            _status2Str(_troveData._status),
            '"} ]'
        );
    }

    function dynamicTextComponents(TroveData memory _troveData) public view returns (string memory) {
        string memory id = LibString.toHexString(_troveData._tokenId);
        id = string.concat(LibString.slice(id, 0, 6), "...", LibString.slice(id, 38, 42));

        return string.concat(
            baseSVG._formattedIdEl(id),
            baseSVG._formattedAddressEl(_troveData._owner),
            baseSVG._collLogo(IERC20Metadata(_troveData._collToken).symbol(), assetReader),
            baseSVG._statusEl(_status2Str(_troveData._status)),
            baseSVG._dynamicTextEls(_troveData._debtAmount, _troveData._collAmount, _troveData._interestRate)
        );
    }

    function _status2Str(ITroveManager.Status status) internal pure returns (string memory) {
        if (status == ITroveManager.Status.active) return "Active";
        if (status == ITroveManager.Status.closedByOwner) return "Closed";
        if (status == ITroveManager.Status.closedByLiquidation) return "Liquidated";
        if (status == ITroveManager.Status.zombie) return "Below Min Debt";
        return "";
    }
}

File 22 of 44 : IInterestRouter.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IInterestRouter {
// Currently the Interest Router doesn’t need any specific function
}

File 23 of 44 : IPriceFeed.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IPriceFeed {
    function fetchPrice() external returns (uint256, bool);
    function fetchRedemptionPrice() external returns (uint256, bool);
    function lastGoodPrice() external view returns (uint256);
    function setAddresses(address _borrowerOperationsAddress) external;
}

File 24 of 44 : IBoldRewardsReceiver.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IBoldRewardsReceiver {
    function triggerBoldRewards(uint256 _boldYield) external;
}

File 25 of 44 : IERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.20;

import {IERC20} from "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC-20 standard.
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}

File 26 of 44 : IERC5267.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC5267.sol)

pragma solidity ^0.8.20;

interface IERC5267 {
    /**
     * @dev MAY be emitted to signal that the domain could have changed.
     */
    event EIP712DomainChanged();

    /**
     * @dev returns the fields and values that describe the domain separator used by this contract for EIP-712
     * signature.
     */
    function eip712Domain()
        external
        view
        returns (
            bytes1 fields,
            string memory name,
            string memory version,
            uint256 chainId,
            address verifyingContract,
            bytes32 salt,
            uint256[] memory extensions
        );
}

File 27 of 44 : BatchId.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

type BatchId is address;

using {equals as ==, notEquals as !=, isZero, isNotZero} for BatchId global;

function equals(BatchId a, BatchId b) pure returns (bool) {
    return BatchId.unwrap(a) == BatchId.unwrap(b);
}

function notEquals(BatchId a, BatchId b) pure returns (bool) {
    return !(a == b);
}

function isZero(BatchId x) pure returns (bool) {
    return x == BATCH_ID_ZERO;
}

function isNotZero(BatchId x) pure returns (bool) {
    return !x.isZero();
}

BatchId constant BATCH_ID_ZERO = BatchId.wrap(address(0));

File 28 of 44 : IERC721Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/IERC721Metadata.sol)

pragma solidity ^0.8.20;

import {IERC721} from "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional metadata extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Metadata is IERC721 {
    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);
}

File 29 of 44 : Constants.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.24;

address constant ZERO_ADDRESS = address(0);

uint256 constant MAX_UINT256 = type(uint256).max;

uint256 constant DECIMAL_PRECISION = 1e18;
uint256 constant _100pct = DECIMAL_PRECISION;
uint256 constant _1pct = DECIMAL_PRECISION / 100;

// Amount of ETH to be locked in gas pool on opening troves
uint256 constant ETH_GAS_COMPENSATION = 0.0375 ether;

// Fraction of collateral awarded to liquidator
uint256 constant COLL_GAS_COMPENSATION_DIVISOR = 200; // dividing by 200 yields 0.5%
uint256 constant COLL_GAS_COMPENSATION_CAP = 2 ether; // Max coll gas compensation capped at 2 ETH

// Minimum amount of net Bold debt a trove must have
uint256 constant MIN_DEBT = 2000e18;

uint256 constant MIN_ANNUAL_INTEREST_RATE = _1pct / 2; // 0.5%
uint256 constant MAX_ANNUAL_INTEREST_RATE = _100pct;

// Batch management params
uint128 constant MAX_ANNUAL_BATCH_MANAGEMENT_FEE = uint128(_100pct);
uint128 constant MIN_INTEREST_RATE_CHANGE_PERIOD = 1 seconds; // prevents more than one adjustment per block

uint256 constant REDEMPTION_FEE_FLOOR = _1pct / 2; // 0.5%

// For the debt / shares ratio to increase by a factor 1e9
// at a average annual debt increase (compounded interest + fees) of 10%, it would take more than 217 years (log(1e9)/log(1.1))
// at a average annual debt increase (compounded interest + fees) of 50%, it would take more than 51 years (log(1e9)/log(1.5))
// The increase pace could be forced to be higher through an inflation attack,
// but precisely the fact that we have this max value now prevents the attack
uint256 constant MAX_BATCH_SHARES_RATIO = 1e9;

// Half-life of 12h. 12h = 720 min
// (1/2) = d^720 => d = (1/2)^(1/720)
uint256 constant REDEMPTION_MINUTE_DECAY_FACTOR = 999037758833783000;

// BETA: 18 digit decimal. Parameter by which to divide the redeemed fraction, in order to calc the new base rate from a redemption.
// Corresponds to (1 / ALPHA) in the white paper.
uint256 constant REDEMPTION_BETA = 2;

// To prevent redemptions unless Bold depegs below 0.95 and allow the system to take off
uint256 constant INITIAL_BASE_RATE = 5 * _1pct - REDEMPTION_FEE_FLOOR; // 5% initial redemption rate

// Discount to be used once the shutdown thas been triggered
uint256 constant URGENT_REDEMPTION_BONUS = 1e16; // 1%

uint256 constant ONE_MINUTE = 1 minutes;
uint256 constant ONE_YEAR = 365 days;
uint256 constant UPFRONT_INTEREST_PERIOD = 7 days;
uint256 constant INTEREST_RATE_ADJ_COOLDOWN = 3 days;

uint256 constant SP_YIELD_SPLIT = 72 * _1pct; // 72%

// Dummy contract that lets legacy Hardhat tests query some of the constants
contract Constants {
    uint256 public constant _ETH_GAS_COMPENSATION = ETH_GAS_COMPENSATION;
    uint256 public constant _MIN_DEBT = MIN_DEBT;
}

File 30 of 44 : LiquityMath.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

import {DECIMAL_PRECISION} from "./Constants.sol";

library LiquityMath {
    function _min(uint256 _a, uint256 _b) internal pure returns (uint256) {
        return (_a < _b) ? _a : _b;
    }

    function _max(uint256 _a, uint256 _b) internal pure returns (uint256) {
        return (_a >= _b) ? _a : _b;
    }

    function _sub_min_0(uint256 _a, uint256 _b) internal pure returns (uint256) {
        return (_a > _b) ? _a - _b : 0;
    }

    /* 
    * Multiply two decimal numbers and use normal rounding rules:
    * -round product up if 19'th mantissa digit >= 5
    * -round product down if 19'th mantissa digit < 5
    *
    * Used only inside the exponentiation, _decPow().
    */
    function decMul(uint256 x, uint256 y) internal pure returns (uint256 decProd) {
        uint256 prod_xy = x * y;

        decProd = (prod_xy + DECIMAL_PRECISION / 2) / DECIMAL_PRECISION;
    }

    /* 
    * _decPow: Exponentiation function for 18-digit decimal base, and integer exponent n.
    * 
    * Uses the efficient "exponentiation by squaring" algorithm. O(log(n)) complexity. 
    * 
    * Called by function CollateralRegistry._calcDecayedBaseRate, that represent time in units of minutes
    *
    * The exponent is capped to avoid reverting due to overflow. The cap 525600000 equals
    * "minutes in 1000 years": 60 * 24 * 365 * 1000
    * 
    * If a period of > 1000 years is ever used as an exponent in either of the above functions, the result will be
    * negligibly different from just passing the cap, since: 
    *
    * In function 1), the decayed base rate will be 0 for 1000 years or > 1000 years
    * In function 2), the difference in tokens issued at 1000 years and any time > 1000 years, will be negligible
    */
    function _decPow(uint256 _base, uint256 _minutes) internal pure returns (uint256) {
        if (_minutes > 525600000) _minutes = 525600000; // cap to avoid overflow

        if (_minutes == 0) return DECIMAL_PRECISION;

        uint256 y = DECIMAL_PRECISION;
        uint256 x = _base;
        uint256 n = _minutes;

        // Exponentiation-by-squaring
        while (n > 1) {
            if (n % 2 == 0) {
                x = decMul(x, x);
                n = n / 2;
            } else {
                // if (n % 2 != 0)
                y = decMul(x, y);
                x = decMul(x, x);
                n = (n - 1) / 2;
            }
        }

        return decMul(x, y);
    }

    function _getAbsoluteDifference(uint256 _a, uint256 _b) internal pure returns (uint256) {
        return (_a >= _b) ? _a - _b : _b - _a;
    }

    function _computeCR(uint256 _coll, uint256 _debt, uint256 _price) internal pure returns (uint256) {
        if (_debt > 0) {
            uint256 newCollRatio = _coll * _price / _debt;

            return newCollRatio;
        }
        // Return the maximal value for uint256 if the debt is 0. Represents "infinite" CR.
        else {
            // if (_debt == 0)
            return 2 ** 256 - 1;
        }
    }
}

File 31 of 44 : IAddRemoveManagers.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IAddRemoveManagers {
    function setAddManager(uint256 _troveId, address _manager) external;
    function setRemoveManager(uint256 _troveId, address _manager) external;
    function setRemoveManagerWithReceiver(uint256 _troveId, address _manager, address _receiver) external;
    function addManagerOf(uint256 _troveId) external view returns (address);
    function removeManagerReceiverOf(uint256 _troveId) external view returns (address, address);
}

File 32 of 44 : TroveChange.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

struct TroveChange {
    uint256 appliedRedistBoldDebtGain;
    uint256 appliedRedistCollGain;
    uint256 collIncrease;
    uint256 collDecrease;
    uint256 debtIncrease;
    uint256 debtDecrease;
    uint256 newWeightedRecordedDebt;
    uint256 oldWeightedRecordedDebt;
    uint256 upfrontFee;
    uint256 batchAccruedManagementFee;
    uint256 newWeightedRecordedBatchManagementFee;
    uint256 oldWeightedRecordedBatchManagementFee;
}

File 33 of 44 : SSTORE2.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

/// @notice Read and write to persistent storage at a fraction of the cost.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SSTORE2.sol)
/// @author Saw-mon-and-Natalie (https://github.com/Saw-mon-and-Natalie)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SSTORE2.sol)
/// @author Modified from 0xSequence (https://github.com/0xSequence/sstore2/blob/master/contracts/SSTORE2.sol)
/// @author Modified from SSTORE3 (https://github.com/Philogy/sstore3)
library SSTORE2 {
    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                         CONSTANTS                          */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev The proxy initialization code.
    uint256 private constant _CREATE3_PROXY_INITCODE = 0x67363d3d37363d34f03d5260086018f3;

    /// @dev Hash of the `_CREATE3_PROXY_INITCODE`.
    /// Equivalent to `keccak256(abi.encodePacked(hex"67363d3d37363d34f03d5260086018f3"))`.
    bytes32 internal constant CREATE3_PROXY_INITCODE_HASH =
        0x21c35dbe1b344a2488cf3321d6ce542f8e9f305544ff09e4993a62319a497c1f;

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                        CUSTOM ERRORS                       */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Unable to deploy the storage contract.
    error DeploymentFailed();

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                         WRITE LOGIC                        */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Writes `data` into the bytecode of a storage contract and returns its address.
    function write(bytes memory data) internal returns (address pointer) {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(data) // Let `l` be `n + 1`. +1 as we prefix a STOP opcode.
            /**
             * ---------------------------------------------------+
             * Opcode | Mnemonic       | Stack     | Memory       |
             * ---------------------------------------------------|
             * 61 l   | PUSH2 l        | l         |              |
             * 80     | DUP1           | l l       |              |
             * 60 0xa | PUSH1 0xa      | 0xa l l   |              |
             * 3D     | RETURNDATASIZE | 0 0xa l l |              |
             * 39     | CODECOPY       | l         | [0..l): code |
             * 3D     | RETURNDATASIZE | 0 l       | [0..l): code |
             * F3     | RETURN         |           | [0..l): code |
             * 00     | STOP           |           |              |
             * ---------------------------------------------------+
             * @dev Prefix the bytecode with a STOP opcode to ensure it cannot be called.
             * Also PUSH2 is used since max contract size cap is 24,576 bytes which is less than 2 ** 16.
             */
            // Do a out-of-gas revert if `n + 1` is more than 2 bytes.
            mstore(add(data, gt(n, 0xfffe)), add(0xfe61000180600a3d393df300, shl(0x40, n)))
            // Deploy a new contract with the generated creation code.
            pointer := create(0, add(data, 0x15), add(n, 0xb))
            if iszero(pointer) {
                mstore(0x00, 0x30116425) // `DeploymentFailed()`.
                revert(0x1c, 0x04)
            }
            mstore(data, n) // Restore the length of `data`.
        }
    }

    /// @dev Writes `data` into the bytecode of a storage contract with `salt`
    /// and returns its normal CREATE2 deterministic address.
    function writeCounterfactual(bytes memory data, bytes32 salt)
        internal
        returns (address pointer)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(data)
            // Do a out-of-gas revert if `n + 1` is more than 2 bytes.
            mstore(add(data, gt(n, 0xfffe)), add(0xfe61000180600a3d393df300, shl(0x40, n)))
            // Deploy a new contract with the generated creation code.
            pointer := create2(0, add(data, 0x15), add(n, 0xb), salt)
            if iszero(pointer) {
                mstore(0x00, 0x30116425) // `DeploymentFailed()`.
                revert(0x1c, 0x04)
            }
            mstore(data, n) // Restore the length of `data`.
        }
    }

    /// @dev Writes `data` into the bytecode of a storage contract and returns its address.
    /// This uses the so-called "CREATE3" workflow,
    /// which means that `pointer` is agnostic to `data, and only depends on `salt`.
    function writeDeterministic(bytes memory data, bytes32 salt)
        internal
        returns (address pointer)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(data)
            mstore(0x00, _CREATE3_PROXY_INITCODE) // Store the `_PROXY_INITCODE`.
            let proxy := create2(0, 0x10, 0x10, salt)
            if iszero(proxy) {
                mstore(0x00, 0x30116425) // `DeploymentFailed()`.
                revert(0x1c, 0x04)
            }
            mstore(0x14, proxy) // Store the proxy's address.
            // 0xd6 = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x01).
            // 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex).
            mstore(0x00, 0xd694)
            mstore8(0x34, 0x01) // Nonce of the proxy contract (1).
            pointer := keccak256(0x1e, 0x17)

            // Do a out-of-gas revert if `n + 1` is more than 2 bytes.
            mstore(add(data, gt(n, 0xfffe)), add(0xfe61000180600a3d393df300, shl(0x40, n)))
            if iszero(
                mul( // The arguments of `mul` are evaluated last to first.
                    extcodesize(pointer),
                    call(gas(), proxy, 0, add(data, 0x15), add(n, 0xb), codesize(), 0x00)
                )
            ) {
                mstore(0x00, 0x30116425) // `DeploymentFailed()`.
                revert(0x1c, 0x04)
            }
            mstore(data, n) // Restore the length of `data`.
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                    ADDRESS CALCULATIONS                    */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Returns the initialization code hash of the storage contract for `data`.
    /// Used for mining vanity addresses with create2crunch.
    function initCodeHash(bytes memory data) internal pure returns (bytes32 hash) {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(data)
            // Do a out-of-gas revert if `n + 1` is more than 2 bytes.
            returndatacopy(returndatasize(), returndatasize(), gt(n, 0xfffe))
            mstore(data, add(0x61000180600a3d393df300, shl(0x40, n)))
            hash := keccak256(add(data, 0x15), add(n, 0xb))
            mstore(data, n) // Restore the length of `data`.
        }
    }

    /// @dev Equivalent to `predictCounterfactualAddress(data, salt, address(this))`
    function predictCounterfactualAddress(bytes memory data, bytes32 salt)
        internal
        view
        returns (address pointer)
    {
        pointer = predictCounterfactualAddress(data, salt, address(this));
    }

    /// @dev Returns the CREATE2 address of the storage contract for `data`
    /// deployed with `salt` by `deployer`.
    /// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly.
    function predictCounterfactualAddress(bytes memory data, bytes32 salt, address deployer)
        internal
        pure
        returns (address predicted)
    {
        bytes32 hash = initCodeHash(data);
        /// @solidity memory-safe-assembly
        assembly {
            // Compute and store the bytecode hash.
            mstore8(0x00, 0xff) // Write the prefix.
            mstore(0x35, hash)
            mstore(0x01, shl(96, deployer))
            mstore(0x15, salt)
            predicted := keccak256(0x00, 0x55)
            // Restore the part of the free memory pointer that has been overwritten.
            mstore(0x35, 0)
        }
    }

    /// @dev Equivalent to `predictDeterministicAddress(salt, address(this))`.
    function predictDeterministicAddress(bytes32 salt) internal view returns (address pointer) {
        pointer = predictDeterministicAddress(salt, address(this));
    }

    /// @dev Returns the "CREATE3" deterministic address for `salt` with `deployer`.
    function predictDeterministicAddress(bytes32 salt, address deployer)
        internal
        pure
        returns (address pointer)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let m := mload(0x40) // Cache the free memory pointer.
            mstore(0x00, deployer) // Store `deployer`.
            mstore8(0x0b, 0xff) // Store the prefix.
            mstore(0x20, salt) // Store the salt.
            mstore(0x40, CREATE3_PROXY_INITCODE_HASH) // Store the bytecode hash.

            mstore(0x14, keccak256(0x0b, 0x55)) // Store the proxy's address.
            mstore(0x40, m) // Restore the free memory pointer.
            // 0xd6 = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x01).
            // 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex).
            mstore(0x00, 0xd694)
            mstore8(0x34, 0x01) // Nonce of the proxy contract (1).
            pointer := keccak256(0x1e, 0x17)
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                         READ LOGIC                         */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Equivalent to `read(pointer, 0, 2 ** 256 - 1)`.
    function read(address pointer) internal view returns (bytes memory data) {
        /// @solidity memory-safe-assembly
        assembly {
            data := mload(0x40)
            let n := and(0xffffffffff, sub(extcodesize(pointer), 0x01))
            extcodecopy(pointer, add(data, 0x1f), 0x00, add(n, 0x21))
            mstore(data, n) // Store the length.
            mstore(0x40, add(n, add(data, 0x40))) // Allocate memory.
        }
    }

    /// @dev Equivalent to `read(pointer, start, 2 ** 256 - 1)`.
    function read(address pointer, uint256 start) internal view returns (bytes memory data) {
        /// @solidity memory-safe-assembly
        assembly {
            data := mload(0x40)
            let n := and(0xffffffffff, sub(extcodesize(pointer), 0x01))
            extcodecopy(pointer, add(data, 0x1f), start, add(n, 0x21))
            mstore(data, mul(sub(n, start), lt(start, n))) // Store the length.
            mstore(0x40, add(data, add(0x40, mload(data)))) // Allocate memory.
        }
    }

    /// @dev Returns a slice of the data on `pointer` from `start` to `end`.
    /// `start` and `end` will be clamped to the range `[0, args.length]`.
    /// The `pointer` MUST be deployed via the SSTORE2 write functions.
    /// Otherwise, the behavior is undefined.
    /// Out-of-gas reverts if `pointer` does not have any code.
    function read(address pointer, uint256 start, uint256 end)
        internal
        view
        returns (bytes memory data)
    {
        /// @solidity memory-safe-assembly
        assembly {
            data := mload(0x40)
            if iszero(lt(end, 0xffff)) { end := 0xffff }
            let d := mul(sub(end, start), lt(start, end))
            extcodecopy(pointer, add(data, 0x1f), start, add(d, 0x01))
            if iszero(and(0xff, mload(add(data, d)))) {
                let n := sub(extcodesize(pointer), 0x01)
                returndatacopy(returndatasize(), returndatasize(), shr(40, n))
                d := mul(gt(n, start), sub(d, mul(gt(end, n), sub(end, n))))
            }
            mstore(data, d) // Store the length.
            mstore(add(add(data, 0x20), d), 0) // Zeroize the slot after the bytes.
            mstore(0x40, add(add(data, 0x40), d)) // Allocate memory.
        }
    }
}

File 34 of 44 : JSON.sol
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

// JSON utilities for base64 encoded ERC721 JSON metadata scheme
library json {
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// @dev JSON requires that double quotes be escaped or JSONs will not build correctly
    /// string.concat also requires an escape, use \\" or the constant DOUBLE_QUOTES to represent " in JSON
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    string constant DOUBLE_QUOTES = '\\"';

    function formattedMetadata(
        string memory name,
        string memory description,
        string memory svgImg,
        string memory attributes
    ) internal pure returns (string memory) {
        return string.concat(
            "data:application/json;base64,",
            encode(
                bytes(
                    string.concat(
                        "{",
                        _prop("name", name),
                        _prop("description", description),
                        _xmlImage(svgImg),
                        ',"attributes":',
                        attributes,
                        "}"
                    )
                )
            )
        );
    }

    function _xmlImage(string memory _svgImg) internal pure returns (string memory) {
        return _prop("image", string.concat("data:image/svg+xml;base64,", encode(bytes(_svgImg))), true);
    }

    function _prop(string memory _key, string memory _val) internal pure returns (string memory) {
        return string.concat('"', _key, '": ', '"', _val, '", ');
    }

    function _prop(string memory _key, string memory _val, bool last) internal pure returns (string memory) {
        if (last) {
            return string.concat('"', _key, '": ', '"', _val, '"');
        } else {
            return string.concat('"', _key, '": ', '"', _val, '", ');
        }
    }

    function _object(string memory _key, string memory _val) internal pure returns (string memory) {
        return string.concat('"', _key, '": ', "{", _val, "}");
    }

    /**
     * taken from Openzeppelin
     * @dev Base64 Encoding/Decoding Table
     */
    string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    /**
     * @dev Converts a `bytes` to its Bytes64 `string` representation.
     */
    function encode(bytes memory data) internal pure returns (string memory) {
        /**
         * Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
         * https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
         */
        if (data.length == 0) return "";

        // Loads the table into memory
        string memory table = _TABLE;

        // Encoding takes 3 bytes chunks of binary data from `bytes` data parameter
        // and split into 4 numbers of 6 bits.
        // The final Base64 length should be `bytes` data length multiplied by 4/3 rounded up
        // - `data.length + 2`  -> Round up
        // - `/ 3`              -> Number of 3-bytes chunks
        // - `4 *`              -> 4 characters for each chunk
        string memory result = new string(4 * ((data.length + 2) / 3));

        assembly {
            // Prepare the lookup table (skip the first "length" byte)
            let tablePtr := add(table, 1)

            // Prepare result pointer, jump over length
            let resultPtr := add(result, 32)

            // Run over the input, 3 bytes at a time
            for {
                let dataPtr := data
                let endPtr := add(data, mload(data))
            } lt(dataPtr, endPtr) {} {
                // Advance 3 bytes
                dataPtr := add(dataPtr, 3)
                let input := mload(dataPtr)

                // To write each character, shift the 3 bytes (18 bits) chunk
                // 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
                // and apply logical AND with 0x3F which is the number of
                // the previous character in the ASCII table prior to the Base64 Table
                // The result is then added to the table to get the character to write,
                // and finally write it in the result pointer but with a left shift
                // of 256 (1 byte) - 8 (1 ASCII char) = 248 bits

                mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance

                mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance

                mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance

                mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance
            }

            // When data `bytes` is not exactly 3 bytes long
            // it is padded with `=` characters at the end
            switch mod(mload(data), 3)
            case 1 {
                mstore8(sub(resultPtr, 1), 0x3d)
                mstore8(sub(resultPtr, 2), 0x3d)
            }
            case 2 { mstore8(sub(resultPtr, 1), 0x3d) }
        }

        return result;
    }
}

File 35 of 44 : baseSVG.sol
//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {svg} from "./SVG.sol";
import {utils, LibString, numUtils} from "./Utils.sol";
import "./FixedAssets.sol";

library baseSVG {
    string constant GEIST = 'style="font-family: Geist" ';
    string constant DARK_BLUE = "#121B44";
    string constant STOIC_WHITE = "#DEE4FB";

    function _svgProps() internal pure returns (string memory) {
        return string.concat(
            svg.prop("width", "300"),
            svg.prop("height", "484"),
            svg.prop("viewBox", "0 0 300 484"),
            svg.prop("style", "background:none")
        );
    }

    function _baseElements(FixedAssetReader _assetReader) internal view returns (string memory) {
        return string.concat(
            svg.rect(
                string.concat(
                    svg.prop("fill", DARK_BLUE),
                    svg.prop("rx", "8"),
                    svg.prop("width", "300"),
                    svg.prop("height", "484")
                )
            ),
            _styles(_assetReader),
            _leverageLogo(),
            _boldLogo(_assetReader),
            _staticTextEls()
        );
    }

    function _styles(FixedAssetReader _assetReader) private view returns (string memory) {
        return svg.el(
            "style",
            utils.NULL,
            string.concat(
                '@font-face { font-family: "Geist"; src: url("data:font/woff2;utf-8;base64,',
                _assetReader.readAsset(bytes4(keccak256("geist"))),
                '"); }'
            )
        );
    }

    function _leverageLogo() internal pure returns (string memory) {
        return string.concat(
            svg.path(
                "M20.2 31.2C19.1 32.4 17.6 33 16 33L16 21C17.6 21 19.1 21.6 20.2 22.7C21.4 23.9 22 25.4 22 27C22 28.6 21.4 30.1 20.2 31.2Z",
                svg.prop("fill", STOIC_WHITE)
            ),
            svg.path(
                "M22 27C22 25.4 22.6 23.9 23.8 22.7C25 21.6 26.4 21 28 21V33C26.4 33 25 32.4 24 31.2C22.6 30.1 22 28.6 22 27Z",
                svg.prop("fill", STOIC_WHITE)
            )
        );
    }

    function _boldLogo(FixedAssetReader _assetReader) internal view returns (string memory) {
        return svg.el(
            "image",
            string.concat(
                svg.prop("x", "264"),
                svg.prop("y", "373.5"),
                svg.prop("width", "20"),
                svg.prop("height", "20"),
                svg.prop(
                    "href",
                    string.concat("data:image/svg+xml;base64,", _assetReader.readAsset(bytes4(keccak256("BOLD"))))
                )
            )
        );
    }

    function _staticTextEls() internal pure returns (string memory) {
        return string.concat(
            svg.text(
                string.concat(
                    GEIST,
                    svg.prop("x", "16"),
                    svg.prop("y", "358"),
                    svg.prop("font-size", "14"),
                    svg.prop("fill", "white")
                ),
                "Collateral"
            ),
            svg.text(
                string.concat(
                    GEIST,
                    svg.prop("x", "16"),
                    svg.prop("y", "389"),
                    svg.prop("font-size", "14"),
                    svg.prop("fill", "white")
                ),
                "Debt"
            ),
            svg.text(
                string.concat(
                    GEIST,
                    svg.prop("x", "16"),
                    svg.prop("y", "420"),
                    svg.prop("font-size", "14"),
                    svg.prop("fill", "white")
                ),
                "Interest Rate"
            ),
            svg.text(
                string.concat(
                    GEIST,
                    svg.prop("x", "265"),
                    svg.prop("y", "422"),
                    svg.prop("font-size", "20"),
                    svg.prop("fill", "white")
                ),
                "%"
            ),
            svg.text(
                string.concat(
                    GEIST,
                    svg.prop("x", "16"),
                    svg.prop("y", "462"),
                    svg.prop("font-size", "14"),
                    svg.prop("fill", "white")
                ),
                "Owner"
            )
        );
    }

    function _formattedDynamicEl(string memory _value, uint256 _x, uint256 _y) internal pure returns (string memory) {
        return svg.text(
            string.concat(
                GEIST,
                svg.prop("text-anchor", "end"),
                svg.prop("x", LibString.toString(_x)),
                svg.prop("y", LibString.toString(_y)),
                svg.prop("font-size", "20"),
                svg.prop("fill", "white")
            ),
            _value
        );
    }

    function _formattedIdEl(string memory _id) internal pure returns (string memory) {
        return svg.text(
            string.concat(
                GEIST,
                svg.prop("text-anchor", "end"),
                svg.prop("x", "284"),
                svg.prop("y", "33"),
                svg.prop("font-size", "14"),
                svg.prop("fill", "white")
            ),
            _id
        );
    }

    function _formattedAddressEl(address _address) internal pure returns (string memory) {
        return svg.text(
            string.concat(
                GEIST,
                svg.prop("text-anchor", "end"),
                svg.prop("x", "284"),
                svg.prop("y", "462"),
                svg.prop("font-size", "14"),
                svg.prop("fill", "white")
            ),
            string.concat(
                LibString.slice(LibString.toHexStringChecksummed(_address), 0, 6),
                "...",
                LibString.slice(LibString.toHexStringChecksummed(_address), 38, 42)
            )
        );
    }

    function _collLogo(string memory _collName, FixedAssetReader _assetReader) internal view returns (string memory) {
        return svg.el(
            "image",
            string.concat(
                svg.prop("x", "264"),
                svg.prop("y", "342.5"),
                svg.prop("width", "20"),
                svg.prop("height", "20"),
                svg.prop(
                    "href",
                    string.concat(
                        "data:image/svg+xml;base64,", _assetReader.readAsset(bytes4(keccak256(bytes(_collName))))
                    )
                )
            )
        );
    }

    function _statusEl(string memory _status) internal pure returns (string memory) {
        return svg.text(
            string.concat(
                GEIST, svg.prop("x", "40"), svg.prop("y", "33"), svg.prop("font-size", "14"), svg.prop("fill", "white")
            ),
            _status
        );
    }

    function _dynamicTextEls(uint256 _debt, uint256 _coll, uint256 _annualInterestRate)
        internal
        pure
        returns (string memory)
    {
        return string.concat(
            _formattedDynamicEl(numUtils.toLocaleString(_coll, 18, 4), 256, 360),
            _formattedDynamicEl(numUtils.toLocaleString(_debt, 18, 2), 256, 391),
            _formattedDynamicEl(numUtils.toLocaleString(_annualInterestRate, 16, 2), 256, 422)
        );
    }
}

File 36 of 44 : bauhaus.sol
//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "./SVG.sol";

library bauhaus {
    string constant GOLDEN = "#F5D93A";
    string constant CORAL = "#FB7C59";
    string constant GREEN = "#63D77D";
    string constant CYAN = "#95CBF3";
    string constant BLUE = "#405AE5";
    string constant DARK_BLUE = "#121B44";
    string constant BROWN = "#D99664";

    enum colorCode {
        GOLDEN,
        CORAL,
        GREEN,
        CYAN,
        BLUE,
        DARK_BLUE,
        BROWN
    }

    function _bauhaus(string memory _collName, uint256 _troveId) internal pure returns (string memory) {
        bytes32 collSig = keccak256(bytes(_collName));
        uint256 variant = _troveId % 4;

        if (collSig == keccak256("WETH")) {
            return _img1(variant);
        } else if (collSig == keccak256("wstETH")) {
            return _img2(variant);
        } else {
            // assume rETH
            return _img3(variant);
        }
    }

    function _colorCode2Hex(colorCode _color) private pure returns (string memory) {
        if (_color == colorCode.GOLDEN) {
            return GOLDEN;
        } else if (_color == colorCode.CORAL) {
            return CORAL;
        } else if (_color == colorCode.GREEN) {
            return GREEN;
        } else if (_color == colorCode.CYAN) {
            return CYAN;
        } else if (_color == colorCode.BLUE) {
            return BLUE;
        } else if (_color == colorCode.DARK_BLUE) {
            return DARK_BLUE;
        } else {
            return BROWN;
        }
    }

    struct COLORS {
        colorCode rect1;
        colorCode rect2;
        colorCode rect3;
        colorCode rect4;
        colorCode rect5;
        colorCode poly;
        colorCode circle1;
        colorCode circle2;
        colorCode circle3;
    }

    function _colors1(uint256 _variant) internal pure returns (COLORS memory) {
        if (_variant == 0) {
            return COLORS(
                colorCode.BLUE, // rect1
                colorCode.GOLDEN, // rect2
                colorCode.GOLDEN, // rect3
                colorCode.BROWN, // rect4
                colorCode.CORAL, // rect5
                colorCode.CYAN, // poly
                colorCode.GREEN, // circle1
                colorCode.DARK_BLUE, // circle2
                colorCode.GOLDEN // circle3
            );
        } else if (_variant == 1) {
            return COLORS(
                colorCode.GREEN, // rect1
                colorCode.BLUE, // rect2
                colorCode.GOLDEN, // rect3
                colorCode.BROWN, // rect4
                colorCode.GOLDEN, // rect5
                colorCode.CORAL, // poly
                colorCode.BLUE, // circle1
                colorCode.DARK_BLUE, // circle2
                colorCode.BLUE // circle3
            );
        } else if (_variant == 2) {
            return COLORS(
                colorCode.BLUE, // rect1
                colorCode.GOLDEN, // rect2
                colorCode.CYAN, // rect3
                colorCode.GOLDEN, // rect4
                colorCode.BROWN, // rect5
                colorCode.GREEN, // poly
                colorCode.CORAL, // circle1
                colorCode.DARK_BLUE, // circle2
                colorCode.BROWN // circle3
            );
        } else {
            return COLORS(
                colorCode.CYAN, // rect1
                colorCode.BLUE, // rect2
                colorCode.BLUE, // rect3
                colorCode.BROWN, // rect4
                colorCode.BLUE, // rect5
                colorCode.GREEN, // poly
                colorCode.GOLDEN, // circle1
                colorCode.DARK_BLUE, // circle2
                colorCode.BLUE // circle3
            );
        }
    }

    function _img1(uint256 _variant) internal pure returns (string memory) {
        COLORS memory colors = _colors1(_variant);
        return string.concat(_rects1(colors), _polygons1(colors), _circles1(colors));
    }

    function _rects1(COLORS memory _colors) internal pure returns (string memory) {
        return string.concat(
            //background
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "55"),
                    svg.prop("width", "268"),
                    svg.prop("height", "268"),
                    svg.prop("fill", DARK_BLUE)
                )
            ),
            // large right rect | rect1
            svg.rect(
                string.concat(
                    svg.prop("x", "128"),
                    svg.prop("y", "55"),
                    svg.prop("width", "156"),
                    svg.prop("height", "268"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect1))
                )
            ),
            // small upper right rect | rect2
            svg.rect(
                string.concat(
                    svg.prop("x", "228"),
                    svg.prop("y", "55"),
                    svg.prop("width", "56"),
                    svg.prop("height", "56"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect2))
                )
            ),
            // large central left rect | rect3
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "111"),
                    svg.prop("width", "134"),
                    svg.prop("height", "156"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect3))
                )
            ),
            // small lower left rect | rect4
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "267"),
                    svg.prop("width", "112"),
                    svg.prop("height", "56"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect4))
                )
            ),
            // small lower right rect | rect5
            svg.rect(
                string.concat(
                    svg.prop("x", "228"),
                    svg.prop("y", "267"),
                    svg.prop("width", "56"),
                    svg.prop("height", "56"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect5))
                )
            )
        );
    }

    function _polygons1(COLORS memory _colors) internal pure returns (string memory) {
        return string.concat(
            // left triangle | poly1
            svg.polygon(
                string.concat(svg.prop("points", "16,55 72,55 16,111"), svg.prop("fill", _colorCode2Hex(_colors.poly)))
            ),
            // right triangle | poly2
            svg.polygon(
                string.concat(svg.prop("points", "72,55 128,55 72,111"), svg.prop("fill", _colorCode2Hex(_colors.poly)))
            )
        );
    }

    function _circles1(COLORS memory _colors) internal pure returns (string memory) {
        return string.concat(
            //large central circle | circle1
            svg.circle(
                string.concat(
                    svg.prop("cx", "150"),
                    svg.prop("cy", "189"),
                    svg.prop("r", "78"),
                    svg.prop("fill", _colorCode2Hex(_colors.circle1))
                )
            ),
            //small right circle | circle2
            svg.circle(
                string.concat(
                    svg.prop("cx", "228"),
                    svg.prop("cy", "295"),
                    svg.prop("r", "28"),
                    svg.prop("fill", _colorCode2Hex(_colors.circle2))
                )
            ),
            //small right half circle | circle3
            svg.path(
                "M228 267C220.574 267 213.452 269.95 208.201 275.201C202.95 280.452 200 287.574 200 295C200 302.426 202.95 309.548 208.201 314.799C213.452 320.05 220.574 323 228 323L228 267Z",
                svg.prop("fill", _colorCode2Hex(_colors.circle3))
            )
        );
    }

    function _colors2(uint256 _variant) internal pure returns (COLORS memory) {
        if (_variant == 0) {
            return COLORS(
                colorCode.BROWN, // rect1
                colorCode.GOLDEN, // rect2
                colorCode.BLUE, // rect3
                colorCode.GREEN, // rect4
                colorCode.CORAL, // rect5
                colorCode.GOLDEN, // unused
                colorCode.GOLDEN, // circle1
                colorCode.CYAN, // circle2
                colorCode.GREEN // circle3
            );
        } else if (_variant == 1) {
            return COLORS(
                colorCode.GREEN, // rect1
                colorCode.BROWN, // rect2
                colorCode.GOLDEN, // rect3
                colorCode.BLUE, // rect4
                colorCode.CYAN, // rect5
                colorCode.GOLDEN, // unused
                colorCode.GREEN, // circle1
                colorCode.CORAL, // circle2
                colorCode.BLUE // circle3
            );
        } else if (_variant == 2) {
            return COLORS(
                colorCode.BLUE, // rect1
                colorCode.GOLDEN, // rect2
                colorCode.GREEN, // rect3
                colorCode.BLUE, // rect4
                colorCode.CORAL, // rect5
                colorCode.GOLDEN, // unused
                colorCode.CYAN, // circle1
                colorCode.BROWN, // circle2
                colorCode.BROWN // circle3
            );
        } else {
            return COLORS(
                colorCode.GOLDEN, // rect1
                colorCode.GREEN, // rect2
                colorCode.BLUE, // rect3
                colorCode.GOLDEN, // rect4
                colorCode.BROWN, // rect5
                colorCode.GOLDEN, // unused
                colorCode.BROWN, // circle1
                colorCode.CYAN, // circle2
                colorCode.CORAL // circle3
            );
        }
    }

    function _img2(uint256 _variant) internal pure returns (string memory) {
        COLORS memory colors = _colors2(_variant);
        return string.concat(_rects2(colors), _circles2(colors));
    }

    function _rects2(COLORS memory _colors) internal pure returns (string memory) {
        return string.concat(
            //background
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "55"),
                    svg.prop("width", "268"),
                    svg.prop("height", "268"),
                    svg.prop("fill", DARK_BLUE)
                )
            ),
            // large upper right rect | rect1
            svg.rect(
                string.concat(
                    svg.prop("x", "128"),
                    svg.prop("y", "55"),
                    svg.prop("width", "156"),
                    svg.prop("height", "156"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect1))
                )
            ),
            // large central left rect | rect2
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "111"),
                    svg.prop("width", "134"),
                    svg.prop("height", "100"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect2))
                )
            ),
            // large lower left rect | rect3
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "211"),
                    svg.prop("width", "212"),
                    svg.prop("height", "56"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect3))
                )
            ),
            // small lower central rect | rect4
            svg.rect(
                string.concat(
                    svg.prop("x", "72"),
                    svg.prop("y", "267"),
                    svg.prop("width", "78"),
                    svg.prop("height", "56"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect4))
                )
            ),
            // small lower right rect | rect5
            svg.rect(
                string.concat(
                    svg.prop("x", "150"),
                    svg.prop("y", "267"),
                    svg.prop("width", "134"),
                    svg.prop("height", "56"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect5))
                )
            )
        );
    }

    function _circles2(COLORS memory _colors) internal pure returns (string memory) {
        return string.concat(
            //lower left circle | circle1
            svg.circle(
                string.concat(
                    svg.prop("cx", "44"),
                    svg.prop("cy", "295"),
                    svg.prop("r", "28"),
                    svg.prop("fill", _colorCode2Hex(_colors.circle1))
                )
            ),
            //upper left half circle | circle2
            svg.path(
                "M16 55C16 62.4 17.4 69.6 20.3 76.4C23.1 83.2 27.2 89.4 32.4 94.6C37.6 99.8 43.8 103.9 50.6 106.7C57.4 109.6 64.6 111 72 111C79.4 111 86.6 109.6 93.4 106.7C100.2 103.9 106.4 99.8 111.6 94.6C116.8 89.4 120.9 83.2 123.7 76.4C126.6 69.6 128 62.4 128 55L16 55Z",
                svg.prop("fill", _colorCode2Hex(_colors.circle2))
            ),
            //central right half circle | circle3
            svg.path(
                "M284 211C284 190.3 275.8 170.5 261.2 155.8C246.5 141.2 226.7 133 206 133C185.3 133 165.5 141.2 150.9 155.86C136.2 170.5 128 190.3 128 211L284 211Z",
                svg.prop("fill", _colorCode2Hex(_colors.circle3))
            )
        );
    }

    function _colors3(uint256 _variant) internal pure returns (COLORS memory) {
        if (_variant == 0) {
            return COLORS(
                colorCode.BLUE, // rect1
                colorCode.CORAL, // rect2
                colorCode.BLUE, // rect3
                colorCode.GREEN, // rect4
                colorCode.GOLDEN, // unused
                colorCode.GOLDEN, // unused
                colorCode.GOLDEN, // circle1
                colorCode.CYAN, // circle2
                colorCode.GOLDEN // circle3
            );
        } else if (_variant == 1) {
            return COLORS(
                colorCode.CORAL, // rect1
                colorCode.GREEN, // rect2
                colorCode.BROWN, // rect3
                colorCode.GOLDEN, // rect4
                colorCode.GOLDEN, // unused
                colorCode.GOLDEN, // unused
                colorCode.BLUE, // circle1
                colorCode.BLUE, // circle2
                colorCode.CYAN // circle3
            );
        } else if (_variant == 2) {
            return COLORS(
                colorCode.CORAL, // rect1
                colorCode.CYAN, // rect2
                colorCode.CORAL, // rect3
                colorCode.GOLDEN, // rect4
                colorCode.GOLDEN, // unused
                colorCode.GOLDEN, // unused
                colorCode.GREEN, // circle1
                colorCode.BLUE, // circle2
                colorCode.GREEN // circle3
            );
        } else {
            return COLORS(
                colorCode.GOLDEN, // rect1
                colorCode.CORAL, // rect2
                colorCode.GREEN, // rect3
                colorCode.BLUE, // rect4
                colorCode.GOLDEN, // unused
                colorCode.GOLDEN, // unused
                colorCode.BROWN, // circle1
                colorCode.BLUE, // circle2
                colorCode.GREEN // circle3
            );
        }
    }

    function _img3(uint256 _variant) internal pure returns (string memory) {
        COLORS memory colors = _colors3(_variant);
        return string.concat(_rects3(colors), _circles3(colors));
    }

    function _rects3(COLORS memory _colors) internal pure returns (string memory) {
        return string.concat(
            //background
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "55"),
                    svg.prop("width", "268"),
                    svg.prop("height", "268"),
                    svg.prop("fill", DARK_BLUE)
                )
            ),
            // lower left rect | rect1
            svg.rect(
                string.concat(
                    svg.prop("x", "16"),
                    svg.prop("y", "205"),
                    svg.prop("width", "75"),
                    svg.prop("height", "118"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect1))
                )
            ),
            // central rect | rect2
            svg.rect(
                string.concat(
                    svg.prop("x", "91"),
                    svg.prop("y", "205"),
                    svg.prop("width", "136"),
                    svg.prop("height", "59"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect2))
                )
            ),
            // central right rect | rect3
            svg.rect(
                string.concat(
                    svg.prop("x", "166"),
                    svg.prop("y", "180"),
                    svg.prop("width", "118"),
                    svg.prop("height", "25"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect3))
                )
            ),
            // upper right rect | rect4
            svg.rect(
                string.concat(
                    svg.prop("x", "166"),
                    svg.prop("y", "55"),
                    svg.prop("width", "118"),
                    svg.prop("height", "126"),
                    svg.prop("fill", _colorCode2Hex(_colors.rect4))
                )
            )
        );
    }

    function _circles3(COLORS memory _colors) internal pure returns (string memory) {
        return string.concat(
            //upper left circle | circle1
            svg.circle(
                string.concat(
                    svg.prop("cx", "91"),
                    svg.prop("cy", "130"),
                    svg.prop("r", "75"),
                    svg.prop("fill", _colorCode2Hex(_colors.circle1))
                )
            ),
            //upper right half circle | circle2
            svg.path(
                "M284 264 166 264 166 263C166 232 193 206 225 205C258 206 284 232 284 264C284 264 284 264 284 264Z",
                svg.prop("fill", _colorCode2Hex(_colors.circle2))
            ),
            //lower right half circle | circle3
            svg.path(
                "M284 323 166 323 166 323C166 290 193 265 225 264C258 265 284 290 284 323C284 323 284 323 284 323Z",
                svg.prop("fill", _colorCode2Hex(_colors.circle3))
            )
        );
    }
}

File 37 of 44 : IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC-20 standard as defined in the ERC.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

File 38 of 44 : IERC721.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.20;

import {IERC165} from "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC-721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
     *   a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC-721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or
     *   {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
     *   a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC-721
     * or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
     * understand this adds an external call which potentially creates a reentrancy vulnerability.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the address zero.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}

File 39 of 44 : SVG.sol
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {utils, LibString} from "./Utils.sol";

/// @notice Core SVG utility library which helps us construct onchain SVG's with a simple, web-like API.
/// @author Modified from (https://github.com/w1nt3r-eth/hot-chain-svg/blob/main/contracts/SVG.sol) by w1nt3r-eth.

library svg {
    /* GLOBAL CONSTANTS */
    string internal constant _SVG = 'xmlns="http://www.w3.org/2000/svg"';
    string internal constant _HTML = 'xmlns="http://www.w3.org/1999/xhtml"';
    string internal constant _XMLNS = "http://www.w3.org/2000/xmlns/ ";
    string internal constant _XLINK = "http://www.w3.org/1999/xlink ";

    /* MAIN ELEMENTS */
    function g(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("g", _props, _children);
    }

    function _svg(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("svg", string.concat(_SVG, " ", _props), _children);
    }

    function style(string memory _title, string memory _props) internal pure returns (string memory) {
        return el("style", string.concat(".", _title, " ", _props));
    }

    function path(string memory _d) internal pure returns (string memory) {
        return el("path", prop("d", _d, true));
    }

    function path(string memory _d, string memory _props) internal pure returns (string memory) {
        return el("path", string.concat(prop("d", _d), _props));
    }

    function path(string memory _d, string memory _props, string memory _children)
        internal
        pure
        returns (string memory)
    {
        return el("path", string.concat(prop("d", _d), _props), _children);
    }

    function text(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("text", _props, _children);
    }

    function line(string memory _props) internal pure returns (string memory) {
        return el("line", _props);
    }

    function line(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("line", _props, _children);
    }

    function circle(string memory _props) internal pure returns (string memory) {
        return el("circle", _props);
    }

    function circle(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("circle", _props, _children);
    }

    function circle(string memory cx, string memory cy, string memory r) internal pure returns (string memory) {
        return el("circle", string.concat(prop("cx", cx), prop("cy", cy), prop("r", r, true)));
    }

    function circle(string memory cx, string memory cy, string memory r, string memory _children)
        internal
        pure
        returns (string memory)
    {
        return el("circle", string.concat(prop("cx", cx), prop("cy", cy), prop("r", r, true)), _children);
    }

    function circle(string memory cx, string memory cy, string memory r, string memory _props, string memory _children)
        internal
        pure
        returns (string memory)
    {
        return el("circle", string.concat(prop("cx", cx), prop("cy", cy), prop("r", r), _props), _children);
    }

    function ellipse(string memory _props) internal pure returns (string memory) {
        return el("ellipse", _props);
    }

    function ellipse(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("ellipse", _props, _children);
    }

    function polygon(string memory _props) internal pure returns (string memory) {
        return el("polygon", _props);
    }

    function polygon(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("polygon", _props, _children);
    }

    function polyline(string memory _props) internal pure returns (string memory) {
        return el("polyline", _props);
    }

    function polyline(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("polyline", _props, _children);
    }

    function rect(string memory _props) internal pure returns (string memory) {
        return el("rect", _props);
    }

    function rect(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("rect", _props, _children);
    }

    function filter(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("filter", _props, _children);
    }

    function cdata(string memory _content) internal pure returns (string memory) {
        return string.concat("<![CDATA[", _content, "]]>");
    }

    /* GRADIENTS */
    function radialGradient(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("radialGradient", _props, _children);
    }

    function linearGradient(string memory _props, string memory _children) internal pure returns (string memory) {
        return el("linearGradient", _props, _children);
    }

    function gradientStop(uint256 offset, string memory stopColor, string memory _props)
        internal
        pure
        returns (string memory)
    {
        return el(
            "stop",
            string.concat(
                prop("stop-color", stopColor),
                " ",
                prop("offset", string.concat(LibString.toString(offset), "%")),
                " ",
                _props
            ),
            utils.NULL
        );
    }

    /* ANIMATION */
    function animateTransform(string memory _props) internal pure returns (string memory) {
        return el("animateTransform", _props);
    }

    function animate(string memory _props) internal pure returns (string memory) {
        return el("animate", _props);
    }

    /* COMMON */
    // A generic element, can be used to construct any SVG (or HTML) element
    function el(string memory _tag, string memory _props, string memory _children)
        internal
        pure
        returns (string memory)
    {
        return string.concat("<", _tag, " ", _props, ">", _children, "</", _tag, ">");
    }

    // A generic element, can be used to construct SVG (or HTML) elements without children
    function el(string memory _tag, string memory _props) internal pure returns (string memory) {
        return string.concat("<", _tag, " ", _props, "/>");
    }

    // an SVG attribute
    function prop(string memory _key, string memory _val) internal pure returns (string memory) {
        return string.concat(_key, "=", '"', _val, '" ');
    }

    function prop(string memory _key, string memory _val, bool last) internal pure returns (string memory) {
        if (last) {
            return string.concat(_key, "=", '"', _val, '"');
        } else {
            return string.concat(_key, "=", '"', _val, '" ');
        }
    }
}

File 40 of 44 : Utils.sol
//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "lib/Solady/src/utils/LibString.sol";

library numUtils {
    function toLocale(string memory _wholeNumber) internal pure returns (string memory) {
        bytes memory b = bytes(_wholeNumber);
        uint256 len = b.length;
        if (len < 4) return _wholeNumber;

        uint256 numCommas = (len - 1) / 3;

        bytes memory result = new bytes(len + numCommas);

        uint256 j = result.length - 1;
        uint256 k = len;
        for (uint256 i = 0; i < len; i++) {
            result[j] = b[k - 1];
            j = j > 1 ? j - 1 : 0;
            k--;
            if (k > 0 && (len - k) % 3 == 0) {
                result[j] = ",";
                j = j > 1 ? j - 1 : 0;
            }
        }

        return string(result);
    }

    // returns a string representation of a number with commas, where result = _value / 10 ** _divisor
    function toLocaleString(uint256 _value, uint8 _divisor, uint8 _precision) internal pure returns (string memory) {
        uint256 whole;
        uint256 fraction;

        if (_divisor > 0) {
            whole = _value / 10 ** _divisor;
            // check if the divisor is less than the precision
            if (_divisor <= _precision) {
                fraction = (_value % 10 ** _divisor);
                // adjust fraction to be the same as the precision
                fraction = fraction * 10 ** (_precision - _divisor);

                // if whole is zero, then add another zero to the fraction, special case if the value is 1
                fraction = (whole == 0 && _value != 1) ? fraction * 10 : fraction;
            } else {
                fraction = (_value % 10 ** _divisor) / 10 ** (_divisor - _precision - 1);
            }
        } else {
            whole = _value;
        }

        string memory wholeStr = toLocale(LibString.toString(whole));

        if (fraction == 0) {
            if (whole > 0 && _precision > 0) wholeStr = string.concat(wholeStr, ".");
            for (uint8 i = 0; i < _precision; i++) {
                wholeStr = string.concat(wholeStr, "0");
            }

            return wholeStr;
        }

        string memory fractionStr = LibString.slice(LibString.toString(fraction), 0, _precision);

        // pad with leading zeros
        if (_precision > bytes(fractionStr).length) {
            uint256 len = _precision - bytes(fractionStr).length;
            string memory zeroStr = "";

            for (uint8 i = 0; i < len; i++) {
                zeroStr = string.concat(zeroStr, "0");
            }

            fractionStr = string.concat(zeroStr, fractionStr);
        }

        return string.concat(wholeStr, _precision > 0 ? "." : "", fractionStr);
    }
}

/// @notice Core utils used extensively to format CSS and numbers.
/// @author Modified from (https://github.com/w1nt3r-eth/hot-chain-svg/blob/main/contracts/Utils.sol) by w1nt3r-eth.

library utils {
    // used to simulate empty strings
    string internal constant NULL = "";

    // formats a CSS variable line. includes a semicolon for formatting.
    function setCssVar(string memory _key, string memory _val) internal pure returns (string memory) {
        return string.concat("--", _key, ":", _val, ";");
    }

    // formats getting a css variable
    function getCssVar(string memory _key) internal pure returns (string memory) {
        return string.concat("var(--", _key, ")");
    }

    // formats getting a def URL
    function getDefURL(string memory _id) internal pure returns (string memory) {
        return string.concat("url(#", _id, ")");
    }

    // formats rgba white with a specified opacity / alpha
    function white_a(uint256 _a) internal pure returns (string memory) {
        return rgba(255, 255, 255, _a);
    }

    // formats rgba black with a specified opacity / alpha
    function black_a(uint256 _a) internal pure returns (string memory) {
        return rgba(0, 0, 0, _a);
    }

    // formats generic rgba color in css
    function rgba(uint256 _r, uint256 _g, uint256 _b, uint256 _a) internal pure returns (string memory) {
        string memory formattedA = _a < 100 ? string.concat("0.", LibString.toString(_a)) : "1";
        return string.concat(
            "rgba(",
            LibString.toString(_r),
            ",",
            LibString.toString(_g),
            ",",
            LibString.toString(_b),
            ",",
            formattedA,
            ")"
        );
    }

    function cssBraces(string memory _attribute, string memory _value) internal pure returns (string memory) {
        return string.concat(" {", _attribute, ": ", _value, "}");
    }

    function cssBraces(string[] memory _attributes, string[] memory _values) internal pure returns (string memory) {
        require(_attributes.length == _values.length, "Utils: Unbalanced Arrays");

        uint256 len = _attributes.length;

        string memory results = " {";

        for (uint256 i = 0; i < len; i++) {
            results = string.concat(results, _attributes[i], ": ", _values[i], "; ");
        }

        return string.concat(results, "}");
    }

    //deals with integers (i.e. no decimals)
    function points(uint256[2][] memory pointsArray) internal pure returns (string memory) {
        require(pointsArray.length >= 3, "Utils: Array too short");

        uint256 len = pointsArray.length - 1;

        string memory results = 'points="';

        for (uint256 i = 0; i < len; i++) {
            results = string.concat(
                results, LibString.toString(pointsArray[i][0]), ",", LibString.toString(pointsArray[i][1]), " "
            );
        }

        return string.concat(
            results, LibString.toString(pointsArray[len][0]), ",", LibString.toString(pointsArray[len][1]), '"'
        );
    }

    // allows for a uniform precision to be applied to all points
    function points(uint256[2][] memory pointsArray, uint256 decimalPrecision) internal pure returns (string memory) {
        require(pointsArray.length >= 3, "Utils: Array too short");

        uint256 len = pointsArray.length - 1;

        string memory results = 'points="';

        for (uint256 i = 0; i < len; i++) {
            results = string.concat(
                results,
                toString(pointsArray[i][0], decimalPrecision),
                ",",
                toString(pointsArray[i][1], decimalPrecision),
                " "
            );
        }

        return string.concat(
            results,
            toString(pointsArray[len][0], decimalPrecision),
            ",",
            toString(pointsArray[len][1], decimalPrecision),
            '"'
        );
    }

    // checks if two strings are equal
    function stringsEqual(string memory _a, string memory _b) internal pure returns (bool) {
        return keccak256(abi.encodePacked(_a)) == keccak256(abi.encodePacked(_b));
    }

    // returns the length of a string in characters
    function utfStringLength(string memory _str) internal pure returns (uint256 length) {
        uint256 i = 0;
        bytes memory string_rep = bytes(_str);

        while (i < string_rep.length) {
            if (string_rep[i] >> 7 == 0) {
                i += 1;
            } else if (string_rep[i] >> 5 == bytes1(uint8(0x6))) {
                i += 2;
            } else if (string_rep[i] >> 4 == bytes1(uint8(0xE))) {
                i += 3;
            } else if (string_rep[i] >> 3 == bytes1(uint8(0x1E))) {
                i += 4;
            }
            //For safety
            else {
                i += 1;
            }

            length++;
        }
    }

    // allows the insertion of a decimal point in the returned string at precision
    function toString(uint256 value, uint256 precision) internal pure returns (string memory) {
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        require(precision <= digits && precision > 0, "Utils: precision invalid");
        precision == digits ? digits += 2 : digits++; //adds a space for the decimal point, 2 if it is the whole uint

        uint256 decimalPlacement = digits - precision - 1;
        bytes memory buffer = new bytes(digits);

        buffer[decimalPlacement] = 0x2E; // add the decimal point, ASCII 46/hex 2E
        if (decimalPlacement == 1) {
            buffer[0] = 0x30;
        }

        while (value != 0) {
            digits -= 1;
            if (digits != decimalPlacement) {
                buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
                value /= 10;
            }
        }

        return string(buffer);
    }
}

File 41 of 44 : FixedAssets.sol
//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "lib/Solady/src/utils/SSTORE2.sol";

contract FixedAssetReader {
    struct Asset {
        uint128 start;
        uint128 end;
    }

    address public immutable pointer;

    mapping(bytes4 => Asset) public assets;

    function readAsset(bytes4 _sig) public view returns (string memory) {
        return string(SSTORE2.read(pointer, uint256(assets[_sig].start), uint256(assets[_sig].end)));
    }

    constructor(address _pointer, bytes4[] memory _sigs, Asset[] memory _assets) {
        pointer = _pointer;
        require(_sigs.length == _assets.length, "FixedAssetReader: Invalid input");
        for (uint256 i = 0; i < _sigs.length; i++) {
            assets[_sigs[i]] = _assets[i];
        }
    }
}

File 42 of 44 : IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC-165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[ERC].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

File 43 of 44 : LibString.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import {LibBytes} from "./LibBytes.sol";

/// @notice Library for converting numbers into strings and other string operations.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibString.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/LibString.sol)
///
/// @dev Note:
/// For performance and bytecode compactness, most of the string operations are restricted to
/// byte strings (7-bit ASCII), except where otherwise specified.
/// Usage of byte string operations on charsets with runes spanning two or more bytes
/// can lead to undefined behavior.
library LibString {
    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                          STRUCTS                           */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Goated string storage struct that totally MOGs, no cap, fr.
    /// Uses less gas and bytecode than Solidity's native string storage. It's meta af.
    /// Packs length with the first 31 bytes if <255 bytes, so it’s mad tight.
    struct StringStorage {
        bytes32 _spacer;
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                        CUSTOM ERRORS                       */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev The length of the output is too small to contain all the hex digits.
    error HexLengthInsufficient();

    /// @dev The length of the string is more than 32 bytes.
    error TooBigForSmallString();

    /// @dev The input string must be a 7-bit ASCII.
    error StringNot7BitASCII();

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                         CONSTANTS                          */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev The constant returned when the `search` is not found in the string.
    uint256 internal constant NOT_FOUND = type(uint256).max;

    /// @dev Lookup for '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.
    uint128 internal constant ALPHANUMERIC_7_BIT_ASCII = 0x7fffffe07fffffe03ff000000000000;

    /// @dev Lookup for 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.
    uint128 internal constant LETTERS_7_BIT_ASCII = 0x7fffffe07fffffe0000000000000000;

    /// @dev Lookup for 'abcdefghijklmnopqrstuvwxyz'.
    uint128 internal constant LOWERCASE_7_BIT_ASCII = 0x7fffffe000000000000000000000000;

    /// @dev Lookup for 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
    uint128 internal constant UPPERCASE_7_BIT_ASCII = 0x7fffffe0000000000000000;

    /// @dev Lookup for '0123456789'.
    uint128 internal constant DIGITS_7_BIT_ASCII = 0x3ff000000000000;

    /// @dev Lookup for '0123456789abcdefABCDEF'.
    uint128 internal constant HEXDIGITS_7_BIT_ASCII = 0x7e0000007e03ff000000000000;

    /// @dev Lookup for '01234567'.
    uint128 internal constant OCTDIGITS_7_BIT_ASCII = 0xff000000000000;

    /// @dev Lookup for '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'.
    uint128 internal constant PRINTABLE_7_BIT_ASCII = 0x7fffffffffffffffffffffff00003e00;

    /// @dev Lookup for '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'.
    uint128 internal constant PUNCTUATION_7_BIT_ASCII = 0x78000001f8000001fc00fffe00000000;

    /// @dev Lookup for ' \t\n\r\x0b\x0c'.
    uint128 internal constant WHITESPACE_7_BIT_ASCII = 0x100003e00;

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                 STRING STORAGE OPERATIONS                  */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Sets the value of the string storage `$` to `s`.
    function set(StringStorage storage $, string memory s) internal {
        LibBytes.set(bytesStorage($), bytes(s));
    }

    /// @dev Sets the value of the string storage `$` to `s`.
    function setCalldata(StringStorage storage $, string calldata s) internal {
        LibBytes.setCalldata(bytesStorage($), bytes(s));
    }

    /// @dev Sets the value of the string storage `$` to the empty string.
    function clear(StringStorage storage $) internal {
        delete $._spacer;
    }

    /// @dev Returns whether the value stored is `$` is the empty string "".
    function isEmpty(StringStorage storage $) internal view returns (bool) {
        return uint256($._spacer) & 0xff == uint256(0);
    }

    /// @dev Returns the length of the value stored in `$`.
    function length(StringStorage storage $) internal view returns (uint256) {
        return LibBytes.length(bytesStorage($));
    }

    /// @dev Returns the value stored in `$`.
    function get(StringStorage storage $) internal view returns (string memory) {
        return string(LibBytes.get(bytesStorage($)));
    }

    /// @dev Helper to cast `$` to a `BytesStorage`.
    function bytesStorage(StringStorage storage $)
        internal
        pure
        returns (LibBytes.BytesStorage storage casted)
    {
        /// @solidity memory-safe-assembly
        assembly {
            casted.slot := $.slot
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                     DECIMAL OPERATIONS                     */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Returns the base 10 decimal representation of `value`.
    function toString(uint256 value) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            // The maximum value of a uint256 contains 78 digits (1 byte per digit), but
            // we allocate 0xa0 bytes to keep the free memory pointer 32-byte word aligned.
            // We will need 1 word for the trailing zeros padding, 1 word for the length,
            // and 3 words for a maximum of 78 digits.
            result := add(mload(0x40), 0x80)
            mstore(0x40, add(result, 0x20)) // Allocate memory.
            mstore(result, 0) // Zeroize the slot after the string.

            let end := result // Cache the end of the memory to calculate the length later.
            let w := not(0) // Tsk.
            // We write the string from rightmost digit to leftmost digit.
            // The following is essentially a do-while loop that also handles the zero case.
            for { let temp := value } 1 {} {
                result := add(result, w) // `sub(result, 1)`.
                // Store the character to the pointer.
                // The ASCII index of the '0' character is 48.
                mstore8(result, add(48, mod(temp, 10)))
                temp := div(temp, 10) // Keep dividing `temp` until zero.
                if iszero(temp) { break }
            }
            let n := sub(end, result)
            result := sub(result, 0x20) // Move the pointer 32 bytes back to make room for the length.
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the base 10 decimal representation of `value`.
    function toString(int256 value) internal pure returns (string memory result) {
        if (value >= 0) return toString(uint256(value));
        unchecked {
            result = toString(~uint256(value) + 1);
        }
        /// @solidity memory-safe-assembly
        assembly {
            // We still have some spare memory space on the left,
            // as we have allocated 3 words (96 bytes) for up to 78 digits.
            let n := mload(result) // Load the string length.
            mstore(result, 0x2d) // Store the '-' character.
            result := sub(result, 1) // Move back the string pointer by a byte.
            mstore(result, add(n, 1)) // Update the string length.
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                   HEXADECIMAL OPERATIONS                   */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Returns the hexadecimal representation of `value`,
    /// left-padded to an input length of `byteCount` bytes.
    /// The output is prefixed with "0x" encoded using 2 hexadecimal digits per byte,
    /// giving a total length of `byteCount * 2 + 2` bytes.
    /// Reverts if `byteCount` is too small for the output to contain all the digits.
    function toHexString(uint256 value, uint256 byteCount)
        internal
        pure
        returns (string memory result)
    {
        result = toHexStringNoPrefix(value, byteCount);
        /// @solidity memory-safe-assembly
        assembly {
            let n := add(mload(result), 2) // Compute the length.
            mstore(result, 0x3078) // Store the "0x" prefix.
            result := sub(result, 2) // Move the pointer.
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the hexadecimal representation of `value`,
    /// left-padded to an input length of `byteCount` bytes.
    /// The output is not prefixed with "0x" and is encoded using 2 hexadecimal digits per byte,
    /// giving a total length of `byteCount * 2` bytes.
    /// Reverts if `byteCount` is too small for the output to contain all the digits.
    function toHexStringNoPrefix(uint256 value, uint256 byteCount)
        internal
        pure
        returns (string memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            // We need 0x20 bytes for the trailing zeros padding, `byteCount * 2` bytes
            // for the digits, 0x02 bytes for the prefix, and 0x20 bytes for the length.
            // We add 0x20 to the total and round down to a multiple of 0x20.
            // (0x20 + 0x20 + 0x02 + 0x20) = 0x62.
            result := add(mload(0x40), and(add(shl(1, byteCount), 0x42), not(0x1f)))
            mstore(0x40, add(result, 0x20)) // Allocate memory.
            mstore(result, 0) // Zeroize the slot after the string.

            let end := result // Cache the end to calculate the length later.
            // Store "0123456789abcdef" in scratch space.
            mstore(0x0f, 0x30313233343536373839616263646566)

            let start := sub(result, add(byteCount, byteCount))
            let w := not(1) // Tsk.
            let temp := value
            // We write the string from rightmost digit to leftmost digit.
            // The following is essentially a do-while loop that also handles the zero case.
            for {} 1 {} {
                result := add(result, w) // `sub(result, 2)`.
                mstore8(add(result, 1), mload(and(temp, 15)))
                mstore8(result, mload(and(shr(4, temp), 15)))
                temp := shr(8, temp)
                if iszero(xor(result, start)) { break }
            }
            if temp {
                mstore(0x00, 0x2194895a) // `HexLengthInsufficient()`.
                revert(0x1c, 0x04)
            }
            let n := sub(end, result)
            result := sub(result, 0x20)
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the hexadecimal representation of `value`.
    /// The output is prefixed with "0x" and encoded using 2 hexadecimal digits per byte.
    /// As address are 20 bytes long, the output will left-padded to have
    /// a length of `20 * 2 + 2` bytes.
    function toHexString(uint256 value) internal pure returns (string memory result) {
        result = toHexStringNoPrefix(value);
        /// @solidity memory-safe-assembly
        assembly {
            let n := add(mload(result), 2) // Compute the length.
            mstore(result, 0x3078) // Store the "0x" prefix.
            result := sub(result, 2) // Move the pointer.
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the hexadecimal representation of `value`.
    /// The output is prefixed with "0x".
    /// The output excludes leading "0" from the `toHexString` output.
    /// `0x00: "0x0", 0x01: "0x1", 0x12: "0x12", 0x123: "0x123"`.
    function toMinimalHexString(uint256 value) internal pure returns (string memory result) {
        result = toHexStringNoPrefix(value);
        /// @solidity memory-safe-assembly
        assembly {
            let o := eq(byte(0, mload(add(result, 0x20))), 0x30) // Whether leading zero is present.
            let n := add(mload(result), 2) // Compute the length.
            mstore(add(result, o), 0x3078) // Store the "0x" prefix, accounting for leading zero.
            result := sub(add(result, o), 2) // Move the pointer, accounting for leading zero.
            mstore(result, sub(n, o)) // Store the length, accounting for leading zero.
        }
    }

    /// @dev Returns the hexadecimal representation of `value`.
    /// The output excludes leading "0" from the `toHexStringNoPrefix` output.
    /// `0x00: "0", 0x01: "1", 0x12: "12", 0x123: "123"`.
    function toMinimalHexStringNoPrefix(uint256 value)
        internal
        pure
        returns (string memory result)
    {
        result = toHexStringNoPrefix(value);
        /// @solidity memory-safe-assembly
        assembly {
            let o := eq(byte(0, mload(add(result, 0x20))), 0x30) // Whether leading zero is present.
            let n := mload(result) // Get the length.
            result := add(result, o) // Move the pointer, accounting for leading zero.
            mstore(result, sub(n, o)) // Store the length, accounting for leading zero.
        }
    }

    /// @dev Returns the hexadecimal representation of `value`.
    /// The output is encoded using 2 hexadecimal digits per byte.
    /// As address are 20 bytes long, the output will left-padded to have
    /// a length of `20 * 2` bytes.
    function toHexStringNoPrefix(uint256 value) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            // We need 0x20 bytes for the trailing zeros padding, 0x20 bytes for the length,
            // 0x02 bytes for the prefix, and 0x40 bytes for the digits.
            // The next multiple of 0x20 above (0x20 + 0x20 + 0x02 + 0x40) is 0xa0.
            result := add(mload(0x40), 0x80)
            mstore(0x40, add(result, 0x20)) // Allocate memory.
            mstore(result, 0) // Zeroize the slot after the string.

            let end := result // Cache the end to calculate the length later.
            mstore(0x0f, 0x30313233343536373839616263646566) // Store the "0123456789abcdef" lookup.

            let w := not(1) // Tsk.
            // We write the string from rightmost digit to leftmost digit.
            // The following is essentially a do-while loop that also handles the zero case.
            for { let temp := value } 1 {} {
                result := add(result, w) // `sub(result, 2)`.
                mstore8(add(result, 1), mload(and(temp, 15)))
                mstore8(result, mload(and(shr(4, temp), 15)))
                temp := shr(8, temp)
                if iszero(temp) { break }
            }
            let n := sub(end, result)
            result := sub(result, 0x20)
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the hexadecimal representation of `value`.
    /// The output is prefixed with "0x", encoded using 2 hexadecimal digits per byte,
    /// and the alphabets are capitalized conditionally according to
    /// https://eips.ethereum.org/EIPS/eip-55
    function toHexStringChecksummed(address value) internal pure returns (string memory result) {
        result = toHexString(value);
        /// @solidity memory-safe-assembly
        assembly {
            let mask := shl(6, div(not(0), 255)) // `0b010000000100000000 ...`
            let o := add(result, 0x22)
            let hashed := and(keccak256(o, 40), mul(34, mask)) // `0b10001000 ... `
            let t := shl(240, 136) // `0b10001000 << 240`
            for { let i := 0 } 1 {} {
                mstore(add(i, i), mul(t, byte(i, hashed)))
                i := add(i, 1)
                if eq(i, 20) { break }
            }
            mstore(o, xor(mload(o), shr(1, and(mload(0x00), and(mload(o), mask)))))
            o := add(o, 0x20)
            mstore(o, xor(mload(o), shr(1, and(mload(0x20), and(mload(o), mask)))))
        }
    }

    /// @dev Returns the hexadecimal representation of `value`.
    /// The output is prefixed with "0x" and encoded using 2 hexadecimal digits per byte.
    function toHexString(address value) internal pure returns (string memory result) {
        result = toHexStringNoPrefix(value);
        /// @solidity memory-safe-assembly
        assembly {
            let n := add(mload(result), 2) // Compute the length.
            mstore(result, 0x3078) // Store the "0x" prefix.
            result := sub(result, 2) // Move the pointer.
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the hexadecimal representation of `value`.
    /// The output is encoded using 2 hexadecimal digits per byte.
    function toHexStringNoPrefix(address value) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            // Allocate memory.
            // We need 0x20 bytes for the trailing zeros padding, 0x20 bytes for the length,
            // 0x02 bytes for the prefix, and 0x28 bytes for the digits.
            // The next multiple of 0x20 above (0x20 + 0x20 + 0x02 + 0x28) is 0x80.
            mstore(0x40, add(result, 0x80))
            mstore(0x0f, 0x30313233343536373839616263646566) // Store the "0123456789abcdef" lookup.

            result := add(result, 2)
            mstore(result, 40) // Store the length.
            let o := add(result, 0x20)
            mstore(add(o, 40), 0) // Zeroize the slot after the string.
            value := shl(96, value)
            // We write the string from rightmost digit to leftmost digit.
            // The following is essentially a do-while loop that also handles the zero case.
            for { let i := 0 } 1 {} {
                let p := add(o, add(i, i))
                let temp := byte(i, value)
                mstore8(add(p, 1), mload(and(temp, 15)))
                mstore8(p, mload(shr(4, temp)))
                i := add(i, 1)
                if eq(i, 20) { break }
            }
        }
    }

    /// @dev Returns the hex encoded string from the raw bytes.
    /// The output is encoded using 2 hexadecimal digits per byte.
    function toHexString(bytes memory raw) internal pure returns (string memory result) {
        result = toHexStringNoPrefix(raw);
        /// @solidity memory-safe-assembly
        assembly {
            let n := add(mload(result), 2) // Compute the length.
            mstore(result, 0x3078) // Store the "0x" prefix.
            result := sub(result, 2) // Move the pointer.
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the hex encoded string from the raw bytes.
    /// The output is encoded using 2 hexadecimal digits per byte.
    function toHexStringNoPrefix(bytes memory raw) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(raw)
            result := add(mload(0x40), 2) // Skip 2 bytes for the optional prefix.
            mstore(result, add(n, n)) // Store the length of the output.

            mstore(0x0f, 0x30313233343536373839616263646566) // Store the "0123456789abcdef" lookup.
            let o := add(result, 0x20)
            let end := add(raw, n)
            for {} iszero(eq(raw, end)) {} {
                raw := add(raw, 1)
                mstore8(add(o, 1), mload(and(mload(raw), 15)))
                mstore8(o, mload(and(shr(4, mload(raw)), 15)))
                o := add(o, 2)
            }
            mstore(o, 0) // Zeroize the slot after the string.
            mstore(0x40, add(o, 0x20)) // Allocate memory.
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                   RUNE STRING OPERATIONS                   */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Returns the number of UTF characters in the string.
    function runeCount(string memory s) internal pure returns (uint256 result) {
        /// @solidity memory-safe-assembly
        assembly {
            if mload(s) {
                mstore(0x00, div(not(0), 255))
                mstore(0x20, 0x0202020202020202020202020202020202020202020202020303030304040506)
                let o := add(s, 0x20)
                let end := add(o, mload(s))
                for { result := 1 } 1 { result := add(result, 1) } {
                    o := add(o, byte(0, mload(shr(250, mload(o)))))
                    if iszero(lt(o, end)) { break }
                }
            }
        }
    }

    /// @dev Returns if this string is a 7-bit ASCII string.
    /// (i.e. all characters codes are in [0..127])
    function is7BitASCII(string memory s) internal pure returns (bool result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := 1
            let mask := shl(7, div(not(0), 255))
            let n := mload(s)
            if n {
                let o := add(s, 0x20)
                let end := add(o, n)
                let last := mload(end)
                mstore(end, 0)
                for {} 1 {} {
                    if and(mask, mload(o)) {
                        result := 0
                        break
                    }
                    o := add(o, 0x20)
                    if iszero(lt(o, end)) { break }
                }
                mstore(end, last)
            }
        }
    }

    /// @dev Returns if this string is a 7-bit ASCII string,
    /// AND all characters are in the `allowed` lookup.
    /// Note: If `s` is empty, returns true regardless of `allowed`.
    function is7BitASCII(string memory s, uint128 allowed) internal pure returns (bool result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := 1
            if mload(s) {
                let allowed_ := shr(128, shl(128, allowed))
                let o := add(s, 0x20)
                for { let end := add(o, mload(s)) } 1 {} {
                    result := and(result, shr(byte(0, mload(o)), allowed_))
                    o := add(o, 1)
                    if iszero(and(result, lt(o, end))) { break }
                }
            }
        }
    }

    /// @dev Converts the bytes in the 7-bit ASCII string `s` to
    /// an allowed lookup for use in `is7BitASCII(s, allowed)`.
    /// To save runtime gas, you can cache the result in an immutable variable.
    function to7BitASCIIAllowedLookup(string memory s) internal pure returns (uint128 result) {
        /// @solidity memory-safe-assembly
        assembly {
            if mload(s) {
                let o := add(s, 0x20)
                for { let end := add(o, mload(s)) } 1 {} {
                    result := or(result, shl(byte(0, mload(o)), 1))
                    o := add(o, 1)
                    if iszero(lt(o, end)) { break }
                }
                if shr(128, result) {
                    mstore(0x00, 0xc9807e0d) // `StringNot7BitASCII()`.
                    revert(0x1c, 0x04)
                }
            }
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                   BYTE STRING OPERATIONS                   */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    // For performance and bytecode compactness, byte string operations are restricted
    // to 7-bit ASCII strings. All offsets are byte offsets, not UTF character offsets.
    // Usage of byte string operations on charsets with runes spanning two or more bytes
    // can lead to undefined behavior.

    /// @dev Returns `subject` all occurrences of `needle` replaced with `replacement`.
    function replace(string memory subject, string memory needle, string memory replacement)
        internal
        pure
        returns (string memory)
    {
        return string(LibBytes.replace(bytes(subject), bytes(needle), bytes(replacement)));
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from left to right, starting from `from`.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function indexOf(string memory subject, string memory needle, uint256 from)
        internal
        pure
        returns (uint256)
    {
        return LibBytes.indexOf(bytes(subject), bytes(needle), from);
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from left to right.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function indexOf(string memory subject, string memory needle) internal pure returns (uint256) {
        return LibBytes.indexOf(bytes(subject), bytes(needle), 0);
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from right to left, starting from `from`.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function lastIndexOf(string memory subject, string memory needle, uint256 from)
        internal
        pure
        returns (uint256)
    {
        return LibBytes.lastIndexOf(bytes(subject), bytes(needle), from);
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from right to left.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function lastIndexOf(string memory subject, string memory needle)
        internal
        pure
        returns (uint256)
    {
        return LibBytes.lastIndexOf(bytes(subject), bytes(needle), type(uint256).max);
    }

    /// @dev Returns true if `needle` is found in `subject`, false otherwise.
    function contains(string memory subject, string memory needle) internal pure returns (bool) {
        return LibBytes.contains(bytes(subject), bytes(needle));
    }

    /// @dev Returns whether `subject` starts with `needle`.
    function startsWith(string memory subject, string memory needle) internal pure returns (bool) {
        return LibBytes.startsWith(bytes(subject), bytes(needle));
    }

    /// @dev Returns whether `subject` ends with `needle`.
    function endsWith(string memory subject, string memory needle) internal pure returns (bool) {
        return LibBytes.endsWith(bytes(subject), bytes(needle));
    }

    /// @dev Returns `subject` repeated `times`.
    function repeat(string memory subject, uint256 times) internal pure returns (string memory) {
        return string(LibBytes.repeat(bytes(subject), times));
    }

    /// @dev Returns a copy of `subject` sliced from `start` to `end` (exclusive).
    /// `start` and `end` are byte offsets.
    function slice(string memory subject, uint256 start, uint256 end)
        internal
        pure
        returns (string memory)
    {
        return string(LibBytes.slice(bytes(subject), start, end));
    }

    /// @dev Returns a copy of `subject` sliced from `start` to the end of the string.
    /// `start` is a byte offset.
    function slice(string memory subject, uint256 start) internal pure returns (string memory) {
        return string(LibBytes.slice(bytes(subject), start, type(uint256).max));
    }

    /// @dev Returns all the indices of `needle` in `subject`.
    /// The indices are byte offsets.
    function indicesOf(string memory subject, string memory needle)
        internal
        pure
        returns (uint256[] memory)
    {
        return LibBytes.indicesOf(bytes(subject), bytes(needle));
    }

    /// @dev Returns a arrays of strings based on the `delimiter` inside of the `subject` string.
    function split(string memory subject, string memory delimiter)
        internal
        pure
        returns (string[] memory result)
    {
        bytes[] memory a = LibBytes.split(bytes(subject), bytes(delimiter));
        /// @solidity memory-safe-assembly
        assembly {
            result := a
        }
    }

    /// @dev Returns a concatenated string of `a` and `b`.
    /// Cheaper than `string.concat()` and does not de-align the free memory pointer.
    function concat(string memory a, string memory b) internal pure returns (string memory) {
        return string(LibBytes.concat(bytes(a), bytes(b)));
    }

    /// @dev Returns a copy of the string in either lowercase or UPPERCASE.
    /// WARNING! This function is only compatible with 7-bit ASCII strings.
    function toCase(string memory subject, bool toUpper)
        internal
        pure
        returns (string memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(subject)
            if n {
                result := mload(0x40)
                let o := add(result, 0x20)
                let d := sub(subject, result)
                let flags := shl(add(70, shl(5, toUpper)), 0x3ffffff)
                for { let end := add(o, n) } 1 {} {
                    let b := byte(0, mload(add(d, o)))
                    mstore8(o, xor(and(shr(b, flags), 0x20), b))
                    o := add(o, 1)
                    if eq(o, end) { break }
                }
                mstore(result, n) // Store the length.
                mstore(o, 0) // Zeroize the slot after the string.
                mstore(0x40, add(o, 0x20)) // Allocate memory.
            }
        }
    }

    /// @dev Returns a string from a small bytes32 string.
    /// `s` must be null-terminated, or behavior will be undefined.
    function fromSmallString(bytes32 s) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            let n := 0
            for {} byte(n, s) { n := add(n, 1) } {} // Scan for '\0'.
            mstore(result, n) // Store the length.
            let o := add(result, 0x20)
            mstore(o, s) // Store the bytes of the string.
            mstore(add(o, n), 0) // Zeroize the slot after the string.
            mstore(0x40, add(result, 0x40)) // Allocate memory.
        }
    }

    /// @dev Returns the small string, with all bytes after the first null byte zeroized.
    function normalizeSmallString(bytes32 s) internal pure returns (bytes32 result) {
        /// @solidity memory-safe-assembly
        assembly {
            for {} byte(result, s) { result := add(result, 1) } {} // Scan for '\0'.
            mstore(0x00, s)
            mstore(result, 0x00)
            result := mload(0x00)
        }
    }

    /// @dev Returns the string as a normalized null-terminated small string.
    function toSmallString(string memory s) internal pure returns (bytes32 result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(s)
            if iszero(lt(result, 33)) {
                mstore(0x00, 0xec92f9a3) // `TooBigForSmallString()`.
                revert(0x1c, 0x04)
            }
            result := shl(shl(3, sub(32, result)), mload(add(s, result)))
        }
    }

    /// @dev Returns a lowercased copy of the string.
    /// WARNING! This function is only compatible with 7-bit ASCII strings.
    function lower(string memory subject) internal pure returns (string memory result) {
        result = toCase(subject, false);
    }

    /// @dev Returns an UPPERCASED copy of the string.
    /// WARNING! This function is only compatible with 7-bit ASCII strings.
    function upper(string memory subject) internal pure returns (string memory result) {
        result = toCase(subject, true);
    }

    /// @dev Escapes the string to be used within HTML tags.
    function escapeHTML(string memory s) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            let end := add(s, mload(s))
            let o := add(result, 0x20)
            // Store the bytes of the packed offsets and strides into the scratch space.
            // `packed = (stride << 5) | offset`. Max offset is 20. Max stride is 6.
            mstore(0x1f, 0x900094)
            mstore(0x08, 0xc0000000a6ab)
            // Store "&quot;&amp;&#39;&lt;&gt;" into the scratch space.
            mstore(0x00, shl(64, 0x2671756f743b26616d703b262333393b266c743b2667743b))
            for {} iszero(eq(s, end)) {} {
                s := add(s, 1)
                let c := and(mload(s), 0xff)
                // Not in `["\"","'","&","<",">"]`.
                if iszero(and(shl(c, 1), 0x500000c400000000)) {
                    mstore8(o, c)
                    o := add(o, 1)
                    continue
                }
                let t := shr(248, mload(c))
                mstore(o, mload(and(t, 0x1f)))
                o := add(o, shr(5, t))
            }
            mstore(o, 0) // Zeroize the slot after the string.
            mstore(result, sub(o, add(result, 0x20))) // Store the length.
            mstore(0x40, add(o, 0x20)) // Allocate memory.
        }
    }

    /// @dev Escapes the string to be used within double-quotes in a JSON.
    /// If `addDoubleQuotes` is true, the result will be enclosed in double-quotes.
    function escapeJSON(string memory s, bool addDoubleQuotes)
        internal
        pure
        returns (string memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            let o := add(result, 0x20)
            if addDoubleQuotes {
                mstore8(o, 34)
                o := add(1, o)
            }
            // Store "\\u0000" in scratch space.
            // Store "0123456789abcdef" in scratch space.
            // Also, store `{0x08:"b", 0x09:"t", 0x0a:"n", 0x0c:"f", 0x0d:"r"}`.
            // into the scratch space.
            mstore(0x15, 0x5c75303030303031323334353637383961626364656662746e006672)
            // Bitmask for detecting `["\"","\\"]`.
            let e := or(shl(0x22, 1), shl(0x5c, 1))
            for { let end := add(s, mload(s)) } iszero(eq(s, end)) {} {
                s := add(s, 1)
                let c := and(mload(s), 0xff)
                if iszero(lt(c, 0x20)) {
                    if iszero(and(shl(c, 1), e)) {
                        // Not in `["\"","\\"]`.
                        mstore8(o, c)
                        o := add(o, 1)
                        continue
                    }
                    mstore8(o, 0x5c) // "\\".
                    mstore8(add(o, 1), c)
                    o := add(o, 2)
                    continue
                }
                if iszero(and(shl(c, 1), 0x3700)) {
                    // Not in `["\b","\t","\n","\f","\d"]`.
                    mstore8(0x1d, mload(shr(4, c))) // Hex value.
                    mstore8(0x1e, mload(and(c, 15))) // Hex value.
                    mstore(o, mload(0x19)) // "\\u00XX".
                    o := add(o, 6)
                    continue
                }
                mstore8(o, 0x5c) // "\\".
                mstore8(add(o, 1), mload(add(c, 8)))
                o := add(o, 2)
            }
            if addDoubleQuotes {
                mstore8(o, 34)
                o := add(1, o)
            }
            mstore(o, 0) // Zeroize the slot after the string.
            mstore(result, sub(o, add(result, 0x20))) // Store the length.
            mstore(0x40, add(o, 0x20)) // Allocate memory.
        }
    }

    /// @dev Escapes the string to be used within double-quotes in a JSON.
    function escapeJSON(string memory s) internal pure returns (string memory result) {
        result = escapeJSON(s, false);
    }

    /// @dev Encodes `s` so that it can be safely used in a URI,
    /// just like `encodeURIComponent` in JavaScript.
    /// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
    /// See: https://datatracker.ietf.org/doc/html/rfc2396
    /// See: https://datatracker.ietf.org/doc/html/rfc3986
    function encodeURIComponent(string memory s) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            // Store "0123456789ABCDEF" in scratch space.
            // Uppercased to be consistent with JavaScript's implementation.
            mstore(0x0f, 0x30313233343536373839414243444546)
            let o := add(result, 0x20)
            for { let end := add(s, mload(s)) } iszero(eq(s, end)) {} {
                s := add(s, 1)
                let c := and(mload(s), 0xff)
                // If not in `[0-9A-Z-a-z-_.!~*'()]`.
                if iszero(and(1, shr(c, 0x47fffffe87fffffe03ff678200000000))) {
                    mstore8(o, 0x25) // '%'.
                    mstore8(add(o, 1), mload(and(shr(4, c), 15)))
                    mstore8(add(o, 2), mload(and(c, 15)))
                    o := add(o, 3)
                    continue
                }
                mstore8(o, c)
                o := add(o, 1)
            }
            mstore(result, sub(o, add(result, 0x20))) // Store the length.
            mstore(o, 0) // Zeroize the slot after the string.
            mstore(0x40, add(o, 0x20)) // Allocate memory.
        }
    }

    /// @dev Returns whether `a` equals `b`.
    function eq(string memory a, string memory b) internal pure returns (bool result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := eq(keccak256(add(a, 0x20), mload(a)), keccak256(add(b, 0x20), mload(b)))
        }
    }

    /// @dev Returns whether `a` equals `b`, where `b` is a null-terminated small string.
    function eqs(string memory a, bytes32 b) internal pure returns (bool result) {
        /// @solidity memory-safe-assembly
        assembly {
            // These should be evaluated on compile time, as far as possible.
            let m := not(shl(7, div(not(iszero(b)), 255))) // `0x7f7f ...`.
            let x := not(or(m, or(b, add(m, and(b, m)))))
            let r := shl(7, iszero(iszero(shr(128, x))))
            r := or(r, shl(6, iszero(iszero(shr(64, shr(r, x))))))
            r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
            r := or(r, shl(4, lt(0xffff, shr(r, x))))
            r := or(r, shl(3, lt(0xff, shr(r, x))))
            // forgefmt: disable-next-item
            result := gt(eq(mload(a), add(iszero(x), xor(31, shr(3, r)))),
                xor(shr(add(8, r), b), shr(add(8, r), mload(add(a, 0x20)))))
        }
    }

    /// @dev Packs a single string with its length into a single word.
    /// Returns `bytes32(0)` if the length is zero or greater than 31.
    function packOne(string memory a) internal pure returns (bytes32 result) {
        /// @solidity memory-safe-assembly
        assembly {
            // We don't need to zero right pad the string,
            // since this is our own custom non-standard packing scheme.
            result :=
                mul(
                    // Load the length and the bytes.
                    mload(add(a, 0x1f)),
                    // `length != 0 && length < 32`. Abuses underflow.
                    // Assumes that the length is valid and within the block gas limit.
                    lt(sub(mload(a), 1), 0x1f)
                )
        }
    }

    /// @dev Unpacks a string packed using {packOne}.
    /// Returns the empty string if `packed` is `bytes32(0)`.
    /// If `packed` is not an output of {packOne}, the output behavior is undefined.
    function unpackOne(bytes32 packed) internal pure returns (string memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40) // Grab the free memory pointer.
            mstore(0x40, add(result, 0x40)) // Allocate 2 words (1 for the length, 1 for the bytes).
            mstore(result, 0) // Zeroize the length slot.
            mstore(add(result, 0x1f), packed) // Store the length and bytes.
            mstore(add(add(result, 0x20), mload(result)), 0) // Right pad with zeroes.
        }
    }

    /// @dev Packs two strings with their lengths into a single word.
    /// Returns `bytes32(0)` if combined length is zero or greater than 30.
    function packTwo(string memory a, string memory b) internal pure returns (bytes32 result) {
        /// @solidity memory-safe-assembly
        assembly {
            let aLen := mload(a)
            // We don't need to zero right pad the strings,
            // since this is our own custom non-standard packing scheme.
            result :=
                mul(
                    or( // Load the length and the bytes of `a` and `b`.
                    shl(shl(3, sub(0x1f, aLen)), mload(add(a, aLen))), mload(sub(add(b, 0x1e), aLen))),
                    // `totalLen != 0 && totalLen < 31`. Abuses underflow.
                    // Assumes that the lengths are valid and within the block gas limit.
                    lt(sub(add(aLen, mload(b)), 1), 0x1e)
                )
        }
    }

    /// @dev Unpacks strings packed using {packTwo}.
    /// Returns the empty strings if `packed` is `bytes32(0)`.
    /// If `packed` is not an output of {packTwo}, the output behavior is undefined.
    function unpackTwo(bytes32 packed)
        internal
        pure
        returns (string memory resultA, string memory resultB)
    {
        /// @solidity memory-safe-assembly
        assembly {
            resultA := mload(0x40) // Grab the free memory pointer.
            resultB := add(resultA, 0x40)
            // Allocate 2 words for each string (1 for the length, 1 for the byte). Total 4 words.
            mstore(0x40, add(resultB, 0x40))
            // Zeroize the length slots.
            mstore(resultA, 0)
            mstore(resultB, 0)
            // Store the lengths and bytes.
            mstore(add(resultA, 0x1f), packed)
            mstore(add(resultB, 0x1f), mload(add(add(resultA, 0x20), mload(resultA))))
            // Right pad with zeroes.
            mstore(add(add(resultA, 0x20), mload(resultA)), 0)
            mstore(add(add(resultB, 0x20), mload(resultB)), 0)
        }
    }

    /// @dev Directly returns `a` without copying.
    function directReturn(string memory a) internal pure {
        assembly {
            // Assumes that the string does not start from the scratch space.
            let retStart := sub(a, 0x20)
            let retUnpaddedSize := add(mload(a), 0x40)
            // Right pad with zeroes. Just in case the string is produced
            // by a method that doesn't zero right pad.
            mstore(add(retStart, retUnpaddedSize), 0)
            mstore(retStart, 0x20) // Store the return offset.
            // End the transaction, returning the string.
            return(retStart, and(not(0x1f), add(0x1f, retUnpaddedSize)))
        }
    }
}

File 44 of 44 : LibBytes.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

/// @notice Library for byte related operations.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibBytes.sol)
library LibBytes {
    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                          STRUCTS                           */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Goated bytes storage struct that totally MOGs, no cap, fr.
    /// Uses less gas and bytecode than Solidity's native bytes storage. It's meta af.
    /// Packs length with the first 31 bytes if <255 bytes, so it’s mad tight.
    struct BytesStorage {
        bytes32 _spacer;
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                         CONSTANTS                          */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev The constant returned when the `search` is not found in the bytes.
    uint256 internal constant NOT_FOUND = type(uint256).max;

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                  BYTE STORAGE OPERATIONS                   */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Sets the value of the bytes storage `$` to `s`.
    function set(BytesStorage storage $, bytes memory s) internal {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(s)
            let packed := or(0xff, shl(8, n))
            for { let i := 0 } 1 {} {
                if iszero(gt(n, 0xfe)) {
                    i := 0x1f
                    packed := or(n, shl(8, mload(add(s, i))))
                    if iszero(gt(n, i)) { break }
                }
                let o := add(s, 0x20)
                mstore(0x00, $.slot)
                for { let p := keccak256(0x00, 0x20) } 1 {} {
                    sstore(add(p, shr(5, i)), mload(add(o, i)))
                    i := add(i, 0x20)
                    if iszero(lt(i, n)) { break }
                }
                break
            }
            sstore($.slot, packed)
        }
    }

    /// @dev Sets the value of the bytes storage `$` to `s`.
    function setCalldata(BytesStorage storage $, bytes calldata s) internal {
        /// @solidity memory-safe-assembly
        assembly {
            let packed := or(0xff, shl(8, s.length))
            for { let i := 0 } 1 {} {
                if iszero(gt(s.length, 0xfe)) {
                    i := 0x1f
                    packed := or(s.length, shl(8, shr(8, calldataload(s.offset))))
                    if iszero(gt(s.length, i)) { break }
                }
                mstore(0x00, $.slot)
                for { let p := keccak256(0x00, 0x20) } 1 {} {
                    sstore(add(p, shr(5, i)), calldataload(add(s.offset, i)))
                    i := add(i, 0x20)
                    if iszero(lt(i, s.length)) { break }
                }
                break
            }
            sstore($.slot, packed)
        }
    }

    /// @dev Sets the value of the bytes storage `$` to the empty bytes.
    function clear(BytesStorage storage $) internal {
        delete $._spacer;
    }

    /// @dev Returns whether the value stored is `$` is the empty bytes "".
    function isEmpty(BytesStorage storage $) internal view returns (bool) {
        return uint256($._spacer) & 0xff == uint256(0);
    }

    /// @dev Returns the length of the value stored in `$`.
    function length(BytesStorage storage $) internal view returns (uint256 result) {
        result = uint256($._spacer);
        /// @solidity memory-safe-assembly
        assembly {
            let n := and(0xff, result)
            result := or(mul(shr(8, result), eq(0xff, n)), mul(n, iszero(eq(0xff, n))))
        }
    }

    /// @dev Returns the value stored in `$`.
    function get(BytesStorage storage $) internal view returns (bytes memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            let o := add(result, 0x20)
            let packed := sload($.slot)
            let n := shr(8, packed)
            for { let i := 0 } 1 {} {
                if iszero(eq(and(packed, 0xff), 0xff)) {
                    mstore(o, packed)
                    n := and(0xff, packed)
                    i := 0x1f
                    if iszero(gt(n, i)) { break }
                }
                mstore(0x00, $.slot)
                for { let p := keccak256(0x00, 0x20) } 1 {} {
                    mstore(add(o, i), sload(add(p, shr(5, i))))
                    i := add(i, 0x20)
                    if iszero(lt(i, n)) { break }
                }
                break
            }
            mstore(result, n) // Store the length of the memory.
            mstore(add(o, n), 0) // Zeroize the slot after the bytes.
            mstore(0x40, add(add(o, n), 0x20)) // Allocate memory.
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                      BYTES OPERATIONS                      */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Returns `subject` all occurrences of `needle` replaced with `replacement`.
    function replace(bytes memory subject, bytes memory needle, bytes memory replacement)
        internal
        pure
        returns (bytes memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            let needleLen := mload(needle)
            let replacementLen := mload(replacement)
            let d := sub(result, subject) // Memory difference.
            let i := add(subject, 0x20) // Subject bytes pointer.
            mstore(0x00, add(i, mload(subject))) // End of subject.
            if iszero(gt(needleLen, mload(subject))) {
                let subjectSearchEnd := add(sub(mload(0x00), needleLen), 1)
                let h := 0 // The hash of `needle`.
                if iszero(lt(needleLen, 0x20)) { h := keccak256(add(needle, 0x20), needleLen) }
                let s := mload(add(needle, 0x20))
                for { let m := shl(3, sub(0x20, and(needleLen, 0x1f))) } 1 {} {
                    let t := mload(i)
                    // Whether the first `needleLen % 32` bytes of `subject` and `needle` matches.
                    if iszero(shr(m, xor(t, s))) {
                        if h {
                            if iszero(eq(keccak256(i, needleLen), h)) {
                                mstore(add(i, d), t)
                                i := add(i, 1)
                                if iszero(lt(i, subjectSearchEnd)) { break }
                                continue
                            }
                        }
                        // Copy the `replacement` one word at a time.
                        for { let j := 0 } 1 {} {
                            mstore(add(add(i, d), j), mload(add(add(replacement, 0x20), j)))
                            j := add(j, 0x20)
                            if iszero(lt(j, replacementLen)) { break }
                        }
                        d := sub(add(d, replacementLen), needleLen)
                        if needleLen {
                            i := add(i, needleLen)
                            if iszero(lt(i, subjectSearchEnd)) { break }
                            continue
                        }
                    }
                    mstore(add(i, d), t)
                    i := add(i, 1)
                    if iszero(lt(i, subjectSearchEnd)) { break }
                }
            }
            let end := mload(0x00)
            let n := add(sub(d, add(result, 0x20)), end)
            // Copy the rest of the bytes one word at a time.
            for {} lt(i, end) { i := add(i, 0x20) } { mstore(add(i, d), mload(i)) }
            let o := add(i, d)
            mstore(o, 0) // Zeroize the slot after the bytes.
            mstore(0x40, add(o, 0x20)) // Allocate memory.
            mstore(result, n) // Store the length.
        }
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from left to right, starting from `from`.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function indexOf(bytes memory subject, bytes memory needle, uint256 from)
        internal
        pure
        returns (uint256 result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result := not(0) // Initialize to `NOT_FOUND`.
            for { let subjectLen := mload(subject) } 1 {} {
                if iszero(mload(needle)) {
                    result := from
                    if iszero(gt(from, subjectLen)) { break }
                    result := subjectLen
                    break
                }
                let needleLen := mload(needle)
                let subjectStart := add(subject, 0x20)

                subject := add(subjectStart, from)
                let end := add(sub(add(subjectStart, subjectLen), needleLen), 1)
                let m := shl(3, sub(0x20, and(needleLen, 0x1f)))
                let s := mload(add(needle, 0x20))

                if iszero(and(lt(subject, end), lt(from, subjectLen))) { break }

                if iszero(lt(needleLen, 0x20)) {
                    for { let h := keccak256(add(needle, 0x20), needleLen) } 1 {} {
                        if iszero(shr(m, xor(mload(subject), s))) {
                            if eq(keccak256(subject, needleLen), h) {
                                result := sub(subject, subjectStart)
                                break
                            }
                        }
                        subject := add(subject, 1)
                        if iszero(lt(subject, end)) { break }
                    }
                    break
                }
                for {} 1 {} {
                    if iszero(shr(m, xor(mload(subject), s))) {
                        result := sub(subject, subjectStart)
                        break
                    }
                    subject := add(subject, 1)
                    if iszero(lt(subject, end)) { break }
                }
                break
            }
        }
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from left to right.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function indexOf(bytes memory subject, bytes memory needle) internal pure returns (uint256) {
        return indexOf(subject, needle, 0);
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from right to left, starting from `from`.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function lastIndexOf(bytes memory subject, bytes memory needle, uint256 from)
        internal
        pure
        returns (uint256 result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            for {} 1 {} {
                result := not(0) // Initialize to `NOT_FOUND`.
                let needleLen := mload(needle)
                if gt(needleLen, mload(subject)) { break }
                let w := result

                let fromMax := sub(mload(subject), needleLen)
                if iszero(gt(fromMax, from)) { from := fromMax }

                let end := add(add(subject, 0x20), w)
                subject := add(add(subject, 0x20), from)
                if iszero(gt(subject, end)) { break }
                // As this function is not too often used,
                // we shall simply use keccak256 for smaller bytecode size.
                for { let h := keccak256(add(needle, 0x20), needleLen) } 1 {} {
                    if eq(keccak256(subject, needleLen), h) {
                        result := sub(subject, add(end, 1))
                        break
                    }
                    subject := add(subject, w) // `sub(subject, 1)`.
                    if iszero(gt(subject, end)) { break }
                }
                break
            }
        }
    }

    /// @dev Returns the byte index of the first location of `needle` in `subject`,
    /// needleing from right to left.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function lastIndexOf(bytes memory subject, bytes memory needle)
        internal
        pure
        returns (uint256)
    {
        return lastIndexOf(subject, needle, type(uint256).max);
    }

    /// @dev Returns true if `needle` is found in `subject`, false otherwise.
    function contains(bytes memory subject, bytes memory needle) internal pure returns (bool) {
        return indexOf(subject, needle) != NOT_FOUND;
    }

    /// @dev Returns whether `subject` starts with `needle`.
    function startsWith(bytes memory subject, bytes memory needle)
        internal
        pure
        returns (bool result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(needle)
            // Just using keccak256 directly is actually cheaper.
            let t := eq(keccak256(add(subject, 0x20), n), keccak256(add(needle, 0x20), n))
            result := lt(gt(n, mload(subject)), t)
        }
    }

    /// @dev Returns whether `subject` ends with `needle`.
    function endsWith(bytes memory subject, bytes memory needle)
        internal
        pure
        returns (bool result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(needle)
            let notInRange := gt(n, mload(subject))
            // `subject + 0x20 + max(subject.length - needle.length, 0)`.
            let t := add(add(subject, 0x20), mul(iszero(notInRange), sub(mload(subject), n)))
            // Just using keccak256 directly is actually cheaper.
            result := gt(eq(keccak256(t, n), keccak256(add(needle, 0x20), n)), notInRange)
        }
    }

    /// @dev Returns `subject` repeated `times`.
    function repeat(bytes memory subject, uint256 times)
        internal
        pure
        returns (bytes memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let l := mload(subject) // Subject length.
            if iszero(or(iszero(times), iszero(l))) {
                result := mload(0x40)
                subject := add(subject, 0x20)
                let o := add(result, 0x20)
                for {} 1 {} {
                    // Copy the `subject` one word at a time.
                    for { let j := 0 } 1 {} {
                        mstore(add(o, j), mload(add(subject, j)))
                        j := add(j, 0x20)
                        if iszero(lt(j, l)) { break }
                    }
                    o := add(o, l)
                    times := sub(times, 1)
                    if iszero(times) { break }
                }
                mstore(o, 0) // Zeroize the slot after the bytes.
                mstore(0x40, add(o, 0x20)) // Allocate memory.
                mstore(result, sub(o, add(result, 0x20))) // Store the length.
            }
        }
    }

    /// @dev Returns a copy of `subject` sliced from `start` to `end` (exclusive).
    /// `start` and `end` are byte offsets.
    function slice(bytes memory subject, uint256 start, uint256 end)
        internal
        pure
        returns (bytes memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let l := mload(subject) // Subject length.
            if iszero(gt(l, end)) { end := l }
            if iszero(gt(l, start)) { start := l }
            if lt(start, end) {
                result := mload(0x40)
                let n := sub(end, start)
                let i := add(subject, start)
                let w := not(0x1f)
                // Copy the `subject` one word at a time, backwards.
                for { let j := and(add(n, 0x1f), w) } 1 {} {
                    mstore(add(result, j), mload(add(i, j)))
                    j := add(j, w) // `sub(j, 0x20)`.
                    if iszero(j) { break }
                }
                let o := add(add(result, 0x20), n)
                mstore(o, 0) // Zeroize the slot after the bytes.
                mstore(0x40, add(o, 0x20)) // Allocate memory.
                mstore(result, n) // Store the length.
            }
        }
    }

    /// @dev Returns a copy of `subject` sliced from `start` to the end of the bytes.
    /// `start` is a byte offset.
    function slice(bytes memory subject, uint256 start)
        internal
        pure
        returns (bytes memory result)
    {
        result = slice(subject, start, type(uint256).max);
    }

    /// @dev Reduces the size of `subject` to `n`.
    /// If `n` is greater than the size of `subject`, this will be a no-op.
    function truncate(bytes memory subject, uint256 n)
        internal
        pure
        returns (bytes memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result := subject
            mstore(mul(lt(n, mload(result)), result), n)
        }
    }

    /// @dev Returns a copy of `subject`, with the length reduced to `n`.
    /// If `n` is greater than the size of `subject`, this will be a no-op.
    function truncatedCalldata(bytes calldata subject, uint256 n)
        internal
        pure
        returns (bytes calldata result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result.offset := subject.offset
            result.length := xor(n, mul(xor(n, subject.length), lt(subject.length, n)))
        }
    }

    /// @dev Returns all the indices of `needle` in `subject`.
    /// The indices are byte offsets.
    function indicesOf(bytes memory subject, bytes memory needle)
        internal
        pure
        returns (uint256[] memory result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let searchLen := mload(needle)
            if iszero(gt(searchLen, mload(subject))) {
                result := mload(0x40)
                let i := add(subject, 0x20)
                let o := add(result, 0x20)
                let subjectSearchEnd := add(sub(add(i, mload(subject)), searchLen), 1)
                let h := 0 // The hash of `needle`.
                if iszero(lt(searchLen, 0x20)) { h := keccak256(add(needle, 0x20), searchLen) }
                let s := mload(add(needle, 0x20))
                for { let m := shl(3, sub(0x20, and(searchLen, 0x1f))) } 1 {} {
                    let t := mload(i)
                    // Whether the first `searchLen % 32` bytes of `subject` and `needle` matches.
                    if iszero(shr(m, xor(t, s))) {
                        if h {
                            if iszero(eq(keccak256(i, searchLen), h)) {
                                i := add(i, 1)
                                if iszero(lt(i, subjectSearchEnd)) { break }
                                continue
                            }
                        }
                        mstore(o, sub(i, add(subject, 0x20))) // Append to `result`.
                        o := add(o, 0x20)
                        i := add(i, searchLen) // Advance `i` by `searchLen`.
                        if searchLen {
                            if iszero(lt(i, subjectSearchEnd)) { break }
                            continue
                        }
                    }
                    i := add(i, 1)
                    if iszero(lt(i, subjectSearchEnd)) { break }
                }
                mstore(result, shr(5, sub(o, add(result, 0x20)))) // Store the length of `result`.
                // Allocate memory for result.
                // We allocate one more word, so this array can be recycled for {split}.
                mstore(0x40, add(o, 0x20))
            }
        }
    }

    /// @dev Returns a arrays of bytess based on the `delimiter` inside of the `subject` bytes.
    function split(bytes memory subject, bytes memory delimiter)
        internal
        pure
        returns (bytes[] memory result)
    {
        uint256[] memory indices = indicesOf(subject, delimiter);
        /// @solidity memory-safe-assembly
        assembly {
            let w := not(0x1f)
            let indexPtr := add(indices, 0x20)
            let indicesEnd := add(indexPtr, shl(5, add(mload(indices), 1)))
            mstore(add(indicesEnd, w), mload(subject))
            mstore(indices, add(mload(indices), 1))
            for { let prevIndex := 0 } 1 {} {
                let index := mload(indexPtr)
                mstore(indexPtr, 0x60)
                if iszero(eq(index, prevIndex)) {
                    let element := mload(0x40)
                    let l := sub(index, prevIndex)
                    mstore(element, l) // Store the length of the element.
                    // Copy the `subject` one word at a time, backwards.
                    for { let o := and(add(l, 0x1f), w) } 1 {} {
                        mstore(add(element, o), mload(add(add(subject, prevIndex), o)))
                        o := add(o, w) // `sub(o, 0x20)`.
                        if iszero(o) { break }
                    }
                    mstore(add(add(element, 0x20), l), 0) // Zeroize the slot after the bytes.
                    // Allocate memory for the length and the bytes, rounded up to a multiple of 32.
                    mstore(0x40, add(element, and(add(l, 0x3f), w)))
                    mstore(indexPtr, element) // Store the `element` into the array.
                }
                prevIndex := add(index, mload(delimiter))
                indexPtr := add(indexPtr, 0x20)
                if iszero(lt(indexPtr, indicesEnd)) { break }
            }
            result := indices
            if iszero(mload(delimiter)) {
                result := add(indices, 0x20)
                mstore(result, sub(mload(indices), 2))
            }
        }
    }

    /// @dev Returns a concatenated bytes of `a` and `b`.
    /// Cheaper than `bytes.concat()` and does not de-align the free memory pointer.
    function concat(bytes memory a, bytes memory b) internal pure returns (bytes memory result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(0x40)
            let w := not(0x1f)
            let aLen := mload(a)
            // Copy `a` one word at a time, backwards.
            for { let o := and(add(aLen, 0x20), w) } 1 {} {
                mstore(add(result, o), mload(add(a, o)))
                o := add(o, w) // `sub(o, 0x20)`.
                if iszero(o) { break }
            }
            let bLen := mload(b)
            let output := add(result, aLen)
            // Copy `b` one word at a time, backwards.
            for { let o := and(add(bLen, 0x20), w) } 1 {} {
                mstore(add(output, o), mload(add(b, o)))
                o := add(o, w) // `sub(o, 0x20)`.
                if iszero(o) { break }
            }
            let totalLen := add(aLen, bLen)
            let last := add(add(result, 0x20), totalLen)
            mstore(last, 0) // Zeroize the slot after the bytes.
            mstore(result, totalLen) // Store the length.
            mstore(0x40, add(last, 0x20)) // Allocate memory.
        }
    }

    /// @dev Returns whether `a` equals `b`.
    function eq(bytes memory a, bytes memory b) internal pure returns (bool result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := eq(keccak256(add(a, 0x20), mload(a)), keccak256(add(b, 0x20), mload(b)))
        }
    }

    /// @dev Returns whether `a` equals `b`, where `b` is a null-terminated small bytes.
    function eqs(bytes memory a, bytes32 b) internal pure returns (bool result) {
        /// @solidity memory-safe-assembly
        assembly {
            // These should be evaluated on compile time, as far as possible.
            let m := not(shl(7, div(not(iszero(b)), 255))) // `0x7f7f ...`.
            let x := not(or(m, or(b, add(m, and(b, m)))))
            let r := shl(7, iszero(iszero(shr(128, x))))
            r := or(r, shl(6, iszero(iszero(shr(64, shr(r, x))))))
            r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
            r := or(r, shl(4, lt(0xffff, shr(r, x))))
            r := or(r, shl(3, lt(0xff, shr(r, x))))
            // forgefmt: disable-next-item
            result := gt(eq(mload(a), add(iszero(x), xor(31, shr(3, r)))),
                xor(shr(add(8, r), b), shr(add(8, r), mload(add(a, 0x20)))))
        }
    }

    /// @dev Directly returns `a` without copying.
    function directReturn(bytes memory a) internal pure {
        assembly {
            // Assumes that the bytes does not start from the scratch space.
            let retStart := sub(a, 0x20)
            let retUnpaddedSize := add(mload(a), 0x40)
            // Right pad with zeroes. Just in case the bytes is produced
            // by a method that doesn't zero right pad.
            mstore(add(retStart, retUnpaddedSize), 0)
            mstore(retStart, 0x20) // Store the return offset.
            // End the transaction, returning the bytes.
            return(retStart, and(not(0x1f), add(0x1f, retUnpaddedSize)))
        }
    }

    /// @dev Returns the word at `offset`, without any bounds checks.
    /// To load an address, you can use `address(bytes20(load(a, offset)))`.
    function load(bytes memory a, uint256 offset) internal pure returns (bytes32 result) {
        /// @solidity memory-safe-assembly
        assembly {
            result := mload(add(add(a, 0x20), offset))
        }
    }

    /// @dev Returns the word at `offset`, without any bounds checks.
    /// To load an address, you can use `address(bytes20(loadCalldata(a, offset)))`.
    function loadCalldata(bytes calldata a, uint256 offset)
        internal
        pure
        returns (bytes32 result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result := calldataload(add(a.offset, offset))
        }
    }
}

Settings
{
  "remappings": [
    "@chimera/=lib/V2-gov/lib/chimera/src/",
    "@ensdomains/=lib/V2-gov/lib/v4-core/node_modules/@ensdomains/",
    "@openzeppelin/=lib/V2-gov/lib/v4-core/lib/openzeppelin-contracts/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "Solady/=lib/Solady/src/",
    "V2-gov/=lib/V2-gov/",
    "chimera/=lib/V2-gov/lib/chimera/src/",
    "ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/V2-gov/lib/v4-core/lib/forge-gas-snapshot/src/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "hardhat/=lib/V2-gov/lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "openzeppelin/=lib/V2-gov/lib/openzeppelin-contracts/",
    "solmate/=lib/V2-gov/lib/v4-core/lib/solmate/src/",
    "v4-core/=lib/V2-gov/lib/v4-core/"
  ],
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "metadata": {
    "useLiteralContent": false,
    "bytecodeHash": "ipfs",
    "appendCBOR": true
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "evmVersion": "cancun",
  "viaIR": false,
  "libraries": {}
}

Contract ABI

[{"inputs":[{"internalType":"contract IAddressesRegistry","name":"_addressesRegistry","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"BatchSharesRatioTooHigh","type":"error"},{"inputs":[],"name":"CallerNotBorrowerOperations","type":"error"},{"inputs":[],"name":"CallerNotCollateralRegistry","type":"error"},{"inputs":[],"name":"EmptyData","type":"error"},{"inputs":[{"internalType":"uint256","name":"_coll","type":"uint256"}],"name":"MinCollNotReached","type":"error"},{"inputs":[],"name":"NotEnoughBoldBalance","type":"error"},{"inputs":[],"name":"NotShutDown","type":"error"},{"inputs":[],"name":"NothingToLiquidate","type":"error"},{"inputs":[],"name":"OnlyOneTroveLeft","type":"error"},{"inputs":[],"name":"ZeroAmount","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newActivePoolAddress","type":"address"}],"name":"ActivePoolAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_interestBatchManager","type":"address"},{"indexed":false,"internalType":"enum ITroveEvents.BatchOperation","name":"_operation","type":"uint8"},{"indexed":false,"internalType":"uint256","name":"_debt","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_coll","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_annualInterestRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_annualManagementFee","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_totalDebtShares","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_debtIncreaseFromUpfrontFee","type":"uint256"}],"name":"BatchUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_troveId","type":"uint256"},{"indexed":false,"internalType":"address","name":"_interestBatchManager","type":"address"},{"indexed":false,"internalType":"uint256","name":"_batchDebtShares","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_coll","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_stake","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_snapshotOfTotalCollRedist","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_snapshotOfTotalDebtRedist","type":"uint256"}],"name":"BatchedTroveUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newBoldTokenAddress","type":"address"}],"name":"BoldTokenAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newBorrowerOperationsAddress","type":"address"}],"name":"BorrowerOperationsAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_collSurplusPoolAddress","type":"address"}],"name":"CollSurplusPoolAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_collateralRegistryAddress","type":"address"}],"name":"CollateralRegistryAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newDefaultPoolAddress","type":"address"}],"name":"DefaultPoolAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_gasPoolAddress","type":"address"}],"name":"GasPoolAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_debtOffsetBySP","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_debtRedistributed","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_boldGasCompensation","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_collGasCompensation","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_collSentToSP","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_collRedistributed","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_collSurplus","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_L_ETH","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_L_boldDebt","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_price","type":"uint256"}],"name":"Liquidation","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newPriceFeedAddress","type":"address"}],"name":"PriceFeedAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_attemptedBoldAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_actualBoldAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_ETHSent","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_ETHFee","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_price","type":"uint256"}],"name":"Redemption","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_troveId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_ETHFee","type":"uint256"}],"name":"RedemptionFeePaidToTrove","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_sortedTrovesAddress","type":"address"}],"name":"SortedTrovesAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_stabilityPoolAddress","type":"address"}],"name":"StabilityPoolAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newTroveNFTAddress","type":"address"}],"name":"TroveNFTAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_troveId","type":"uint256"},{"indexed":false,"internalType":"enum ITroveEvents.Operation","name":"_operation","type":"uint8"},{"indexed":false,"internalType":"uint256","name":"_annualInterestRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_debtIncreaseFromRedist","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_debtIncreaseFromUpfrontFee","type":"uint256"},{"indexed":false,"internalType":"int256","name":"_debtChangeFromOperation","type":"int256"},{"indexed":false,"internalType":"uint256","name":"_collIncreaseFromRedist","type":"uint256"},{"indexed":false,"internalType":"int256","name":"_collChangeFromOperation","type":"int256"}],"name":"TroveOperation","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_troveId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_debt","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_coll","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_stake","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_annualInterestRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_snapshotOfTotalCollRedist","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_snapshotOfTotalDebtRedist","type":"uint256"}],"name":"TroveUpdated","type":"event"},{"inputs":[],"name":"CCR","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"Troves","outputs":[{"internalType":"uint256","name":"debt","type":"uint256"},{"internalType":"uint256","name":"coll","type":"uint256"},{"internalType":"uint256","name":"stake","type":"uint256"},{"internalType":"enum ITroveManager.Status","name":"status","type":"uint8"},{"internalType":"uint64","name":"arrayIndex","type":"uint64"},{"internalType":"uint64","name":"lastDebtUpdateTime","type":"uint64"},{"internalType":"uint64","name":"lastInterestRateAdjTime","type":"uint64"},{"internalType":"uint256","name":"annualInterestRate","type":"uint256"},{"internalType":"address","name":"interestBatchManager","type":"address"},{"internalType":"uint256","name":"batchDebtShares","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"activePool","outputs":[{"internalType":"contract IActivePool","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"batchIds","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"_troveArray","type":"uint256[]"}],"name":"batchLiquidateTroves","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"borrowerOperations","outputs":[{"internalType":"contract IBorrowerOperations","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_price","type":"uint256"}],"name":"getCurrentICR","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getEntireSystemColl","outputs":[{"internalType":"uint256","name":"entireSystemColl","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getEntireSystemDebt","outputs":[{"internalType":"uint256","name":"entireSystemDebt","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_batchAddress","type":"address"}],"name":"getLatestBatchData","outputs":[{"components":[{"internalType":"uint256","name":"entireDebtWithoutRedistribution","type":"uint256"},{"internalType":"uint256","name":"entireCollWithoutRedistribution","type":"uint256"},{"internalType":"uint256","name":"accruedInterest","type":"uint256"},{"internalType":"uint256","name":"recordedDebt","type":"uint256"},{"internalType":"uint256","name":"annualInterestRate","type":"uint256"},{"internalType":"uint256","name":"weightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"annualManagementFee","type":"uint256"},{"internalType":"uint256","name":"accruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"weightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"lastDebtUpdateTime","type":"uint256"},{"internalType":"uint256","name":"lastInterestRateAdjTime","type":"uint256"}],"internalType":"struct LatestBatchData","name":"batch","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"}],"name":"getLatestTroveData","outputs":[{"components":[{"internalType":"uint256","name":"entireDebt","type":"uint256"},{"internalType":"uint256","name":"entireColl","type":"uint256"},{"internalType":"uint256","name":"redistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"redistCollGain","type":"uint256"},{"internalType":"uint256","name":"accruedInterest","type":"uint256"},{"internalType":"uint256","name":"recordedDebt","type":"uint256"},{"internalType":"uint256","name":"annualInterestRate","type":"uint256"},{"internalType":"uint256","name":"weightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"accruedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"lastInterestRateAdjTime","type":"uint256"}],"internalType":"struct LatestTroveData","name":"trove","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"}],"name":"getTroveAnnualInterestRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_index","type":"uint256"}],"name":"getTroveFromTroveIdsArray","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getTroveIdsCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"}],"name":"getTroveStatus","outputs":[{"internalType":"enum ITroveManager.Status","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getUnbackedPortionPriceAndRedeemability","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"lastZombieTroveId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_newColl","type":"uint256"},{"internalType":"uint256","name":"_newDebt","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"}],"name":"onAdjustTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_newTroveColl","type":"uint256"},{"internalType":"uint256","name":"_newTroveDebt","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"},{"internalType":"address","name":"_batchAddress","type":"address"},{"internalType":"uint256","name":"_newBatchColl","type":"uint256"},{"internalType":"uint256","name":"_newBatchDebt","type":"uint256"}],"name":"onAdjustTroveInsideBatch","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_newColl","type":"uint256"},{"internalType":"uint256","name":"_newDebt","type":"uint256"},{"internalType":"uint256","name":"_newAnnualInterestRate","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"}],"name":"onAdjustTroveInterestRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_newTroveColl","type":"uint256"},{"internalType":"uint256","name":"_newTroveDebt","type":"uint256"},{"internalType":"address","name":"_batchAddress","type":"address"},{"internalType":"uint256","name":"_newBatchColl","type":"uint256"},{"internalType":"uint256","name":"_newBatchDebt","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"}],"name":"onApplyTroveInterest","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"},{"internalType":"address","name":"_batchAddress","type":"address"},{"internalType":"uint256","name":"_newBatchColl","type":"uint256"},{"internalType":"uint256","name":"_newBatchDebt","type":"uint256"}],"name":"onCloseTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_batchAddress","type":"address"},{"internalType":"uint256","name":"_newColl","type":"uint256"},{"internalType":"uint256","name":"_newDebt","type":"uint256"},{"internalType":"uint256","name":"_newAnnualManagementFee","type":"uint256"}],"name":"onLowerBatchManagerAnnualFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"uint256","name":"_troveId","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"},{"internalType":"uint256","name":"_annualInterestRate","type":"uint256"}],"name":"onOpenTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"uint256","name":"_troveId","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"},{"internalType":"address","name":"_batchAddress","type":"address"},{"internalType":"uint256","name":"_batchColl","type":"uint256"},{"internalType":"uint256","name":"_batchDebt","type":"uint256"}],"name":"onOpenTroveAndJoinBatch","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"},{"internalType":"uint256","name":"_annualInterestRate","type":"uint256"},{"internalType":"uint256","name":"_annualManagementFee","type":"uint256"}],"name":"onRegisterBatchManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_newTroveColl","type":"uint256"},{"internalType":"uint256","name":"_newTroveDebt","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"_troveChange","type":"tuple"},{"internalType":"address","name":"_batchAddress","type":"address"},{"internalType":"uint256","name":"_newBatchColl","type":"uint256"},{"internalType":"uint256","name":"_newBatchDebt","type":"uint256"},{"internalType":"uint256","name":"_newAnnualInterestRate","type":"uint256"}],"name":"onRemoveFromBatch","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_batchAddress","type":"address"},{"internalType":"uint256","name":"_newColl","type":"uint256"},{"internalType":"uint256","name":"_newDebt","type":"uint256"},{"internalType":"uint256","name":"_newAnnualInterestRate","type":"uint256"},{"internalType":"uint256","name":"_upfrontFee","type":"uint256"}],"name":"onSetBatchManagerAnnualInterestRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"troveId","type":"uint256"},{"internalType":"uint256","name":"troveColl","type":"uint256"},{"internalType":"uint256","name":"troveDebt","type":"uint256"},{"components":[{"internalType":"uint256","name":"appliedRedistBoldDebtGain","type":"uint256"},{"internalType":"uint256","name":"appliedRedistCollGain","type":"uint256"},{"internalType":"uint256","name":"collIncrease","type":"uint256"},{"internalType":"uint256","name":"collDecrease","type":"uint256"},{"internalType":"uint256","name":"debtIncrease","type":"uint256"},{"internalType":"uint256","name":"debtDecrease","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedDebt","type":"uint256"},{"internalType":"uint256","name":"upfrontFee","type":"uint256"},{"internalType":"uint256","name":"batchAccruedManagementFee","type":"uint256"},{"internalType":"uint256","name":"newWeightedRecordedBatchManagementFee","type":"uint256"},{"internalType":"uint256","name":"oldWeightedRecordedBatchManagementFee","type":"uint256"}],"internalType":"struct TroveChange","name":"troveChange","type":"tuple"},{"internalType":"address","name":"newBatchAddress","type":"address"},{"internalType":"uint256","name":"newBatchColl","type":"uint256"},{"internalType":"uint256","name":"newBatchDebt","type":"uint256"}],"internalType":"struct ITroveManager.OnSetInterestBatchManagerParams","name":"_params","type":"tuple"}],"name":"onSetInterestBatchManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_redeemer","type":"address"},{"internalType":"uint256","name":"_boldamount","type":"uint256"},{"internalType":"uint256","name":"_price","type":"uint256"},{"internalType":"uint256","name":"_redemptionRate","type":"uint256"},{"internalType":"uint256","name":"_maxIterations","type":"uint256"}],"name":"redeemCollateral","outputs":[{"internalType":"uint256","name":"_redemeedAmount","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"rewardSnapshots","outputs":[{"internalType":"uint256","name":"coll","type":"uint256"},{"internalType":"uint256","name":"boldDebt","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"}],"name":"setTroveStatusToActive","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"shutdown","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"shutdownTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sortedTroves","outputs":[{"internalType":"contract ISortedTroves","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"stabilityPool","outputs":[{"internalType":"contract IStabilityPool","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"troveNFT","outputs":[{"internalType":"contract ITroveNFT","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_boldAmount","type":"uint256"},{"internalType":"uint256[]","name":"_troveIds","type":"uint256[]"},{"internalType":"uint256","name":"_minCollateral","type":"uint256"}],"name":"urgentRedemption","outputs":[],"stateMutability":"nonpayable","type":"function"}]

61014060405234801562000011575f80fd5b506040516200685038038062006850833981016040819052620000349162000b52565b80806001600160a01b0316637f7dde4a6040518163ffffffff1660e01b8152600401602060405180830381865afa15801562000072573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019062000098919062000b52565b5f806101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b0316633cc742256040518163ffffffff1660e01b8152600401602060405180830381865afa158015620000f9573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906200011f919062000b52565b60015f6101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b031663741bef1a6040518163ffffffff1660e01b8152600401602060405180830381865afa15801562000181573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620001a7919062000b52565b600280546001600160a01b0319166001600160a01b039283161790555f54604051911681527f78f058b189175430c48dc02699e3a0031ea4ff781536dc2fab847de4babdd8829060200160405180910390a16001546040516001600160a01b0390911681527f5ee0cae2f063ed938bb55046f6a932fb6ae792bf43624806bb90abe68a50be9b9060200160405180910390a16002546040516001600160a01b0390911681527f8c537274438aa850a330284665d81a85dd38267d09e4050d416bfc94142db2649060200160405180910390a150806001600160a01b0316635733d58f6040518163ffffffff1660e01b81526004016020604051808303815f875af1158015620002b8573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620002de919062000b77565b60a08181525050806001600160a01b031663794e57246040518163ffffffff1660e01b81526004016020604051808303815f875af115801562000323573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019062000349919062000b77565b60c08181525050806001600160a01b03166358d5a9616040518163ffffffff1660e01b81526004016020604051808303815f875af11580156200038e573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620003b4919062000b77565b60e08181525050806001600160a01b031663370619be6040518163ffffffff1660e01b81526004016020604051808303815f875af1158015620003f9573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906200041f919062000b77565b6101008181525050806001600160a01b0316631170f2d46040518163ffffffff1660e01b81526004016020604051808303815f875af115801562000465573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906200048b919062000b77565b6101208181525050806001600160a01b031663059e01136040518163ffffffff1660e01b8152600401602060405180830381865afa158015620004d0573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620004f6919062000b52565b60035f6101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b03166377553ad46040518163ffffffff1660e01b8152600401602060405180830381865afa15801562000558573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906200057e919062000b52565b600480546001600160a01b0319166001600160a01b039283161781556040805163048c661d60e01b815290519284169263048c661d928281019260209291908290030181865afa158015620005d5573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620005fb919062000b52565b60055f6101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b031663fe9d03236040518163ffffffff1660e01b8152600401602060405180830381865afa1580156200065d573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019062000683919062000b52565b60065f6101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b031663cda775f96040518163ffffffff1660e01b8152600401602060405180830381865afa158015620006e5573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906200070b919062000b52565b60075f6101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b031663630afce56040518163ffffffff1660e01b8152600401602060405180830381865afa1580156200076d573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019062000793919062000b52565b60085f6101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b031663ae9187546040518163ffffffff1660e01b8152600401602060405180830381865afa158015620007f5573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906200081b919062000b52565b60095f6101000a8154816001600160a01b0302191690836001600160a01b03160217905550806001600160a01b031663ad5c46486040518163ffffffff1660e01b81526004016020604051808303815f875af11580156200087e573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620008a4919062000b52565b6001600160a01b03166080816001600160a01b031681525050806001600160a01b031663d330fadd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015620008fa573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019062000920919062000b52565b600a80546001600160a01b0319166001600160a01b03928316179055600354604051911681527f39b3d3f08f5292d52497444fc183b3915a339c0b41fb021bf52ae59505e455b29060200160405180910390a16004546040516001600160a01b0390911681527f3ca631ffcd2a9b5d9ae18543fc82f58eb4ca33af9e6ab01b7a8e95331e6ed9859060200160405180910390a16005546040516001600160a01b0390911681527f82966d27eea39b038ee0fa30cd16532bb24f6e65d31cb58fb227aa5766cdcc7f9060200160405180910390a16006546040516001600160a01b0390911681527fcfb07d791fcafc032b35837b50eb84b74df518cf4cc287e8084f47630fa70fa09060200160405180910390a16007546040516001600160a01b0390911681527fe67f36a6e961157d6eff83b91f3af5a62131ceb6f04954ef74f51c1c05e7f88d9060200160405180910390a16008546040516001600160a01b0390911681527f28fe9b1bb8b27b863bb5635cb5bbd4e1beb7af490191ba03efe587680895b4fd9060200160405180910390a16009546040516001600160a01b0390911681527f65f4cf077bc01e4742eb5ad98326f6e95b63548ea24b17f8d5e823111fe788009060200160405180910390a1600a546040516001600160a01b0390911681527f4f8a3037ce0d3c62ab7c79fec792f6db7216b27b94e09faf499753381c33f8479060200160405180910390a15062000b8f565b6001600160a01b038116811462000b4f575f80fd5b50565b5f6020828403121562000b63575f80fd5b815162000b708162000b3a565b9392505050565b5f6020828403121562000b88575f80fd5b5051919050565b60805160a05160c05160e0516101005161012051615c7962000bd75f395f614fea01525f614f8a01525f610fe101525f613bf601525f61034001525f613ee40152615c795ff3fe608060405234801561000f575f80fd5b5060043610610229575f3560e01c806377553ad41161012a578063b3e16c30116100b4578063ca0ca8d211610079578063ca0ca8d214610662578063e47bfaf114610675578063ef49a6b4146106a7578063f8a239e8146106ba578063fc0e74d1146106cd575f80fd5b8063b3e16c3014610603578063b5a672c214610616578063bf49e64914610629578063c067e95c1461063c578063c2020f041461064f575f80fd5b8063a411219c116100fa578063a411219c146104ab578063aad3f4041461053f578063ae918754146105ca578063b0141775146105dd578063b3d346b9146105f0575f80fd5b806377553ad414610476578063795d26c3146104895780637f7dde4a14610491578063887105d3146104a3575f80fd5b8063499b069f116101b65780635d6485881161017b5780635d6485881461036b5780635ef3b8bf146103a6578063613cacae146103b95780636daed00014610450578063735ab2a414610463575f80fd5b8063499b069f146102fb5780634aff96e11461030e5780634ea15f37146103165780635733d58f1461033b5780635856908114610362575f80fd5b80631ca2d7d9116101fc5780631ca2d7d91461029c5780631cf74075146102af5780631d7be800146102c25780631ef11b62146102d557806338116fa3146102e8575f80fd5b8063015402871461022d578063048c661d14610249578063059e0113146102745780630f83069314610287575b5f80fd5b61023660155481565b6040519081526020015b60405180910390f35b60055461025c906001600160a01b031681565b6040516001600160a01b039091168152602001610240565b60035461025c906001600160a01b031681565b61029a610295366004615230565b6106d5565b005b61029a6102aa366004615393565b610804565b61029a6102bd3660046153f7565b610aab565b61029a6102d036600461545d565b610c49565b6102366102e336600461549b565b610d87565b61029a6102f636600461549b565b610dab565b61029a6103093660046154b2565b610de0565b601354610236565b61031e610ec7565b604080519384526020840192909252151590820152606001610240565b6102367f000000000000000000000000000000000000000000000000000000000000000081565b61023660185481565b61039161037936600461549b565b60126020525f90815260409020805460019091015482565b60408051928352602083019190915201610240565b6102366103b436600461549b565b611026565b6103cc6103c73660046154e4565b61113a565b60405161024091905f61016082019050825182526020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015160c083015260e083015160e083015261010080840151818401525061012080840151818401525061014080840151818401525092915050565b61029a61045e3660046154ff565b611151565b61029a61047136600461553f565b611203565b60045461025c906001600160a01b031681565b61023661132b565b5f5461025c906001600160a01b031681565b610236611420565b6105296104b936600461549b565b600b6020525f9081526040902080546001820154600283015460038401546004850154600586015460069096015494959394929360ff8316936001600160401b036101008504811694600160481b8104821694600160881b9091049091169290916001600160a01b03909116908a565b6040516102409a999897969594939291906155b8565b61055261054d36600461549b565b6114de565b60405161024091905f61014082019050825182526020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015160c083015260e083015160e083015261010080840151818401525061012080840151818401525092915050565b60095461025c906001600160a01b031681565b61029a6105eb366004615623565b6114f0565b61025c6105fe36600461549b565b611718565b61029a610611366004615669565b611740565b61029a61062436600461567a565b611af1565b61029a6106373660046156dc565b611cd3565b61023661064a366004615749565b611e4f565b61029a61065d366004615769565b611e7e565b61029a6106703660046157e6565b612249565b61069a61068336600461549b565b5f908152600b602052604090206003015460ff1690565b604051610240919061581e565b61029a6106b536600461582c565b6122df565b6102366106c83660046154ff565b612661565b61029a612ac8565b6106dd612b2b565b5f858152600b6020908152604090912060018082018790558582556004820185905560039091018054600160481b600160c81b031916600160481b426001600160401b031690810267ffffffffffffffff60881b191691909117600160881b919091021790555461075f916001600160a01b0390911690833590840135612b58565b6010545f8681526012602052604090209081556011546001909101555f858152600b60205260409081902060020154601054601154925188935f80516020615bc4833981519152936107b79389938b938a92916158cc565b60405180910390a2845f80516020615c04833981519152600384845f01358561010001355f87602001355f6040516107f59796959493929190615904565b60405180910390a25050505050565b61080c612b2b565b5f61081a8560400151612c15565b6040868101515f898152600b60209081528382206001808201949094556002810186905560038101805460ff19811686178255601354600590930180546001600160a01b038e166001600160a01b0319909116179055426001600160401b03908116600160881b0267ffffffffffffffff60881b1991909416610100021678ffffffffffffffff0000000000000000ffffffffffffffffff1990911617919091178417905560105460129091529290209182556011549101559050601380546001810182555f919091527f66de8ffda797e3de9c05e8fc57b3bf0ec28a930d40b0d285d93c06501cf6a09001869055608085015161091a5761091a615942565b61092e868587886080015187876001612c48565b5f81600d5461093d919061596a565b600d8190556003546040516340c10f1960e01b81526001600160a01b038b81166004830152602482018b90529293509116906340c10f19906044015f604051808303815f87803b15801561098f575f80fd5b505af11580156109a1573d5f803e3d5ffd5b50505050865f80516020615be483398151915286600b5f8b81526020019081526020015f20600601548960400151866010546011546040516109e89695949392919061597d565b60405180910390a26001600160a01b0385165f908152600c60205260408082206003015461010089015160808a01518a84015193518c955f80516020615c0483398151915295610a4395600795909492939092918491615904565b60405180910390a26001600160a01b0385165f818152600c602052604080822080546001820154600383015460048481015460059095015495515f80516020615c2483398151915297610a9997929692916159c0565b60405180910390a25050505050505050565b610ab3612b2b565b5f878152600b6020908152604080832060019081018a9055601054601290935292209081556011549101555f610ae98888612fd0565b90505f8611610afa57610afa615942565b610b0a8885878987876001612c48565b60015485516020870151610b28926001600160a01b03169190612b58565b875f80516020615be483398151915285600b5f8c81526020019081526020015f20600601548a85601054601154604051610b679695949392919061597d565b60405180910390a2875f80516020615c048339815191526002600c5f886001600160a01b03166001600160a01b031681526020019081526020015f2060030154885f01518961010001518a60a001518b60800151610bc591906159ce565b8b602001518c606001518d60400151610bde91906159ce565b604051610bf19796959493929190615904565b60405180910390a26001600160a01b0384165f818152600c6020526040808220805460018201546003830154600484015460059094015494515f80516020615c2483398151915296610a9996600696929091906159c0565b610c51612b2b565b5f848152600b6020908152604090912060018082018690558482556003909101805467ffffffffffffffff60481b1916600160481b426001600160401b03160217905554610cb0916001600160a01b0390911690833590840135612b58565b5f610cbb8585612fd0565b6010545f87815260126020526040902090815560115460019091015590505f858152600b60205260409081902060040154601054601154925188935f80516020615bc483398151915293610d169389938b93899392916158cc565b60405180910390a25f858152600b602052604090206004015485905f80516020615c04833981519152906002908535610100870135610d5d60a089013560808a01356159ce565b6020890135610d7460608b013560408c01356159ce565b6040516107f59796959493929190615904565b5f60138281548110610d9b57610d9b6159f4565b905f5260205f2001549050919050565b610db3612b2b565b5f818152600b60205260409020600301805460ff19166001179055601554819003610ddd575f6015555b50565b610de8612b2b565b601480546001600160a01b0385165f818152600c6020526040808220600281018054600383018a90556004909201889055426001600160401b03908116600160801b0277ffffffffffffffff0000000000000000ffffffffffffffff199093169616959095171790935583546001810185559381527fce6d7b5282bd9a3661ae061feed1dbda4e52ab073b1f9285be6e155d9c38d4ec90930180546001600160a01b03191682179055905190915f80516020615c2483398151915291610eba91908190819088908890839081906159c0565b60405180910390a2505050565b5f805f80610ed361132b565b90505f60055f9054906101000a90046001600160a01b03166001600160a01b031663f71c69406040518163ffffffff1660e01b8152600401602060405180830381865afa158015610f26573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610f4a9190615a08565b90505f818311610f5a575f610f64565b610f648284615a1f565b90505f60025f9054906101000a90046001600160a01b03166001600160a01b031663b5b65cef6040518163ffffffff1660e01b815260040160408051808303815f875af1158015610fb7573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610fdb9190615a41565b5090505f7f000000000000000000000000000000000000000000000000000000000000000061100983613018565b101580156110175750601854155b92989197509195509350505050565b5f818152600b6020908152604080832081516101408101835281548152600182015493810193909352600281015491830191909152600381015483929190606083019060ff16600481111561107d5761107d615590565b600481111561108e5761108e615590565b815260038201546001600160401b03610100820481166020840152600160481b820481166040840152600160881b9091041660608201526004820154608082015260058201546001600160a01b031660a082015260069091015460c09091015290505f6110fd82610100015190565b90506001600160a01b0381161561112f576001600160a01b03165f908152600c60205260409020600301549392505050565b5060e0015192915050565b61114261507c565b61114c8282613042565b919050565b611159612b2b565b6001600160a01b0385165f818152600c60205260409081902060018101879055858155600381018590556002808201805477ffffffffffffffffffffffffffffffff00000000000000001916600160401b426001600160401b031690810267ffffffffffffffff60801b191691909117600160801b91909102179055600482015460059092015492515f80516020615c24833981519152936107f59389928b928a92918a906159c0565b61120b612b2b565b61121a858585858560026131a6565b60015484516020860151611238926001600160a01b03169190612b58565b845f80516020615bc48339815191525f805f805f8060405161125f969594939291906158cc565b60405180910390a2845f80516020615c0483398151915260015f875f01518861010001518960a001518a6080015161129791906159ce565b8a602001518b606001518c604001516112b091906159ce565b6040516112c39796959493929190615904565b60405180910390a26001600160a01b03831615611324576001600160a01b0383165f818152600c6020526040808220805460018201546003830154600484015460059485015495515f80516020615c24833981519152976107f597916159c0565b5050505050565b5f8054604080516308aa0f3360e31b8152905183926001600160a01b03169163455079989160048083019260209291908290030181865afa158015611372573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906113969190615a08565b90505f60015f9054906101000a90046001600160a01b03166001600160a01b031663455079986040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113e9573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061140d9190615a08565b9050611419818361596a565b9250505090565b5f8054604080516301b3d98160e11b8152905183926001600160a01b031691630367b3029160048083019260209291908290030181865afa158015611467573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061148b9190615a08565b90505f60015f9054906101000a90046001600160a01b03166001600160a01b0316630367b3026040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113e9573d5f803e3d5ffd5b6114e66150cb565b61114c82826134f4565b6114f8612b2b565b5f6115068360400151612c15565b9050826101000151836080015161151d919061596a565b5f858152600b60205260408082209283558501516001808401919091556002830184905560038301805460ff198116831782556013805470ffffffffffffffffffffffffffffffffff199092166101006001600160401b039384160267ffffffffffffffff60481b191617600160481b429390931692830217841767ffffffffffffffff60881b1916600160881b92909202919091179091556004909301859055825490810183559181527f66de8ffda797e3de9c05e8fc57b3bf0ec28a930d40b0d285d93c06501cf6a090909101859055600d546115fd90839061596a565b600d8190556003546040516340c10f1960e01b81526001600160a01b038981166004830152602482018990529293509116906340c10f19906044015f604051808303815f87803b15801561164f575f80fd5b505af1158015611661573d5f803e3d5ffd5b50506010545f888152601260205260409020908155601154600190910155506116879050565b845f80516020615bc483398151915285610100015186608001516116ab919061596a565b866040015185876010546011546040516116ca969594939291906158cc565b60405180910390a2845f80516020615c048339815191525f855f88610100015189608001515f8b604001516040516117089796959493929190615904565b60405180910390a2505050505050565b60148181548110611727575f80fd5b5f918252602090912001546001600160a01b0316905081565b611748612b2b565b5f61175b36839003830160608401615a6b565b60105483355f908152601260209081526040808320938455601154600194850155600b82528220828155600481019290925560038201805467ffffffffffffffff60481b1916905585013591015590506117bd61020083016101e084016154e4565b82355f908152600b602090815260409091206005810180546001600160a01b0319166001600160a01b0394909416939093179092556003909101805467ffffffffffffffff60881b1916600160881b426001600160401b0316021790558181015161182a91840135615a1f565b6040808301919091526101008201518251909161184a9190850135615a1f565b6118549190615a1f565b6080820152604082013561186a5761186a615942565b61189a823561188161020085016101e086016154e4565b8385604001358661020001358761022001356001612c48565b600154815160208301516118b8926001600160a01b03169190612b58565b81355f80516020615be48339815191526118da61020085016101e086016154e4565b84355f908152600b602090815260409182902060068101546002909101546010546011549451611911969593948b0135939061597d565b60405180910390a281355f80516020615c048339815191526008600c5f61194061020088016101e089016154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060030154845f01518561010001515f87602001515f6040516119879796959493929190615904565b60405180910390a26119a161020083016101e084016154e4565b6001600160a01b03165f80516020615c248339815191526004600c5f6119cf61020088016101e089016154e4565b6001600160a01b0316815260208101919091526040015f9081205490600c90611a0061020089016101e08a016154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060010154600c5f886101e0016020810190611a3a91906154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060030154600c5f896101e0016020810190611a7491906154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060040154600c5f8a6101e0016020810190611aae91906154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f20600501545f604051611ae597969594939291906159c0565b60405180910390a25050565b611af9612b2b565b5f878152600b602052604090206001018690556001600160a01b03841615611ba3575f8511611b2a57611b2a615942565b611b488785611b3e36859003850185615a6b565b8887876001612c48565b6001600160a01b0384165f818152600c6020526040808220600381810154600483015460059093015493515f80516020615c2483398151915295611b969593948a948c9493919290916159c0565b60405180910390a2611bda565b5f878152600b60205260409020858155600301805467ffffffffffffffff60481b1916600160481b426001600160401b0316021790555b600154611bf6906001600160a01b031682356020840135612b58565b601080545f8981526012602090815260408083209384556011546001909401849055600b909152908190206002810154600490910154935491518b945f80516020615bc483398151915294611c52948c948e94909392906158cc565b60405180910390a25f878152600b6020526040902060049081015488915f80516020615c04833981519152918435610100860135611c9860a088013560808901356159ce565b6020880135611caf60608a013560408b01356159ce565b604051611cc29796959493929190615904565b60405180910390a250505050505050565b611cdb612b2b565b611cea888888888888886136b1565b5f888152600b6020908152604080832089815560018082018c90556003820180546004909301879055600160481b600160c81b0319909216600160481b426001600160401b031690810267ffffffffffffffff60881b191691909117600160881b91909102179091556010546012909352922090815560115491015560015485516020870151611d84926001600160a01b03169190612b58565b5f888152600b6020526040908190206002015460105460115492518b935f80516020615bc483398151915293611dc0938c938e938992916158cc565b60405180910390a2845161010086015160208701516040518b935f80516020615c0483398151915293611dfd93600993889392915f918290615904565b60405180910390a26001600160a01b0384165f818152600c6020526040808220805460018201546003830154600484015460059485015495515f80516020615c2483398151915297610a9997916159c0565b5f611e586150cb565b611e6284826134f4565b611e748160200151825f015185613886565b9150505b92915050565b611e866138b6565b611e8f846138d8565b600854611ea6906001600160a01b031633866138f8565b5f546001600160a01b0316611eb9615114565b60025460408051630fdb11cf60e01b815281515f936001600160a01b031692630fdb11cf9260048082019391829003018187875af1158015611efd573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611f219190615a41565b509050865f5b8681101561209157811561209157611f3d615169565b888883818110611f4f57611f4f6159f4565b6020029190910135808352610140830151611f6a92506134f4565b80515f908152600b6020526040902060030154611f899060ff16613986565b1580611f99575061014081015151155b15611fa45750612089565b80515f908152600b60205260409020600501546001600160a01b03166001600160a01b03166020820181905215611fe357611fe38682602001516139bd565b600154611ffb906001600160a01b0316848684613ac0565b806060015185606001818151612011919061596a565b905250604081015160a08601805161202a90839061596a565b90525060a08101518551869061204190839061596a565b90525060e081015160c08601805161205a90839061596a565b90525060c081015160e08601805161207390839061596a565b90525060408101516120859084615a1f565b9250505b600101611f27565b5084836060015110156120c857826060015160405163de7c5c2d60e01b81526004016120bf91815260200190565b60405180910390fd5b60a083810151606080860151604080518d815260208101949094528301525f90820152608081018490527fc8f82a388a36012a2cfc586d40688baa3fe0112eff350cfd00b7e4dddaf6a5a4910160405180910390a16040516371d4eb2160e01b81526001600160a01b038516906371d4eb219061214b9086905f90600401615a86565b5f604051808303815f87803b158015612162575f80fd5b505af1158015612174573d5f803e3d5ffd5b505050506060830151604051634fa7288f60e11b815233600482015260248101919091526001600160a01b03851690639f4e511e906044015f604051808303815f87803b1580156121c3575f80fd5b505af11580156121d5573d5f803e3d5ffd5b505060085460a0860151604051632770a7eb60e21b815233600482015260248101919091526001600160a01b039091169250639dc29fac91506044015b5f604051808303815f87803b158015612229575f80fd5b505af115801561223b573d5f803e3d5ffd5b505050505050505050505050565b612251612b2b565b6001600160a01b0384165f818152600c602052604080822060018082018890558682556004820186905560028201805467ffffffffffffffff60401b1916600160401b426001600160401b031602179055600382015460059092015492515f80516020615c24833981519152946122d194929389938b9391928a926159c0565b60405180910390a250505050565b80515f03612300576040516399d8fec960e01b815260040160405180910390fd5b5f546001546005546001600160a01b03928316929182169116612321615114565b6123296151d6565b60025460408051630fdb11cf60e01b815281515f936001600160a01b031692630fdb11cf9260048082019391829003018187875af115801561236d573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906123919190615a41565b5090505f846001600160a01b031663f71c69406040518163ffffffff1660e01b8152600401602060405180830381865afa1580156123d1573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906123f59190615a08565b90506124058683838b8789613b91565b8360a001515f0361242957604051638c20998d60e01b815260040160405180910390fd5b6040516371d4eb2160e01b81526001600160a01b038816906371d4eb21906124579087905f90600401615a86565b5f604051808303815f87803b15801561246e575f80fd5b505af1158015612480573d5f803e3d5ffd5b505050505f8360200151118061249957505f8360400151115b15612509576020830151604080850151905163335525ad60e01b81526001600160a01b0388169263335525ad926124db92600401918252602082015260400190565b5f604051808303815f87803b1580156124f2575f80fd5b505af1158015612504573d5f803e3d5ffd5b505050505b61251d878785606001518660800151613c65565b60a08301511561258f5760075460a0840151604051634fa7288f60e11b81526001600160a01b039283166004820152602481019190915290881690639f4e511e906044015f604051808303815f87803b158015612578575f80fd5b505af115801561258a573d5f803e3d5ffd5b505050505b61259c87845f0151613dac565b7f7243af9a1cff94d3429b2ee00b78c1c10589259f20dc167cb67704f38f9e824e836020015184606001518560c00151865f0151876040015188608001518960a001516010546011548b60405161263c9a99989796959493929190998a5260208a019890985260408901969096526060880194909452608087019290925260a086015260c085015260e08401526101008301526101208201526101400190565b60405180910390a161265787338560c00151865f0151613ead565b5050505050505050565b5f61266a613f8d565b5f546009546001600160a01b039182169116612684615114565b5f8861268e615169565b601554156126a8576015548152600161012082015261270b565b846001600160a01b0316634d6228316040518163ffffffff1660e01b8152600401602060405180830381865afa1580156126e4573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906127089190615a08565b81525b5f885f03612718575f1998505b81511580159061272757505f83115b801561273257505f89115b1561297f578861274181615b16565b9950505f826101200151156127b757866001600160a01b0316634d6228316040518163ffffffff1660e01b8152600401602060405180830381865afa15801561278c573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906127b09190615a08565b9050612824565b825160405163040de97d60e21b815260048101919091526001600160a01b03881690631037a5f490602401602060405180830381865afa1580156127fd573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906128219190615a08565b90505b670de0b6b3a764000061283a845f01518e611e4f565b101561284e5782525f610120830152612718565b82515f908152600b60205260409020600501546001600160a01b03166001600160a01b0316602084018190521580159061289e5750816001600160a01b031683602001516001600160a01b031614155b156128b9576128b18884602001516139bd565b826020015191505b6001546128d2906001600160a01b031684868f8f613fb8565b8260600151866060018181516128e8919061596a565b905250604083015160a08701805161290190839061596a565b90525060a08301518651879061291890839061596a565b90525060e083015160c08701805161293190839061596a565b90525060c083015160e08701805161294a90839061596a565b905250608083015161295c908661596a565b945082604001518461296e9190615a1f565b9083525f6101208401529250612718565b7fc8f82a388a36012a2cfc586d40688baa3fe0112eff350cfd00b7e4dddaf6a5a48c8660a001518760600151878f6040516129dc959493929190948552602085019390935260408401919091526060830152608082015260a00190565b60405180910390a16040516371d4eb2160e01b81526001600160a01b038816906371d4eb2190612a129088905f90600401615a86565b5f604051808303815f87803b158015612a29575f80fd5b505af1158015612a3b573d5f803e3d5ffd5b50505050866001600160a01b0316639f4e511e8e87606001516040518363ffffffff1660e01b8152600401612a859291906001600160a01b03929092168252602082015260400190565b5f604051808303815f87803b158015612a9c575f80fd5b505af1158015612aae573d5f803e3d5ffd5b50505060a0909501519d9c50505050505050505050505050565b612ad0612b2b565b426018555f805460408051636d8f962d60e01b815290516001600160a01b0390921692636d8f962d9260048084019382900301818387803b158015612b13575f80fd5b505af1158015612b25573d5f803e3d5ffd5b50505050565b6004546001600160a01b03163314612b565760405163558b0d1f60e01b815260040160405180910390fd5b565b8115612bb4576040516330588f6f60e21b8152600481018390526001600160a01b0384169063c1623dbc906024015f604051808303815f87803b158015612b9d575f80fd5b505af1158015612baf573d5f803e3d5ffd5b505050505b8015612c1057604051631eaed73d60e11b8152600481018290526001600160a01b03841690633d5dae7a906024015f604051808303815f87803b158015612bf9575f80fd5b505af1158015612c0b573d5f803e3d5ffd5b505050505b505050565b5f80600f545f03612c27575081611e78565b600f54600e54612c379085615b2b565b612c419190615b42565b9392505050565b6001600160a01b0386165f908152600c602052604081206005015486516101008801516080890151929392839291612c7f9161596a565b612c89919061596a565b90505f8860a00151821115612cae5760a0890151612ca79083615a1f565b9150612cc4565b818960a00151612cbe9190615a1f565b90505f91505b81158015612cd0575080155b15612cf4576001600160a01b038a165f908152600c60205260409020869055612ea5565b8115612d9e57855f03612d0957819250612d2c565b612d14848787614183565b85612d1f8386615b2b565b612d299190615b42565b92505b5f8b8152600b602052604081206006018054859290612d4c90849061596a565b90915550612d5c9050828761596a565b6001600160a01b038b165f908152600c6020526040902055612d7e838561596a565b6001600160a01b038b165f908152600c6020526040902060050155612ea5565b8015612ea557875f03612e1d57612db58187615a1f565b6001600160a01b038b165f908152600c60209081526040808320939093558d8252600b90522060060154612de99085615a1f565b6001600160a01b038b165f908152600c60209081526040808320600501939093558d8252600b905290812060060155612ea5565b85612e288286615b2b565b612e329190615b42565b925082600b5f8d81526020019081526020015f206006015f828254612e579190615a1f565b90915550612e6790508187615a1f565b6001600160a01b038b165f908152600c6020526040902055612e898385615a1f565b6001600160a01b038b165f908152600c60205260409020600501555b6001600160a01b038a165f908152600c60209081526040808320600201805467ffffffffffffffff60401b1916600160401b426001600160401b031602179055908b0151908b0151612ef7919061596a565b90505f8a60600151821115612f1c5760608b0151612f159083615a1f565b9150612f32565b818b60600151612f2c9190615a1f565b90505f91505b81158015612f3e575080155b15612f65576001600160a01b038c165f908152600c60205260409020600101899055612fc1565b8115612f9557612f75828a61596a565b6001600160a01b038d165f908152600c6020526040902060010155612fc1565b8015612fc157612fa5818a615a1f565b6001600160a01b038d165f908152600c60205260409020600101555b50505050505050505050505050565b5f612fda82612c15565b5f848152600b60205260409020600201805490829055600d54919250908290613004908390615a1f565b61300e919061596a565b600d555092915050565b5f80613022611420565b90505f61302d61132b565b905061303a828286613886565b949350505050565b6001600160a01b0382165f908152600c6020908152604091829020825161010081018452815480825260018301549382019390935260028201546001600160401b0380821695830195909552600160401b81048516606080840191909152600160801b909104909416608080830191909152600383015460a08301908152600484015460c084015260059093015460e08301529385018390529051928401839052916130ed91615b2b565b60a083015260608101515f9061310b906001600160401b03166141ba565b905061311b8360a00151826141fa565b604084015260c080830151908401819052606084015161313b9190615b2b565b610100840181905261314d90826141fa565b60e0840181905260408401516060850151613168919061596a565b613172919061596a565b8352506020808201519083015260608101516001600160401b03908116610120840152608090910151166101409091015250565b6013546131b28161421d565b6131bc878261423e565b5f878152600b60209081526040808320815161014081018352815481526001820154938101939093526002810154918301919091526003810154606083019060ff16600481111561320f5761320f615590565b600481111561322057613220615590565b815260038201546001600160401b03610100820481166020840152600160481b820481166040840152600160881b9091041660608201526004820154608082015260058201546001600160a01b0390811660a083015260069092015460c090910152909150861615613353576001816060015160048111156132a4576132a4615590565b036133075760095460405163f476125960e01b8152600481018a90526001600160a01b039091169063f4761259906024015f604051808303815f87803b1580156132ec575f80fd5b505af11580156132fe573d5f803e3d5ffd5b50505050613337565b60048160600151600481111561331f5761331f615590565b14801561332d575087601554145b15613337575f6015555b61334e8888606001518960a001518a8a8a8a6136b1565b6133fe565b60018160600151600481111561336b5761336b615590565b036133ce57600954604051634cc8221560e01b8152600481018a90526001600160a01b0390911690634cc82215906024015f604051808303815f87803b1580156133b3575f80fd5b505af11580156133c5573d5f803e3d5ffd5b505050506133fe565b6004816060015160048111156133e6576133e6615590565b1480156133f4575087601554145b156133fe575f6015555b5f8160400151600d546134119190615a1f565b600d8190555f8a8152600b602052604081208181556001808201839055600282018390556003820180546001600160c81b03191680825560048085018690556005850180546001600160a01b031916905560069094019490945593945087939291849081111561348357613483615590565b02179055505f89815260126020526040808220828155600101919091556003549051630852cd8d60e31b8152600481018b90526001600160a01b03909116906342966c68906024015f604051808303815f87803b1580156134e2575f80fd5b505af1158015612fc1573d5f803e3d5ffd5b5f828152600b60205260409020600501546001600160a01b031680156135325761351c61507c565b6135268282613042565b612b2584838584614311565b5f838152600b6020908152604080832060020154601290925290912060010154601154670de0b6b3a76400009161356891615a1f565b6135729083615b2b565b61357c9190615b42565b6040808501919091525f85815260126020522054601054670de0b6b3a7640000916135a691615a1f565b6135b09083615b2b565b6135ba9190615b42565b60608401525f848152600b602081815260408320805460a08801908152938890529190526004015460c0850181905290516135f59190615b2b565b60e08401525f848152600b602052604081206003015461362490600160481b90046001600160401b03166141ba565b90506136348460e00151826141fa565b60808501819052604085015160a086015161364f919061596a565b613659919061596a565b845260608401515f868152600b602052604090206001015461367b919061596a565b6020858101919091525f958652600b90525050604090922060030154600160881b90046001600160401b03166101209091015250565b5f878152600b60209081526040808320815161014081018352815481526001820154938101939093526002810154918301919091526003810154606083019060ff16600481111561370457613704615590565b600481111561371557613715615590565b815260038201546001600160401b0361010080830482166020850152600160481b830482166040850152600160881b9092041660608301526004830154608083015260058301546001600160a01b031660a083015260069092015460c0909101528651908701519192505f9161378b9089615a1f565b6137959190615a1f565b90505f8660200151896137a89190615a1f565b6101208401516001600160a01b0388165f908152600c60205260408120600501805493945091926137da908490615a1f565b909155506137ea90508285615a1f565b6001600160a01b0387165f908152600c602052604090205561380c8186615a1f565b6001600160a01b03969096165f908152600c6020908152604080832060018101999099556002909801805467ffffffffffffffff60401b1916600160401b426001600160401b0316021790559a8152600b909a525050509186206005810180546001600160a01b0319169055600601959095555050505050565b5f82156138ac575f836138998487615b2b565b6138a39190615b42565b9150612c419050565b505f199392505050565b6018545f03612b565760405163f8b7c7bf60e01b815260040160405180910390fd5b805f03610ddd57604051631f2a200560e01b815260040160405180910390fd5b6040516370a0823160e01b81526001600160a01b0383811660048301525f91908516906370a0823190602401602060405180830381865afa15801561393f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906139639190615a08565b905081811015612b25576040516307a5137f60e11b815260040160405180910390fd5b5f600182600481111561399b5761399b615590565b1480611e78575060048260048111156139b6576139b6615590565b1492915050565b6139c561507c565b6139cf8282613042565b80516001600160a01b0383165f908152600c60205260409020908155600201805467ffffffffffffffff60401b1916600160401b426001600160401b031602179055613a19615114565b60a082015160e082015260808201518251613a349190615b2b565b60c08083019190915260e08301516101208301526101008301516101608301528201518251613a639190615b2b565b6101408201526040516371d4eb2160e01b81526001600160a01b038516906371d4eb2190613a979084908790600401615a86565b5f604051808303815f87803b158015613aae575f80fd5b505af1158015612657573d5f803e3d5ffd5b613ad2838261014001515f015161458e565b604082015281613af1662386f26fc10000670de0b6b3a764000061596a565b8260400151613b009190615b2b565b613b0a9190615b42565b60608201819052610140820151602001511015613b6e57610140810151602001516060820152613b49662386f26fc10000670de0b6b3a764000061596a565b8282610140015160200151613b5e9190615b2b565b613b689190615b42565b60408201525b60208101516001600160a01b03161515613b898583836145a3565b505050505050565b835f5b8451811015612657575f858281518110613bb057613bb06159f4565b6020908102919091018101515f818152600b909252604090912060030154909150613bdd9060ff16613986565b613be75750613c5d565b5f613bf2828a611e4f565b90507f0000000000000000000000000000000000000000000000000000000000000000811015613c5a57613c246151d6565b613c2c6150cb565b613c3a8c85888e85876149e8565b6020820151613c499087615a1f565b9550613c5781838a8a614dfa565b50505b50505b600101613b94565b8115612b25576016545f90613c82670de0b6b3a764000084615b2b565b613c8c919061596a565b90505f601754670de0b6b3a764000085613ca69190615b2b565b613cb0919061596a565b90505f600d5483613cc19190615b42565b90505f600d5483613cd29190615b42565b9050600d5482613ce29190615b2b565b613cec9085615a1f565b601655600d54613cfc9082615b2b565b613d069084615a1f565b601755601054613d1790839061596a565b601055601154613d2890829061596a565b60115560405162afc6b160e31b8152600481018790526001600160a01b0388169063057e3588906024015f604051808303815f87803b158015613d69575f80fd5b505af1158015613d7b573d5f803e3d5ffd5b505060405163c2283f1f60e01b8152600481018890526001600160a01b038b16925063c2283f1f9150602401612212565b600d54600e819055505f826001600160a01b0316630367b3026040518163ffffffff1660e01b8152600401602060405180830381865afa158015613df2573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190613e169190615a08565b90505f60015f9054906101000a90046001600160a01b03166001600160a01b0316630367b3026040518163ffffffff1660e01b8152600401602060405180830381865afa158015613e69573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190613e8d9190615a08565b905080613e9a8484615a1f565b613ea4919061596a565b600f5550505050565b8115613f52576006546040516323b872dd60e01b81526001600160a01b0391821660048201528482166024820152604481018490527f0000000000000000000000000000000000000000000000000000000000000000909116906323b872dd906064016020604051808303815f875af1158015613f2c573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190613f509190615b61565b505b8015612b2557604051634fa7288f60e11b81526001600160a01b03848116600483015260248201839052851690639f4e511e90604401613a97565b600a546001600160a01b03163314612b5657604051630126422b60e21b815260040160405180910390fd5b613fca845f01518561014001516134f4565b613fdc838561014001515f015161458e565b604085018190525f908390613ffa90670de0b6b3a764000090615b2b565b6140049190615b42565b9050670de0b6b3a76400006140198383615b2b565b6140239190615b42565b608086018190526140349082615a1f565b606086015260208501516001600160a01b031615155f6140558888846145a3565b9050686c6b935b8bbd4000008110156126575786610120015161416e5786515f908152600b60205260409020600301805460ff1916600417905581156140fb57600954875160405163f476125960e01b81526001600160a01b039092169163f4761259916140c99160040190815260200190565b5f604051808303815f87803b1580156140e0575f80fd5b505af11580156140f2573d5f803e3d5ffd5b5050505061415d565b6009548751604051634cc8221560e01b81526001600160a01b0390921691634cc822159161412f9160040190815260200190565b5f604051808303815f87803b158015614146575f80fd5b505af1158015614158573d5f803e3d5ffd5b505050505b80156141695786516015555b612657565b805f03612657575f6015555050505050505050565b81614192633b9aca0085615b2b565b10801561419c5750805b15612c105760405163086b275d60e31b815260040160405180910390fd5b5f6018545f036141ce57611e788242615a1f565b5f6018541180156141e0575060185482105b156141f35781601854611e789190615a1f565b505f919050565b5f670de0b6b3a76400006301e133806142138486615b2b565b612c379190615b42565b80600103610ddd57604051633307cee360e01b815260040160405180910390fd5b5f828152600b602052604081206003015461010090046001600160401b031690614269600184615a1f565b90505f6013828154811061427f5761427f6159f4565b905f5260205f2001549050806013846001600160401b0316815481106142a7576142a76159f4565b5f91825260208083209190910192909255828152600b90915260409020600301805468ffffffffffffffff0019166101006001600160401b0386160217905560138054806142f7576142f7615b7a565b600190038181905f5260205f20015f905590555050505050565b5f848152600b60209081526040808320815161014081018352815481526001820154938101939093526002810154918301919091526003810154606083019060ff16600481111561436457614364615590565b600481111561437557614375615590565b815260038201546001600160401b0361010082048116602080850191909152600160481b83048216604080860191909152600160881b9093049091166060840152600484015460808401526005808501546001600160a01b0390811660a086015260069095015460c0909401939093526101208501519389165f908152600c825282812090930154858301518b85526012909252919092206001015460115494955092939092670de0b6b3a76400009161442f9190615a1f565b6144399083615b2b565b6144439190615b42565b6040808801919091525f89815260126020522054601054670de0b6b3a76400009161446d91615a1f565b6144779083615b2b565b6144819190615b42565b60608701528115614505578183866060015161449d9190615b2b565b6144a79190615b42565b60a0870181905260808601516144bc91615b2b565b60e0870152604085015182906144d3908590615b2b565b6144dd9190615b42565b608087015260e085015182906144f4908590615b2b565b6144fe9190615b42565b6101008701525b60808086015160c088015261010087015190870151604088015160a089015161452e919061596a565b614538919061596a565b614542919061596a565b865260608601516020850151614558919061596a565b602087015261014085015160c085015161457b91906001600160401b0316614f28565b6101209096019590955250505050505050565b5f81831061459c5781612c41565b5090919050565b5f8083604001518461014001515f01516145bd9190615a1f565b90505f8460600151856101400151602001516145d99190615a1f565b6101408601516040015160a087015290508315614756576146038560200151866101600151613042565b5f8560400151866101400151604001518761016001515f0151614626919061596a565b6146309190615a1f565b61016087015160a081015160c0890152608001519091506146519082615b2b565b60e087015261465e615114565b60408088015160a0830152606080890151818401526101408901805190920151835290510151602082015261016080880180516101000151918301919091525160c001516146ac9083615b2b565b6101408201525f546020880151604051631bb3f79760e31b81526001600160a01b039092169163dd9fbcb8916146e791859190600401615a86565b5f604051808303815f87803b1580156146fe575f80fd5b505af1158015614710573d5f803e3d5ffd5b505088515f908152600b6020908152604082206001018790558a51818c01516101608d015192830151925161474f96509194509286928a929091612c48565b50506147c8565b61014085015160e081015160c08088019190915201516147769083615b2b565b60e086015284515f908152600b602052604080822084905586518252808220600101839055865182529020600301805467ffffffffffffffff60481b1916600160481b426001600160401b0316021790555b84516147d49082612fd0565b61010086015261014085015160408101516060909101516147f6918891612b58565b84516010545f9182526012602052604090912090815560115460019091015583156148725784516020808701515f838152600b9092526040918290206006015461010089015160105460115494515f80516020615be4833981519152956148659594938993909290919061597d565b60405180910390a26148b7565b845f01515f80516020615bc4833981519152838388610100015189610140015160c001516010546011546040516148ae969594939291906158cc565b60405180910390a25b845f01515f80516020615c04833981519152600687610140015160c00151886101400151604001515f8a604001516148ee90615b8e565b8b6101400151606001518c6060015161490690615b8e565b6040516149199796959493929190615904565b60405180910390a283156149a557602080860180516001600160a01b039081165f818152600c909452604080852054845184168652818620600101546101608c0151608081015160c0909101519651909516875282872060050154925193965f80516020615c248339815191529661499c966006969495939490939192906159c0565b60405180910390a25b845160808601516040519081527fc7e8309b9b14e7a8561ed352b9fd8733de32417fb7b6a69f5671f79e7bb29ddd9060200160405180910390a250949350505050565b6003546040516331a9108f60e11b8152600481018790525f916001600160a01b031690636352211e90602401602060405180830381865afa158015614a2f573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190614a539190615ba8565b9050614a5f86846134f4565b5f868152600b60205260409020600501546001600160a01b0316801515614a8461507c565b8115614a9457614a948382613042565b614aa78a87604001518860600151612b58565b614ab48660200151614f37565b80865260208701515f91614ac791615a1f565b9050614ad8875f0151828b8b614f54565b60a08b015260808a0152606089015260408801526020870152614af9615114565b602080890151606080840191909152895160a084015289015182820152604089015182528301518351614b33918d918491899160036131a6565b8315614c3f57608083015160408901518951614b4f9190615a1f565b614b599190615b2b565b8360a00151614b68919061596a565b60e088015260808301518351614b7e9190615b2b565b61010088015260e083015161012082015260c083015160408901518951614ba59190615a1f565b614baf9190615b2b565b836101000151614bbf919061596a565b61016082015260c08301518351614bd69190615b2b565b6101408201525f54604051631bb3f79760e31b81526001600160a01b039091169063dd9fbcb890614c0d9084908990600401615a86565b5f604051808303815f87803b158015614c24575f80fd5b505af1158015614c36573d5f803e3d5ffd5b50505050614c4a565b60e080890151908801525b60a087015115614cbb5760075460a0880151604051633f10abab60e01b81526001600160a01b0389811660048301526024820192909252911690633f10abab906044015f604051808303815f87803b158015614ca4575f80fd5b505af1158015614cb6573d5f803e3d5ffd5b505050505b60048054604051638fef27ab60e01b81529182018d90526001600160a01b031690638fef27ab906024015f604051808303815f87803b158015614cfc575f80fd5b505af1158015614d0e573d5f803e3d5ffd5b505050508a5f80516020615bc48339815191525f805f805f80604051614d39969594939291906158cc565b60405180910390a28a5f80516020615c0483398151915260055f8b604001515f8d5f0151614d6690615b8e565b8e606001518f60200151614d7990615b8e565b604051614d8c9796959493929190615904565b60405180910390a2831561223b576001600160a01b0385165f818152600c602052604080822080546001820154608089015160c08a015160059485015495515f80516020615c2483398151915297614de497916159c0565b60405180910390a2505050505050505050505050565b825182518390614e0b90839061596a565b90525060c08201805166853a0d2313c0009190614e2990839061596a565b905250835160a082018051614e3f90839061596a565b9052506020840151606082018051614e5890839061596a565b905250604084015181518290614e6f90839061596a565b90525060e0808401519082018051614e8890839061596a565b90525061010083015160c082018051614ea290839061596a565b9052506020808401519083018051614ebb90839061596a565b9052506040808401519083018051614ed490839061596a565b9052506060808401519083018051614eed90839061596a565b9052506080808401519083018051614f0690839061596a565b90525060a0808401519083018051614f1f90839061596a565b90525050505050565b5f8183101561459c5781612c41565b5f611e78614f4660c884615b42565b671bc16d674ec8000061458e565b5f80808080808715614fb557614f6a8a8961458e565b955089614f77878b615b2b565b614f819190615b42565b9050614faf81877f00000000000000000000000000000000000000000000000000000000000000008a615023565b90955091505b614fbf868b615a1f565b93508315615017575f614fd2828b615a1f565b905080156150155761500f614fe7848361596a565b867f00000000000000000000000000000000000000000000000000000000000000008b615023565b90945092505b505b50945094509450945094565b5f80808361503986670de0b6b3a764000061596a565b6150439088615b2b565b61504d9190615b42565b90508087111561506b579150816150648188615a1f565b9150615072565b8692505f91505b5094509492505050565b6040518061016001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b6040518061014001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b6040518061018001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b6040518061018001604052805f81526020015f6001600160a01b031681526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f151581526020016151c46150cb565b81526020016151d161507c565b905290565b6040518061012001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b5f610180828403121561522a575f80fd5b50919050565b5f805f805f6102008688031215615245575f80fd5b8535945060208601359350604086013592506060860135915061526b8760808801615219565b90509295509295909350565b6001600160a01b0381168114610ddd575f80fd5b634e487b7160e01b5f52604160045260245ffd5b60405161018081016001600160401b03811182821017156152c2576152c261528b565b60405290565b604051601f8201601f191681016001600160401b03811182821017156152f0576152f061528b565b604052919050565b5f6101808284031215615309575f80fd5b61531161529f565b9050813581526020820135602082015260408201356040820152606082013560608201526080820135608082015260a082013560a082015260c082013560c082015260e082013560e082015261010080830135818301525061012080830135818301525061014080830135818301525061016080830135818301525092915050565b5f805f805f8061022087890312156153a9575f80fd5b86356153b481615277565b9550602087013594506153ca88604089016152f8565b93506101c08701356153db81615277565b959894975092956101e081013594610200909101359350915050565b5f805f805f805f610240888a03121561540e575f80fd5b87359650602088013595506040880135945061542d8960608a016152f8565b93506101e088013561543e81615277565b9699959850939692959461020084013594506102209093013592915050565b5f805f806101e08587031215615471575f80fd5b8435935060208501359250604085013591506154908660608701615219565b905092959194509250565b5f602082840312156154ab575f80fd5b5035919050565b5f805f606084860312156154c4575f80fd5b83356154cf81615277565b95602085013595506040909401359392505050565b5f602082840312156154f4575f80fd5b8135612c4181615277565b5f805f805f60a08688031215615513575f80fd5b853561551e81615277565b97602087013597506040870135966060810135965060800135945092505050565b5f805f805f6102008688031215615554575f80fd5b8535945061556587602088016152f8565b93506101a086013561557681615277565b949793965093946101c081013594506101e0013592915050565b634e487b7160e01b5f52602160045260245ffd5b600581106155b4576155b4615590565b9052565b8a8152602081018a90526040810189905261014081016155db606083018a6155a4565b6001600160401b03978816608083015295871660a08201529390951660c084015260e08301919091526001600160a01b03166101008201526101200191909152949350505050565b5f805f806101e08587031215615637575f80fd5b843561564281615277565b93506020850135925061565886604087016152f8565b939692955092936101c00135925050565b5f610240828403121561522a575f80fd5b5f805f805f805f610240888a031215615691575f80fd5b87359650602088013595506040880135945060608801356156b181615277565b93506080880135925060a088013591506156ce8960c08a01615219565b905092959891949750929550565b5f805f805f805f80610260898b0312156156f4575f80fd5b8835975060208901359650604089013595506157138a60608b016152f8565b94506101e089013561572481615277565b979a969950949793969561020085013595506102208501359461024001359350915050565b5f806040838503121561575a575f80fd5b50508035926020909101359150565b5f805f806060858703121561577c575f80fd5b8435935060208501356001600160401b0380821115615799575f80fd5b818701915087601f8301126157ac575f80fd5b8135818111156157ba575f80fd5b8860208260051b85010111156157ce575f80fd5b95986020929092019750949560400135945092505050565b5f805f80608085870312156157f9575f80fd5b843561580481615277565b966020860135965060408601359560600135945092505050565b60208101611e7882846155a4565b5f602080838503121561583d575f80fd5b82356001600160401b0380821115615853575f80fd5b818501915085601f830112615866575f80fd5b8135818111156158785761587861528b565b8060051b91506158898483016152c8565b81815291830184019184810190888411156158a2575f80fd5b938501935b838510156158c0578435825293850193908501906158a7565b98975050505050505050565b958652602086019490945260408501929092526060840152608083015260a082015260c00190565b600a81106155b4576155b4615590565b60e08101615912828a6158f4565b8760208301528660408301528560608301528460808301528360a08301528260c083015298975050505050505050565b634e487b7160e01b5f52600160045260245ffd5b634e487b7160e01b5f52601160045260245ffd5b80820180821115611e7857611e78615956565b6001600160a01b03969096168652602086019490945260408501929092526060840152608083015260a082015260c00190565b600781106155b4576155b4615590565b60e08101615912828a6159b0565b8181035f8312801583831316838312821617156159ed576159ed615956565b5092915050565b634e487b7160e01b5f52603260045260245ffd5b5f60208284031215615a18575f80fd5b5051919050565b81810381811115611e7857611e78615956565b8051801515811461114c575f80fd5b5f8060408385031215615a52575f80fd5b82519150615a6260208401615a32565b90509250929050565b5f6101808284031215615a7c575f80fd5b612c4183836152f8565b825181526020808401519082015260408084015190820152606080840151908201526080808401519082015260a0808401519082015260c0808401519082015260e0808401519082015261010080840151908201526101208084015190820152610140808401519082015261016080840151908201526001600160a01b0382166101808201526101a08101612c41565b5f81615b2457615b24615956565b505f190190565b8082028115828204841417611e7857611e78615956565b5f82615b5c57634e487b7160e01b5f52601260045260245ffd5b500490565b5f60208284031215615b71575f80fd5b612c4182615a32565b634e487b7160e01b5f52603160045260245ffd5b5f600160ff1b8201615ba257615ba2615956565b505f0390565b5f60208284031215615bb8575f80fd5b8151612c418161527756fe0fba2673863b12c7b8463f3fa2f9b0cb1d534c573cdec5b5d895ee00d6ce6f5e6464838e073667756f10746b26734b60870fdcad31d7861c6e5603430bccac61962110f281c1213763cd97a546b337b3cbfd25a31ea9723e9d8b7376ba45da1aecf6daab6f1facdfdd8dfe32b525744d8a7a940824dd52e2b53c24028ee5faa0a2646970667358221220603af07787131add9b356ca9d57147b80fa987abfaa1185b094ba16f323e41df64736f6c6343000818003300000000000000000000000036b9adccf2a08839ddc342d7a9d259b984fbd855

Deployed Bytecode

0x608060405234801561000f575f80fd5b5060043610610229575f3560e01c806377553ad41161012a578063b3e16c30116100b4578063ca0ca8d211610079578063ca0ca8d214610662578063e47bfaf114610675578063ef49a6b4146106a7578063f8a239e8146106ba578063fc0e74d1146106cd575f80fd5b8063b3e16c3014610603578063b5a672c214610616578063bf49e64914610629578063c067e95c1461063c578063c2020f041461064f575f80fd5b8063a411219c116100fa578063a411219c146104ab578063aad3f4041461053f578063ae918754146105ca578063b0141775146105dd578063b3d346b9146105f0575f80fd5b806377553ad414610476578063795d26c3146104895780637f7dde4a14610491578063887105d3146104a3575f80fd5b8063499b069f116101b65780635d6485881161017b5780635d6485881461036b5780635ef3b8bf146103a6578063613cacae146103b95780636daed00014610450578063735ab2a414610463575f80fd5b8063499b069f146102fb5780634aff96e11461030e5780634ea15f37146103165780635733d58f1461033b5780635856908114610362575f80fd5b80631ca2d7d9116101fc5780631ca2d7d91461029c5780631cf74075146102af5780631d7be800146102c25780631ef11b62146102d557806338116fa3146102e8575f80fd5b8063015402871461022d578063048c661d14610249578063059e0113146102745780630f83069314610287575b5f80fd5b61023660155481565b6040519081526020015b60405180910390f35b60055461025c906001600160a01b031681565b6040516001600160a01b039091168152602001610240565b60035461025c906001600160a01b031681565b61029a610295366004615230565b6106d5565b005b61029a6102aa366004615393565b610804565b61029a6102bd3660046153f7565b610aab565b61029a6102d036600461545d565b610c49565b6102366102e336600461549b565b610d87565b61029a6102f636600461549b565b610dab565b61029a6103093660046154b2565b610de0565b601354610236565b61031e610ec7565b604080519384526020840192909252151590820152606001610240565b6102367f00000000000000000000000000000000000000000000000014d1120d7b16000081565b61023660185481565b61039161037936600461549b565b60126020525f90815260409020805460019091015482565b60408051928352602083019190915201610240565b6102366103b436600461549b565b611026565b6103cc6103c73660046154e4565b61113a565b60405161024091905f61016082019050825182526020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015160c083015260e083015160e083015261010080840151818401525061012080840151818401525061014080840151818401525092915050565b61029a61045e3660046154ff565b611151565b61029a61047136600461553f565b611203565b60045461025c906001600160a01b031681565b61023661132b565b5f5461025c906001600160a01b031681565b610236611420565b6105296104b936600461549b565b600b6020525f9081526040902080546001820154600283015460038401546004850154600586015460069096015494959394929360ff8316936001600160401b036101008504811694600160481b8104821694600160881b9091049091169290916001600160a01b03909116908a565b6040516102409a999897969594939291906155b8565b61055261054d36600461549b565b6114de565b60405161024091905f61014082019050825182526020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015160c083015260e083015160e083015261010080840151818401525061012080840151818401525092915050565b60095461025c906001600160a01b031681565b61029a6105eb366004615623565b6114f0565b61025c6105fe36600461549b565b611718565b61029a610611366004615669565b611740565b61029a61062436600461567a565b611af1565b61029a6106373660046156dc565b611cd3565b61023661064a366004615749565b611e4f565b61029a61065d366004615769565b611e7e565b61029a6106703660046157e6565b612249565b61069a61068336600461549b565b5f908152600b602052604090206003015460ff1690565b604051610240919061581e565b61029a6106b536600461582c565b6122df565b6102366106c83660046154ff565b612661565b61029a612ac8565b6106dd612b2b565b5f858152600b6020908152604090912060018082018790558582556004820185905560039091018054600160481b600160c81b031916600160481b426001600160401b031690810267ffffffffffffffff60881b191691909117600160881b919091021790555461075f916001600160a01b0390911690833590840135612b58565b6010545f8681526012602052604090209081556011546001909101555f858152600b60205260409081902060020154601054601154925188935f80516020615bc4833981519152936107b79389938b938a92916158cc565b60405180910390a2845f80516020615c04833981519152600384845f01358561010001355f87602001355f6040516107f59796959493929190615904565b60405180910390a25050505050565b61080c612b2b565b5f61081a8560400151612c15565b6040868101515f898152600b60209081528382206001808201949094556002810186905560038101805460ff19811686178255601354600590930180546001600160a01b038e166001600160a01b0319909116179055426001600160401b03908116600160881b0267ffffffffffffffff60881b1991909416610100021678ffffffffffffffff0000000000000000ffffffffffffffffff1990911617919091178417905560105460129091529290209182556011549101559050601380546001810182555f919091527f66de8ffda797e3de9c05e8fc57b3bf0ec28a930d40b0d285d93c06501cf6a09001869055608085015161091a5761091a615942565b61092e868587886080015187876001612c48565b5f81600d5461093d919061596a565b600d8190556003546040516340c10f1960e01b81526001600160a01b038b81166004830152602482018b90529293509116906340c10f19906044015f604051808303815f87803b15801561098f575f80fd5b505af11580156109a1573d5f803e3d5ffd5b50505050865f80516020615be483398151915286600b5f8b81526020019081526020015f20600601548960400151866010546011546040516109e89695949392919061597d565b60405180910390a26001600160a01b0385165f908152600c60205260408082206003015461010089015160808a01518a84015193518c955f80516020615c0483398151915295610a4395600795909492939092918491615904565b60405180910390a26001600160a01b0385165f818152600c602052604080822080546001820154600383015460048481015460059095015495515f80516020615c2483398151915297610a9997929692916159c0565b60405180910390a25050505050505050565b610ab3612b2b565b5f878152600b6020908152604080832060019081018a9055601054601290935292209081556011549101555f610ae98888612fd0565b90505f8611610afa57610afa615942565b610b0a8885878987876001612c48565b60015485516020870151610b28926001600160a01b03169190612b58565b875f80516020615be483398151915285600b5f8c81526020019081526020015f20600601548a85601054601154604051610b679695949392919061597d565b60405180910390a2875f80516020615c048339815191526002600c5f886001600160a01b03166001600160a01b031681526020019081526020015f2060030154885f01518961010001518a60a001518b60800151610bc591906159ce565b8b602001518c606001518d60400151610bde91906159ce565b604051610bf19796959493929190615904565b60405180910390a26001600160a01b0384165f818152600c6020526040808220805460018201546003830154600484015460059094015494515f80516020615c2483398151915296610a9996600696929091906159c0565b610c51612b2b565b5f848152600b6020908152604090912060018082018690558482556003909101805467ffffffffffffffff60481b1916600160481b426001600160401b03160217905554610cb0916001600160a01b0390911690833590840135612b58565b5f610cbb8585612fd0565b6010545f87815260126020526040902090815560115460019091015590505f858152600b60205260409081902060040154601054601154925188935f80516020615bc483398151915293610d169389938b93899392916158cc565b60405180910390a25f858152600b602052604090206004015485905f80516020615c04833981519152906002908535610100870135610d5d60a089013560808a01356159ce565b6020890135610d7460608b013560408c01356159ce565b6040516107f59796959493929190615904565b5f60138281548110610d9b57610d9b6159f4565b905f5260205f2001549050919050565b610db3612b2b565b5f818152600b60205260409020600301805460ff19166001179055601554819003610ddd575f6015555b50565b610de8612b2b565b601480546001600160a01b0385165f818152600c6020526040808220600281018054600383018a90556004909201889055426001600160401b03908116600160801b0277ffffffffffffffff0000000000000000ffffffffffffffff199093169616959095171790935583546001810185559381527fce6d7b5282bd9a3661ae061feed1dbda4e52ab073b1f9285be6e155d9c38d4ec90930180546001600160a01b03191682179055905190915f80516020615c2483398151915291610eba91908190819088908890839081906159c0565b60405180910390a2505050565b5f805f80610ed361132b565b90505f60055f9054906101000a90046001600160a01b03166001600160a01b031663f71c69406040518163ffffffff1660e01b8152600401602060405180830381865afa158015610f26573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610f4a9190615a08565b90505f818311610f5a575f610f64565b610f648284615a1f565b90505f60025f9054906101000a90046001600160a01b03166001600160a01b031663b5b65cef6040518163ffffffff1660e01b815260040160408051808303815f875af1158015610fb7573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610fdb9190615a41565b5090505f7f0000000000000000000000000000000000000000000000000f43fc2c04ee000061100983613018565b101580156110175750601854155b92989197509195509350505050565b5f818152600b6020908152604080832081516101408101835281548152600182015493810193909352600281015491830191909152600381015483929190606083019060ff16600481111561107d5761107d615590565b600481111561108e5761108e615590565b815260038201546001600160401b03610100820481166020840152600160481b820481166040840152600160881b9091041660608201526004820154608082015260058201546001600160a01b031660a082015260069091015460c09091015290505f6110fd82610100015190565b90506001600160a01b0381161561112f576001600160a01b03165f908152600c60205260409020600301549392505050565b5060e0015192915050565b61114261507c565b61114c8282613042565b919050565b611159612b2b565b6001600160a01b0385165f818152600c60205260409081902060018101879055858155600381018590556002808201805477ffffffffffffffffffffffffffffffff00000000000000001916600160401b426001600160401b031690810267ffffffffffffffff60801b191691909117600160801b91909102179055600482015460059092015492515f80516020615c24833981519152936107f59389928b928a92918a906159c0565b61120b612b2b565b61121a858585858560026131a6565b60015484516020860151611238926001600160a01b03169190612b58565b845f80516020615bc48339815191525f805f805f8060405161125f969594939291906158cc565b60405180910390a2845f80516020615c0483398151915260015f875f01518861010001518960a001518a6080015161129791906159ce565b8a602001518b606001518c604001516112b091906159ce565b6040516112c39796959493929190615904565b60405180910390a26001600160a01b03831615611324576001600160a01b0383165f818152600c6020526040808220805460018201546003830154600484015460059485015495515f80516020615c24833981519152976107f597916159c0565b5050505050565b5f8054604080516308aa0f3360e31b8152905183926001600160a01b03169163455079989160048083019260209291908290030181865afa158015611372573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906113969190615a08565b90505f60015f9054906101000a90046001600160a01b03166001600160a01b031663455079986040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113e9573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061140d9190615a08565b9050611419818361596a565b9250505090565b5f8054604080516301b3d98160e11b8152905183926001600160a01b031691630367b3029160048083019260209291908290030181865afa158015611467573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061148b9190615a08565b90505f60015f9054906101000a90046001600160a01b03166001600160a01b0316630367b3026040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113e9573d5f803e3d5ffd5b6114e66150cb565b61114c82826134f4565b6114f8612b2b565b5f6115068360400151612c15565b9050826101000151836080015161151d919061596a565b5f858152600b60205260408082209283558501516001808401919091556002830184905560038301805460ff198116831782556013805470ffffffffffffffffffffffffffffffffff199092166101006001600160401b039384160267ffffffffffffffff60481b191617600160481b429390931692830217841767ffffffffffffffff60881b1916600160881b92909202919091179091556004909301859055825490810183559181527f66de8ffda797e3de9c05e8fc57b3bf0ec28a930d40b0d285d93c06501cf6a090909101859055600d546115fd90839061596a565b600d8190556003546040516340c10f1960e01b81526001600160a01b038981166004830152602482018990529293509116906340c10f19906044015f604051808303815f87803b15801561164f575f80fd5b505af1158015611661573d5f803e3d5ffd5b50506010545f888152601260205260409020908155601154600190910155506116879050565b845f80516020615bc483398151915285610100015186608001516116ab919061596a565b866040015185876010546011546040516116ca969594939291906158cc565b60405180910390a2845f80516020615c048339815191525f855f88610100015189608001515f8b604001516040516117089796959493929190615904565b60405180910390a2505050505050565b60148181548110611727575f80fd5b5f918252602090912001546001600160a01b0316905081565b611748612b2b565b5f61175b36839003830160608401615a6b565b60105483355f908152601260209081526040808320938455601154600194850155600b82528220828155600481019290925560038201805467ffffffffffffffff60481b1916905585013591015590506117bd61020083016101e084016154e4565b82355f908152600b602090815260409091206005810180546001600160a01b0319166001600160a01b0394909416939093179092556003909101805467ffffffffffffffff60881b1916600160881b426001600160401b0316021790558181015161182a91840135615a1f565b6040808301919091526101008201518251909161184a9190850135615a1f565b6118549190615a1f565b6080820152604082013561186a5761186a615942565b61189a823561188161020085016101e086016154e4565b8385604001358661020001358761022001356001612c48565b600154815160208301516118b8926001600160a01b03169190612b58565b81355f80516020615be48339815191526118da61020085016101e086016154e4565b84355f908152600b602090815260409182902060068101546002909101546010546011549451611911969593948b0135939061597d565b60405180910390a281355f80516020615c048339815191526008600c5f61194061020088016101e089016154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060030154845f01518561010001515f87602001515f6040516119879796959493929190615904565b60405180910390a26119a161020083016101e084016154e4565b6001600160a01b03165f80516020615c248339815191526004600c5f6119cf61020088016101e089016154e4565b6001600160a01b0316815260208101919091526040015f9081205490600c90611a0061020089016101e08a016154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060010154600c5f886101e0016020810190611a3a91906154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060030154600c5f896101e0016020810190611a7491906154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f2060040154600c5f8a6101e0016020810190611aae91906154e4565b6001600160a01b03166001600160a01b031681526020019081526020015f20600501545f604051611ae597969594939291906159c0565b60405180910390a25050565b611af9612b2b565b5f878152600b602052604090206001018690556001600160a01b03841615611ba3575f8511611b2a57611b2a615942565b611b488785611b3e36859003850185615a6b565b8887876001612c48565b6001600160a01b0384165f818152600c6020526040808220600381810154600483015460059093015493515f80516020615c2483398151915295611b969593948a948c9493919290916159c0565b60405180910390a2611bda565b5f878152600b60205260409020858155600301805467ffffffffffffffff60481b1916600160481b426001600160401b0316021790555b600154611bf6906001600160a01b031682356020840135612b58565b601080545f8981526012602090815260408083209384556011546001909401849055600b909152908190206002810154600490910154935491518b945f80516020615bc483398151915294611c52948c948e94909392906158cc565b60405180910390a25f878152600b6020526040902060049081015488915f80516020615c04833981519152918435610100860135611c9860a088013560808901356159ce565b6020880135611caf60608a013560408b01356159ce565b604051611cc29796959493929190615904565b60405180910390a250505050505050565b611cdb612b2b565b611cea888888888888886136b1565b5f888152600b6020908152604080832089815560018082018c90556003820180546004909301879055600160481b600160c81b0319909216600160481b426001600160401b031690810267ffffffffffffffff60881b191691909117600160881b91909102179091556010546012909352922090815560115491015560015485516020870151611d84926001600160a01b03169190612b58565b5f888152600b6020526040908190206002015460105460115492518b935f80516020615bc483398151915293611dc0938c938e938992916158cc565b60405180910390a2845161010086015160208701516040518b935f80516020615c0483398151915293611dfd93600993889392915f918290615904565b60405180910390a26001600160a01b0384165f818152600c6020526040808220805460018201546003830154600484015460059485015495515f80516020615c2483398151915297610a9997916159c0565b5f611e586150cb565b611e6284826134f4565b611e748160200151825f015185613886565b9150505b92915050565b611e866138b6565b611e8f846138d8565b600854611ea6906001600160a01b031633866138f8565b5f546001600160a01b0316611eb9615114565b60025460408051630fdb11cf60e01b815281515f936001600160a01b031692630fdb11cf9260048082019391829003018187875af1158015611efd573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611f219190615a41565b509050865f5b8681101561209157811561209157611f3d615169565b888883818110611f4f57611f4f6159f4565b6020029190910135808352610140830151611f6a92506134f4565b80515f908152600b6020526040902060030154611f899060ff16613986565b1580611f99575061014081015151155b15611fa45750612089565b80515f908152600b60205260409020600501546001600160a01b03166001600160a01b03166020820181905215611fe357611fe38682602001516139bd565b600154611ffb906001600160a01b0316848684613ac0565b806060015185606001818151612011919061596a565b905250604081015160a08601805161202a90839061596a565b90525060a08101518551869061204190839061596a565b90525060e081015160c08601805161205a90839061596a565b90525060c081015160e08601805161207390839061596a565b90525060408101516120859084615a1f565b9250505b600101611f27565b5084836060015110156120c857826060015160405163de7c5c2d60e01b81526004016120bf91815260200190565b60405180910390fd5b60a083810151606080860151604080518d815260208101949094528301525f90820152608081018490527fc8f82a388a36012a2cfc586d40688baa3fe0112eff350cfd00b7e4dddaf6a5a4910160405180910390a16040516371d4eb2160e01b81526001600160a01b038516906371d4eb219061214b9086905f90600401615a86565b5f604051808303815f87803b158015612162575f80fd5b505af1158015612174573d5f803e3d5ffd5b505050506060830151604051634fa7288f60e11b815233600482015260248101919091526001600160a01b03851690639f4e511e906044015f604051808303815f87803b1580156121c3575f80fd5b505af11580156121d5573d5f803e3d5ffd5b505060085460a0860151604051632770a7eb60e21b815233600482015260248101919091526001600160a01b039091169250639dc29fac91506044015b5f604051808303815f87803b158015612229575f80fd5b505af115801561223b573d5f803e3d5ffd5b505050505050505050505050565b612251612b2b565b6001600160a01b0384165f818152600c602052604080822060018082018890558682556004820186905560028201805467ffffffffffffffff60401b1916600160401b426001600160401b031602179055600382015460059092015492515f80516020615c24833981519152946122d194929389938b9391928a926159c0565b60405180910390a250505050565b80515f03612300576040516399d8fec960e01b815260040160405180910390fd5b5f546001546005546001600160a01b03928316929182169116612321615114565b6123296151d6565b60025460408051630fdb11cf60e01b815281515f936001600160a01b031692630fdb11cf9260048082019391829003018187875af115801561236d573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906123919190615a41565b5090505f846001600160a01b031663f71c69406040518163ffffffff1660e01b8152600401602060405180830381865afa1580156123d1573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906123f59190615a08565b90506124058683838b8789613b91565b8360a001515f0361242957604051638c20998d60e01b815260040160405180910390fd5b6040516371d4eb2160e01b81526001600160a01b038816906371d4eb21906124579087905f90600401615a86565b5f604051808303815f87803b15801561246e575f80fd5b505af1158015612480573d5f803e3d5ffd5b505050505f8360200151118061249957505f8360400151115b15612509576020830151604080850151905163335525ad60e01b81526001600160a01b0388169263335525ad926124db92600401918252602082015260400190565b5f604051808303815f87803b1580156124f2575f80fd5b505af1158015612504573d5f803e3d5ffd5b505050505b61251d878785606001518660800151613c65565b60a08301511561258f5760075460a0840151604051634fa7288f60e11b81526001600160a01b039283166004820152602481019190915290881690639f4e511e906044015f604051808303815f87803b158015612578575f80fd5b505af115801561258a573d5f803e3d5ffd5b505050505b61259c87845f0151613dac565b7f7243af9a1cff94d3429b2ee00b78c1c10589259f20dc167cb67704f38f9e824e836020015184606001518560c00151865f0151876040015188608001518960a001516010546011548b60405161263c9a99989796959493929190998a5260208a019890985260408901969096526060880194909452608087019290925260a086015260c085015260e08401526101008301526101208201526101400190565b60405180910390a161265787338560c00151865f0151613ead565b5050505050505050565b5f61266a613f8d565b5f546009546001600160a01b039182169116612684615114565b5f8861268e615169565b601554156126a8576015548152600161012082015261270b565b846001600160a01b0316634d6228316040518163ffffffff1660e01b8152600401602060405180830381865afa1580156126e4573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906127089190615a08565b81525b5f885f03612718575f1998505b81511580159061272757505f83115b801561273257505f89115b1561297f578861274181615b16565b9950505f826101200151156127b757866001600160a01b0316634d6228316040518163ffffffff1660e01b8152600401602060405180830381865afa15801561278c573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906127b09190615a08565b9050612824565b825160405163040de97d60e21b815260048101919091526001600160a01b03881690631037a5f490602401602060405180830381865afa1580156127fd573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906128219190615a08565b90505b670de0b6b3a764000061283a845f01518e611e4f565b101561284e5782525f610120830152612718565b82515f908152600b60205260409020600501546001600160a01b03166001600160a01b0316602084018190521580159061289e5750816001600160a01b031683602001516001600160a01b031614155b156128b9576128b18884602001516139bd565b826020015191505b6001546128d2906001600160a01b031684868f8f613fb8565b8260600151866060018181516128e8919061596a565b905250604083015160a08701805161290190839061596a565b90525060a08301518651879061291890839061596a565b90525060e083015160c08701805161293190839061596a565b90525060c083015160e08701805161294a90839061596a565b905250608083015161295c908661596a565b945082604001518461296e9190615a1f565b9083525f6101208401529250612718565b7fc8f82a388a36012a2cfc586d40688baa3fe0112eff350cfd00b7e4dddaf6a5a48c8660a001518760600151878f6040516129dc959493929190948552602085019390935260408401919091526060830152608082015260a00190565b60405180910390a16040516371d4eb2160e01b81526001600160a01b038816906371d4eb2190612a129088905f90600401615a86565b5f604051808303815f87803b158015612a29575f80fd5b505af1158015612a3b573d5f803e3d5ffd5b50505050866001600160a01b0316639f4e511e8e87606001516040518363ffffffff1660e01b8152600401612a859291906001600160a01b03929092168252602082015260400190565b5f604051808303815f87803b158015612a9c575f80fd5b505af1158015612aae573d5f803e3d5ffd5b50505060a0909501519d9c50505050505050505050505050565b612ad0612b2b565b426018555f805460408051636d8f962d60e01b815290516001600160a01b0390921692636d8f962d9260048084019382900301818387803b158015612b13575f80fd5b505af1158015612b25573d5f803e3d5ffd5b50505050565b6004546001600160a01b03163314612b565760405163558b0d1f60e01b815260040160405180910390fd5b565b8115612bb4576040516330588f6f60e21b8152600481018390526001600160a01b0384169063c1623dbc906024015f604051808303815f87803b158015612b9d575f80fd5b505af1158015612baf573d5f803e3d5ffd5b505050505b8015612c1057604051631eaed73d60e11b8152600481018290526001600160a01b03841690633d5dae7a906024015f604051808303815f87803b158015612bf9575f80fd5b505af1158015612c0b573d5f803e3d5ffd5b505050505b505050565b5f80600f545f03612c27575081611e78565b600f54600e54612c379085615b2b565b612c419190615b42565b9392505050565b6001600160a01b0386165f908152600c602052604081206005015486516101008801516080890151929392839291612c7f9161596a565b612c89919061596a565b90505f8860a00151821115612cae5760a0890151612ca79083615a1f565b9150612cc4565b818960a00151612cbe9190615a1f565b90505f91505b81158015612cd0575080155b15612cf4576001600160a01b038a165f908152600c60205260409020869055612ea5565b8115612d9e57855f03612d0957819250612d2c565b612d14848787614183565b85612d1f8386615b2b565b612d299190615b42565b92505b5f8b8152600b602052604081206006018054859290612d4c90849061596a565b90915550612d5c9050828761596a565b6001600160a01b038b165f908152600c6020526040902055612d7e838561596a565b6001600160a01b038b165f908152600c6020526040902060050155612ea5565b8015612ea557875f03612e1d57612db58187615a1f565b6001600160a01b038b165f908152600c60209081526040808320939093558d8252600b90522060060154612de99085615a1f565b6001600160a01b038b165f908152600c60209081526040808320600501939093558d8252600b905290812060060155612ea5565b85612e288286615b2b565b612e329190615b42565b925082600b5f8d81526020019081526020015f206006015f828254612e579190615a1f565b90915550612e6790508187615a1f565b6001600160a01b038b165f908152600c6020526040902055612e898385615a1f565b6001600160a01b038b165f908152600c60205260409020600501555b6001600160a01b038a165f908152600c60209081526040808320600201805467ffffffffffffffff60401b1916600160401b426001600160401b031602179055908b0151908b0151612ef7919061596a565b90505f8a60600151821115612f1c5760608b0151612f159083615a1f565b9150612f32565b818b60600151612f2c9190615a1f565b90505f91505b81158015612f3e575080155b15612f65576001600160a01b038c165f908152600c60205260409020600101899055612fc1565b8115612f9557612f75828a61596a565b6001600160a01b038d165f908152600c6020526040902060010155612fc1565b8015612fc157612fa5818a615a1f565b6001600160a01b038d165f908152600c60205260409020600101555b50505050505050505050505050565b5f612fda82612c15565b5f848152600b60205260409020600201805490829055600d54919250908290613004908390615a1f565b61300e919061596a565b600d555092915050565b5f80613022611420565b90505f61302d61132b565b905061303a828286613886565b949350505050565b6001600160a01b0382165f908152600c6020908152604091829020825161010081018452815480825260018301549382019390935260028201546001600160401b0380821695830195909552600160401b81048516606080840191909152600160801b909104909416608080830191909152600383015460a08301908152600484015460c084015260059093015460e08301529385018390529051928401839052916130ed91615b2b565b60a083015260608101515f9061310b906001600160401b03166141ba565b905061311b8360a00151826141fa565b604084015260c080830151908401819052606084015161313b9190615b2b565b610100840181905261314d90826141fa565b60e0840181905260408401516060850151613168919061596a565b613172919061596a565b8352506020808201519083015260608101516001600160401b03908116610120840152608090910151166101409091015250565b6013546131b28161421d565b6131bc878261423e565b5f878152600b60209081526040808320815161014081018352815481526001820154938101939093526002810154918301919091526003810154606083019060ff16600481111561320f5761320f615590565b600481111561322057613220615590565b815260038201546001600160401b03610100820481166020840152600160481b820481166040840152600160881b9091041660608201526004820154608082015260058201546001600160a01b0390811660a083015260069092015460c090910152909150861615613353576001816060015160048111156132a4576132a4615590565b036133075760095460405163f476125960e01b8152600481018a90526001600160a01b039091169063f4761259906024015f604051808303815f87803b1580156132ec575f80fd5b505af11580156132fe573d5f803e3d5ffd5b50505050613337565b60048160600151600481111561331f5761331f615590565b14801561332d575087601554145b15613337575f6015555b61334e8888606001518960a001518a8a8a8a6136b1565b6133fe565b60018160600151600481111561336b5761336b615590565b036133ce57600954604051634cc8221560e01b8152600481018a90526001600160a01b0390911690634cc82215906024015f604051808303815f87803b1580156133b3575f80fd5b505af11580156133c5573d5f803e3d5ffd5b505050506133fe565b6004816060015160048111156133e6576133e6615590565b1480156133f4575087601554145b156133fe575f6015555b5f8160400151600d546134119190615a1f565b600d8190555f8a8152600b602052604081208181556001808201839055600282018390556003820180546001600160c81b03191680825560048085018690556005850180546001600160a01b031916905560069094019490945593945087939291849081111561348357613483615590565b02179055505f89815260126020526040808220828155600101919091556003549051630852cd8d60e31b8152600481018b90526001600160a01b03909116906342966c68906024015f604051808303815f87803b1580156134e2575f80fd5b505af1158015612fc1573d5f803e3d5ffd5b5f828152600b60205260409020600501546001600160a01b031680156135325761351c61507c565b6135268282613042565b612b2584838584614311565b5f838152600b6020908152604080832060020154601290925290912060010154601154670de0b6b3a76400009161356891615a1f565b6135729083615b2b565b61357c9190615b42565b6040808501919091525f85815260126020522054601054670de0b6b3a7640000916135a691615a1f565b6135b09083615b2b565b6135ba9190615b42565b60608401525f848152600b602081815260408320805460a08801908152938890529190526004015460c0850181905290516135f59190615b2b565b60e08401525f848152600b602052604081206003015461362490600160481b90046001600160401b03166141ba565b90506136348460e00151826141fa565b60808501819052604085015160a086015161364f919061596a565b613659919061596a565b845260608401515f868152600b602052604090206001015461367b919061596a565b6020858101919091525f958652600b90525050604090922060030154600160881b90046001600160401b03166101209091015250565b5f878152600b60209081526040808320815161014081018352815481526001820154938101939093526002810154918301919091526003810154606083019060ff16600481111561370457613704615590565b600481111561371557613715615590565b815260038201546001600160401b0361010080830482166020850152600160481b830482166040850152600160881b9092041660608301526004830154608083015260058301546001600160a01b031660a083015260069092015460c0909101528651908701519192505f9161378b9089615a1f565b6137959190615a1f565b90505f8660200151896137a89190615a1f565b6101208401516001600160a01b0388165f908152600c60205260408120600501805493945091926137da908490615a1f565b909155506137ea90508285615a1f565b6001600160a01b0387165f908152600c602052604090205561380c8186615a1f565b6001600160a01b03969096165f908152600c6020908152604080832060018101999099556002909801805467ffffffffffffffff60401b1916600160401b426001600160401b0316021790559a8152600b909a525050509186206005810180546001600160a01b0319169055600601959095555050505050565b5f82156138ac575f836138998487615b2b565b6138a39190615b42565b9150612c419050565b505f199392505050565b6018545f03612b565760405163f8b7c7bf60e01b815260040160405180910390fd5b805f03610ddd57604051631f2a200560e01b815260040160405180910390fd5b6040516370a0823160e01b81526001600160a01b0383811660048301525f91908516906370a0823190602401602060405180830381865afa15801561393f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906139639190615a08565b905081811015612b25576040516307a5137f60e11b815260040160405180910390fd5b5f600182600481111561399b5761399b615590565b1480611e78575060048260048111156139b6576139b6615590565b1492915050565b6139c561507c565b6139cf8282613042565b80516001600160a01b0383165f908152600c60205260409020908155600201805467ffffffffffffffff60401b1916600160401b426001600160401b031602179055613a19615114565b60a082015160e082015260808201518251613a349190615b2b565b60c08083019190915260e08301516101208301526101008301516101608301528201518251613a639190615b2b565b6101408201526040516371d4eb2160e01b81526001600160a01b038516906371d4eb2190613a979084908790600401615a86565b5f604051808303815f87803b158015613aae575f80fd5b505af1158015612657573d5f803e3d5ffd5b613ad2838261014001515f015161458e565b604082015281613af1662386f26fc10000670de0b6b3a764000061596a565b8260400151613b009190615b2b565b613b0a9190615b42565b60608201819052610140820151602001511015613b6e57610140810151602001516060820152613b49662386f26fc10000670de0b6b3a764000061596a565b8282610140015160200151613b5e9190615b2b565b613b689190615b42565b60408201525b60208101516001600160a01b03161515613b898583836145a3565b505050505050565b835f5b8451811015612657575f858281518110613bb057613bb06159f4565b6020908102919091018101515f818152600b909252604090912060030154909150613bdd9060ff16613986565b613be75750613c5d565b5f613bf2828a611e4f565b90507f0000000000000000000000000000000000000000000000000f43fc2c04ee0000811015613c5a57613c246151d6565b613c2c6150cb565b613c3a8c85888e85876149e8565b6020820151613c499087615a1f565b9550613c5781838a8a614dfa565b50505b50505b600101613b94565b8115612b25576016545f90613c82670de0b6b3a764000084615b2b565b613c8c919061596a565b90505f601754670de0b6b3a764000085613ca69190615b2b565b613cb0919061596a565b90505f600d5483613cc19190615b42565b90505f600d5483613cd29190615b42565b9050600d5482613ce29190615b2b565b613cec9085615a1f565b601655600d54613cfc9082615b2b565b613d069084615a1f565b601755601054613d1790839061596a565b601055601154613d2890829061596a565b60115560405162afc6b160e31b8152600481018790526001600160a01b0388169063057e3588906024015f604051808303815f87803b158015613d69575f80fd5b505af1158015613d7b573d5f803e3d5ffd5b505060405163c2283f1f60e01b8152600481018890526001600160a01b038b16925063c2283f1f9150602401612212565b600d54600e819055505f826001600160a01b0316630367b3026040518163ffffffff1660e01b8152600401602060405180830381865afa158015613df2573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190613e169190615a08565b90505f60015f9054906101000a90046001600160a01b03166001600160a01b0316630367b3026040518163ffffffff1660e01b8152600401602060405180830381865afa158015613e69573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190613e8d9190615a08565b905080613e9a8484615a1f565b613ea4919061596a565b600f5550505050565b8115613f52576006546040516323b872dd60e01b81526001600160a01b0391821660048201528482166024820152604481018490527f000000000000000000000000b8e28767ff0f6197b6820b7c6e2cf75b48b94adf909116906323b872dd906064016020604051808303815f875af1158015613f2c573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190613f509190615b61565b505b8015612b2557604051634fa7288f60e11b81526001600160a01b03848116600483015260248201839052851690639f4e511e90604401613a97565b600a546001600160a01b03163314612b5657604051630126422b60e21b815260040160405180910390fd5b613fca845f01518561014001516134f4565b613fdc838561014001515f015161458e565b604085018190525f908390613ffa90670de0b6b3a764000090615b2b565b6140049190615b42565b9050670de0b6b3a76400006140198383615b2b565b6140239190615b42565b608086018190526140349082615a1f565b606086015260208501516001600160a01b031615155f6140558888846145a3565b9050686c6b935b8bbd4000008110156126575786610120015161416e5786515f908152600b60205260409020600301805460ff1916600417905581156140fb57600954875160405163f476125960e01b81526001600160a01b039092169163f4761259916140c99160040190815260200190565b5f604051808303815f87803b1580156140e0575f80fd5b505af11580156140f2573d5f803e3d5ffd5b5050505061415d565b6009548751604051634cc8221560e01b81526001600160a01b0390921691634cc822159161412f9160040190815260200190565b5f604051808303815f87803b158015614146575f80fd5b505af1158015614158573d5f803e3d5ffd5b505050505b80156141695786516015555b612657565b805f03612657575f6015555050505050505050565b81614192633b9aca0085615b2b565b10801561419c5750805b15612c105760405163086b275d60e31b815260040160405180910390fd5b5f6018545f036141ce57611e788242615a1f565b5f6018541180156141e0575060185482105b156141f35781601854611e789190615a1f565b505f919050565b5f670de0b6b3a76400006301e133806142138486615b2b565b612c379190615b42565b80600103610ddd57604051633307cee360e01b815260040160405180910390fd5b5f828152600b602052604081206003015461010090046001600160401b031690614269600184615a1f565b90505f6013828154811061427f5761427f6159f4565b905f5260205f2001549050806013846001600160401b0316815481106142a7576142a76159f4565b5f91825260208083209190910192909255828152600b90915260409020600301805468ffffffffffffffff0019166101006001600160401b0386160217905560138054806142f7576142f7615b7a565b600190038181905f5260205f20015f905590555050505050565b5f848152600b60209081526040808320815161014081018352815481526001820154938101939093526002810154918301919091526003810154606083019060ff16600481111561436457614364615590565b600481111561437557614375615590565b815260038201546001600160401b0361010082048116602080850191909152600160481b83048216604080860191909152600160881b9093049091166060840152600484015460808401526005808501546001600160a01b0390811660a086015260069095015460c0909401939093526101208501519389165f908152600c825282812090930154858301518b85526012909252919092206001015460115494955092939092670de0b6b3a76400009161442f9190615a1f565b6144399083615b2b565b6144439190615b42565b6040808801919091525f89815260126020522054601054670de0b6b3a76400009161446d91615a1f565b6144779083615b2b565b6144819190615b42565b60608701528115614505578183866060015161449d9190615b2b565b6144a79190615b42565b60a0870181905260808601516144bc91615b2b565b60e0870152604085015182906144d3908590615b2b565b6144dd9190615b42565b608087015260e085015182906144f4908590615b2b565b6144fe9190615b42565b6101008701525b60808086015160c088015261010087015190870151604088015160a089015161452e919061596a565b614538919061596a565b614542919061596a565b865260608601516020850151614558919061596a565b602087015261014085015160c085015161457b91906001600160401b0316614f28565b6101209096019590955250505050505050565b5f81831061459c5781612c41565b5090919050565b5f8083604001518461014001515f01516145bd9190615a1f565b90505f8460600151856101400151602001516145d99190615a1f565b6101408601516040015160a087015290508315614756576146038560200151866101600151613042565b5f8560400151866101400151604001518761016001515f0151614626919061596a565b6146309190615a1f565b61016087015160a081015160c0890152608001519091506146519082615b2b565b60e087015261465e615114565b60408088015160a0830152606080890151818401526101408901805190920151835290510151602082015261016080880180516101000151918301919091525160c001516146ac9083615b2b565b6101408201525f546020880151604051631bb3f79760e31b81526001600160a01b039092169163dd9fbcb8916146e791859190600401615a86565b5f604051808303815f87803b1580156146fe575f80fd5b505af1158015614710573d5f803e3d5ffd5b505088515f908152600b6020908152604082206001018790558a51818c01516101608d015192830151925161474f96509194509286928a929091612c48565b50506147c8565b61014085015160e081015160c08088019190915201516147769083615b2b565b60e086015284515f908152600b602052604080822084905586518252808220600101839055865182529020600301805467ffffffffffffffff60481b1916600160481b426001600160401b0316021790555b84516147d49082612fd0565b61010086015261014085015160408101516060909101516147f6918891612b58565b84516010545f9182526012602052604090912090815560115460019091015583156148725784516020808701515f838152600b9092526040918290206006015461010089015160105460115494515f80516020615be4833981519152956148659594938993909290919061597d565b60405180910390a26148b7565b845f01515f80516020615bc4833981519152838388610100015189610140015160c001516010546011546040516148ae969594939291906158cc565b60405180910390a25b845f01515f80516020615c04833981519152600687610140015160c00151886101400151604001515f8a604001516148ee90615b8e565b8b6101400151606001518c6060015161490690615b8e565b6040516149199796959493929190615904565b60405180910390a283156149a557602080860180516001600160a01b039081165f818152600c909452604080852054845184168652818620600101546101608c0151608081015160c0909101519651909516875282872060050154925193965f80516020615c248339815191529661499c966006969495939490939192906159c0565b60405180910390a25b845160808601516040519081527fc7e8309b9b14e7a8561ed352b9fd8733de32417fb7b6a69f5671f79e7bb29ddd9060200160405180910390a250949350505050565b6003546040516331a9108f60e11b8152600481018790525f916001600160a01b031690636352211e90602401602060405180830381865afa158015614a2f573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190614a539190615ba8565b9050614a5f86846134f4565b5f868152600b60205260409020600501546001600160a01b0316801515614a8461507c565b8115614a9457614a948382613042565b614aa78a87604001518860600151612b58565b614ab48660200151614f37565b80865260208701515f91614ac791615a1f565b9050614ad8875f0151828b8b614f54565b60a08b015260808a0152606089015260408801526020870152614af9615114565b602080890151606080840191909152895160a084015289015182820152604089015182528301518351614b33918d918491899160036131a6565b8315614c3f57608083015160408901518951614b4f9190615a1f565b614b599190615b2b565b8360a00151614b68919061596a565b60e088015260808301518351614b7e9190615b2b565b61010088015260e083015161012082015260c083015160408901518951614ba59190615a1f565b614baf9190615b2b565b836101000151614bbf919061596a565b61016082015260c08301518351614bd69190615b2b565b6101408201525f54604051631bb3f79760e31b81526001600160a01b039091169063dd9fbcb890614c0d9084908990600401615a86565b5f604051808303815f87803b158015614c24575f80fd5b505af1158015614c36573d5f803e3d5ffd5b50505050614c4a565b60e080890151908801525b60a087015115614cbb5760075460a0880151604051633f10abab60e01b81526001600160a01b0389811660048301526024820192909252911690633f10abab906044015f604051808303815f87803b158015614ca4575f80fd5b505af1158015614cb6573d5f803e3d5ffd5b505050505b60048054604051638fef27ab60e01b81529182018d90526001600160a01b031690638fef27ab906024015f604051808303815f87803b158015614cfc575f80fd5b505af1158015614d0e573d5f803e3d5ffd5b505050508a5f80516020615bc48339815191525f805f805f80604051614d39969594939291906158cc565b60405180910390a28a5f80516020615c0483398151915260055f8b604001515f8d5f0151614d6690615b8e565b8e606001518f60200151614d7990615b8e565b604051614d8c9796959493929190615904565b60405180910390a2831561223b576001600160a01b0385165f818152600c602052604080822080546001820154608089015160c08a015160059485015495515f80516020615c2483398151915297614de497916159c0565b60405180910390a2505050505050505050505050565b825182518390614e0b90839061596a565b90525060c08201805166853a0d2313c0009190614e2990839061596a565b905250835160a082018051614e3f90839061596a565b9052506020840151606082018051614e5890839061596a565b905250604084015181518290614e6f90839061596a565b90525060e0808401519082018051614e8890839061596a565b90525061010083015160c082018051614ea290839061596a565b9052506020808401519083018051614ebb90839061596a565b9052506040808401519083018051614ed490839061596a565b9052506060808401519083018051614eed90839061596a565b9052506080808401519083018051614f0690839061596a565b90525060a0808401519083018051614f1f90839061596a565b90525050505050565b5f8183101561459c5781612c41565b5f611e78614f4660c884615b42565b671bc16d674ec8000061458e565b5f80808080808715614fb557614f6a8a8961458e565b955089614f77878b615b2b565b614f819190615b42565b9050614faf81877f00000000000000000000000000000000000000000000000000b1a2bc2ec500008a615023565b90955091505b614fbf868b615a1f565b93508315615017575f614fd2828b615a1f565b905080156150155761500f614fe7848361596a565b867f000000000000000000000000000000000000000000000000016345785d8a00008b615023565b90945092505b505b50945094509450945094565b5f80808361503986670de0b6b3a764000061596a565b6150439088615b2b565b61504d9190615b42565b90508087111561506b579150816150648188615a1f565b9150615072565b8692505f91505b5094509492505050565b6040518061016001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b6040518061014001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b6040518061018001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b6040518061018001604052805f81526020015f6001600160a01b031681526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f151581526020016151c46150cb565b81526020016151d161507c565b905290565b6040518061012001604052805f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81526020015f81525090565b5f610180828403121561522a575f80fd5b50919050565b5f805f805f6102008688031215615245575f80fd5b8535945060208601359350604086013592506060860135915061526b8760808801615219565b90509295509295909350565b6001600160a01b0381168114610ddd575f80fd5b634e487b7160e01b5f52604160045260245ffd5b60405161018081016001600160401b03811182821017156152c2576152c261528b565b60405290565b604051601f8201601f191681016001600160401b03811182821017156152f0576152f061528b565b604052919050565b5f6101808284031215615309575f80fd5b61531161529f565b9050813581526020820135602082015260408201356040820152606082013560608201526080820135608082015260a082013560a082015260c082013560c082015260e082013560e082015261010080830135818301525061012080830135818301525061014080830135818301525061016080830135818301525092915050565b5f805f805f8061022087890312156153a9575f80fd5b86356153b481615277565b9550602087013594506153ca88604089016152f8565b93506101c08701356153db81615277565b959894975092956101e081013594610200909101359350915050565b5f805f805f805f610240888a03121561540e575f80fd5b87359650602088013595506040880135945061542d8960608a016152f8565b93506101e088013561543e81615277565b9699959850939692959461020084013594506102209093013592915050565b5f805f806101e08587031215615471575f80fd5b8435935060208501359250604085013591506154908660608701615219565b905092959194509250565b5f602082840312156154ab575f80fd5b5035919050565b5f805f606084860312156154c4575f80fd5b83356154cf81615277565b95602085013595506040909401359392505050565b5f602082840312156154f4575f80fd5b8135612c4181615277565b5f805f805f60a08688031215615513575f80fd5b853561551e81615277565b97602087013597506040870135966060810135965060800135945092505050565b5f805f805f6102008688031215615554575f80fd5b8535945061556587602088016152f8565b93506101a086013561557681615277565b949793965093946101c081013594506101e0013592915050565b634e487b7160e01b5f52602160045260245ffd5b600581106155b4576155b4615590565b9052565b8a8152602081018a90526040810189905261014081016155db606083018a6155a4565b6001600160401b03978816608083015295871660a08201529390951660c084015260e08301919091526001600160a01b03166101008201526101200191909152949350505050565b5f805f806101e08587031215615637575f80fd5b843561564281615277565b93506020850135925061565886604087016152f8565b939692955092936101c00135925050565b5f610240828403121561522a575f80fd5b5f805f805f805f610240888a031215615691575f80fd5b87359650602088013595506040880135945060608801356156b181615277565b93506080880135925060a088013591506156ce8960c08a01615219565b905092959891949750929550565b5f805f805f805f80610260898b0312156156f4575f80fd5b8835975060208901359650604089013595506157138a60608b016152f8565b94506101e089013561572481615277565b979a969950949793969561020085013595506102208501359461024001359350915050565b5f806040838503121561575a575f80fd5b50508035926020909101359150565b5f805f806060858703121561577c575f80fd5b8435935060208501356001600160401b0380821115615799575f80fd5b818701915087601f8301126157ac575f80fd5b8135818111156157ba575f80fd5b8860208260051b85010111156157ce575f80fd5b95986020929092019750949560400135945092505050565b5f805f80608085870312156157f9575f80fd5b843561580481615277565b966020860135965060408601359560600135945092505050565b60208101611e7882846155a4565b5f602080838503121561583d575f80fd5b82356001600160401b0380821115615853575f80fd5b818501915085601f830112615866575f80fd5b8135818111156158785761587861528b565b8060051b91506158898483016152c8565b81815291830184019184810190888411156158a2575f80fd5b938501935b838510156158c0578435825293850193908501906158a7565b98975050505050505050565b958652602086019490945260408501929092526060840152608083015260a082015260c00190565b600a81106155b4576155b4615590565b60e08101615912828a6158f4565b8760208301528660408301528560608301528460808301528360a08301528260c083015298975050505050505050565b634e487b7160e01b5f52600160045260245ffd5b634e487b7160e01b5f52601160045260245ffd5b80820180821115611e7857611e78615956565b6001600160a01b03969096168652602086019490945260408501929092526060840152608083015260a082015260c00190565b600781106155b4576155b4615590565b60e08101615912828a6159b0565b8181035f8312801583831316838312821617156159ed576159ed615956565b5092915050565b634e487b7160e01b5f52603260045260245ffd5b5f60208284031215615a18575f80fd5b5051919050565b81810381811115611e7857611e78615956565b8051801515811461114c575f80fd5b5f8060408385031215615a52575f80fd5b82519150615a6260208401615a32565b90509250929050565b5f6101808284031215615a7c575f80fd5b612c4183836152f8565b825181526020808401519082015260408084015190820152606080840151908201526080808401519082015260a0808401519082015260c0808401519082015260e0808401519082015261010080840151908201526101208084015190820152610140808401519082015261016080840151908201526001600160a01b0382166101808201526101a08101612c41565b5f81615b2457615b24615956565b505f190190565b8082028115828204841417611e7857611e78615956565b5f82615b5c57634e487b7160e01b5f52601260045260245ffd5b500490565b5f60208284031215615b71575f80fd5b612c4182615a32565b634e487b7160e01b5f52603160045260245ffd5b5f600160ff1b8201615ba257615ba2615956565b505f0390565b5f60208284031215615bb8575f80fd5b8151612c418161527756fe0fba2673863b12c7b8463f3fa2f9b0cb1d534c573cdec5b5d895ee00d6ce6f5e6464838e073667756f10746b26734b60870fdcad31d7861c6e5603430bccac61962110f281c1213763cd97a546b337b3cbfd25a31ea9723e9d8b7376ba45da1aecf6daab6f1facdfdd8dfe32b525744d8a7a940824dd52e2b53c24028ee5faa0a2646970667358221220603af07787131add9b356ca9d57147b80fa987abfaa1185b094ba16f323e41df64736f6c63430008180033

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

00000000000000000000000036b9adccf2a08839ddc342d7a9d259b984fbd855

-----Decoded View---------------
Arg [0] : _addressesRegistry (address): 0x36b9ADccF2a08839DDC342d7a9D259B984fbD855

-----Encoded View---------------
1 Constructor Arguments found :
Arg [0] : 00000000000000000000000036b9adccf2a08839ddc342d7a9d259b984fbd855


Block Transaction Gas Used Reward
view all blocks produced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
[ Download: CSV Export  ]

A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.