Extending the vault

OrderVault compiles to 24,242 of the 24,576-byte contract limit, so there are 334 bytes of headroom (forge build --sizes). That is enough for a small change but not a large one, so before adding logic, know how to make room. In rough order of how much each frees:

  • Move logic into an external library. This is the biggest lever and the pattern the vault already uses: MarketGuard (the Chainlink + TWAP read and bounds) and OrderCollection (the one-time NFT-collection create) are external libraries called with delegatecall, which keeps their bytecode out of the vault. Pricing and cost maths (_gasToTinybar, _reserve, the share calculations) or the scheduling internals are the next candidates to extract the same way.
  • Move the read-only views to a lens contract. The cost and status views (checkCost, fillCost, minBudget, sweepStatus, nextCheckDelay) exist for the frontend; a separate read-only "lens" contract can hold them and read the vault's storage, freeing the vault of code it never needs on-chain.
  • Keep using custom errors and events (the vault has no revert strings) and keep storage structs packed (Order, SweepState) — both are already done, and both are cheap ways to stay small.

Once there is room, the usual changes:

  • Add a market: add a function to script/MarketConfig.sol like usdcDai() (both tokens need Chainlink feeds), call listMarket from Deploy.s.sol, and redeploy. The frontend lists every market the vault has.
  • Tune how often checks run: SweepParams (minInterval, maxInterval, maxMoveBpsPerHour). Lower maxMoveBpsPerHour costs less and reacts later. Change a live market with updateMarket.
  • Tune the guard: GuardParams. MarketGuard.paramsValid keeps it meaningful: TWAP 5 min to 1 day, oracle age 60 s to 26 h, deviation and slippage at most 10%.
  • Recalibrate costs: measure a few scheduled sweeps on the mirror node (gas_used in /api/v1/contracts/{id}/results/{timestamp}) and call setCosts. The frontend reads costs from the vault, so nothing else changes.
  • Mainnet: add mainnet addresses to MarketConfig.sol, allow chain 295 in Deploy.s.sol (it deploys to testnet only), add the network to scaffold.config.ts, and tighten maxOracleAge to the mainnet feeds' heartbeat.

From README.md at v1.1 (6ddd9a3) · View source