lbamm-handler-protocol
LBAMM transfer handler architecture -- how transfer handlers work during swap settlement, the ILimitBreakAMMTransferHandler interface, executor validation hooks, callback patterns, and custom handler development. Use this skill whenever the user asks about LBAMM transfer handlers, swap settlement, ammHandleTransfer, custom settlement logic, ITransferHandlerExecutorValidation, how swaps use transfer data, or writing a custom handler contract.
Skill body
LBAMM Transfer Handler Protocol Knowledge Base
Transfer handlers are modular settlement components in LBAMM. During swap finalization, instead of using standard transferFrom to acquire input tokens, the AMM delegates token acquisition to a handler contract. This enables gasless swaps (permits), on-chain order books (CLOB), RFQ systems, and any custom settlement logic.
How Handlers Work
- User calls
singleSwap()/multiSwap()/directSwap()with a non-emptytransferDataparameter (at least 32 bytes) - The AMM reads the first 32 bytes as the handler address (left-padded
bytes32), validates the top 96 bits are zero - The remaining bytes (
transferData[32:]) are passed astransferExtraDatato the handler - AMM computes swap amounts (amountIn, amountOut) via the pool type
- AMM calls
handler.ammHandleTransfer()with the swap context - The handler transfers exactly
amountIninput tokens to the AMM - AMM verifies via strict balance check:
balanceBefore + amountIn == balanceAfter - AMM transfers tokenOut to recipient and settles hook fees
- If handler returned non-empty
callbackData, AMM executes it on the handler via low-levelcall
Transfer Data Encoding
// Use bytes.concat, NOT abi.encode
bytes memory transferData = bytes.concat(
bytes32(uint256(uint160(address(handler)))), // 32 bytes: handler address
handlerExtraData // Raw handler-specific bytes
);
If transferData.length < 32, no handler is used (standard safeTransferFrom).
Core Interfaces
ILimitBreakAMMTransferHandler
interface ILimitBreakAMMTransferHandler {
function ammHandleTransfer(
address executor,
SwapOrder calldata swapOrder,
uint256 amountIn,
uint256 amountOut,
BPSFeeWithRecipient calldata exchangeFee,
FlatFeeWithRecipient calldata feeOnTop,
bytes calldata transferExtraData
) external returns (bytes memory callbackData);
function transferHandlerManifestUri() external view returns (string memory manifestUri);
}
ITransferHandlerExecutorValidation
Optional hook that handlers can use to validate who can execute/fill orders.
interface ITransferHandlerExecutorValidation {
function validateExecutor(
bytes32 handlerId,
address executor,
SwapOrder calldata swapOrder,
uint256 amountIn,
uint256 amountOut,
BPSFeeWithRecipient calldata exchangeFee,
FlatFeeWithRecipient calldata feeOnTop,
bytes calldata hookData
) external; // Revert to block execution
}
Deployed Handlers
| Handler | Purpose | Skill |
|---|---|---|
| PermitTransferHandler | Gasless swaps via EIP-712 signed permits with optional cosigner | lbamm-permit-handler |
| CLOBTransferHandler | On-chain limit order book with maker deposits and FIFO fills | lbamm-clob |
Repository
GitHub: https://github.com/limitbreakinc/lbamm-hooks-and-handlers
Project Setup
- Foundry:
foundry.tomlmust exist. If not:forge init - LBAMM Core:
lib/lbamm-core/must exist. If not:forge install limitbreakinc/lbamm-core - LBAMM Hooks and Handlers:
lib/lbamm-hooks-and-handlers/must exist. If not:forge install limitbreakinc/lbamm-hooks-and-handlers - Remappings:
remappings.txtneeds both@limitbreak/lbamm-core/=lib/lbamm-core/and@limitbreak/lbamm-hooks-and-handlers/=lib/lbamm-hooks-and-handlers/. Append if missing – do not overwrite existing entries.
Reference Files
references/handler-architecture.md– Handler invocation flow, callback pattern, transfer data encodingreferences/custom-handler-patterns.md– Example custom handler implementations (RFQ, whitelists, fee-gated)
Related Skills
- lbamm-permit-handler – Generate integration code for the PermitTransferHandler (gasless swaps)
- lbamm-clob – Generate integration code for the CLOBTransferHandler (limit orders)
- lbamm-integrator – Generate router contracts that use handlers via directSwap
- lbamm-test – Generate Foundry test suites for handler contracts
- lbamm-protocol – Core AMM architecture, pool types, hooks, fees