Build an extension
Extensions run before or after swaps, deposits and withdrawals. They can reject an action or record its result, but they cannot replace the current quote or change the executed amounts. You can reuse standard extensions or write your own.
For a public pool, only withdrawal extensions can lower the tier. Any extension registered on beforeRemoveLiquidity or afterRemoveLiquidity makes the pool 🔴 High Risk, including Relatick's standard extensions. Adding swap or deposit extensions doesn't lower the tier.
See Pool tiers.
1. Pick the trigger
beforeSwapafterSwapbeforeSwap receives, plus final slot 0, amount deltas, platform feebeforeAddLiquidityafterAddLiquiditybeforeAddLiquidity receives, plus amounts addedbeforeRemoveLiquidityafterRemoveLiquiditybeforeRemoveLiquidity receives, plus amounts removedSlot 0 snapshots hold the whole packed slot 0: bin index and position, fees, asymmetry and pause level.
Any extension can revert. A revert undoes the whole action, token transfers included.
Swap extensions also run inside simulateSwapAndRevert. That function is permissionless: the caller chooses sender and the quote, and all state changes are rolled back. Extensions should behave the same as in a real swap so that router quotes stay accurate.
Below are a few examples and use cases:
beforeSwapbeforeSwapafterSwapafterSwapbeforeAddLiquidityafterSwap2. Implement it
Use the exact signatures in IMetricOmmExtensions. Implement initialize(address pool, bytes data) and the extensions you register, and return each function's own selector on success, for example IMetricOmmExtensions.afterSwap.selector. A revert or a wrong selector rejects the action.
- Initialization. Authenticate the factory, validate and store settings per pool, and prevent reinitialization.
initializeruns before the factory records the pool's registry entry and admin, so don't read either of them insideinitialize. - Identity. In an extension,
msg.senderis the pool: authenticate it. Thesenderargument is the pool's caller, usually a router, not the trader.extensionDatais caller-supplied and forwarded unchanged to every extension of that action: validate it, and bind any permit to the account, action, pool, chain, expiry and nonce. - State. Use the supplied deltas and snapshots. Don't reenter the pool's guarded methods, including
getSellAndBuyPrices().
{% hint style="warning" %} Authenticate the pool. An extension that doesn't check that msg.sender is a pool it was initialized for can be called by anyone, with made-up arguments. {% endhint %}
3. Attach and test
At pool creation, supply up to seven extension addresses, one initialization payload each, and the execution order per action. Addresses and order are fixed for the life of the pool; settings can stay mutable if your extension allows it. Don't register withdrawal extensions unless blocking LP exits is intended.
Confirm that each guard you need is initialized with settings that turn it on. For example, a StopLoss with drawdownE6 = 0 is attached but does nothing.
Test through the real router and liquidity entry points, not only direct calls: unauthorized callers, replayed permits, partial fills, both directions, limit boundaries, rollback, and withdrawals. Keep extension work bounded and check gas.
Next: Configure a pool.
Last updated 29 days ago
Relatick