Running Tests
Run all tests with Foundry:Test Structure
The test suite includes two main test contracts:HooksTrampoline.t.sol- Core functionality testsGasLimitEnforcement.t.sol- Gas enforcement edge cases
Core Test Contract
The main test contract sets up a mock settlement address and deploys the trampoline:test/HooksTrampoline.t.sol
Key Test Cases
Authorization Tests
The trampoline must only be callable from the settlement contract:Gas Limit Enforcement
Tests verify that hooks receive the specified gas limit:Revert Handling
Hooks can revert without failing the entire transaction:Execution Order
Hooks must execute in the specified order:Out of Gas Protection
The trampoline protects against excessive gas consumption:Test Helper Contracts
The test suite includes several helper contracts to test different scenarios:GasRecorder
Records the gas available when called:Counter
Simple counter for testing successful hook execution:Reverter
Always reverts with a custom message:CallInOrder
Verifies hooks are called in sequence:Hummer
Consumes excessive gas to test gas limits:GasCraver
Requires a specific amount of gas to execute:Writing Tests for Custom Hooks
Gas Limit Edge Cases
TheGasLimitEnforcement.t.sol file tests complex gas scenarios:
TRAMPOLINE_OVERHEAD = 4_000- Gas overhead before calling hookGAS_CRAVER_OVERHEAD = 117- Gas used before checking gas limitBOUND_ON_GAS_COST = 60_000- Maximum expected gas cost
Best Practices
- Use
vm.prank(settlement)- Always callexecute()from the settlement address - Test gas limits - Verify hooks handle insufficient gas gracefully
- Test reverts - Ensure reverted hooks don’t break other hooks
- Test order - Verify hooks execute in the correct sequence
- Use helper contracts - Create simple contracts to test specific behaviors
- Test edge cases - Include tests for out-of-gas and authorization failures
