One inventory across a Uniswap v4 pool on HyperEVM and a spot float on HyperCore, priced and rebalanced from the same order book.
A Counterbook vault keeps one inventory for one pair in two places. The pool leg earns from EVM swap flow. The book leg keeps the vault's overall exposure on target by trading on HyperCore. Both are valued at the same reference price, the HyperCore order book mid read in the current block.
Drawing diagram…
flowchart TB
accTitle: Counterbook system
accDescr: Swaps reach the Uniswap v4 PoolManager, which calls the Counterbook hook. The hook and the vault read HyperCore through precompiles. The hook and the vault instruct the CoreAgent, which sends actions through CoreWriter to the HyperCore spot order book. Actions execute a few seconds later and their results show up in later reads.
subgraph EVM["HyperEVM"]
R["Router, aggregator<br/>or liquidator"] -->|swap| PM["Uniswap v4<br/>PoolManager"]
PM <-->|"beforeSwap<br/>afterSwap"| H["CounterbookHook"]
V["CounterbookVault"] -->|"add / remove<br/>liquidity"| PM
H -->|read| PC[("Read<br/>precompiles")]
V -->|read| PC
H -->|"place / cancel"| A["CoreAgent"]
V -->|"sweep"| A
A -->|sendRawAction| CW["CoreWriter"]
end
subgraph CORE["HyperCore"]
OB[("Spot order book<br/>HYPE/USDC")]
end
CW -.->|"a few seconds later"| OB
OB -.->|"book and balances,<br/>later blocks"| PC| Contract | Job | Who can call it |
|---|---|---|
CounterbookHook | Checks the pool setup, prices every swap, tracks the vault's inventory, runs the fast rebalancing loop | The PoolManager (hook callbacks). Anyone can call poke(). |
CounterbookVault | ERC-20 shares, owns the v4 position, deposits and redemptions, recentering, sweeps, NAV, the performance fee, curator parameters | Anyone, behind guards. The curator for parameters. |
CoreAgent | Is the vault's HyperCore account. Sends only three CoreWriter actions and holds tokens on their way to HyperCore. | The hook and the vault only (immutable addresses) |
Why three contracts:
modifyLiquidity.Drawing diagram…
sequenceDiagram
accTitle: Swap sequence
accDescr: A router swaps through the PoolManager. In beforeSwap the hook reads the HyperCore best bid and offer and the perp oracle, computes the fee and returns it with the override flag. After the swap, the hook updates its cached inventory and, if the vault is skewed past the soft band, places or cancels an order through the CoreAgent and CoreWriter. HyperCore executes it a few seconds later.
participant U as Router
participant PM as PoolManager
participant H as Hook
participant PC as Precompiles
participant A as CoreAgent
participant CW as CoreWriter
U->>PM: swap
PM->>H: beforeSwap
H->>PC: read BBO, oracle
PC-->>H: bid, ask, oracle
H->>H: fee
H-->>PM: fee + override flag
PM->>PM: swap at fee
PM->>H: afterSwap
H->>H: update inventory
opt skew past soft band
H->>A: place / cancel
A->>CW: sendRawAction
end
H-->>PM: done
Note over CW: Executes on HyperCore<br/>a few seconds laterbeforeSwap reads the best bid and offer (BBO) of the HyperCore spot pair and the perp oracle price, plus the pool's own price from slot0. It computes the fee and returns it with Uniswap's OVERRIDE_FEE_FLAG. If a read fails, the hook charges a fixed fallback fee instead of reverting.afterSwap adds the LP fee the swap paid to the hook's cached copy of the vault inventory. The position is valued at the reference price, so the swap itself does not change the vault's skew.The fee model page has the formula. The rebalancing page covers step 4.
The vault manages its inventory with two loops on different time scales.
| Loop | Question it answers | Trigger | Mechanism |
|---|---|---|---|
| Fast loop | Is the vault's net exposure on target? | afterSwap, or poke() when no swaps flow | A maker order on HyperCore, or an IOC order past the hard band |
| Slow loop | Is the inventory split sensibly between venues? | sweep(), permissionless | Bridge idle tokens between HyperEVM and the HyperCore float |
After directional flow, the pool leg ends up long the asset traders sold to it. The fast loop sells that excess on HyperCore, so the vault as a whole is back on target while the pool position is untouched. Over time the venues drift apart: the pool leg holds more of one asset and the HyperCore float more of the other. The slow loop moves idle HyperEVM tokens to the float, or float back to HyperEVM, to keep HyperCore's share of NAV near its target. Tokens inside the position only become idle when a recenter cannot fit them into the new range.
recenter() is permissionless. Anyone can call it once the recenter cooldown has passed, provided the HyperCore reads succeed and the basis and spread are within their caps. It does not need the pool price to be near the reference, and it does not wait for transfers in flight.CoreWriter actions do not execute inside the EVM transaction. Hyperliquid delays order actions sent from CoreWriter on-chain for a few seconds. The hook never learns directly whether an order filled or was rejected, and an add-liquidity-only (ALO) order that would cross the book on arrival is rejected silently.
Counterbook therefore infers order state from HyperCore balances in later blocks, and computes NAV from balances, never from order status. A missed fill delays the next rebalance; it never corrupts accounting. See what the hook cannot see.