04 · Developers
Write a module
The smallest contract that can join a stack, and the mistakes that unbalance the pool.
Extend LaunchHook. You get registration, the trader, exact-input checks, and the hub-aware take and fee relay. Implement IMarketModule only if you cannot extend the base. The function signatures have to match either way.
Checklist
- Decide which callbacks you need. Return that set from
getHookPermissionsby adding a function onHookFlags. Do not turn on a callback you do not implement. The hub will call it. - Deploy at an address whose low bits equal
HookFlags.toUint(permissions).BaseHook's constructor checks this and reverts otherwise. The pool's hook is still the hub. The module's own flags exist because the module is also a valid standalone hook. - In
createLaunch, call_registerand_setLaunchToken. Store your parameters in a struct packed so the hot path reads one slot when it can. Do not add a setter. - In
beforeSwap, call_onlyExactInputand_load. Return a zeroBeforeSwapDelta. The hub reverts anything else. If you set a fee, returnfee | OVERRIDE_FEE_FLAG. A fee of zero with the flag is a real override. The hub keeps it. - In
afterSwap, if you need tokens from the pool, call_takeand return that amount as a positive unspecified delta when you are the pool's hook. Under the hub,_takecallspulland the hub ignores your returned delta and uses the sum of pulls instead. Still return the amount. Standalone mode needs it. - Do not call
poolManager.takeorupdateDynamicLPFeeyourself. Those calls must come from the address stored on the pool. The base contract already branches on whether the hub called you. - Call
attachHubbeforecreateLaunchif this module will sit under a hub._attachedToallowskey.hooks == hubonly after the hub is set.
Accounting
A positive after-swap delta means the hook took that much of the unspecified currency from the pool. Unspecified currency on an exact-input swap is the output. If you take 1 token and return a delta of 0, the PoolManager's books do not balance and the swap reverts. If you return 1 and take 0, the same thing happens. Take and return the same number.
Tokens you keep, such as a fee pot or a lock, should be transferred to address(this) inside _take. Under the hub, pull sends them to the module, not to the hub. Claims then transfer from the module to the user. Check _attachedTo(key) in the claim, not address(key.hooks) == address(this), or the tokens are stuck whenever the pool points at the hub.
Tests
test/utils/LaunchFixture.sol etches a hook at a flagged address, funds two mock ERC-20s, and swaps through TraderRouter. Copy a case from test/ThirdWaveHooks.t.sol for a standalone hook, and from test/TalosHook.t.sol for a module under the hub. Run forge test --offline. via_ir is on, so a cold compile takes about a minute.
Some callbacks override a non-view base function and the compiler says the state mutability can be restricted to view. Leave that. The override has to match BaseHook. The unreachable return in LaunchHook.moduleBeforeSwap's sibling path is the same kind of leftover. It is not a logic branch you should "clean up" by changing the signature.
The words these pages use, in plain English.