Skip to content

Commit

Permalink
[Refactor] Prevent stack too deep (#23)
Browse files Browse the repository at this point in the history
* refactor: refactor to V4SettlementParams

* refactor: align BinSwapRotuter with CLSwapRouter code

* refactor: C4ExactOutputSingleParams to V4BinExactOutputSingleParams
  • Loading branch information
ChefMist authored May 20, 2024
1 parent 5fe0d6b commit b0043f9
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137919
138072
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130645
130798
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135458
135611
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131425
131578
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131500
131653
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164334
164395
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143372
143527
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136878
137033
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136965
137120
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169043
169086
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129331
129374
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInput.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
247202
247337
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180236
180361
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactOutput.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
253199
253346
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179807
179932
10 changes: 10 additions & 0 deletions src/interfaces/ISwapRouterBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ interface ISwapRouterBase {
IPoolManager poolManager;
bytes32 parameters;
}

/// @notice V4SettlementParams
/// - payer: Address of the payer
/// - settle: If true, transfer token from `payer` to Vault. If false, must perform the settle elsewhere
/// - take: If true, transfer token from Vault to `recipient`. If false, must perform the take elsewhere.
struct V4SettlementParams {
address payer;
bool settle;
bool take;
}
}
41 changes: 21 additions & 20 deletions src/pool-bin/BinSwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ contract BinSwapRouter is
abi.decode(vault.lock(abi.encode(SwapInfo(SwapType.ExactInput, msg.sender, abi.encode(params)))), (uint256));
}

function exactOutputSingle(V4ExactOutputSingleParams calldata params, uint256 deadline)
function exactOutputSingle(V4BinExactOutputSingleParams calldata params, uint256 deadline)
external
payable
override
Expand All @@ -67,7 +67,7 @@ contract BinSwapRouter is
);
}

function exactOutput(V4ExactOutputParams calldata params, uint256 deadline)
function exactOutput(V4BinExactOutputParams calldata params, uint256 deadline)
external
payable
override
Expand All @@ -82,26 +82,27 @@ contract BinSwapRouter is
function lockAcquired(bytes calldata data) external override vaultOnly returns (bytes memory) {
SwapInfo memory swapInfo = abi.decode(data, (SwapInfo));

if (swapInfo.swapType == SwapType.ExactInputSingle) {
V4BinExactInputSingleParams memory params = abi.decode(swapInfo.params, (V4BinExactInputSingleParams));
uint256 amountOut = _v4BinSwapExactInputSingle(params, swapInfo.msgSender, true, true);
/// @dev By default for SwapRouter, the payer will always be msg.sender and will perform take/settle after the swap.
V4SettlementParams memory settlementParams =
V4SettlementParams({payer: swapInfo.msgSender, settle: true, take: true});

return abi.encode(amountOut);
} else if (swapInfo.swapType == SwapType.ExactInput) {
V4BinExactInputParams memory params = abi.decode(swapInfo.params, (V4BinExactInputParams));
uint256 amountOut = _v4BinSwapExactInput(params, swapInfo.msgSender, true, true);

return abi.encode(amountOut);
} else if (swapInfo.swapType == SwapType.ExactOutputSingle) {
V4ExactOutputSingleParams memory params = abi.decode(swapInfo.params, (V4ExactOutputSingleParams));
uint256 amountIn = _v4BinSwapExactOutputSingle(params, swapInfo.msgSender, true, true);

return abi.encode(amountIn);
if (swapInfo.swapType == SwapType.ExactInput) {
return
abi.encode(_v4BinSwapExactInput(abi.decode(swapInfo.params, (V4BinExactInputParams)), settlementParams));
} else if (swapInfo.swapType == SwapType.ExactInputSingle) {
return abi.encode(
_v4BinSwapExactInputSingle(abi.decode(swapInfo.params, (V4BinExactInputSingleParams)), settlementParams)
);
} else if (swapInfo.swapType == SwapType.ExactOutput) {
V4ExactOutputParams memory params = abi.decode(swapInfo.params, (V4ExactOutputParams));
uint256 amountIn = _v4BinSwapExactOutput(params, swapInfo.msgSender, true, true);

return abi.encode(amountIn);
return abi.encode(
_v4BinSwapExactOutput(abi.decode(swapInfo.params, (V4BinExactOutputParams)), settlementParams)
);
} else if (swapInfo.swapType == SwapType.ExactOutputSingle) {
return abi.encode(
_v4BinSwapExactOutputSingle(
abi.decode(swapInfo.params, (V4BinExactOutputSingleParams)), settlementParams
)
);
} else {
revert InvalidSwapType();
}
Expand Down
53 changes: 25 additions & 28 deletions src/pool-bin/BinSwapRouterBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
}

/// @notice Perform a swap with `amountIn` in and ensure at least `amountOutMinimum` out
/// @param settle If true, transfer token from `msgSender` to Vault. If false, must perform the settle elsewhere
/// @param take If true, transfer token from Vault to `recipient`. If false, must perform the take elsewhere
function _v4BinSwapExactInputSingle(
V4BinExactInputSingleParams memory params,
address msgSender,
bool settle,
bool take
V4SettlementParams memory settlementParams
) internal returns (uint256 amountOut) {
amountOut = uint256(
_swapExactPrivate(
params.poolKey,
params.swapForY,
msgSender,
settlementParams.payer,
params.recipient,
params.amountIn,
settle,
take,
settlementParams.settle,
settlementParams.take,
params.hookData
)
);
Expand All @@ -52,9 +48,7 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
}

/// @notice Perform a swap with `amountIn` in and ensure at least `amountOutMinimum` out
/// @param settle If true, transfer token from `msgSender` to Vault. If false, must perform the settle elsewhere
/// @param take If true, transfer token from Vault to `recipient`. If false, must perform the take elsewhere
function _v4BinSwapExactInput(V4BinExactInputParams memory params, address msgSender, bool settle, bool take)
function _v4BinSwapExactInput(V4BinExactInputParams memory params, V4SettlementParams memory settlementParams)
internal
returns (uint256 amountOut)
{
Expand All @@ -67,11 +61,11 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
state.amountOut = _swapExactPrivate(
state.poolKey,
state.swapForY,
msgSender,
settlementParams.payer,
params.recipient,
params.amountIn,
i == 0 && settle, // only settle at first iteration AND settle = true
i == state.pathLength - 1 && take, // only take at last iteration AND take = true
i == 0 && settlementParams.settle, // only settle at first iteration AND settle = true
i == state.pathLength - 1 && settlementParams.take, // only take at last iteration AND take = true
params.path[i].hookData
);

Expand All @@ -88,20 +82,23 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
}

/// @notice Perform a swap that ensure at least `amountOut` tokens with `amountInMaximum` tokens
/// @param settle If true, transfer token from `msgSender` to Vault. If false, must perform the settle elsewhere
/// @param take If true, transfer token from Vault to `recipient`. If false, must perform the take elsewhere
function _v4BinSwapExactOutputSingle(
V4ExactOutputSingleParams memory params,
address msgSender,
bool settle,
bool take
V4BinExactOutputSingleParams memory params,
V4SettlementParams memory settlementParams
) internal returns (uint256 amountIn) {
(uint128 amtIn,,) = binPoolManager.getSwapIn(params.poolKey, params.swapForY, params.amountOut);

if (amtIn > params.amountInMaximum) revert TooMuchRequested();

uint128 amountOutReal = _swapExactPrivate(
params.poolKey, params.swapForY, msgSender, params.recipient, amtIn, settle, take, params.hookData
params.poolKey,
params.swapForY,
settlementParams.payer,
params.recipient,
amtIn,
settlementParams.settle,
settlementParams.take,
params.hookData
);

if (amountOutReal < params.amountOut) revert TooLittleReceived();
Expand All @@ -118,7 +115,7 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
}

/// @notice Perform a swap that ensure at least `amountOut` tokens with `amountInMaximum` tokens
function _v4BinSwapExactOutput(V4ExactOutputParams memory params, address msgSender, bool settle, bool take)
function _v4BinSwapExactOutput(V4BinExactOutputParams memory params, V4SettlementParams memory settlementParams)
internal
returns (uint256 amountIn)
{
Expand All @@ -135,11 +132,11 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
state.amountOut = _swapExactPrivate(
state.poolKey,
!state.swapForY,
msgSender,
settlementParams.payer,
params.recipient,
state.amountIn,
i == 1 && settle, // only settle at first swap AND settle = true
i == state.pathLength && take, // only take at last iteration AND take = true
i == 1 && settlementParams.settle, // only settle at first swap AND settle = true
i == state.pathLength && settlementParams.take, // only take at last iteration AND take = true
params.path[i - 1].hookData
);

Expand All @@ -163,7 +160,7 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
function _swapExactPrivate(
PoolKey memory poolKey,
bool swapForY,
address msgSender,
address payer,
address recipient,
uint128 amountIn,
bool settle,
Expand All @@ -173,10 +170,10 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
BalanceDelta delta = binPoolManager.swap(poolKey, swapForY, amountIn, hookData);

if (swapForY) {
if (settle) _payAndSettle(poolKey.currency0, msgSender, delta.amount0());
if (settle) _payAndSettle(poolKey.currency0, payer, delta.amount0());
if (take) vault.take(poolKey.currency1, recipient, uint128(-delta.amount1()));
} else {
if (settle) _payAndSettle(poolKey.currency1, msgSender, delta.amount1());
if (settle) _payAndSettle(poolKey.currency1, payer, delta.amount1());
if (take) vault.take(poolKey.currency0, recipient, uint128(-delta.amount0()));
}

Expand Down
4 changes: 2 additions & 2 deletions src/pool-bin/interfaces/IBinSwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ interface IBinSwapRouter is IBinSwapRouterBase {
/// @notice Swaps as little as possible of one token for `amountOut` of another token
/// @param params The parameters necessary for the swap, encoded as `ExactOutputSingleParams` in calldata
/// @return amountIn The amount of the input token
function exactOutputSingle(V4ExactOutputSingleParams calldata params, uint256 deadline)
function exactOutputSingle(V4BinExactOutputSingleParams calldata params, uint256 deadline)
external
payable
returns (uint256 amountIn);

/// @notice Swaps as little as possible of one token for `amountOut` of another along the specified path (reversed)
/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactOutputParams` in calldata
/// @return amountIn The amount of the input token
function exactOutput(V4ExactOutputParams calldata params, uint256 deadline)
function exactOutput(V4BinExactOutputParams calldata params, uint256 deadline)
external
payable
returns (uint256 amountIn);
Expand Down
4 changes: 2 additions & 2 deletions src/pool-bin/interfaces/IBinSwapRouterBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface IBinSwapRouterBase is ISwapRouterBase {
uint128 amountOutMinimum;
}

struct V4ExactOutputSingleParams {
struct V4BinExactOutputSingleParams {
PoolKey poolKey;
bool swapForY;
address recipient;
Expand All @@ -33,7 +33,7 @@ interface IBinSwapRouterBase is ISwapRouterBase {
bytes hookData;
}

struct V4ExactOutputParams {
struct V4BinExactOutputParams {
Currency currencyOut;
PathKey[] path;
address recipient;
Expand Down
24 changes: 10 additions & 14 deletions src/pool-cl/CLSwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,23 @@ contract CLSwapRouter is
function lockAcquired(bytes calldata encodedSwapInfo) external vaultOnly returns (bytes memory) {
SwapInfo memory swapInfo = abi.decode(encodedSwapInfo, (SwapInfo));

/// @dev By default for SwapRouter, the payer will always be msg.sender and will perform take/settle after the swap.
V4SettlementParams memory settlementParams =
V4SettlementParams({payer: swapInfo.msgSender, settle: true, take: true});

if (swapInfo.swapType == SwapType.ExactInput) {
return abi.encode(
_v4CLSwapExactInput(abi.decode(swapInfo.params, (V4CLExactInputParams)), swapInfo.msgSender, true, true)
);
return
abi.encode(_v4CLSwapExactInput(abi.decode(swapInfo.params, (V4CLExactInputParams)), settlementParams));
} else if (swapInfo.swapType == SwapType.ExactInputSingle) {
return abi.encode(
_v4CLSwapExactInputSingle(
abi.decode(swapInfo.params, (V4CLExactInputSingleParams)), swapInfo.msgSender, true, true
)
_v4CLSwapExactInputSingle(abi.decode(swapInfo.params, (V4CLExactInputSingleParams)), settlementParams)
);
} else if (swapInfo.swapType == SwapType.ExactOutput) {
return abi.encode(
_v4CLSwapExactOutput(
abi.decode(swapInfo.params, (V4CLExactOutputParams)), swapInfo.msgSender, true, true
)
);
return
abi.encode(_v4CLSwapExactOutput(abi.decode(swapInfo.params, (V4CLExactOutputParams)), settlementParams));
} else if (swapInfo.swapType == SwapType.ExactOutputSingle) {
return abi.encode(
_v4CLSwapExactOutputSingle(
abi.decode(swapInfo.params, (V4CLExactOutputSingleParams)), swapInfo.msgSender, true, true
)
_v4CLSwapExactOutputSingle(abi.decode(swapInfo.params, (V4CLExactOutputSingleParams)), settlementParams)
);
} else {
revert InvalidSwapType();
Expand Down
Loading

0 comments on commit b0043f9

Please sign in to comment.