S Price: $0.067903 (+1.65%)
Gas: 55 Gwei

Contract

0x9d68d6a5FE625778b4F879AB027C895711B421fE

Overview

S Balance

Sonic LogoSonic LogoSonic Logo0 S

S Value

$0.00

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Block
From
To
Close Trove To R...478980372025-09-23 11:42:17125 days ago1758627737IN
0x9d68d6a5...711B421fE
0 S0.0225073255
Close Trove To R...476359652025-09-21 10:50:36127 days ago1758451836IN
0x9d68d6a5...711B421fE
0 S0.0221839255
Open Trove With ...474293792025-09-19 19:02:06129 days ago1758308526IN
0x9d68d6a5...711B421fE
9,701 S0.0013888655
Close Trove To R...473627182025-09-19 6:39:49129 days ago1758263989IN
0x9d68d6a5...711B421fE
0 S0.0212264451.5
Open Trove With ...472556342025-09-18 8:58:58130 days ago1758185938IN
0x9d68d6a5...711B421fE
1,001 S0.0470404651.5
Open Trove With ...471397142025-09-17 9:43:18131 days ago1758102198IN
0x9d68d6a5...711B421fE
1,601 S0.0435367256.375
Close Trove To R...466647572025-09-13 1:34:12136 days ago1757727252IN
0x9d68d6a5...711B421fE
0 S0.0219245955
Open Trove With ...466646692025-09-13 1:33:11136 days ago1757727191IN
0x9d68d6a5...711B421fE
1,501 S0.0414894155
Open Trove With ...466636962025-09-13 1:22:33136 days ago1757726553IN
0x9d68d6a5...711B421fE
1,503 S0.0087051660
Open Trove With ...466633722025-09-13 1:19:53136 days ago1757726393IN
0x9d68d6a5...711B421fE
1,501 S0.0079797355
Open Trove With ...465832092025-09-12 8:26:14136 days ago1757665574IN
0x9d68d6a5...711B421fE
1,141 S0.0356356750.0001
Open Trove With ...455613342025-09-03 3:49:47146 days ago1756871387IN
0x9d68d6a5...711B421fE
2,001 S0.0388037655
Close Trove To R...455500142025-09-03 1:24:01146 days ago1756862641IN
0x9d68d6a5...711B421fE
0 S0.0220545655
Open Trove With ...455139462025-09-02 18:02:48146 days ago1756836168IN
0x9d68d6a5...711B421fE
1,901 S0.0392809455
Open Trove With ...455091102025-09-02 17:03:41146 days ago1756832621IN
0x9d68d6a5...711B421fE
1,001 S0.0101927460
Open Trove With ...455088282025-09-02 17:00:29146 days ago1756832429IN
0x9d68d6a5...711B421fE
986.545 S0.0085121450.1071
Open Trove With ...454835692025-09-02 12:49:18146 days ago1756817358IN
0x9d68d6a5...711B421fE
1,251 S0.0516937855
Open Trove With ...454834272025-09-02 12:48:16146 days ago1756817296IN
0x9d68d6a5...711B421fE
1,001 S0.0097838357.75
Open Trove With ...454834002025-09-02 12:48:02146 days ago1756817282IN
0x9d68d6a5...711B421fE
1,001 S0.0102497260.5

Latest 25 internal transactions (View All)

Advanced mode:
Parent Transaction Hash Block From To
478980372025-09-23 11:42:17125 days ago1758627737
0x9d68d6a5...711B421fE
1,251 S
478980372025-09-23 11:42:17125 days ago1758627737
0x9d68d6a5...711B421fE
1,251 S
477737892025-09-22 13:11:48126 days ago1758546708
0x9d68d6a5...711B421fE
396,899 S
477737892025-09-22 13:11:48126 days ago1758546708
0x9d68d6a5...711B421fE
396,899 S
477735292025-09-22 13:08:03126 days ago1758546483
0x9d68d6a5...711B421fE
1 S
477735292025-09-22 13:08:03126 days ago1758546483
0x9d68d6a5...711B421fE
1 S
476359652025-09-21 10:50:36127 days ago1758451836
0x9d68d6a5...711B421fE
1,601 S
476359652025-09-21 10:50:36127 days ago1758451836
0x9d68d6a5...711B421fE
1,601 S
473627182025-09-19 6:39:49129 days ago1758263989
0x9d68d6a5...711B421fE
2,001 S
473627182025-09-19 6:39:49129 days ago1758263989
0x9d68d6a5...711B421fE
2,001 S
472556342025-09-18 8:58:58130 days ago1758185938
0x9d68d6a5...711B421fE
1,001 S
471397142025-09-17 9:43:18131 days ago1758102198
0x9d68d6a5...711B421fE
1,601 S
470043002025-09-16 5:35:30132 days ago1758000930
0x9d68d6a5...711B421fE
399,001 S
470043002025-09-16 5:35:30132 days ago1758000930
0x9d68d6a5...711B421fE
399,001 S
466647572025-09-13 1:34:12136 days ago1757727252
0x9d68d6a5...711B421fE
1,501 S
466647572025-09-13 1:34:12136 days ago1757727252
0x9d68d6a5...711B421fE
1,501 S
466646692025-09-13 1:33:11136 days ago1757727191
0x9d68d6a5...711B421fE
1,501 S
466636962025-09-13 1:22:33136 days ago1757726553
0x9d68d6a5...711B421fE
1,503 S
466633722025-09-13 1:19:53136 days ago1757726393
0x9d68d6a5...711B421fE
1,501 S
465832092025-09-12 8:26:14136 days ago1757665574
0x9d68d6a5...711B421fE
1,141 S
455613342025-09-03 3:49:47146 days ago1756871387
0x9d68d6a5...711B421fE
2,001 S
455500142025-09-03 1:24:01146 days ago1756862641
0x9d68d6a5...711B421fE
1,901 S
455500142025-09-03 1:24:01146 days ago1756862641
0x9d68d6a5...711B421fE
1,901 S
455139462025-09-02 18:02:48146 days ago1756836168
0x9d68d6a5...711B421fE
1,901 S
455091102025-09-02 17:03:41146 days ago1756832621
0x9d68d6a5...711B421fE
1,001 S
View All Internal Transactions
Cross-Chain Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
LeverageWETHZapper

Compiler Version
v0.8.24+commit.e11b9ed9

Optimization Enabled:
Yes with 200 runs

Other Settings:
cancun EvmVersion
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

import "./WETHZapper.sol";
import "../Dependencies/Constants.sol";
import "./Interfaces/ILeverageZapper.sol";

contract LeverageWETHZapper is WETHZapper, ILeverageZapper {
  constructor(
    IAddressesRegistry _addressesRegistry,
    IFlashLoanProvider _flashLoanProvider,
    IExchange _exchange,
    address _USDC
  ) WETHZapper(_addressesRegistry, _flashLoanProvider, _exchange, _USDC) {
    // Approval of coll (WETH) to BorrowerOperations is done in parent WETHZapper
    // Approve One to exchange module (Coll is approved in parent WETHZapper)
    oneToken.approve(address(_exchange), type(uint256).max);
  }

  function openLeveragedTroveWithRawETH(OpenLeveragedTroveParams memory _params)
    external
    payable
  {
    require(
      msg.value == ETH_GAS_COMPENSATION + _params.collAmount, "LZ: Wrong amount of ETH"
    );
    require(
      _params.batchManager == address(0) || _params.annualInterestRate == 0,
      "LZ: Cannot choose interest if joining a batch"
    );

    // Include the original sender in the index, so it is included in the final troveId
    _params.ownerIndex = _getTroveIndex(msg.sender, _params.ownerIndex);

    // Set initial balances to make sure there are not leftovers
    InitialBalances memory initialBalances;
    _setInitialTokensAndBalances(WETH, oneToken, initialBalances);

    // Convert ETH to WETH
    WETH.deposit{ value: msg.value }();

    // Flash loan coll
    flashLoanProvider.makeFlashLoan(
      WETH,
      _params.flashLoanAmount,
      IFlashLoanProvider.Operation.OpenTrove,
      false,
      abi.encode(_params)
    );

    // return leftovers to user
    _returnLeftovers(initialBalances);
  }

  // Callback from the flash loan provider
  function receiveFlashLoanOnOpenLeveragedTrove(
    OpenLeveragedTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external override {
    require(msg.sender == address(flashLoanProvider), "LZ: Caller not FlashLoan provider");

    uint256 totalCollAmount = _params.collAmount + _effectiveFlashLoanAmount;
    // We compute oneAmount off-chain for efficiency

    uint256 troveId;
    // Open trove
    if (_params.batchManager == address(0)) {
      troveId = borrowerOperations.openTrove(
        _params.owner,
        _params.ownerIndex,
        totalCollAmount,
        _params.oneAmount,
        _params.upperHint,
        _params.lowerHint,
        _params.annualInterestRate,
        _params.maxUpfrontFee,
        // Add this contract as add/receive manager to be able to fully adjust trove,
        // while keeping the same management functionality
        address(this), // add manager
        address(this), // remove manager
        address(this) // receiver for remove manager
      );
    } else {
      IBorrowerOperations.OpenTroveAndJoinInterestBatchManagerParams memory
        openTroveAndJoinInterestBatchManagerParams = IBorrowerOperations
          .OpenTroveAndJoinInterestBatchManagerParams({
          owner: _params.owner,
          ownerIndex: _params.ownerIndex,
          collAmount: totalCollAmount,
          oneAmount: _params.oneAmount,
          upperHint: _params.upperHint,
          lowerHint: _params.lowerHint,
          interestBatchManager: _params.batchManager,
          maxUpfrontFee: _params.maxUpfrontFee,
          // Add this contract as add/receive manager to be able to fully adjust trove,
          // while keeping the same management functionality
          addManager: address(this), // add manager
          removeManager: address(this), // remove manager
          receiver: address(this) // receiver for remove manager
         });
      troveId = borrowerOperations.openTroveAndJoinInterestBatchManager(
        openTroveAndJoinInterestBatchManagerParams
      );
    }

    // Set add/remove managers
    _setAddManager(troveId, _params.addManager);
    _setRemoveManagerAndReceiver(troveId, _params.removeManager, _params.receiver);

    // Swap One to Coll
    exchange.swapFromOne(_params.oneAmount, _params.flashLoanAmount);

    // Send coll back to return flash loan
    WETH.transfer(address(flashLoanProvider), _params.flashLoanAmount);
    // WETH reverts on failure:
    // https://etherscan.io/token/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2#code
  }

  function leverUpTrove(LeverUpTroveParams calldata _params) external {
    address owner = troveNFT.ownerOf(_params.troveId);
    address receiver =
      _requireSenderIsOwnerOrRemoveManagerAndGetReceiver(_params.troveId, owner);
    _requireZapperIsReceiver(_params.troveId);

    // Set initial balances to make sure there are not leftovers
    InitialBalances memory initialBalances;
    _setInitialTokensBalancesAndReceiver(WETH, oneToken, initialBalances, receiver);

    // Flash loan coll
    flashLoanProvider.makeFlashLoan(
      WETH,
      _params.flashLoanAmount,
      IFlashLoanProvider.Operation.LeverUpTrove,
      false,
      abi.encode(_params)
    );

    // return leftovers to user
    _returnLeftovers(initialBalances);
  }

  // Callback from the flash loan provider
  function receiveFlashLoanOnLeverUpTrove(
    LeverUpTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external override {
    require(msg.sender == address(flashLoanProvider), "LZ: Caller not FlashLoan provider");

    // Adjust trove
    // With the received coll from flash loan, we increase both the trove coll and debt
    borrowerOperations.adjustTrove(
      _params.troveId,
      _effectiveFlashLoanAmount, // flash loan amount minus fee
      true, // _isCollIncrease
      _params.oneAmount,
      true, // _isDebtIncrease
      _params.maxUpfrontFee
    );

    // Swap One to Coll
    // No need to use a min: if the obtained amount is not enough, the flash loan return
    // below won’t be enough
    // And the flash loan provider will revert after this function exits
    // The frontend should calculate in advance the `_params.oneAmount` needed for this
    // to work
    exchange.swapFromOne(_params.oneAmount, _params.flashLoanAmount);

    // Send coll back to return flash loan
    WETH.transfer(address(flashLoanProvider), _params.flashLoanAmount);
  }

  function leverDownTrove(LeverDownTroveParams calldata _params) external {
    address owner = troveNFT.ownerOf(_params.troveId);
    address receiver =
      _requireSenderIsOwnerOrRemoveManagerAndGetReceiver(_params.troveId, owner);
    _requireZapperIsReceiver(_params.troveId);

    // Set initial balances to make sure there are not leftovers
    InitialBalances memory initialBalances;
    _setInitialTokensBalancesAndReceiver(WETH, oneToken, initialBalances, receiver);

    // Flash loan coll
    flashLoanProvider.makeFlashLoan(
      WETH,
      _params.flashLoanAmount,
      IFlashLoanProvider.Operation.LeverDownTrove,
      false,
      abi.encode(_params)
    );

    // return leftovers to user
    _returnLeftovers(initialBalances);
  }

  // Callback from the flash loan provider
  function receiveFlashLoanOnLeverDownTrove(
    LeverDownTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external override {
    require(msg.sender == address(flashLoanProvider), "LZ: Caller not FlashLoan provider");

    // Swap Coll from flash loan to One, so we can repay and downsize trove
    // We swap the flash loan minus the flash loan fee
    // The frontend should calculate in advance the `_params.minOneAmount` to achieve the
    // desired leverage ratio
    // (with some slippage tolerance)
    uint256 receivedOneAmount =
      exchange.swapToOne(_effectiveFlashLoanAmount, _params.minOneAmount);

    // Adjust trove
    borrowerOperations.adjustTrove(
      _params.troveId,
      _params.flashLoanAmount,
      false, // _isCollIncrease
      receivedOneAmount,
      false, // _isDebtIncrease
      0
    );

    // Send coll back to return flash loan
    WETH.transfer(address(flashLoanProvider), _params.flashLoanAmount);
  }

  // As formulas are symmetrical, it can be used in both ways
  function leverageRatioToCollateralRatio(uint256 _inputRatio)
    external
    pure
    returns (uint256)
  {
    return _inputRatio * DECIMAL_PRECISION / (_inputRatio - DECIMAL_PRECISION);
  }
}

// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

import "./BaseZapper.sol";
import "../Dependencies/Constants.sol";

contract WETHZapper is BaseZapper {
  constructor(
    IAddressesRegistry _addressesRegistry,
    IFlashLoanProvider _flashLoanProvider,
    IExchange _exchange,
    address _USDC
  ) BaseZapper(_addressesRegistry, _flashLoanProvider, _exchange, _USDC) {
    require(
      address(WETH) == address(_addressesRegistry.collToken()), "WZ: Wrong coll branch"
    );

    // Approve coll to BorrowerOperations
    WETH.approve(address(borrowerOperations), type(uint256).max);
    // Approve Coll to exchange module (for closeTroveFromCollateral)
    WETH.approve(address(_exchange), type(uint256).max);
  }

  function openTroveWithRawETH(OpenTroveParams calldata _params)
    external
    payable
    returns (uint256)
  {
    require(msg.value > ETH_GAS_COMPENSATION, "WZ: Insufficient ETH");
    require(
      _params.batchManager == address(0) || _params.annualInterestRate == 0,
      "WZ: Cannot choose interest if joining a batch"
    );

    // Convert ETH to WETH
    WETH.deposit{ value: msg.value }();

    uint256 troveId;
    // Include sender in index
    uint256 index = _getTroveIndex(_params.ownerIndex);
    if (_params.batchManager == address(0)) {
      troveId = borrowerOperations.openTrove(
        _params.owner,
        index,
        msg.value - ETH_GAS_COMPENSATION,
        _params.oneAmount,
        _params.upperHint,
        _params.lowerHint,
        _params.annualInterestRate,
        _params.maxUpfrontFee,
        // Add this contract as add/receive manager to be able to fully adjust trove,
        // while keeping the same management functionality
        address(this), // add manager
        address(this), // remove manager
        address(this) // receiver for remove manager
      );
    } else {
      IBorrowerOperations.OpenTroveAndJoinInterestBatchManagerParams memory
        openTroveAndJoinInterestBatchManagerParams = IBorrowerOperations
          .OpenTroveAndJoinInterestBatchManagerParams({
          owner: _params.owner,
          ownerIndex: index,
          collAmount: msg.value - ETH_GAS_COMPENSATION,
          oneAmount: _params.oneAmount,
          upperHint: _params.upperHint,
          lowerHint: _params.lowerHint,
          interestBatchManager: _params.batchManager,
          maxUpfrontFee: _params.maxUpfrontFee,
          // Add this contract as add/receive manager to be able to fully adjust trove,
          // while keeping the same management functionality
          addManager: address(this), // add manager
          removeManager: address(this), // remove manager
          receiver: address(this) // receiver for remove manager
         });
      troveId = borrowerOperations.openTroveAndJoinInterestBatchManager(
        openTroveAndJoinInterestBatchManagerParams
      );
    }

    oneToken.transfer(msg.sender, _params.oneAmount);

    // Set add/remove managers
    _setAddManager(troveId, _params.addManager);
    _setRemoveManagerAndReceiver(troveId, _params.removeManager, _params.receiver);

    return troveId;
  }

  function addCollWithRawETH(uint256 _troveId) external payable {
    address owner = troveNFT.ownerOf(_troveId);
    _requireSenderIsOwnerOrAddManager(_troveId, owner);
    // Convert ETH to WETH
    WETH.deposit{ value: msg.value }();

    borrowerOperations.addColl(_troveId, msg.value);
  }

  function withdrawCollToRawETH(uint256 _troveId, uint256 _amount) external {
    address owner = troveNFT.ownerOf(_troveId);
    address payable receiver =
      payable(_requireSenderIsOwnerOrRemoveManagerAndGetReceiver(_troveId, owner));
    _requireZapperIsReceiver(_troveId);

    borrowerOperations.withdrawColl(_troveId, _amount);

    // Convert WETH to ETH
    WETH.withdraw(_amount);
    (bool success,) = receiver.call{ value: _amount }("");
    require(success, "WZ: Sending ETH failed");
  }

  function withdrawOne(uint256 _troveId, uint256 _oneAmount, uint256 _maxUpfrontFee)
    external
  {
    address owner = troveNFT.ownerOf(_troveId);
    address receiver = _requireSenderIsOwnerOrRemoveManagerAndGetReceiver(_troveId, owner);
    _requireZapperIsReceiver(_troveId);

    borrowerOperations.withdrawOne(_troveId, _oneAmount, _maxUpfrontFee);

    // Send One
    oneToken.transfer(receiver, _oneAmount);
  }

  function repayOne(uint256 _troveId, uint256 _oneAmount) external {
    address owner = troveNFT.ownerOf(_troveId);
    _requireSenderIsOwnerOrAddManager(_troveId, owner);

    // Set initial balances to make sure there are not leftovers
    InitialBalances memory initialBalances;
    _setInitialTokensAndBalances(WETH, oneToken, initialBalances);

    // Pull One
    oneToken.transferFrom(msg.sender, address(this), _oneAmount);

    borrowerOperations.repayOne(_troveId, _oneAmount);

    // return leftovers to user
    _returnLeftovers(initialBalances);
  }

  function adjustTroveWithRawETH(
    uint256 _troveId,
    uint256 _collChange,
    bool _isCollIncrease,
    uint256 _oneChange,
    bool _isDebtIncrease,
    uint256 _maxUpfrontFee
  ) external payable {
    InitialBalances memory initialBalances;
    address payable receiver = _adjustTrovePre(
      _troveId, _collChange, _isCollIncrease, _oneChange, _isDebtIncrease, initialBalances
    );
    borrowerOperations.adjustTrove(
      _troveId, _collChange, _isCollIncrease, _oneChange, _isDebtIncrease, _maxUpfrontFee
    );
    _adjustTrovePost(
      _collChange, _isCollIncrease, _oneChange, _isDebtIncrease, receiver, initialBalances
    );
  }

  function adjustZombieTroveWithRawETH(
    uint256 _troveId,
    uint256 _collChange,
    bool _isCollIncrease,
    uint256 _oneChange,
    bool _isDebtIncrease,
    uint256 _upperHint,
    uint256 _lowerHint,
    uint256 _maxUpfrontFee
  ) external payable {
    InitialBalances memory initialBalances;
    address payable receiver = _adjustTrovePre(
      _troveId, _collChange, _isCollIncrease, _oneChange, _isDebtIncrease, initialBalances
    );
    borrowerOperations.adjustZombieTrove(
      _troveId,
      _collChange,
      _isCollIncrease,
      _oneChange,
      _isDebtIncrease,
      _upperHint,
      _lowerHint,
      _maxUpfrontFee
    );
    _adjustTrovePost(
      _collChange, _isCollIncrease, _oneChange, _isDebtIncrease, receiver, initialBalances
    );
  }

  function _adjustTrovePre(
    uint256 _troveId,
    uint256 _collChange,
    bool _isCollIncrease,
    uint256 _oneChange,
    bool _isDebtIncrease,
    InitialBalances memory _initialBalances
  ) internal returns (address payable) {
    if (_isCollIncrease) {
      require(_collChange == msg.value, "WZ: Wrong coll amount");
    } else {
      require(msg.value == 0, "WZ: Not adding coll, no ETH should be received");
    }

    address payable receiver = payable(
      _checkAdjustTroveManagers(_troveId, _collChange, _isCollIncrease, _isDebtIncrease)
    );

    // Set initial balances to make sure there are not leftovers
    _setInitialTokensAndBalances(WETH, oneToken, _initialBalances);

    // ETH -> WETH
    if (_isCollIncrease) {
      WETH.deposit{ value: _collChange }();
    }

    // Pull One
    if (!_isDebtIncrease) {
      oneToken.transferFrom(msg.sender, address(this), _oneChange);
    }

    return receiver;
  }

  function _adjustTrovePost(
    uint256 _collChange,
    bool _isCollIncrease,
    uint256 _oneChange,
    bool _isDebtIncrease,
    address payable _receiver,
    InitialBalances memory _initialBalances
  ) internal {
    // Send One
    if (_isDebtIncrease) {
      oneToken.transfer(_receiver, _oneChange);
    }

    // return ONE leftovers to user (trying to repay more than possible)
    uint256 currentOneBalance = oneToken.balanceOf(address(this));
    if (currentOneBalance > _initialBalances.balances[1]) {
      oneToken.transfer(
        _initialBalances.receiver, currentOneBalance - _initialBalances.balances[1]
      );
    }
    // There shouldn’t be Collateral leftovers, everything sent should end up in the trove
    // But ETH and WETH balance can be non-zero if someone accidentally send it to this
    // contract

    // WETH -> ETH
    if (!_isCollIncrease && _collChange > 0) {
      WETH.withdraw(_collChange);
      (bool success,) = _receiver.call{ value: _collChange }("");
      require(success, "WZ: Sending ETH failed");
    }
  }

  function closeTroveToRawETH(uint256 _troveId) external {
    address owner = troveNFT.ownerOf(_troveId);
    address payable receiver =
      payable(_requireSenderIsOwnerOrRemoveManagerAndGetReceiver(_troveId, owner));
    _requireZapperIsReceiver(_troveId);

    // pull One for repayment
    LatestTroveData memory trove = troveManager.getLatestTroveData(_troveId);
    oneToken.transferFrom(msg.sender, address(this), trove.entireDebt);

    borrowerOperations.closeTrove(_troveId);

    WETH.withdraw(trove.entireColl + ETH_GAS_COMPENSATION);
    (bool success,) = receiver.call{ value: trove.entireColl + ETH_GAS_COMPENSATION }("");
    require(success, "WZ: Sending ETH failed");
  }

  function closeTroveFromCollateral(
    uint256 _troveId,
    uint256 _flashLoanAmount,
    uint256 _minExpectedCollateral
  ) external override {
    address owner = troveNFT.ownerOf(_troveId);
    address payable receiver =
      payable(_requireSenderIsOwnerOrRemoveManagerAndGetReceiver(_troveId, owner));
    _requireZapperIsReceiver(_troveId);

    CloseTroveParams memory params = CloseTroveParams({
      troveId: _troveId,
      flashLoanAmount: _flashLoanAmount,
      minExpectedCollateral: _minExpectedCollateral,
      receiver: receiver
    });

    // Set initial balances to make sure there are not leftovers
    InitialBalances memory initialBalances;
    initialBalances.tokens[0] = WETH;
    initialBalances.tokens[1] = oneToken;
    _setInitialBalancesAndReceiver(initialBalances, receiver);

    // Flash loan coll
    flashLoanProvider.makeFlashLoan(
      WETH,
      _flashLoanAmount,
      IFlashLoanProvider.Operation.CloseTrove,
      false,
      abi.encode(params)
    );

    // return leftovers to user
    _returnLeftovers(initialBalances);
  }

  function receiveFlashLoanOnCloseTroveFromCollateral(
    CloseTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external {
    require(msg.sender == address(flashLoanProvider), "WZ: Caller not FlashLoan provider");

    LatestTroveData memory trove = troveManager.getLatestTroveData(_params.troveId);
    uint256 collLeft = trove.entireColl - _params.flashLoanAmount;
    require(
      collLeft >= _params.minExpectedCollateral, "WZ: Not enough collateral received"
    );

    // Swap Coll from flash loan to One, so we can repay and close trove
    // We swap the flash loan minus the flash loan fee
    exchange.swapToOne(_effectiveFlashLoanAmount, trove.entireDebt);

    // We asked for a min of entireDebt in swapToOne call above, so we don’t check again
    // here:
    // uint256 receivedOneAmount = exchange.swapToOne(_effectiveFlashLoanAmount,
    // trove.entireDebt);
    //require(receivedOneAmount >= trove.entireDebt, "WZ: Not enough ONE obtained to
    // repay");

    borrowerOperations.closeTrove(_params.troveId);

    // Send coll back to return flash loan
    WETH.transfer(address(flashLoanProvider), _params.flashLoanAmount);

    uint256 ethToSendBack = collLeft + ETH_GAS_COMPENSATION;
    // Send coll left and gas compensation
    WETH.withdraw(ethToSendBack);
    (bool success,) = _params.receiver.call{ value: ethToSendBack }("");
    require(success, "WZ: Sending ETH failed");
  }

  receive() external payable { }

  // Unimplemented flash loan receive functions for leverage
  function receiveFlashLoanOnOpenLeveragedTrove(
    ILeverageZapper.OpenLeveragedTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external virtual override { }
  function receiveFlashLoanOnLeverUpTrove(
    ILeverageZapper.LeverUpTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external virtual override { }
  function receiveFlashLoanOnLeverDownTrove(
    ILeverageZapper.LeverDownTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external virtual override { }
}

File 3 of 56 : 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 = 1 ether;

// Liquidation
uint256 constant MIN_LIQUIDATION_PENALTY_SP = 5e16; // 5%
uint256 constant MAX_LIQUIDATION_PENALTY_REDISTRIBUTION = 20e16; // 20%

// Collateral branch parameters (SETH = staked ETH, i.e. wstETH / rETH)
uint256 constant CCR_WETH = 150 * _1pct;
uint256 constant CCR_SETH = 160 * _1pct;

uint256 constant MCR_WETH = 110 * _1pct;
uint256 constant MCR_SETH = 120 * _1pct;

uint256 constant SCR_WETH = 110 * _1pct;
uint256 constant SCR_SETH = 120 * _1pct;

// Batch CR buffer (same for all branches for now)
// On top of MCR to join a batch, or adjust inside a batch
uint256 constant BCR_ALL = 10 * _1pct;

uint256 constant LIQUIDATION_PENALTY_SP_WETH = 5 * _1pct;
uint256 constant LIQUIDATION_PENALTY_SP_SETH = 5 * _1pct;

uint256 constant LIQUIDATION_PENALTY_REDISTRIBUTION_WETH = 10 * _1pct;
uint256 constant LIQUIDATION_PENALTY_REDISTRIBUTION_SETH = 20 * _1pct;

// Fraction of collateral awarded to liquidator
uint256 constant COLL_GAS_COMPENSATION_DIVISOR = 200; // dividing by 200 yields 0.5%

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

uint256 constant MIN_ANNUAL_INTEREST_RATE = _1pct / 2; // 0.5%
uint256 constant MAX_ANNUAL_INTEREST_RATE = 250 * _1pct;

// Batch management params
uint128 constant MAX_ANNUAL_BATCH_MANAGEMENT_FEE = uint128(_100pct / 10); // 10%
uint128 constant MIN_INTEREST_RATE_CHANGE_PERIOD = 1 hours; // only applies to batch
  // managers / batched Troves

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 6h. 6h = 360 min
// (1/2) = d^360 => d = (1/2)^(1/360)
uint256 constant REDEMPTION_MINUTE_DECAY_FACTOR = 998_076_443_575_628_800;

// 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 = 1;

// To prevent redemptions unless One depegs below 0.95 and allow the system to take off
uint256 constant INITIAL_BASE_RATE = _100pct; // 100% initial redemption rate

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

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 = 7 days;

uint256 constant SP_YIELD_SPLIT = 50 * _1pct; // 50%
uint256 constant STAKING_YIELD_SPLIT = 25 * _1pct; // 25%

uint256 constant MIN_ONE_IN_SP = 1e18;

// 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;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IZapper.sol";

interface ILeverageZapper is IZapper {
  struct OpenLeveragedTroveParams {
    address owner;
    uint256 ownerIndex;
    uint256 collAmount;
    uint256 flashLoanAmount;
    uint256 oneAmount;
    uint256 upperHint;
    uint256 lowerHint;
    uint256 annualInterestRate;
    address batchManager;
    uint256 maxUpfrontFee;
    address addManager;
    address removeManager;
    address receiver;
  }

  struct LeverUpTroveParams {
    uint256 troveId;
    uint256 flashLoanAmount;
    uint256 oneAmount;
    uint256 maxUpfrontFee;
  }

  struct LeverDownTroveParams {
    uint256 troveId;
    uint256 flashLoanAmount;
    uint256 minOneAmount;
  }

  function openLeveragedTroveWithRawETH(OpenLeveragedTroveParams calldata _params)
    external
    payable;

  function leverUpTrove(LeverUpTroveParams calldata _params) external;

  function leverDownTrove(LeverDownTroveParams calldata _params) external;

  function leverageRatioToCollateralRatio(uint256 _inputRatio)
    external
    pure
    returns (uint256);
}

// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

import "../Interfaces/IWETH.sol";
import "../Interfaces/IAddressesRegistry.sol";
import "../Interfaces/IBorrowerOperations.sol";
import "../Dependencies/AddRemoveManagers.sol";
import "./LeftoversSweep.sol";
import "./Interfaces/IFlashLoanProvider.sol";
import "./Interfaces/IFlashLoanReceiver.sol";
import "./Interfaces/IExchange.sol";
import "./Interfaces/IZapper.sol";

abstract contract BaseZapper is
  AddRemoveManagers,
  LeftoversSweep,
  IFlashLoanReceiver,
  IZapper
{
  IBorrowerOperations public immutable borrowerOperations; // LST branch (i.e., not WETH
    // as collateral)
  ITroveManager public immutable troveManager;
  IWETH public immutable WETH;
  IERC20 public immutable USDC;
  IOneToken public immutable oneToken;

  IFlashLoanProvider public immutable flashLoanProvider;
  IExchange public immutable exchange;

  constructor(
    IAddressesRegistry _addressesRegistry,
    IFlashLoanProvider _flashLoanProvider,
    IExchange _exchange,
    address _USDC
  ) AddRemoveManagers(_addressesRegistry) LeftoversSweep(IERC20(_USDC)) {
    borrowerOperations = _addressesRegistry.borrowerOperations();
    troveManager = _addressesRegistry.troveManager();
    oneToken = _addressesRegistry.oneToken();
    WETH = _addressesRegistry.WETH();
    USDC = IERC20(_USDC);

    flashLoanProvider = _flashLoanProvider;
    exchange = _exchange;
  }

  function _getTroveIndex(address _sender, uint256 _ownerIndex)
    internal
    pure
    returns (uint256)
  {
    return uint256(keccak256(abi.encode(_sender, _ownerIndex)));
  }

  function _getTroveIndex(uint256 _ownerIndex) internal view returns (uint256) {
    return _getTroveIndex(msg.sender, _ownerIndex);
  }

  function _requireZapperIsReceiver(uint256 _troveId) internal view {
    (, address receiver) = borrowerOperations.removeManagerReceiverOf(_troveId);
    require(receiver == address(this), "BZ: Zapper is not receiver for this trove");
  }

  function _checkAdjustTroveManagers(
    uint256 _troveId,
    uint256 _collChange,
    bool _isCollIncrease,
    bool _isDebtIncrease
  ) internal view returns (address) {
    address owner = troveNFT.ownerOf(_troveId);
    address receiver = owner;

    if ((!_isCollIncrease && _collChange > 0) || _isDebtIncrease) {
      receiver = _requireSenderIsOwnerOrRemoveManagerAndGetReceiver(_troveId, owner);
      _requireZapperIsReceiver(_troveId);
    } else {
      // RemoveManager assumes AddManager, so if the former is set, there's no need to
      // check the latter
      _requireSenderIsOwnerOrAddManager(_troveId, owner);
      // No need to check the type of trove change for two reasons:
      // - If the check above fails, it means sender is not owner, nor AddManager, nor
      // RemoveManager.
      //   An independent 3rd party should not be allowed here.
      // - If it's not collIncrease or debtDecrease, _requireNonZeroAdjustment would
      // revert
    }

    return receiver;
  }
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IFlashLoanProvider.sol";
import "./IExchange.sol";

interface IZapper {
  struct OpenTroveParams {
    address owner;
    uint256 ownerIndex;
    uint256 collAmount;
    uint256 oneAmount;
    uint256 upperHint;
    uint256 lowerHint;
    uint256 annualInterestRate;
    address batchManager;
    uint256 maxUpfrontFee;
    address addManager;
    address removeManager;
    address receiver;
  }

  struct CloseTroveParams {
    uint256 troveId;
    uint256 flashLoanAmount;
    uint256 minExpectedCollateral;
    address receiver;
  }

  function flashLoanProvider() external view returns (IFlashLoanProvider);

  function exchange() external view returns (IExchange);

  function openTroveWithRawETH(OpenTroveParams calldata _params)
    external
    payable
    returns (uint256);

  function closeTroveFromCollateral(
    uint256 _troveId,
    uint256 _flashLoanAmount,
    uint256 _minExpectedCollateral
  ) external;
}

// 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;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IActivePool.sol";
import "./IOneToken.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;
    IOneToken oneToken;
    IWETH WETH;
  }

  function CCR() external returns (uint256);
  function SCR() external returns (uint256);
  function MCR() external returns (uint256);
  function BCR() 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 oneToken() external view returns (IOneToken);
  function WETH() external returns (IWETH);
  function stakingPoolAddress() external view returns (address);

  function setAddresses(AddressVars memory _vars, address _stakingPoolAddress) external;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./ILiquityBase.sol";
import "./IAddRemoveManagers.sol";
import "./IOneToken.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 _oneAmount,
    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 oneAmount;
    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 withdrawOne(uint256 _troveId, uint256 _amount, uint256 _maxUpfrontFee)
    external;

  function repayOne(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 _oneChange,
    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 kickFromBatch(uint256 _troveId, uint256 _upperHint, uint256 _lowerHint)
    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;
}

// SPDX-License-Identifier: BUSL-1.1

pragma solidity 0.8.24;

import "../Interfaces/IAddRemoveManagers.sol";
import "../Interfaces/IAddressesRegistry.sol";
import "../Interfaces/ITroveNFT.sol";

contract AddRemoveManagers is IAddRemoveManagers {
  ITroveNFT internal immutable troveNFT;

  struct RemoveManagerReceiver {
    address manager;
    address receiver;
  }

  /*
  * Mapping from TroveId to granted address for operations that "give" money to the trove
  (add collateral, pay debt).
     * Useful for instance for cold/hot wallet setups.
  * If its value is zero address, any address is allowed to do those operations on behalf
  of trove owner.
  * Otherwise, only the address in this mapping (and the trove owner) will be allowed.
     * To restrict this permission to no one, trove owner should be set in this mapping.
     */
  mapping(uint256 => address) public addManagerOf;

  /*
  * Mapping from TroveId to granted addresses for operations that "withdraw" money from
  the trove (withdraw collateral, borrow),
  * and for each of those addresses another address for the receiver of those withdrawn
  funds.
     * Useful for instance for cold/hot wallet setups or for automations.
     * Only the address in this mapping, if any, and the trove owner, will be allowed.
     * Therefore, by default this permission is restricted to no one.
     * If the receiver is zero, the owner is assumed as the receiver.
     * RemoveManager also assumes AddManager permission
     */
  mapping(uint256 => RemoveManagerReceiver) public removeManagerReceiverOf;

  error EmptyManager();
  error NotBorrower();
  error NotOwnerNorAddManager();
  error NotOwnerNorRemoveManager();

  event TroveNFTAddressChanged(address _newTroveNFTAddress);
  event AddManagerUpdated(uint256 indexed _troveId, address _newAddManager);
  event RemoveManagerAndReceiverUpdated(
    uint256 indexed _troveId, address _newRemoveManager, address _newReceiver
  );

  constructor(IAddressesRegistry _addressesRegistry) {
    troveNFT = _addressesRegistry.troveNFT();
    emit TroveNFTAddressChanged(address(troveNFT));
  }

  function setAddManager(uint256 _troveId, address _manager) external {
    _requireCallerIsBorrower(_troveId);
    _setAddManager(_troveId, _manager);
  }

  function _setAddManager(uint256 _troveId, address _manager) internal {
    addManagerOf[_troveId] = _manager;
    emit AddManagerUpdated(_troveId, _manager);
  }

  function setRemoveManager(uint256 _troveId, address _manager) external {
    setRemoveManagerWithReceiver(_troveId, _manager, troveNFT.ownerOf(_troveId));
  }

  function setRemoveManagerWithReceiver(
    uint256 _troveId,
    address _manager,
    address _receiver
  ) public {
    _requireCallerIsBorrower(_troveId);
    _setRemoveManagerAndReceiver(_troveId, _manager, _receiver);
  }

  function _setRemoveManagerAndReceiver(
    uint256 _troveId,
    address _manager,
    address _receiver
  ) internal {
    _requireNonZeroManagerUnlessWiping(_manager, _receiver);
    removeManagerReceiverOf[_troveId].manager = _manager;
    removeManagerReceiverOf[_troveId].receiver = _receiver;
    emit RemoveManagerAndReceiverUpdated(_troveId, _manager, _receiver);
  }

  function _wipeAddRemoveManagers(uint256 _troveId) internal {
    delete addManagerOf[_troveId];
    delete removeManagerReceiverOf[_troveId];
    emit AddManagerUpdated(_troveId, address(0));
    emit RemoveManagerAndReceiverUpdated(_troveId, address(0), address(0));
  }

  function _requireNonZeroManagerUnlessWiping(address _manager, address _receiver)
    internal
    pure
  {
    if (_manager == address(0) && _receiver != address(0)) {
      revert EmptyManager();
    }
  }

  function _requireCallerIsBorrower(uint256 _troveId) internal view {
    if (msg.sender != troveNFT.ownerOf(_troveId)) {
      revert NotBorrower();
    }
  }

  function _requireSenderIsOwnerOrAddManager(uint256 _troveId, address _owner)
    internal
    view
  {
    address addManager = addManagerOf[_troveId];
    if (msg.sender != _owner && addManager != address(0) && msg.sender != addManager) {
      // RemoveManager assumes AddManager permission too
      address removeManager = removeManagerReceiverOf[_troveId].manager;
      if (msg.sender != removeManager) {
        revert NotOwnerNorAddManager();
      }
    }
  }

  function _requireSenderIsOwnerOrRemoveManagerAndGetReceiver(
    uint256 _troveId,
    address _owner
  ) internal view returns (address) {
    address manager = removeManagerReceiverOf[_troveId].manager;
    address receiver = removeManagerReceiverOf[_troveId].receiver;
    if (msg.sender != _owner && msg.sender != manager) {
      revert NotOwnerNorRemoveManager();
    }
    if (receiver == address(0) || msg.sender != manager) {
      return _owner;
    }
    return receiver;
  }
}

// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";

import { ERC20StandardizedWrapper } from "../Sonata/ERC20StandardizedWrapper.sol";
import "../Interfaces/IOneToken.sol";

contract LeftoversSweep {
  using SafeERC20 for IERC20;

  IERC20 private immutable USDC;

  constructor(IERC20 _usdc) {
    USDC = _usdc;
  }

  struct InitialBalances {
    IERC20[4] tokens; // paving the way for completely dynamic routes
    uint256[4] balances;
    bool[4] isStandardized;
    address receiver;
  }

  function _setInitialTokensAndBalances(
    IERC20 _collToken,
    IOneToken _oneToken,
    InitialBalances memory _initialBalances
  ) internal view {
    _setInitialTokensBalancesAndReceiver(
      _collToken, _oneToken, _initialBalances, msg.sender
    );
  }

  function _setInitialTokensBalancesAndReceiver(
    IERC20 _collToken,
    IOneToken _oneToken,
    InitialBalances memory _initialBalances,
    address _receiver
  ) internal view virtual {
    _initialBalances.tokens[0] = _collToken;
    _initialBalances.tokens[1] = _oneToken;
    _initialBalances.tokens[2] = USDC;
    _setInitialBalancesAndReceiver(_initialBalances, _receiver);
  }

  function _setInitialBalances(InitialBalances memory _initialBalances) internal view {
    _setInitialBalancesAndReceiver(_initialBalances, msg.sender);
  }

  function _setInitialBalancesAndReceiver(
    InitialBalances memory _initialBalances,
    address _receiver
  ) internal view {
    for (uint256 i = 0; i < _initialBalances.tokens.length; i++) {
      if (address(_initialBalances.tokens[i]) == address(0)) break;

      _initialBalances.balances[i] = _initialBalances.tokens[i].balanceOf(address(this));
    }
    _initialBalances.receiver = _receiver;
  }

  function _returnLeftovers(InitialBalances memory _initialBalances) internal {
    IERC20 token;
    for (uint256 i = 0; i < _initialBalances.tokens.length; i++) {
      token = _initialBalances.tokens[i];
      if (address(token) == address(0)) break;

      uint256 currentBalance = token.balanceOf(address(this));
      if (currentBalance <= _initialBalances.balances[i]) continue;

      if (_initialBalances.isStandardized[i]) {
        ERC20StandardizedWrapper(address(token)).unwrapTo(
          _initialBalances.receiver, currentBalance - _initialBalances.balances[i]
        );
      } else {
        token.safeTransfer(
          _initialBalances.receiver, currentBalance - _initialBalances.balances[i]
        );
      }
    }
  }
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "./ILeverageZapper.sol";
import "./IFlashLoanReceiver.sol";

interface IFlashLoanProvider {
  enum Operation {
    OpenTrove,
    CloseTrove,
    LeverUpTrove,
    LeverDownTrove
  }

  function receiver() external view returns (IFlashLoanReceiver);

  function makeFlashLoan(
    IERC20 _token,
    uint256 _amount,
    Operation _operation,
    bool _isStandardizedWrapper,
    bytes calldata userData
  ) external;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IZapper.sol";
import "./ILeverageZapper.sol";

interface IFlashLoanReceiver {
  function receiveFlashLoanOnOpenLeveragedTrove(
    ILeverageZapper.OpenLeveragedTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external;
  function receiveFlashLoanOnLeverUpTrove(
    ILeverageZapper.LeverUpTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external;
  function receiveFlashLoanOnLeverDownTrove(
    ILeverageZapper.LeverDownTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external;
  function receiveFlashLoanOnCloseTroveFromCollateral(
    IZapper.CloseTroveParams calldata _params,
    uint256 _effectiveFlashLoanAmount
  ) external;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IExchange {
  function swapFromOne(uint256 _oneAmount, uint256 _minCollAmount) external;

  function swapToOne(uint256 _collAmount, uint256 _minOneAmount)
    external
    returns (uint256);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
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);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IInterestRouter.sol";
import "./IOneRewardsReceiver.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 (IOneRewardsReceiver);

  function getCollBalance() external view returns (uint256);
  function getOneDebt() 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 calcPendingSTAStakingYield() 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;
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

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

interface IOneToken is IERC20Metadata, IERC20Permit, 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;

  function setDebtCapPerBranch(address _borrowerOperationAddress, uint256 _capOneDebt)
    external;
}

// 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;
}

// 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 getOneDebt() external view returns (uint256);
  function sendCollToActivePool(uint256 _amount) external;
  function receiveColl(uint256 _amount) external;

  function increaseOneDebt(uint256 _amount) external;
  function decreaseOneDebt(uint256 _amount) external;
}

// 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);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IMultiTroveGetter {
  struct CombinedTroveData {
    uint256 id;
    uint256 entireDebt;
    uint256 entireColl;
    uint256 redistOneDebtGain;
    uint256 redistCollGain;
    uint256 accruedInterest;
    uint256 recordedDebt;
    uint256 annualInterestRate;
    uint256 accruedBatchManagementFee;
    uint256 lastInterestRateAdjTime;
    uint256 stake;
    uint256 lastDebtUpdateTime;
    address interestBatchManager;
    uint256 batchDebtShares;
    uint256 snapshotETH;
    uint256 snapshotOneDebt;
  }

  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);
}

// 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);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IActivePool.sol";
import "./ILiquityBase.sol";
import "./IOneToken.sol";
import "./ITroveManager.sol";
import "./IOneRewardsReceiver.sol";

/*
 * The Stability Pool holds One tokens deposited by Stability Pool depositors.
 *
* When a trove is liquidated, then depending on system conditions, some of its One debt
gets offset with
* One in the Stability Pool:  that is, the offset debt evaporates, and an equal amount of
One tokens in the Stability Pool is burned.
 *
* Thus, a liquidation causes each depositor to receive a One 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 One 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, IOneRewardsReceiver {
  function oneToken() external view returns (IOneToken);
  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 ONE 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 One 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 One held in the pool. Changes when users deposit/withdraw, and when Trove debt
  is offset.
     */
  function getTotalOneDeposits() 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 ONE 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 getCompoundedOneDeposit(address _depositor) external view returns (uint256);

  function scaleToS(uint256 _scale) external view returns (uint256);

  function scaleToB(uint256 _scale) external view returns (uint256);

  function P() external view returns (uint256);
  function currentScale() external view returns (uint256);

  function P_PRECISION() external view returns (uint256);
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./ILiquityBase.sol";
import "./ITroveNFT.sol";
import "./IBorrowerOperations.sol";
import "./IStabilityPool.sol";
import "./IOneToken.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 oneToken() external view returns (IOneToken);
  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 oneDebt);

  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 _oneAmount,
    uint256 _price,
    uint256 _redemptionRate,
    uint256 _maxIterations
  ) external returns (uint256 _redemeedAmount);

  function shutdown() external;
  function urgentRedemption(
    uint256 _oneAmount,
    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 --

  function COLL_GAS_COMPENSATION_CAP() external view returns (uint256);
}

// 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;
}

//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "Solady/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 "../Interfaces/ITroveManager.sol";

interface IMetadataNFT {
  struct TroveData {
    uint256 _tokenId;
    address _owner;
    address _collToken;
    address _oneToken;
    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;

  constructor(FixedAssetReader _assetReader) {
    assetReader = _assetReader;
  }

  function uri(TroveData memory _troveData) public view returns (string memory) {
    string memory attr = attributes(_troveData);
    return json.formattedMetadata(
      string.concat("Sonata - ", IERC20Metadata(_troveData._collToken).name()),
      string.concat(
        "Sonata is a collateralized debt platform. Users can lock up ",
        IERC20Metadata(_troveData._collToken).symbol(),
        " to issue stablecoin tokens (ONE) to their own Ethereum address. The individual collateralized debt positions are called Troves, and are represented as NFTs."
      ),
      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._oneToken),
      '"}, {"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 "";
  }
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

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

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

  function redeemCollateral(
    uint256 _oneamount,
    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 oneToken() external view returns (IOneToken);

  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 getEffectiveRedemptionFeeInOne(uint256 _redeemAmount)
    external
    view
    returns (uint256);
}

File 28 of 56 : IInterestRouter.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

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

// 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);
}

// 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 getEntireBranchDebt() external view returns (uint256);
  function getEntireBranchColl() external view returns (uint256);
}

// 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);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
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 amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

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

    /**
     * @dev Moves `amount` 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 amount) 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 `amount` 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 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` 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 amount) external returns (bool);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using Address for address;

    /**
     * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    /**
     * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
     * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
     */
    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    /**
     * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 oldAllowance = token.allowance(address(this), spender);
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
    }

    /**
     * @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
        }
    }

    /**
     * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
     * to be set to zero before setting it to a non-zero value, such as USDT.
     */
    function forceApprove(IERC20 token, address spender, uint256 value) internal {
        bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);

        if (!_callOptionalReturnBool(token, approvalCall)) {
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
            _callOptionalReturn(token, approvalCall);
        }
    }

    /**
     * @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
     * Revert on invalid signature.
     */
    function safePermit(
        IERC20Permit token,
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        uint256 nonceBefore = token.nonces(owner);
        token.permit(owner, spender, value, deadline, v, r, s);
        uint256 nonceAfter = token.nonces(owner);
        require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
     */
    function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
        // and not revert is the subcall reverts.

        (bool success, bytes memory returndata) = address(token).call(data);
        return
            success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
    }
}

// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

import { ERC20 } from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {
  SafeERC20,
  IERC20
} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";

contract ERC20StandardizedWrapper is ERC20 {
  using SafeERC20 for IERC20;

  IERC20 public immutable INPUT_TOKEN;
  uint256 public immutable DECIMALS_SCALE;

  constructor(address _inputToken)
    ERC20(
      string.concat("Sonata Standardized ", ERC20(_inputToken).name()),
      string.concat("WSTA-", ERC20(_inputToken).symbol())
    )
  {
    INPUT_TOKEN = IERC20(_inputToken);
    uint8 inputTokenDecimals = ERC20(_inputToken).decimals();

    require(
      inputTokenDecimals < 18,
      "ERC20StandardizedWrapper: Input token decimals must be less than 18"
    );

    DECIMALS_SCALE = 10 ** (18 - inputTokenDecimals);
  }

  // @dev We use ether format to keep a standard on the implementation.
  function wrap(uint256 _etherAmount) external returns (uint256 received_) {
    uint256 scaleToTokenInputDecimals = _etherAmount / DECIMALS_SCALE;

    INPUT_TOKEN.safeTransferFrom(msg.sender, address(this), scaleToTokenInputDecimals);

    received_ = scaleToTokenInputDecimals * DECIMALS_SCALE;
    // @dev We scale it back, to avoid input manipulation.
    _mint(msg.sender, received_);

    return received_;
  }

  function unwrap(uint256 _etherAmount) external returns (uint256) {
    return _unwrap(msg.sender, _etherAmount);
  }

  function unwrapTo(address _receiver, uint256 _etherAmount) external returns (uint256) {
    return _unwrap(_receiver, _etherAmount);
  }

  function _unwrap(address _receiver, uint256 _etherAmount)
    internal
    returns (uint256 received_)
  {
    _burn(msg.sender, _etherAmount / DECIMALS_SCALE * DECIMALS_SCALE);

    received_ = _etherAmount / DECIMALS_SCALE;
    INPUT_TOKEN.safeTransfer(_receiver, received_);

    return received_;
  }

  function etherToTokenDecimals(uint256 _etherAmount) external view returns (uint256) {
    return _etherAmount / DECIMALS_SCALE;
  }

  function removeDustOnAmount(uint256 _wad) external view returns (uint256) {
    return _wad / DECIMALS_SCALE * DECIMALS_SCALE;
  }

  function tokenDecimalsToEther(uint256 _tokenDecimals) external view returns (uint256) {
    return _tokenDecimals * DECIMALS_SCALE;
  }

  function isStandardized() external pure returns (bool) {
    return true;
  }
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IOneRewardsReceiver {
  function triggerOneRewards(uint256 _oneYield) external;
}

File 36 of 56 : TroveChange.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

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

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (token/ERC20/extensions/IERC20Permit.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 *
 * ==== Security Considerations
 *
 * There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
 * expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
 * considered as an intention to spend the allowance in any specific way. The second is that because permits have
 * built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
 * take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
 * generally recommended is:
 *
 * ```solidity
 * function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
 *     try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
 *     doThing(..., value);
 * }
 *
 * function doThing(..., uint256 value) public {
 *     token.safeTransferFrom(msg.sender, address(this), value);
 *     ...
 * }
 * ```
 *
 * Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
 * `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
 * {SafeERC20-safeTransferFrom}).
 *
 * Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
 * contracts should have entry points that don't rely on permit.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     *
     * CAUTION: See Security Considerations above.
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    /**
     * @dev Returns the current nonce for `owner`. This value must be
     * included whenever a signature is generated for {permit}.
     *
     * Every successful call to {permit} increases ``owner``'s nonce by one. This
     * prevents a signature from being used multiple times.
     */
    function nonces(address owner) external view returns (uint256);

    /**
     * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}

File 38 of 56 : IERC5267.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (interfaces/IERC5267.sol)

pragma solidity ^0.8.0;

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
        );
}

// 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 40 of 56 : LatestTroveData.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

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

File 41 of 56 : LatestBatchData.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

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

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/extensions/IERC721Metadata.sol)

pragma solidity ^0.8.0;

import "../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);
}

// 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))
            let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
            extcodecopy(pointer, add(data, 0x1f), start, add(l, 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.
        }
    }
}

//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;
  }
}

//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(),
      _oneLogo(_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 _oneLogo(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("ONE")))
          )
        )
      )
    );
  }

  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)
    );
  }
}

//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))
      )
    );
  }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     *
     * Furthermore, `isContract` will also return true if the target contract within
     * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
     * which only has an effect at the end of a transaction.
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
     * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
     *
     * _Available since v4.8._
     */
    function verifyCallResultFromTarget(
        address target,
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        if (success) {
            if (returndata.length == 0) {
                // only check isContract if the call was successful and the return data is empty
                // otherwise we already know that it was a contract
                require(isContract(target), "Address: call to non-contract");
            }
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    /**
     * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason or using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    function _revert(bytes memory returndata, string memory errorMessage) private pure {
        // Look for revert reason and bubble it up if present
        if (returndata.length > 0) {
            // The easiest way to bubble the revert reason is using memory via assembly
            /// @solidity memory-safe-assembly
            assembly {
                let returndata_size := mload(returndata)
                revert(add(32, returndata), returndata_size)
            }
        } else {
            revert(errorMessage);
        }
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/ERC20.sol)

pragma solidity ^0.8.0;

import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20PresetMinterPauser}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * The default value of {decimals} is 18. To change this, you should override
 * this function so it returns a different value.
 *
 * We have followed general OpenZeppelin Contracts guidelines: functions revert
 * instead returning `false` on failure. This behavior is nonetheless
 * conventional and does not conflict with the expectations of ERC20
 * applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
contract ERC20 is Context, IERC20, IERC20Metadata {
    mapping(address => uint256) private _balances;

    mapping(address => mapping(address => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;

    /**
     * @dev Sets the values for {name} and {symbol}.
     *
     * All two of these values are immutable: they can only be set once during
     * construction.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5.05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the default value returned by this function, unless
     * it's overridden.
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view virtual override returns (uint8) {
        return 18;
    }

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address to, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     *
     * Requirements:
     *
     * - `from` and `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     * - the caller must have allowance for ``from``'s tokens of at least
     * `amount`.
     */
    function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, amount);
        _transfer(from, to, amount);
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, allowance(owner, spender) + addedValue);
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        address owner = _msgSender();
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(owner, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    /**
     * @dev Moves `amount` of tokens from `from` to `to`.
     *
     * This internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     */
    function _transfer(address from, address to, uint256 amount) internal virtual {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
            // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
            // decrementing then incrementing.
            _balances[to] += amount;
        }

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply += amount;
        unchecked {
            // Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
            _balances[account] += amount;
        }
        emit Transfer(address(0), account, amount);

        _afterTokenTransfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance - amount;
            // Overflow not possible: amount <= accountBalance <= totalSupply.
            _totalSupply -= amount;
        }

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address owner, address spender, uint256 amount) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    /**
     * @dev Updates `owner` s allowance for `spender` based on spent `amount`.
     *
     * Does not update the allowance amount in case of infinite allowance.
     * Revert if not enough allowance is available.
     *
     * Might emit an {Approval} event.
     */
    function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "ERC20: insufficient allowance");
            unchecked {
                _approve(owner, spender, currentAllowance - amount);
            }
        }
    }

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}

    /**
     * @dev Hook that is called after any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * has been transferred to `to`.
     * - when `from` is zero, `amount` tokens have been minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 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 ERC721 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 ERC721
     * 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 caller.
     *
     * 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 50 of 56 : 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, '" ');
    }
  }
}

//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "Solady/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);
  }
}

//SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "Solady/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];
    }
  }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * 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[EIP 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 55 of 56 : 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 Returns the uint8 at index `i`. If out-of-bounds, returns 0.
    function uint8At(StringStorage storage $, uint256 i) internal view returns (uint8) {
        return LibBytes.uint8At(bytesStorage($), i);
    }

    /// @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 an 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 Returns 0 if `a == b`, -1 if `a < b`, +1 if `a > b`.
    /// If `a` == b[:a.length]`, and `a.length < b.length`, returns -1.
    function cmp(string memory a, string memory b) internal pure returns (int256) {
        return LibBytes.cmp(bytes(a), bytes(b));
    }

    /// @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 {
        /// @solidity memory-safe-assembly
        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 56 of 56 : 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(or(packed, 0xff), packed)) {
                    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.
        }
    }

    /// @dev Returns the uint8 at index `i`. If out-of-bounds, returns 0.
    function uint8At(BytesStorage storage $, uint256 i) internal view returns (uint8 result) {
        /// @solidity memory-safe-assembly
        assembly {
            for { let packed := sload($.slot) } 1 {} {
                if iszero(eq(or(packed, 0xff), packed)) {
                    if iszero(gt(i, 0x1e)) {
                        result := byte(i, packed)
                        break
                    }
                    if iszero(gt(i, and(0xff, packed))) {
                        mstore(0x00, $.slot)
                        let j := sub(i, 0x1f)
                        result := byte(and(j, 0x1f), sload(add(keccak256(0x00, 0x20), shr(5, j))))
                    }
                    break
                }
                if iszero(gt(i, shr(8, packed))) {
                    mstore(0x00, $.slot)
                    result := byte(and(i, 0x1f), sload(add(keccak256(0x00, 0x20), shr(5, i))))
                }
                break
            }
        }
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                      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, starting from `from`. Optimized for byte needles.
    /// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
    function indexOfByte(bytes memory subject, bytes1 needle, uint256 from)
        internal
        pure
        returns (uint256 result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result := not(0) // Initialize to `NOT_FOUND`.
            if gt(mload(subject), from) {
                let start := add(subject, 0x20)
                let end := add(start, mload(subject))
                let m := div(not(0), 255) // `0x0101 ... `.
                let h := mul(byte(0, needle), m) // Replicating needle mask.
                m := not(shl(7, m)) // `0x7f7f ... `.
                for { let i := add(start, from) } 1 {} {
                    let c := xor(mload(i), h) // Load 32-byte chunk and xor with mask.
                    c := not(or(or(add(and(c, m), m), c), m)) // Each needle byte will be `0x80`.
                    if c {
                        c := and(not(shr(shl(3, sub(end, i)), not(0))), c) // Truncate bytes past the end.
                        if c {
                            let r := shl(7, lt(0x8421084210842108cc6318c6db6d54be, c)) // Save bytecode.
                            r := or(shl(6, lt(0xffffffffffffffff, shr(r, c))), r)
                            // forgefmt: disable-next-item
                            result := add(sub(i, start), shr(3, xor(byte(and(0x1f, shr(byte(24,
                                mul(0x02040810204081, shr(r, c))), 0x8421084210842108cc6318c6db6d54be)),
                                0xc0c8c8d0c8e8d0d8c8e8e0e8d0d8e0f0c8d0e8d0e0e0d8f0d0d0e0d8f8f8f8f8), r)))
                            break
                        }
                    }
                    i := add(i, 0x20)
                    if iszero(lt(i, end)) { break }
                }
            }
        }
    }

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

    /// @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 Returns a copy of `subject` sliced from `start` to `end` (exclusive).
    /// `start` and `end` are byte offsets. Faster than Solidity's native slicing.
    function sliceCalldata(bytes calldata subject, uint256 start, uint256 end)
        internal
        pure
        returns (bytes calldata result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            end := xor(end, mul(xor(end, subject.length), lt(subject.length, end)))
            start := xor(start, mul(xor(start, subject.length), lt(subject.length, start)))
            result.offset := add(subject.offset, start)
            result.length := mul(lt(start, end), sub(end, start))
        }
    }

    /// @dev Returns a copy of `subject` sliced from `start` to the end of the bytes.
    /// `start` is a byte offset. Faster than Solidity's native slicing.
    function sliceCalldata(bytes calldata subject, uint256 start)
        internal
        pure
        returns (bytes calldata result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            start := xor(start, mul(xor(start, subject.length), lt(subject.length, start)))
            result.offset := add(subject.offset, start)
            result.length := mul(lt(start, subject.length), sub(subject.length, start))
        }
    }

    /// @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 an 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 Returns 0 if `a == b`, -1 if `a < b`, +1 if `a > b`.
    /// If `a` == b[:a.length]`, and `a.length < b.length`, returns -1.
    function cmp(bytes memory a, bytes memory b) internal pure returns (int256 result) {
        /// @solidity memory-safe-assembly
        assembly {
            let aLen := mload(a)
            let bLen := mload(b)
            let n := and(xor(aLen, mul(xor(aLen, bLen), lt(bLen, aLen))), not(0x1f))
            if n {
                for { let i := 0x20 } 1 {} {
                    let x := mload(add(a, i))
                    let y := mload(add(b, i))
                    if iszero(or(xor(x, y), eq(i, n))) {
                        i := add(i, 0x20)
                        continue
                    }
                    result := sub(gt(x, y), lt(x, y))
                    break
                }
            }
            // forgefmt: disable-next-item
            if iszero(result) {
                let l := 0x201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a090807060504030201
                let x := and(mload(add(add(a, 0x20), n)), shl(shl(3, byte(sub(aLen, n), l)), not(0)))
                let y := and(mload(add(add(b, 0x20), n)), shl(shl(3, byte(sub(bLen, n), l)), not(0)))
                result := sub(gt(x, y), lt(x, y))
                if iszero(result) { result := sub(gt(aLen, bLen), lt(aLen, bLen)) }
            }
        }
    }

    /// @dev Directly returns `a` without copying.
    function directReturn(bytes memory a) internal pure {
        /// @solidity memory-safe-assembly
        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 Directly returns `a` with minimal copying.
    function directReturn(bytes[] memory a) internal pure {
        /// @solidity memory-safe-assembly
        assembly {
            let n := mload(a) // `a.length`.
            let o := add(a, 0x20) // Start of elements in `a`.
            let u := a // Highest memory slot.
            let w := not(0x1f)
            for { let i := 0 } iszero(eq(i, n)) { i := add(i, 1) } {
                let c := add(o, shl(5, i)) // Location of pointer to `a[i]`.
                let s := mload(c) // `a[i]`.
                let l := mload(s) // `a[i].length`.
                let r := and(l, 0x1f) // `a[i].length % 32`.
                let z := add(0x20, and(l, w)) // Offset of last word in `a[i]` from `s`.
                // If `s` comes before `o`, or `s` is not zero right padded.
                if iszero(lt(lt(s, o), or(iszero(r), iszero(shl(shl(3, r), mload(add(s, z))))))) {
                    let m := mload(0x40)
                    mstore(m, l) // Copy `a[i].length`.
                    for {} 1 {} {
                        mstore(add(m, z), mload(add(s, z))) // Copy `a[i]`, backwards.
                        z := add(z, w) // `sub(z, 0x20)`.
                        if iszero(z) { break }
                    }
                    let e := add(add(m, 0x20), l)
                    mstore(e, 0) // Zeroize the slot after the copied bytes.
                    mstore(0x40, add(e, 0x20)) // Allocate memory.
                    s := m
                }
                mstore(c, sub(s, o)) // Convert to calldata offset.
                let t := add(l, add(s, 0x20))
                if iszero(lt(t, u)) { u := t }
            }
            let retStart := add(a, w) // Assumes `a` doesn't start from scratch space.
            mstore(retStart, 0x20) // Store the return offset.
            return(retStart, add(0x40, sub(u, retStart))) // End the transaction.
        }
    }

    /// @dev Returns the word at `offset`, without any bounds checks.
    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.
    function loadCalldata(bytes calldata a, uint256 offset)
        internal
        pure
        returns (bytes32 result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            result := calldataload(add(a.offset, offset))
        }
    }

    /// @dev Returns a slice representing a static struct in the calldata. Performs bounds checks.
    function staticStructInCalldata(bytes calldata a, uint256 offset)
        internal
        pure
        returns (bytes calldata result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let l := sub(a.length, 0x20)
            result.offset := add(a.offset, offset)
            result.length := sub(a.length, offset)
            if or(shr(64, or(l, a.offset)), gt(offset, l)) { revert(l, 0x00) }
        }
    }

    /// @dev Returns a slice representing a dynamic struct in the calldata. Performs bounds checks.
    function dynamicStructInCalldata(bytes calldata a, uint256 offset)
        internal
        pure
        returns (bytes calldata result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let l := sub(a.length, 0x20)
            let s := calldataload(add(a.offset, offset)) // Relative offset of `result` from `a.offset`.
            result.offset := add(a.offset, s)
            result.length := sub(a.length, s)
            if or(shr(64, or(s, or(l, a.offset))), gt(offset, l)) { revert(l, 0x00) }
        }
    }

    /// @dev Returns bytes in calldata. Performs bounds checks.
    function bytesInCalldata(bytes calldata a, uint256 offset)
        internal
        pure
        returns (bytes calldata result)
    {
        /// @solidity memory-safe-assembly
        assembly {
            let l := sub(a.length, 0x20)
            let s := calldataload(add(a.offset, offset)) // Relative offset of `result` from `a.offset`.
            result.offset := add(add(a.offset, s), 0x20)
            result.length := calldataload(add(a.offset, s))
            // forgefmt: disable-next-item
            if or(shr(64, or(result.length, or(s, or(l, a.offset)))),
                or(gt(add(s, result.length), l), gt(offset, l))) { revert(l, 0x00) }
        }
    }

    /// @dev Returns empty calldata bytes. For silencing the compiler.
    function emptyCalldata() internal pure returns (bytes calldata result) {
        /// @solidity memory-safe-assembly
        assembly {
            result.length := 0
        }
    }
}

Settings
{
  "remappings": [
    "@chimera/=lib/V2-gov/lib/chimera/src/",
    "@openzeppelin/contracts/=lib/V2-gov/lib/openzeppelin-contracts/contracts/",
    "Solady/=lib/Solady/src/",
    "V2-gov/=lib/V2-gov/src/",
    "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-std/=lib/forge-std/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "v4-core/=lib/V2-gov/lib/v4-core/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/"
  ],
  "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
}

Contract Security Audit

Contract ABI

API
[{"inputs":[{"internalType":"contract IAddressesRegistry","name":"_addressesRegistry","type":"address"},{"internalType":"contract IFlashLoanProvider","name":"_flashLoanProvider","type":"address"},{"internalType":"contract IExchange","name":"_exchange","type":"address"},{"internalType":"address","name":"_USDC","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"EmptyManager","type":"error"},{"inputs":[],"name":"NotBorrower","type":"error"},{"inputs":[],"name":"NotOwnerNorAddManager","type":"error"},{"inputs":[],"name":"NotOwnerNorRemoveManager","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_troveId","type":"uint256"},{"indexed":false,"internalType":"address","name":"_newAddManager","type":"address"}],"name":"AddManagerUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_troveId","type":"uint256"},{"indexed":false,"internalType":"address","name":"_newRemoveManager","type":"address"},{"indexed":false,"internalType":"address","name":"_newReceiver","type":"address"}],"name":"RemoveManagerAndReceiverUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_newTroveNFTAddress","type":"address"}],"name":"TroveNFTAddressChanged","type":"event"},{"inputs":[],"name":"USDC","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"contract IWETH","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"}],"name":"addCollWithRawETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"addManagerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_collChange","type":"uint256"},{"internalType":"bool","name":"_isCollIncrease","type":"bool"},{"internalType":"uint256","name":"_oneChange","type":"uint256"},{"internalType":"bool","name":"_isDebtIncrease","type":"bool"},{"internalType":"uint256","name":"_maxUpfrontFee","type":"uint256"}],"name":"adjustTroveWithRawETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_collChange","type":"uint256"},{"internalType":"bool","name":"_isCollIncrease","type":"bool"},{"internalType":"uint256","name":"_oneChange","type":"uint256"},{"internalType":"bool","name":"_isDebtIncrease","type":"bool"},{"internalType":"uint256","name":"_upperHint","type":"uint256"},{"internalType":"uint256","name":"_lowerHint","type":"uint256"},{"internalType":"uint256","name":"_maxUpfrontFee","type":"uint256"}],"name":"adjustZombieTroveWithRawETH","outputs":[],"stateMutability":"payable","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":"_flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"_minExpectedCollateral","type":"uint256"}],"name":"closeTroveFromCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"}],"name":"closeTroveToRawETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"exchange","outputs":[{"internalType":"contract IExchange","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"flashLoanProvider","outputs":[{"internalType":"contract IFlashLoanProvider","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"troveId","type":"uint256"},{"internalType":"uint256","name":"flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"minOneAmount","type":"uint256"}],"internalType":"struct ILeverageZapper.LeverDownTroveParams","name":"_params","type":"tuple"}],"name":"leverDownTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"troveId","type":"uint256"},{"internalType":"uint256","name":"flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"oneAmount","type":"uint256"},{"internalType":"uint256","name":"maxUpfrontFee","type":"uint256"}],"internalType":"struct ILeverageZapper.LeverUpTroveParams","name":"_params","type":"tuple"}],"name":"leverUpTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_inputRatio","type":"uint256"}],"name":"leverageRatioToCollateralRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"oneToken","outputs":[{"internalType":"contract IOneToken","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"ownerIndex","type":"uint256"},{"internalType":"uint256","name":"collAmount","type":"uint256"},{"internalType":"uint256","name":"flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"oneAmount","type":"uint256"},{"internalType":"uint256","name":"upperHint","type":"uint256"},{"internalType":"uint256","name":"lowerHint","type":"uint256"},{"internalType":"uint256","name":"annualInterestRate","type":"uint256"},{"internalType":"address","name":"batchManager","type":"address"},{"internalType":"uint256","name":"maxUpfrontFee","type":"uint256"},{"internalType":"address","name":"addManager","type":"address"},{"internalType":"address","name":"removeManager","type":"address"},{"internalType":"address","name":"receiver","type":"address"}],"internalType":"struct ILeverageZapper.OpenLeveragedTroveParams","name":"_params","type":"tuple"}],"name":"openLeveragedTroveWithRawETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"ownerIndex","type":"uint256"},{"internalType":"uint256","name":"collAmount","type":"uint256"},{"internalType":"uint256","name":"oneAmount","type":"uint256"},{"internalType":"uint256","name":"upperHint","type":"uint256"},{"internalType":"uint256","name":"lowerHint","type":"uint256"},{"internalType":"uint256","name":"annualInterestRate","type":"uint256"},{"internalType":"address","name":"batchManager","type":"address"},{"internalType":"uint256","name":"maxUpfrontFee","type":"uint256"},{"internalType":"address","name":"addManager","type":"address"},{"internalType":"address","name":"removeManager","type":"address"},{"internalType":"address","name":"receiver","type":"address"}],"internalType":"struct IZapper.OpenTroveParams","name":"_params","type":"tuple"}],"name":"openTroveWithRawETH","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"troveId","type":"uint256"},{"internalType":"uint256","name":"flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"minExpectedCollateral","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"}],"internalType":"struct IZapper.CloseTroveParams","name":"_params","type":"tuple"},{"internalType":"uint256","name":"_effectiveFlashLoanAmount","type":"uint256"}],"name":"receiveFlashLoanOnCloseTroveFromCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"troveId","type":"uint256"},{"internalType":"uint256","name":"flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"minOneAmount","type":"uint256"}],"internalType":"struct ILeverageZapper.LeverDownTroveParams","name":"_params","type":"tuple"},{"internalType":"uint256","name":"_effectiveFlashLoanAmount","type":"uint256"}],"name":"receiveFlashLoanOnLeverDownTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"troveId","type":"uint256"},{"internalType":"uint256","name":"flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"oneAmount","type":"uint256"},{"internalType":"uint256","name":"maxUpfrontFee","type":"uint256"}],"internalType":"struct ILeverageZapper.LeverUpTroveParams","name":"_params","type":"tuple"},{"internalType":"uint256","name":"_effectiveFlashLoanAmount","type":"uint256"}],"name":"receiveFlashLoanOnLeverUpTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"ownerIndex","type":"uint256"},{"internalType":"uint256","name":"collAmount","type":"uint256"},{"internalType":"uint256","name":"flashLoanAmount","type":"uint256"},{"internalType":"uint256","name":"oneAmount","type":"uint256"},{"internalType":"uint256","name":"upperHint","type":"uint256"},{"internalType":"uint256","name":"lowerHint","type":"uint256"},{"internalType":"uint256","name":"annualInterestRate","type":"uint256"},{"internalType":"address","name":"batchManager","type":"address"},{"internalType":"uint256","name":"maxUpfrontFee","type":"uint256"},{"internalType":"address","name":"addManager","type":"address"},{"internalType":"address","name":"removeManager","type":"address"},{"internalType":"address","name":"receiver","type":"address"}],"internalType":"struct ILeverageZapper.OpenLeveragedTroveParams","name":"_params","type":"tuple"},{"internalType":"uint256","name":"_effectiveFlashLoanAmount","type":"uint256"}],"name":"receiveFlashLoanOnOpenLeveragedTrove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"removeManagerReceiverOf","outputs":[{"internalType":"address","name":"manager","type":"address"},{"internalType":"address","name":"receiver","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_oneAmount","type":"uint256"}],"name":"repayOne","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"address","name":"_manager","type":"address"}],"name":"setAddManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"address","name":"_manager","type":"address"}],"name":"setRemoveManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"address","name":"_manager","type":"address"},{"internalType":"address","name":"_receiver","type":"address"}],"name":"setRemoveManagerWithReceiver","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"troveManager","outputs":[{"internalType":"contract ITroveManager","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdrawCollToRawETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_troveId","type":"uint256"},{"internalType":"uint256","name":"_oneAmount","type":"uint256"},{"internalType":"uint256","name":"_maxUpfrontFee","type":"uint256"}],"name":"withdrawOne","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]

6101a060405234801562000011575f80fd5b5060405162004ce538038062004ce583398101604081905262000034916200055a565b83838383838383838084806001600160a01b031663059e01136040518163ffffffff1660e01b8152600401602060405180830381865afa1580156200007b573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620000a19190620005bf565b6001600160a01b031660808190526040519081527f39b3d3f08f5292d52497444fc183b3915a339c0b41fb021bf52ae59505e455b29060200160405180910390a150806001600160a01b031660a0816001600160a01b03168152505050836001600160a01b03166377553ad46040518163ffffffff1660e01b8152600401602060405180830381865afa1580156200013b573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620001619190620005bf565b6001600160a01b031660c0816001600160a01b031681525050836001600160a01b0316633d83908a6040518163ffffffff1660e01b8152600401602060405180830381865afa158015620001b7573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620001dd9190620005bf565b6001600160a01b031660e0816001600160a01b031681525050836001600160a01b031663a27eccc16040518163ffffffff1660e01b8152600401602060405180830381865afa15801562000233573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620002599190620005bf565b6001600160a01b0316610140816001600160a01b031681525050836001600160a01b031663ad5c46486040518163ffffffff1660e01b81526004016020604051808303815f875af1158015620002b1573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620002d79190620005bf565b6001600160a01b03908116610100529081166101205291821661016052811661018052604080516318dc64a360e11b8152905191871692506331b8c9469160048083019260209291908290030181865afa15801562000338573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906200035e9190620005bf565b6001600160a01b0316610100516001600160a01b031614620003c65760405162461bcd60e51b815260206004820152601560248201527f575a3a2057726f6e6720636f6c6c206272616e63680000000000000000000000604482015260640160405180910390fd5b6101005160c05160405163095ea7b360e01b81526001600160a01b0391821660048201525f19602482015291169063095ea7b3906044016020604051808303815f875af11580156200041a573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620004409190620005e4565b506101005160405163095ea7b360e01b81526001600160a01b0384811660048301525f1960248301529091169063095ea7b3906044016020604051808303815f875af115801562000493573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620004b99190620005e4565b50506101405160405163095ea7b360e01b81526001600160a01b0387811660048301525f196024830152909116935063095ea7b3925060440190506020604051808303815f875af115801562000511573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190620005379190620005e4565b505050505062000605565b6001600160a01b038116811462000557575f80fd5b50565b5f805f80608085870312156200056e575f80fd5b84516200057b8162000542565b60208601519094506200058e8162000542565b6040860151909350620005a18162000542565b6060860151909250620005b48162000542565b939692955090935050565b5f60208284031215620005d0575f80fd5b8151620005dd8162000542565b9392505050565b5f60208284031215620005f5575f80fd5b81518015158114620005dd575f80fd5b60805160a05160c05160e0516101005161012051610140516101605161018051614451620008945f395f81816105460152818161073001528181610f76015281816119420152611d7d01525f818161034401528181610628015281816107d5015281816109d701528181610cab01528181611019015281816117a3015281816118e301528181611a9501528181611beb01528181611eb5015281816121780152612b0b01525f8181610424015281816109af01528181610b9201528181610bd9015281816111c7015281816114670152818161176e015281816121270152818161288e01528181612a74015281816131db0152818161329e0152818161333a015281816133ca015261345c01525f6103de01525f8181610476015281816107a60152818161098e01528181610a0601528181610b7101528181610fea0152818161155101528181611746015281816117d201528181611a6601528181611e8601528181611f5001528181612106015281816121a1015281816123000152818161240c015281816125ce01528181612a5301528181612a9b01528181612b3a015281816131ba01528181613208015261351c01525f818161029c015281816113cd0152611c7a01525f81816103ab015281816106b001528181610c6901528181610d2501528181610e92015281816111530152818161129d015281816114f0015281816119f201528181611b6601528181611e120152818161228d0152818161249301528181612677015281816127fe0152612c8001525f612d7c01525f818161084f015281816108f601528181610ae7015281816110a60152818161132c015281816116800152818161206e015281816121e70152818161238d015281816136e7015261387b01526144515ff3fe6080604052600436106101bd575f3560e01c8063a89efd8c116100f2578063d2f7265a11610092578063ebfaa4e611610062578063ebfaa4e6146105c5578063edb66227146105e4578063f926c2d2146105f7578063facb886f1461060a575f80fd5b8063d2f7265a14610535578063d3695fa514610568578063d6491eaf14610587578063d7cf1f24146105a6575f80fd5b8063b6ce0cc5116100cd578063b6ce0cc5146104b7578063bcc9e60e146104e4578063c20b794914610503578063ce52584114610516575f80fd5b8063a89efd8c14610446578063ad5c464814610465578063ade8971a14610498575f80fd5b8063617933ad1161015d57806377553ad41161013857806377553ad41461039a57806389a30271146103cd5780639665814714610400578063a27eccc114610413575f80fd5b8063617933ad14610314578063639af6d01461033357806370986fe114610366575f80fd5b80632e59d86b116101985780632e59d86b1461026c5780633d83908a1461028b578063573f45d7146102d65780635b47b69d146102f5575f80fd5b80630e01617c146101c85780631d02c42e1461022c578063292a3f0b1461024d575f80fd5b366101c457005b5f80fd5b3480156101d3575f80fd5b506102076101e2366004613b66565b600160208190525f918252604090912080549101546001600160a01b03918216911682565b604080516001600160a01b039384168152929091166020830152015b60405180910390f35b348015610237575f80fd5b5061024b610246366004613b93565b61061d565b005b348015610258575f80fd5b5061024b610267366004613be0565b610848565b348015610277575f80fd5b5061024b610286366004613c0e565b6108de565b348015610296575f80fd5b506102be7f000000000000000000000000000000000000000000000000000000000000000081565b6040516001600160a01b039091168152602001610223565b3480156102e1575f80fd5b5061024b6102f0366004613c2f565b610acf565b348015610300575f80fd5b5061024b61030f366004613c4f565b610ca0565b34801561031f575f80fd5b5061024b61032e366004613c7e565b61108e565b34801561033e575f80fd5b506102be7f000000000000000000000000000000000000000000000000000000000000000081565b348015610371575f80fd5b506102be610380366004613b66565b5f602081905290815260409020546001600160a01b031681565b3480156103a5575f80fd5b506102be7f000000000000000000000000000000000000000000000000000000000000000081565b3480156103d8575f80fd5b506102be7f000000000000000000000000000000000000000000000000000000000000000081565b61024b61040e366004613cb4565b611248565b34801561041e575f80fd5b506102be7f000000000000000000000000000000000000000000000000000000000000000081565b348015610451575f80fd5b5061024b610460366004613b66565b611314565b348015610470575f80fd5b506102be7f000000000000000000000000000000000000000000000000000000000000000081565b3480156104a3575f80fd5b5061024b6104b2366004613c7e565b611668565b3480156104c2575f80fd5b506104d66104d1366004613b66565b6118a3565b604051908152602001610223565b3480156104ef575f80fd5b5061024b6104fe366004613d1e565b6118d8565b61024b610511366004613d47565b611b03565b348015610521575f80fd5b5061024b610530366004613b93565b611be0565b348015610540575f80fd5b506102be7f000000000000000000000000000000000000000000000000000000000000000081565b348015610573575f80fd5b5061024b610582366004613be0565b61202f565b348015610592575f80fd5b5061024b6105a1366004613db1565b612042565b3480156105b1575f80fd5b5061024b6105c0366004613df0565b612056565b3480156105d0575f80fd5b5061024b6105df366004613c2f565b6121cf565b61024b6105f2366004613b66565b612375565b6104d6610605366004613e0a565b6124f2565b61024b610618366004613e81565b61294f565b336001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000161461066e5760405162461bcd60e51b815260040161066590613f45565b60405180910390fd5b60408051632139494f60e21b815283356004820152602481018390526001604482018190529184013560648201526084810191909152606083013560a48201527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906384e5253c9060c4015f604051808303815f87803b1580156106f9575f80fd5b505af115801561070b573d5f803e3d5ffd5b5050604080516342d9e56d60e01b8152908501356004820152602085013560248201527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031692506342d9e56d91506044015f604051808303815f87803b15801561077b575f80fd5b505af115801561078d573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016925063a9059cbb9150610803907f000000000000000000000000000000000000000000000000000000000000000090602087013590600401613f86565b6020604051808303815f875af115801561081f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906108439190613f9f565b505050565b6108da82827f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316636352211e866040518263ffffffff1660e01b815260040161089b91815260200190565b602060405180830381865afa1580156108b6573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906105a19190613fba565b5050565b6040516331a9108f60e11b8152813560048201525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa158015610943573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906109679190613fba565b90505f610975833583612bd6565b90506109818335612c68565b610989613b0f565b6109d57f00000000000000000000000000000000000000000000000000000000000000007f00000000000000000000000000000000000000000000000000000000000000008385612d5d565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03166399bff1217f0000000000000000000000000000000000000000000000000000000000000000866020013560025f89604051602001610a6491908135815260208083013590820152604080830135908201526060918201359181019190915260800190565b6040516020818303038152906040526040518663ffffffff1660e01b8152600401610a93959493929190614022565b5f604051808303815f87803b158015610aaa575f80fd5b505af1158015610abc573d5f803e3d5ffd5b50505050610ac981612dae565b50505050565b6040516331a9108f60e11b8152600481018390525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa158015610b34573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610b589190613fba565b9050610b648382612f78565b610b6c613b0f565b610bb77f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000083612ffc565b6040516323b872dd60e01b8152336004820152306024820152604481018490527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906323b872dd906064016020604051808303815f875af1158015610c27573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610c4b9190613f9f565b5060405163573f45d760e01b815260048101859052602481018490527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063573f45d790604401610a93565b336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614610ce85760405162461bcd60e51b815260040161066590613f45565b5f610cf7826040850135614088565b90505f80610d0d6101208601610100870161409b565b6001600160a01b031603610de7576001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016639cb90ba6610d57602087018761409b565b86602001358588608001358960a001358a60c001358b60e001358c61012001353030306040518c63ffffffff1660e01b8152600401610da09b9a999897969594939291906140b6565b6020604051808303815f875af1158015610dbc573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610de09190614115565b9050610f0b565b6040805161016081019091525f9080610e03602088018861409b565b6001600160a01b0316815260200186602001358152602001848152602001866080013581526020018660a0013581526020018660c00135815260200186610100016020810190610e53919061409b565b6001600160a01b039081168252610120880135602083015230604080840182905260608401829052608090930152905163c440844f60e01b81529192507f0000000000000000000000000000000000000000000000000000000000000000169063c440844f90610ec790849060040161412c565b6020604051808303815f875af1158015610ee3573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610f079190614115565b9150505b610f2681610f216101608701610140880161409b565b613008565b610f5381610f3c6101808701610160880161409b565b610f4e6101a08801610180890161409b565b613069565b6040516342d9e56d60e01b815260808501356004820152606085013560248201527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906342d9e56d906044015f604051808303815f87803b158015610fbf575f80fd5b505af1158015610fd1573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016925063a9059cbb9150611047907f000000000000000000000000000000000000000000000000000000000000000090606089013590600401613f86565b6020604051808303815f875af1158015611063573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906110879190613f9f565b5050505050565b6040516331a9108f60e11b8152600481018490525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa1580156110f3573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906111179190613fba565b90505f6111248583612bd6565b905061112f85612c68565b60405163617933ad60e01b81526004810186905260248101859052604481018490527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063617933ad906064015f604051808303815f87803b15801561119c575f80fd5b505af11580156111ae573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016925063a9059cbb91506112009084908890600401613f86565b6020604051808303815f875af115801561121c573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906112409190613f9f565b505050505050565b611250613b0f565b5f61125f8888888888876130ee565b604051632139494f60e21b8152600481018a905260248101899052871515604482015260648101879052851515608482015260a481018590529091507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906384e5253c9060c4015f604051808303815f87803b1580156112e6575f80fd5b505af11580156112f8573d5f803e3d5ffd5b5050505061130a87878787858761331d565b5050505050505050565b6040516331a9108f60e11b8152600481018290525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa158015611379573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061139d9190613fba565b90505f6113aa8383612bd6565b90506113b583612c68565b604051632ab4fd0160e21b8152600481018490525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063aad3f4049060240161014060405180830381865afa15801561141b573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061143f91906141e7565b80516040516323b872dd60e01b815233600482015230602482015260448101919091529091507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906323b872dd906064016020604051808303815f875af11580156114b5573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906114d99190613f9f565b50604051635aa6d46160e01b8152600481018590527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690635aa6d461906024015f604051808303815f87803b158015611539575f80fd5b505af115801561154b573d5f803e3d5ffd5b505050507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316632e1a7d4d670de0b6b3a764000083602001516115969190614088565b6040518263ffffffff1660e01b81526004016115b491815260200190565b5f604051808303815f87803b1580156115cb575f80fd5b505af11580156115dd573d5f803e3d5ffd5b505050505f826001600160a01b0316670de0b6b3a764000083602001516116049190614088565b6040515b5f6040518083038185875af1925050503d805f8114611642576040519150601f19603f3d011682016040523d82523d5f602084013e611647565b606091505b50509050806110875760405162461bcd60e51b81526004016106659061426c565b6040516331a9108f60e11b8152600481018490525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa1580156116cd573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116f19190613fba565b90505f6116fe8583612bd6565b905061170985612c68565b60408051608081018252868152602081018690529081018490526001600160a01b038216606082015261173a613b0f565b80516001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000811690915281517f0000000000000000000000000000000000000000000000000000000000000000909116602091909101526117a181846135ea565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03166399bff1217f00000000000000000000000000000000000000000000000000000000000000008860015f8760405160200161183591908151815260208083015190820152604080830151908201526060918201516001600160a01b03169181019190915260800190565b6040516020818303038152906040526040518663ffffffff1660e01b8152600401611864959493929190614022565b5f604051808303815f87803b15801561187b575f80fd5b505af115801561188d573d5f803e3d5ffd5b5050505061189a81612dae565b50505050505050565b5f6118b6670de0b6b3a7640000836142b0565b6118c8670de0b6b3a7640000846142c3565b6118d291906142da565b92915050565b336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146119205760405162461bcd60e51b815260040161066590613f45565b6040805163ddbf7b7560e01b8152600481018390529083013560248201525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063ddbf7b75906044016020604051808303815f875af1158015611990573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906119b49190614115565b604051632139494f60e21b815284356004820152602085013560248201525f60448201819052606482018390526084820181905260a48201529091507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906384e5253c9060c4015f604051808303815f87803b158015611a3b575f80fd5b505af1158015611a4d573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016925063a9059cbb9150611ac3907f000000000000000000000000000000000000000000000000000000000000000090602088013590600401613f86565b6020604051808303815f875af1158015611adf573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610ac99190613f9f565b611b0b613b0f565b5f611b1a8a8a8a8a8a876130ee565b60405163dcfbd29360e01b8152600481018c9052602481018b9052891515604482015260648101899052871515608482015260a4810187905260c4810186905260e481018590529091507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063dcfbd29390610104015f604051808303815f87803b158015611bb0575f80fd5b505af1158015611bc2573d5f803e3d5ffd5b50505050611bd489898989858761331d565b50505050505050505050565b336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614611c625760405162461bcd60e51b815260206004820152602160248201527f575a3a2043616c6c6572206e6f7420466c6173684c6f616e2070726f766964656044820152603960f91b6064820152608401610665565b604051632ab4fd0160e21b8152823560048201525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063aad3f4049060240161014060405180830381865afa158015611cc8573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611cec91906141e7565b90505f83602001358260200151611d0391906142b0565b90508360400135811015611d645760405162461bcd60e51b815260206004820152602260248201527f575a3a204e6f7420656e6f75676820636f6c6c61746572616c20726563656976604482015261195960f21b6064820152608401610665565b815160405163ddbf7b7560e01b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169163ddbf7b7591611dbb918791600401918252602082015260400190565b6020604051808303815f875af1158015611dd7573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611dfb9190614115565b50604051635aa6d46160e01b8152843560048201527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690635aa6d461906024015f604051808303815f87803b158015611e5b575f80fd5b505af1158015611e6d573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016925063a9059cbb9150611ee3907f000000000000000000000000000000000000000000000000000000000000000090602089013590600401613f86565b6020604051808303815f875af1158015611eff573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611f239190613f9f565b505f611f37670de0b6b3a764000083614088565b604051632e1a7d4d60e01b8152600481018290529091507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690632e1a7d4d906024015f604051808303815f87803b158015611f99575f80fd5b505af1158015611fab573d5f803e3d5ffd5b505f9250611fc2915050608087016060880161409b565b6001600160a01b0316826040515f6040518083038185875af1925050503d805f8114612009576040519150601f19603f3d011682016040523d82523d5f602084013e61200e565b606091505b50509050806112405760405162461bcd60e51b81526004016106659061426c565b612038826136d1565b6108da8282613008565b61204b836136d1565b610843838383613069565b6040516331a9108f60e11b8152813560048201525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa1580156120bb573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906120df9190613fba565b90505f6120ed833583612bd6565b90506120f98335612c68565b612101613b0f565b61214d7f00000000000000000000000000000000000000000000000000000000000000007f00000000000000000000000000000000000000000000000000000000000000008385612d5d565b6040805185356020828101919091528601358183018190529186013560608201526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016916399bff121917f000000000000000000000000000000000000000000000000000000000000000091906003905f90608001610a64565b6040516331a9108f60e11b8152600481018390525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa158015612234573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906122589190613fba565b90505f6122658483612bd6565b905061227084612c68565b6040516302c06f1b60e51b815260048101859052602481018490527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063580de360906044015f604051808303815f87803b1580156122d6575f80fd5b505af11580156122e8573d5f803e3d5ffd5b5050604051632e1a7d4d60e01b8152600481018690527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169250632e1a7d4d91506024015f604051808303815f87803b15801561234b575f80fd5b505af115801561235d573d5f803e3d5ffd5b505050505f816001600160a01b031684604051611608565b6040516331a9108f60e11b8152600481018290525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa1580156123da573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906123fe9190613fba565b905061240a8282612f78565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004015f604051808303818588803b158015612463575f80fd5b505af1158015612475573d5f803e3d5ffd5b5050604051630167d53d60e61b8152600481018690523460248201527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031693506359f54f40925060440190505f604051808303815f87803b1580156124e0575f80fd5b505af1158015611240573d5f803e3d5ffd5b5f670de0b6b3a764000034116125415760405162461bcd60e51b81526020600482015260146024820152730aeb4744092dce6eaccccd2c6d2cadce8408aa8960631b6044820152606401610665565b5f612553610100840160e0850161409b565b6001600160a01b0316148061256a575060c0820135155b6125cc5760405162461bcd60e51b815260206004820152602d60248201527f575a3a2043616e6e6f742063686f6f736520696e746572657374206966206a6f60448201526c0d2dcd2dcce40c240c4c2e8c6d609b1b6064820152608401610665565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004015f604051808303818588803b158015612625575f80fd5b505af1158015612637573d5f803e3d5ffd5b50505050505f8061264b846020013561378c565b90505f61265f610100860160e0870161409b565b6001600160a01b031603612746576001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016639cb90ba66126a9602087018761409b565b836126bc670de0b6b3a7640000346142b0565b886060013589608001358a60a001358b60c001358c61010001353030306040518c63ffffffff1660e01b81526004016126ff9b9a999897969594939291906140b6565b6020604051808303815f875af115801561271b573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061273f9190614115565b9150612877565b6040805161016081019091525f9080612762602088018861409b565b6001600160a01b0316815260208101849052604001612789670de0b6b3a7640000346142b0565b815260200186606001358152602001866080013581526020018660a0013581526020018660e00160208101906127bf919061409b565b6001600160a01b039081168252610100880135602083015230604080840182905260608401829052608090930152905163c440844f60e01b81529192507f0000000000000000000000000000000000000000000000000000000000000000169063c440844f9061283390849060040161412c565b6020604051808303815f875af115801561284f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906128739190614115565b9250505b60405163a9059cbb60e01b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169063a9059cbb906128c9903390606089013590600401613f86565b6020604051808303815f875af11580156128e5573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906129099190613f9f565b5061292082610f216101408701610120880161409b565b612948826129366101608701610140880161409b565b610f4e6101808801610160890161409b565b5092915050565b604081015161296690670de0b6b3a7640000614088565b34146129b45760405162461bcd60e51b815260206004820152601760248201527f4c5a3a2057726f6e6720616d6f756e74206f66204554480000000000000000006044820152606401610665565b6101008101516001600160a01b031615806129d1575060e0810151155b612a335760405162461bcd60e51b815260206004820152602d60248201527f4c5a3a2043616e6e6f742063686f6f736520696e746572657374206966206a6f60448201526c0d2dcd2dcce40c240c4c2e8c6d609b1b6064820152608401610665565b612a41338260200151613793565b6020820152612a4e613b0f565b612a997f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000083612ffc565b7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004015f604051808303818588803b158015612af2575f80fd5b505af1158015612b04573d5f803e3d5ffd5b50505050507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03166399bff1217f000000000000000000000000000000000000000000000000000000000000000084606001515f8087604051602001612b7191906142f9565b6040516020818303038152906040526040518663ffffffff1660e01b8152600401612ba0959493929190614022565b5f604051808303815f87803b158015612bb7575f80fd5b505af1158015612bc9573d5f803e3d5ffd5b505050506108da81612dae565b5f828152600160208190526040822080549101546001600160a01b03918216919081169084163314801590612c145750336001600160a01b03831614155b15612c32576040516310bb5c9d60e31b815260040160405180910390fd5b6001600160a01b0381161580612c515750336001600160a01b03831614155b15612c605783925050506118d2565b949350505050565b604051630380585f60e21b8152600481018290525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690630e01617c906024016040805180830381865afa158015612ccc573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190612cf091906143c1565b9150506001600160a01b03811630146108da5760405162461bcd60e51b815260206004820152602960248201527f425a3a205a6170706572206973206e6f7420726563656976657220666f7220746044820152686869732074726f766560b81b6064820152608401610665565b81516001600160a01b03858116909152825184821660209091015282517f0000000000000000000000000000000000000000000000000000000000000000909116604090910152610ac982826135ea565b5f805b60048110156108435782518160048110612dcd57612dcd61429c565b602002015191506001600160a01b03821615610843576040516370a0823160e01b81523060048201525f906001600160a01b038416906370a0823190602401602060405180830381865afa158015612e27573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190612e4b9190614115565b905083602001518260048110612e6357612e6361429c565b60200201518111612e745750612f70565b83604001518260048110612e8a57612e8a61429c565b602002015115612f3057826001600160a01b031663c12ea3ca856060015186602001518560048110612ebe57612ebe61429c565b6020020151612ecd90856142b0565b6040518363ffffffff1660e01b8152600401612eea929190613f86565b6020604051808303815f875af1158015612f06573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190612f2a9190614115565b50612f6e565b612f6e846060015185602001518460048110612f4e57612f4e61429c565b6020020151612f5d90846142b0565b6001600160a01b03861691906137c6565b505b600101612db1565b5f828152602081905260409020546001600160a01b039081169082163314801590612fab57506001600160a01b03811615155b8015612fc05750336001600160a01b03821614155b15610843575f838152600160205260409020546001600160a01b0316338114610ac957604051636522e96960e01b815260040160405180910390fd5b61084383838333612d5d565b5f828152602081815260409182902080546001600160a01b0319166001600160a01b038516908117909155915191825283917f3942babd464ceb1c7d319f75245a8cd41334592b45507f072e7020e63c22a8dc910160405180910390a25050565b613073828261381c565b5f8381526001602081815260409283902080546001600160a01b03199081166001600160a01b038881169182178455929094018054909116918616918217905583519283529082015284917f649442545e0f313a6d8087b19bc47bd2bd9b63f79d23a773446e00d2ea01d169910160405180910390a2505050565b5f84156131415734861461313c5760405162461bcd60e51b815260206004820152601560248201527415d68e8815dc9bdb99c818dbdb1b08185b5bdd5b9d605a1b6044820152606401610665565b6131a6565b34156131a65760405162461bcd60e51b815260206004820152602e60248201527f575a3a204e6f7420616464696e6720636f6c6c2c206e6f204554482073686f7560448201526d1b19081899481c9958d95a5d995960921b6064820152608401610665565b5f6131b388888887613859565b90506132007f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000085612ffc565b8515613277577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663d0e30db0886040518263ffffffff1660e01b81526004015f604051808303818588803b15801561325f575f80fd5b505af1158015613271573d5f803e3d5ffd5b50505050505b83613312576040516323b872dd60e01b8152336004820152306024820152604481018690527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906323b872dd906064016020604051808303815f875af11580156132ec573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906133109190613f9f565b505b979650505050505050565b82156133b35760405163a9059cbb60e01b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169063a9059cbb906133719085908890600401613f86565b6020604051808303815f875af115801561338d573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906133b19190613f9f565b505b6040516370a0823160e01b81523060048201525f907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906370a0823190602401602060405180830381865afa158015613417573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061343b9190614115565b60208381015101519091508111156134f457606082015160208084015101517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169163a9059cbb9161349590856142b0565b6040518363ffffffff1660e01b81526004016134b2929190613f86565b6020604051808303815f875af11580156134ce573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906134f29190613f9f565b505b8515801561350157505f87115b1561189a57604051632e1a7d4d60e01b8152600481018890527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690632e1a7d4d906024015f604051808303815f87803b158015613565575f80fd5b505af1158015613577573d5f803e3d5ffd5b505050505f836001600160a01b0316886040515f6040518083038185875af1925050503d805f81146135c4576040519150601f19603f3d011682016040523d82523d5f602084013e6135c9565b606091505b505090508061130a5760405162461bcd60e51b81526004016106659061426c565b5f5b60048110156136bf5782515f90826004811061360a5761360a61429c565b60200201516001600160a01b0316146136bf57825181600481106136305761363061429c565b60200201516040516370a0823160e01b81523060048201526001600160a01b03909116906370a0823190602401602060405180830381865afa158015613678573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061369c9190614115565b836020015182600481106136b2576136b261429c565b60200201526001016135ec565b506001600160a01b0316606090910152565b6040516331a9108f60e11b8152600481018290527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690636352211e90602401602060405180830381865afa158015613734573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906137589190613fba565b6001600160a01b0316336001600160a01b03161461378957604051631963d1e760e31b815260040160405180910390fd5b50565b5f6118d233835b5f82826040516020016137a7929190613f86565b60408051601f1981840301815291905280516020909101209392505050565b6108438363a9059cbb60e01b84846040516024016137e5929190613f86565b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b03199093169290921790915261392f565b6001600160a01b03821615801561383b57506001600160a01b03811615155b156108da57604051632235921760e01b815260040160405180910390fd5b6040516331a9108f60e11b8152600481018590525f9081906001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001690636352211e90602401602060405180830381865afa1580156138c0573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906138e49190613fba565b905080841580156138f457505f86115b806138fc5750835b1561391b5761390b8783612bd6565b905061391687612c68565b613925565b6139258783612f78565b9695505050505050565b5f613983826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316613a029092919063ffffffff16565b905080515f14806139a35750808060200190518101906139a39190613f9f565b6108435760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610665565b6060612c6084845f85855f80866001600160a01b03168587604051613a2791906143ee565b5f6040518083038185875af1925050503d805f8114613a61576040519150601f19603f3d011682016040523d82523d5f602084013e613a66565b606091505b50915091506133128783838760608315613ae05782515f03613ad9576001600160a01b0385163b613ad95760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610665565b5081612c60565b612c608383815115613af55781518083602001fd5b8060405162461bcd60e51b81526004016106659190614409565b6040518060800160405280613b22613b48565b8152602001613b2f613b48565b8152602001613b3c613b48565b81525f60209091015290565b60405180608001604052806004906020820280368337509192915050565b5f60208284031215613b76575f80fd5b5035919050565b5f60808284031215613b8d575f80fd5b50919050565b5f8060a08385031215613ba4575f80fd5b613bae8484613b7d565b946080939093013593505050565b6001600160a01b0381168114613789575f80fd5b8035613bdb81613bbc565b919050565b5f8060408385031215613bf1575f80fd5b823591506020830135613c0381613bbc565b809150509250929050565b5f60808284031215613c1e575f80fd5b613c288383613b7d565b9392505050565b5f8060408385031215613c40575f80fd5b50508035926020909101359150565b5f808284036101c0811215613c62575f80fd5b6101a080821215613c71575f80fd5b9395938601359450505050565b5f805f60608486031215613c90575f80fd5b505081359360208301359350604090920135919050565b8015158114613789575f80fd5b5f805f805f8060c08789031215613cc9575f80fd5b86359550602087013594506040870135613ce281613ca7565b9350606087013592506080870135613cf981613ca7565b8092505060a087013590509295509295509295565b5f60608284031215613b8d575f80fd5b5f8060808385031215613d2f575f80fd5b613d398484613d0e565b946060939093013593505050565b5f805f805f805f80610100898b031215613d5f575f80fd5b88359750602089013596506040890135613d7881613ca7565b9550606089013594506080890135613d8f81613ca7565b979a969950949793969560a0850135955060c08501359460e001359350915050565b5f805f60608486031215613dc3575f80fd5b833592506020840135613dd581613bbc565b91506040840135613de581613bbc565b809150509250925092565b5f60608284031215613e00575f80fd5b613c288383613d0e565b5f6101808284031215613b8d575f80fd5b6040516101a0810167ffffffffffffffff81118282101715613e4b57634e487b7160e01b5f52604160045260245ffd5b60405290565b604051610140810167ffffffffffffffff81118282101715613e4b57634e487b7160e01b5f52604160045260245ffd5b5f6101a08284031215613e92575f80fd5b613e9a613e1b565b613ea383613bd0565b81526020830135602082015260408301356040820152606083013560608201526080830135608082015260a083013560a082015260c083013560c082015260e083013560e0820152610100613ef9818501613bd0565b908201526101208381013590820152610140613f16818501613bd0565b90820152610160613f28848201613bd0565b90820152610180613f3a848201613bd0565b908201529392505050565b60208082526021908201527f4c5a3a2043616c6c6572206e6f7420466c6173684c6f616e2070726f766964656040820152603960f91b606082015260800190565b6001600160a01b03929092168252602082015260400190565b5f60208284031215613faf575f80fd5b8151613c2881613ca7565b5f60208284031215613fca575f80fd5b8151613c2881613bbc565b5f5b83811015613fef578181015183820152602001613fd7565b50505f910152565b5f815180845261400e816020860160208601613fd5565b601f01601f19169290920160200192915050565b6001600160a01b0386168152602081018590525f6004851061405257634e487b7160e01b5f52602160045260245ffd5b846040830152831515606083015260a0608083015261331260a0830184613ff7565b634e487b7160e01b5f52601160045260245ffd5b808201808211156118d2576118d2614074565b5f602082840312156140ab575f80fd5b8135613c2881613bbc565b6001600160a01b039b8c168152602081019a909a5260408a01989098526060890196909652608088019490945260a087019290925260c086015260e0850152841661010084015283166101208301529091166101408201526101600190565b5f60208284031215614125575f80fd5b5051919050565b81516001600160a01b0316815261016081016020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015161418a60c08401826001600160a01b03169052565b5060e083015160e0830152610100808401516141b0828501826001600160a01b03169052565b5050610120838101516001600160a01b038116848301525050610140838101516001600160a01b038116848301525b505092915050565b5f61014082840312156141f8575f80fd5b614200613e51565b825181526020830151602082015260408301516040820152606083015160608201526080830151608082015260a083015160a082015260c083015160c082015260e083015160e08201526101008084015181830152506101208084015181830152508091505092915050565b60208082526016908201527515d68e8814d95b991a5b99c81155120819985a5b195960521b604082015260600190565b634e487b7160e01b5f52603260045260245ffd5b818103818111156118d2576118d2614074565b80820281158282048414176118d2576118d2614074565b5f826142f457634e487b7160e01b5f52601260045260245ffd5b500490565b81516001600160a01b031681526101a081016020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015160c083015260e083015160e08301526101008084015161436c828501826001600160a01b03169052565b50506101208381015190830152610140808401516001600160a01b038116828501525050610160838101516001600160a01b038116848301525050610180838101516001600160a01b038116848301526141df565b5f80604083850312156143d2575f80fd5b82516143dd81613bbc565b6020840151909250613c0381613bbc565b5f82516143ff818460208701613fd5565b9190910192915050565b602081525f613c286020830184613ff756fea2646970667358221220f4048805a1b9a4098e2c32d77f91b98637c79d4ba14be7e4032b6ad4fce45cf164736f6c634300081800330000000000000000000000000a41a0b63e3a1c5bd2069792c5baeab34690ab3500000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50100000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d913900000000000000000000000029219dd400f2bf60e5a23d13be72b486d4038894

Deployed Bytecode

0x6080604052600436106101bd575f3560e01c8063a89efd8c116100f2578063d2f7265a11610092578063ebfaa4e611610062578063ebfaa4e6146105c5578063edb66227146105e4578063f926c2d2146105f7578063facb886f1461060a575f80fd5b8063d2f7265a14610535578063d3695fa514610568578063d6491eaf14610587578063d7cf1f24146105a6575f80fd5b8063b6ce0cc5116100cd578063b6ce0cc5146104b7578063bcc9e60e146104e4578063c20b794914610503578063ce52584114610516575f80fd5b8063a89efd8c14610446578063ad5c464814610465578063ade8971a14610498575f80fd5b8063617933ad1161015d57806377553ad41161013857806377553ad41461039a57806389a30271146103cd5780639665814714610400578063a27eccc114610413575f80fd5b8063617933ad14610314578063639af6d01461033357806370986fe114610366575f80fd5b80632e59d86b116101985780632e59d86b1461026c5780633d83908a1461028b578063573f45d7146102d65780635b47b69d146102f5575f80fd5b80630e01617c146101c85780631d02c42e1461022c578063292a3f0b1461024d575f80fd5b366101c457005b5f80fd5b3480156101d3575f80fd5b506102076101e2366004613b66565b600160208190525f918252604090912080549101546001600160a01b03918216911682565b604080516001600160a01b039384168152929091166020830152015b60405180910390f35b348015610237575f80fd5b5061024b610246366004613b93565b61061d565b005b348015610258575f80fd5b5061024b610267366004613be0565b610848565b348015610277575f80fd5b5061024b610286366004613c0e565b6108de565b348015610296575f80fd5b506102be7f000000000000000000000000772ac325300082355e18534070b7ca6fcd9ca2b681565b6040516001600160a01b039091168152602001610223565b3480156102e1575f80fd5b5061024b6102f0366004613c2f565b610acf565b348015610300575f80fd5b5061024b61030f366004613c4f565b610ca0565b34801561031f575f80fd5b5061024b61032e366004613c7e565b61108e565b34801561033e575f80fd5b506102be7f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50181565b348015610371575f80fd5b506102be610380366004613b66565b5f602081905290815260409020546001600160a01b031681565b3480156103a5575f80fd5b506102be7f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b81565b3480156103d8575f80fd5b506102be7f00000000000000000000000029219dd400f2bf60e5a23d13be72b486d403889481565b61024b61040e366004613cb4565b611248565b34801561041e575f80fd5b506102be7f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c0037581565b348015610451575f80fd5b5061024b610460366004613b66565b611314565b348015610470575f80fd5b506102be7f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad3881565b3480156104a3575f80fd5b5061024b6104b2366004613c7e565b611668565b3480156104c2575f80fd5b506104d66104d1366004613b66565b6118a3565b604051908152602001610223565b3480156104ef575f80fd5b5061024b6104fe366004613d1e565b6118d8565b61024b610511366004613d47565b611b03565b348015610521575f80fd5b5061024b610530366004613b93565b611be0565b348015610540575f80fd5b506102be7f00000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d913981565b348015610573575f80fd5b5061024b610582366004613be0565b61202f565b348015610592575f80fd5b5061024b6105a1366004613db1565b612042565b3480156105b1575f80fd5b5061024b6105c0366004613df0565b612056565b3480156105d0575f80fd5b5061024b6105df366004613c2f565b6121cf565b61024b6105f2366004613b66565b612375565b6104d6610605366004613e0a565b6124f2565b61024b610618366004613e81565b61294f565b336001600160a01b037f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec501161461066e5760405162461bcd60e51b815260040161066590613f45565b60405180910390fd5b60408051632139494f60e21b815283356004820152602481018390526001604482018190529184013560648201526084810191909152606083013560a48201527f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b0316906384e5253c9060c4015f604051808303815f87803b1580156106f9575f80fd5b505af115801561070b573d5f803e3d5ffd5b5050604080516342d9e56d60e01b8152908501356004820152602085013560248201527f00000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d91396001600160a01b031692506342d9e56d91506044015f604051808303815f87803b15801561077b575f80fd5b505af115801561078d573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad3816925063a9059cbb9150610803907f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50190602087013590600401613f86565b6020604051808303815f875af115801561081f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906108439190613f9f565b505050565b6108da82827f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b0316636352211e866040518263ffffffff1660e01b815260040161089b91815260200190565b602060405180830381865afa1580156108b6573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906105a19190613fba565b5050565b6040516331a9108f60e11b8152813560048201525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa158015610943573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906109679190613fba565b90505f610975833583612bd6565b90506109818335612c68565b610989613b0f565b6109d57f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad387f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c003758385612d5d565b7f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec5016001600160a01b03166399bff1217f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad38866020013560025f89604051602001610a6491908135815260208083013590820152604080830135908201526060918201359181019190915260800190565b6040516020818303038152906040526040518663ffffffff1660e01b8152600401610a93959493929190614022565b5f604051808303815f87803b158015610aaa575f80fd5b505af1158015610abc573d5f803e3d5ffd5b50505050610ac981612dae565b50505050565b6040516331a9108f60e11b8152600481018390525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa158015610b34573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610b589190613fba565b9050610b648382612f78565b610b6c613b0f565b610bb77f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad387f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c0037583612ffc565b6040516323b872dd60e01b8152336004820152306024820152604481018490527f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c003756001600160a01b0316906323b872dd906064016020604051808303815f875af1158015610c27573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610c4b9190613f9f565b5060405163573f45d760e01b815260048101859052602481018490527f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b03169063573f45d790604401610a93565b336001600160a01b037f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec5011614610ce85760405162461bcd60e51b815260040161066590613f45565b5f610cf7826040850135614088565b90505f80610d0d6101208601610100870161409b565b6001600160a01b031603610de7576001600160a01b037f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b16639cb90ba6610d57602087018761409b565b86602001358588608001358960a001358a60c001358b60e001358c61012001353030306040518c63ffffffff1660e01b8152600401610da09b9a999897969594939291906140b6565b6020604051808303815f875af1158015610dbc573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610de09190614115565b9050610f0b565b6040805161016081019091525f9080610e03602088018861409b565b6001600160a01b0316815260200186602001358152602001848152602001866080013581526020018660a0013581526020018660c00135815260200186610100016020810190610e53919061409b565b6001600160a01b039081168252610120880135602083015230604080840182905260608401829052608090930152905163c440844f60e01b81529192507f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b169063c440844f90610ec790849060040161412c565b6020604051808303815f875af1158015610ee3573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610f079190614115565b9150505b610f2681610f216101608701610140880161409b565b613008565b610f5381610f3c6101808701610160880161409b565b610f4e6101a08801610180890161409b565b613069565b6040516342d9e56d60e01b815260808501356004820152606085013560248201527f00000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d91396001600160a01b0316906342d9e56d906044015f604051808303815f87803b158015610fbf575f80fd5b505af1158015610fd1573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad3816925063a9059cbb9150611047907f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50190606089013590600401613f86565b6020604051808303815f875af1158015611063573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906110879190613f9f565b5050505050565b6040516331a9108f60e11b8152600481018490525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa1580156110f3573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906111179190613fba565b90505f6111248583612bd6565b905061112f85612c68565b60405163617933ad60e01b81526004810186905260248101859052604481018490527f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b03169063617933ad906064015f604051808303815f87803b15801561119c575f80fd5b505af11580156111ae573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c0037516925063a9059cbb91506112009084908890600401613f86565b6020604051808303815f875af115801561121c573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906112409190613f9f565b505050505050565b611250613b0f565b5f61125f8888888888876130ee565b604051632139494f60e21b8152600481018a905260248101899052871515604482015260648101879052851515608482015260a481018590529091507f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b0316906384e5253c9060c4015f604051808303815f87803b1580156112e6575f80fd5b505af11580156112f8573d5f803e3d5ffd5b5050505061130a87878787858761331d565b5050505050505050565b6040516331a9108f60e11b8152600481018290525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa158015611379573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061139d9190613fba565b90505f6113aa8383612bd6565b90506113b583612c68565b604051632ab4fd0160e21b8152600481018490525f907f000000000000000000000000772ac325300082355e18534070b7ca6fcd9ca2b66001600160a01b03169063aad3f4049060240161014060405180830381865afa15801561141b573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061143f91906141e7565b80516040516323b872dd60e01b815233600482015230602482015260448101919091529091507f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c003756001600160a01b0316906323b872dd906064016020604051808303815f875af11580156114b5573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906114d99190613f9f565b50604051635aa6d46160e01b8152600481018590527f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b031690635aa6d461906024015f604051808303815f87803b158015611539575f80fd5b505af115801561154b573d5f803e3d5ffd5b505050507f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b0316632e1a7d4d670de0b6b3a764000083602001516115969190614088565b6040518263ffffffff1660e01b81526004016115b491815260200190565b5f604051808303815f87803b1580156115cb575f80fd5b505af11580156115dd573d5f803e3d5ffd5b505050505f826001600160a01b0316670de0b6b3a764000083602001516116049190614088565b6040515b5f6040518083038185875af1925050503d805f8114611642576040519150601f19603f3d011682016040523d82523d5f602084013e611647565b606091505b50509050806110875760405162461bcd60e51b81526004016106659061426c565b6040516331a9108f60e11b8152600481018490525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa1580156116cd573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116f19190613fba565b90505f6116fe8583612bd6565b905061170985612c68565b60408051608081018252868152602081018690529081018490526001600160a01b038216606082015261173a613b0f565b80516001600160a01b037f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad38811690915281517f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c00375909116602091909101526117a181846135ea565b7f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec5016001600160a01b03166399bff1217f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad388860015f8760405160200161183591908151815260208083015190820152604080830151908201526060918201516001600160a01b03169181019190915260800190565b6040516020818303038152906040526040518663ffffffff1660e01b8152600401611864959493929190614022565b5f604051808303815f87803b15801561187b575f80fd5b505af115801561188d573d5f803e3d5ffd5b5050505061189a81612dae565b50505050505050565b5f6118b6670de0b6b3a7640000836142b0565b6118c8670de0b6b3a7640000846142c3565b6118d291906142da565b92915050565b336001600160a01b037f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50116146119205760405162461bcd60e51b815260040161066590613f45565b6040805163ddbf7b7560e01b8152600481018390529083013560248201525f907f00000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d91396001600160a01b03169063ddbf7b75906044016020604051808303815f875af1158015611990573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906119b49190614115565b604051632139494f60e21b815284356004820152602085013560248201525f60448201819052606482018390526084820181905260a48201529091507f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b0316906384e5253c9060c4015f604051808303815f87803b158015611a3b575f80fd5b505af1158015611a4d573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad3816925063a9059cbb9150611ac3907f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50190602088013590600401613f86565b6020604051808303815f875af1158015611adf573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610ac99190613f9f565b611b0b613b0f565b5f611b1a8a8a8a8a8a876130ee565b60405163dcfbd29360e01b8152600481018c9052602481018b9052891515604482015260648101899052871515608482015260a4810187905260c4810186905260e481018590529091507f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b03169063dcfbd29390610104015f604051808303815f87803b158015611bb0575f80fd5b505af1158015611bc2573d5f803e3d5ffd5b50505050611bd489898989858761331d565b50505050505050505050565b336001600160a01b037f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec5011614611c625760405162461bcd60e51b815260206004820152602160248201527f575a3a2043616c6c6572206e6f7420466c6173684c6f616e2070726f766964656044820152603960f91b6064820152608401610665565b604051632ab4fd0160e21b8152823560048201525f907f000000000000000000000000772ac325300082355e18534070b7ca6fcd9ca2b66001600160a01b03169063aad3f4049060240161014060405180830381865afa158015611cc8573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611cec91906141e7565b90505f83602001358260200151611d0391906142b0565b90508360400135811015611d645760405162461bcd60e51b815260206004820152602260248201527f575a3a204e6f7420656e6f75676820636f6c6c61746572616c20726563656976604482015261195960f21b6064820152608401610665565b815160405163ddbf7b7560e01b81526001600160a01b037f00000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d9139169163ddbf7b7591611dbb918791600401918252602082015260400190565b6020604051808303815f875af1158015611dd7573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611dfb9190614115565b50604051635aa6d46160e01b8152843560048201527f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b031690635aa6d461906024015f604051808303815f87803b158015611e5b575f80fd5b505af1158015611e6d573d5f803e3d5ffd5b505060405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad3816925063a9059cbb9150611ee3907f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50190602089013590600401613f86565b6020604051808303815f875af1158015611eff573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611f239190613f9f565b505f611f37670de0b6b3a764000083614088565b604051632e1a7d4d60e01b8152600481018290529091507f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b031690632e1a7d4d906024015f604051808303815f87803b158015611f99575f80fd5b505af1158015611fab573d5f803e3d5ffd5b505f9250611fc2915050608087016060880161409b565b6001600160a01b0316826040515f6040518083038185875af1925050503d805f8114612009576040519150601f19603f3d011682016040523d82523d5f602084013e61200e565b606091505b50509050806112405760405162461bcd60e51b81526004016106659061426c565b612038826136d1565b6108da8282613008565b61204b836136d1565b610843838383613069565b6040516331a9108f60e11b8152813560048201525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa1580156120bb573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906120df9190613fba565b90505f6120ed833583612bd6565b90506120f98335612c68565b612101613b0f565b61214d7f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad387f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c003758385612d5d565b6040805185356020828101919091528601358183018190529186013560608201526001600160a01b037f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50116916399bff121917f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad3891906003905f90608001610a64565b6040516331a9108f60e11b8152600481018390525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa158015612234573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906122589190613fba565b90505f6122658483612bd6565b905061227084612c68565b6040516302c06f1b60e51b815260048101859052602481018490527f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b03169063580de360906044015f604051808303815f87803b1580156122d6575f80fd5b505af11580156122e8573d5f803e3d5ffd5b5050604051632e1a7d4d60e01b8152600481018690527f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b03169250632e1a7d4d91506024015f604051808303815f87803b15801561234b575f80fd5b505af115801561235d573d5f803e3d5ffd5b505050505f816001600160a01b031684604051611608565b6040516331a9108f60e11b8152600481018290525f907f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa1580156123da573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906123fe9190613fba565b905061240a8282612f78565b7f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004015f604051808303818588803b158015612463575f80fd5b505af1158015612475573d5f803e3d5ffd5b5050604051630167d53d60e61b8152600481018690523460248201527f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b031693506359f54f40925060440190505f604051808303815f87803b1580156124e0575f80fd5b505af1158015611240573d5f803e3d5ffd5b5f670de0b6b3a764000034116125415760405162461bcd60e51b81526020600482015260146024820152730aeb4744092dce6eaccccd2c6d2cadce8408aa8960631b6044820152606401610665565b5f612553610100840160e0850161409b565b6001600160a01b0316148061256a575060c0820135155b6125cc5760405162461bcd60e51b815260206004820152602d60248201527f575a3a2043616e6e6f742063686f6f736520696e746572657374206966206a6f60448201526c0d2dcd2dcce40c240c4c2e8c6d609b1b6064820152608401610665565b7f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004015f604051808303818588803b158015612625575f80fd5b505af1158015612637573d5f803e3d5ffd5b50505050505f8061264b846020013561378c565b90505f61265f610100860160e0870161409b565b6001600160a01b031603612746576001600160a01b037f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b16639cb90ba66126a9602087018761409b565b836126bc670de0b6b3a7640000346142b0565b886060013589608001358a60a001358b60c001358c61010001353030306040518c63ffffffff1660e01b81526004016126ff9b9a999897969594939291906140b6565b6020604051808303815f875af115801561271b573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061273f9190614115565b9150612877565b6040805161016081019091525f9080612762602088018861409b565b6001600160a01b0316815260208101849052604001612789670de0b6b3a7640000346142b0565b815260200186606001358152602001866080013581526020018660a0013581526020018660e00160208101906127bf919061409b565b6001600160a01b039081168252610100880135602083015230604080840182905260608401829052608090930152905163c440844f60e01b81529192507f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b169063c440844f9061283390849060040161412c565b6020604051808303815f875af115801561284f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906128739190614115565b9250505b60405163a9059cbb60e01b81526001600160a01b037f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c00375169063a9059cbb906128c9903390606089013590600401613f86565b6020604051808303815f875af11580156128e5573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906129099190613f9f565b5061292082610f216101408701610120880161409b565b612948826129366101608701610140880161409b565b610f4e6101808801610160890161409b565b5092915050565b604081015161296690670de0b6b3a7640000614088565b34146129b45760405162461bcd60e51b815260206004820152601760248201527f4c5a3a2057726f6e6720616d6f756e74206f66204554480000000000000000006044820152606401610665565b6101008101516001600160a01b031615806129d1575060e0810151155b612a335760405162461bcd60e51b815260206004820152602d60248201527f4c5a3a2043616e6e6f742063686f6f736520696e746572657374206966206a6f60448201526c0d2dcd2dcce40c240c4c2e8c6d609b1b6064820152608401610665565b612a41338260200151613793565b6020820152612a4e613b0f565b612a997f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad387f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c0037583612ffc565b7f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b031663d0e30db0346040518263ffffffff1660e01b81526004015f604051808303818588803b158015612af2575f80fd5b505af1158015612b04573d5f803e3d5ffd5b50505050507f00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec5016001600160a01b03166399bff1217f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad3884606001515f8087604051602001612b7191906142f9565b6040516020818303038152906040526040518663ffffffff1660e01b8152600401612ba0959493929190614022565b5f604051808303815f87803b158015612bb7575f80fd5b505af1158015612bc9573d5f803e3d5ffd5b505050506108da81612dae565b5f828152600160208190526040822080549101546001600160a01b03918216919081169084163314801590612c145750336001600160a01b03831614155b15612c32576040516310bb5c9d60e31b815260040160405180910390fd5b6001600160a01b0381161580612c515750336001600160a01b03831614155b15612c605783925050506118d2565b949350505050565b604051630380585f60e21b8152600481018290525f907f0000000000000000000000002cd8d57c75feeefd05f84e3f68c6268daed9f08b6001600160a01b031690630e01617c906024016040805180830381865afa158015612ccc573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190612cf091906143c1565b9150506001600160a01b03811630146108da5760405162461bcd60e51b815260206004820152602960248201527f425a3a205a6170706572206973206e6f7420726563656976657220666f7220746044820152686869732074726f766560b81b6064820152608401610665565b81516001600160a01b03858116909152825184821660209091015282517f00000000000000000000000029219dd400f2bf60e5a23d13be72b486d4038894909116604090910152610ac982826135ea565b5f805b60048110156108435782518160048110612dcd57612dcd61429c565b602002015191506001600160a01b03821615610843576040516370a0823160e01b81523060048201525f906001600160a01b038416906370a0823190602401602060405180830381865afa158015612e27573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190612e4b9190614115565b905083602001518260048110612e6357612e6361429c565b60200201518111612e745750612f70565b83604001518260048110612e8a57612e8a61429c565b602002015115612f3057826001600160a01b031663c12ea3ca856060015186602001518560048110612ebe57612ebe61429c565b6020020151612ecd90856142b0565b6040518363ffffffff1660e01b8152600401612eea929190613f86565b6020604051808303815f875af1158015612f06573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190612f2a9190614115565b50612f6e565b612f6e846060015185602001518460048110612f4e57612f4e61429c565b6020020151612f5d90846142b0565b6001600160a01b03861691906137c6565b505b600101612db1565b5f828152602081905260409020546001600160a01b039081169082163314801590612fab57506001600160a01b03811615155b8015612fc05750336001600160a01b03821614155b15610843575f838152600160205260409020546001600160a01b0316338114610ac957604051636522e96960e01b815260040160405180910390fd5b61084383838333612d5d565b5f828152602081815260409182902080546001600160a01b0319166001600160a01b038516908117909155915191825283917f3942babd464ceb1c7d319f75245a8cd41334592b45507f072e7020e63c22a8dc910160405180910390a25050565b613073828261381c565b5f8381526001602081815260409283902080546001600160a01b03199081166001600160a01b038881169182178455929094018054909116918616918217905583519283529082015284917f649442545e0f313a6d8087b19bc47bd2bd9b63f79d23a773446e00d2ea01d169910160405180910390a2505050565b5f84156131415734861461313c5760405162461bcd60e51b815260206004820152601560248201527415d68e8815dc9bdb99c818dbdb1b08185b5bdd5b9d605a1b6044820152606401610665565b6131a6565b34156131a65760405162461bcd60e51b815260206004820152602e60248201527f575a3a204e6f7420616464696e6720636f6c6c2c206e6f204554482073686f7560448201526d1b19081899481c9958d95a5d995960921b6064820152608401610665565b5f6131b388888887613859565b90506132007f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad387f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c0037585612ffc565b8515613277577f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b031663d0e30db0886040518263ffffffff1660e01b81526004015f604051808303818588803b15801561325f575f80fd5b505af1158015613271573d5f803e3d5ffd5b50505050505b83613312576040516323b872dd60e01b8152336004820152306024820152604481018690527f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c003756001600160a01b0316906323b872dd906064016020604051808303815f875af11580156132ec573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906133109190613f9f565b505b979650505050505050565b82156133b35760405163a9059cbb60e01b81526001600160a01b037f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c00375169063a9059cbb906133719085908890600401613f86565b6020604051808303815f875af115801561338d573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906133b19190613f9f565b505b6040516370a0823160e01b81523060048201525f907f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c003756001600160a01b0316906370a0823190602401602060405180830381865afa158015613417573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061343b9190614115565b60208381015101519091508111156134f457606082015160208084015101517f0000000000000000000000004871c910877c4e89f72ec355ff709b2543c003756001600160a01b03169163a9059cbb9161349590856142b0565b6040518363ffffffff1660e01b81526004016134b2929190613f86565b6020604051808303815f875af11580156134ce573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906134f29190613f9f565b505b8515801561350157505f87115b1561189a57604051632e1a7d4d60e01b8152600481018890527f000000000000000000000000039e2fb66102314ce7b64ce5ce3e5183bc94ad386001600160a01b031690632e1a7d4d906024015f604051808303815f87803b158015613565575f80fd5b505af1158015613577573d5f803e3d5ffd5b505050505f836001600160a01b0316886040515f6040518083038185875af1925050503d805f81146135c4576040519150601f19603f3d011682016040523d82523d5f602084013e6135c9565b606091505b505090508061130a5760405162461bcd60e51b81526004016106659061426c565b5f5b60048110156136bf5782515f90826004811061360a5761360a61429c565b60200201516001600160a01b0316146136bf57825181600481106136305761363061429c565b60200201516040516370a0823160e01b81523060048201526001600160a01b03909116906370a0823190602401602060405180830381865afa158015613678573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061369c9190614115565b836020015182600481106136b2576136b261429c565b60200201526001016135ec565b506001600160a01b0316606090910152565b6040516331a9108f60e11b8152600481018290527f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422036001600160a01b031690636352211e90602401602060405180830381865afa158015613734573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906137589190613fba565b6001600160a01b0316336001600160a01b03161461378957604051631963d1e760e31b815260040160405180910390fd5b50565b5f6118d233835b5f82826040516020016137a7929190613f86565b60408051601f1981840301815291905280516020909101209392505050565b6108438363a9059cbb60e01b84846040516024016137e5929190613f86565b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b03199093169290921790915261392f565b6001600160a01b03821615801561383b57506001600160a01b03811615155b156108da57604051632235921760e01b815260040160405180910390fd5b6040516331a9108f60e11b8152600481018590525f9081906001600160a01b037f0000000000000000000000000e38e67de7f78c4d72f4b7b1894347d28fa422031690636352211e90602401602060405180830381865afa1580156138c0573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906138e49190613fba565b905080841580156138f457505f86115b806138fc5750835b1561391b5761390b8783612bd6565b905061391687612c68565b613925565b6139258783612f78565b9695505050505050565b5f613983826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316613a029092919063ffffffff16565b905080515f14806139a35750808060200190518101906139a39190613f9f565b6108435760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610665565b6060612c6084845f85855f80866001600160a01b03168587604051613a2791906143ee565b5f6040518083038185875af1925050503d805f8114613a61576040519150601f19603f3d011682016040523d82523d5f602084013e613a66565b606091505b50915091506133128783838760608315613ae05782515f03613ad9576001600160a01b0385163b613ad95760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610665565b5081612c60565b612c608383815115613af55781518083602001fd5b8060405162461bcd60e51b81526004016106659190614409565b6040518060800160405280613b22613b48565b8152602001613b2f613b48565b8152602001613b3c613b48565b81525f60209091015290565b60405180608001604052806004906020820280368337509192915050565b5f60208284031215613b76575f80fd5b5035919050565b5f60808284031215613b8d575f80fd5b50919050565b5f8060a08385031215613ba4575f80fd5b613bae8484613b7d565b946080939093013593505050565b6001600160a01b0381168114613789575f80fd5b8035613bdb81613bbc565b919050565b5f8060408385031215613bf1575f80fd5b823591506020830135613c0381613bbc565b809150509250929050565b5f60808284031215613c1e575f80fd5b613c288383613b7d565b9392505050565b5f8060408385031215613c40575f80fd5b50508035926020909101359150565b5f808284036101c0811215613c62575f80fd5b6101a080821215613c71575f80fd5b9395938601359450505050565b5f805f60608486031215613c90575f80fd5b505081359360208301359350604090920135919050565b8015158114613789575f80fd5b5f805f805f8060c08789031215613cc9575f80fd5b86359550602087013594506040870135613ce281613ca7565b9350606087013592506080870135613cf981613ca7565b8092505060a087013590509295509295509295565b5f60608284031215613b8d575f80fd5b5f8060808385031215613d2f575f80fd5b613d398484613d0e565b946060939093013593505050565b5f805f805f805f80610100898b031215613d5f575f80fd5b88359750602089013596506040890135613d7881613ca7565b9550606089013594506080890135613d8f81613ca7565b979a969950949793969560a0850135955060c08501359460e001359350915050565b5f805f60608486031215613dc3575f80fd5b833592506020840135613dd581613bbc565b91506040840135613de581613bbc565b809150509250925092565b5f60608284031215613e00575f80fd5b613c288383613d0e565b5f6101808284031215613b8d575f80fd5b6040516101a0810167ffffffffffffffff81118282101715613e4b57634e487b7160e01b5f52604160045260245ffd5b60405290565b604051610140810167ffffffffffffffff81118282101715613e4b57634e487b7160e01b5f52604160045260245ffd5b5f6101a08284031215613e92575f80fd5b613e9a613e1b565b613ea383613bd0565b81526020830135602082015260408301356040820152606083013560608201526080830135608082015260a083013560a082015260c083013560c082015260e083013560e0820152610100613ef9818501613bd0565b908201526101208381013590820152610140613f16818501613bd0565b90820152610160613f28848201613bd0565b90820152610180613f3a848201613bd0565b908201529392505050565b60208082526021908201527f4c5a3a2043616c6c6572206e6f7420466c6173684c6f616e2070726f766964656040820152603960f91b606082015260800190565b6001600160a01b03929092168252602082015260400190565b5f60208284031215613faf575f80fd5b8151613c2881613ca7565b5f60208284031215613fca575f80fd5b8151613c2881613bbc565b5f5b83811015613fef578181015183820152602001613fd7565b50505f910152565b5f815180845261400e816020860160208601613fd5565b601f01601f19169290920160200192915050565b6001600160a01b0386168152602081018590525f6004851061405257634e487b7160e01b5f52602160045260245ffd5b846040830152831515606083015260a0608083015261331260a0830184613ff7565b634e487b7160e01b5f52601160045260245ffd5b808201808211156118d2576118d2614074565b5f602082840312156140ab575f80fd5b8135613c2881613bbc565b6001600160a01b039b8c168152602081019a909a5260408a01989098526060890196909652608088019490945260a087019290925260c086015260e0850152841661010084015283166101208301529091166101408201526101600190565b5f60208284031215614125575f80fd5b5051919050565b81516001600160a01b0316815261016081016020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015161418a60c08401826001600160a01b03169052565b5060e083015160e0830152610100808401516141b0828501826001600160a01b03169052565b5050610120838101516001600160a01b038116848301525050610140838101516001600160a01b038116848301525b505092915050565b5f61014082840312156141f8575f80fd5b614200613e51565b825181526020830151602082015260408301516040820152606083015160608201526080830151608082015260a083015160a082015260c083015160c082015260e083015160e08201526101008084015181830152506101208084015181830152508091505092915050565b60208082526016908201527515d68e8814d95b991a5b99c81155120819985a5b195960521b604082015260600190565b634e487b7160e01b5f52603260045260245ffd5b818103818111156118d2576118d2614074565b80820281158282048414176118d2576118d2614074565b5f826142f457634e487b7160e01b5f52601260045260245ffd5b500490565b81516001600160a01b031681526101a081016020830151602083015260408301516040830152606083015160608301526080830151608083015260a083015160a083015260c083015160c083015260e083015160e08301526101008084015161436c828501826001600160a01b03169052565b50506101208381015190830152610140808401516001600160a01b038116828501525050610160838101516001600160a01b038116848301525050610180838101516001600160a01b038116848301526141df565b5f80604083850312156143d2575f80fd5b82516143dd81613bbc565b6020840151909250613c0381613bbc565b5f82516143ff818460208701613fd5565b9190910192915050565b602081525f613c286020830184613ff756fea2646970667358221220f4048805a1b9a4098e2c32d77f91b98637c79d4ba14be7e4032b6ad4fce45cf164736f6c63430008180033

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

0000000000000000000000000a41a0b63e3a1c5bd2069792c5baeab34690ab3500000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec50100000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d913900000000000000000000000029219dd400f2bf60e5a23d13be72b486d4038894

-----Decoded View---------------
Arg [0] : _addressesRegistry (address): 0x0A41a0B63e3a1c5bd2069792c5BaeAB34690Ab35
Arg [1] : _flashLoanProvider (address): 0x29c0e08c3B9087a1B935B3964d633b8d356Ec501
Arg [2] : _exchange (address): 0x15e7b49A112c6a36353b1f1C6353E4B2Cd9D9139
Arg [3] : _USDC (address): 0x29219dd400f2Bf60E5a23d13Be72B486D4038894

-----Encoded View---------------
4 Constructor Arguments found :
Arg [0] : 0000000000000000000000000a41a0b63e3a1c5bd2069792c5baeab34690ab35
Arg [1] : 00000000000000000000000029c0e08c3b9087a1b935b3964d633b8d356ec501
Arg [2] : 00000000000000000000000015e7b49a112c6a36353b1f1c6353e4b2cd9d9139
Arg [3] : 00000000000000000000000029219dd400f2bf60e5a23d13be72b486d4038894


Block Transaction Gas Used Reward
view all blocks ##produced##

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

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading
[ Download: CSV Export  ]
[ 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.