Running Tests
Execute all tests:Test Organization
The test suite is organized into two primary test contracts:
The main test setup deploys a trampoline with a mock settlement address:
Core Test Scenarios
Authorization Verification
The trampoline rejects calls from non-settlement addresses:Gas Allocation
Tests confirm hooks receive their specified gas allowances using aGasRecorder helper contract:
Revert Tolerance
Multiple hooks execute sequentially even when individual hooks fail:Sequential Execution
Hook ordering is enforced through aCallInOrder helper:
Out-of-Gas Protection
The trampoline limits gas usage to approximately the specified amount plus call overhead:Helper Contracts
The test suite uses several mock contracts:Writing Custom Hook Tests
1
Create test contract
Inherit from
forge-std/Test.sol:2
Deploy hook in setUp
3
Build hook arrays
4
Execute via settlement context
Always use
vm.prank(settlement) before calling execute():5
Verify state and gas
6
Test authorization
If your hook validates
msg.sender:Gas Testing Constants
Key values used in gas calculations:Best Practices
Always use settlement context
Always use settlement context
Call
execute() from the settlement address using vm.prank(settlement). Direct calls will revert with NotASettlement.Test insufficient gas scenarios
Test insufficient gas scenarios
Verify behavior when hooks don’t receive enough gas. The trampoline should revert by consuming all remaining gas.
Verify reverts don't cascade
Verify reverts don't cascade
Test that a reverting hook doesn’t prevent subsequent hooks from executing.
Confirm sequential ordering
Confirm sequential ordering
Use helper contracts like
CallInOrder to verify hooks execute in the correct order.Use simple helper contracts
Use simple helper contracts
Create focused mock contracts that test specific behaviors rather than complex multi-purpose mocks.
Include edge cases
Include edge cases
Test authorization failures, out-of-gas conditions, empty hook arrays, and hooks targeting EOAs.
Related
- Gas Management — Deep dive into gas limit enforcement
- Usage Guide — Implementation patterns for hooks