Skip to main content

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

Direct calls to execute() from any address other than the settlement contract will revert with NotASettlement() error.

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

If there’s insufficient gas to forward the requested gasLimit, the transaction reverts by consuming all remaining gas. This prevents partial execution issues.

Setting Gas Limits

1

Measure function gas usage

2

Add overhead buffer

Overhead considerations:
  • 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):
Gas Recording (133K gas):
Ordered Operations (25K gas):

Handling Reverts

Revert Behavior

  1. Revert caught by trampoline
  2. Trampoline continues with remaining hooks
  3. 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

If hooks depend on each other’s state changes, ensure they’re ordered correctly. A reverting hook will not execute its state changes, potentially affecting subsequent hooks.

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

Without gas limits, an INVALID opcode would consume 63/64ths of all transaction gas.

Best Practices

  • Measure actual gas usage in tests
  • Add 10-20% buffer for variations
  • Never set arbitrarily high limits
  • Don’t assume all hooks will succeed
  • Design hooks to be idempotent when possible
  • Consider using post-hooks for critical operations
  • Ensure target contracts are trusted
  • Verify hook implementations before deployment
  • Consider using allowlists for critical operations
  • 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.
Last modified on March 4, 2026