Skip to content

Commit

Permalink
chore: clean up testUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist committed Sep 5, 2024
1 parent 0d4c51a commit 75c15f7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
24 changes: 19 additions & 5 deletions test/pool-bin/utils/BinTestUtils.sol
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
}
24 changes: 19 additions & 5 deletions test/pool-cl/utils/CLTestUtils.sol
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 75c15f7

Please sign in to comment.