Uniswap / v4-periphery

🦄 🦄 🦄 🦄 Peripheral smart contracts for interacting with Uniswap v4
https://blog.uniswap.org/uniswap-v4
GNU General Public License v2.0
673 stars 453 forks source link

Add Override Fee Library #114

Open saucepoint opened 2 months ago

saucepoint commented 2 months ago

Component

General design optimization (improving efficiency, cleanliness, or developer experience)

Describe the suggested feature and problem it solves.

https://github.com/Uniswap/v4-core/pull/648 introduced a feature where beforeSwap can return and set the LP fee.

The fee is 24 bits, and the 2nd highest bit must be set to 1 in order for the override to apply. A helper library that provides syntatic sugar will be nice

Describe the desired implementation.


library OverrideFeeLibrary {
    uint24 public constant OVERRIDE_FEE_FLAG = 0x400000;

    function asOverrideFee(uint24 self) internal returns (uint24) {
        return self | OVERRIDE_FEE_FLAG;
    }
}

contract ExampleHook {
    using OverrideFeeLibrary for uint24;

    // ....

    function beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata)
        external
        view
        override
        returns (bytes4, BeforeSwapDelta, uint24)
    {
        // attach the override flag to `fee` to enable overriding the pool's stored fee
        return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, fee.asOverrideFee());
    }
}

Describe alternatives.

No response

Additional context.

No response

linear[bot] commented 2 months ago

PROTO-220 Add Override Fee Library