EquityVault

0xd635aa5436e2631dc42c1bbe1609fc0d2519dbdf

Verification
Verified
v0.8.24+commit.e11b9ed9
Type
Contract
11,503 bytes
ABI entries
116
31 read · 28 write
License
none

Contract information

Address
0xd635aa5436e2631dc42c1bbe1609fc0d2519dbdf
Chain
Robinhood Chain (4663)
Compiler
v0.8.24+commit.e11b9ed9
Optimization
Enabled
Creation tx
0xf38bcf383b…7a161336a6

Token

Name
Element Equity LP
Symbol
eqLP
Decimals
6
Holders
2

Read contract (31)

GENESIS_MIN_DEPOSIT()uint256
MINIMUM_LIQUIDITY()uint256
adlState(bytes32)uint128, uint64
allowance(address, address)uint256
autoPayoutsFrozen()bool
balanceOf(address)uint256
cumulativeFees()uint256
decimals()uint8
depositFeeBps()uint16
engine()address
escrowedCollateral()uint256
freeLiquidity()uint256
insuranceBalance()uint256
lpFeeBps()uint16
lpPrincipal()uint256
marketRisk(bytes32)uint256, uint256, uint128, uint128
minFreeLiquidityBps()uint16
name()string
navSolvent()bool
owner()address
paused()bool
pendingOwner()address
queue(bytes32, uint256)address, uint128, uint64, uint8
queueTotalOwed()uint256
sideBucket()uint256
symbol()string
totalReserved()uint256
totalSupply()uint256
totalUsdg()uint256
usdg()address
withdrawFeeBps()uint16

Events (26)

AdlHaircutAppliedApprovalAutoPayoutsFrozenSetCollateralCreditedDepositedEngineSetEnqueuedFeeAccruedFeesSetFeesSweptHarvestedInsuranceAccruedInsuranceWithdrawnLiquidatorReimbursedLpFeeBpsSetMinFreeLiquidityBpsSetOwnershipTransferStartedOwnershipTransferredPausedSetPoolReimbursedFromInsuranceQueueEntryVoidedReserveUpdatedSettledSideBucketDrainedTransferWithdrawn

ABI

[
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "usdg_",
        "type": "address"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "constructor"
  },
  {
    "inputs": [],
    "name": "BadBps",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "BadFeeBps",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "BadLpFeeBps",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "BadSide",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "DustMint",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "spender",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "allowance",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "needed",
        "type": "uint256"
      }
    ],
    "name": "ERC20InsufficientAllowance",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "sender",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "balance",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "needed",
        "type": "uint256"
      }
    ],
    "name": "ERC20InsufficientBalance",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "approver",
        "type": "address"
      }
    ],
    "name": "ERC20InvalidApprover",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "receiver",
        "type": "address"
      }
    ],
    "name": "ERC20InvalidReceiver",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "sender",
        "type": "address"
      }
    ],
    "name": "ERC20InvalidSender",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "spender",
        "type": "address"
      }
    ],
    "name": "ERC20InvalidSpender",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "GenesisBelowMinimum",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "InsufficientFreeLiquidity",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "NavInsolvent",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "NotEngine",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "NotPending",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "owner",
        "type": "address"
      }
    ],
    "name": "OwnableInvalidOwner",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "account",
        "type": "address"
      }
    ],
    "name": "OwnableUnauthorizedAccount",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "PhantomNav",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "uint8",
        "name": "bits",
        "type": "uint8"
      },
      {
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "SafeCastOverflowedUintDowncast",
    "type": "error"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "token",
        "type": "address"
      }
    ],
    "name": "SafeERC20FailedOperation",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "Slippage",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "StrandedGenesis",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "StrandedPool",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "VaultPaused",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "ZeroAddress",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "ZeroAmount",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "ZeroEnqueue",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "ZeroNav",
    "type": "error"
  },
  {
    "inputs": [],
    "name": "ZeroSupply",
    "type": "error"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "indexed": false,
        "internalType": "uint16",
        "name": "fBps",
        "type": "uint16"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "totalCut",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "cutsDone",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint128",
        "name": "epoch",
        "type": "uint128"
      }
    ],
    "name": "AdlHaircutApplied",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "owner",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "spender",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "Approval",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "bool",
        "name": "frozen",
        "type": "bool"
      }
    ],
    "name": "AutoPayoutsFrozenSet",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "CollateralCredited",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "lp",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "fee",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "lpMinted",
        "type": "uint256"
      }
    ],
    "name": "Deposited",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "engine",
        "type": "address"
      }
    ],
    "name": "EngineSet",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "indexed": true,
        "internalType": "uint256",
        "name": "idx",
        "type": "uint256"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "payee",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "Enqueued",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "bool",
        "name": "alreadyInPool",
        "type": "bool"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "lpShare",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "sweptToCumulative",
        "type": "uint256"
      }
    ],
    "name": "FeeAccrued",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint16",
        "name": "depositFeeBps",
        "type": "uint16"
      },
      {
        "indexed": false,
        "internalType": "uint16",
        "name": "withdrawFeeBps",
        "type": "uint16"
      }
    ],
    "name": "FeesSet",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "FeesSwept",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "indexed": true,
        "internalType": "uint256",
        "name": "idx",
        "type": "uint256"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "owner",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "fromSide",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "fromLp",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "fromInsurance",
        "type": "uint256"
      }
    ],
    "name": "Harvested",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "InsuranceAccrued",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "InsuranceWithdrawn",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "liquidator",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "paid",
        "type": "uint256"
      }
    ],
    "name": "LiquidatorReimbursed",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint16",
        "name": "lpFeeBps",
        "type": "uint16"
      }
    ],
    "name": "LpFeeBpsSet",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint16",
        "name": "bps",
        "type": "uint16"
      }
    ],
    "name": "MinFreeLiquidityBpsSet",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "previousOwner",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "newOwner",
        "type": "address"
      }
    ],
    "name": "OwnershipTransferStarted",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "previousOwner",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "newOwner",
        "type": "address"
      }
    ],
    "name": "OwnershipTransferred",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "bool",
        "name": "paused",
        "type": "bool"
      }
    ],
    "name": "PausedSet",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "PoolReimbursedFromInsurance",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "indexed": true,
        "internalType": "uint256",
        "name": "idx",
        "type": "uint256"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "owner",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "QueueEntryVoided",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "indexed": false,
        "internalType": "uint8",
        "name": "side",
        "type": "uint8"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "newReservedLong",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "newReservedShort",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "totalReserved",
        "type": "uint256"
      }
    ],
    "name": "ReserveUpdated",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "payee",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "paid",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "badDebt",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "lpGain",
        "type": "uint256"
      }
    ],
    "name": "Settled",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "SideBucketDrained",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "from",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "Transfer",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "lp",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "lpBurned",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "netReturn",
        "type": "uint256"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "fee",
        "type": "uint256"
      }
    ],
    "name": "Withdrawn",
    "type": "event"
  },
  {
    "inputs": [],
    "name": "GENESIS_MIN_DEPOSIT",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "MINIMUM_LIQUIDITY",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "acceptOwnership",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      },
      {
        "internalType": "bool",
        "name": "alreadyInPool",
        "type": "bool"
      }
    ],
    "name": "accrueFee",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "accrueToInsurance",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "",
        "type": "bytes32"
      }
    ],
    "name": "adlState",
    "outputs": [
      {
        "internalType": "uint128",
        "name": "currentEpoch",
        "type": "uint128"
      },
      {
        "internalType": "uint64",
        "name": "lastCutIdx",
        "type": "uint64"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "owner",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "spender",
        "type": "address"
      }
    ],
    "name": "allowance",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "spender",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "approve",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "autoPayoutsFrozen",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "account",
        "type": "address"
      }
    ],
    "name": "balanceOf",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "fee",
        "type": "uint256"
      }
    ],
    "name": "chargeEscrowedFee",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "creditCollateral",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "cumulativeFees",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "decimals",
    "outputs": [
      {
        "internalType": "uint8",
        "name": "",
        "type": "uint8"
      }
    ],
    "stateMutability": "pure",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "minLpOut",
        "type": "uint256"
      }
    ],
    "name": "deposit",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "depositFeeBps",
    "outputs": [
      {
        "internalType": "uint16",
        "name": "",
        "type": "uint16"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "engine",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "escrowedCollateral",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "freeLiquidity",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "internalType": "uint16",
        "name": "fBps",
        "type": "uint16"
      },
      {
        "internalType": "uint8",
        "name": "maxEntries",
        "type": "uint8"
      },
      {
        "internalType": "uint256",
        "name": "deficitCap",
        "type": "uint256"
      }
    ],
    "name": "haircutQueueTail",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "totalCut",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "cutsDone",
        "type": "uint256"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "internalType": "uint256",
        "name": "maxEntries",
        "type": "uint256"
      }
    ],
    "name": "harvest",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "insuranceBalance",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "lpFeeBps",
    "outputs": [
      {
        "internalType": "uint16",
        "name": "",
        "type": "uint16"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "lpPrincipal",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "",
        "type": "bytes32"
      }
    ],
    "name": "marketRisk",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "reservedLong",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "reservedShort",
        "type": "uint256"
      },
      {
        "internalType": "uint128",
        "name": "queueHead",
        "type": "uint128"
      },
      {
        "internalType": "uint128",
        "name": "queueTail",
        "type": "uint128"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "minFreeLiquidityBps",
    "outputs": [
      {
        "internalType": "uint16",
        "name": "",
        "type": "uint16"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "name",
    "outputs": [
      {
        "internalType": "string",
        "name": "",
        "type": "string"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "navSolvent",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "owner",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "paused",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "pendingOwner",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "",
        "type": "bytes32"
      },
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "name": "queue",
    "outputs": [
      {
        "internalType": "address",
        "name": "owner",
        "type": "address"
      },
      {
        "internalType": "uint128",
        "name": "amount",
        "type": "uint128"
      },
      {
        "internalType": "uint64",
        "name": "enqueuedAt",
        "type": "uint64"
      },
      {
        "internalType": "uint8",
        "name": "status",
        "type": "uint8"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "queueTotalOwed",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "liquidator",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "shortfall",
        "type": "uint256"
      }
    ],
    "name": "reimburseLiquidator",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "paid",
        "type": "uint256"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "reimbursePoolFromInsurance",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "internalType": "uint8",
        "name": "side",
        "type": "uint8"
      },
      {
        "internalType": "uint256",
        "name": "reserved",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "directPayout",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "enqueueAmount",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "badDebt",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "lpGain",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "collateralReleased",
        "type": "uint256"
      },
      {
        "internalType": "address",
        "name": "payee",
        "type": "address"
      }
    ],
    "name": "releaseAndEnqueue",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "internalType": "uint8",
        "name": "side",
        "type": "uint8"
      },
      {
        "internalType": "uint256",
        "name": "reserved",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "netReturn",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "badDebt",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "lpGain",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "collateralReleased",
        "type": "uint256"
      },
      {
        "internalType": "address",
        "name": "payee",
        "type": "address"
      }
    ],
    "name": "releaseAndSettle",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "actuallyPaid",
        "type": "uint256"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "renounceOwnership",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bool",
        "name": "frozen",
        "type": "bool"
      }
    ],
    "name": "setAutoPayoutsFrozen",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "engine_",
        "type": "address"
      }
    ],
    "name": "setEngine",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint16",
        "name": "depositBps",
        "type": "uint16"
      },
      {
        "internalType": "uint16",
        "name": "withdrawBps",
        "type": "uint16"
      }
    ],
    "name": "setFees",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint16",
        "name": "bps",
        "type": "uint16"
      }
    ],
    "name": "setLpFeeBps",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint16",
        "name": "bps",
        "type": "uint16"
      }
    ],
    "name": "setMinFreeLiquidityBps",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bool",
        "name": "paused_",
        "type": "bool"
      }
    ],
    "name": "setPaused",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "sideBucket",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "to",
        "type": "address"
      }
    ],
    "name": "sweepFees",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "symbol",
    "outputs": [
      {
        "internalType": "string",
        "name": "",
        "type": "string"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "totalReserved",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "totalSupply",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "totalUsdg",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "transfer",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "from",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "transferFrom",
    "outputs": [
      {
        "internalType": "bool",
        "name": "",
        "type": "bool"
      }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "newOwner",
        "type": "address"
      }
    ],
    "name": "transferOwnership",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "internalType": "uint8",
        "name": "side",
        "type": "uint8"
      },
      {
        "internalType": "int256",
        "name": "delta",
        "type": "int256"
      }
    ],
    "name": "updateReserve",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "usdg",
    "outputs": [
      {
        "internalType": "contract IERC20",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "marketId",
        "type": "bytes32"
      },
      {
        "internalType": "uint256",
        "name": "idx",
        "type": "uint256"
      }
    ],
    "name": "voidQueueEntry",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "lpAmount",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "minOut",
        "type": "uint256"
      }
    ],
    "name": "withdraw",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "withdrawFeeBps",
    "outputs": [
      {
        "internalType": "uint16",
        "name": "",
        "type": "uint16"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      }
    ],
    "name": "withdrawInsurance",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  }
]

Source code

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

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {Ownable2Step, Ownable} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {VaultMath} from "./libs/VaultMath.sol";

contract EquityVault is ERC20, Ownable2Step {
    using SafeERC20 for IERC20;

    error NotEngine();
    error NavInsolvent();
    error VaultPaused();
    error ZeroAmount();
    error ZeroAddress();

    error PhantomNav();
    error GenesisBelowMinimum();

    error StrandedGenesis();
    error DustMint();
    error Slippage();
    error InsufficientFreeLiquidity();
    error BadLpFeeBps();
    error BadFeeBps();
    error BadSide();
    error BadBps();
    error ZeroEnqueue();
    error NotPending();

    event Deposited(address indexed lp, uint256 amount, uint256 fee, uint256 lpMinted);
    event Withdrawn(address indexed lp, uint256 lpBurned, uint256 netReturn, uint256 fee);
    event EngineSet(address indexed engine);
    event PausedSet(bool paused);
    event FeesSet(uint16 depositFeeBps, uint16 withdrawFeeBps);
    event LpFeeBpsSet(uint16 lpFeeBps);
    event MinFreeLiquidityBpsSet(uint16 bps);
    event FeesSwept(address indexed to, uint256 amount);
    event CollateralCredited(uint256 amount);
    event ReserveUpdated(
        bytes32 indexed marketId, uint8 side, uint256 newReservedLong, uint256 newReservedShort, uint256 totalReserved
    );
    event FeeAccrued(uint256 amount, bool alreadyInPool, uint256 lpShare, uint256 sweptToCumulative);
    event Settled(bytes32 indexed marketId, address indexed payee, uint256 paid, uint256 badDebt, uint256 lpGain);
    event Enqueued(bytes32 indexed marketId, uint256 indexed idx, address indexed payee, uint256 amount);
    event AutoPayoutsFrozenSet(bool frozen);
    event Harvested(
        bytes32 indexed marketId,
        uint256 indexed idx,
        address indexed owner,
        uint256 amount,
        uint256 fromSide,
        uint256 fromLp,
        uint256 fromInsurance
    );
    event SideBucketDrained(uint256 amount);
    event QueueEntryVoided(bytes32 indexed marketId, uint256 indexed idx, address indexed owner, uint256 amount);
    event AdlHaircutApplied(bytes32 indexed marketId, uint16 fBps, uint256 totalCut, uint256 cutsDone, uint128 epoch);
    event InsuranceAccrued(uint256 amount);
    event LiquidatorReimbursed(address indexed liquidator, uint256 paid);
    event InsuranceWithdrawn(address indexed to, uint256 amount);
    event PoolReimbursedFromInsurance(uint256 amount);

    IERC20 public immutable usdg;
    address public engine;
    bool public paused;
    uint256 public totalUsdg;
    uint256 public escrowedCollateral;
    uint256 public cumulativeFees;
    uint256 public sideBucket;
    uint256 public queueTotalOwed;
    uint256 public insuranceBalance;
    uint256 public totalReserved;
    uint16 public depositFeeBps;
    uint16 public withdrawFeeBps;
    uint16 public lpFeeBps = 5000;
    uint16 public minFreeLiquidityBps;

    struct MarketRisk {
        uint256 reservedLong;
        uint256 reservedShort;
        uint128 queueHead;
        uint128 queueTail;
    }

    mapping(bytes32 => MarketRisk) public marketRisk;

    struct AdlState {
        uint128 currentEpoch;
        uint64 lastCutIdx;
    }

    mapping(bytes32 => AdlState) public adlState;

    struct QueueEntry {
        address owner;
        uint128 amount;
        uint64 enqueuedAt;
        uint8 status;
    }

    mapping(bytes32 => mapping(uint256 => QueueEntry)) public queue;

    bool public autoPayoutsFrozen;

    uint256 public constant MINIMUM_LIQUIDITY = 1_000;
    uint256 public constant GENESIS_MIN_DEPOSIT = 100_000_000;

    constructor(address usdg_) ERC20("Element Equity LP", "eqLP") Ownable(msg.sender) {
        if (usdg_ == address(0)) revert ZeroAddress();
        usdg = IERC20(usdg_);
    }

    function decimals() public pure override returns (uint8) {
        return 6;
    }

    modifier onlyEngine() {
        if (msg.sender != engine) revert NotEngine();
        _;
    }

    function lpPrincipal() public view returns (uint256) {
        uint256 t = totalUsdg;
        uint256 e = escrowedCollateral;
        return t > e ? t - e : 0;
    }

    function freeLiquidity() public view returns (uint256) {
        uint256 p = lpPrincipal();
        uint256 r = totalReserved;
        p = p > r ? p - r : 0;
        uint256 q = queueTotalOwed;
        return p > q ? p - q : 0;
    }

    function navSolvent() public view returns (bool) {
        return usdg.balanceOf(address(this)) >= totalUsdg + cumulativeFees + sideBucket + insuranceBalance;
    }

    function _checkNav() internal view {
        if (!navSolvent()) revert NavInsolvent();
    }

    function deposit(uint256 amount, uint256 minLpOut) external {
        if (amount == 0) revert ZeroAmount();
        if (paused) revert VaultPaused();

        if (totalUsdg > usdg.balanceOf(address(this))) revert PhantomNav();

        bool genesis = totalSupply() == 0;
        if (genesis) {
            if (amount < GENESIS_MIN_DEPOSIT) revert GenesisBelowMinimum();
            if (totalUsdg != 0) revert StrandedGenesis();
        }

        (uint256 net, uint256 fee) = VaultMath.applyFeeBps(amount, depositFeeBps);
        uint256 minted = VaultMath.calcMintAmount(net, totalSupply(), lpPrincipal());
        uint256 userMint = minted;
        if (genesis) {
            if (minted <= MINIMUM_LIQUIDITY) revert DustMint();
            userMint = minted - MINIMUM_LIQUIDITY;
        }
        if (userMint < minLpOut) revert Slippage();

        usdg.safeTransferFrom(msg.sender, address(this), amount);

        totalUsdg += amount;
        if (genesis) _mint(address(0xdead), MINIMUM_LIQUIDITY);
        _mint(msg.sender, userMint);
        _checkNav();
        emit Deposited(msg.sender, amount, fee, userMint);
    }

    function withdraw(uint256 lpAmount, uint256 minOut) external {
        if (lpAmount == 0) revert ZeroAmount();
        if (paused) revert VaultPaused();

        uint256 grossReturn = VaultMath.calcReturnAmount(lpAmount, lpPrincipal(), totalSupply());

        (uint256 netReturn, uint256 fee) = VaultMath.applyFeeBps(grossReturn, withdrawFeeBps);
        if (netReturn < minOut) revert Slippage();

        if (freeLiquidity() < netReturn) revert InsufficientFreeLiquidity();

        _burn(msg.sender, lpAmount);
        totalUsdg -= netReturn;
        usdg.safeTransfer(msg.sender, netReturn);
        _checkNav();
        emit Withdrawn(msg.sender, lpAmount, netReturn, fee);
    }

    function creditCollateral(uint256 amount) external onlyEngine {
        totalUsdg += amount;
        escrowedCollateral += amount;
        _checkNav();
        emit CollateralCredited(amount);
    }

    function updateReserve(bytes32 marketId, uint8 side, int256 delta) external onlyEngine {
        if (side > 1) revert BadSide();
        if (delta >= 0) {
            MarketRisk storage risk = marketRisk[marketId];
            uint256 add = uint256(delta);
            if (side == 0) risk.reservedLong += add;
            else risk.reservedShort += add;
            totalReserved += add;
            emit ReserveUpdated(marketId, side, risk.reservedLong, risk.reservedShort, totalReserved);
        } else {
            _releaseReserve(marketId, side, uint256(-delta));
        }
    }

    function _releaseReserve(bytes32 marketId, uint8 side, uint256 amount) private {
        if (side > 1) revert BadSide();
        MarketRisk storage risk = marketRisk[marketId];
        uint256 current = side == 0 ? risk.reservedLong : risk.reservedShort;
        uint256 removed = amount < current ? amount : current;
        if (side == 0) risk.reservedLong = current - removed;
        else risk.reservedShort = current - removed;
        totalReserved = totalReserved > removed ? totalReserved - removed : 0;

        emit ReserveUpdated(marketId, side, risk.reservedLong, risk.reservedShort, totalReserved);
    }

    function _releaseEscrow(uint256 collateralReleased) private {
        uint256 escrow = escrowedCollateral;
        escrowedCollateral = escrow > collateralReleased ? escrow - collateralReleased : 0;
    }

    function _payDirect(address payee, uint256 amount) private returns (uint256 toPay) {
        toPay = amount;
        uint256 total = totalUsdg;
        if (total < toPay) toPay = total;
        uint256 cash = usdg.balanceOf(address(this));
        if (cash < toPay) toPay = cash;
        if (toPay > 0) {
            totalUsdg = total - toPay;
            usdg.safeTransfer(payee, toPay);
        }
    }

    function _applyLpRetention(VaultMath.SettlePlan memory plan) private {
        if (plan.lpRoute == VaultMath.LpRoute.SideBucket) {
            uint256 total_ = totalUsdg;
            uint256 moved = plan.lpGain < total_ ? plan.lpGain : total_;
            sideBucket += moved;
            totalUsdg = total_ - moved;
        }
        uint256 side_ = sideBucket;
        sideBucket = side_ > plan.badFromSide ? side_ - plan.badFromSide : 0;
        uint256 total = totalUsdg;
        totalUsdg = total > plan.badFromTotal ? total - plan.badFromTotal : 0;
    }

    function chargeEscrowedFee(uint256 fee) external onlyEngine {
        _releaseEscrow(fee);
        uint256 lpShare = (fee * lpFeeBps) / 10_000;
        uint256 sweepable = fee - lpShare;
        uint256 movable = sweepable < totalUsdg ? sweepable : totalUsdg;
        cumulativeFees += movable;
        totalUsdg -= movable;
        _checkNav();
        emit FeeAccrued(fee, true, lpShare, movable);
    }

    function accrueFee(uint256 amount, bool alreadyInPool) external onlyEngine {
        uint256 lpShare = (amount * lpFeeBps) / 10_000;
        uint256 sweepable = amount - lpShare;
        uint256 swept;
        if (!alreadyInPool) {
            cumulativeFees += sweepable;
            totalUsdg += lpShare;
            swept = sweepable;
        } else {
            uint256 movable = sweepable < totalUsdg ? sweepable : totalUsdg;
            cumulativeFees += movable;
            totalUsdg -= movable;
            swept = movable;
        }
        _checkNav();
        emit FeeAccrued(amount, alreadyInPool, lpShare, swept);
    }

    function releaseAndSettle(
        bytes32 marketId,
        uint8 side,
        uint256 reserved,
        uint256 netReturn,
        uint256 badDebt,
        uint256 lpGain,
        uint256 collateralReleased,
        address payee
    ) external onlyEngine returns (uint256 actuallyPaid) {
        if (payee == address(0)) revert ZeroAddress();
        _releaseReserve(marketId, side, reserved);
        _releaseEscrow(collateralReleased);

        _applyLpRetention(VaultMath.computeSettlePlan(netReturn, badDebt, lpGain, false, queueTotalOwed, sideBucket));

        actuallyPaid = _payDirect(payee, netReturn);
        _checkNav();
        emit Settled(marketId, payee, actuallyPaid, badDebt, lpGain);
    }

    function releaseAndEnqueue(
        bytes32 marketId,
        uint8 side,
        uint256 reserved,
        uint256 directPayout,
        uint256 enqueueAmount,
        uint256 badDebt,
        uint256 lpGain,
        uint256 collateralReleased,
        address payee
    ) external onlyEngine {
        if (enqueueAmount == 0) revert ZeroEnqueue();

        if (payee == address(0)) revert ZeroAddress();
        _releaseReserve(marketId, side, reserved);
        _releaseEscrow(collateralReleased);

        _applyLpRetention(VaultMath.computeSettlePlan(0, badDebt, lpGain, false, queueTotalOwed, sideBucket));

        uint256 toPay = _payDirect(payee, directPayout);

        uint256 idx = marketRisk[marketId].queueTail++;
        queueTotalOwed += enqueueAmount;
        queue[marketId][idx] = QueueEntry(payee, SafeCast.toUint128(enqueueAmount), uint64(block.timestamp), 0);

        _checkNav();
        emit Settled(marketId, payee, toPay, badDebt, lpGain);
        emit Enqueued(marketId, idx, payee, enqueueAmount);
    }

    function harvest(bytes32 marketId, uint256 maxEntries) external {
        MarketRisk storage risk = marketRisk[marketId];
        uint128 head = risk.queueHead;
        uint128 tail = risk.queueTail;
        uint256 processed;

        while (processed < maxEntries && head < tail) {
            QueueEntry storage entry = queue[marketId][head];
            if (entry.status != 0) {

                unchecked {
                    head += 1;
                    processed += 1;
                }
                risk.queueHead = head;
                continue;
            }
            VaultMath.HarvestPayment memory p = VaultMath.computeHarvestPayment(
                sideBucket,
                totalUsdg,
                totalReserved,
                escrowedCollateral,
                autoPayoutsFrozen ? 0 : insuranceBalance,
                entry.amount
            );
            if (!p.covered) break;

            sideBucket -= p.fromSide;
            totalUsdg -= p.fromLp;
            insuranceBalance -= p.fromInsurance;
            queueTotalOwed -= entry.amount;
            entry.status = 1;
            uint256 idx = head;
            unchecked {
                head += 1;
                processed += 1;
            }
            risk.queueHead = head;
            usdg.safeTransfer(entry.owner, entry.amount);
            emit Harvested(marketId, idx, entry.owner, entry.amount, p.fromSide, p.fromLp, p.fromInsurance);
        }

        if (head == tail && queueTotalOwed == 0 && sideBucket > 0) {
            uint256 drained = sideBucket;
            sideBucket = 0;
            totalUsdg += drained;
            emit SideBucketDrained(drained);
        }
        _checkNav();
    }

    function voidQueueEntry(bytes32 marketId, uint256 idx) external onlyEngine {
        QueueEntry storage entry = queue[marketId][idx];
        if (entry.status != 0 || entry.owner == address(0)) revert NotPending();
        entry.status = 2;
        queueTotalOwed -= entry.amount;
        emit QueueEntryVoided(marketId, idx, entry.owner, entry.amount);
    }

    function haircutQueueTail(bytes32 marketId, uint16 fBps, uint8 maxEntries, uint256 deficitCap)
        external
        onlyEngine
        returns (uint256 totalCut, uint256 cutsDone)
    {
        AdlState storage st = adlState[marketId];
        uint256 tail = marketRisk[marketId].queueTail;
        uint256 startIdx;
        {

            uint256 head = marketRisk[marketId].queueHead;
            uint256 frontier = st.lastCutIdx;
            if (head >= frontier) st.currentEpoch += 1;
            startIdx = head > frontier ? head : frontier;
        }
        (totalCut, cutsDone) = _haircutWalk(marketId, fBps, maxEntries, deficitCap, startIdx, tail);
        _checkNav();
        emit AdlHaircutApplied(marketId, fBps, totalCut, cutsDone, st.currentEpoch);
    }

    function _haircutWalk(
        bytes32 marketId,
        uint16 fBps,
        uint8 maxEntries,
        uint256 deficitRemaining,
        uint256 idx,
        uint256 tail
    ) private returns (uint256 totalCut, uint256 cutsDone) {
        address[] memory seen = new address[](maxEntries);
        uint256 seenLen;

        while (idx < tail && cutsDone < maxEntries && deficitRemaining > 0) {
            QueueEntry storage entry = queue[marketId][idx];
            if (entry.status == 0 && !_seenOwner(seen, seenLen, entry.owner)) {
                uint256 cut = (uint256(entry.amount) * fBps) / 10_000;
                if (cut > deficitRemaining) cut = deficitRemaining;
                if (cut > 0) {
                    entry.amount = uint128(uint256(entry.amount) - cut);
                    queueTotalOwed -= cut;
                    deficitRemaining -= cut;
                    adlState[marketId].lastCutIdx = uint64(idx + 1);
                    seen[seenLen] = entry.owner;
                    unchecked {
                        seenLen += 1;
                        cutsDone += 1;
                        totalCut += cut;
                    }
                }
            }
            unchecked {
                idx += 1;
            }
        }
    }

    function _seenOwner(address[] memory seen, uint256 len, address owner) private pure returns (bool) {
        for (uint256 j = 0; j < len; j++) {
            if (seen[j] == owner) return true;
        }
        return false;
    }

    function accrueToInsurance(uint256 amount) external onlyEngine {
        uint256 total = totalUsdg;
        uint256 capped = amount < total ? amount : total;
        insuranceBalance += capped;
        totalUsdg = total - capped;
        _checkNav();
        emit InsuranceAccrued(capped);
    }

    function reimburseLiquidator(address liquidator, uint256 shortfall) external onlyEngine returns (uint256 paid) {
        if (autoPayoutsFrozen) return 0;
        uint256 available = insuranceBalance;
        paid = shortfall < available ? shortfall : available;
        if (paid > 0) {
            insuranceBalance = available - paid;
            usdg.safeTransfer(liquidator, paid);
        }
        _checkNav();
        emit LiquidatorReimbursed(liquidator, paid);
    }

    function setAutoPayoutsFrozen(bool frozen) external onlyOwner {
        autoPayoutsFrozen = frozen;
        emit AutoPayoutsFrozenSet(frozen);
    }

    function withdrawInsurance(address to, uint256 amount) external onlyOwner {
        if (to == address(0)) revert ZeroAddress();
        insuranceBalance -= amount;
        usdg.safeTransfer(to, amount);
        _checkNav();
        emit InsuranceWithdrawn(to, amount);
    }

    function reimbursePoolFromInsurance(uint256 amount) external onlyOwner {
        insuranceBalance -= amount;
        totalUsdg += amount;
        _checkNav();
        emit PoolReimbursedFromInsurance(amount);
    }

    function setEngine(address engine_) external onlyOwner {
        if (engine_ == address(0)) revert ZeroAddress();
        engine = engine_;
        emit EngineSet(engine_);
    }

    function setPaused(bool paused_) external onlyOwner {
        paused = paused_;
        emit PausedSet(paused_);
    }

    function setFees(uint16 depositBps, uint16 withdrawBps) external onlyOwner {
        if (depositBps > 1000 || withdrawBps > 1000) revert BadFeeBps();
        depositFeeBps = depositBps;
        withdrawFeeBps = withdrawBps;
        emit FeesSet(depositBps, withdrawBps);
    }

    function setLpFeeBps(uint16 bps) external onlyOwner {
        if (bps < 1000 || bps > 9000) revert BadLpFeeBps();
        lpFeeBps = bps;
        emit LpFeeBpsSet(bps);
    }

    function setMinFreeLiquidityBps(uint16 bps) external onlyOwner {
        if (bps > 10_000) revert BadBps();
        minFreeLiquidityBps = bps;
        emit MinFreeLiquidityBpsSet(bps);
    }

    function sweepFees(address to) external onlyOwner {
        if (to == address(0)) revert ZeroAddress();
        uint256 amount = cumulativeFees;
        cumulativeFees = 0;
        usdg.safeTransfer(to, amount);
        _checkNav();
        emit FeesSwept(to, amount);
    }
}
Chain explorer2741msChain node81ms