Skip to content

Commit

Permalink
Spearbit 99 (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
dianakocsis authored Sep 5, 2024
1 parent a0ef70d commit d1afb1b
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6909
7107
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121352
121387
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116653
116688
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125468
125503
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129385
129420
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179217
179252
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175101
175136
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229056
229091
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
224964
224999
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220265
220300
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128666
128698
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120633
120665
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116009
116041
15 changes: 10 additions & 5 deletions src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ 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 = (
-_swap(
params.poolKey,
params.zeroForOne,
int256(int128(params.amountOut)),
params.sqrtPriceLimitX96,
params.hookData
params.poolKey, params.zeroForOne, int256(int128(amountOut)), params.sqrtPriceLimitX96, params.hookData
)
).toUint128();
if (amountIn > params.amountInMaximum) revert V4TooMuchRequested(params.amountInMaximum, amountIn);
Expand All @@ -142,6 +143,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 d1afb1b

Please sign in to comment.