From 75c15f70afb7a7bc86b75eafe8a8e80079a4f30c Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:41:41 +0800 Subject: [PATCH] chore: clean up testUtils --- test/pool-bin/utils/BinTestUtils.sol | 24 +++++++++++++++++++----- test/pool-cl/utils/CLTestUtils.sol | 24 +++++++++++++++++++----- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/test/pool-bin/utils/BinTestUtils.sol b/test/pool-bin/utils/BinTestUtils.sol index a05cc59..9f10f1c 100644 --- a/test/pool-bin/utils/BinTestUtils.sol +++ b/test/pool-bin/utils/BinTestUtils.sol @@ -1,8 +1,9 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.24; -import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol"; +import {IERC20} from "forge-std/interfaces/IERC20.sol"; import {Test} from "forge-std/Test.sol"; +import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol"; import {BinPoolManager} from "pancake-v4-core/src/pool-bin/BinPoolManager.sol"; import {Vault} from "pancake-v4-core/src/Vault.sol"; import {Currency} from "pancake-v4-core/src/types/Currency.sol"; @@ -64,11 +65,11 @@ contract BinTestUtils is DeployPermit2 { token0.approve(address(permit2), type(uint256).max); token1.approve(address(permit2), type(uint256).max); - permit2.approve(address(token0), address(positionManager), type(uint160).max, uint48(block.timestamp + 1000)); - permit2.approve(address(token1), address(positionManager), type(uint160).max, uint48(block.timestamp + 1000)); + permit2.approve(address(token0), address(positionManager), type(uint160).max, type(uint48).max); + permit2.approve(address(token1), address(positionManager), type(uint160).max, type(uint48).max); - permit2.approve(address(token0), address(universalRouter), type(uint160).max, uint48(block.timestamp + 1000)); - permit2.approve(address(token1), address(universalRouter), type(uint160).max, uint48(block.timestamp + 1000)); + permit2.approve(address(token0), address(universalRouter), type(uint160).max, type(uint48).max); + permit2.approve(address(token1), address(universalRouter), type(uint160).max, type(uint48).max); return SortTokens.sort(token0, token1); } @@ -147,4 +148,17 @@ contract BinTestUtils is DeployPermit2 { relativeIds[i] = int256(uint256(absoluteIds[i])) - int256(uint256(activeId)); } } + + /// @notice permit2 approve from user addr to contractToApprove for currency + function permit2Approve(address userAddr, Currency currency, address contractToApprove) internal { + vm.startPrank(userAddr); + + // If contractToApprove uses permit2, we must execute 2 permits/approvals. + // 1. First, the caller must approve permit2 on the token. + IERC20(Currency.unwrap(currency)).approve(address(permit2), type(uint256).max); + + // 2. Then, the caller must approve contractToApprove as a spender of permit2. + permit2.approve(Currency.unwrap(currency), address(contractToApprove), type(uint160).max, type(uint48).max); + vm.stopPrank(); + } } diff --git a/test/pool-cl/utils/CLTestUtils.sol b/test/pool-cl/utils/CLTestUtils.sol index 6598b02..aace0f5 100644 --- a/test/pool-cl/utils/CLTestUtils.sol +++ b/test/pool-cl/utils/CLTestUtils.sol @@ -1,8 +1,9 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.24; -import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol"; +import {IERC20} from "forge-std/interfaces/IERC20.sol"; import {Test, console} from "forge-std/Test.sol"; +import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol"; import {CLPoolManager} from "pancake-v4-core/src/pool-cl/CLPoolManager.sol"; import {Vault} from "pancake-v4-core/src/Vault.sol"; import {Currency} from "pancake-v4-core/src/types/Currency.sol"; @@ -67,11 +68,11 @@ contract CLTestUtils is DeployPermit2 { token0.approve(address(permit2), type(uint256).max); token1.approve(address(permit2), type(uint256).max); - permit2.approve(address(token0), address(positionManager), type(uint160).max, uint48(block.timestamp + 1000)); - permit2.approve(address(token1), address(positionManager), type(uint160).max, uint48(block.timestamp + 1000)); + permit2.approve(address(token0), address(positionManager), type(uint160).max, type(uint48).max); + permit2.approve(address(token1), address(positionManager), type(uint160).max, type(uint48).max); - permit2.approve(address(token0), address(universalRouter), type(uint160).max, uint48(block.timestamp + 1000)); - permit2.approve(address(token1), address(universalRouter), type(uint160).max, uint48(block.timestamp + 1000)); + permit2.approve(address(token0), address(universalRouter), type(uint160).max, type(uint48).max); + permit2.approve(address(token1), address(universalRouter), type(uint160).max, type(uint48).max); return SortTokens.sort(token0, token1); } @@ -106,4 +107,17 @@ contract CLTestUtils is DeployPermit2 { universalRouter.execute(commands, inputs); } + + /// @notice permit2 approve from user addr to contractToApprove for currency + function permit2Approve(address userAddr, Currency currency, address contractToApprove) internal { + vm.startPrank(userAddr); + + // If contractToApprove uses permit2, we must execute 2 permits/approvals. + // 1. First, the caller must approve permit2 on the token. + IERC20(Currency.unwrap(currency)).approve(address(permit2), type(uint256).max); + + // 2. Then, the caller must approve contractToApprove as a spender of permit2. + permit2.approve(Currency.unwrap(currency), address(contractToApprove), type(uint160).max, type(uint48).max); + vm.stopPrank(); + } }