Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use deployCodeTo cheatcode for testing hooks #122

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@uniswap/v4-core/=lib/v4-core/
solmate/=lib/solmate/src/
forge-std/=lib/forge-std/src/
forge-std/=lib/v4-core/lib/forge-std/src/
@openzeppelin/=lib/openzeppelin-contracts/
11 changes: 6 additions & 5 deletions test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {Test} from "forge-std/Test.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {FullRange} from "../contracts/hooks/examples/FullRange.sol";
import {FullRangeImplementation} from "./shared/implementation/FullRangeImplementation.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol";
Expand All @@ -21,6 +20,7 @@ import {FullMath} from "@uniswap/v4-core/src/libraries/FullMath.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {HookEnabledSwapRouter} from "./utils/HookEnabledSwapRouter.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {HookTestAddress} from "./utils/HookTestAddress.sol";

contract TestFullRange is Test, Deployers, GasSnapshot {
using PoolIdLibrary for PoolKey;
Expand Down Expand Up @@ -66,8 +66,10 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
MockERC20 token1;
MockERC20 token2;

FullRangeImplementation fullRange = FullRangeImplementation(
address(uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG | Hooks.BEFORE_SWAP_FLAG))
FullRange fullRange = FullRange(
HookTestAddress.getHookAddress(
uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG | Hooks.BEFORE_SWAP_FLAG)
)
);

PoolId id;
Expand All @@ -87,8 +89,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
token1 = tokens[1];
token2 = tokens[2];

FullRangeImplementation impl = new FullRangeImplementation(manager, fullRange);
vm.etch(address(fullRange), address(impl).code);
deployCodeTo("contracts/hooks/examples/FullRange.sol:FullRange", abi.encode(manager), address(fullRange));

key = createPoolKey(token0, token1);
id = key.toId();
Expand Down
28 changes: 10 additions & 18 deletions test/GeomeanOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {Test} from "forge-std/Test.sol";
import {GetSender} from "./shared/GetSender.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {GeomeanOracle} from "../contracts/hooks/examples/GeomeanOracle.sol";
import {GeomeanOracleImplementation} from "./shared/implementation/GeomeanOracleImplementation.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol";
Expand All @@ -16,6 +15,7 @@ import {PoolModifyLiquidityTest} from "@uniswap/v4-core/src/test/PoolModifyLiqui
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {Oracle} from "../contracts/libraries/Oracle.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {HookTestAddress} from "./utils/HookTestAddress.sol";

contract TestGeomeanOracle is Test, Deployers {
using PoolIdLibrary for PoolKey;
Expand All @@ -24,8 +24,8 @@ contract TestGeomeanOracle is Test, Deployers {

TestERC20 token0;
TestERC20 token1;
GeomeanOracleImplementation geomeanOracle = GeomeanOracleImplementation(
address(
GeomeanOracle geomeanOracle = GeomeanOracle(
HookTestAddress.getHookAddress(
uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.AFTER_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG | Hooks.BEFORE_SWAP_FLAG
Expand All @@ -41,18 +41,10 @@ contract TestGeomeanOracle is Test, Deployers {
token0 = TestERC20(Currency.unwrap(currency0));
token1 = TestERC20(Currency.unwrap(currency1));

vm.record();
GeomeanOracleImplementation impl = new GeomeanOracleImplementation(manager, geomeanOracle);
(, bytes32[] memory writes) = vm.accesses(address(impl));
vm.etch(address(geomeanOracle), address(impl).code);
// for each storage key that was written during the hook implementation, copy the value over
unchecked {
for (uint256 i = 0; i < writes.length; i++) {
bytes32 slot = writes[i];
vm.store(address(geomeanOracle), slot, vm.load(address(impl), slot));
}
}
geomeanOracle.setTime(1);
deployCodeTo(
"contracts/hooks/examples/GeomeanOracle.sol:GeomeanOracle", abi.encode(manager), address(geomeanOracle)
);

key = PoolKey(currency0, currency1, 0, MAX_TICK_SPACING, geomeanOracle);
id = key.toId();

Expand Down Expand Up @@ -139,7 +131,7 @@ contract TestGeomeanOracle is Test, Deployers {

function testBeforeModifyPositionObservation() public {
manager.initialize(key, SQRT_PRICE_2_1, ZERO_BYTES);
geomeanOracle.setTime(3); // advance 2 seconds
skip(2); // advance 2 seconds
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
Expand All @@ -162,7 +154,7 @@ contract TestGeomeanOracle is Test, Deployers {

function testBeforeModifyPositionObservationAndCardinality() public {
manager.initialize(key, SQRT_PRICE_2_1, ZERO_BYTES);
geomeanOracle.setTime(3); // advance 2 seconds
skip(2); // advance 2 seconds
geomeanOracle.increaseCardinalityNext(key, 2);
GeomeanOracle.ObservationState memory observationState = geomeanOracle.getState(key);
assertEq(observationState.index, 0);
Expand Down Expand Up @@ -200,7 +192,7 @@ contract TestGeomeanOracle is Test, Deployers {

function testPermanentLiquidity() public {
manager.initialize(key, SQRT_PRICE_2_1, ZERO_BYTES);
geomeanOracle.setTime(3); // advance 2 seconds
skip(2); // advance 2 seconds
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
Expand Down
17 changes: 4 additions & 13 deletions test/LimitOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {Test} from "forge-std/Test.sol";
import {GetSender} from "./shared/GetSender.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {LimitOrder, Epoch, EpochLibrary} from "../contracts/hooks/examples/LimitOrder.sol";
import {LimitOrderImplementation} from "./shared/implementation/LimitOrderImplementation.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol";
Expand All @@ -16,6 +15,7 @@ import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {HookEnabledSwapRouter} from "./utils/HookEnabledSwapRouter.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {HookTestAddress} from "./utils/HookTestAddress.sol";

contract TestLimitOrder is Test, Deployers {
using PoolIdLibrary for PoolKey;
Expand All @@ -26,7 +26,8 @@ contract TestLimitOrder is Test, Deployers {
HookEnabledSwapRouter router;
TestERC20 token0;
TestERC20 token1;
LimitOrder limitOrder = LimitOrder(address(uint160(Hooks.AFTER_INITIALIZE_FLAG | Hooks.AFTER_SWAP_FLAG)));
LimitOrder limitOrder =
LimitOrder(HookTestAddress.getHookAddress(uint160(Hooks.AFTER_INITIALIZE_FLAG | Hooks.AFTER_SWAP_FLAG)));
PoolId id;

function setUp() public {
Expand All @@ -37,17 +38,7 @@ contract TestLimitOrder is Test, Deployers {
token0 = TestERC20(Currency.unwrap(currency0));
token1 = TestERC20(Currency.unwrap(currency1));

vm.record();
LimitOrderImplementation impl = new LimitOrderImplementation(manager, limitOrder);
(, bytes32[] memory writes) = vm.accesses(address(impl));
vm.etch(address(limitOrder), address(impl).code);
// for each storage key that was written during the hook implementation, copy the value over
unchecked {
for (uint256 i = 0; i < writes.length; i++) {
bytes32 slot = writes[i];
vm.store(address(limitOrder), slot, vm.load(address(impl), slot));
}
}
deployCodeTo("contracts/hooks/examples/LimitOrder.sol:LimitOrder", abi.encode(manager), address(limitOrder));

// key = PoolKey(currency0, currency1, 3000, 60, limitOrder);
(key, id) = initPoolAndAddLiquidity(currency0, currency1, limitOrder, 3000, SQRT_PRICE_1_1, ZERO_BYTES);
Expand Down
20 changes: 7 additions & 13 deletions test/TWAMM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {Vm} from "forge-std/Vm.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";
import {IERC20Minimal} from "@uniswap/v4-core/src/interfaces/external/IERC20Minimal.sol";
import {TWAMMImplementation} from "./shared/implementation/TWAMMImplementation.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
Expand All @@ -20,6 +19,7 @@ import {CurrencyLibrary, Currency} from "@uniswap/v4-core/src/types/Currency.sol
import {TWAMM} from "../contracts/hooks/examples/TWAMM.sol";
import {ITWAMM} from "../contracts/interfaces/ITWAMM.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {HookTestAddress} from "./utils/HookTestAddress.sol";

contract TWAMMTest is Test, Deployers, GasSnapshot {
using PoolIdLibrary for PoolKey;
Expand All @@ -43,8 +43,11 @@ contract TWAMMTest is Test, Deployers, GasSnapshot {
uint256 earningsFactorLast
);

TWAMM twamm =
TWAMM(address(uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG)));
TWAMM twamm = TWAMM(
HookTestAddress.getHookAddress(
uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG)
)
);
address hookAddress;
MockERC20 token0;
MockERC20 token1;
Expand All @@ -58,16 +61,7 @@ contract TWAMMTest is Test, Deployers, GasSnapshot {
token0 = MockERC20(Currency.unwrap(currency0));
token1 = MockERC20(Currency.unwrap(currency1));

TWAMMImplementation impl = new TWAMMImplementation(manager, 10_000, twamm);
(, bytes32[] memory writes) = vm.accesses(address(impl));
vm.etch(address(twamm), address(impl).code);
// for each storage key that was written during the hook implementation, copy the value over
unchecked {
for (uint256 i = 0; i < writes.length; i++) {
bytes32 slot = writes[i];
vm.store(address(twamm), slot, vm.load(address(impl), slot));
}
}
deployCodeTo("contracts/hooks/examples/TWAMM.sol:TWAMM", abi.encode(manager, 10_000), address(twamm));

(poolKey, poolId) = initPool(currency0, currency1, twamm, 3000, SQRT_PRICE_1_1, ZERO_BYTES);

Expand Down
16 changes: 0 additions & 16 deletions test/shared/implementation/FullRangeImplementation.sol

This file was deleted.

26 changes: 0 additions & 26 deletions test/shared/implementation/GeomeanOracleImplementation.sol

This file was deleted.

16 changes: 0 additions & 16 deletions test/shared/implementation/LimitOrderImplementation.sol

This file was deleted.

16 changes: 0 additions & 16 deletions test/shared/implementation/TWAMMImplementation.sol

This file was deleted.

8 changes: 8 additions & 0 deletions test/utils/HookTestAddress.sol
Copy link
Collaborator Author

@saucepoint saucepoint Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer to see this in v4-core's Deployers (or periphery's equivalent) to remove one less import required for devs

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

library HookTestAddress {
function getHookAddress(uint160 flags) internal pure returns (address hookAddress) {
hookAddress = address(uint160(flags) ^ (0x4444 << 144));
}
}
Loading