Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
dianakocsis committed Aug 2, 2024
2 parents ca44df3 + 30863cc commit d226241
Show file tree
Hide file tree
Showing 70 changed files with 1,046 additions and 204 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BaseActionsRouter_mock10commands.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61756
61778

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
134509
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
134647

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127656
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127794
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
336712
336663
1 change: 1 addition & 0 deletions .forge-snapshots/PositionManager_mint_nativeWithSweep.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
345146
Original file line number Diff line number Diff line change
@@ -1 +1 @@
345267
345169
Original file line number Diff line number Diff line change
@@ -1 +1 @@
344532
344710
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
314694
314645
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
315336
315287
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
240918
240869
Original file line number Diff line number Diff line change
@@ -1 +1 @@
370064
370969
Original file line number Diff line number Diff line change
@@ -1 +1 @@
320712
320663
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
372012
371963
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
371253
371342
Original file line number Diff line number Diff line change
@@ -1 +1 @@
416388
416316
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6942
7995
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120501
120558
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119696
119753
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128568
128625
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135398
135455
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186897
186962
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178832
178897
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238421
238494
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
230380
230453
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134171
134509
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119274
119612
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118447
118790
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126298
126261
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120531
120494
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129403
129366
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134204
134167
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186306
186277
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
183201
183172
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238448
238427
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
235367
235346
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229600
229579
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132975
132930
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125069
125024
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119360
119315
27 changes: 10 additions & 17 deletions src/PositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {PositionConfig, PositionConfigLibrary} from "./libraries/PositionConfig.
import {BaseActionsRouter} from "./base/BaseActionsRouter.sol";
import {Actions} from "./libraries/Actions.sol";
import {CalldataDecoder} from "./libraries/CalldataDecoder.sol";
import {Permit2Forwarder} from "./base/Permit2Forwarder.sol";
import {SlippageCheckLibrary} from "./libraries/SlippageCheck.sol";

contract PositionManager is
Expand All @@ -34,7 +35,8 @@ contract PositionManager is
Multicall,
DeltaResolver,
ReentrancyLock,
BaseActionsRouter
BaseActionsRouter,
Permit2Forwarder
{
using SafeTransferLib for *;
using CurrencyLibrary for Currency;
Expand All @@ -52,14 +54,11 @@ contract PositionManager is
/// @inheritdoc IPositionManager
mapping(uint256 tokenId => bytes32 configId) public positionConfigs;

IAllowanceTransfer public immutable permit2;

constructor(IPoolManager _poolManager, IAllowanceTransfer _permit2)
BaseActionsRouter(_poolManager)
Permit2Forwarder(_permit2)
ERC721Permit("Uniswap V4 Positions NFT", "UNI-V4-POSM", "1")
{
permit2 = _permit2;
}
{}

modifier checkDeadline(uint256 deadline) {
if (block.timestamp > deadline) revert DeadlinePassed();
Expand Down Expand Up @@ -107,7 +106,7 @@ contract PositionManager is
address owner,
bytes calldata hookData
) = params.decodeMintParams();
_mint(config, liquidity, amount0Max, amount1Max, owner, hookData);
_mint(config, liquidity, amount0Max, amount1Max, _mapRecipient(owner), hookData);
} else if (action == Actions.CLOSE_CURRENCY) {
Currency currency = params.decodeCurrency();
_close(currency);
Expand All @@ -124,15 +123,15 @@ contract PositionManager is
bytes calldata hookData
) = params.decodeBurnParams();
_burn(tokenId, config, amount0Min, amount1Min, hookData);
} else if (action == Actions.SETTLE_WITH_BALANCE) {
Currency currency = params.decodeCurrency();
_settleWithBalance(currency);
} else if (action == Actions.SETTLE) {
(Currency currency, uint256 amount, bool payerIsUser) = params.decodeCurrencyUint256AndBool();
_settle(currency, _mapPayer(payerIsUser), _mapSettleAmount(amount, currency));
} else if (action == Actions.SETTLE_PAIR) {
(Currency currency0, Currency currency1) = params.decodeCurrencyPair();
_settlePair(currency0, currency1);
} else if (action == Actions.SWEEP) {
(Currency currency, address to) = params.decodeCurrencyAndAddress();
_sweep(currency, to);
_sweep(currency, _mapRecipient(to));
} else {
revert UnsupportedAction(action);
}
Expand Down Expand Up @@ -218,12 +217,6 @@ contract PositionManager is
poolManager.clear(currency, uint256(currencyDelta));
}

/// @dev uses this addresses balance to settle a negative delta
function _settleWithBalance(Currency currency) internal {
// set the payer to this address, performs a transfer.
_settle(currency, address(this), _getFullSettleAmount(currency));
}

function _settlePair(Currency currency0, Currency currency1) internal {
// the locker is the payer when settling
address caller = _msgSender();
Expand Down
30 changes: 16 additions & 14 deletions src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";

import {PathKey, PathKeyLib} from "./libraries/PathKey.sol";
import {CalldataDecoder} from "./libraries/CalldataDecoder.sol";
import {BipsLibrary} from "./libraries/BipsLibrary.sol";
import {IV4Router} from "./interfaces/IV4Router.sol";
import {BaseActionsRouter} from "./base/BaseActionsRouter.sol";
import {DeltaResolver} from "./base/DeltaResolver.sol";
Expand All @@ -21,15 +23,17 @@ import {SafeCastTemp} from "./libraries/SafeCast.sol";
/// An inheriting contract should call _executeActions at the point that they wish actions to be executed
abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
using SafeCastTemp for *;
using SafeCast for *;
using PathKeyLib for PathKey;
using CalldataDecoder for bytes;
using BipsLibrary for uint256;

constructor(IPoolManager _poolManager) BaseActionsRouter(_poolManager) {}

// TODO native support !!
function _handleAction(uint256 action, bytes calldata params) internal override {
// swap actions and payment actions in different blocks for gas efficiency
if (action < Actions.SETTLE) {
if (action < Actions.SETTLE_ALL) {
if (action == Actions.SWAP_EXACT_IN) {
IV4Router.ExactInputParams calldata swapParams = params.decodeSwapExactInParams();
_swapExactInput(swapParams);
Expand All @@ -50,29 +54,27 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
Currency currency = params.decodeCurrency();
// TODO should it have a maxAmountOut added slippage protection?
_settle(currency, _msgSender(), _getFullSettleAmount(currency));
} else if (action == Actions.SETTLE_WITH_BALANCE) {
Currency currency = params.decodeCurrency();
_settle(currency, address(this), _getFullSettleAmount(currency));
} else if (action == Actions.SETTLE) {
(Currency currency, uint256 amount, bool payerIsUser) = params.decodeCurrencyUint256AndBool();
_settle(currency, _mapPayer(payerIsUser), _mapSettleAmount(amount, currency));
} else if (action == Actions.TAKE_ALL) {
(Currency currency, address recipient) = params.decodeCurrencyAndAddress();
uint256 amount = _getFullTakeAmount(currency);

// TODO should _take have a minAmountOut added slippage check?
// TODO recipient mapping
_take(currency, recipient, amount);
_take(currency, _mapRecipient(recipient), amount);
} else if (action == Actions.TAKE_PORTION) {
(Currency currency, address recipient, uint256 bips) = params.decodeCurrencyAddressAndUint256();
_take(currency, _mapRecipient(recipient), _getFullTakeAmount(currency).calculatePortion(bips));
} else {
revert UnsupportedAction(action);
}
}
}

function _swapExactInputSingle(IV4Router.ExactInputSingleParams calldata params) private {
uint128 amountIn =
_mapInputAmount(params.amountIn, params.zeroForOne ? params.poolKey.currency0 : params.poolKey.currency1);
uint128 amountOut = _swap(
params.poolKey,
params.zeroForOne,
int256(-int128(params.amountIn)),
params.sqrtPriceLimitX96,
params.hookData
params.poolKey, params.zeroForOne, int256(-int128(amountIn)), params.sqrtPriceLimitX96, params.hookData
).toUint128();
if (amountOut < params.amountOutMinimum) revert TooLittleReceived();
}
Expand All @@ -82,8 +84,8 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
// Caching for gas savings
uint256 pathLength = params.path.length;
uint128 amountOut;
uint128 amountIn = params.amountIn;
Currency currencyIn = params.currencyIn;
uint128 amountIn = _mapInputAmount(params.amountIn, currencyIn);
PathKey calldata pathKey;

for (uint256 i = 0; i < pathLength; i++) {
Expand Down
18 changes: 18 additions & 0 deletions src/base/BaseActionsRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity ^0.8.24;
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {SafeCallback} from "./SafeCallback.sol";
import {CalldataDecoder} from "../libraries/CalldataDecoder.sol";
import {Actions} from "../libraries/Actions.sol";
import {Constants} from "../libraries/Constants.sol";

/// @notice Abstract contract for performing a combination of actions on Uniswap v4.
/// @dev Suggested uint256 action values are defined in Actions.sol, however any definition can be used
Expand Down Expand Up @@ -52,4 +54,20 @@ abstract contract BaseActionsRouter is SafeCallback {
/// `msg.sender` shouldnt be used, as this will be the v4 pool manager contract that calls `unlockCallback`
/// If using ReentrancyLock.sol, this function can return _getLocker()
function _msgSender() internal view virtual returns (address);

/// @notice Calculates the address for a action
function _mapRecipient(address recipient) internal view returns (address) {
if (recipient == Constants.MSG_SENDER) {
return _msgSender();
} else if (recipient == Constants.ADDRESS_THIS) {
return address(this);
} else {
return recipient;
}
}

/// @notice Calculates the payer for an action
function _mapPayer(bool payerIsUser) internal view returns (address) {
return payerIsUser ? _msgSender() : address(this);
}
}
Loading

0 comments on commit d226241

Please sign in to comment.