Skip to content

Commit

Permalink
Dynamic after fee mock + test base
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoeth committed Oct 13, 2024
1 parent cbcea23 commit 2a950eb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ libs = ['lib'] # A list of library dire
optimizer = true # Enable or disable the solc optimizer
optimizer_runs = 200 # The number of optimizer runs
fs_permissions = [{ access = "read", path = "./"}] # Gives permission to read files for deployment keys.
evm_version = "cancun" # The EVM version to use

[fuzz]
runs = 100 # The number of times to run the fuzzing tests
Expand Down
2 changes: 1 addition & 1 deletion src/base/DynamicAfterFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {BeforeSwapDelta} from "v4-core/src/types/BeforeSwapDelta.sol";

abstract contract DynamicAfterFee is BaseHook {
mapping(PoolId => BalanceDelta) private _targetDeltas;
mapping(PoolId => BalanceDelta) internal _targetDeltas;

constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

Expand Down
37 changes: 37 additions & 0 deletions test/DynamicAfterFee.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {Deployers} from "v4-core/test/utils/Deployers.sol";
import {DynamicAfterFeeMock} from "test/mocks/DynamicAfterFeeMock.sol";
import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {PoolSwapTest} from "v4-core/src/test/PoolSwapTest.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {LPFeeLibrary} from "v4-core/src/libraries/LPFeeLibrary.sol";

contract DynamicAfterFeeTest is Test, Deployers {
DynamicAfterFeeMock hook;
PoolKey noHookKey;

function setUp() public {
deployFreshManagerAndRouters();
deployMintAndApprove2Currencies();

hook = DynamicAfterFeeMock(
address(uint160(Hooks.BEFORE_SWAP_FLAG | Hooks.AFTER_SWAP_FLAG | Hooks.AFTER_SWAP_RETURNS_DELTA_FLAG))
);
deployCodeTo("test/mocks/DynamicAfterFeeMock.sol:DynamicAfterFeeMock", abi.encode(manager), address(hook));

(key,) = initPoolAndAddLiquidity(
currency0, currency1, IHooks(address(hook)), LPFeeLibrary.DYNAMIC_FEE_FLAG, SQRT_PRICE_1_1
);
(noHookKey,) = initPoolAndAddLiquidity(currency0, currency1, IHooks(address(0)), 100, SQRT_PRICE_1_1);

vm.label(Currency.unwrap(currency0), "currency0");
vm.label(Currency.unwrap(currency1), "currency1");
}
}
40 changes: 40 additions & 0 deletions test/mocks/DynamicAfterFeeMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "src/base/DynamicAfterFee.sol";
import {BeforeSwapDeltaLibrary} from "v4-core/src/types/BeforeSwapDelta.sol";
import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol";

contract DynamicAfterFeeMock is DynamicAfterFee {
constructor(IPoolManager _poolManager) DynamicAfterFee(_poolManager) {}

function _beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata)
internal
override
returns (bytes4, BeforeSwapDelta, uint24)
{
PoolId poolId = key.toId();
_targetDeltas[poolId] = BalanceDelta.wrap(0);

return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}

function getHookPermissions() public pure virtual 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: true,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}
}

0 comments on commit 2a950eb

Please sign in to comment.