feat: Add transferFromNeeded to non permit2 methods

This will block an attempt to transfer from the user when we expect the funds to already be in the router.

chores:
- add docs
- in EncodingContext, rename transfer to transfer_type

Took 58 minutes
This commit is contained in:
Diana Carvalho
2025-05-19 12:02:57 +01:00
parent e0c195f63d
commit cc9e88cfed
13 changed files with 179 additions and 76 deletions

View File

@@ -82,6 +82,7 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup {
false,
false,
ALICE,
true,
swap
);
@@ -116,7 +117,7 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup {
vm.expectRevert(TychoRouter__UndefinedMinAmountOut.selector);
tychoRouter.singleSwap(
amountIn, WETH_ADDR, DAI_ADDR, 0, false, false, ALICE, swap
amountIn, WETH_ADDR, DAI_ADDR, 0, false, false, ALICE, true, swap
);
}
@@ -150,6 +151,7 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup {
false,
false,
ALICE,
true,
swap
);
}
@@ -192,6 +194,7 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup {
false,
false,
ALICE,
true,
swap
);
}
@@ -287,6 +290,49 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup {
vm.stopPrank();
}
function testSingleSwapNoTransferNeededIllegalTransfer() public {
// Tokens are already in the router, there is no need to transfer them.
// Failure because there will be an attempt on an illegal transfer.
uint256 amountIn = 1 ether;
deal(WETH_ADDR, address(tychoRouter), amountIn);
vm.startPrank(ALICE);
// Approve the tokenIn to be transferred to the router
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn);
bytes memory protocolData = encodeUniswapV2Swap(
WETH_ADDR,
WETH_DAI_POOL,
ALICE,
false,
RestrictTransferFrom.TransferType.TransferFrom
);
bytes memory swap =
encodeSingleSwap(address(usv2Executor), protocolData);
vm.expectRevert(
abi.encodeWithSelector(
RestrictTransferFrom__ExceededTransferFromAllowance.selector,
0, // allowed amount
amountIn // attempted amount
)
);
uint256 amountOut = tychoRouter.singleSwap(
amountIn,
WETH_ADDR,
DAI_ADDR,
2000 * 1e18,
false,
false,
ALICE,
false,
swap
);
vm.stopPrank();
}
function testSingleSwapIntegration() public {
// Tests swapping WETH -> DAI on a USV2 pool with regular approvals
deal(WETH_ADDR, ALICE, 1 ether);