Skip to content

Commit

Permalink
Merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 4, 2024
2 parents fd3ce50 + 255b20e commit e8cd67f
Show file tree
Hide file tree
Showing 87 changed files with 797 additions and 292 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47359
47468
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47176
47286
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124007
124115
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123514
123612
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131085
131193
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130592
130691
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142673
142671
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151521
151519
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151521
151519
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withTakePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150893
150879
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109613
109612
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117064
117062
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116436
116422
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135112
135220
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127852
127960
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129780
129778
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_take_take.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117597
117595
Original file line number Diff line number Diff line change
@@ -1 +1 @@
155771
155744
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154773
154746
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137571
137544
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133916
133889
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174832
174805
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144788
144761
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
341593
341266
Original file line number Diff line number Diff line change
@@ -1 +1 @@
350085
349758
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349387
349060
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319575
319248
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
320217
319890
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
245799
245472
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375617
375290
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325593
325266
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
376893
376566
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
376033
375706
Original file line number Diff line number Diff line change
@@ -1 +1 @@
421320
420993
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79595
79470
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62483
62358
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit_twice.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45371
45246
1 change: 1 addition & 0 deletions .forge-snapshots/PositionManager_subscribe.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
88474
1 change: 1 addition & 0 deletions .forge-snapshots/PositionManager_unsubscribe.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
62639
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8566
8773
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
version: nightly

- name: Run tests
run: forge test -vvv
run: forge test --isolate -vvv
env:
FOUNDRY_PROFILE: ci
FORGE_SNAPSHOT_CHECK: true
9 changes: 7 additions & 2 deletions script/DeployPosm.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol"
contract DeployPosmTest is Script {
function setUp() public {}

function run(address poolManager, address permit2) public returns (PositionManager posm) {
function run(address poolManager, address permit2, uint256 unsubscribeGasLimit)
public
returns (PositionManager posm)
{
vm.startBroadcast();

posm = new PositionManager{salt: hex"03"}(IPoolManager(poolManager), IAllowanceTransfer(permit2));
posm = new PositionManager{salt: hex"03"}(
IPoolManager(poolManager), IAllowanceTransfer(permit2), unsubscribeGasLimit
);
console2.log("PositionManager", address(posm));

vm.stopBroadcast();
Expand Down
48 changes: 29 additions & 19 deletions src/PositionManager.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
pragma solidity 0.8.26;

import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
Expand All @@ -24,7 +24,7 @@ import {Actions} from "./libraries/Actions.sol";
import {Notifier} from "./base/Notifier.sol";
import {CalldataDecoder} from "./libraries/CalldataDecoder.sol";
import {Permit2Forwarder} from "./base/Permit2Forwarder.sol";
import {SlippageCheckLibrary} from "./libraries/SlippageCheck.sol";
import {SlippageCheck} from "./libraries/SlippageCheck.sol";
import {PositionConfigId, PositionConfigIdLibrary} from "./libraries/PositionConfigId.sol";

// 444444444
Expand Down Expand Up @@ -111,7 +111,7 @@ contract PositionManager is
using SafeCast for uint256;
using SafeCast for int256;
using CalldataDecoder for bytes;
using SlippageCheckLibrary for BalanceDelta;
using SlippageCheck for BalanceDelta;
using PositionConfigIdLibrary for PositionConfigId;

/// @inheritdoc IPositionManager
Expand All @@ -125,16 +125,17 @@ contract PositionManager is
return positionConfigs[tokenId];
}

constructor(IPoolManager _poolManager, IAllowanceTransfer _permit2)
constructor(IPoolManager _poolManager, IAllowanceTransfer _permit2, uint256 _unsubscribeGasLimit)
BaseActionsRouter(_poolManager)
Permit2Forwarder(_permit2)
ERC721Permit_v4("Uniswap V4 Positions NFT", "UNI-V4-POSM")
Notifier(_unsubscribeGasLimit)
{}

/// @notice Reverts if the deadline has passed
/// @param deadline The timestamp at which the call is no longer valid, passed in by the caller
modifier checkDeadline(uint256 deadline) {
if (block.timestamp > deadline) revert DeadlinePassed();
if (block.timestamp > deadline) revert DeadlinePassed(deadline);
_;
}

Expand Down Expand Up @@ -192,6 +193,7 @@ contract PositionManager is
bytes calldata hookData
) = params.decodeModifyLiquidityParams();
_increase(tokenId, config, liquidity, amount0Max, amount1Max, hookData);
return;
} else if (action == Actions.DECREASE_LIQUIDITY) {
(
uint256 tokenId,
Expand All @@ -202,6 +204,7 @@ contract PositionManager is
bytes calldata hookData
) = params.decodeModifyLiquidityParams();
_decrease(tokenId, config, liquidity, amount0Min, amount1Min, hookData);
return;
} else if (action == Actions.MINT_POSITION) {
(
PositionConfig calldata config,
Expand All @@ -212,6 +215,7 @@ contract PositionManager is
bytes calldata hookData
) = params.decodeMintParams();
_mint(config, liquidity, amount0Max, amount1Max, _mapRecipient(owner), hookData);
return;
} else if (action == Actions.BURN_POSITION) {
// Will automatically decrease liquidity to 0 if the position is not already empty.
(
Expand All @@ -222,35 +226,40 @@ contract PositionManager is
bytes calldata hookData
) = params.decodeBurnParams();
_burn(tokenId, config, amount0Min, amount1Min, hookData);
} else {
revert UnsupportedAction(action);
return;
}
} else {
if (action == Actions.SETTLE_PAIR) {
(Currency currency0, Currency currency1) = params.decodeCurrencyPair();
_settlePair(currency0, currency1);
return;
} else if (action == Actions.TAKE_PAIR) {
(Currency currency0, Currency currency1, address to) = params.decodeCurrencyPairAndAddress();
_takePair(currency0, currency1, to);
(Currency currency0, Currency currency1, address recipient) = params.decodeCurrencyPairAndAddress();
_takePair(currency0, currency1, _mapRecipient(recipient));
return;
} else if (action == Actions.SETTLE) {
(Currency currency, uint256 amount, bool payerIsUser) = params.decodeCurrencyUint256AndBool();
_settle(currency, _mapPayer(payerIsUser), _mapSettleAmount(amount, currency));
return;
} else if (action == Actions.TAKE) {
(Currency currency, address recipient, uint256 amount) = params.decodeCurrencyAddressAndUint256();
_take(currency, _mapRecipient(recipient), _mapTakeAmount(amount, currency));
return;
} else if (action == Actions.CLOSE_CURRENCY) {
Currency currency = params.decodeCurrency();
_close(currency);
return;
} else if (action == Actions.CLEAR_OR_TAKE) {
(Currency currency, uint256 amountMax) = params.decodeCurrencyAndUint256();
_clearOrTake(currency, amountMax);
return;
} else if (action == Actions.SWEEP) {
(Currency currency, address to) = params.decodeCurrencyAndAddress();
_sweep(currency, _mapRecipient(to));
} else {
revert UnsupportedAction(action);
return;
}
}
revert UnsupportedAction(action);
}

/// @dev Calling increase with 0 liquidity will credit the caller with any underlying fees of the position
Expand Down Expand Up @@ -301,10 +310,9 @@ contract PositionManager is
}
_mint(owner, tokenId);

(BalanceDelta liquidityDelta, BalanceDelta feesAccrued) =
_modifyLiquidity(config, liquidity.toInt256(), bytes32(tokenId), hookData);
// Slippage checks should be done on the principal liquidityDelta which is the liquidityDelta - feesAccrued
(liquidityDelta - feesAccrued).validateMaxIn(amount0Max, amount1Max);
// fee delta can be ignored as this is a new position
(BalanceDelta liquidityDelta,) = _modifyLiquidity(config, liquidity.toInt256(), bytes32(tokenId), hookData);
liquidityDelta.validateMaxIn(amount0Max, amount1Max);
positionConfigs[tokenId].setConfigId(config.toId());

emit MintPosition(tokenId, config);
Expand All @@ -318,7 +326,7 @@ contract PositionManager is
uint128 amount1Min,
bytes calldata hookData
) internal onlyIfApproved(msgSender(), tokenId) onlyValidConfig(tokenId, config) {
uint256 liquidity = uint256(getPositionLiquidity(tokenId, config));
uint256 liquidity = getPositionLiquidity(tokenId, config);

// Can only call modify if there is non zero liquidity.
if (liquidity > 0) {
Expand All @@ -328,9 +336,13 @@ contract PositionManager is
(liquidityDelta - feesAccrued).validateMinOut(amount0Min, amount1Min);
}

bool hasSubscriber = positionConfigs[tokenId].hasSubscriber();

delete positionConfigs[tokenId];
// Burn the token.
_burn(tokenId);

if (hasSubscriber) _unsubscribe(tokenId, config);
}

function _settlePair(Currency currency0, Currency currency1) internal {
Expand All @@ -340,8 +352,7 @@ contract PositionManager is
_settle(currency1, caller, _getFullDebt(currency1));
}

function _takePair(Currency currency0, Currency currency1, address to) internal {
address recipient = _mapRecipient(to);
function _takePair(Currency currency0, Currency currency1, address recipient) internal {
_take(currency0, recipient, _getFullCredit(currency0));
_take(currency1, recipient, _getFullCredit(currency1));
}
Expand Down Expand Up @@ -404,7 +415,6 @@ contract PositionManager is
// implementation of abstract function DeltaResolver._pay
function _pay(Currency currency, address payer, uint256 amount) internal override {
if (payer == address(this)) {
// TODO: currency is guaranteed to not be eth so the native check in transfer is not optimal.
currency.transfer(address(poolManager), amount);
} else {
permit2.transferFrom(payer, address(poolManager), uint160(amount), Currency.unwrap(currency));
Expand Down
Loading

0 comments on commit e8cd67f

Please sign in to comment.