How a stitch works
Three contracts, none of them holding anything for longer than a transaction. This page walks a single rebalance through them.
The book
PatchBook is a registry. A recipe is a name, a creator, a list of up to sixteen constituents, a weight for each in basis points summing to ten thousand, and a creator fee capped at fifty basis points. Every constituent is an ERC-20 plus the Uniswap V4 pool key that prices it against USDG; the book checks that the pool exists, is initialised, and really pairs that token with USDG. USDG itself may be a constituent with no pool, so a recipe can hold cash.
Only the creator can reweight, and reweighting is the only thing a creator can do. Constituents never change. This is the one structural decision that makes an open mandate safe to give: a follower consents to a universe once, and the creator can never route them into a token they did not agree to. A creator who wants out sets a hundred percent USDG.
The mandate
A follower keeps every token in their own wallet and approves the Stitcher on the ones they hold, the way they would any router. Then they record a mandate: which recipe, how far a constituent may drift from its weight before anyone may act, how much slippage a swap may take against pool spot, what tip the executor earns, and how long the wallet rests between stitches. One mandate per wallet. Revoking deletes it.
The stitch
Anyone may call stitch on a follower, passing the trades they think are needed. The contract does not trust them. It reads spot for every constituent from the PoolManager, values the wallet in USDG, and refuses unless at least one constituent is past the drift bound and the cooldown has elapsed.
Inside one PoolManager unlock, it runs the trades in order. Each pulls its input from the follower, swaps on the recipe's pool, and must return at least spot less the mandate's slippage, with spot fixed before any trade so the executor's own swaps cannot move the yardstick. Tip and creator fee are cut from the output; the rest goes back to the follower.
Then it revalues at that same pre-trade spot. Every constituent must be inside the bound, and the total must be at least what it was less slippage, tip and fee. Otherwise everything reverts and the executor has paid gas for nothing.
Dividends
Robinhood's stock tokens pay dividends as a multiplier that only their UI balance sees; the raw ERC-20 balance never moves and the pools cannot see it either. After a dividend the pool re-prices the raw unit, so the wallet shows a surplus on that constituent. The next stitch sells the surplus and spreads it across the recipe. Nothing special was written for this. It falls out of valuing in raw units at pool spot, which is what a wallet can actually sell for.
Entering and leaving
Enter pulls USDG and buys every constituent at the recipe's weights in one transaction, cutting the creator fee from each purchase. Exit sells everything the wallet holds in the recipe back to USDG, with no fee. Neither requires a mandate. A wallet can follow a recipe by hand forever and never let anyone touch it.