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

[Part 1] feat: refactor v4 to infinity #77

Merged
merged 14 commits into from
Feb 5, 2025
Merged
Prev Previous commit
Next Next commit
feat: further refactoring of v4 -> infinity
  • Loading branch information
ChefMist committed Jan 28, 2025
commit 75bd2f5f6fe3a2f9739cccbee2673ffe7cbd250d
14 changes: 7 additions & 7 deletions src/MixedQuoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ contract MixedQuoter is IMixedQuoter, IPancakeV3SwapCallback, Multicall_v4 {

/// @dev if withContext is false, each swap is isolated and does not influence the outcome of subsequent swaps within the same pool
/// @dev if withContext is true, all swap results will influence the outcome of subsequent swaps within the same pool
/// @dev if withContext is true, non-v4 pools only support one swap direction for same pool
/// @dev if withContext is true, non-infinity pools (v3, v2, ss) only support one swap direction for same pool
function quoteMixedExactInputWithContext(
address[] calldata paths,
bytes calldata actions,
Expand Down Expand Up @@ -315,7 +315,7 @@ contract MixedQuoter is IMixedQuoter, IPancakeV3SwapCallback, Multicall_v4 {
exactAmount: amountIn.toUint128(),
hookData: clParams.hookData
});
// will execute all swap history of same v4 pool in one transaction if withContext is true
// will execute all swap history of same infinity pool in one transaction if withContext is true
if (withContext) {
bytes32 poolHash = MixedQuoterRecorder.getV4CLPoolHash(clParams.poolKey);
bytes memory swapListBytes = MixedQuoterRecorder.getV4PoolSwapList(poolHash);
Expand Down Expand Up @@ -352,7 +352,7 @@ contract MixedQuoter is IMixedQuoter, IPancakeV3SwapCallback, Multicall_v4 {
exactAmount: amountIn.toUint128(),
hookData: binParams.hookData
});
// will execute all swap history of same v4 pool in one transaction if withContext is true
// will execute all swap history of same infinity pool in one transaction if withContext is true
if (withContext) {
bytes32 poolHash = MixedQuoterRecorder.getV4BinPoolHash(binParams.poolKey);
bytes memory swapListBytes = MixedQuoterRecorder.getV4PoolSwapList(poolHash);
Expand Down Expand Up @@ -450,8 +450,8 @@ contract MixedQuoter is IMixedQuoter, IPancakeV3SwapCallback, Multicall_v4 {
}

/// @notice Convert WETH to native currency for V4 pools
/// @dev for example, quote route are v3 WETH pool[token0, WETH] and v4 native pool[NATIVE,token1]
/// paths is [token0, WETH, token1], we need to convert WETH to NATIVE when quote v4 pool
/// @dev for example, quote route are v3 WETH pool[token0, WETH] and infinity native pool[NATIVE,token1]
/// paths is [token0, WETH, token1], we need to convert WETH to NATIVE when quote infinity pool
function convertWETHToV4NativeCurency(PoolKey memory poolKey, address tokenIn, address tokenOut)
private
view
Expand All @@ -468,8 +468,8 @@ contract MixedQuoter is IMixedQuoter, IPancakeV3SwapCallback, Multicall_v4 {
return (tokenIn, tokenOut);
}

/// @dev Convert native currency to WETH for Non-V4 pools.
/// For example, quote route are v4 native pool[NATIVE, token0] and v3 WETH pool[WETH, token1].
/// @dev Convert native currency to WETH for Non-Infinity pools.
/// For example, quote route are infinity native pool[NATIVE, token0] and v3 WETH pool[WETH, token1].
//// paths is [token0, NATIVE, token1], we need to convert NATIVE to WETH when quote v3 pool
function convertNativeToWETH(address tokenIn, address tokenOut) private view returns (address, address) {
if (Currency.wrap(tokenIn).isNative()) {
Expand Down
6 changes: 3 additions & 3 deletions src/base/BaseActionsRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {SafeCallback} from "./SafeCallback.sol";
import {CalldataDecoder} from "../libraries/CalldataDecoder.sol";
import {ActionConstants} from "../libraries/ActionConstants.sol";

/// @notice Abstract contract for performing a combination of actions on Pancakeswap v4.
/// @notice Abstract contract for performing a combination of actions on Pancakeswap infinity.
/// @dev Suggested uint256 action values are defined in Actions.sol, however any definition can be used
abstract contract BaseActionsRouter is SafeCallback {
using CalldataDecoder for bytes;
Expand All @@ -20,7 +20,7 @@ abstract contract BaseActionsRouter is SafeCallback {

constructor(IVault _vault) SafeCallback(_vault) {}

/// @notice internal function that triggers the execution of a set of actions on v4
/// @notice internal function that triggers the execution of a set of actions on infinity
/// @dev inheriting contracts should call this function to trigger execution
function _executeActions(bytes calldata data) internal {
vault.lock(data);
Expand Down Expand Up @@ -53,7 +53,7 @@ abstract contract BaseActionsRouter is SafeCallback {
/// @notice function that returns address considered executor of the actions
/// @dev The other context functions, _msgData and _msgValue, are not supported by this contract
/// In many contracts this will be the address that calls the initial entry point that calls `_executeActions`
/// `msg.sender` shouldnt be used, as this will be the v4 vault contract that calls `lockAcquired`
/// `msg.sender` shouldnt be used, as this will be the vault contract that calls `lockAcquired`
/// If using ReentrancyLock.sol, this function can return _getLocker()
function msgSender() public view virtual returns (address);

Expand Down
42 changes: 21 additions & 21 deletions src/base/BaseMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ contract BaseMigrator is IBaseMigrator, Permit2Forwarder, Multicall_v4, SelfPerm
/// @notice compare if tokens from v2 pair are the same as token0/token1. Revert with
/// `TOKEN_NOT_MATCH` if tokens does not match
/// @param v2Pair the address of v2 pair
/// @param token0 token0 of v4 poolKey
/// @param token1 token1 of v4 poolKey
/// @return shouldReversePair if the order of tokens from v2 pair is different from v4 pair (only when WETH is involved)
/// @param token0 token0 of infinity poolKey
/// @param token1 token1 of infinity poolKey
/// @return shouldReversePair if the order of tokens from v2 pair is different from infinity pair (only when WETH is involved)
function checkTokensOrderAndMatchFromV2(address v2Pair, Currency token0, Currency token1)
internal
view
Expand All @@ -56,9 +56,9 @@ contract BaseMigrator is IBaseMigrator, Permit2Forwarder, Multicall_v4, SelfPerm
/// `TOKEN_NOT_MATCH` if tokens does not match
/// @param nfp the address of v3#nfp
/// @param tokenId the tokenId of v3 pool
/// @param token0 token0 of v4 poolKey
/// @param token1 token1 of v4 poolKey
/// @return shouldReversePair if the order of tokens from v3 pool is different from v4 pair (only when WETH is involved)
/// @param token0 token0 of infinity poolKey
/// @param token1 token1 of infinity poolKey
/// @return shouldReversePair if the order of tokens from v3 pool is different from infinity pair (only when WETH is involved)
function checkTokensOrderAndMatchFromV3(address nfp, uint256 tokenId, Currency token0, Currency token1)
internal
view
Expand All @@ -71,9 +71,9 @@ contract BaseMigrator is IBaseMigrator, Permit2Forwarder, Multicall_v4, SelfPerm
/// @notice withdraw liquidity from v2 pool (fee will always be included)
/// It may revert if amount0/amount1 received is less than expected
/// @param v2PoolParams the parameters to withdraw liquidity from v2 pool
/// @param shouldReversePair if the order of tokens from v2 pair is different from v4 pair (only when WETH is involved)
/// @return amount0Received the actual amount of token0 received (in order of v4 pool)
/// @return amount1Received the actual amount of token1 received (in order of v4 pool)
/// @param shouldReversePair if the order of tokens from v2 pair is different from infinity pair (only when WETH is involved)
/// @return amount0Received the actual amount of token0 received (in order of infinity pool)
/// @return amount1Received the actual amount of token1 received (in order of infinity pool)
function withdrawLiquidityFromV2(V2PoolParams calldata v2PoolParams, bool shouldReversePair)
internal
returns (uint256 amount0Received, uint256 amount1Received)
Expand All @@ -87,8 +87,8 @@ contract BaseMigrator is IBaseMigrator, Permit2Forwarder, Multicall_v4, SelfPerm
revert INSUFFICIENT_AMOUNTS_RECEIVED();
}

/// @notice the order may mismatch with v4 pool when WETH is invovled
/// the following check makes sure that the output always match the order of v4 pool
/// @notice the order may mismatch with infinity pool when WETH is invovled
/// the following check makes sure that the output always match the order of infinity pool
if (shouldReversePair) {
(amount0Received, amount1Received) = (amount1Received, amount0Received);
}
Expand All @@ -97,9 +97,9 @@ contract BaseMigrator is IBaseMigrator, Permit2Forwarder, Multicall_v4, SelfPerm
/// @notice withdraw liquidity from v3 pool and collect fee if specified in `v3PoolParams`
/// It may revert if the caller is not the owner of the token or amount0/amount1 received is less than expected
/// @param v3PoolParams the parameters to withdraw liquidity from v3 pool
/// @param shouldReversePair if the order of tokens from v3 pool is different from v4 pair (only when WETH is involved)
/// @return amount0Received the actual amount of token0 received (in order of v4 pool)
/// @return amount1Received the actual amount of token1 received (in order of v4 pool)
/// @param shouldReversePair if the order of tokens from v3 pool is different from infinity pair (only when WETH is involved)
/// @return amount0Received the actual amount of token0 received (in order of infinity pool)
/// @return amount1Received the actual amount of token1 received (in order of infinity pool)
function withdrawLiquidityFromV3(V3PoolParams calldata v3PoolParams, bool shouldReversePair)
internal
returns (uint256 amount0Received, uint256 amount1Received)
Expand Down Expand Up @@ -132,8 +132,8 @@ contract BaseMigrator is IBaseMigrator, Permit2Forwarder, Multicall_v4, SelfPerm
});
(amount0Received, amount1Received) = nfp.collect(collectParams);

/// @notice the order may mismatch with v4 pool when WETH is invovled
/// the following check makes sure that the output always match the order of v4 pool
/// @notice the order may mismatch with infinity pool when WETH is invovled
/// the following check makes sure that the output always match the order of infinity pool
if (shouldReversePair) {
(amount0Received, amount1Received) = (amount1Received, amount0Received);
}
Expand Down Expand Up @@ -182,13 +182,13 @@ contract BaseMigrator is IBaseMigrator, Permit2Forwarder, Multicall_v4, SelfPerm
positionManagerPermit2.approve(Currency.unwrap(currency), to, type(uint160).max, type(uint48).max);
}

/// @notice Check and revert if tokens from both v2/v3 and v4 pair does not match
/// Return true if match but v2v3Token1 is WETH which should be ETH in v4 pair
/// @notice Check and revert if tokens from both v2/v3 and infinity pair does not match
/// Return true if match but v2v3Token1 is WETH which should be ETH in infinity pair
/// @param v2v3Token0 token0 from v2/v3 pair
/// @param v2v3Token1 token1 from v2/v3 pair
/// @param v4Token0 token0 from v4 pair
/// @param v4Token1 token1 from v4 pair
/// @return shouldReversePair if the order of tokens from v2/v3 pair is different from v4 pair (only when WETH is involved)
/// @param v4Token0 token0 from infinity pair
/// @param v4Token1 token1 from infinity pair
/// @return shouldReversePair if the order of tokens from v2/v3 pair is different from infinity pair (only when WETH is involved)
function _checkIfTokenPairMatchAndOrder(
address v2v3Token0,
address v2v3Token1,
Expand Down
2 changes: 1 addition & 1 deletion src/base/SafeCallback.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {IVault} from "infinity-core/src/interfaces/IVault.sol";
import {ImmutableState} from "./ImmutableState.sol";

/// @title Safe Callback
/// @notice A contract that only allows the Pancake v4 Vault to call the lockAcquired function
/// @notice A contract that only allows the PCS Infinity Vault to call the lockAcquired function
abstract contract SafeCallback is ImmutableState, ILockCallback {
/// @notice Thrown when calling lockAcquired where the caller is not the Vault
error NotVault();
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IImmutableState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import {IVault} from "infinity-core/src/interfaces/IVault.sol";
/// @title IImmutableState
/// @notice Interface for the ImmutableState contract
interface IImmutableState {
/// @notice The Pancakeswap v4 Vault contract
/// @notice The Pancakeswap Infinity Vault contract
function vault() external view returns (IVault);
}
2 changes: 1 addition & 1 deletion src/interfaces/IMixedQuoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.24;
import {PoolKey} from "infinity-core/src/types/PoolKey.sol";

/// @title MixedQuoter Interface
/// @notice Supports quoting the calculated amounts for exact input swaps. Is specialized for routes containing a mix of Stable, V2, V3 liquidity, v4 liquidity.
/// @notice Supports quoting the calculated amounts for exact input swaps. Is specialized for routes containing a mix of Stable, V2, V3 liquidity, infinity liquidity.
/// @notice For each pool also tells you the number of initialized ticks crossed and the sqrt price of the pool after the swap.
/// @dev These functions are not marked view because they rely on calling non-view functions and reverting
/// to compute the result. They are also not gas efficient and should not be called on-chain.
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/ISelfPermitERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface ISelfPermitERC721 {
/// @notice Permits this contract to spend a given token from `msg.sender`
/// @dev The `owner` is always msg.sender and the `spender` is always address(this).
/// Please always use selfPermitERC721IfNecessary if possible prevent calls from failing due to a frontrun of a call to #selfPermitERC721.
/// For details check https://github.com/pancakeswap/pancake-v4-periphery/pull/62#discussion_r1675410282
/// For details check https://github.com/pancakeswap/infinity-periphery/pull/62#discussion_r1675410282
/// @param token The address of the token spent
/// @param tokenId The token ID of the token spent
/// @param deadline A timestamp, the current blocktime must be less than or equal to this timestamp
Expand Down
14 changes: 7 additions & 7 deletions src/libraries/MixedQuoterRecorder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity 0.8.26;

import {PoolKey} from "infinity-core/src/types/PoolKey.sol";

/// @dev Record all token accumulation and swap direction of the transaction for non-v4 pools.
/// @dev Record v4 swap history list for v4 pools.
/// @dev Record all token accumulation and swap direction of the transaction for non-infinity pools.
/// @dev Record infinity swap history list for infinity pools.
library MixedQuoterRecorder {
/// @dev uint256 internal constant SWAP_DIRECTION = uint256(keccak256("MIXED_QUOTER_SWAP_DIRECTION")) - 1;
uint256 internal constant SWAP_DIRECTION = 0x420071594cddc2905acbd674683749db4c139d373cc290ba8d49c75296a9f1f9;
Expand Down Expand Up @@ -45,7 +45,7 @@ library MixedQuoterRecorder {
error INVALID_SWAP_DIRECTION();

/// @dev Record and check the swap direction of the transaction.
/// @dev Only support one direction for same non-v4 pool in one transaction.
/// @dev Only support one direction for same non-infinity pool in one transaction.
/// @param poolHash The hash of the pool.
/// @param isZeroForOne The direction of the swap.
function setAndCheckSwapDirection(bytes32 poolHash, bool isZeroForOne) internal {
Expand Down Expand Up @@ -122,7 +122,7 @@ library MixedQuoterRecorder {
}
}

/// @dev Record the swap history list of the v4 pool.
/// @dev Record the swap history list of infinity pool.
/// @param poolHash The hash of the pool.
/// @param swapListBytes The swap history list bytes.
function setV4PoolSwapList(bytes32 poolHash, bytes memory swapListBytes) internal {
Expand All @@ -139,7 +139,7 @@ library MixedQuoterRecorder {
}
}

/// @dev Get the swap history list of the v4 pool.
/// @dev Get the swap history list of infinity pool.
/// @param poolHash The hash of the pool.
/// @return swapListBytes The swap history list bytes.
function getV4PoolSwapList(bytes32 poolHash) internal view returns (bytes memory swapListBytes) {
Expand Down Expand Up @@ -187,14 +187,14 @@ library MixedQuoterRecorder {
return keccak256(abi.encode(token0, token1, fee, SWAP_V3));
}

/// @dev Get the v4 cl pool hash.
/// @dev Get the infinity cl pool hash.
/// @param key The pool key.
/// @return poolHash The hash of the pool.
function getV4CLPoolHash(PoolKey memory key) internal pure returns (bytes32) {
return keccak256(abi.encode(key, SWAP_V4_CL));
}

/// @dev Get the v4 bin pool hash.
/// @dev Get the infinity bin pool hash.
/// @param key The pool key.
/// @return poolHash The hash of the pool.
function getV4BinPoolHash(PoolKey memory key) internal pure returns (bytes32) {
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Planner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Plan {

using Planner for Plan global;

/// @notice Constructs a plan of actions to be executed on Pancakeswap v4.
/// @notice Constructs a plan of actions to be executed on Pancakeswap infinity.
library Planner {
function init() internal pure returns (Plan memory plan) {
return Plan({actions: bytes(""), params: new bytes[](0)});
Expand Down
4 changes: 2 additions & 2 deletions src/pool-bin/BinPositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {NativeWrapper} from "../base/NativeWrapper.sol";
import {IWETH9} from "../interfaces/external/IWETH9.sol";

/// @title BinPositionManager
/// @notice Contract for modifying liquidity for PCS v4 Bin pools
/// @notice Contract for modifying liquidity for PCS infinity Bin pools
contract BinPositionManager is
IBinPositionManager,
BinFungibleToken,
Expand Down Expand Up @@ -278,7 +278,7 @@ contract BinPositionManager is
hookData
);

/// Slippage checks, similar to CL type. However, this is different from TJ, in PCS v4,
/// Slippage checks, similar to CL type. However, this is different from TJ. In PCS infinity,
/// as hooks can impact delta (take extra token), user need to be protected with amountMax instead
delta.validateMaxIn(amount0Max, amount1Max);

Expand Down
8 changes: 4 additions & 4 deletions src/pool-bin/interfaces/IBinMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ interface IBinMigrator is IBaseMigrator {
bytes hookData;
}

/// @notice Migrate liquidity from v2 to v4
/// @notice Migrate liquidity from v2 to infinity
/// @param v2PoolParams ncessary info for removing liqudity the source v2 pool
/// @param v4PoolParams necessary info for adding liquidity the target v4 bin-pool
/// @param v4PoolParams necessary info for adding liquidity the target infinity bin-pool
/// @param extraAmount0 the extra amount of token0 that user wants to add (optional, usually 0)
/// if pool token0 is ETH and msg.value == 0, WETH will be taken from sender.
/// Otherwise if pool token0 is ETH and msg.value !=0, method will assume user have sent extraAmount0 in msg.value
Expand All @@ -38,9 +38,9 @@ interface IBinMigrator is IBaseMigrator {
uint256 extraAmount1
) external payable;

/// @notice Migrate liquidity from v3 to v4
/// @notice Migrate liquidity from v3 to infinity
/// @param v3PoolParams ncessary info for removing liqudity the source v3 pool
/// @param v4PoolParams necessary info for adding liquidity the target v4 bin-pool
/// @param v4PoolParams necessary info for adding liquidity the target infinity bin-pool
/// @param extraAmount0 the extra amount of token0 that user wants to add (optional, usually 0)
/// if pool token0 is ETH and msg.value == 0, WETH will be taken from sender.
/// Otherwise if pool token0 is ETH and msg.value !=0, method will assume user have sent extraAmount0 in msg.value
Expand Down
2 changes: 1 addition & 1 deletion src/pool-bin/interfaces/IBinPositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface IBinPositionManager is IPositionManager {

function binPoolManager() external view returns (IBinPoolManager);

/// @notice Initialize a v4 PCS bin pool
/// @notice Initialize a infinity PCS bin pool
/// @dev If the pool is already initialized, this function will not revert
/// @param key the PoolKey of the pool to initialize
/// @param activeId the active bin id of the pool
Expand Down
Loading