fix: Hook and hook data are per pool! not per swap

This was a big bug that was blocking a grouped swap in uniswap v4 with hooks
Updated execution and encoding to match te fix

Took 49 minutes
This commit is contained in:
Diana Carvalho
2025-10-09 11:16:26 +02:00
parent f82ae3b92a
commit d13b4d8586
7 changed files with 168 additions and 111 deletions

View File

@@ -59,6 +59,8 @@ contract UniswapV4Executor is
address intermediaryToken;
uint24 fee;
int24 tickSpacing;
address hook;
bytes hookData;
}
constructor(IPoolManager _poolManager, address _permit2)
@@ -89,8 +91,6 @@ contract UniswapV4Executor is
bool zeroForOne,
TransferType transferType,
address receiver,
address hook,
bytes memory hookData,
UniswapV4Executor.UniswapV4Pool[] memory pools
) = _decodeData(data);
bytes memory swapData;
@@ -100,7 +100,7 @@ contract UniswapV4Executor is
currency1: Currency.wrap(zeroForOne ? tokenOut : tokenIn),
fee: pools[0].fee,
tickSpacing: pools[0].tickSpacing,
hooks: IHooks(hook)
hooks: IHooks(pools[0].hook)
});
swapData = abi.encodeWithSelector(
this.swapExactInputSingle.selector,
@@ -109,7 +109,7 @@ contract UniswapV4Executor is
amountIn,
transferType,
receiver,
hookData
pools[0].hookData
);
} else {
PathKey[] memory path = new PathKey[](pools.length);
@@ -118,8 +118,8 @@ contract UniswapV4Executor is
intermediateCurrency: Currency.wrap(pools[i].intermediaryToken),
fee: pools[i].fee,
tickSpacing: pools[i].tickSpacing,
hooks: IHooks(hook),
hookData: hookData
hooks: IHooks(pools[i].hook),
hookData: pools[i].hookData
});
}
@@ -149,8 +149,6 @@ contract UniswapV4Executor is
bool zeroForOne,
TransferType transferType,
address receiver,
address hook,
bytes memory hookData,
UniswapV4Pool[] memory pools
)
{
@@ -163,42 +161,71 @@ contract UniswapV4Executor is
zeroForOne = data[40] != 0;
transferType = TransferType(uint8(data[41]));
receiver = address(bytes20(data[42:62]));
hook = address(bytes20(data[62:82]));
bytes calldata remaining = data[82:];
bytes calldata remaining = data[62:];
// Decode first pool with hook data
if (remaining.length < 48) {
// 20 + 3 + 3 + 20 + 2 = 48 minimum
revert UniswapV4Executor__InvalidDataLength();
}
address firstToken = address(bytes20(remaining[0:20]));
uint24 firstFee = uint24(bytes3(remaining[20:23]));
int24 firstTickSpacing = int24(uint24(bytes3(remaining[23:26])));
UniswapV4Pool memory firstPool =
UniswapV4Pool(firstToken, firstFee, firstTickSpacing);
address firstHook = address(bytes20(remaining[26:46]));
uint16 firstHookDataLength = uint16(bytes2(remaining[46:48]));
uint256 firstPoolTotalLength = 48 + firstHookDataLength;
if (remaining.length < firstPoolTotalLength) {
revert UniswapV4Executor__InvalidDataLength();
}
bytes memory firstHookData = remaining[48:48 + firstHookDataLength];
// Remaining after first pool are ple encoded
bytes[] memory encodedPools =
LibPrefixLengthEncodedByteArray.toArray(remaining[26:]);
bytes[] memory encodedPools = LibPrefixLengthEncodedByteArray.toArray(
remaining[firstPoolTotalLength:]
);
pools = new UniswapV4Pool[](1 + encodedPools.length);
pools[0] = firstPool;
uint256 encodedPoolsLength = 26;
uint256 plePoolsTotalLength;
pools[0] = UniswapV4Pool(
firstToken, firstFee, firstTickSpacing, firstHook, firstHookData
);
// Decode subsequent pools
for (uint256 i = 0; i < encodedPools.length; i++) {
bytes memory poolsData = encodedPools[i];
bytes memory poolData = encodedPools[i];
address intermediaryToken;
uint24 fee;
int24 tickSpacing;
address hook;
uint16 hookDataLength;
// slither-disable-next-line assembly
assembly {
intermediaryToken := mload(add(poolsData, add(0, 20)))
fee := shr(232, mload(add(poolsData, add(0, 52))))
tickSpacing := shr(232, mload(add(poolsData, add(0, 55))))
let dataPtr := add(poolData, 0x20)
intermediaryToken := shr(96, mload(dataPtr))
fee := and(shr(232, mload(add(dataPtr, 20))), 0xffffff)
tickSpacing := and(shr(208, mload(add(dataPtr, 20))), 0xffffff)
hook := shr(96, mload(add(dataPtr, 26)))
hookDataLength := and(shr(240, mload(add(dataPtr, 46))), 0xffff)
}
pools[i + 1] = UniswapV4Pool(intermediaryToken, fee, tickSpacing);
plePoolsTotalLength += 2 + encodedPoolsLength; // 2 bytes prefix + data
}
hookData = remaining[26 + plePoolsTotalLength:];
if (poolData.length < 48 + hookDataLength) {
revert UniswapV4Executor__InvalidDataLength();
}
bytes memory hookData = new bytes(hookDataLength);
for (uint256 j = 0; j < hookDataLength; j++) {
hookData[j] = poolData[48 + j];
}
pools[i + 1] = UniswapV4Pool(
intermediaryToken, fee, tickSpacing, hook, hookData
);
}
}
/**

File diff suppressed because one or more lines are too long

View File

@@ -24,8 +24,6 @@ contract UniswapV4ExecutorExposed is UniswapV4Executor {
bool zeroForOne,
RestrictTransferFrom.TransferType transferType,
address receiver,
address hook,
bytes memory hookData,
UniswapV4Pool[] memory pools
)
{
@@ -61,12 +59,16 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
pools[0] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: USDT_ADDR,
fee: pool1Fee,
tickSpacing: tickSpacing1
tickSpacing: tickSpacing1,
hook: address(0),
hookData: bytes("")
});
pools[1] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: USDE_ADDR,
fee: pool2Fee,
tickSpacing: tickSpacing2
tickSpacing: tickSpacing2,
hook: address(0),
hookData: bytes("0x12345")
});
bytes memory data = UniswapV4Utils.encodeExactInput(
@@ -75,8 +77,6 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
zeroForOne,
RestrictTransferFrom.TransferType.Transfer,
ALICE,
address(0),
bytes(""),
pools
);
@@ -86,8 +86,6 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
bool zeroForOneDecoded,
RestrictTransferFrom.TransferType transferType,
address receiver,
address hook,
bytes memory hookData,
UniswapV4Executor.UniswapV4Pool[] memory decodedPools
) = uniswapV4Exposed.decodeData(data);
@@ -99,7 +97,7 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
uint8(RestrictTransferFrom.TransferType.Transfer)
);
assertEq(receiver, ALICE);
assertEq(hook, address(0));
assertEq(decodedPools[0].hook, address(0));
assertEq(decodedPools.length, 2);
assertEq(decodedPools[0].intermediaryToken, USDT_ADDR);
assertEq(decodedPools[0].fee, pool1Fee);
@@ -107,6 +105,7 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
assertEq(decodedPools[1].intermediaryToken, USDE_ADDR);
assertEq(decodedPools[1].fee, pool2Fee);
assertEq(decodedPools[1].tickSpacing, tickSpacing2);
assertEq(decodedPools[1].hookData, bytes("0x12345"));
}
function testSingleSwap() public {
@@ -121,7 +120,9 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
pools[0] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: USDT_ADDR,
fee: uint24(100),
tickSpacing: int24(1)
tickSpacing: int24(1),
hook: address(0),
hookData: bytes("")
});
bytes memory data = UniswapV4Utils.encodeExactInput(
@@ -130,8 +131,6 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
true,
RestrictTransferFrom.TransferType.Transfer,
ALICE,
address(0),
bytes(""),
pools
);
@@ -175,12 +174,16 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
pools[0] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: USDT_ADDR,
fee: uint24(100),
tickSpacing: int24(1)
tickSpacing: int24(1),
hook: address(0),
hookData: bytes("")
});
pools[1] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: WBTC_ADDR,
fee: uint24(3000),
tickSpacing: int24(60)
tickSpacing: int24(60),
hook: address(0),
hookData: bytes("")
});
bytes memory data = UniswapV4Utils.encodeExactInput(
@@ -189,8 +192,6 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
true,
RestrictTransferFrom.TransferType.Transfer,
ALICE,
address(0),
bytes(""),
pools
);
@@ -237,7 +238,9 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
pools[0] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: WETH_ADDR,
fee: uint24(500),
tickSpacing: int24(1)
tickSpacing: int24(1),
hook: hook,
hookData: bytes("")
});
bytes memory data = UniswapV4Utils.encodeExactInput(
@@ -246,8 +249,6 @@ contract UniswapV4ExecutorTest is Constants, TestUtils {
true,
RestrictTransferFrom.TransferType.Transfer,
ALICE,
hook,
bytes(""),
pools
);
@@ -295,15 +296,15 @@ contract UniswapV4ExecutorTestForEuler is Constants, TestUtils {
deal(RLUSD_ADDR, address(uniswapV4Exposed), amountIn);
address eulerProxy = 0xe1Ce9AF672f8854845E5474400B6ddC7AE458a10;
uint256 rlusdEulerBalanceBefore = RLUSD.balanceOf(eulerProxy);
uint256 rlusdBalanceBeforeSwapExecutor =
RLUSD.balanceOf(address(uniswapV4Exposed));
UniswapV4Executor.UniswapV4Pool[] memory pools =
new UniswapV4Executor.UniswapV4Pool[](1);
pools[0] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: USDT_ADDR,
fee: uint24(50),
tickSpacing: int24(1)
tickSpacing: int24(1),
hook: address(0xF87ACF8428F2f9403AAA0256A7272d6549ECa8A8),
hookData: bytes("")
});
bytes memory data = UniswapV4Utils.encodeExactInput(
@@ -312,8 +313,6 @@ contract UniswapV4ExecutorTestForEuler is Constants, TestUtils {
true,
RestrictTransferFrom.TransferType.Transfer,
ALICE,
address(0xF87ACF8428F2f9403AAA0256A7272d6549ECa8A8),
bytes(""),
pools
);
@@ -340,7 +339,9 @@ contract TychoRouterForBalancerV3Test is TychoRouterTestSetup {
pools[0] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: USDT_ADDR,
fee: uint24(100),
tickSpacing: int24(1)
tickSpacing: int24(1),
hook: address(0),
hookData: bytes("")
});
bytes memory protocolData = UniswapV4Utils.encodeExactInput(
@@ -349,8 +350,6 @@ contract TychoRouterForBalancerV3Test is TychoRouterTestSetup {
true,
RestrictTransferFrom.TransferType.TransferFrom,
ALICE,
address(0),
bytes(""),
pools
);
@@ -385,12 +384,16 @@ contract TychoRouterForBalancerV3Test is TychoRouterTestSetup {
pools[0] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: USDT_ADDR,
fee: uint24(100),
tickSpacing: int24(1)
tickSpacing: int24(1),
hook: address(0),
hookData: bytes("")
});
pools[1] = UniswapV4Executor.UniswapV4Pool({
intermediaryToken: WBTC_ADDR,
fee: uint24(3000),
tickSpacing: int24(60)
tickSpacing: int24(60),
hook: address(0),
hookData: bytes("")
});
bytes memory protocolData = UniswapV4Utils.encodeExactInput(
@@ -399,8 +402,6 @@ contract TychoRouterForBalancerV3Test is TychoRouterTestSetup {
true,
RestrictTransferFrom.TransferType.TransferFrom,
ALICE,
address(0),
bytes(""),
pools
);

View File

@@ -10,8 +10,6 @@ library UniswapV4Utils {
bool zeroForOne,
RestrictTransferFrom.TransferType transferType,
address receiver,
address hook,
bytes memory hookData,
UniswapV4Executor.UniswapV4Pool[] memory pools
) public pure returns (bytes memory) {
require(pools.length > 0, "Must have at least one pool");
@@ -19,7 +17,10 @@ library UniswapV4Utils {
bytes memory firstPool = abi.encodePacked(
pools[0].intermediaryToken,
bytes3(pools[0].fee),
pools[0].tickSpacing
pools[0].tickSpacing,
pools[0].hook,
bytes2(uint16(pools[0].hookData.length)),
pools[0].hookData
);
bytes[] memory encodedExtraPools = new bytes[](pools.length - 1);
@@ -27,7 +28,10 @@ library UniswapV4Utils {
encodedExtraPools[i - 1] = abi.encodePacked(
pools[i].intermediaryToken,
bytes3(pools[i].fee),
pools[i].tickSpacing
pools[i].tickSpacing,
pools[i].hook,
bytes2(uint16(pools[i].hookData.length)),
pools[i].hookData
);
}
@@ -37,10 +41,8 @@ library UniswapV4Utils {
zeroForOne,
transferType,
receiver,
hook,
firstPool,
pleEncode(encodedExtraPools),
hookData
pleEncode(encodedExtraPools)
);
}