Skip to content

Commit

Permalink
add reverting tests, fix fuzz for hooks.t.sol (#548)
Browse files Browse the repository at this point in the history
* add reverting tests, fix fuzz for hooks.t.sol

* cleanup

* fix comments

* fix failing CI

* fix other CI

* typo in comment

* make hooks 160s

---------

Co-authored-by: Alice Henshaw <[email protected]>
Co-authored-by: Alice <[email protected]>
  • Loading branch information
3 people authored May 17, 2024
1 parent e3cea10 commit 6786fd4
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 82 deletions.
32 changes: 16 additions & 16 deletions src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ library Hooks {
using BeforeSwapDeltaLibrary for BeforeSwapDelta;
using ParseBytes for bytes;

uint256 internal constant BEFORE_INITIALIZE_FLAG = 1 << 159;
uint256 internal constant AFTER_INITIALIZE_FLAG = 1 << 158;
uint160 internal constant BEFORE_INITIALIZE_FLAG = 1 << 159;
uint160 internal constant AFTER_INITIALIZE_FLAG = 1 << 158;

uint256 internal constant BEFORE_ADD_LIQUIDITY_FLAG = 1 << 157;
uint256 internal constant AFTER_ADD_LIQUIDITY_FLAG = 1 << 156;
uint160 internal constant BEFORE_ADD_LIQUIDITY_FLAG = 1 << 157;
uint160 internal constant AFTER_ADD_LIQUIDITY_FLAG = 1 << 156;

uint256 internal constant BEFORE_REMOVE_LIQUIDITY_FLAG = 1 << 155;
uint256 internal constant AFTER_REMOVE_LIQUIDITY_FLAG = 1 << 154;
uint160 internal constant BEFORE_REMOVE_LIQUIDITY_FLAG = 1 << 155;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_FLAG = 1 << 154;

uint256 internal constant BEFORE_SWAP_FLAG = 1 << 153;
uint256 internal constant AFTER_SWAP_FLAG = 1 << 152;
uint160 internal constant BEFORE_SWAP_FLAG = 1 << 153;
uint160 internal constant AFTER_SWAP_FLAG = 1 << 152;

uint256 internal constant BEFORE_DONATE_FLAG = 1 << 151;
uint256 internal constant AFTER_DONATE_FLAG = 1 << 150;
uint160 internal constant BEFORE_DONATE_FLAG = 1 << 151;
uint160 internal constant AFTER_DONATE_FLAG = 1 << 150;

uint256 internal constant BEFORE_SWAP_RETURNS_DELTA_FLAG = 1 << 149;
uint256 internal constant AFTER_SWAP_RETURNS_DELTA_FLAG = 1 << 148;
uint256 internal constant AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 147;
uint256 internal constant AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 146;
uint160 internal constant BEFORE_SWAP_RETURNS_DELTA_FLAG = 1 << 149;
uint160 internal constant AFTER_SWAP_RETURNS_DELTA_FLAG = 1 << 148;
uint160 internal constant AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 147;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 146;

struct Permissions {
bool beforeInitialize;
Expand Down Expand Up @@ -318,8 +318,8 @@ library Hooks {
}
}

function hasPermission(IHooks self, uint256 flag) internal pure returns (bool) {
return uint256(uint160(address(self))) & flag != 0;
function hasPermission(IHooks self, uint160 flag) internal pure returns (bool) {
return uint160(address(self)) & flag != 0;
}

/// @notice bubble up revert if present. Else throw FailedHookCall
Expand Down
106 changes: 106 additions & 0 deletions src/test/EmptyRevertHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {IHooks} from "../interfaces/IHooks.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";

contract EmptyRevertHook is IHooks {
function beforeInitialize(
address, /* sender **/
PoolKey calldata, /* key **/
uint160, /* sqrtPriceX96 **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert();
}

function afterInitialize(
address, /* sender **/
PoolKey calldata, /* key **/
uint160, /* sqrtPriceX96 **/
int24, /* tick **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert();
}

function beforeAddLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert();
}

function afterAddLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
BalanceDelta, /* delta **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, BalanceDelta) {
revert();
}

function beforeRemoveLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert();
}

function afterRemoveLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
BalanceDelta, /* delta **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, BalanceDelta) {
revert();
}

function beforeSwap(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.SwapParams calldata, /* params **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, BeforeSwapDelta, uint24) {
revert();
}

function afterSwap(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.SwapParams calldata, /* params **/
BalanceDelta, /* delta **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, int128) {
revert();
}

function beforeDonate(
address, /* sender **/
PoolKey calldata, /* key **/
uint256, /* amount0 **/
uint256, /* amount1 **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert();
}

function afterDonate(
address, /* sender **/
PoolKey calldata, /* key **/
uint256, /* amount0 **/
uint256, /* amount1 **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert();
}
}
Loading

0 comments on commit 6786fd4

Please sign in to comment.