Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
dianakocsis committed Sep 5, 2024
2 parents 761b112 + d1afb1b commit d1fd29b
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6865
7063
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121378
121413
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116679
116714
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125494
125529
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129411
129446
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179239
179274
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175123
175158
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229074
229109
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
224982
225017
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220283
220318
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128684
128716
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120651
120683
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116027
116059
11 changes: 10 additions & 1 deletion src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
}

function _swapExactOutputSingle(IV4Router.ExactOutputSingleParams calldata params) private {
uint128 amountOut = params.amountOut;
if (amountOut == ActionConstants.OPEN_DELTA) {
amountOut =
_getFullDebt(params.zeroForOne ? params.poolKey.currency1 : params.poolKey.currency0).toUint128();
}
uint128 amountIn = (
uint256(
-int256(
_swap(
params.poolKey,
params.zeroForOne,
int256(uint256(params.amountOut)),
int256(uint256(amountOut)),
params.sqrtPriceLimitX96,
params.hookData
)
Expand All @@ -146,6 +151,10 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
Currency currencyOut = params.currencyOut;
PathKey calldata pathKey;

if (amountOut == ActionConstants.OPEN_DELTA) {
amountOut = _getFullDebt(currencyOut).toUint128();
}

for (uint256 i = pathLength; i > 0; i--) {
pathKey = params.path[i - 1];
(PoolKey memory poolKey, bool oneForZero) = pathKey.getPoolAndSwapDirection(currencyOut);
Expand Down
59 changes: 59 additions & 0 deletions test/router/V4Router.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,35 @@ contract V4RouterTest is RoutingTestHelpers {
assertEq(outputBalanceAfter - outputBalanceBefore, amountOut);
}

function test_swapExactOutputSingle_swapOpenDelta() public {
uint256 expectedAmountIn = 1008049273448486163;

IV4Router.ExactOutputSingleParams memory params = IV4Router.ExactOutputSingleParams(
key0, true, ActionConstants.OPEN_DELTA, uint128(expectedAmountIn + 1), 0, bytes("")
);

plan = plan.add(Actions.TAKE, abi.encode(key0.currency1, ActionConstants.ADDRESS_THIS, 1 ether));
plan = plan.add(Actions.SWAP_EXACT_OUT_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE, abi.encode(key0.currency0, ActionConstants.OPEN_DELTA, true));

bytes memory data = plan.encode();

uint256 callerInputBefore = key0.currency0.balanceOfSelf();
uint256 routerInputBefore = key0.currency1.balanceOfSelf();
uint256 callerOutputBefore = key0.currency1.balanceOfSelf();

router.executeActions(data);

uint256 callerInputAfter = key0.currency0.balanceOfSelf();
uint256 routerInputAfter = key0.currency1.balanceOfSelf();
uint256 callerOutputAfter = key0.currency1.balanceOfSelf();

// caller paid
assertEq(callerInputBefore - expectedAmountIn, callerInputAfter);
assertEq(routerInputBefore, routerInputAfter);
assertEq(callerOutputBefore, callerOutputAfter);
}

function test_swapExactOut_revertsForAmountIn() public {
uint256 amountOut = 1 ether;
uint256 expectedAmountIn = 1008049273448486163;
Expand Down Expand Up @@ -641,6 +670,36 @@ contract V4RouterTest is RoutingTestHelpers {
assertEq(outputBalanceAfter - outputBalanceBefore, amountOut);
}

function test_swapExactOut_swapOpenDelta() public {
uint256 expectedAmountIn = 1008049273448486163;

tokenPath.push(currency0);
tokenPath.push(currency1);

IV4Router.ExactOutputParams memory params = _getExactOutputParams(tokenPath, ActionConstants.OPEN_DELTA);

plan = plan.add(Actions.TAKE, abi.encode(key0.currency1, ActionConstants.ADDRESS_THIS, 1 ether));
plan = plan.add(Actions.SWAP_EXACT_OUT, abi.encode(params));
plan = plan.add(Actions.SETTLE, abi.encode(key0.currency0, ActionConstants.OPEN_DELTA, true));

bytes memory data = plan.encode();

uint256 callerInputBefore = key0.currency0.balanceOfSelf();
uint256 routerInputBefore = key0.currency1.balanceOfSelf();
uint256 callerOutputBefore = key0.currency1.balanceOfSelf();

router.executeActions(data);

uint256 callerInputAfter = key0.currency0.balanceOfSelf();
uint256 routerInputAfter = key0.currency1.balanceOfSelf();
uint256 callerOutputAfter = key0.currency1.balanceOfSelf();

// caller paid
assertEq(callerInputBefore - expectedAmountIn, callerInputAfter);
assertEq(routerInputBefore, routerInputAfter);
assertEq(callerOutputBefore, callerOutputAfter);
}

/*//////////////////////////////////////////////////////////////
ETH -> ERC20 and ERC20 -> ETH EXACT OUTPUT
//////////////////////////////////////////////////////////////*/
Expand Down

0 comments on commit d1fd29b

Please sign in to comment.