From eb1aa6cdcd03a462ea8f1a5d35b0171d4c7531af Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Wed, 16 Apr 2025 19:00:43 +0100 Subject: [PATCH 1/4] chore: Correct typo in all balancerAfter/Before (again) --- don't change below this line --- ENG-4314 Took 1 hour 14 minutes --- foundry/src/TychoRouter.sol | 1 - foundry/test/TychoRouterIntegration.t.sol | 68 +++++++++++------------ foundry/test/TychoRouterSingleSwap.t.sol | 12 ++-- 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/foundry/src/TychoRouter.sol b/foundry/src/TychoRouter.sol index f9597a1..9e244f3 100644 --- a/foundry/src/TychoRouter.sol +++ b/foundry/src/TychoRouter.sol @@ -537,7 +537,6 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard { uint256 initialBalanceTokenOut = _balanceOf(tokenOut, receiver); amountOut = _sequentialSwap(amountIn, swaps); - uint256 currentBalanceTokenIn = _balanceOf(tokenIn, address(this)); if (amountOut < minAmountOut) { revert TychoRouter__NegativeSlippage(amountOut, minAmountOut); diff --git a/foundry/test/TychoRouterIntegration.t.sol b/foundry/test/TychoRouterIntegration.t.sol index f168175..c1ee318 100644 --- a/foundry/test/TychoRouterIntegration.t.sol +++ b/foundry/test/TychoRouterIntegration.t.sol @@ -9,16 +9,16 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { deal(WETH_ADDR, ALICE, 1 ether); vm.startPrank(ALICE); IERC20(WETH_ADDR).approve(address(tychoRouterAddr), 1 ether); - uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(DAI_ADDR).balanceOf(ALICE); // Encoded solution generated using `test_split_swap_strategy_encoder_no_permit2` (bool success,) = tychoRouterAddr.call( hex"79b9b93b0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000008f1d5c1cae37400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000059005700010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb11cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000100000000000000" ); vm.stopPrank(); - uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(DAI_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 2659881924818443699787); + assertEq(balanceAfter - balanceBefore, 2659881924818443699787); } function testSplitUSV4Integration() public { @@ -30,7 +30,7 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { // USDC ──(USV4)──> ETH ───(USV4)──> PEPE // deal(USDC_ADDR, ALICE, 1 ether); - uint256 balancerBefore = IERC20(PEPE_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(PEPE_ADDR).balanceOf(ALICE); // Approve permit2 vm.startPrank(ALICE); @@ -42,10 +42,10 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = IERC20(PEPE_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(PEPE_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 97191013220606467325121599); + assertEq(balanceAfter - balanceBefore, 97191013220606467325121599); } function testSplitUSV4IntegrationInputETH() public { @@ -56,7 +56,7 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { // ETH ───(USV4)──> PEPE // deal(ALICE, 1 ether); - uint256 balancerBefore = IERC20(PEPE_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(PEPE_ADDR).balanceOf(ALICE); // Encoded solution generated using `test_split_encoding_strategy_usv4_eth_in` (bool success,) = tychoRouterAddr.call{value: 1 ether}( @@ -65,10 +65,10 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = IERC20(PEPE_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(PEPE_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 242373460199848577067005852); + assertEq(balanceAfter - balanceBefore, 242373460199848577067005852); } function testSplitUSV4IntegrationOutputETH() public { @@ -79,7 +79,7 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { // USDC ───(USV4)──> ETH // deal(USDC_ADDR, ALICE, 3000_000000); - uint256 balancerBefore = ALICE.balance; + uint256 balanceBefore = ALICE.balance; // Approve permit2 vm.startPrank(ALICE); @@ -92,18 +92,18 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = ALICE.balance; + uint256 balanceAfter = ALICE.balance; assertTrue(success, "Call Failed"); - console.logUint(balancerAfter - balancerBefore); - assertEq(balancerAfter - balancerBefore, 1117254495486192350); + console.logUint(balanceAfter - balanceBefore); + assertEq(balanceAfter - balanceBefore, 1117254495486192350); } function testSplitSwapSingleWithWrapIntegration() public { // Tests swapping WETH -> DAI on a USV2 pool, but ETH is received from the user // and wrapped before the swap deal(ALICE, 1 ether); - uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(DAI_ADDR).balanceOf(ALICE); // Approve permit2 vm.startPrank(ALICE); @@ -114,17 +114,17 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(DAI_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 2659881924818443699787); + assertEq(balanceAfter - balanceBefore, 2659881924818443699787); } function testSplitSwapSingleWithUnwrapIntegration() public { // Tests swapping DAI -> WETH on a USV2 pool, and WETH is unwrapped to ETH // before sending back to the user deal(DAI_ADDR, ALICE, 3000 ether); - uint256 balancerBefore = ALICE.balance; + uint256 balanceBefore = ALICE.balance; // Approve permit2 vm.startPrank(ALICE); @@ -136,10 +136,10 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = ALICE.balance; + uint256 balanceAfter = ALICE.balance; assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 1120007305574805922); + assertEq(balanceAfter - balanceBefore, 1120007305574805922); } function testSplitEkuboIntegration() public { @@ -153,7 +153,7 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { vm.stopPrank(); deal(ALICE, 1 ether); - uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(USDC_ADDR).balanceOf(ALICE); // Approve permit2 vm.startPrank(ALICE); @@ -162,10 +162,10 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { hex"79b9b93b0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000078007600010000003d7ebc40af7092e3f1c81f2e996cba5cae2090d705cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4851d02a5948496a67827242eabc5725531342527c0000000000000000000000000000000000000000" ); - uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertGe(balancerAfter - balancerBefore, 26173932); + assertGe(balanceAfter - balanceBefore, 26173932); assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); } @@ -176,7 +176,7 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { // WETH ─┤ // └──(USV2)──> DAI ───(USV2)──> USDC deal(WETH_ADDR, ALICE, 1 ether); - uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(USDC_ADDR).balanceOf(ALICE); // Approve permit2 vm.startPrank(ALICE); @@ -188,10 +188,10 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertGe(balancerAfter - balancerBefore, 26173932); + assertGe(balanceAfter - balanceBefore, 26173932); // All input tokens are transferred to the router at first. Make sure we used // all of it (and thus our splits are correct). @@ -203,22 +203,22 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { // // WETH ──(USV2)──> WBTC ───(USV2)──> USDC deal(WETH_ADDR, ALICE, 1 ether); - uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(USDC_ADDR).balanceOf(ALICE); // Approve permit2 vm.startPrank(ALICE); IERC20(WETH_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max); // Encoded solution generated using `test_sequential_swap_strategy_encoder_complex_route` (bool success,) = tychoRouterAddr.call( - hex"51bcc7b60000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000006826193a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067fe934200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000041d137d0776bc16ff9c49bfd3e96103ceb6926654f314489cafcf5a64ab7a9c4f2061ed5ffdef67c33c3c5b78036d28d9eb73da156a0e68d8740235be50e88a3481b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a800525615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d01395000200525615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a416cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20100000000000000000000000000000000000000000000000000" + hex"51bcc7b60000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000682714ab00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ff8eb300000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000000412fe66c22814eb271e37bb03303bae445eb96aa50fae9680a0ae685ee5795aebf1f5bb7718154c69680bcfc00cc9be525b2b021f57a1bddb4db622139acd425d41b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a800525615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d940004375dff511095cc5a197a54140a24efef3a416000200525615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a416cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20105000000000000000000000000000000000000000000000000" ); vm.stopPrank(); - uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 2552915143); + assertEq(balanceAfter - balanceBefore, 2552915143); assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); } @@ -227,22 +227,22 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { // // WETH ──(USV2)──> WBTC ───(USV2)──> USDC deal(WETH_ADDR, ALICE, 1 ether); - uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(USDC_ADDR).balanceOf(ALICE); // Approve permit2 vm.startPrank(ALICE); IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max); // Encoded solution generated using `test_sequential_swap_strategy_encoder_no_permit2` (bool success,) = tychoRouterAddr.call( - hex"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000a800525615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d01395000100525615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a416cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20100000000000000000000000000000000000000000000000000" + hex"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000a800525615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d940004375dff511095cc5a197a54140a24efef3a416000100525615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a416cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20105000000000000000000000000000000000000000000000000" ); vm.stopPrank(); - uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 2552915143); + assertEq(balanceAfter - balanceBefore, 2552915143); assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); } @@ -254,7 +254,7 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup { IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max); // Encoded solution generated using `test_cyclic_sequential_swap_split_strategy` (bool success,) = tychoRouterAddr.call( - hex"7c5538460000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f4308e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000000000000000000000000000000000006826193a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067fe934200000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000004194fc497ac440b520981d23b4713425da21dc1c801e657d218a917b5c51339a660b9a5fe0a346cb0aacc0d67ebf03f8fa3ec9fade437ef1b08ea837b2442931b61b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0006e00010000002e234dae75c793f67a35089c9d99245e1c58470ba0b86991c6218b36c1d19d4a2e9eb0ce3606eb48c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f43ede3eca2a72b3aecc820e955b36f38437d0139588e6a0c2ddd26feeb64f039a2c41296fcb3f56400102006e01000000002e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000bb8cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc28ad599c3a0ff1de082011efddc58f1908eb6e6d80000" + hex"7c5538460000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f4308e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f5e10000000000000000000000000000000000000000000000000000000000682714ab00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ff8eb30000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000417badef4d6a9158869bdd061a492f6ca4eb4e012b9295f5562414f83ccbc37982241b4de1d934b4f9f0d51f792b12019f806953e7a7ba49d6cfe0487458753e871b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0006e00010000002e234dae75c793f67a35089c9d99245e1c58470ba0b86991c6218b36c1d19d4a2e9eb0ce3606eb48c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f43ede3eca2a72b3aecc820e955b36f38437d0139588e6a0c2ddd26feeb64f039a2c41296fcb3f56400102006e01000000002e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000bb8cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc28ad599c3a0ff1de082011efddc58f1908eb6e6d80000" ); assertTrue(success, "Call Failed"); diff --git a/foundry/test/TychoRouterSingleSwap.t.sol b/foundry/test/TychoRouterSingleSwap.t.sol index f2fba1b..93beba0 100644 --- a/foundry/test/TychoRouterSingleSwap.t.sol +++ b/foundry/test/TychoRouterSingleSwap.t.sol @@ -290,7 +290,7 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup { function testSingleSwapIntegration() public { // Tests swapping WETH -> DAI on a USV2 pool with regular approvals deal(WETH_ADDR, ALICE, 1 ether); - uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(DAI_ADDR).balanceOf(ALICE); vm.startPrank(ALICE); IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max); @@ -301,15 +301,15 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(DAI_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 2659881924818443699787); + assertEq(balanceAfter - balanceBefore, 2659881924818443699787); } function testSingleSwapIntegrationPermit2() public { // Tests swapping WETH -> DAI on a USV2 pool with permit2 deal(WETH_ADDR, ALICE, 1 ether); - uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceBefore = IERC20(DAI_ADDR).balanceOf(ALICE); vm.startPrank(ALICE); IERC20(WETH_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max); @@ -320,8 +320,8 @@ contract TychoRouterSingleSwapTest is TychoRouterTestSetup { vm.stopPrank(); - uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE); + uint256 balanceAfter = IERC20(DAI_ADDR).balanceOf(ALICE); assertTrue(success, "Call Failed"); - assertEq(balancerAfter - balancerBefore, 2659881924818443699787); + assertEq(balanceAfter - balanceBefore, 2659881924818443699787); } } From efe12cfcd671ec1a93c106dcacf4535ebb54923e Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Wed, 16 Apr 2025 19:01:15 +0100 Subject: [PATCH 2/4] feat: Support in between swaps optimizations --- don't change below this line --- ENG-4314 Took 31 seconds --- foundry/test/TychoRouterSequentialSwap.t.sol | 96 +++ src/encoding/evm/constants.rs | 17 +- src/encoding/evm/group_swaps.rs | 6 +- .../evm/strategy_encoder/strategy_encoders.rs | 628 +++++++++++++----- .../transfer_optimizations.rs | 84 ++- 5 files changed, 649 insertions(+), 182 deletions(-) diff --git a/foundry/test/TychoRouterSequentialSwap.t.sol b/foundry/test/TychoRouterSequentialSwap.t.sol index 9e35c6f..f8c0776 100644 --- a/foundry/test/TychoRouterSequentialSwap.t.sol +++ b/foundry/test/TychoRouterSequentialSwap.t.sol @@ -340,4 +340,100 @@ contract TychoRouterSequentialSwapTest is TychoRouterTestSetup { tychoRouter.exposedSequentialSwap(amountIn, pleEncode(swaps)); assertEq(IERC20(USDC_ADDR).balanceOf(tychoRouterAddr), 99889294); } + + function testUSV3USV2Integration() public { + // Performs a sequential swap from WETH to USDC though WBTC and DAI using USV3 and USV2 pools + // + // WETH ──(USV3)──> WBTC ───(USV2)──> USDC + deal(WETH_ADDR, ALICE, 1 ether); + uint256 balanceBefore = IERC20(USDC_ADDR).balanceOf(ALICE); + + // Approve permit2 + vm.startPrank(ALICE); + IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max); + // Encoded solution generated using `test_uniswap_v3_uniswap_v2` + (bool success,) = tychoRouterAddr.call( + hex"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000bf00692e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc22260fac5e5542a773aa44fbcfedf7c193bc2c599000bb8004375dff511095cc5a197a54140a24efef3a416cbcdf9626bc03e24f779434178a73a0b4bad62ed000100525615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a416cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2010500" + ); + + vm.stopPrank(); + + uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE); + + assertTrue(success, "Call Failed"); + assertEq(balanceAfter - balanceBefore, 2554299052); + assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); + } + + function testUSV3USV3Integration() public { + // Performs a sequential swap from WETH to USDC though WBTC using USV3 pools + // + // WETH ──(USV3)──> WBTC ───(USV3)──> USDC + deal(WETH_ADDR, ALICE, 1 ether); + uint256 balanceBefore = IERC20(USDC_ADDR).balanceOf(ALICE); + + // Approve permit2 + vm.startPrank(ALICE); + IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max); + // Encoded solution generated using `test_uniswap_v3_uniswap_v3` + (bool success,) = tychoRouterAddr.call( + hex"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000d600692e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc22260fac5e5542a773aa44fbcfedf7c193bc2c599000bb83ede3eca2a72b3aecc820e955b36f38437d01395cbcdf9626bc03e24f779434178a73a0b4bad62ed000100692e234dae75c793f67a35089c9d99245e1c58470b2260fac5e5542a773aa44fbcfedf7c193bc2c599a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000bb8cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc299ac8ca7087fa4a2a1fb6357269965a2014abc35010000000000000000000000" + ); + + vm.stopPrank(); + + uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE); + + assertTrue(success, "Call Failed"); + assertEq(balanceAfter - balanceBefore, 2647438249); + assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); + } + + function testUSV3CurveIntegration() public { + // Performs a sequential swap from WETH to USDT though WBTC using USV3 and Curve pools + // + // WETH ──(USV3)──> WBTC ───(USV3)──> USDT + deal(WETH_ADDR, ALICE, 1 ether); + uint256 balanceBefore = IERC20(USDT_ADDR).balanceOf(ALICE); + + // Approve permit2 + vm.startPrank(ALICE); + IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max); + // Encoded solution generated using `test_uniswap_v3_curve` + (bool success,) = tychoRouterAddr.call( + hex"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000d600692e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc22260fac5e5542a773aa44fbcfedf7c193bc2c599000bb83ede3eca2a72b3aecc820e955b36f38437d01395cbcdf9626bc03e24f779434178a73a0b4bad62ed000100691d1499e622d69689cdf9004d05ec547d650ff2112260fac5e5542a773aa44fbcfedf7c193bc2c599dac17f958d2ee523a2206206994597c13d831ec7d51a44d3fae010294c616388b506acda1bfaae460301000105cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000" + ); + + vm.stopPrank(); + + uint256 balanceAfter = IERC20(USDT_ADDR).balanceOf(ALICE); + + assertTrue(success, "Call Failed"); + assertEq(balanceAfter - balanceBefore, 2650183330); + assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); + } + + function testBalancerV2USV2Integration() public { + // Performs a sequential swap from WETH to USDC though WBTC using Balancer v2 and USV2 pools + // + // WETH ──(balancer)──> WBTC ───(USV2)──> USDC + deal(WETH_ADDR, ALICE, 1 ether); + uint256 balanceBefore = IERC20(USDT_ADDR).balanceOf(ALICE); + + // Approve permit2 + vm.startPrank(ALICE); + IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max); + // Encoded solution generated using `test_uniswap_v3_curve` + (bool success,) = tychoRouterAddr.call( + hex"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000c80072c7183455a4c133ae270771860664b6b7ec320bb1c02aaa39b223fe8d0a0e5c4f27ead9083c756cc22260fac5e5542a773aa44fbcfedf7c193bc2c599a6f548df93de924d73be7d25dc02554c6bd66db500020000000000000000000e004375dff511095cc5a197a54140a24efef3a416010300525615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a416cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20105000000000000000000000000000000000000000000000000" + ); + + vm.stopPrank(); + + uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE); + + assertTrue(success, "Call Failed"); + assertEq(balanceAfter - balanceBefore, 2549391308); + assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); + } } diff --git a/src/encoding/evm/constants.rs b/src/encoding/evm/constants.rs index ba0bce7..c2962f7 100644 --- a/src/encoding/evm/constants.rs +++ b/src/encoding/evm/constants.rs @@ -13,7 +13,7 @@ pub const PROTOCOL_SPECIFIC_CONFIG: &str = pub static GROUPABLE_PROTOCOLS: LazyLock> = LazyLock::new(|| { let mut set = HashSet::new(); set.insert("uniswap_v4"); - set.insert("balancer_v3"); + set.insert("vm:balancer_v3"); set.insert("ekubo_v2"); set }); @@ -23,18 +23,21 @@ pub static IN_TRANSFER_OPTIMIZABLE_PROTOCOLS: LazyLock> = LazyLock::new(|| { let mut set = HashSet::new(); set.insert("uniswap_v2"); + set.insert("sushiswap_v2"); + set.insert("pancakeswap_v2"); set.insert("uniswap_v3"); + set.insert("pancakeswap_v3"); set.insert("ekubo_v2"); set }); -/// These protocols expect funds to be in the router at the time of swap. -pub static PROTOCOLS_EXPECTING_FUNDS_IN_ROUTER: LazyLock> = +// These protocols do not support chained swaps from the same protocol. This is the case for uniswap +// v3 because of the callback logic. The only way for this to work it would be to call the second +// swap during the callback of the first swap. This is currently not supported. +pub static PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS: LazyLock> = LazyLock::new(|| { let mut set = HashSet::new(); - set.insert("vm:curve"); - set.insert("balancer_v2"); - // TODO remove uniswap_v4 when we add callback support for transfer optimizations - set.insert("uniswap_v4"); + set.insert("uniswap_v3"); + set.insert("pancakeswap_v3"); set }); diff --git a/src/encoding/evm/group_swaps.rs b/src/encoding/evm/group_swaps.rs index 0e4206c..1409b9f 100644 --- a/src/encoding/evm/group_swaps.rs +++ b/src/encoding/evm/group_swaps.rs @@ -255,7 +255,7 @@ mod tests { let swap_weth_wbtc = Swap { component: ProtocolComponent { - protocol_system: "balancer_v3".to_string(), + protocol_system: "vm:balancer_v3".to_string(), ..Default::default() }, token_in: weth.clone(), @@ -264,7 +264,7 @@ mod tests { }; let swap_wbtc_usdc = Swap { component: ProtocolComponent { - protocol_system: "balancer_v3".to_string(), + protocol_system: "vm:balancer_v3".to_string(), ..Default::default() }, token_in: wbtc.clone(), @@ -306,7 +306,7 @@ mod tests { swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], input_token: weth.clone(), output_token: usdc.clone(), - protocol_system: "balancer_v3".to_string(), + protocol_system: "vm:balancer_v3".to_string(), split: 0.5f64, }, SwapGroup { diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 45a83d8..ceb11d4 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -8,6 +8,7 @@ use crate::encoding::{ errors::EncodingError, evm::{ approvals::permit2::Permit2, + constants::{IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS}, group_swaps::group_swaps, strategy_encoder::{ strategy_validators::{SequentialSwapValidator, SplitSwapValidator, SwapValidator}, @@ -133,6 +134,7 @@ impl StrategyEncoder for SingleSwapStrategyEncoder { self.wrapped_address.clone(), self.permit2.clone().is_some(), wrap, + false, ); let encoding_context = EncodingContext { @@ -289,6 +291,7 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { } let mut swaps = vec![]; + let mut next_in_between_swap_optimization = true; for (i, grouped_swap) in grouped_swaps.iter().enumerate() { let protocol = grouped_swap.protocol_system.clone(); let swap_encoder = self @@ -300,12 +303,31 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { )) })?; - // if it is the last swap and there isn't an unwrap at the end, we can set the receiver - // to the final user - let swap_receiver = if i == grouped_swaps.len() - 1 && !unwrap { - solution.receiver.clone() + let in_between_swap_optimization = next_in_between_swap_optimization; + let next_swap = grouped_swaps.get(i + 1); + // if there is a next swap + let swap_receiver = if let Some(next) = next_swap { + // if the protocol of the next swap supports transfer in optimization + if IN_TRANSFER_OPTIMIZABLE_PROTOCOLS.contains(&next.protocol_system.as_str()) { + // if the protocol does not allow for chained swaps, we can't optimize the + // receiver of this swap nor the transfer in of the next swap + if PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS + .contains(&next.protocol_system.as_str()) && + PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS.contains(protocol.as_str()) + { + next_in_between_swap_optimization = false; + self.router_address.clone() + } else { + Bytes::from_str(&next.swaps[0].component.id.clone()).map_err(|_| { + EncodingError::FatalError("Invalid component id".to_string()) + })? + } + } else { + // the protocol of the next swap does not support transfer in optimization + self.router_address.clone() + } } else { - self.router_address.clone() + solution.receiver.clone() // last swap - there is not next swap }; let mut grouped_protocol_data: Vec = vec![]; @@ -317,6 +339,7 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { self.wrapped_address.clone(), self.permit2.clone().is_some(), wrap, + in_between_swap_optimization, ); let encoding_context = EncodingContext { @@ -553,6 +576,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { self.wrapped_address.clone(), self.permit2.clone().is_some(), wrap, + false, ); let encoding_context = EncodingContext { @@ -1148,164 +1172,466 @@ mod tests { println!("test_split_swap_strategy_encoder_complex_route: {}", _hex_calldata); } - #[test] - fn test_sequential_swap_strategy_encoder_complex_route() { - // Note: This test does not assert anything. It is only used to obtain integration test - // data for our router solidity test. - // - // Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools - // - // WETH ───(USV2)──> WBTC ───(USV2)──> USDC + mod sequential { + use super::*; - // Set up a mock private key for signing - let private_key = - "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string(); + #[test] + fn test_sequential_swap_strategy_encoder_complex_route() { + // Note: This test does not assert anything. It is only used to obtain integration test + // data for our router solidity test. + // + // Performs a sequential swap from WETH to USDC though WBTC using USV2 pools + // + // WETH ───(USV2)──> WBTC ───(USV2)──> USDC - let weth = weth(); - let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); - let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); + // Set up a mock private key for signing + let private_key = + "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string(); - let swap_weth_wbtc = Swap { - component: ProtocolComponent { - id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(), - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, - token_in: weth.clone(), - token_out: wbtc.clone(), - split: 0f64, - }; - let swap_wbtc_usdc = Swap { - component: ProtocolComponent { - id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(), - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, - token_in: wbtc.clone(), - token_out: usdc.clone(), - split: 0f64, - }; - let swap_encoder_registry = get_swap_encoder_registry(); - let encoder = SequentialSwapStrategyEncoder::new( - eth_chain(), - swap_encoder_registry, - Some(private_key), - Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), - ) - .unwrap(); - let solution = Solution { - exact_out: false, - given_token: weth, - given_amount: BigUint::from_str("1_000000000000000000").unwrap(), - checked_token: usdc, - expected_amount: None, - checked_amount: Some(BigUint::from_str("26173932").unwrap()), - sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), - receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), - swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], - ..Default::default() - }; + let weth = weth(); + let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); + let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); - let (calldata, _) = encoder - .encode_strategy(solution) + let swap_weth_wbtc = Swap { + component: ProtocolComponent { + id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: weth.clone(), + token_out: wbtc.clone(), + split: 0f64, + }; + let swap_wbtc_usdc = Swap { + component: ProtocolComponent { + id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: wbtc.clone(), + token_out: usdc.clone(), + split: 0f64, + }; + let swap_encoder_registry = get_swap_encoder_registry(); + let encoder = SequentialSwapStrategyEncoder::new( + eth_chain(), + swap_encoder_registry, + Some(private_key), + Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), + ) .unwrap(); - - let _hex_calldata = encode(&calldata); - println!("test_sequential_swap_strategy_encoder_complex_route: {}", _hex_calldata); - } - - #[test] - fn test_sequential_swap_strategy_encoder_no_permit2() { - // Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools - // - // WETH ───(USV2)──> WBTC ───(USV2)──> USDC - - let weth = weth(); - let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); - let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); - - let swap_weth_wbtc = Swap { - component: ProtocolComponent { - id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(), - protocol_system: "uniswap_v2".to_string(), + let solution = Solution { + exact_out: false, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").unwrap(), + checked_token: usdc, + expected_amount: None, + checked_amount: Some(BigUint::from_str("26173932").unwrap()), + sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], ..Default::default() - }, - token_in: weth.clone(), - token_out: wbtc.clone(), - split: 0f64, - }; - let swap_wbtc_usdc = Swap { - component: ProtocolComponent { - id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(), - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, - token_in: wbtc.clone(), - token_out: usdc.clone(), - split: 0f64, - }; - let swap_encoder_registry = get_swap_encoder_registry(); - let encoder = SequentialSwapStrategyEncoder::new( - eth_chain(), - swap_encoder_registry, - None, - Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), - ) - .unwrap(); - let solution = Solution { - exact_out: false, - given_token: weth, - given_amount: BigUint::from_str("1_000000000000000000").unwrap(), - checked_token: usdc, - expected_amount: None, - checked_amount: Some(BigUint::from_str("26173932").unwrap()), - sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), - receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), - swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], - ..Default::default() - }; + }; - let (calldata, _) = encoder - .encode_strategy(solution) + let (calldata, _) = encoder + .encode_strategy(solution) + .unwrap(); + + let _hex_calldata = encode(&calldata); + println!("test_sequential_swap_strategy_encoder_complex_route: {}", _hex_calldata); + } + + #[test] + fn test_sequential_swap_strategy_encoder_no_permit2() { + // Performs a sequential swap from WETH to USDC though WBTC using USV2 pools + // + // WETH ───(USV2)──> WBTC ───(USV2)──> USDC + + let weth = weth(); + let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); + let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); + + let swap_weth_wbtc = Swap { + component: ProtocolComponent { + id: "0xBb2b8038a1640196FbE3e38816F3e67Cba72D940".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: weth.clone(), + token_out: wbtc.clone(), + split: 0f64, + }; + let swap_wbtc_usdc = Swap { + component: ProtocolComponent { + id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: wbtc.clone(), + token_out: usdc.clone(), + split: 0f64, + }; + let swap_encoder_registry = get_swap_encoder_registry(); + let encoder = SequentialSwapStrategyEncoder::new( + eth_chain(), + swap_encoder_registry, + None, + Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), + ) .unwrap(); + let solution = Solution { + exact_out: false, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").unwrap(), + checked_token: usdc, + expected_amount: None, + checked_amount: Some(BigUint::from_str("26173932").unwrap()), + sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], + ..Default::default() + }; - let hex_calldata = encode(&calldata); - println!("test_sequential_swap_strategy_encoder_no_permit2: {}", hex_calldata); + let (calldata, _) = encoder + .encode_strategy(solution) + .unwrap(); - let expected = String::from(concat!( - "e8a980d7", /* function selector */ - "0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount in - "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in - "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token ou - "00000000000000000000000000000000000000000000000000000000018f61ec", // min amount out - "0000000000000000000000000000000000000000000000000000000000000000", // wrap - "0000000000000000000000000000000000000000000000000000000000000000", // unwrap - "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver - "0000000000000000000000000000000000000000000000000000000000000100", /* length ple - * encode */ - "00000000000000000000000000000000000000000000000000000000000000a8", - // swap 1 - "0052", // swap length - "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address - "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in - "bb2b8038a1640196fbe3e38816f3e67cba72d940", // component id - "3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver (router) - "00", // zero to one - "01", // transfer type - // swap 2 - "0052", // swap length - "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address - "2260fac5e5542a773aa44fbcfedf7c193bc2c599", // token in - "004375dff511095cc5a197a54140a24efef3a416", // component id - "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver (final user) - "01", // zero to one - "00", // transfer type - "000000000000000000000000000000000000000000000000", // padding - )); + let hex_calldata = encode(&calldata); + println!("test_sequential_swap_strategy_encoder_no_permit2: {}", hex_calldata); - assert_eq!(hex_calldata, expected); + let expected = String::from(concat!( + "e8a980d7", /* function selector */ + "0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount in + "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in + "000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token ou + "00000000000000000000000000000000000000000000000000000000018f61ec", /* min amount out */ + "0000000000000000000000000000000000000000000000000000000000000000", // wrap + "0000000000000000000000000000000000000000000000000000000000000000", // unwrap + "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver + "0000000000000000000000000000000000000000000000000000000000000100", /* length ple + * encode */ + "00000000000000000000000000000000000000000000000000000000000000a8", + // swap 1 + "0052", // swap length + "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address + "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in + "bb2b8038a1640196fbe3e38816f3e67cba72d940", // component id + "004375dff511095cc5a197a54140a24efef3a416", // receiver (next pool) + "00", // zero to one + "01", // transfer type + // swap 2 + "0052", // swap length + "5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address + "2260fac5e5542a773aa44fbcfedf7c193bc2c599", // token in + "004375dff511095cc5a197a54140a24efef3a416", // component id + "cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver (final user) + "01", // zero to one + "05", // transfer type - None + "000000000000000000000000000000000000000000000000", // padding + )); + + assert_eq!(hex_calldata, expected); + } + + #[test] + fn test_uniswap_v3_uniswap_v2() { + // Note: This test does not assert anything. It is only used to obtain integration test + // data for our router solidity test. + // + // Performs a sequential swap from WETH to USDC though WBTC using USV3 and USV2 + // pools + // + // WETH ───(USV3)──> WBTC ───(USV2)──> USDC + + let weth = weth(); + let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); + let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); + + let swap_weth_wbtc = Swap { + component: ProtocolComponent { + id: "0xCBCdF9626bC03E24f779434178A73a0B4bad62eD".to_string(), + protocol_system: "uniswap_v3".to_string(), + static_attributes: { + let mut attrs = HashMap::new(); + attrs.insert( + "fee".to_string(), + Bytes::from(BigInt::from(3000).to_signed_bytes_be()), + ); + attrs + }, + ..Default::default() + }, + token_in: weth.clone(), + token_out: wbtc.clone(), + split: 0f64, + }; + let swap_wbtc_usdc = Swap { + component: ProtocolComponent { + id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: wbtc.clone(), + token_out: usdc.clone(), + split: 0f64, + }; + let swap_encoder_registry = get_swap_encoder_registry(); + let encoder = SequentialSwapStrategyEncoder::new( + eth_chain(), + swap_encoder_registry, + None, + Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), + ) + .unwrap(); + let solution = Solution { + exact_out: false, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").unwrap(), + checked_token: usdc, + expected_amount: None, + checked_amount: Some(BigUint::from_str("26173932").unwrap()), + sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], + ..Default::default() + }; + + let (calldata, _) = encoder + .encode_strategy(solution) + .unwrap(); + + let _hex_calldata = encode(&calldata); + println!("test_uniswap_v3_uniswap_v2: {}", _hex_calldata); + } + + #[test] + fn test_uniswap_v3_uniswap_v3() { + // Note: This test does not assert anything. It is only used to obtain integration test + // data for our router solidity test. + // + // Performs a sequential swap from WETH to USDC though WBTC using USV3 pools + // There is no optimization between the two USV3 pools, this is currently not supported. + // + // WETH ───(USV3)──> WBTC ───(USV3)──> USDC + + let weth = weth(); + let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); + let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); + + let swap_weth_wbtc = Swap { + component: ProtocolComponent { + id: "0xCBCdF9626bC03E24f779434178A73a0B4bad62eD".to_string(), + protocol_system: "uniswap_v3".to_string(), + static_attributes: { + let mut attrs = HashMap::new(); + attrs.insert( + "fee".to_string(), + Bytes::from(BigInt::from(3000).to_signed_bytes_be()), + ); + attrs + }, + ..Default::default() + }, + token_in: weth.clone(), + token_out: wbtc.clone(), + split: 0f64, + }; + let swap_wbtc_usdc = Swap { + component: ProtocolComponent { + id: "0x99ac8cA7087fA4A2A1FB6357269965A2014ABc35".to_string(), + protocol_system: "uniswap_v3".to_string(), + static_attributes: { + let mut attrs = HashMap::new(); + attrs.insert( + "fee".to_string(), + Bytes::from(BigInt::from(3000).to_signed_bytes_be()), + ); + attrs + }, + ..Default::default() + }, + token_in: wbtc.clone(), + token_out: usdc.clone(), + split: 0f64, + }; + let swap_encoder_registry = get_swap_encoder_registry(); + let encoder = SequentialSwapStrategyEncoder::new( + eth_chain(), + swap_encoder_registry, + None, + Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), + ) + .unwrap(); + let solution = Solution { + exact_out: false, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").unwrap(), + checked_token: usdc, + expected_amount: None, + checked_amount: Some(BigUint::from_str("26173932").unwrap()), + sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], + ..Default::default() + }; + + let (calldata, _) = encoder + .encode_strategy(solution) + .unwrap(); + + let _hex_calldata = encode(&calldata); + println!("test_uniswap_v3_uniswap_v3: {}", _hex_calldata); + } + + #[test] + fn test_uniswap_v3_curve() { + // Note: This test does not assert anything. It is only used to obtain integration test + // data for our router solidity test. + // + // Performs a sequential swap from WETH to USDT though WBTC using USV3 and curve + // pools + // + // WETH ───(USV3)──> WBTC ───(curve)──> USDT + + let weth = weth(); + let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); + let usdt = Bytes::from_str("0xdAC17F958D2ee523a2206206994597C13D831ec7").unwrap(); + + let swap_weth_wbtc = Swap { + component: ProtocolComponent { + id: "0xCBCdF9626bC03E24f779434178A73a0B4bad62eD".to_string(), + protocol_system: "uniswap_v3".to_string(), + static_attributes: { + let mut attrs = HashMap::new(); + attrs.insert( + "fee".to_string(), + Bytes::from(BigInt::from(3000).to_signed_bytes_be()), + ); + attrs + }, + ..Default::default() + }, + token_in: weth.clone(), + token_out: wbtc.clone(), + split: 0f64, + }; + + let swap_wbtc_usdt = Swap { + component: ProtocolComponent { + id: String::from("0xD51a44d3FaE010294C616388b506AcdA1bfAAE46"), + protocol_system: String::from("vm:curve"), + static_attributes: { + let mut attrs: HashMap = HashMap::new(); + attrs.insert( + "factory".into(), + Bytes::from( + "0x0000000000000000000000000000000000000000" + .as_bytes() + .to_vec(), + ), + ); + attrs + }, + ..Default::default() + }, + token_in: wbtc.clone(), + token_out: usdt.clone(), + split: 0f64, + }; + let swap_encoder_registry = get_swap_encoder_registry(); + let encoder = SequentialSwapStrategyEncoder::new( + eth_chain(), + swap_encoder_registry, + None, + Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), + ) + .unwrap(); + let solution = Solution { + exact_out: false, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").unwrap(), + checked_token: usdt, + expected_amount: None, + checked_amount: Some(BigUint::from_str("26173932").unwrap()), + sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + swaps: vec![swap_weth_wbtc, swap_wbtc_usdt], + ..Default::default() + }; + + let (calldata, _) = encoder + .encode_strategy(solution) + .unwrap(); + + let _hex_calldata = encode(&calldata); + println!("test_uniswap_v3_curve: {}", _hex_calldata); + } + + #[test] + fn test_balancer_v2_uniswap_v2() { + // Note: This test does not assert anything. It is only used to obtain integration test + // data for our router solidity test. + // + // Performs a sequential swap from WETH to USDC though WBTC using balancer and USV2 + // pools + // + // WETH ───(balancer)──> WBTC ───(USV2)──> USDC + + let weth = weth(); + let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); + let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); + + let swap_weth_wbtc = Swap { + component: ProtocolComponent { + id: "0xa6f548df93de924d73be7d25dc02554c6bd66db500020000000000000000000e" + .to_string(), + protocol_system: "vm:balancer_v2".to_string(), + ..Default::default() + }, + token_in: weth.clone(), + token_out: wbtc.clone(), + split: 0f64, + }; + + let swap_wbtc_usdc = Swap { + component: ProtocolComponent { + id: "0x004375Dff511095CC5A197A54140a24eFEF3A416".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: wbtc.clone(), + token_out: usdc.clone(), + split: 0f64, + }; + let swap_encoder_registry = get_swap_encoder_registry(); + let encoder = SequentialSwapStrategyEncoder::new( + eth_chain(), + swap_encoder_registry, + None, + Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), + ) + .unwrap(); + let solution = Solution { + exact_out: false, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").unwrap(), + checked_token: usdc, + expected_amount: None, + checked_amount: Some(BigUint::from_str("26173932").unwrap()), + sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), + swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], + ..Default::default() + }; + + let (calldata, _) = encoder + .encode_strategy(solution) + .unwrap(); + + let _hex_calldata = encode(&calldata); + println!("test_balancer_v2_uniswap_v2: {}", _hex_calldata); + } } - #[test] fn test_split_encoding_strategy_usv4() { // Performs a sequential swap from USDC to PEPE though ETH using two consecutive USV4 pools diff --git a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs index 08ac181..9d53296 100644 --- a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs +++ b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs @@ -1,13 +1,14 @@ use tycho_common::Bytes; use crate::encoding::{ - evm::constants::{IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, PROTOCOLS_EXPECTING_FUNDS_IN_ROUTER}, + evm::constants::IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, models::{Swap, TransferType}, }; /// A trait that defines how the tokens will be transferred into the given pool given the solution. pub trait TransferOptimization { /// Returns the transfer method that should be used for the given swap and solution. + #[allow(clippy::too_many_arguments)] fn get_transfer_type( &self, swap: Swap, @@ -16,37 +17,40 @@ pub trait TransferOptimization { wrapped_token: Bytes, permit2: bool, wrap: bool, + in_between_swap_optimization: bool, ) -> TransferType { - let send_funds_to_pool: bool = + let in_transfer_optimizable: bool = IN_TRANSFER_OPTIMIZABLE_PROTOCOLS.contains(&swap.component.protocol_system.as_str()); - let funds_expected_in_router: bool = - PROTOCOLS_EXPECTING_FUNDS_IN_ROUTER.contains(&swap.component.protocol_system.as_str()); - // In the case of wrapping, check if the swap's token in is the wrapped token to - // determine if it's the first swap. Otherwise, compare to the given token. let is_first_swap = swap.token_in == given_token; if swap.token_in == native_token { // Funds are already in router. All protocols currently take care of native transfers. TransferType::None } else if (swap.token_in == wrapped_token) && wrap { + // Wrapping already happened in the router so we can just use a normal transfer. TransferType::TransferToProtocol - } else if is_first_swap && send_funds_to_pool { - if permit2 { - // Transfer from swapper to pool using permit2. - TransferType::TransferPermit2ToProtocol - } else { - // Transfer from swapper to pool. - TransferType::TransferFromToProtocol - } - } else if is_first_swap && funds_expected_in_router { - if permit2 { + // first swap + } else if is_first_swap { + if in_transfer_optimizable { + if permit2 { + // Transfer from swapper to pool using permit2. + TransferType::TransferPermit2ToProtocol + } else { + // Transfer from swapper to pool. + TransferType::TransferFromToProtocol + } + } else if permit2 { // Transfer from swapper to router using permit2. TransferType::TransferPermit2ToRouter } else { // Transfer from swapper to router. TransferType::TransferFromToRouter } + // all other swaps + } else if !in_transfer_optimizable || in_between_swap_optimization { + // funds should already be in the router or in the next pool + TransferType::None } else { TransferType::TransferToProtocol } @@ -93,7 +97,7 @@ mod tests { }; let strategy = MockStrategy {}; let transfer_method = - strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), true, false); + strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), true, false, false); assert_eq!(transfer_method, TransferType::TransferPermit2ToProtocol); } @@ -111,7 +115,7 @@ mod tests { }; let strategy = MockStrategy {}; let transfer_method = - strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), false, false); + strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), false, false, false); assert_eq!(transfer_method, TransferType::TransferFromToProtocol); } @@ -130,7 +134,7 @@ mod tests { }; let strategy = MockStrategy {}; let transfer_method = - strategy.get_transfer_type(swap.clone(), eth(), eth(), weth(), false, false); + strategy.get_transfer_type(swap.clone(), eth(), eth(), weth(), false, false, false); assert_eq!(transfer_method, TransferType::None); } @@ -149,7 +153,7 @@ mod tests { }; let strategy = MockStrategy {}; let transfer_method = - strategy.get_transfer_type(swap.clone(), eth(), eth(), weth(), false, true); + strategy.get_transfer_type(swap.clone(), eth(), eth(), weth(), false, true, false); assert_eq!(transfer_method, TransferType::TransferToProtocol); } @@ -168,7 +172,45 @@ mod tests { }; let strategy = MockStrategy {}; let transfer_method = - strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), false, false); + strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), false, false, false); assert_eq!(transfer_method, TransferType::TransferToProtocol); } + + #[test] + fn test_not_first_swap_funds_in_router() { + // Not the first swap and the protocol requires the funds to be in the router (which they + // already are, so the transfer type is None) + let swap = Swap { + component: ProtocolComponent { + protocol_system: "vm:curve".to_string(), + ..Default::default() + }, + token_in: usdc(), + token_out: dai(), + split: 0f64, + }; + let strategy = MockStrategy {}; + let transfer_method = + strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), false, false, false); + assert_eq!(transfer_method, TransferType::None); + } + + #[test] + fn test_not_first_swap_in_between_swap_optimization() { + // Not the first swap and the in between swaps are optimized. The funds should already be in + // the next pool or in the router + let swap = Swap { + component: ProtocolComponent { + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: usdc(), + token_out: dai(), + split: 0f64, + }; + let strategy = MockStrategy {}; + let transfer_method = + strategy.get_transfer_type(swap.clone(), weth(), eth(), weth(), false, false, true); + assert_eq!(transfer_method, TransferType::None); + } } From 8aa5b08b419e45de5f69c72904f939881fe912ba Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Thu, 17 Apr 2025 09:33:52 +0100 Subject: [PATCH 3/4] fix: unsupported protocols for chained swaps are always unsupported not only when the previous swap if of the same protocol! Improve docs and naming --- don't change below this line --- ENG-4314 Took 15 minutes --- src/encoding/evm/constants.rs | 11 +++++++---- .../evm/strategy_encoder/strategy_encoders.rs | 7 +++---- .../evm/strategy_encoder/transfer_optimizations.rs | 1 - 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/encoding/evm/constants.rs b/src/encoding/evm/constants.rs index c2962f7..09da9f9 100644 --- a/src/encoding/evm/constants.rs +++ b/src/encoding/evm/constants.rs @@ -19,6 +19,7 @@ pub static GROUPABLE_PROTOCOLS: LazyLock> = LazyLock::new( }); /// These protocols support the optimization of transferring straight from the user. +/// Any protocols that are not defined here expect funds to be in the router at the time of swap. pub static IN_TRANSFER_OPTIMIZABLE_PROTOCOLS: LazyLock> = LazyLock::new(|| { let mut set = HashSet::new(); @@ -31,10 +32,12 @@ pub static IN_TRANSFER_OPTIMIZABLE_PROTOCOLS: LazyLock> = set }); -// These protocols do not support chained swaps from the same protocol. This is the case for uniswap -// v3 because of the callback logic. The only way for this to work it would be to call the second -// swap during the callback of the first swap. This is currently not supported. -pub static PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS: LazyLock> = +// These protocols do not support chained swaps. The tokens can not be sent directly from the +// previous pool into a pool of this protocol. The tokens need to be sent to the router and only +// then transferred into the pool. This is the case for uniswap v3 because of the callback logic. +// The only way for this to work it would be to call the second swap during the callback of the +// first swap. This is currently not supported. +pub static UNSUPPORTED_PROTOCOLS_FOR_CHAINED_SWAPS: LazyLock> = LazyLock::new(|| { let mut set = HashSet::new(); set.insert("uniswap_v3"); diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index ceb11d4..89a5f66 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -8,7 +8,7 @@ use crate::encoding::{ errors::EncodingError, evm::{ approvals::permit2::Permit2, - constants::{IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS}, + constants::{IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, UNSUPPORTED_PROTOCOLS_FOR_CHAINED_SWAPS}, group_swaps::group_swaps, strategy_encoder::{ strategy_validators::{SequentialSwapValidator, SplitSwapValidator, SwapValidator}, @@ -311,9 +311,8 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { if IN_TRANSFER_OPTIMIZABLE_PROTOCOLS.contains(&next.protocol_system.as_str()) { // if the protocol does not allow for chained swaps, we can't optimize the // receiver of this swap nor the transfer in of the next swap - if PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS - .contains(&next.protocol_system.as_str()) && - PROTOCOLS_NOT_OPTIMIZED_CHAINED_SWAPS.contains(protocol.as_str()) + if UNSUPPORTED_PROTOCOLS_FOR_CHAINED_SWAPS + .contains(&next.protocol_system.as_str()) { next_in_between_swap_optimization = false; self.router_address.clone() diff --git a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs index 9d53296..bb72ad0 100644 --- a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs +++ b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs @@ -30,7 +30,6 @@ pub trait TransferOptimization { } else if (swap.token_in == wrapped_token) && wrap { // Wrapping already happened in the router so we can just use a normal transfer. TransferType::TransferToProtocol - // first swap } else if is_first_swap { if in_transfer_optimizable { if permit2 { From 244b7d3482da7bd99e5b78b325b135d2adce09f7 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Thu, 17 Apr 2025 15:51:01 +0100 Subject: [PATCH 4/4] fix: Rename constants and update docstrings for clarity --- don't change below this line --- ENG-4314 Took 44 minutes Took 9 seconds --- src/encoding/evm/constants.rs | 49 +++++++++---------- .../evm/strategy_encoder/strategy_encoders.rs | 8 ++- .../transfer_optimizations.rs | 10 ++-- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/encoding/evm/constants.rs b/src/encoding/evm/constants.rs index 09da9f9..1ac6bb3 100644 --- a/src/encoding/evm/constants.rs +++ b/src/encoding/evm/constants.rs @@ -18,29 +18,28 @@ pub static GROUPABLE_PROTOCOLS: LazyLock> = LazyLock::new( set }); -/// These protocols support the optimization of transferring straight from the user. -/// Any protocols that are not defined here expect funds to be in the router at the time of swap. -pub static IN_TRANSFER_OPTIMIZABLE_PROTOCOLS: LazyLock> = - LazyLock::new(|| { - let mut set = HashSet::new(); - set.insert("uniswap_v2"); - set.insert("sushiswap_v2"); - set.insert("pancakeswap_v2"); - set.insert("uniswap_v3"); - set.insert("pancakeswap_v3"); - set.insert("ekubo_v2"); - set - }); +/// These protocols need an external in transfer to the pool. This transfer can be from the router, +/// from the user or from the previous pool. Any protocols that are not defined here expect funds to +/// be in the router at the time of swap and do the transfer themselves from msg.sender +pub static IN_TRANSFER_REQUIRED_PROTOCOLS: LazyLock> = LazyLock::new(|| { + let mut set = HashSet::new(); + set.insert("uniswap_v2"); + set.insert("sushiswap_v2"); + set.insert("pancakeswap_v2"); + set.insert("uniswap_v3"); + set.insert("pancakeswap_v3"); + set.insert("ekubo_v2"); + set +}); -// These protocols do not support chained swaps. The tokens can not be sent directly from the -// previous pool into a pool of this protocol. The tokens need to be sent to the router and only -// then transferred into the pool. This is the case for uniswap v3 because of the callback logic. -// The only way for this to work it would be to call the second swap during the callback of the -// first swap. This is currently not supported. -pub static UNSUPPORTED_PROTOCOLS_FOR_CHAINED_SWAPS: LazyLock> = - LazyLock::new(|| { - let mut set = HashSet::new(); - set.insert("uniswap_v3"); - set.insert("pancakeswap_v3"); - set - }); +// The protocols here are a subset of the ones defined in IN_TRANSFER_REQUIRED_PROTOCOLS. The tokens +// can not be sent directly from the previous pool into a pool of this protocol. The tokens need to +// be sent to the router and only then transferred into the pool. This is the case for uniswap v3 +// because of the callback logic. The only way for this to work it would be to call the second swap +// during the callback of the first swap. This is currently not supported. +pub static CALLBACK_CONSTRAINED_PROTOCOLS: LazyLock> = LazyLock::new(|| { + let mut set = HashSet::new(); + set.insert("uniswap_v3"); + set.insert("pancakeswap_v3"); + set +}); diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 89a5f66..85c187d 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -8,7 +8,7 @@ use crate::encoding::{ errors::EncodingError, evm::{ approvals::permit2::Permit2, - constants::{IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, UNSUPPORTED_PROTOCOLS_FOR_CHAINED_SWAPS}, + constants::{CALLBACK_CONSTRAINED_PROTOCOLS, IN_TRANSFER_REQUIRED_PROTOCOLS}, group_swaps::group_swaps, strategy_encoder::{ strategy_validators::{SequentialSwapValidator, SplitSwapValidator, SwapValidator}, @@ -308,12 +308,10 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { // if there is a next swap let swap_receiver = if let Some(next) = next_swap { // if the protocol of the next swap supports transfer in optimization - if IN_TRANSFER_OPTIMIZABLE_PROTOCOLS.contains(&next.protocol_system.as_str()) { + if IN_TRANSFER_REQUIRED_PROTOCOLS.contains(&next.protocol_system.as_str()) { // if the protocol does not allow for chained swaps, we can't optimize the // receiver of this swap nor the transfer in of the next swap - if UNSUPPORTED_PROTOCOLS_FOR_CHAINED_SWAPS - .contains(&next.protocol_system.as_str()) - { + if CALLBACK_CONSTRAINED_PROTOCOLS.contains(&next.protocol_system.as_str()) { next_in_between_swap_optimization = false; self.router_address.clone() } else { diff --git a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs index bb72ad0..97e08fc 100644 --- a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs +++ b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs @@ -1,7 +1,7 @@ use tycho_common::Bytes; use crate::encoding::{ - evm::constants::IN_TRANSFER_OPTIMIZABLE_PROTOCOLS, + evm::constants::IN_TRANSFER_REQUIRED_PROTOCOLS, models::{Swap, TransferType}, }; @@ -19,8 +19,8 @@ pub trait TransferOptimization { wrap: bool, in_between_swap_optimization: bool, ) -> TransferType { - let in_transfer_optimizable: bool = - IN_TRANSFER_OPTIMIZABLE_PROTOCOLS.contains(&swap.component.protocol_system.as_str()); + let in_transfer_required: bool = + IN_TRANSFER_REQUIRED_PROTOCOLS.contains(&swap.component.protocol_system.as_str()); let is_first_swap = swap.token_in == given_token; @@ -31,7 +31,7 @@ pub trait TransferOptimization { // Wrapping already happened in the router so we can just use a normal transfer. TransferType::TransferToProtocol } else if is_first_swap { - if in_transfer_optimizable { + if in_transfer_required { if permit2 { // Transfer from swapper to pool using permit2. TransferType::TransferPermit2ToProtocol @@ -47,7 +47,7 @@ pub trait TransferOptimization { TransferType::TransferFromToRouter } // all other swaps - } else if !in_transfer_optimizable || in_between_swap_optimization { + } else if !in_transfer_required || in_between_swap_optimization { // funds should already be in the router or in the next pool TransferType::None } else {