Description:
Multi-signature wallet contract requiring multiple confirmations for transaction execution.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"src/MevHookUpgradeableV10.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolId} from "@uniswap/v4-core/src/types/PoolId.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BaseHookUpgradable} from "./base/BaseHookUpgradable.sol";
import {HookData} from "./MevHookUpgradeable.sol";
import {MevHookUpgradeableV9} from "./MevHookUpgradeableV9.sol";
/// @title MEV Hook – Upgradeable implementation V10
/// @dev Extends {MevHookUpgradeableV9} maintaining state layout compatibility.
/// @notice Adds minimum MEV payment requirement that can be dynamically updated by admin
contract MevHookUpgradeableV10 is MevHookUpgradeableV9 {
// -------------------------------------------------------------------
// State Variables
// -------------------------------------------------------------------
/// @notice Minimum MEV payment required from searchers (in wei)
/// @dev Default is 0.008 ETH = 8000000000000000 wei
uint256 public minMevPayment;
// -------------------------------------------------------------------
// Events
// -------------------------------------------------------------------
/// @notice Emitted when minimum MEV payment is updated
/// @param oldMinPayment Previous minimum payment value
/// @param newMinPayment New minimum payment value
event MinMevPaymentUpdated(uint256 oldMinPayment, uint256 newMinPayment);
// -------------------------------------------------------------------
// Errors
// -------------------------------------------------------------------
/// @notice Thrown when MEV payment is below the minimum required
/// @param provided The amount provided
/// @param required The minimum amount required
error MevPaymentTooLow(uint256 provided, uint256 required);
/// @notice Thrown when trying to set an excessively high minimum payment
error MinPaymentTooHigh();
// -------------------------------------------------------------------
// ETH Receive Logic
// -------------------------------------------------------------------
/// @notice Fallback function to receive ETH and redistribute it
/// @dev Enforces minimum MEV payment requirement after basefee deduction
receive() external payable virtual override nonReentrant {
// reset unlocked block
unlockedBlock = 0;
// Gas optimization: Extract packed data in single operation
uint176 packed = packedRefundDetails;
uint16 poolIndex = uint16(packed & 0xFFFF);
address lastSwapper = address(uint160(packed >> 16));
if (poolIndex == 0) revert NoLastUsedPool();
// Pool index is stored as 1-based, so subtract 1 for array access
PoolKey memory pool = allPoolKeys[poolIndex - 1];
uint256 amount = msg.value;
// Calculate minimum required to cover basefee for the unlock tx
uint256 minRequired = block.basefee * unlockTxGasUsed;
if (amount < minRequired) revert TipTooLow(amount, minRequired);
// Subtract the required base fee from the amount to get the actual tip
amount -= minRequired;
// Check if MEV payment meets minimum requirement
if (amount < minMevPayment) revert MevPaymentTooLow(amount, minMevPayment);
// Transfer the required basefee to the signer
(bool okBaseTransfer, ) = signer.call{value: minRequired}("");
if (!okBaseTransfer) revert EthTransferFailed();
// Calculate fee only if needed
uint256 feeAmount = 0;
if (defaultHookFees.mevFeeBps > 0) {
HookFees memory fee = getFeeConfig(pool.toId(), address(0));
feeAmount = (amount * fee.mevFeeBps) / 10_000;
}
// Remaining amount goes to the pool
uint256 poolAmount = amount - feeAmount;
// Transfer fee to feeRecipient if set
if (feeAmount > 0 && feeRecipient != address(0)) {
(bool okFeeTransfer, ) = feeRecipient.call{value: feeAmount}("");
if (!okFeeTransfer) revert EthTransferFailed();
}
// Donate remaining ETH to pool
if (poolAmount > 0) {
donateToPool(pool, poolAmount);
}
emit Tip(pool.toId(), lastSwapper, poolAmount, feeAmount);
}
// -------------------------------------------------------------------
// Admin Functions
// -------------------------------------------------------------------
/// @notice Set the minimum MEV payment required from searchers
/// @param newMinPayment New minimum payment amount in wei
/// @dev Only callable by admin. Validates that value is reasonable (≤ 1 ETH)
function setMinMevPayment(uint256 newMinPayment) external onlyRole(DEFAULT_ADMIN_ROLE) {
// Sanity check: prevent accidentally setting too high (max 1 ETH)
if (newMinPayment > 1 ether) revert MinPaymentTooHigh();
uint256 oldMinPayment = minMevPayment;
minMevPayment = newMinPayment;
emit MinMevPaymentUpdated(oldMinPayment, newMinPayment);
}
}
"
},
"lib/v4-core/src/types/BeforeSwapDelta.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Return type of the beforeSwap hook.
// Upper 128 bits is the delta in specified tokens. Lower 128 bits is delta in unspecified tokens (to match the afterSwap hook)
type BeforeSwapDelta is int256;
// Creates a BeforeSwapDelta from specified and unspecified
function toBeforeSwapDelta(int128 deltaSpecified, int128 deltaUnspecified)
pure
returns (BeforeSwapDelta beforeSwapDelta)
{
assembly ("memory-safe") {
beforeSwapDelta := or(shl(128, deltaSpecified), and(sub(shl(128, 1), 1), deltaUnspecified))
}
}
/// @notice Library for getting the specified and unspecified deltas from the BeforeSwapDelta type
library BeforeSwapDeltaLibrary {
/// @notice A BeforeSwapDelta of 0
BeforeSwapDelta public constant ZERO_DELTA = BeforeSwapDelta.wrap(0);
/// extracts int128 from the upper 128 bits of the BeforeSwapDelta
/// returned by beforeSwap
function getSpecifiedDelta(BeforeSwapDelta delta) internal pure returns (int128 deltaSpecified) {
assembly ("memory-safe") {
deltaSpecified := sar(128, delta)
}
}
/// extracts int128 from the lower 128 bits of the BeforeSwapDelta
/// returned by beforeSwap and afterSwap
function getUnspecifiedDelta(BeforeSwapDelta delta) internal pure returns (int128 deltaUnspecified) {
assembly ("memory-safe") {
deltaUnspecified := signextend(15, delta)
}
}
}
"
},
"lib/v4-core/src/types/BalanceDelta.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {SafeCast} from "../libraries/SafeCast.sol";
/// @dev Two `int128` values packed into a single `int256` where the upper 128 bits represent the amount0
/// and the lower 128 bits represent the amount1.
type BalanceDelta is int256;
using {add as +, sub as -, eq as ==, neq as !=} for BalanceDelta global;
using BalanceDeltaLibrary for BalanceDelta global;
using SafeCast for int256;
function toBalanceDelta(int128 _amount0, int128 _amount1) pure returns (BalanceDelta balanceDelta) {
assembly ("memory-safe") {
balanceDelta := or(shl(128, _amount0), and(sub(shl(128, 1), 1), _amount1))
}
}
function add(BalanceDelta a, BalanceDelta b) pure returns (BalanceDelta) {
int256 res0;
int256 res1;
assembly ("memory-safe") {
let a0 := sar(128, a)
let a1 := signextend(15, a)
let b0 := sar(128, b)
let b1 := signextend(15, b)
res0 := add(a0, b0)
res1 := add(a1, b1)
}
return toBalanceDelta(res0.toInt128(), res1.toInt128());
}
function sub(BalanceDelta a, BalanceDelta b) pure returns (BalanceDelta) {
int256 res0;
int256 res1;
assembly ("memory-safe") {
let a0 := sar(128, a)
let a1 := signextend(15, a)
let b0 := sar(128, b)
let b1 := signextend(15, b)
res0 := sub(a0, b0)
res1 := sub(a1, b1)
}
return toBalanceDelta(res0.toInt128(), res1.toInt128());
}
function eq(BalanceDelta a, BalanceDelta b) pure returns (bool) {
return BalanceDelta.unwrap(a) == BalanceDelta.unwrap(b);
}
function neq(BalanceDelta a, BalanceDelta b) pure returns (bool) {
return BalanceDelta.unwrap(a) != BalanceDelta.unwrap(b);
}
/// @notice Library for getting the amount0 and amount1 deltas from the BalanceDelta type
library BalanceDeltaLibrary {
/// @notice A BalanceDelta of 0
BalanceDelta public constant ZERO_DELTA = BalanceDelta.wrap(0);
function amount0(BalanceDelta balanceDelta) internal pure returns (int128 _amount0) {
assembly ("memory-safe") {
_amount0 := sar(128, balanceDelta)
}
}
function amount1(BalanceDelta balanceDelta) internal pure returns (int128 _amount1) {
assembly ("memory-safe") {
_amount1 := signextend(15, balanceDelta)
}
}
}
"
},
"lib/v4-core/src/interfaces/IPoolManager.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Currency} from "../types/Currency.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IHooks} from "./IHooks.sol";
import {IERC6909Claims} from "./external/IERC6909Claims.sol";
import {IProtocolFees} from "./IProtocolFees.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {PoolId} from "../types/PoolId.sol";
import {IExtsload} from "./IExtsload.sol";
import {IExttload} from "./IExttload.sol";
/// @notice Interface for the PoolManager
interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload, IExttload {
/// @notice Thrown when a currency is not netted out after the contract is unlocked
error CurrencyNotSettled();
/// @notice Thrown when trying to interact with a non-initialized pool
error PoolNotInitialized();
/// @notice Thrown when unlock is called, but the contract is already unlocked
error AlreadyUnlocked();
/// @notice Thrown when a function is called that requires the contract to be unlocked, but it is not
error ManagerLocked();
/// @notice Pools are limited to type(int16).max tickSpacing in #initialize, to prevent overflow
error TickSpacingTooLarge(int24 tickSpacing);
/// @notice Pools must have a positive non-zero tickSpacing passed to #initialize
error TickSpacingTooSmall(int24 tickSpacing);
/// @notice PoolKey must have currencies where address(currency0) < address(currency1)
error CurrenciesOutOfOrderOrEqual(address currency0, address currency1);
/// @notice Thrown when a call to updateDynamicLPFee is made by an address that is not the hook,
/// or on a pool that does not have a dynamic swap fee.
error UnauthorizedDynamicLPFeeUpdate();
/// @notice Thrown when trying to swap amount of 0
error SwapAmountCannotBeZero();
///@notice Thrown when native currency is passed to a non native settlement
error NonzeroNativeValue();
/// @notice Thrown when `clear` is called with an amount that is not exactly equal to the open currency delta.
error MustClearExactPositiveDelta();
/// @notice Emitted when a new pool is initialized
/// @param id The abi encoded hash of the pool key struct for the new pool
/// @param currency0 The first currency of the pool by address sort order
/// @param currency1 The second currency of the pool by address sort order
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks
/// @param hooks The hooks contract address for the pool, or address(0) if none
/// @param sqrtPriceX96 The price of the pool on initialization
/// @param tick The initial tick of the pool corresponding to the initialized price
event Initialize(
PoolId indexed id,
Currency indexed currency0,
Currency indexed currency1,
uint24 fee,
int24 tickSpacing,
IHooks hooks,
uint160 sqrtPriceX96,
int24 tick
);
/// @notice Emitted when a liquidity position is modified
/// @param id The abi encoded hash of the pool key struct for the pool that was modified
/// @param sender The address that modified the pool
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param liquidityDelta The amount of liquidity that was added or removed
/// @param salt The extra data to make positions unique
event ModifyLiquidity(
PoolId indexed id, address indexed sender, int24 tickLower, int24 tickUpper, int256 liquidityDelta, bytes32 salt
);
/// @notice Emitted for swaps between currency0 and currency1
/// @param id The abi encoded hash of the pool key struct for the pool that was modified
/// @param sender The address that initiated the swap call, and that received the callback
/// @param amount0 The delta of the currency0 balance of the pool
/// @param amount1 The delta of the currency1 balance of the pool
/// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of the price of the pool after the swap
/// @param fee The swap fee in hundredths of a bip
event Swap(
PoolId indexed id,
address indexed sender,
int128 amount0,
int128 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick,
uint24 fee
);
/// @notice Emitted for donations
/// @param id The abi encoded hash of the pool key struct for the pool that was donated to
/// @param sender The address that initiated the donate call
/// @param amount0 The amount donated in currency0
/// @param amount1 The amount donated in currency1
event Donate(PoolId indexed id, address indexed sender, uint256 amount0, uint256 amount1);
/// @notice All interactions on the contract that account deltas require unlocking. A caller that calls `unlock` must implement
/// `IUnlockCallback(msg.sender).unlockCallback(data)`, where they interact with the remaining functions on this contract.
/// @dev The only functions callable without an unlocking are `initialize` and `updateDynamicLPFee`
/// @param data Any data to pass to the callback, via `IUnlockCallback(msg.sender).unlockCallback(data)`
/// @return The data returned by the call to `IUnlockCallback(msg.sender).unlockCallback(data)`
function unlock(bytes calldata data) external returns (bytes memory);
/// @notice Initialize the state for a given pool ID
/// @dev A swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee
/// @param key The pool key for the pool to initialize
/// @param sqrtPriceX96 The initial square root price
/// @return tick The initial tick of the pool
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);
struct ModifyLiquidityParams {
// the lower and upper tick of the position
int24 tickLower;
int24 tickUpper;
// how to modify the liquidity
int256 liquidityDelta;
// a value to set if you want unique liquidity positions at the same range
bytes32 salt;
}
/// @notice Modify the liquidity for the given pool
/// @dev Poke by calling with a zero liquidityDelta
/// @param key The pool to modify liquidity in
/// @param params The parameters for modifying the liquidity
/// @param hookData The data to pass through to the add/removeLiquidity hooks
/// @return callerDelta The balance delta of the caller of modifyLiquidity. This is the total of both principal, fee deltas, and hook deltas if applicable
/// @return feesAccrued The balance delta of the fees generated in the liquidity range. Returned for informational purposes
/// @dev Note that feesAccrued can be artificially inflated by a malicious actor and integrators should be careful using the value
/// For pools with a single liquidity position, actors can donate to themselves to inflate feeGrowthGlobal (and consequently feesAccrued)
/// atomically donating and collecting fees in the same unlockCallback may make the inflated value more extreme
function modifyLiquidity(PoolKey memory key, ModifyLiquidityParams memory params, bytes calldata hookData)
external
returns (BalanceDelta callerDelta, BalanceDelta feesAccrued);
struct SwapParams {
/// Whether to swap token0 for token1 or vice versa
bool zeroForOne;
/// The desired input amount if negative (exactIn), or the desired output amount if positive (exactOut)
int256 amountSpecified;
/// The sqrt price at which, if reached, the swap will stop executing
uint160 sqrtPriceLimitX96;
}
/// @notice Swap against the given pool
/// @param key The pool to swap in
/// @param params The parameters for swapping
/// @param hookData The data to pass through to the swap hooks
/// @return swapDelta The balance delta of the address swapping
/// @dev Swapping on low liquidity pools may cause unexpected swap amounts when liquidity available is less than amountSpecified.
/// Additionally note that if interacting with hooks that have the BEFORE_SWAP_RETURNS_DELTA_FLAG or AFTER_SWAP_RETURNS_DELTA_FLAG
/// the hook may alter the swap input/output. Integrators should perform checks on the returned swapDelta.
function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData)
external
returns (BalanceDelta swapDelta);
/// @notice Donate the given currency amounts to the in-range liquidity providers of a pool
/// @dev Calls to donate can be frontrun adding just-in-time liquidity, with the aim of receiving a portion donated funds.
/// Donors should keep this in mind when designing donation mechanisms.
/// @dev This function donates to in-range LPs at slot0.tick. In certain edge-cases of the swap algorithm, the `sqrtPrice` of
/// a pool can be at the lower boundary of tick `n`, but the `slot0.tick` of the pool is already `n - 1`. In this case a call to
/// `donate` would donate to tick `n - 1` (slot0.tick) not tick `n` (getTickAtSqrtPrice(slot0.sqrtPriceX96)).
/// Read the comments in `Pool.swap()` for more information about this.
/// @param key The key of the pool to donate to
/// @param amount0 The amount of currency0 to donate
/// @param amount1 The amount of currency1 to donate
/// @param hookData The data to pass through to the donate hooks
/// @return BalanceDelta The delta of the caller after the donate
function donate(PoolKey memory key, uint256 amount0, uint256 amount1, bytes calldata hookData)
external
returns (BalanceDelta);
/// @notice Writes the current ERC20 balance of the specified currency to transient storage
/// This is used to checkpoint balances for the manager and derive deltas for the caller.
/// @dev This MUST be called before any ERC20 tokens are sent into the contract, but can be skipped
/// for native tokens because the amount to settle is determined by the sent value.
/// However, if an ERC20 token has been synced and not settled, and the caller instead wants to settle
/// native funds, this function can be called with the native currency to then be able to settle the native currency
function sync(Currency currency) external;
/// @notice Called by the user to net out some value owed to the user
/// @dev Will revert if the requested amount is not available, consider using `mint` instead
/// @dev Can also be used as a mechanism for free flash loans
/// @param currency The currency to withdraw from the pool manager
/// @param to The address to withdraw to
/// @param amount The amount of currency to withdraw
function take(Currency currency, address to, uint256 amount) external;
/// @notice Called by the user to pay what is owed
/// @return paid The amount of currency settled
function settle() external payable returns (uint256 paid);
/// @notice Called by the user to pay on behalf of another address
/// @param recipient The address to credit for the payment
/// @return paid The amount of currency settled
function settleFor(address recipient) external payable returns (uint256 paid);
/// @notice WARNING - Any currency that is cleared, will be non-retrievable, and locked in the contract permanently.
/// A call to clear will zero out a positive balance WITHOUT a corresponding transfer.
/// @dev This could be used to clear a balance that is considered dust.
/// Additionally, the amount must be the exact positive balance. This is to enforce that the caller is aware of the amount being cleared.
function clear(Currency currency, uint256 amount) external;
/// @notice Called by the user to move value into ERC6909 balance
/// @param to The address to mint the tokens to
/// @param id The currency address to mint to ERC6909s, as a uint256
/// @param amount The amount of currency to mint
/// @dev The id is converted to a uint160 to correspond to a currency address
/// If the upper 12 bytes are not 0, they will be 0-ed out
function mint(address to, uint256 id, uint256 amount) external;
/// @notice Called by the user to move value from ERC6909 balance
/// @param from The address to burn the tokens from
/// @param id The currency address to burn from ERC6909s, as a uint256
/// @param amount The amount of currency to burn
/// @dev The id is converted to a uint160 to correspond to a currency address
/// If the upper 12 bytes are not 0, they will be 0-ed out
function burn(address from, uint256 id, uint256 amount) external;
/// @notice Updates the pools lp fees for the a pool that has enabled dynamic lp fees.
/// @dev A swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee
/// @param key The key of the pool to update dynamic LP fees for
/// @param newDynamicLPFee The new dynamic pool LP fee
function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicLPFee) external;
}
"
},
"lib/v4-core/src/types/PoolId.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {PoolKey} from "./PoolKey.sol";
type PoolId is bytes32;
/// @notice Library for computing the ID of a pool
library PoolIdLibrary {
/// @notice Returns value equal to keccak256(abi.encode(poolKey))
function toId(PoolKey memory poolKey) internal pure returns (PoolId poolId) {
assembly ("memory-safe") {
// 0xa0 represents the total size of the poolKey struct (5 slots of 32 bytes)
poolId := keccak256(poolKey, 0xa0)
}
}
}
"
},
"lib/v4-core/src/types/PoolKey.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Currency} from "./Currency.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {PoolIdLibrary} from "./PoolId.sol";
using PoolIdLibrary for PoolKey global;
/// @notice Returns the key for identifying a pool
struct PoolKey {
/// @notice The lower currency of the pool, sorted numerically
Currency currency0;
/// @notice The higher currency of the pool, sorted numerically
Currency currency1;
/// @notice The pool LP fee, capped at 1_000_000. If the highest bit is 1, the pool has a dynamic fee and must be exactly equal to 0x800000
uint24 fee;
/// @notice Ticks that involve positions must be a multiple of tick spacing
int24 tickSpacing;
/// @notice The hooks of the pool
IHooks hooks;
}
"
},
"src/base/BaseHookUpgradable.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Upgradeable OpenZeppelin imports
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
// Uniswap v4 core imports
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
// Local imports
import {ImmutableStateUpgradable} from "./ImmutableStateUpgradable.sol";
/// @title Base Hook (Upgradable)
/// @notice Abstract contract for Uniswap v4 hook implementations, designed for upgradeability
abstract contract BaseHookUpgradable is Initializable, IHooks, ImmutableStateUpgradable {
error HookNotImplemented();
/// @notice Initializer for the BaseHookUpgradable contract
/// @param _poolManager The address of the PoolManager contract
function __BaseHookUpgradable_init(IPoolManager _poolManager) internal onlyInitializing {
__ImmutableState_init(_poolManager);
validateHookAddress(this);
}
/// @notice Returns a struct of permissions to signal which hook functions are to be implemented
/// @dev Used at deployment to validate the address correctly represents the expected permissions
function getHookPermissions() public pure virtual returns (Hooks.Permissions memory);
/// @notice Validates the deployed hook address agrees with the expected permissions of the hook
/// @dev this function is virtual so that we can override it during testing,
/// which allows us to deploy an implementation to any address
/// and then etch the bytecode into the correct address
function validateHookAddress(BaseHookUpgradable _this) internal pure virtual {
Hooks.validateHookPermissions(_this, getHookPermissions());
}
/// @inheritdoc IHooks
function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96)
external
onlyPoolManager
returns (bytes4)
{
return _beforeInitialize(sender, key, sqrtPriceX96);
}
function _beforeInitialize(address, PoolKey calldata, uint160) internal virtual returns (bytes4) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, int24 tick)
external
onlyPoolManager
returns (bytes4)
{
return _afterInitialize(sender, key, sqrtPriceX96, tick);
}
function _afterInitialize(address, PoolKey calldata, uint160, int24) internal virtual returns (bytes4) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function beforeAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external onlyPoolManager returns (bytes4) {
return _beforeAddLiquidity(sender, key, params, hookData);
}
function _beforeAddLiquidity(address, PoolKey calldata, IPoolManager.ModifyLiquidityParams calldata, bytes calldata)
internal
virtual
returns (bytes4)
{
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function beforeRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external onlyPoolManager returns (bytes4) {
return _beforeRemoveLiquidity(sender, key, params, hookData);
}
function _beforeRemoveLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
bytes calldata
) internal virtual returns (bytes4) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
BalanceDelta feesAccrued,
bytes calldata hookData
) external onlyPoolManager returns (bytes4, BalanceDelta) {
return _afterAddLiquidity(sender, key, params, delta, feesAccrued, hookData);
}
function _afterAddLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
BalanceDelta,
bytes calldata
) internal virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
BalanceDelta feesAccrued,
bytes calldata hookData
) external onlyPoolManager returns (bytes4, BalanceDelta) {
return _afterRemoveLiquidity(sender, key, params, delta, feesAccrued, hookData);
}
function _afterRemoveLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
BalanceDelta,
bytes calldata
) internal virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function beforeSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) external onlyPoolManager returns (bytes4, BeforeSwapDelta, uint24) {
return _beforeSwap(sender, key, params, hookData);
}
function _beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata)
internal
virtual
returns (bytes4, BeforeSwapDelta, uint24)
{
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) external onlyPoolManager returns (bytes4, int128) {
return _afterSwap(sender, key, params, delta, hookData);
}
function _afterSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata)
internal
virtual
returns (bytes4, int128)
{
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function beforeDonate(
address sender,
PoolKey calldata key,
uint256 amount0,
uint256 amount1,
bytes calldata hookData
) external onlyPoolManager returns (bytes4) {
return _beforeDonate(sender, key, amount0, amount1, hookData);
}
function _beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata)
internal
virtual
returns (bytes4)
{
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterDonate(
address sender,
PoolKey calldata key,
uint256 amount0,
uint256 amount1,
bytes calldata hookData
) external onlyPoolManager returns (bytes4) {
return _afterDonate(sender, key, amount0, amount1, hookData);
}
function _afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata)
internal
virtual
returns (bytes4)
{
revert HookNotImplemented();
}
}
"
},
"src/MevHookUpgradeable.sol": {
"content": "// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
// Upgradeable OpenZeppelin imports
import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {EIP712Upgradeable} from "@openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
// Uniswap v4 imports
import {BalanceDelta, toBalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
// Local imports
import {BaseHookUpgradable} from "./base/BaseHookUpgradable.sol";
// *********************************************************************
// Structs used for authorization and swap operations
// *********************************************************************
/// @notice Struct containing authorization data for hook
struct HookData {
// -------- FOR MEV AUCTIONS --------
/// @notice nonce committed by MEV bot
uint256 nonce;
// --------- FOR SWAPS --------
/// @notice Signature authorizing the swap
bytes signature;
/// @notice deadline after which the signature is invalid
uint256 deadline;
/// @notice the swapper address
address swapper;
}
/// @notice Parameters for swap operations
struct SwapParams {
/// @notice Unique identifier of the pool
PoolId poolId;
/// @notice Sender address
address sender;
/// @notice Whether to swap token0 for token1 (true) or vice versa (false)
bool zeroForOne;
/// @notice The desired input amount if negative (exactIn), or the desired output amount if positive (exactOut)
int256 amountSpecified;
/// @notice Timestamp after which the swap is invalid
uint256 deadline;
}
// *********************************************************************
// MevHook Contract – Upgradeable via UUPS Proxy
// *********************************************************************
/// @notice Hook contract for MEV protection by requiring signed transactions
/// @dev Inherits from BaseHookUpgradable, EIP712Upgradeable, AccessControlUpgradeable, and ReentrancyGuardUpgradeable
contract MevHookUpgradeable is
Initializable,
UUPSUpgradeable,
BaseHookUpgradable,
EIP712Upgradeable,
AccessControlUpgradeable,
ReentrancyGuardUpgradeable
{
using PoolIdLibrary for PoolKey;
using SafeCast for uint256;
using SafeCast for int128;
// ***************************************************************
// Roles
// ***************************************************************
/// @notice New role for fee‐management functions (instead of DEFAULT_ADMIN_ROLE)
bytes32 public constant FEE_MANAGER_ROLE = keccak256("FEE_MANAGER_ROLE");
// SIGNER ROLE - can authorize nonces and swap signatures
bytes32 public constant SIGNER_ROLE = keccak256("SIGNER_ROLE");
// ***************************************************************
// Errors
// ***************************************************************
/// @notice Thrown when a signature has already been used
error SignatureAlreadyUsed();
/// @notice Thrown when a swap signature is invalid or from an unauthorized signer
error InvalidSwapSignature();
/// @notice Thrown when a swap is attempted after its deadline has passed
error SwapExpired();
/// @notice Thrown when a signature's length is not exactly 65 bytes
error InvalidSignatureLength();
/// @notice Thrown when attempting to set a fee greater than 100% (10000 basis points)
error FeeExceeds100Percent();
/// @notice Thrown when attempting to set the fee recipient to the zero address
error CannotSetZeroAddress();
/// @notice Error thrown when an invalid nonce is provided
error InvalidNonce();
/// @notice Error thrown when a pool is not initialized with a wrapped native asset
error PoolNotInitializedWithWrappedNativeAsset();
/// @notice Error thrown when the last used pool is not set
error NoLastUsedPool();
/// @notice Error thrown when the ETH transfer fails
error EthTransferFailed();
// ***************************************************************
// Events
// ***************************************************************
/// @notice Emitted when a tip is provided
/// @param tipper The address providing the tip
/// @param tipAmount The amount of the tip
/// @param mevFeeAmount The amount taken as MEV fee
event Tip(address indexed tipper, uint256 tipAmount, uint256 mevFeeAmount);
/// @notice Emitted when a swap signature is used
/// @param signatureHash The hash of the signature that was used
event SwapSignatureUsed(bytes32 signatureHash);
/// @notice Emitted when a nonce is used
/// @param nonce The nonce value that was used
event NonceUsed(uint256 indexed nonce);
/// @notice Emitted when a fee config is set
/// @param poolId The pool id that the fee config was set for
/// @param mevFeeBps The new MEV fee in basis points
/// @param swapFeeBps The new swap fee in basis points
event FeeConfigSet(
PoolId indexed poolId,
address indexed swapper,
uint256 mevFeeBps,
uint256 swapFeeBps
);
// @notice Emitted when a fee recipient is set
/// @param recipient The new fee recipient
event FeeRecipientSet(address indexed recipient);
// ***************************************************************
// Storage Variables – Ordered for Upgrade Safety
// ***************************************************************
// Slot 0: Authorized nonce for MEV swaps
uint256 public authorizedNonce;
// Slot 1: Last used pool (from Uniswap v4)
PoolKey public lastUsedPool;
// Mapping to track used signatures (occupies its own slot per key)
mapping(bytes32 => bool) public usedSignatures;
// Swap fee recipient
address payable public feeRecipient;
// Array of all pools created
PoolKey[] public allPools;
// Total number of pools created
uint256 public allPoolsLength;
// ***************************************************************
// Fee Configuration Structures and Mappings
// ***************************************************************
struct HookFees {
uint256 mevFeeBps;
uint256 swapFeeBps;
}
// Default hook fees
HookFees public defaultHookFees;
// Fee config per pool
mapping(PoolId => HookFees) public hookFeesPerPool;
// Fee config per swapper
mapping(address => HookFees) public hookFeesPerSwapper;
// ***************************************************************
// Callback Data for ETH Donations
// ***************************************************************
struct DonateCallBackData {
PoolKey key;
uint256 amount;
bool isCurrency0;
}
// ***************************************************************
// Initializer (replaces constructor)
// ***************************************************************
/**
* @notice Initialize the MevHook contract.
* @param _poolManager Address of the pool manager contract.
* @param owner Address to receive the DEFAULT_ADMIN_ROLE and FEE_MANAGER_ROLE.
* @param signer Address that will have the SIGNER_ROLE.
*/
function initialize(
IPoolManager _poolManager,
address owner,
address signer
) external initializer {
// Initialize inherited upgradeable contracts.
__BaseHookUpgradable_init(_poolManager);
__EIP712_init("MevHook", "1");
__AccessControl_init();
__ReentrancyGuard_init();
__UUPSUpgradeable_init();
// Set up roles.
_grantRole(DEFAULT_ADMIN_ROLE, owner);
_grantRole(SIGNER_ROLE, signer);
_grantRole(FEE_MANAGER_ROLE, owner);
}
// ***************************************************************
// UUPS Upgrade Authorization
// ***************************************************************
function _authorizeUpgrade(
address newImplementation
) internal override onlyRole(DEFAULT_ADMIN_ROLE) {}
// ***************************************************************
// Public & External Functions
// ***************************************************************
/// @notice Returns the EIP-712 hash of the swap parameters
/// @param params The swap parameters to hash
/// @return The EIP-712 hash
function getSwapParamsHash(
SwapParams memory params
) public view returns (bytes32) {
bytes32 structHash = keccak256(abi.encode(params));
return _hashTypedDataV4(structHash);
}
/// @notice Allows a signer to authorize a nonce
/// @param nonce The nonce to authorize
function authorizeNonce(uint256 nonce) external onlyRole(SIGNER_ROLE) {
authorizedNonce = nonce;
}
/// @notice Sets the last used pool.
/// @param key The pool key to set
function setLastUsedPool(
PoolKey calldata key
) external onlyRole(SIGNER_ROLE) {
lastUsedPool = key;
}
/// @notice Returns the hook permissions as expected by Uniswap v4.
/// @return Hooks.Permissions struct with the hook's permissions
function getHookPermissions()
public
pure
override
returns (Hooks.Permissions memory)
{
// Only the beforeSwap, beforeInitialize, afterInitialize, and afterSwap hooks are enabled.
return
Hooks.Permissions({
beforeSwap: true,
beforeInitialize: true,
afterInitialize: true,
beforeAddLiquidity: false,
afterAddLiquidity: false,
beforeRemoveLiquidity: false,
afterRemoveLiquidity: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}
// ***************************************************************
// Internal Functions for Swap Signature Verification
// ***************************************************************
/// @notice Verifies the signature for swap operations
/// @param poolId Unique identifier of the pool
/// @param zeroForOne Direction of the swap
/// @param amountSpecified Amount to swap
/// @param deadline Timestamp after which the signature is invalid
/// @param signature Signature authorizing the swap
function _verifySwapSignature(
PoolId poolId,
bool zeroForOne,
int256 amountSpecified,
uint256 deadline,
address swapper,
bytes memory signature
) internal view returns (SwapParams memory) {
// Only allow EOA or zero address as swapper.
if (swapper != tx.origin && swapper != address(0)) {
revert InvalidSwapSignature();
}
// Check signature length
if (signature.length != 65) revert InvalidSignatureLength();
// Check for expiry first
if (block.timestamp > deadline) revert SwapExpired();
SwapParams memory params = SwapParams({
poolId: poolId,
sender: swapper,
zeroForOne: zeroForOne,
amountSpecified: amountSpecified,
deadline: deadline
});
bytes32 structHash = getSwapParamsHash(params);
// Recover the signer from the signature.
address recoveredSigner = ECDSA.recover(structHash, signature);
if (!hasRole(SIGNER_ROLE, recoveredSigner))
revert InvalidSwapSignature();
return params;
}
// ***************************************************************
// Uniswap v4 Hook Overrides
// ***************************************************************
/// @notice Hook called before swap execution.
/// @param key Pool key containing token addresses and fee
/// @param params Parameters for the swap
/// @param hookData Data containing authorization signature
/// @return bytes4 Function selector
function _beforeSwap(
address, // removed 'sender' parameter name since it's unused
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) internal virtual override returns (bytes4, BeforeSwapDelta, uint24) {
// Allow bypass in simulation (tx.origin is zero, zero gas price, or zero block timestamp).
if (
tx.origin == address(0) || tx.gasprice == 0 || block.timestamp == 0
) {
return (
BaseHookUpgradable.beforeSwap.selector,
BeforeSwapDeltaLibrary.ZERO_DELTA,
0
);
}
// Decode hook data.
HookData memory data = abi.decode(hookData, (HookData));
// If a nonce is used, verify it.
if (data.nonce != 0) {
if (data.nonce != authorizedNonce) {
revert InvalidNonce();
} else {
emit NonceUsed(data.nonce);
// Clear the nonce after use to prevent replay attacks.
authorizedNonce = 0;
}
}
// Signature based swaps
if (data.nonce == 0) {
_verifySwapSignature(
key.toId(),
params.zeroForOne,
params.amountSpecified,
data.deadline,
data.swapper,
data.signature
);
emit SwapSignatureUsed(keccak256(data.signature));
}
lastUsedPool = key;
return (
BaseHookUpgradable.beforeSwap.selector,
BeforeSwapDeltaLibrary.ZERO_DELTA,
0
);
}
/// @notice Hook called after swap execution.
/// @param key Pool key containing token addresses and fee
/// @param params Parameters for the swap
/// @param delta Balance delta
/// @return bytes4 Function selector
function _afterSwap(
address,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta delta,
bytes calldata
) internal virtual override returns (bytes4, int128) {
// Get fee configuration.
HookFees memory fee = getFeeConfig(key.toId(), tx.origin);
if (fee.swapFeeBps > 0) {
// if the amount specified is negative, and the swap is token0->token1, then the fee is on token0
bool isToken0 = (params.amountSpecified < 0 == params.zeroForOne);
Currency asset = key.currency1;
int128 amount = delta.amount1();
if (!isToken0) {
asset = key.currency0;
amount = delta.amount0();
}
if (amount < 0) amount = -amount;
uint256 computedFee = (uint256(uint128(amount)) * fee.swapFeeBps) /
10_000;
// Mint the fee to the fee recipient.
poolManager.mint(address(feeRecipient), asset.toId(), computedFee);
return (
BaseHookUpgradable.afterSwap.selector,
computedFee.toInt128()
);
}
return (BaseHookUpgradable.afterSwap.selector, 0);
}
/// @notice Ensures at least one asset is ETH during pool initialization.
/// @param key The pool key containing information about the pool being initialized
function _beforeInitialize(
address,
PoolKey calldata key,
uint160
) internal pure override returns (bytes4) {
if (Currency.unwrap(key.currency0) != address(0))
revert PoolNotInitializedWithWrappedNativeAsset();
return BaseHookUpgradable.beforeInitialize.selector;
}
/// @notice Hook called after pool initialization.
/// @param key The pool key containing information about the pool being initialized
function _afterInitialize(
address,
PoolKey calldata key,
uint160,
int24
) internal virtual override returns (bytes4) {
allPools.push(key);
allPoolsLength++;
return BaseHookUpgradable.afterInitialize.selector;
}
// ***************************************************************
// Fee Configuration Functions
// ***************************************************************
// @notice Returns the fee configuration for a given pool id and swapper address
/// @param poolId The pool id to get the fee config for
/// @param swapper The swapper address to get the fee config for
/// @return HookFees The fee configuration for the given pool id and swapper address
function getFeeConfig(
PoolId poolId,
address swapper
) public view returns (HookFees memory) {
HookFees memory fee = defaultHookFees;
if (
(hookFeesPerPool[poolId].mevFeeBps != 0 ||
hookFeesPerPool[poolId].swapFeeBps != 0)
) {
fee = hookFeesPerPool[poolId];
}
if (
swapper != address(0) &&
(hookFeesPerSwapper[swapper].mevFeeBps != 0 ||
hookFeesPerSwapper[swapper].swapFeeBps != 0)
) {
fee = hookFeesPerSwapper[swapper];
}
return fee;
}
/// @notice Sets the fee configuration for a given pool id
/// @param poolId The pool id to set the fee config for
/// @param fee The fee configuration to set
/// @dev Emits a FeeConfigSet event
/// @dev Can only be called by the FEE_MANAGER_ROLE
function setFeeConfigForPool(
PoolId poolId,
HookFees memory fee
) external onlyRole(FEE_MANAGER_ROLE) {
hookFeesPerPool[poolId] = fee;
// If fee is 0, remove the pool from the list.
if (fee.mevFeeBps == 0 && fee.swapFeeBps == 0) {
delete hookFeesPerPool[poolId];
}
emit FeeConfigSet(poolId, address(0), fee.mevFeeBps, fee.swapFeeBps);
}
/// @notice Sets the fee configuration for a given swapper address
/// @param swapper The swapper address to set the fee config for
/// @param fee The fee configuration to set
/// @dev Emits a FeeConfigSet event
/// @dev Can only be called by the FEE_MANAGER_ROLE
function setFeeConfigForSwapper(
address swapper,
HookFees memory fee
) external onlyRole(FEE_MANAGER_ROLE) {
hookFeesPerSwapper[swapper] = fee;
// If fee is 0, remove the swapper from the list.
if (fee.mevFeeBps == 0 && fee.swapFeeBps == 0) {
delete hookFeesPerSwapper[swapper];
}
emit FeeConfigSet(
PoolId.wrap(0x0),
swapper,
fee.mevFeeBps,
fee.swapFeeBps
);
}
/// @notice Sets the default fee configuration
/// @param fee The fee configuration to set
/// @dev Emits a FeeConfigSet event
/// @dev Can only be called by the FEE_MANAGER_ROLE
function setDefaultFeeConfig(
HookFees memory fee
) external onlyRole(FEE_MANAGER_ROLE) {
defaultHookFees = fee;
emit FeeConfigSet(
PoolId.wrap(0x0),
address(0),
defaultHookFees.mevFeeBps,
defaultHookFees.swapFeeBps
);
}
// ***************************************************************
// ETH Donation and Fallback Functionality
// ***************************************************************
/// @notice Fallback function to receive ETH and redistribute it
/// @dev This function is called when ETH is sent to the contract
receive() external payable virtual nonReentrant {
if (address(lastUsedPool.hooks) == address(0)) revert NoLastUsedPool();
// Update state before external calls to prevent reentrancy
PoolKey memory pool = lastUsedPool;
delete lastUsedPool;
uint256 amount = msg.value;
// Calculate distribution amounts based on percentages.
uint256 feeAmount = 0;
// If mevFee is set, use it for fee calculation.
if (defaultHookFees.mevFeeBps > 0) {
HookFees memory fee = getFeeConfig(pool.toId(), address(0));
// mevFee is in basis points (1/100 of a percent).
feeAmount = (amount * fee.mevFeeBps) / 10_000; // Fee based on mevFee
}
uint256 poolAmount = amount - feeAmount;
// Send to fee recipient if set.
if (feeAmount > 0 && feeRecipient != address(0)) {
(bool success, ) = feeRecipient.call{value: feeAmount}("");
if (!success) revert EthTransferFailed();
}
// Donate to the pool.
if (poolAmount > 0) {
donateToPool(pool, poolAmount);
}
emit Tip(msg.sender, poolAmount, feeAmount);
}
/// @notice Helper function to donate ETH to a pool
/// @param key The pool key to donate to
/// @param amount The amount to donate
function donateToPool(PoolKey memory key, uint256 amount) internal {
bool isCurrency0;
if (key.currency0 == Currency.wrap(address(0))) {
isCurrency0 = true;
} else {
isCurrency0 = false;
}
// Encode callback data.
bytes memory callbackData = abi.encode(
DonateCallBackData(key, amount, isCurrency0)
);
// Unlock the pool manager to donate.
poolManager.unlock(callbackData);
}
function unlockCallback(
bytes calldata data
) external onlyPoolManager returns (bytes memory) {
DonateCallBackData memory decoded = abi.decode(
data,
(DonateCallBackData)
);
uint256 amount0;
uint256 amount1;
if (decoded.isCurrency0) {
amount0 = decoded.amount;
amount1 = 0;
} else {
amount0 = 0;
amount1 = decoded.amount;
}
poolManager.sync(Currency.wrap(address(0)));
poolManager.donate(decoded.key, amount0, amount1, bytes(""));
// Send the weth to the pool.
poolManager.settle{value: decoded.amount}();
poolManager.settle();
return bytes("");
}
/// @notice Sets the fee recipient address
/// @param recipient The new fee recipient address
/// @dev Emits a FeeRecipientSet event
/// @dev Can only be called by the FEE_MANAGER_ROLE
function setFeeRecipient(
address recipient
) external onlyRole(FEE_MANAGER_ROLE) {
feeRecipient = payable(recipient);
emit FeeRecipientSet(recipient);
}
}
"
},
"src/MevHookUpgradeableV9.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolId} from "@uniswap/v4-core/src/types/PoolId.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BaseHookUpgradable} from "./base/BaseHookUpgradable.sol";
import {HookData} from "./MevHookUpgradeable.sol";
import {MevHookUpgradeableV8} from "./MevHookUpgradeableV8.sol";
/// @title MEV Hook – Upgradeable implementation V9
/// @dev Extends {MevHookUpgradeableV8} maintaining state layout compatibility.
contract MevHookUpgradeableV9 is MevHookUpgradeableV8 {
// -------------------------------------------------------------------
// State Variables
// -------------------------------------------------------------------
/// @notice Packed refund details to save gas on storage operations
/// @dev Uses a single storage slot: [16 bits poolIndex][160 bits swapper address]
/// @dev poolIndex is 1-based, 0 indicates no refund details
uint176 public packedRefundDetails;
// -------------------------------------------------------------------
// Blacklist State Variables
// -------------------------------------------------------------------
/// @notice Mapping to track blacklisted addresses
mapping(address => bool) public blacklistedAddresses;
/// @notice Array to track all blacklisted addresses for enumeration
address[] public blacklistedAddressesArray;
/// @notice Mapping to track if an address is in the array (for efficient removal)
mapping(address => uint256) public blacklistedAddressesIndex;
// -------------------------------------------------------------------
// Blacklist Events
// -------------------------------------------------------------------
/// @notice Emitted when an address is added to the blacklist
/// @param account The address that was blacklisted
event AddressBlacklisted(address indexed account);
/// @notice Emitted when an address is removed from the blacklist
/// @param account The address that was removed from blacklist
event AddressUnblacklisted(address indexed account);
/// @notice Emitted when multiple addresses are added to the blacklist
/// @param accounts Array of addresses that were blacklisted
event AddressesBlacklisted(address[] accounts);
/// @notice Emitted when multiple addresses are removed from the blacklist
/// @param accounts Array of addresses that were removed from blacklist
event AddressesUnblacklisted(address[] accounts);
// -------------------------------------------------------------------
// Blacklist Errors
// -------------------------------------------------------------------
/// @notice Thrown when a blacklisted address attempts to perform a swap
error AddressIsBlacklisted();
/// @notice Thrown when trying to blacklist the zero address
error CannotBlacklistZeroAddress();
/// @notice Thrown when trying to blacklist an already blacklisted address
error AddressAlreadyBlacklisted();
/// @notice Thrown when trying to unblacklist an address that is not blacklisted
error AddressNotBlacklisted();
/// @notice Hook called before swap execution.
/// @param key Pool key containing token addresses and fee
/// @param params Parameters for the swap
/// @param hookData Data containing authorization signature
/// @return bytes4 Function selector
function _beforeSwap(
address, // removed 'sender' parameter name since it's unused
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) internal virtual override returns (bytes4, BeforeSwapDelta, uint24) {
// Check if tx.origin is blacklisted
if (blacklistedAddresses[tx.origin]) {
revert AddressIsBlacklisted();
}
// Check hookData length first to avoid unnecessary operations
if (hookData.length == 0) {
if (
tx.origin == address(0) ||
tx.gasprice == 0 ||
block.timestamp == 0
) {
return (
BaseHookUpgradable.beforeSwap.selector,
BeforeSwapDeltaLibrary.ZERO_DELTA,
0
);
}
// Only check unlocked block if no signature provided
if (unlockedBlock == block.number) {
// Gas optimization: Pack pool index and swapper into single storage slot
uint16 unlockedPoolIndex = _getOrAddPoolKeyIndexOptimized(key);
packedRefundDetails =
uint176(unlockedPoolIndex) |
(uint176(uint160(tx.origin)) << 16);
// pool is unlocked, no need for signature verification
return (
BaseHookUpgradable.beforeSwap.selector,
BeforeSwapDeltaLibrary.ZERO_DELTA,
0
);
}
// Signature swaps are allowed only when there is a hookData
revert Locked();
}
HookData memory data = abi.decode(hookData, (HookData));
// Signature verification
_verifySwapSignature(
key.toId(),
params.zeroForOne,
params.amountSpecified,
data.deadline,
data.swapper,
data.signature
);
emit SwapSignatureUsed(keccak256(data.signature));
return (
BaseHookUpgradable.beforeSwap.selector,
BeforeSwapDeltaLibrary.ZERO_DELTA,
0
);
}
/// @notice Gas optimized pool key index management
/// @dev Reduces storage operations by caching frequently accessed data
function _getOrAddPoolKeyIndexOptimized(
PoolKey calldata key
) internal returns (uint16) {
PoolId poolId = key.toId();
uint16 idx = poolKeyIndex[poolId];
// Early return if pool already exists
if (idx != 0) {
return idx;
}
// Single storage write for new pool
allPoolKeys.push(key);
uint16 newIndex = uint16(allPoolKeys.length);
poolKeyIndex[poolId] = newIndex;
return newIndex;
}
// -------------------------------------------------------------------
// ETH Receive Logic
// -------------------------------------------------------------------
/// @notice Fallback function to receive ETH and redistribute it
receive() external payable virtual override nonReentrant {
// reset unlocked block
unlockedBlock = 0;
// Gas optimization: Extract packed data in single operation
uint176 packed = packedRefundDetails;
uint16 poolIndex = uint16(packed & 0xFFFF);
address lastSwapper = address(uint160(packed >> 16));
if (poolIndex == 0) revert NoLastUsedPool();
// Pool index is stored as 1-based, so subtract 1 for array access
PoolKey memory pool = allPoolKeys[poolIndex - 1];
uint256 amount = msg.value;
// Calculate minimum required to cover basefee for the unlock tx
uint256 minRequired = block.basefee * unlockTxGasUsed;
if (amount < minRequired) revert TipTooLow(amount, minRequired);
// Subtract the required base fee from the amount to get the actual tip
amount -= minRequired;
// Transfer the required basefee to the signer
(bool okBaseTransfer, ) = signer.call{value: minRequired}("");
if (!okBaseTransfer) revert EthTransferFailed();
// Calculate fee only if needed
Submitted on: 2025-10-19 10:25:13
Comments
Log in to comment.
No comments yet.