Skip to content

Commit

Permalink
clean tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snreynolds committed Sep 26, 2023
1 parent 90e1b79 commit 3ad999a
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 112 deletions.
87 changes: 87 additions & 0 deletions contracts/test/BaseTestHooks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

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

contract BaseTestHooks is IHooks {
error HookNotImplemented();

function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, bytes calldata hookData)
external
virtual
returns (bytes4)
{
revert HookNotImplemented();
}

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

function beforeModifyPosition(
address sender,
PoolKey calldata key,
IPoolManager.ModifyPositionParams calldata params,
bytes calldata hookData
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function afterModifyPosition(
address sender,
PoolKey calldata key,
IPoolManager.ModifyPositionParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

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

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

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

function afterDonate(
address sender,
PoolKey calldata key,
uint256 amount0,
uint256 amount1,
bytes calldata hookData
) external virtual returns (bytes4) {
revert HookNotImplemented();
}
}
47 changes: 47 additions & 0 deletions contracts/test/DynamicFeesTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {BaseTestHooks} from "./BaseTestHooks.sol";
import {IDynamicFeeManager} from "../interfaces/IDynamicFeeManager.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {IHooks} from "../interfaces/IHooks.sol";

contract DynamicFeesTest is BaseTestHooks, IDynamicFeeManager {
uint24 internal fee;
IPoolManager manager;

constructor() {}

function setManager(IPoolManager _manager) external {
manager = _manager;
}

function setFee(uint24 _fee) external {
fee = _fee;
}

function getFee(address, PoolKey calldata) public view returns (uint24) {
return fee;
}

function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, bytes calldata hookData)
external
override
returns (bytes4)
{
// updates the dynamic fee in the pool if update is true
bool _update;
uint24 _fee;

if (hookData.length > 0) {
(_update, _fee) = abi.decode(hookData, (bool, uint24));
}
if (_update == true) {
fee = _fee;

manager.setDynamicFee(key);
}
return IHooks.beforeSwap.selector;
}
}
123 changes: 11 additions & 112 deletions test/foundry-tests/DynamicFees.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,126 +9,18 @@ import {FeeLibrary} from "../../contracts/libraries/FeeLibrary.sol";
import {IPoolManager} from "../../contracts/interfaces/IPoolManager.sol";
import {IFees} from "../../contracts/interfaces/IFees.sol";
import {IHooks} from "../../contracts/interfaces/IHooks.sol";
import {Currency} from "../../contracts/types/Currency.sol";
import {PoolKey} from "../../contracts/types/PoolKey.sol";
import {PoolManager} from "../../contracts/PoolManager.sol";
import {PoolSwapTest} from "../../contracts/test/PoolSwapTest.sol";
import {Deployers} from "./utils/Deployers.sol";
import {IDynamicFeeManager} from "././../../contracts/interfaces/IDynamicFeeManager.sol";
import {Fees} from "./../../contracts/Fees.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {IHooks} from "../../contracts/interfaces/IHooks.sol";
import {IPoolManager} from "../../contracts/interfaces/IPoolManager.sol";
import {BalanceDelta} from "../../contracts/types/BalanceDelta.sol";

contract DynamicFees is IHooks, IDynamicFeeManager {
uint24 internal fee;
IPoolManager manager;

constructor() {}

function setManager(IPoolManager _manager) external {
manager = _manager;
}

function setFee(uint24 _fee) external {
fee = _fee;
}

function getFee(address, PoolKey calldata) public view returns (uint24) {
return fee;
}

function beforeSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) external returns (bytes4) {
// updates the dynamic fee in the pool if update is true
bool _update;
uint24 _fee;

if (hookData.length > 0) {
(_update, _fee) = abi.decode(hookData, (bool, uint24));
}
if (_update == true) {
fee = _fee;

manager.setDynamicFee(key);
}
return IHooks.beforeSwap.selector;
}

function beforeInitialize(address, PoolKey calldata, uint160, bytes calldata)
external
view
override
returns (bytes4)
{
revert("not implemented");
}

function afterInitialize(address, PoolKey calldata, uint160, int24, bytes calldata)
external
view
override
returns (bytes4)
{
revert("not implemented");
}

function beforeModifyPosition(address, PoolKey calldata, IPoolManager.ModifyPositionParams calldata, bytes calldata)
external
view
override
returns (bytes4)
{
revert("not implemented");
}

function afterModifyPosition(
address,
PoolKey calldata,
IPoolManager.ModifyPositionParams calldata,
BalanceDelta,
bytes calldata
) external view override returns (bytes4) {
revert("not implemented");
}

function afterSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata)
external
view
override
returns (bytes4)
{
revert("not implemented");
}

function beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata)
external
view
override
returns (bytes4)
{
revert("not implemented");
}

function afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata)
external
view
override
returns (bytes4)
{
revert("not implemented");
}
}
import {DynamicFeesTest} from "../../contracts/test/DynamicFeesTest.sol";

contract TestDynamicFees is Test, Deployers, GasSnapshot {
using PoolIdLibrary for PoolKey;

DynamicFees dynamicFees = DynamicFees(
DynamicFeesTest dynamicFees = DynamicFeesTest(
address(
uint160(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF)
& uint160(
Expand All @@ -139,7 +31,7 @@ contract TestDynamicFees is Test, Deployers, GasSnapshot {
)
);

DynamicFees dynamicFeesNoHook = DynamicFees(
DynamicFeesTest dynamicFeesNoHook = DynamicFeesTest(
address(
uint160(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF)
& uint160(
Expand All @@ -156,7 +48,7 @@ contract TestDynamicFees is Test, Deployers, GasSnapshot {
PoolSwapTest swapRouter;

function setUp() public {
DynamicFees impl = new DynamicFees();
DynamicFeesTest impl = new DynamicFeesTest();
vm.etch(address(dynamicFees), address(impl).code);
vm.etch(address(dynamicFeesNoHook), address(impl).code);

Expand All @@ -173,6 +65,13 @@ contract TestDynamicFees is Test, Deployers, GasSnapshot {
swapRouter = new PoolSwapTest(manager);
}

function testPoolInitializeFailsWithTooLargeFee() public {
dynamicFees.setFee(1000000);
PoolKey memory key0 = Deployers.createKey(IHooks(address(dynamicFees)), FeeLibrary.DYNAMIC_FEE_FLAG);
vm.expectRevert(IFees.FeeTooLarge.selector);
manager.initialize(key0, SQRT_RATIO_1_1, ZERO_BYTES);
}

function testSwapFailsWithTooLargeFee() public {
dynamicFees.setFee(1000000);
vm.expectRevert(IFees.FeeTooLarge.selector);
Expand Down
6 changes: 6 additions & 0 deletions test/foundry-tests/utils/Deployers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ contract Deployers {
manager.initialize(key, sqrtPriceX96, initData);
}

function createKey(IHooks hooks, uint24 fee) internal returns (PoolKey memory key) {
MockERC20[] memory tokens = deployTokens(2, 2 ** 255);
(Currency currency0, Currency currency1) = SortTokens.sort(tokens[0], tokens[1]);
key = PoolKey(currency0, currency1, fee, fee.isDynamicFee() ? int24(60) : int24(fee / 100 * 2), hooks);
}

function createFreshPool(IHooks hooks, uint24 fee, uint160 sqrtPriceX96)
internal
returns (PoolManager manager, PoolKey memory key, PoolId id)
Expand Down

0 comments on commit 3ad999a

Please sign in to comment.