Skip to content

Commit

Permalink
replace etch with deployCodeTo cheatcode
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Jun 12, 2024
1 parent 3e7f044 commit 9606448
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 118 deletions.
6 changes: 2 additions & 4 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 Down Expand Up @@ -66,7 +65,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
MockERC20 token1;
MockERC20 token2;

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

Expand All @@ -87,8 +86,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
25 changes: 8 additions & 17 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 @@ -24,7 +23,7 @@ contract TestGeomeanOracle is Test, Deployers {

TestERC20 token0;
TestERC20 token1;
GeomeanOracleImplementation geomeanOracle = GeomeanOracleImplementation(
GeomeanOracle geomeanOracle = GeomeanOracle(
address(
uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.AFTER_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
Expand All @@ -41,18 +40,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 +130,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 +153,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 +191,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
13 changes: 1 addition & 12 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 Down Expand Up @@ -37,17 +36,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
12 changes: 1 addition & 11 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 Down Expand Up @@ -58,16 +57,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.

0 comments on commit 9606448

Please sign in to comment.