Understanding Hook Architecture
The trampoline contract provides protection through:- Isolated execution context — hooks run from the trampoline’s address, not the settlement contract
- Gas limit enforcement — maximum gas per hook is specified
- Revert tolerance — individual hook failures don’t cascade
Hook Structure
Creating a Hook
Basic Hook
Multiple Hooks
Executing Hooks
Execute Function Signature
Settlement-Only Modifier
Settlement Integration Example
Hook Implementation
This pattern enables semi-permissioned hook implementations, ensuring execution only within legitimate settlements.
Gas Limit Best Practices
How Gas Limits Work
Gas Forwarding Mechanics
Setting Gas Limits
1
Measure function gas usage
2
Add overhead buffer
- EVM call costs (~700 gas for warm, ~2600 for cold)
- Storage access costs
- Solidity runtime setup
3
Consider worst-case scenarios
Account for variable costs:
- Cold vs. warm storage access
- Varying array lengths
- Conditional logic branches
Gas Limit Examples from Tests
Simple Counter (50K gas):Handling Reverts
Revert Behavior
- Revert caught by trampoline
- Trampoline continues with remaining hooks
- Settlement proceeds normally
Code Implementation
Partial Hook Failure Example
Individual hook failures don’t compromise other traders’ orders by preventing entire settlement completion.
Execution Order
Sequential Processing
Security Considerations
Protected Settlement Context
- Hooks execute from trampoline’s address
- Cannot access settlement contract’s token balances
- Cannot call privileged settlement functions
- Cannot interfere with other orders
Gas Consumption Protection Example
INVALID opcode would consume 63/64ths of all transaction gas.
Best Practices
Always set reasonable gas limits
Always set reasonable gas limits
- Measure actual gas usage in tests
- Add 10-20% buffer for variations
- Never set arbitrarily high limits
Handle hook failures gracefully
Handle hook failures gracefully
- Don’t assume all hooks will succeed
- Design hooks to be idempotent when possible
- Consider using post-hooks for critical operations
Validate hook targets
Validate hook targets
- Ensure target contracts are trusted
- Verify hook implementations before deployment
- Consider using allowlists for critical operations
Test with insufficient gas scenarios
Test with insufficient gas scenarios
- Test hooks with exact gas limits
- Test with insufficient available gas
- Verify settlement continues after hook failures
Complete Example
Next Steps
API Reference
Explore the complete API documentation.
Architecture
Learn about the security model and architecture.