Skip to content

spike: CoW rebalancer integration, proven against mainnet#7

Merged
jayeshy14 merged 1 commit into
mainfrom
feat/cow-rebalancer-spike
Jun 17, 2026
Merged

spike: CoW rebalancer integration, proven against mainnet#7
jayeshy14 merged 1 commit into
mainfrom
feat/cow-rebalancer-spike

Conversation

@jayeshy14

Copy link
Copy Markdown
Owner

Why

The de-risking spike for Stage 3, run before committing to the full rebalancer. The goal is to answer the one question that gates whether this design works: can the protocol be a first-class CoW trader, and does our order encoding match what the real GPv2Settlement verifies on-chain. Both are now proven against live mainnet.

What

  • GPv2Order: minimal port of CoW's order struct, TYPE_HASH, and EIP-712 digest, using abi.encode which is byte-for-byte equivalent to CoW's assembly struct hashing.
  • CoWOrderHandler (spike): implements ERC-1271 isValidSignature so the real GPv2Settlement accepts orders it has not pre-signed, validating each presented order against on-chain state (a sell of a registered constituent into USDC, paid to the vault, at an oracle-anchored minimum-out). The digest is rebound to the decoded order, so a solver cannot pair a valid order's digest with the encoding of a different order. Scope is the sell leg only, with no delta sizing or epoch lifecycle; in the real integration the vault is the order owner.
  • MockGPv2Settlement: a faithful mock of CoW's verify-then-move-tokens path for deterministic, no-network testing.

Findings (the point of the spike)

  • On a mainnet fork, the handler reconstructs the exact digest the real settlement verifies against. The domain separator and the relayer match the live contracts at 0x9008... and 0xC92E..., and the independently recomputed EIP-712 domain formula matches too.
  • isValidSignature returns the magic value for a state-derived order and reverts on tamper (below minOut, wrong receiver, wrong buy token, unregistered sell token, digest/order mismatch).
  • The mock settle moves tokens end-to-end with proportional partial fills and rejects a tampered order at settlement.
  • isValidSignature costs about 14.7k gas per order, paid by solvers rather than the vault, so a 100-constituent reweight is affordable by construction.

Conclusion: CoW can carry the rebalance design. The remaining unproven piece is a full on-chain settle() through a registered solver with live liquidity, which is the natural first integration test for the real rebalancer.

Testing and CI

11 unit tests plus 3 mainnet-fork tests, all passing. The CI workflow runs the fork tests as a non-blocking best-effort step (they need a mainnet RPC), and the mainnet endpoint is configured in foundry.toml. The non-fork suite is 97 tests.

De-risking spike for Stage 3, before building the full rebalancer. Proves the
protocol can be a first-class CoW trader and that the order encoding matches
the real GPv2Settlement, against live mainnet.

- GPv2Order: minimal port of CoW's order struct, TYPE_HASH, and EIP-712 digest
  (abi.encode, byte-for-byte equivalent to their assembly struct hashing).
- CoWOrderHandler (spike): implements ERC-1271 isValidSignature so the real
  GPv2Settlement accepts orders it has not pre-signed, validating each order
  against on-chain state (sell a registered constituent into USDC, paid to the
  vault, at an oracle-anchored minimum-out). The digest is rebound to the
  decoded order so a solver cannot pair a valid digest with a different order.
  Scope: sell leg only, no delta sizing or epoch lifecycle; the real
  integration makes the vault the order owner.
- MockGPv2Settlement: faithful mock of the EIP-1271 verify-then-move-tokens
  path for deterministic, no-network testing.

Findings: on a mainnet fork the handler reconstructs the exact digest the real
settlement verifies (domain separator and relayer match the live contracts),
isValidSignature returns the magic value for a derived order and reverts on
tamper, the mock settle moves tokens with proportional partial fills, and
isValidSignature costs ~14.7k gas per order (paid by solvers, not the vault).
CoW can carry the rebalance design.

11 unit tests plus 3 mainnet-fork tests. CI runs fork tests as a non-blocking
best-effort step (needs a mainnet RPC); the mainnet endpoint is in foundry.toml.
@jayeshy14 jayeshy14 merged commit ea96dec into main Jun 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant