Skip to content

Commit

Permalink
pass hook data along
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilz committed Sep 26, 2023
1 parent 01a5db0 commit 31cbfe5
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterBytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4830
5038
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactIn1Hop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193895
195455
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
270947
274012
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348004
352580
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192342
193868
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactOut1Hop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193055
194621
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
270996
274074
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348965
353558
2 changes: 1 addition & 1 deletion .forge-snapshots/RouterExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
191562
193088
17 changes: 11 additions & 6 deletions contracts/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ abstract contract V4Router is IV4Router {
params.sqrtPriceLimitX96,
msgSender,
true,
true
true,
params.hookData
);
}

Expand All @@ -75,7 +76,8 @@ abstract contract V4Router is IV4Router {
0,
msgSender,
i == 0,
i == pathLength - 1
i == pathLength - 1,
params.path[i].hookData
)
);

Expand All @@ -95,7 +97,8 @@ abstract contract V4Router is IV4Router {
params.sqrtPriceLimitX96,
msgSender,
true,
true
true,
params.hookData
);
}

Expand All @@ -114,7 +117,8 @@ abstract contract V4Router is IV4Router {
0,
msgSender,
i == 1,
i == pathLength
i == pathLength,
params.path[i - 1].hookData
)
);

Expand All @@ -132,7 +136,8 @@ abstract contract V4Router is IV4Router {
uint160 sqrtPriceLimitX96,
address msgSender,
bool settle,
bool take
bool take,
bytes memory hookData
) private returns (int128 reciprocalAmount) {
BalanceDelta delta = poolManager.swap(
poolKey,
Expand All @@ -143,7 +148,7 @@ abstract contract V4Router is IV4Router {
? (zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1)
: sqrtPriceLimitX96
),
bytes("")
hookData
);

if (zeroForOne) {
Expand Down
3 changes: 3 additions & 0 deletions contracts/interfaces/IV4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface IV4Router {
uint24 fee;
int24 tickSpacing;
IHooks hooks;
bytes hookData;
}

struct ExactInputSingleParams {
Expand All @@ -38,6 +39,7 @@ interface IV4Router {
uint128 amountIn;
uint128 amountOutMinimum;
uint160 sqrtPriceLimitX96;
bytes hookData;
}

struct ExactInputParams {
Expand All @@ -55,6 +57,7 @@ interface IV4Router {
uint128 amountOut;
uint128 amountInMaximum;
uint160 sqrtPriceLimitX96;
bytes hookData;
}

struct ExactOutputParams {
Expand Down
22 changes: 11 additions & 11 deletions test/V4Router.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
token2 = new MockERC20("Test2", "2", 18, 2 ** 128);
token3 = new MockERC20("Test3", "3", 18, 2 ** 128);

key0 = createPoolKey(token0, token1);
key1 = createPoolKey(token1, token2);
key2 = createPoolKey(token2, token3);
key0 = createPoolKey(token0, token1, address(0));
key1 = createPoolKey(token1, token2, address(0));
key2 = createPoolKey(token2, token3, address(0));

setupPool(key0);
setupPool(key1);
Expand All @@ -67,7 +67,7 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
uint256 expectedAmountOut = 992054607780215625;

IV4Router.ExactInputSingleParams memory params =
IV4Router.ExactInputSingleParams(key0, true, address(this), uint128(amountIn), 0, 0);
IV4Router.ExactInputSingleParams(key0, true, address(this), uint128(amountIn), 0, 0, bytes(""));

uint256 prevBalance0 = token0.balanceOf(address(this));
uint256 prevBalance1 = token1.balanceOf(address(this));
Expand All @@ -88,7 +88,7 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
uint256 expectedAmountOut = 992054607780215625;

IV4Router.ExactInputSingleParams memory params =
IV4Router.ExactInputSingleParams(key0, false, address(this), uint128(amountIn), 0, 0);
IV4Router.ExactInputSingleParams(key0, false, address(this), uint128(amountIn), 0, 0, bytes(""));

uint256 prevBalance0 = token0.balanceOf(address(this));
uint256 prevBalance1 = token1.balanceOf(address(this));
Expand Down Expand Up @@ -205,7 +205,7 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
uint256 expectedAmountIn = 1008049273448486163;

IV4Router.ExactOutputSingleParams memory params =
IV4Router.ExactOutputSingleParams(key0, true, address(this), uint128(amountOut), 0, 0);
IV4Router.ExactOutputSingleParams(key0, true, address(this), uint128(amountOut), 0, 0, bytes(""));

uint256 prevBalance0 = token0.balanceOf(address(this));
uint256 prevBalance1 = token1.balanceOf(address(this));
Expand All @@ -226,7 +226,7 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
uint256 expectedAmountIn = 1008049273448486163;

IV4Router.ExactOutputSingleParams memory params =
IV4Router.ExactOutputSingleParams(key0, false, address(this), uint128(amountOut), 0, 0);
IV4Router.ExactOutputSingleParams(key0, false, address(this), uint128(amountOut), 0, 0, bytes(""));

uint256 prevBalance0 = token0.balanceOf(address(this));
uint256 prevBalance1 = token1.balanceOf(address(this));
Expand Down Expand Up @@ -341,9 +341,9 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
assertEq(token3.balanceOf(address(router)), 0);
}

function createPoolKey(MockERC20 tokenA, MockERC20 tokenB) internal pure returns (PoolKey memory) {
function createPoolKey(MockERC20 tokenA, MockERC20 tokenB, address hookAddr) internal pure returns (PoolKey memory) {
if (address(tokenA) > address(tokenB)) (tokenA, tokenB) = (tokenB, tokenA);
return PoolKey(Currency.wrap(address(tokenA)), Currency.wrap(address(tokenB)), 3000, 60, IHooks(address(0)));
return PoolKey(Currency.wrap(address(tokenA)), Currency.wrap(address(tokenB)), 3000, 60, IHooks(hookAddr));
}

function setupPool(PoolKey memory poolKey) internal {
Expand All @@ -364,7 +364,7 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
{
IV4Router.PathKey[] memory path = new IV4Router.PathKey[](_tokenPath.length - 1);
for (uint256 i = 0; i < _tokenPath.length - 1; i++) {
path[i] = IV4Router.PathKey(Currency.wrap(address(_tokenPath[i + 1])), 3000, 60, IHooks(address(0)));
path[i] = IV4Router.PathKey(Currency.wrap(address(_tokenPath[i + 1])), 3000, 60, IHooks(address(0)), bytes(""));
}

params.currencyIn = Currency.wrap(address(_tokenPath[0]));
Expand All @@ -381,7 +381,7 @@ contract V4RouterTest is Test, Deployers, GasSnapshot {
{
IV4Router.PathKey[] memory path = new IV4Router.PathKey[](_tokenPath.length - 1);
for (uint256 i = _tokenPath.length - 1; i > 0; i--) {
path[i - 1] = IV4Router.PathKey(Currency.wrap(address(_tokenPath[i - 1])), 3000, 60, IHooks(address(0)));
path[i - 1] = IV4Router.PathKey(Currency.wrap(address(_tokenPath[i - 1])), 3000, 60, IHooks(address(0)), bytes(""));
}

params.currencyOut = Currency.wrap(address(_tokenPath[_tokenPath.length - 1]));
Expand Down

0 comments on commit 31cbfe5

Please sign in to comment.