{
    "Function": "slitherConstructorVariables",
    "File": "contracts/Auction/Auction.sol",
    "Parent Contracts": [
        "contracts/StabilizedPoolExtensions/ProfitDistributorExtension.sol",
        "contracts/StabilizedPoolExtensions/DexHandlerExtension.sol",
        "contracts/StabilizedPoolExtensions/DataLabExtension.sol",
        "contracts/StabilizedPoolExtensions/StabilizerNodeExtension.sol",
        "contracts/StabilizedPoolExtensions/LiquidityExtensionExtension.sol",
        "contracts/StabilizedPoolExtensions/StabilizedPoolUnit.sol",
        "contracts/Permissions.sol",
        "lib/openzeppelin-contracts/contracts/security/ReentrancyGuard.sol",
        "lib/openzeppelin-contracts/contracts/access/AccessControl.sol",
        "lib/openzeppelin-contracts/contracts/utils/introspection/ERC165.sol",
        "lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol",
        "lib/openzeppelin-contracts/contracts/access/IAccessControl.sol",
        "lib/openzeppelin-contracts/contracts/utils/Context.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract Auction is\n  StabilizedPoolUnit,\n  LiquidityExtensionExtension,\n  StabilizerNodeExtension,\n  DataLabExtension,\n  DexHandlerExtension,\n  ProfitDistributorExtension\n{\n  using SafeERC20 for ERC20;\n\n  bytes32 public immutable AUCTION_AMENDER_ROLE;\n  bytes32 public immutable PROFIT_ALLOCATOR_ROLE;\n\n  address public amender;\n\n  uint256 public unclaimedArbTokens;\n  uint256 public replenishingAuctionId;\n  uint256 public currentAuctionId;\n  uint256 public claimableArbitrageRewards;\n  uint256 public nextCommitmentId;\n  uint256 public auctionLength = 600; // 10 minutes\n  uint256 public arbTokenReplenishSplitBps = 7000; // 70%\n  uint256 public maxAuctionEndBps = 9000; // 90% of target price\n  uint256 public auctionEndReserveBps = 9000; // 90% of collateral\n  uint256 public priceLookback = 0;\n  uint256 public reserveRatioLookback = 30; // 30 seconds\n  uint256 public dustThreshold = 1e15;\n  uint256 public earlyEndThreshold;\n  uint256 public costBufferBps = 1000;\n  uint256 private _replenishLimit = 10;\n\n  address public auctionStartController;\n\n  mapping(uint256 => AuctionData) internal idToAuction;\n  mapping(address => uint256[]) internal accountCommitmentEpochs;\n\n  event AuctionCommitment(\n    uint256 commitmentId,\n    uint256 auctionId,\n    address indexed account,\n    uint256 commitment,\n    uint256 purchased\n  );\n\n  event ClaimArbTokens(\n    uint256 auctionId,\n    address indexed account,\n    uint256 amountTokens\n  );\n\n  event AuctionEnded(\n    uint256 id,\n    uint256 commitments,\n    uint256 startingPrice,\n    uint256 finalPrice,\n    uint256 maltPurchased\n  );\n\n  event AuctionStarted(\n    uint256 id,\n    uint256 maxCommitments,\n    uint256 startingPrice,\n    uint256 endingPrice,\n    uint256 startingTime,\n    uint256 endingTime\n  );\n\n  event ArbTokenAllocation(\n    uint256 replenishingAuctionId,\n    uint256 maxArbAllocation\n  );\n\n  event SetAuctionLength(uint256 length);\n  event SetAuctionEndReserveBps(uint256 bps);\n  event SetDustThreshold(uint256 threshold);\n  event SetReserveRatioLookback(uint256 lookback);\n  event SetPriceLookback(uint256 lookback);\n  event SetMaxAuctionEnd(uint256 maxEnd);\n  event SetTokenReplenishSplit(uint256 split);\n  event SetAuctionStartController(address controller);\n  event SetAuctionReplenishId(uint256 id);\n  event SetEarlyEndThreshold(uint256 threshold);\n  event SetCostBufferBps(uint256 costBuffer);\n\n  constructor(\n    address timelock,\n    address repository,\n    address poolFactory,\n    uint256 _auctionLength,\n    uint256 _earlyEndThreshold\n  ) StabilizedPoolUnit(timelock, repository, poolFactory) {\n    auctionLength = _auctionLength;\n    earlyEndThreshold = _earlyEndThreshold;\n\n    // keccak256(\"AUCTION_AMENDER_ROLE\")\n    AUCTION_AMENDER_ROLE = 0x7cfd4d3ca87651951a5df4ff76005c956036fd9aa4b22e6e574caaa56f487f68;\n    // keccak256(\"PROFIT_ALLOCATOR_ROLE\")\n    PROFIT_ALLOCATOR_ROLE = 0x00ed6845b200b0f3e6539c45853016f38cb1b785c1d044aea74da930e58c7c4c;\n  }\n\n  function setupContracts(\n    address _collateralToken,\n    address _liquidityExtension,\n    address _stabilizerNode,\n    address _maltDataLab,\n    address _dexHandler,\n    address _amender,\n    address _profitDistributor,\n    address pool\n  ) external onlyRoleMalt(POOL_FACTORY_ROLE, \"Must be pool factory\") {\n    require(!contractActive, \"Auction: Already setup\");\n    require(_collateralToken != address(0), \"Auction: Col addr(0)\");\n    require(_liquidityExtension != address(0), \"Auction: LE addr(0)\");\n    require(_stabilizerNode != address(0), \"Auction: StabNode addr(0)\");\n    require(_maltDataLab != address(0), \"Auction: DataLab addr(0)\");\n    require(_dexHandler != address(0), \"Auction: DexHandler addr(0)\");\n    require(_amender != address(0), \"Auction: Amender addr(0)\");\n    require(_profitDistributor != address(0), \"Auction: ProfitDist addr(0)\");\n\n    contractActive = true;\n\n    _roleSetup(AUCTION_AMENDER_ROLE, _amender);\n    _roleSetup(PROFIT_ALLOCATOR_ROLE, _profitDistributor);\n    _setupRole(STABILIZER_NODE_ROLE, _stabilizerNode);\n\n    collateralToken = ERC20(_collateralToken);\n    liquidityExtension = ILiquidityExtension(_liquidityExtension);\n    stabilizerNode = IStabilizerNode(_stabilizerNode);\n    maltDataLab = IMaltDataLab(_maltDataLab);\n    dexHandler = IDexHandler(_dexHandler);\n    amender = _amender;\n    profitDistributor = IProfitDistributor(_profitDistributor);\n\n    (, address updater, ) = poolFactory.getPool(pool);\n    _setPoolUpdater(updater);\n  }\n\n  function _beforeSetStabilizerNode(address _stabilizerNode) internal override {\n    _transferRole(\n      _stabilizerNode,\n      address(stabilizerNode),\n      STABILIZER_NODE_ROLE\n    );\n  }\n\n  function _beforeSetProfitDistributor(address _profitDistributor)\n    internal\n    override\n  {\n    _transferRole(\n      _profitDistributor,\n      address(profitDistributor),\n      PROFIT_ALLOCATOR_ROLE\n    );\n  }\n\n  /*\n   * PUBLIC METHODS\n   */\n  function purchaseArbitrageTokens(uint256 amount, uint256 minPurchased)\n    external\n    nonReentrant\n    onlyActive\n  {\n    uint256 currentAuction = currentAuctionId;\n    require(auctionActive(currentAuction), \"No auction running\");\n    require(amount != 0, \"purchaseArb: 0 amount\");\n\n    uint256 oldBalance = collateralToken.balanceOf(address(liquidityExtension));\n\n    collateralToken.safeTransferFrom(\n      msg.sender,\n      address(liquidityExtension),\n      amount\n    );\n\n    uint256 realAmount = collateralToken.balanceOf(\n      address(liquidityExtension)\n    ) - oldBalance;\n\n    require(realAmount <= amount, \"Invalid amount\");\n\n    uint256 realCommitment = _capCommitment(currentAuction, realAmount);\n    require(realCommitment != 0, \"ArbTokens: Real Commitment 0\");\n\n    uint256 purchased = liquidityExtension.purchaseAndBurn(realCommitment);\n    require(purchased >= minPurchased, \"ArbTokens: Insufficient output\");\n\n    AuctionData storage auction = idToAuction[currentAuction];\n\n    require(\n      auction.startingTime <= block.timestamp,\n      \"Auction hasn't started yet\"\n    );\n    require(auction.endingTime > block.timestamp, \"Auction is already over\");\n    require(auction.active == true, \"Auction is not active\");\n\n    auction.commitments = auction.commitments + realCommitment;\n\n    if (auction.accountCommitments[msg.sender].commitment == 0) {\n      accountCommitmentEpochs[msg.sender].push(currentAuction);\n    }\n    auction.accountCommitments[msg.sender].commitment =\n      auction.accountCommitments[msg.sender].commitment +\n      realCommitment;\n    auction.accountCommitments[msg.sender].maltPurchased =\n      auction.accountCommitments[msg.sender].maltPurchased +\n      purchased;\n    auction.maltPurchased = auction.maltPurchased + purchased;\n\n    emit AuctionCommitment(\n      nextCommitmentId,\n      currentAuction,\n      msg.sender,\n      realCommitment,\n      purchased\n    );\n\n    nextCommitmentId = nextCommitmentId + 1;\n\n    if (auction.commitments + auction.pegPrice >= auction.maxCommitments) {\n      _endAuction(currentAuction);\n    }\n  }\n\n  function claimArbitrage(uint256 _auctionId) external nonReentrant onlyActive {\n    uint256 amountTokens = userClaimableArbTokens(msg.sender, _auctionId);\n\n    require(amountTokens > 0, \"No claimable Arb tokens\");\n\n    AuctionData storage auction = idToAuction[_auctionId];\n\n    require(!auction.active, \"Cannot claim tokens on an active auction\");\n\n    AccountCommitment storage commitment = auction.accountCommitments[\n      msg.sender\n    ];\n\n    uint256 redemption = (amountTokens * auction.finalPrice) / auction.pegPrice;\n    uint256 remaining = commitment.commitment -\n      commitment.redeemed -\n      commitment.exited;\n\n    if (redemption > remaining) {\n      redemption = remaining;\n    }\n\n    commitment.redeemed = commitment.redeemed + redemption;\n\n    // Unclaimed represents total outstanding, but not necessarily\n    // claimable yet.\n    // claimableArbitrageRewards represents total amount that is now\n    // available to be claimed\n    if (amountTokens > unclaimedArbTokens) {\n      unclaimedArbTokens = 0;\n    } else {\n      unclaimedArbTokens = unclaimedArbTokens - amountTokens;\n    }\n\n    if (amountTokens > claimableArbitrageRewards) {\n      claimableArbitrageRewards = 0;\n    } else {\n      claimableArbitrageRewards = claimableArbitrageRewards - amountTokens;\n    }\n\n    uint256 totalBalance = collateralToken.balanceOf(address(this));\n    if (amountTokens + dustThreshold >= totalBalance) {\n      amountTokens = totalBalance;\n    }\n\n    collateralToken.safeTransfer(msg.sender, amountTokens);\n\n    emit ClaimArbTokens(_auctionId, msg.sender, amountTokens);\n  }\n\n  function endAuctionEarly() external onlyActive {\n    uint256 currentId = currentAuctionId;\n    AuctionData storage auction = idToAuction[currentId];\n    require(\n      auction.active && block.timestamp >= auction.startingTime,\n      \"No auction running\"\n    );\n    require(\n      auction.commitments >= (auction.maxCommitments - earlyEndThreshold),\n      \"Too early to end\"\n    );\n\n    _endAuction(currentId);\n  }\n\n  /*\n   * PUBLIC VIEW FUNCTIONS\n   */\n  function isAuctionFinished(uint256 _id) public view returns (bool) {\n    AuctionData storage auction = idToAuction[_id];\n\n    return\n      auction.endingTime > 0 &&\n      (block.timestamp >= auction.endingTime ||\n        auction.finalPrice > 0 ||\n        auction.commitments + auction.pegPrice >= auction.maxCommitments);\n  }\n\n  function auctionActive(uint256 _id) public view returns (bool) {\n    AuctionData storage auction = idToAuction[_id];\n\n    return auction.active && block.timestamp >= auction.startingTime;\n  }\n\n  function isAuctionFinalized(uint256 _id) public view returns (bool) {\n    AuctionData storage auction = idToAuction[_id];\n    return auction.finalized;\n  }\n\n  function userClaimableArbTokens(address account, uint256 auctionId)\n    public\n    view\n    returns (uint256)\n  {\n    AuctionData storage auction = idToAuction[auctionId];\n\n    if (\n      auction.claimableTokens == 0 ||\n      auction.finalPrice == 0 ||\n      auction.commitments == 0\n    ) {\n      return 0;\n    }\n\n    AccountCommitment storage commitment = auction.accountCommitments[account];\n\n    uint256 totalTokens = (auction.commitments * auction.pegPrice) /\n      auction.finalPrice;\n\n    uint256 claimablePerc = (auction.claimableTokens * auction.pegPrice) /\n      totalTokens;\n\n    uint256 amountTokens = (commitment.commitment * auction.pegPrice) /\n      auction.finalPrice;\n    uint256 redeemedTokens = (commitment.redeemed * auction.pegPrice) /\n      auction.finalPrice;\n    uint256 exitedTokens = (commitment.exited * auction.pegPrice) /\n      auction.finalPrice;\n\n    uint256 amountOut = ((amountTokens * claimablePerc) / auction.pegPrice) -\n      redeemedTokens -\n      exitedTokens;\n\n    // Avoid leaving dust behind\n    if (amountOut < dustThreshold) {\n      return 0;\n    }\n\n    return amountOut;\n  }\n\n  function balanceOfArbTokens(uint256 _auctionId, address account)\n    public\n    view\n    returns (uint256)\n  {\n    AuctionData storage auction = idToAuction[_auctionId];\n\n    AccountCommitment storage commitment = auction.accountCommitments[account];\n\n    uint256 remaining = commitment.commitment -\n      commitment.redeemed -\n      commitment.exited;\n\n    uint256 price = auction.finalPrice;\n\n    if (auction.finalPrice == 0) {\n      price = currentPrice(_auctionId);\n    }\n\n    return (remaining * auction.pegPrice) / price;\n  }\n\n  function averageMaltPrice(uint256 _id) external view returns (uint256) {\n    AuctionData storage auction = idToAuction[_id];\n\n    if (auction.maltPurchased == 0) {\n      return 0;\n    }\n\n    return (auction.commitments * auction.pegPrice) / auction.maltPurchased;\n  }\n\n  function currentPrice(uint256 _id) public view returns (uint256) {\n    AuctionData storage auction = idToAuction[_id];\n\n    if (auction.startingTime == 0) {\n      return maltDataLab.priceTarget();\n    }\n\n    uint256 secondsSinceStart = 0;\n\n    if (block.timestamp > auction.startingTime) {\n      secondsSinceStart = block.timestamp - auction.startingTime;\n    }\n\n    uint256 auctionDuration = auction.endingTime - auction.startingTime;\n\n    if (secondsSinceStart >= auctionDuration) {\n      return auction.endingPrice;\n    }\n\n    uint256 totalPriceDelta = auction.startingPrice - auction.endingPrice;\n\n    uint256 currentPriceDelta = (totalPriceDelta * secondsSinceStart) /\n      auctionDuration;\n\n    return auction.startingPrice - currentPriceDelta;\n  }\n\n  function getAuctionCommitments(uint256 _id)\n    public\n    view\n    returns (uint256 commitments, uint256 maxCommitments)\n  {\n    AuctionData storage auction = idToAuction[_id];\n\n    return (auction.commitments, auction.maxCommitments);\n  }\n\n  function getAuctionPrices(uint256 _id)\n    public\n    view\n    returns (\n      uint256 startingPrice,\n      uint256 endingPrice,\n      uint256 finalPrice\n    )\n  {\n    AuctionData storage auction = idToAuction[_id];\n\n    return (auction.startingPrice, auction.endingPrice, auction.finalPrice);\n  }\n\n  function auctionExists(uint256 _id) public view returns (bool) {\n    AuctionData storage auction = idToAuction[_id];\n\n    return auction.startingTime > 0;\n  }\n\n  function getAccountCommitments(address account)\n    external\n    view\n    returns (\n      uint256[] memory auctions,\n      uint256[] memory commitments,\n      uint256[] memory awardedTokens,\n      uint256[] memory redeemedTokens,\n      uint256[] memory exitedTokens,\n      uint256[] memory finalPrice,\n      uint256[] memory claimable,\n      bool[] memory finished\n    )\n  {\n    uint256[] memory epochCommitments = accountCommitmentEpochs[account];\n\n    auctions = new uint256[](epochCommitments.length);\n    commitments = new uint256[](epochCommitments.length);\n    awardedTokens = new uint256[](epochCommitments.length);\n    redeemedTokens = new uint256[](epochCommitments.length);\n    exitedTokens = new uint256[](epochCommitments.length);\n    finalPrice = new uint256[](epochCommitments.length);\n    claimable = new uint256[](epochCommitments.length);\n    finished = new bool[](epochCommitments.length);\n\n    for (uint256 i = 0; i < epochCommitments.length; ++i) {\n      AuctionData storage auction = idToAuction[epochCommitments[i]];\n\n      AccountCommitment storage commitment = auction.accountCommitments[\n        account\n      ];\n\n      uint256 price = auction.finalPrice;\n\n      if (auction.finalPrice == 0) {\n        price = currentPrice(epochCommitments[i]);\n      }\n\n      auctions[i] = epochCommitments[i];\n      commitments[i] = commitment.commitment;\n      awardedTokens[i] = (commitment.commitment * auction.pegPrice) / price;\n      redeemedTokens[i] = (commitment.redeemed * auction.pegPrice) / price;\n      exitedTokens[i] = (commitment.exited * auction.pegPrice) / price;\n      finalPrice[i] = price;\n      claimable[i] = userClaimableArbTokens(account, epochCommitments[i]);\n      finished[i] = isAuctionFinished(epochCommitments[i]);\n    }\n  }\n\n  function getAccountCommitmentAuctions(address account)\n    external\n    view\n    returns (uint256[] memory)\n  {\n    return accountCommitmentEpochs[account];\n  }\n\n  function getAuctionParticipationForAccount(address account, uint256 auctionId)\n    external\n    view\n    returns (\n      uint256 commitment,\n      uint256 redeemed,\n      uint256 maltPurchased,\n      uint256 exited\n    )\n  {\n    AccountCommitment storage _commitment = idToAuction[auctionId]\n      .accountCommitments[account];\n\n    return (\n      _commitment.commitment,\n      _commitment.redeemed,\n      _commitment.maltPurchased,\n      _commitment.exited\n    );\n  }\n\n  function hasOngoingAuction() external view returns (bool) {\n    AuctionData storage auction = idToAuction[currentAuctionId];\n\n    return auction.startingTime > 0 && !auction.finalized;\n  }\n\n  function getActiveAuction()\n    external\n    view\n    returns (\n      uint256 auctionId,\n      uint256 maxCommitments,\n      uint256 commitments,\n      uint256 maltPurchased,\n      uint256 startingPrice,\n      uint256 endingPrice,\n      uint256 finalPrice,\n      uint256 pegPrice,\n      uint256 startingTime,\n      uint256 endingTime,\n      uint256 finalBurnBudget\n    )\n  {\n    AuctionData storage auction = idToAuction[currentAuctionId];\n\n    return (\n      currentAuctionId,\n      auction.maxCommitments,\n      auction.commitments,\n      auction.maltPurchased,\n      auction.startingPrice,\n      auction.endingPrice,\n      auction.finalPrice,\n      auction.pegPrice,\n      auction.startingTime,\n      auction.endingTime,\n      auction.finalBurnBudget\n    );\n  }\n\n  function getAuction(uint256 _id)\n    public\n    view\n    returns (\n      uint256 fullRequirement,\n      uint256 maxCommitments,\n      uint256 commitments,\n      uint256 startingPrice,\n      uint256 endingPrice,\n      uint256 finalPrice,\n      uint256 pegPrice,\n      uint256 startingTime,\n      uint256 endingTime,\n      uint256 finalBurnBudget,\n      uint256 exited\n    )\n  {\n    AuctionData storage auction = idToAuction[_id];\n\n    return (\n      auction.fullRequirement,\n      auction.maxCommitments,\n      auction.commitments,\n      auction.startingPrice,\n      auction.endingPrice,\n      auction.finalPrice,\n      auction.pegPrice,\n      auction.startingTime,\n      auction.endingTime,\n      auction.finalBurnBudget,\n      auction.exited\n    );\n  }\n\n  function getAuctionCore(uint256 _id)\n    public\n    view\n    returns (\n      uint256 auctionId,\n      uint256 commitments,\n      uint256 maltPurchased,\n      uint256 startingPrice,\n      uint256 finalPrice,\n      uint256 pegPrice,\n      uint256 startingTime,\n      uint256 endingTime,\n      uint256 preAuctionReserveRatio,\n      bool active\n    )\n  {\n    AuctionData storage auction = idToAuction[_id];\n\n    return (\n      _id,\n      auction.commitments,\n      auction.maltPurchased,\n      auction.startingPrice,\n      auction.finalPrice,\n      auction.pegPrice,\n      auction.startingTime,\n      auction.endingTime,\n      auction.preAuctionReserveRatio,\n      auction.active\n    );\n  }\n\n  /*\n   * INTERNAL FUNCTIONS\n   */\n  function _triggerAuction(\n    uint256 pegPrice,\n    uint256 rRatio,\n    uint256 purchaseAmount\n  ) internal returns (bool) {\n    if (auctionStartController != address(0)) {\n      bool success = IAuctionStartController(auctionStartController)\n        .checkForStart();\n      if (!success) {\n        return false;\n      }\n    }\n    uint256 _auctionIndex = currentAuctionId;\n\n    (uint256 startingPrice, uint256 endingPrice) = _calculateAuctionPricing(\n      rRatio,\n      purchaseAmount\n    );\n\n    AuctionData storage auction = idToAuction[_auctionIndex];\n\n    uint256 decimals = collateralToken.decimals();\n    uint256 maxCommitments = _calcRealMaxRaise(\n      purchaseAmount,\n      rRatio,\n      decimals\n    );\n\n    if (maxCommitments == 0) {\n      return false;\n    }\n\n    auction.fullRequirement = purchaseAmount; // fullRequirement\n    auction.maxCommitments = maxCommitments;\n    auction.startingPrice = startingPrice;\n    auction.endingPrice = endingPrice;\n    auction.pegPrice = pegPrice;\n    auction.startingTime = block.timestamp; // startingTime\n    auction.endingTime = block.timestamp + auctionLength; // endingTime\n    auction.active = true; // active\n    auction.preAuctionReserveRatio = rRatio; // preAuctionReserveRatio\n    auction.finalized = false; // finalized\n\n    require(\n      auction.endingTime == uint256(uint64(auction.endingTime)),\n      \"ending not eq\"\n    );\n\n    emit AuctionStarted(\n      _auctionIndex,\n      auction.maxCommitments,\n      auction.startingPrice,\n      auction.endingPrice,\n      auction.startingTime,\n      auction.endingTime\n    );\n    return true;\n  }\n\n  function _capCommitment(uint256 _id, uint256 _commitment)\n    internal\n    view\n    returns (uint256 realCommitment)\n  {\n    AuctionData storage auction = idToAuction[_id];\n\n    realCommitment = _commitment;\n\n    if (auction.commitments + _commitment >= auction.maxCommitments) {\n      realCommitment = auction.maxCommitments - auction.commitments;\n    }\n  }\n\n  function _endAuction(uint256 _id) internal {\n    AuctionData storage auction = idToAuction[_id];\n\n    require(auction.active == true, \"Auction is already over\");\n\n    auction.active = false;\n    auction.finalPrice = currentPrice(_id);\n\n    uint256 amountArbTokens = (auction.commitments * auction.pegPrice) /\n      auction.finalPrice;\n    unclaimedArbTokens = unclaimedArbTokens + amountArbTokens;\n\n    emit AuctionEnded(\n      _id,\n      auction.commitments,\n      auction.startingPrice,\n      auction.finalPrice,\n      auction.maltPurchased\n    );\n  }\n\n  function _finalizeAuction(uint256 auctionId) internal {\n    (\n      uint256 avgMaltPrice,\n      uint256 commitments,\n      uint256 fullRequirement,\n      uint256 maltPurchased,\n      uint256 finalPrice,\n      uint256 preAuctionReserveRatio\n    ) = _setupAuctionFinalization(auctionId);\n\n    if (commitments >= fullRequirement) {\n      return;\n    }\n\n    uint256 priceTarget = maltDataLab.priceTarget();\n\n    // priceTarget - preAuctionReserveRatio represents maximum deficit per token\n    // priceTarget divided by the max deficit is equivalent to 1 over the max deficit given we are in uint decimal\n    // (commitments * 1/maxDeficit) - commitments\n    uint256 maxBurnSpend = (commitments * priceTarget) /\n      (priceTarget - preAuctionReserveRatio) -\n      commitments;\n\n    uint256 totalTokens = (commitments * priceTarget) / finalPrice;\n\n    uint256 premiumExcess = 0;\n\n    // The assumption here is that each token will be worth 1 Malt when redeemed.\n    // Therefore if totalTokens is greater than the malt purchased then there is a net supply growth\n    // After the tokens are repaid. We want this process to be neutral to supply at the very worst.\n    if (totalTokens > maltPurchased) {\n      // This also assumes current purchase price of Malt is $1, which is higher than it will be in practice.\n      // So the premium excess will actually ensure slight net negative supply growth.\n      premiumExcess = totalTokens - maltPurchased;\n    }\n\n    uint256 realBurnBudget = maltDataLab.getRealBurnBudget(\n      maxBurnSpend,\n      premiumExcess\n    );\n\n    if (realBurnBudget > 0) {\n      AuctionData storage auction = idToAuction[auctionId];\n\n      auction.finalBurnBudget = realBurnBudget;\n      liquidityExtension.allocateBurnBudget(realBurnBudget);\n    }\n  }\n\n  function _setupAuctionFinalization(uint256 auctionId)\n    internal\n    returns (\n      uint256 avgMaltPrice,\n      uint256 commitments,\n      uint256 fullRequirement,\n      uint256 maltPurchased,\n      uint256 finalPrice,\n      uint256 preAuctionReserveRatio\n    )\n  {\n    AuctionData storage auction = idToAuction[auctionId];\n    require(auction.startingTime > 0, \"No auction available for the given id\");\n\n    auction.finalized = true;\n\n    if (auction.maltPurchased > 0) {\n      avgMaltPrice =\n        (auction.commitments * auction.pegPrice) /\n        auction.maltPurchased;\n    }\n\n    return (\n      avgMaltPrice,\n      auction.commitments,\n      auction.fullRequirement,\n      auction.maltPurchased,\n      auction.finalPrice,\n      auction.preAuctionReserveRatio\n    );\n  }\n\n  function _calcRealMaxRaise(\n    uint256 purchaseAmount,\n    uint256 rRatio,\n    uint256 decimals\n  ) internal pure returns (uint256) {\n    uint256 unity = 10**decimals;\n    uint256 realBurn = (purchaseAmount * Math.min(rRatio, unity)) / unity;\n\n    if (purchaseAmount > realBurn) {\n      return purchaseAmount - realBurn;\n    }\n\n    return 0;\n  }\n\n  function _calculateAuctionPricing(uint256 rRatio, uint256 maxCommitments)\n    internal\n    view\n    returns (uint256 startingPrice, uint256 endingPrice)\n  {\n    uint256 priceTarget = maltDataLab.priceTarget();\n    if (rRatio > priceTarget) {\n      rRatio = priceTarget;\n    }\n    startingPrice = maltDataLab.maltPriceAverage(priceLookback);\n    uint256 liquidityExtensionBalance = collateralToken.balanceOf(\n      address(liquidityExtension)\n    );\n\n    (uint256 latestPrice, ) = maltDataLab.lastMaltPrice();\n    uint256 expectedMaltCost = priceTarget;\n    if (latestPrice < priceTarget) {\n      expectedMaltCost =\n        latestPrice +\n        ((priceTarget - latestPrice) * (5000 + costBufferBps)) /\n        10000;\n    }\n\n    // rRatio should never be large enough for this to overflow\n    // uint256 absoluteBottom = rRatio * auctionEndReserveBps / 10000;\n\n    // Absolute bottom is the lowest price\n    uint256 decimals = collateralToken.decimals();\n    uint256 unity = 10**decimals;\n    uint256 absoluteBottom = (maxCommitments * unity) /\n      (liquidityExtensionBalance +\n        ((maxCommitments * unity) / expectedMaltCost));\n\n    uint256 idealBottom = 1; // 1wei just to avoid any issues with it being 0\n\n    if (expectedMaltCost > rRatio) {\n      idealBottom = expectedMaltCost - rRatio;\n    }\n\n    // price should never go below absoluteBottom\n    if (idealBottom < absoluteBottom) {\n      idealBottom = absoluteBottom;\n    }\n\n    // price should never start above the peg price\n    if (startingPrice > priceTarget) {\n      startingPrice = priceTarget;\n    }\n\n    if (idealBottom < startingPrice) {\n      endingPrice = idealBottom;\n    } else if (absoluteBottom < startingPrice) {\n      endingPrice = absoluteBottom;\n    } else {\n      // There are no bottom prices that work with\n      // the startingPrice so set start and end to\n      // the absoluteBottom\n      startingPrice = absoluteBottom;\n      endingPrice = absoluteBottom;\n    }\n\n    // priceTarget should never be large enough to overflow here\n    uint256 maxPrice = (priceTarget * maxAuctionEndBps) / 10000;\n\n    if (endingPrice > maxPrice && maxPrice > absoluteBottom) {\n      endingPrice = maxPrice;\n    }\n  }\n\n  function _checkAuctionFinalization() internal {\n    uint256 currentAuction = currentAuctionId;\n\n    if (isAuctionFinished(currentAuction)) {\n      if (auctionActive(currentAuction)) {\n        _endAuction(currentAuction);\n      }\n\n      if (!isAuctionFinalized(currentAuction)) {\n        _finalizeAuction(currentAuction);\n      }\n      currentAuctionId = currentAuction + 1;\n    }\n  }\n\n  /*\n   * PRIVILEDGED FUNCTIONS\n   */\n  function checkAuctionFinalization()\n    external\n    onlyRoleMalt(STABILIZER_NODE_ROLE, \"Must be stabilizer node\")\n    onlyActive\n  {\n    _checkAuctionFinalization();\n  }\n\n  function accountExit(\n    address account,\n    uint256 auctionId,\n    uint256 amount\n  )\n    external\n    onlyRoleMalt(AUCTION_AMENDER_ROLE, \"Only auction amender\")\n    onlyActive\n  {\n    AuctionData storage auction = idToAuction[auctionId];\n    require(\n      auction.accountCommitments[account].commitment >= amount,\n      \"amend: amount underflows\"\n    );\n\n    if (auction.finalPrice == 0) {\n      return;\n    }\n\n    auction.exited += amount;\n    auction.accountCommitments[account].exited += amount;\n\n    uint256 amountArbTokens = (amount * auction.pegPrice) / auction.finalPrice;\n\n    if (amountArbTokens > unclaimedArbTokens) {\n      unclaimedArbTokens = 0;\n    } else {\n      unclaimedArbTokens = unclaimedArbTokens - amountArbTokens;\n    }\n  }\n\n  function allocateArbRewards(uint256 rewarded)\n    external\n    onlyRoleMalt(PROFIT_ALLOCATOR_ROLE, \"Must be profit allocator node\")\n    onlyActive\n    returns (uint256)\n  {\n    AuctionData storage auction;\n    uint256 replenishingId = replenishingAuctionId; // gas\n    uint256 absorbedCapital;\n    uint256 count = 1;\n    uint256 maxArbAllocation = (rewarded * arbTokenReplenishSplitBps) / 10000;\n\n    // Limit iterations to avoid unbounded loops\n    while (count < _replenishLimit) {\n      auction = idToAuction[replenishingId];\n\n      if (\n        auction.finalPrice == 0 ||\n        auction.startingTime == 0 ||\n        !auction.finalized\n      ) {\n        // if finalPrice or startingTime are not set then this auction has not happened yet\n        // So we are at the end of the journey\n        break;\n      }\n\n      if (auction.commitments > 0) {\n        uint256 totalTokens = (auction.commitments * auction.pegPrice) /\n          auction.finalPrice;\n\n        if (auction.claimableTokens < totalTokens) {\n          uint256 requirement = totalTokens - auction.claimableTokens;\n\n          uint256 usable = maxArbAllocation - absorbedCapital;\n\n          if (absorbedCapital + requirement < maxArbAllocation) {\n            usable = requirement;\n          }\n\n          auction.claimableTokens = auction.claimableTokens + usable;\n          rewarded = rewarded - usable;\n          claimableArbitrageRewards = claimableArbitrageRewards + usable;\n\n          absorbedCapital += usable;\n\n          emit ArbTokenAllocation(replenishingId, usable);\n\n          if (auction.claimableTokens < totalTokens) {\n            break;\n          }\n        }\n      }\n\n      replenishingId += 1;\n      count += 1;\n    }\n\n    replenishingAuctionId = replenishingId;\n\n    if (absorbedCapital != 0) {\n      collateralToken.safeTransferFrom(\n        address(profitDistributor),\n        address(this),\n        absorbedCapital\n      );\n    }\n\n    return rewarded;\n  }\n\n  function triggerAuction(uint256 pegPrice, uint256 purchaseAmount)\n    external\n    onlyRoleMalt(STABILIZER_NODE_ROLE, \"Must be stabilizer node\")\n    onlyActive\n    returns (bool)\n  {\n    if (purchaseAmount == 0 || auctionExists(currentAuctionId)) {\n      return false;\n    }\n\n    // Data is consistent here as this method as the stabilizer\n    // calls maltDataLab.trackPool at the start of stabilize\n    (uint256 rRatio, ) = liquidityExtension.reserveRatioAverage(\n      reserveRatioLookback\n    );\n\n    return _triggerAuction(pegPrice, rRatio, purchaseAmount);\n  }\n\n  function setAuctionLength(uint256 _length)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_length > 0, \"Length must be larger than 0\");\n    auctionLength = _length;\n    emit SetAuctionLength(_length);\n  }\n\n  function setAuctionReplenishId(uint256 _id)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    replenishingAuctionId = _id;\n    emit SetAuctionReplenishId(_id);\n  }\n\n  function setAuctionAmender(address _amender)\n    external\n    onlyRoleMalt(POOL_UPDATER_ROLE, \"Must have pool updater privilege\")\n  {\n    require(_amender != address(0), \"Cannot set 0 address\");\n    _transferRole(_amender, amender, AUCTION_AMENDER_ROLE);\n    amender = _amender;\n  }\n\n  function setAuctionStartController(address _controller)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    // This is allowed to be set to address(0) as its checked before calling methods on it\n    auctionStartController = _controller;\n    emit SetAuctionStartController(_controller);\n  }\n\n  function setTokenReplenishSplit(uint256 _split)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_split != 0 && _split <= 10000, \"Must be between 0-100%\");\n    arbTokenReplenishSplitBps = _split;\n    emit SetTokenReplenishSplit(_split);\n  }\n\n  function setMaxAuctionEnd(uint256 _maxEnd)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_maxEnd != 0 && _maxEnd <= 10000, \"Must be between 0-100%\");\n    maxAuctionEndBps = _maxEnd;\n    emit SetMaxAuctionEnd(_maxEnd);\n  }\n\n  function setPriceLookback(uint256 _lookback)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_lookback > 0, \"Must be above 0\");\n    priceLookback = _lookback;\n    emit SetPriceLookback(_lookback);\n  }\n\n  function setReserveRatioLookback(uint256 _lookback)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_lookback > 0, \"Must be above 0\");\n    reserveRatioLookback = _lookback;\n    emit SetReserveRatioLookback(_lookback);\n  }\n\n  function setAuctionEndReserveBps(uint256 _bps)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_bps != 0 && _bps < 10000, \"Must be between 0-100%\");\n    auctionEndReserveBps = _bps;\n    emit SetAuctionEndReserveBps(_bps);\n  }\n\n  function setDustThreshold(uint256 _threshold)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_threshold > 0, \"Must be between greater than 0\");\n    dustThreshold = _threshold;\n    emit SetDustThreshold(_threshold);\n  }\n\n  function setEarlyEndThreshold(uint256 _threshold)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_threshold > 0, \"Must be between greater than 0\");\n    earlyEndThreshold = _threshold;\n    emit SetEarlyEndThreshold(_threshold);\n  }\n\n  function setCostBufferBps(uint256 _costBuffer)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_costBuffer != 0 && _costBuffer <= 5000, \"Must be > 0 && <= 5000\");\n    costBufferBps = _costBuffer;\n    emit SetCostBufferBps(_costBuffer);\n  }\n\n  function adminEndAuction()\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    uint256 currentId = currentAuctionId;\n    require(auctionActive(currentId), \"No auction running\");\n    _endAuction(currentId);\n  }\n\n  function setReplenishLimit(uint256 _limit)\n    external\n    onlyRoleMalt(ADMIN_ROLE, \"Must have admin privilege\")\n  {\n    require(_limit != 0, \"Not 0\");\n    _replenishLimit = _limit;\n  }\n\n  function _accessControl()\n    internal\n    override(\n      LiquidityExtensionExtension,\n      StabilizerNodeExtension,\n      DataLabExtension,\n      DexHandlerExtension,\n      ProfitDistributorExtension\n    )\n  {\n    _onlyRoleMalt(POOL_UPDATER_ROLE, \"Must have pool updater role\");\n  }\n}"
}