Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 3, 2024
2 parents 2de7daf + 65d3aaa commit 9b8ac69
Show file tree
Hide file tree
Showing 61 changed files with 161 additions and 158 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 @@
47167
47320
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 @@
46984
47137
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123815
123968
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123322
123465
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130893
131046
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130400
130544
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withTakePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150653
150641
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116196
116184
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134968
135121
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127708
127860
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
341353
341053
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349845
349545
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349147
348847
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319335
319035
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319977
319677
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
245559
245259
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375377
375077
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325353
325053
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
376653
376353
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375793
375493
Original file line number Diff line number Diff line change
@@ -1 +1 @@
421080
420802
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79506
79492
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62394
62380
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit_twice.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45282
45268
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8591
8596
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
36 changes: 22 additions & 14 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 Down Expand Up @@ -192,6 +192,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 +203,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 +214,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 +225,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 +309,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 +325,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,6 +335,8 @@ contract PositionManager is
(liquidityDelta - feesAccrued).validateMinOut(amount0Min, amount1Min);
}

if (positionConfigs[tokenId].hasSubscriber()) _unsubscribe(tokenId, config);

delete positionConfigs[tokenId];
// Burn the token.
_burn(tokenId);
Expand All @@ -340,8 +349,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
17 changes: 12 additions & 5 deletions src/V4Router.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
pragma solidity 0.8.26;

import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
Expand Down Expand Up @@ -37,46 +37,53 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
if (action == Actions.SWAP_EXACT_IN) {
IV4Router.ExactInputParams calldata swapParams = params.decodeSwapExactInParams();
_swapExactInput(swapParams);
return;
} else if (action == Actions.SWAP_EXACT_IN_SINGLE) {
IV4Router.ExactInputSingleParams calldata swapParams = params.decodeSwapExactInSingleParams();
_swapExactInputSingle(swapParams);
return;
} else if (action == Actions.SWAP_EXACT_OUT) {
IV4Router.ExactOutputParams calldata swapParams = params.decodeSwapExactOutParams();
_swapExactOutput(swapParams);
return;
} else if (action == Actions.SWAP_EXACT_OUT_SINGLE) {
IV4Router.ExactOutputSingleParams calldata swapParams = params.decodeSwapExactOutSingleParams();
_swapExactOutputSingle(swapParams);
} else {
revert UnsupportedAction(action);
return;
}
} else {
if (action == Actions.SETTLE_TAKE_PAIR) {
(Currency settleCurrency, Currency takeCurrency) = params.decodeCurrencyPair();
_settle(settleCurrency, msgSender(), _getFullDebt(settleCurrency));
_take(takeCurrency, msgSender(), _getFullCredit(takeCurrency));
return;
} else if (action == Actions.SETTLE_ALL) {
(Currency currency, uint256 maxAmount) = params.decodeCurrencyAndUint256();
uint256 amount = _getFullDebt(currency);
if (amount > maxAmount) revert V4TooMuchRequested(maxAmount, amount);
_settle(currency, msgSender(), amount);
return;
} else if (action == Actions.TAKE_ALL) {
(Currency currency, uint256 minAmount) = params.decodeCurrencyAndUint256();
uint256 amount = _getFullCredit(currency);
if (amount < minAmount) revert V4TooLittleReceived(minAmount, amount);
_take(currency, msgSender(), amount);
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.TAKE_PORTION) {
(Currency currency, address recipient, uint256 bips) = params.decodeCurrencyAddressAndUint256();
_take(currency, _mapRecipient(recipient), _getFullCredit(currency).calculatePortion(bips));
} else {
revert UnsupportedAction(action);
return;
}
}
revert UnsupportedAction(action);
}

function _swapExactInputSingle(IV4Router.ExactInputSingleParams calldata params) private {
Expand Down
4 changes: 2 additions & 2 deletions src/base/BaseActionsRouter.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {SafeCallback} from "./SafeCallback.sol";
Expand Down Expand Up @@ -40,7 +40,7 @@ abstract contract BaseActionsRouter is SafeCallback {
if (numActions != params.length) revert InputLengthMismatch();

for (uint256 actionIndex = 0; actionIndex < numActions; actionIndex++) {
uint256 action = uint256(uint8(actions[actionIndex]));
uint256 action = uint8(actions[actionIndex]);

_handleAction(action, params[actionIndex]);
}
Expand Down
6 changes: 4 additions & 2 deletions src/base/DeltaResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ abstract contract DeltaResolver is ImmutableState {
return currency.balanceOfSelf();
} else if (amount == ActionConstants.OPEN_DELTA) {
return _getFullDebt(currency);
} else {
return amount;
}
return amount;
}

/// @notice Calculates the amount for a take action
function _mapTakeAmount(uint256 amount, Currency currency) internal view returns (uint256) {
if (amount == ActionConstants.OPEN_DELTA) {
return _getFullCredit(currency);
} else {
return amount;
}
return amount;
}
}
9 changes: 7 additions & 2 deletions src/base/EIP712_v4.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.0;

import {IEIP712_v4} from "../interfaces/IEIP712_v4.sol";

Expand All @@ -26,7 +26,7 @@ contract EIP712_v4 is IEIP712_v4 {
}

/// @inheritdoc IEIP712_v4
function DOMAIN_SEPARATOR() public view override returns (bytes32) {
function DOMAIN_SEPARATOR() public view returns (bytes32) {
// uses cached version if chainid is unchanged from construction
return block.chainid == _CACHED_CHAIN_ID ? _CACHED_DOMAIN_SEPARATOR : _buildDomainSeparator();
}
Expand All @@ -46,6 +46,11 @@ contract EIP712_v4 is IEIP712_v4 {
mstore(add(fmp, 0x02), domainSeparator)
mstore(add(fmp, 0x22), dataHash)
digest := keccak256(fmp, 0x42)

// now clean the memory we used
mstore(fmp, 0) // fmp held "\x19\x01", domainSeparator
mstore(add(fmp, 0x20), 0) // fmp+0x20 held domainSeparator, dataHash
mstore(add(fmp, 0x40), 0) // fmp+0x40 held dataHash
}
}
}
Loading

0 comments on commit 9b8ac69

Please sign in to comment.