Cross-margin a Uniswap v4 LP with a short hedge on 1inch Aqua: ~90% less margin, 1-tx crash unwind.
ZeroMargin lets someone who earns fees as a Uniswap v4 liquidity provider, and who protects that position with a short, lock up far less collateral. Normally the LP and the short are margined as two unrelated bets, even though they move in opposite directions. We treat them as one portfolio and size the margin from a single stress test: 16 price and volatility scenarios plus a safety buffer for price gaps between venues. In our tests this needs roughly 90% less margin.
Funds stay in the trader’s own wallet. A custom 1inch Aqua app tracks a virtual USDC balance and can only pull money the trader has approved. A Uniswap v4 hook stops any liquidity withdrawal that would leave the portfolio under-collateralized, and the contract recomputes margin onchain before every settlement. Arkiv stores short-lived risk records so a keeper can find positions that are due to unwind.
Our demo runs on a Base fork. In a 30% crash, a normal LP account is liquidated with a 10% penalty. The same position in ZeroMargin lets the short’s profit cover the LP’s loss, and it unwinds in one transaction for a 5 bps fee. The short leg is a pre-funded adapter in this build, not a live perp venue, and it’s designed to be swapped for a real one later.
ZeroMargin is two layers: Solidity contracts on Base and a Next.js terminal on top. The contracts are written in Solidity 0.8.26 and tested with Foundry. The frontend is TypeScript with Next.js 15. We develop and demo against an Anvil fork of Base mainnet, so the real Uniswap v4 PoolManager and the real 1inch Aqua contracts are the ones being called.
The core is SpanMathLib, a pure Solidity library that runs a combined LP-plus-short portfolio through a 16-scenario grid (8 price shocks across 2 volatility states) and adds a 500 bps cross-venue basis haircut. It returns the isolated margin, the worst-case loss and the netted margin. The same math is mirrored in TypeScript, and we check the two against shared golden test vectors so the UI quote always equals what the contract executes.
ClearingAquaApp is a custom 1inch Aqua app. The trader's USDC stays in their own wallet and is exposed as a virtual balance, and the app can only pull what the trader has approved. This is what makes ZeroMargin non-custodial, and it lets the LP deficit and the short hedge's gain settle from one balance instead of two. PortfolioMarginHook is a real Uniswap v4 hook using beforeRemoveLiquidity and afterSwap. It reverts a withdrawal with SpanMarginBreach if the book would drop below its required margin, so the margin rule is enforced by the pool itself and not just by our UI.
Arkiv is our discovery and audit layer. We write 300-second risk attestations to Arkiv, and a keeper queries them for positions marked UNWIND_ELIGIBLE with a margin ratio under 10000 bps. Arkiv is never trusted for settlement: the contract recomputes margin onchain before anything unwinds.
Things worth flagging as hacky: the hook only works at an address whose low bits encode its permissions, so we mined a CREATE2 salt to deploy it at a valid address. The short leg is a pre-funded USDC adapter behind an interface, not a live perp venue, so the demo can show the hedge's gain settling without integrating a venue. We say this plainly in the README and the demo. The risk model is a simplified SPAN-style grid, not CME's production algorithm.

