Skip to content

Commit

Permalink
try-catch-initialize (#397)
Browse files Browse the repository at this point in the history
* try-catch-initialize

* add test

* fix return value

* snapshot

---------

Co-authored-by: Alice Henshaw <[email protected]>
  • Loading branch information
snreynolds and hensha256 authored Nov 23, 2024
1 parent 1394ff2 commit bf39426
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
455931
455917
8 changes: 7 additions & 1 deletion src/base/PoolInitializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
/// @dev Enables create pool + mint liquidity in a single transaction with multicall
abstract contract PoolInitializer is ImmutableState {
/// @notice Initialize a Uniswap v4 Pool
/// @dev If the pool is already initialized, this function will not revert and just return type(int24).max
/// @param key the PoolKey of the pool to initialize
/// @param sqrtPriceX96 the initial sqrtPriceX96 of the pool
/// @return tick The current tick of the pool
function initializePool(PoolKey calldata key, uint160 sqrtPriceX96) external payable returns (int24) {
return poolManager.initialize(key, sqrtPriceX96);
try poolManager.initialize(key, sqrtPriceX96) returns (int24 tick) {
return tick;
} catch {
return type(int24).max;
}
}
}
61 changes: 41 additions & 20 deletions test/position-managers/PositionManager.multicall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,47 @@ contract PositionManagerMulticallTest is Test, Permit2SignatureHelpers, PosmTest
assertGt(result.amount1(), 0);
}

function test_multicall_initializePool_twice_andMint_succeeds() public {
key = PoolKey({currency0: currency0, currency1: currency1, fee: 0, tickSpacing: 10, hooks: IHooks(address(0))});
manager.initialize(key, SQRT_PRICE_1_1);

// Use multicall to initialize the pool again.
bytes[] memory calls = new bytes[](2);
calls[0] = abi.encodeWithSelector(lpm.initializePool.selector, key, SQRT_PRICE_1_1);

config = PositionConfig({
poolKey: key,
tickLower: TickMath.minUsableTick(key.tickSpacing),
tickUpper: TickMath.maxUsableTick(key.tickSpacing)
});

Plan memory planner = Planner.init();
planner.add(
Actions.MINT_POSITION,
abi.encode(
config.poolKey,
config.tickLower,
config.tickUpper,
100e18,
MAX_SLIPPAGE_INCREASE,
MAX_SLIPPAGE_INCREASE,
ActionConstants.MSG_SENDER,
ZERO_BYTES
)
);
bytes memory actions = planner.finalizeModifyLiquidityWithClose(config.poolKey);

calls[1] = abi.encodeWithSelector(IPositionManager.modifyLiquidities.selector, actions, _deadline);

IMulticall_v4(address(lpm)).multicall(calls);

// test swap, doesn't revert, showing the mint succeeded even after initialize reverted
int256 amountSpecified = -1e18;
BalanceDelta result = swap(key, true, amountSpecified, ZERO_BYTES);
assertEq(result.amount0(), amountSpecified);
assertGt(result.amount1(), 0);
}

function test_multicall_initializePool_mint_native() public {
key = PoolKey({
currency0: CurrencyLibrary.ADDRESS_ZERO,
Expand Down Expand Up @@ -227,26 +268,6 @@ contract PositionManagerMulticallTest is Test, Permit2SignatureHelpers, PosmTest
lpm.multicall(calls);
}

// create a pool where tickSpacing is negative
// core's TickSpacingTooSmall(int24) should bubble up through Multicall
function test_multicall_bubbleRevert_core_args() public {
int24 tickSpacing = -10;
key = PoolKey({
currency0: currency0,
currency1: currency1,
fee: 0,
tickSpacing: tickSpacing,
hooks: IHooks(address(0))
});

// Use multicall to initialize a pool
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(PoolInitializer.initializePool.selector, key, SQRT_PRICE_1_1);

vm.expectRevert(abi.encodeWithSelector(IPoolManager.TickSpacingTooSmall.selector, tickSpacing));
lpm.multicall(calls);
}

function test_multicall_permitAndDecrease() public {
config = PositionConfig({poolKey: key, tickLower: -60, tickUpper: 60});
uint256 liquidityAlice = 1e18;
Expand Down

0 comments on commit bf39426

Please sign in to comment.