v4-periphery
v4-periphery copied to clipboard
[Bug]: Missing virtual modifier in BaseHook contract causing inheritance and override problems
Describe the bug
Description
I encountered a compilation error when trying to inherit and override the beforeSwap function as instructed in the test case README. The README example directly inherits and implements the beforeSwap function, but compilation fails in practice.
Root Cause
After examining the code, I found the issue in the main/src/utils/BaseHook.sol file. The beforeSwap function lacks the virtual modifier, preventing child contracts from properly overriding this function.
// Current implementation (problematic):
function beforeSwap(...) returns (...) {
// function implementation
}
// Expected implementation:
function beforeSwap(...) virtual returns (...) {
// function implementation
}
Steps to Reproduce
- Create a new contract that inherits from
BaseHookfollowing the test case README example - Attempt to override the
beforeSwapfunction - Compile the contract
- Observe the compilation error indicating that a non-virtual function cannot be overridden
Impact
This issue prevents developers from correctly extending and customizing hook logic as shown in the documentation examples, hindering the development of new hooks.
Proposed Solution
Add the virtual modifier to the beforeSwap function in the main/src/utils/BaseHook.sol file to allow it to be overridden by child contracts.
Additional Notes
I discovered this issue while learning from the official documentation. The examples in the docs imply these functions are meant to be overridable, but the actual implementation doesn't support this behavior.
Expected Behavior
// ... existing code ... function beforeSwap( address sender, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata hookData ) external virtual returns (bytes4, BeforeSwapDelta, uint24) { // ... existing code ... } // ... existing code ...
To Reproduce
No response
Additional context
No response