v4-periphery icon indicating copy to clipboard operation
v4-periphery copied to clipboard

[Bug]: Missing virtual modifier in BaseHook contract causing inheritance and override problems

Open oftiyf opened this issue 8 months ago • 1 comments

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

  1. Create a new contract that inherits from BaseHook following the test case README example
  2. Attempt to override the beforeSwap function
  3. Compile the contract
  4. 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

oftiyf avatar Mar 03 '25 12:03 oftiyf