Skip to content

Commit

Permalink
ci: update reciprocalAmount logic based on feedback (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist authored Jun 6, 2024
1 parent 60aeb60 commit a916fab
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54399
54579
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52767
52095
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testBurn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26848
26887
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testMint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
67349
67685
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInput.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
255614
255442
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192686
192524
102 changes: 50 additions & 52 deletions src/pool-cl/CLSwapRouterBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ abstract contract CLSwapRouterBase is SwapRouterBase, ICLSwapRouterBase {
V4CLExactInputSingleParams memory params,
V4SettlementParams memory settlementParams
) internal returns (uint256 amountOut) {
amountOut = uint128(
-_swapExactPrivate(
params.poolKey,
params.zeroForOne,
-int256(int128(params.amountIn)),
params.sqrtPriceLimitX96,
settlementParams.payer,
params.recipient,
settlementParams.settle,
settlementParams.take,
params.hookData
)
amountOut = _swapExactPrivate(
params.poolKey,
params.zeroForOne,
-int256(int128(params.amountIn)),
params.sqrtPriceLimitX96,
settlementParams.payer,
params.recipient,
settlementParams.settle,
settlementParams.take,
params.hookData
);
if (amountOut < params.amountOutMinimum) revert TooLittleReceived();
}
Expand All @@ -57,19 +55,16 @@ abstract contract CLSwapRouterBase is SwapRouterBase, ICLSwapRouterBase {

for (uint256 i = 0; i < state.pathLength; i++) {
(state.poolKey, state.zeroForOne) = _getPoolAndSwapDirection(params.path[i], params.currencyIn);

state.amountOut = uint128(
-_swapExactPrivate(
state.poolKey,
state.zeroForOne,
-int256(int128(params.amountIn)),
0,
settlementParams.payer,
params.recipient,
i == 0 && settlementParams.settle,
i == state.pathLength - 1 && settlementParams.take,
params.path[i].hookData
)
state.amountOut = _swapExactPrivate(
state.poolKey,
state.zeroForOne,
-int256(int128(params.amountIn)),
0,
settlementParams.payer,
params.recipient,
i == 0 && settlementParams.settle,
i == state.pathLength - 1 && settlementParams.take,
params.path[i].hookData
);

params.amountIn = state.amountOut;
Expand All @@ -86,18 +81,16 @@ abstract contract CLSwapRouterBase is SwapRouterBase, ICLSwapRouterBase {
V4CLExactOutputSingleParams memory params,
V4SettlementParams memory settlementParams
) internal returns (uint256 amountIn) {
amountIn = uint128(
_swapExactPrivate(
params.poolKey,
params.zeroForOne,
int256(int128(params.amountOut)),
params.sqrtPriceLimitX96,
settlementParams.payer,
params.recipient,
settlementParams.settle,
settlementParams.take,
params.hookData
)
amountIn = _swapExactPrivate(
params.poolKey,
params.zeroForOne,
int256(int128(params.amountOut)),
params.sqrtPriceLimitX96,
settlementParams.payer,
params.recipient,
settlementParams.settle,
settlementParams.take,
params.hookData
);
if (amountIn > params.amountInMaximum) revert TooMuchRequested();
}
Expand All @@ -119,18 +112,16 @@ abstract contract CLSwapRouterBase is SwapRouterBase, ICLSwapRouterBase {

for (uint256 i = state.pathLength; i > 0; i--) {
(state.poolKey, state.oneForZero) = _getPoolAndSwapDirection(params.path[i - 1], params.currencyOut);
state.amountIn = uint128(
_swapExactPrivate(
state.poolKey,
!state.oneForZero,
int256(int128(params.amountOut)),
0,
settlementParams.payer,
params.recipient,
i == 1 && settlementParams.settle,
i == state.pathLength && settlementParams.take,
params.path[i - 1].hookData
)
state.amountIn = _swapExactPrivate(
state.poolKey,
!state.oneForZero,
int256(int128(params.amountOut)),
0,
settlementParams.payer,
params.recipient,
i == 1 && settlementParams.settle,
i == state.pathLength && settlementParams.take,
params.path[i - 1].hookData
);

params.amountOut = state.amountIn;
Expand All @@ -142,6 +133,9 @@ abstract contract CLSwapRouterBase is SwapRouterBase, ICLSwapRouterBase {
}
}

/// @return reciprocalAmount The amount of the reciprocal token
// If exactInput token0 for token1, the reciprocalAmount is the amount of token1.
// If exactOutput token0 for token1, the reciprocalAmount is the amount of token0.
function _swapExactPrivate(
PoolKey memory poolKey,
bool zeroForOne,
Expand All @@ -152,7 +146,7 @@ abstract contract CLSwapRouterBase is SwapRouterBase, ICLSwapRouterBase {
bool settle,
bool take,
bytes memory hookData
) private returns (int128 reciprocalAmount) {
) private returns (uint128 reciprocalAmount) {
BalanceDelta delta = clPoolManager.swap(
poolKey,
ICLPoolManager.SwapParams(
Expand All @@ -166,11 +160,15 @@ abstract contract CLSwapRouterBase is SwapRouterBase, ICLSwapRouterBase {
);

if (zeroForOne) {
reciprocalAmount = amountSpecified < 0 ? -delta.amount1() : -delta.amount0();
/// @dev amountSpecified < 0 indicate exactInput, so reciprocal token is token1 and positive
/// amountSpecified > 0 indicate exactOutput, so reciprocal token is token0 but is negative
reciprocalAmount = amountSpecified < 0 ? uint128(delta.amount1()) : uint128(-delta.amount0());

if (settle) _payAndSettle(poolKey.currency0, payer, -delta.amount0());
if (take) vault.take(poolKey.currency1, recipient, uint128(delta.amount1()));
} else {
reciprocalAmount = amountSpecified < 0 ? -delta.amount0() : -delta.amount1();
reciprocalAmount = amountSpecified < 0 ? uint128(delta.amount0()) : uint128(-delta.amount1());

if (settle) _payAndSettle(poolKey.currency1, payer, -delta.amount1());
if (take) vault.take(poolKey.currency0, recipient, uint128(delta.amount0()));
}
Expand Down
2 changes: 1 addition & 1 deletion test/pool-cl/CLSwapRouter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ contract CLSwapRouterTest is TokenFixture, Test, GasSnapshot {
snapEnd();
}

function testExactInputX() external {
function testExactInput() external {
ISwapRouterBase.PathKey[] memory path = new ISwapRouterBase.PathKey[](2);
path[0] = ISwapRouterBase.PathKey({
intermediateCurrency: currency1,
Expand Down

0 comments on commit a916fab

Please sign in to comment.