Skip to content

Commit

Permalink
add gas tests with revert quoter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun1on committed Aug 13, 2024
1 parent 05ad967 commit 54c12c8
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions .forge-snapshots/MIDDLEWARE_PROTECT-multi-protected.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
872991
1 change: 1 addition & 0 deletions .forge-snapshots/MIDDLEWARE_PROTECT-multi-vanilla.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
475485
1 change: 1 addition & 0 deletions .forge-snapshots/MIDDLEWARE_PROTECT-protected.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
229771
1 change: 1 addition & 0 deletions .forge-snapshots/MIDDLEWARE_PROTECT-vanilla.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
147725
29 changes: 29 additions & 0 deletions test/MiddlewareProtectFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {FrontrunAdd} from "./middleware/FrontrunAdd.sol";
import {LPFeeLibrary} from "@uniswap/v4-core/src/libraries/LPFeeLibrary.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {BaseMiddleware} from "./../src/middleware/BaseMiddleware.sol";
import {BlankSwapHooks} from "./middleware/BlankSwapHooks.sol";

contract MiddlewareProtectFactoryTest is Test, Deployers, GasSnapshot {
HookEnabledSwapRouter router;
Expand Down Expand Up @@ -379,4 +380,32 @@ contract MiddlewareProtectFactoryTest is Test, Deployers, GasSnapshot {
assertEq(counterProxy.beforeRemoveLiquidityCount(id), 1);
assertEq(counterProxy.afterRemoveLiquidityCount(id), 1);
}

function testMiddlewareRemoveGas() public {
uint160 flags = Hooks.BEFORE_SWAP_FLAG | Hooks.AFTER_SWAP_FLAG;
BlankSwapHooks blankSwapHooks = BlankSwapHooks(address(flags));
vm.etch(address(blankSwapHooks), address(new BlankSwapHooks(manager)).code);
(key,) = initPoolAndAddLiquidity(
currency0, currency1, IHooks(address(blankSwapHooks)), 3000, SQRT_PRICE_1_1, ZERO_BYTES
);
swap(key, true, 0.0001 ether, ZERO_BYTES);
snapLastCall("MIDDLEWARE_PROTECT-vanilla");
uint160 maxFeeBips = 0;
(, bytes32 salt) = HookMiner.find(
address(factory),
flags,
type(MiddlewareProtect).creationCode,
abi.encode(address(manager), address(blankSwapHooks))
);
address hookAddress = factory.createMiddleware(address(blankSwapHooks), salt);
(PoolKey memory protectedKey,) =
initPoolAndAddLiquidity(currency0, currency1, IHooks(hookAddress), 3000, SQRT_PRICE_1_1, ZERO_BYTES);
swap(protectedKey, true, 0.0001 ether, ZERO_BYTES);
snapLastCall("MIDDLEWARE_PROTECT-protected");

swap(key, true, 0.01 ether, ZERO_BYTES);
snapLastCall("MIDDLEWARE_PROTECT-multi-vanilla");
swap(protectedKey, true, 0.01 ether, ZERO_BYTES);
snapLastCall("MIDDLEWARE_PROTECT-multi-protected");
}
}
53 changes: 53 additions & 0 deletions test/middleware/BlankSwapHooks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {BaseHook} from "./../../src/base/hooks/BaseHook.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "@uniswap/v4-core/src/types/BalanceDelta.sol";

contract BlankSwapHooks is BaseHook {
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

// for testing
function validateHookAddress(BaseHook _this) internal pure override {}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: false,
afterInitialize: false,
beforeAddLiquidity: false,
afterAddLiquidity: false,
beforeRemoveLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata)
external
override
onlyByPoolManager
returns (bytes4, BeforeSwapDelta, uint24)
{
return (BaseHook.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}

function afterSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata)
external
override
onlyByPoolManager
returns (bytes4, int128)
{
return (BaseHook.afterSwap.selector, 0);
}
}

0 comments on commit 54c12c8

Please sign in to comment.