From 27c9c53889687b890bb4e4e01329f6a67ae7957c Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Thu, 13 Mar 2025 17:34:50 +0000 Subject: [PATCH] feat: Add check to don't support cyclical swaps with native actions Remove unnecessary tests and improve comments --- don't change below this line --- ENG-4331 Took 30 minutes --- foundry/test/TychoRouter.t.sol | 18 --- .../evm/strategy_encoder/strategy_encoders.rs | 135 ------------------ src/encoding/evm/tycho_encoder.rs | 83 ++++++++++- 3 files changed, 76 insertions(+), 160 deletions(-) diff --git a/foundry/test/TychoRouter.t.sol b/foundry/test/TychoRouter.t.sol index 129e903..3801a2e 100644 --- a/foundry/test/TychoRouter.t.sol +++ b/foundry/test/TychoRouter.t.sol @@ -1389,24 +1389,6 @@ contract TychoRouterTest is TychoRouterTestSetup { vm.stopPrank(); } - function testCyclicSwapUnwrapOutputIntegration() public { - vm.deal(tychoRouterAddr, 1 ether); - - vm.startPrank(ALICE); - - // Encoded solution generated using `test_cylic_swap_unwrap_output` - // but manually replacing the executor addresses with the ones in this test - // `dd8559c917393fc8dd2b4dd289c52ff445fde1b0` to `2e234dae75c793f67a35089c9d99245e1c58470b` - // `042c0ebbeab9d9987c2f64ee05f2b3aeb86eaf70` to `f62849f9a0b5bf2913b396098f7c7019b51a820a` - (bool success,) = tychoRouterAddr.call( - hex"d499aa880000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067f7d35e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067d04d66000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041043569db60af05e3bbc0e4fcff373ffeeacc0fa5bbbe322f171becf366be7f1c4d31d26e0b2952b4d7bf09ddc9902bbb2758870ce36b737286f76a2006fcfc861b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e100700001000000f62849f9a0b5bf2913b396098f7c7019b51a820a00000000000000000000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c59901f62849f9a0b5bf2913b396098f7c7019b51a820a2260fac5e5542a773aa44fbcfedf7c193bc2c599000bb800003c006d01020000002e234dae75c793f67a35089c9d99245e1c58470b2260fac5e5542a773aa44fbcfedf7c193bc2c599c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000bb83ede3eca2a72b3aecc820e955b36f38437d01395cbcdf9626bc03e24f779434178a73a0b4bad62ed0100000000000000000000000000000000000000000000000000000000000000" - ); - - assertEq(ALICE.balance, 993164318934741987); // 0.993164318934741987 ETH - - vm.stopPrank(); - } - // Base Network Tests // Make sure to set the RPC_URL to base network function testSwapSingleBase() public { diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 144b5d5..92d1af8 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -1661,139 +1661,4 @@ mod tests { assert_eq!(hex_calldata[..520], expected_input); assert_eq!(hex_calldata[1288..], expected_swaps); } - - #[test] - fn test_cylic_swap_unwrap_output() { - // This test has start and end tokens that are the same - // The flow is: - // ETH -> WBTC -> WETH(unwrap operation) -> ETH - - // Set up a mock private key for signing (Alice's pk in our router tests) - let private_key = - "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string(); - - let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(); - let wbtc = Bytes::from_str("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599").unwrap(); - - // ETH -> WBTC (Uniswap V4) - let swap_eth_wbtc = Swap { - component: ProtocolComponent { - id: "0x54c72c46df32f2cc455e84e41e191b26ed73a29452cdd3d82f511097af9f427e" - .to_string(), - protocol_system: "uniswap_v4".to_string(), - static_attributes: { - let mut attrs = HashMap::new(); - attrs.insert( - "key_lp_fee".to_string(), - Bytes::from(BigInt::from(3000).to_signed_bytes_be()), - ); - attrs.insert( - "tick_spacing".to_string(), - Bytes::from(BigInt::from(60).to_signed_bytes_be()), - ); - attrs - }, - ..Default::default() - }, - token_in: eth(), - token_out: wbtc.clone(), - split: 0f64, - }; - - // WBTC -> WETH (Uniswap V3) - let swap_wbtc_weth = 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: wbtc.clone(), - token_out: weth.clone(), - split: 0f64, - }; - - let swap_encoder_registry = get_swap_encoder_registry(); - let encoder = - SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key)) - .unwrap(); - - let solution = Solution { - exact_out: false, - given_token: eth(), - given_amount: BigUint::from_str("1000000000000000000").unwrap(), // 1 WETH - checked_token: eth(), - expected_amount: None, - checked_amount: Some(BigUint::from_str("993164318934741987").unwrap()), /* Expected output - * from - * test */ - slippage: None, - swaps: vec![swap_eth_wbtc, swap_wbtc_weth], - router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(), - sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), - receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), - native_action: Some(NativeAction::Unwrap), - }; - - let (calldata, _) = encoder - .encode_strategy(solution) - .unwrap(); - - let hex_calldata = hex::encode(&calldata); - - let expected_input = [ - "d499aa88", // selector - "0000000000000000000000000000000000000000000000000de0b6b3a7640000", // given amount - "0000000000000000000000000000000000000000000000000000000000000000", // given token - "0000000000000000000000000000000000000000000000000000000000000000", // checked token - "0000000000000000000000000000000000000000000000000dc86dafa1b2b3e3", // min amount out - "0000000000000000000000000000000000000000000000000000000000000000", // wrap action - "0000000000000000000000000000000000000000000000000000000000000001", // unwrap action - "0000000000000000000000000000000000000000000000000000000000000003", // tokens length - "000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver - ] - .join(""); - - let expected_swaps = [ - "00000000000000000000000000000000000000000000000000000000000000e1", // length of ple encoded swaps without padding - "0070", // ple encoded swaps - "00", // token in index - "01", // token out index - "000000", // split - "042c0ebbeab9d9987c2f64ee05f2b3aeb86eaf70", // executor address - "0000000000000000000000000000000000000000", // token in - "2260fac5e5542a773aa44fbcfedf7c193bc2c599", // intermediary token - "01", // zero2one - "042c0ebbeab9d9987c2f64ee05f2b3aeb86eaf70", // executor address - // Pool params - "2260fac5e5542a773aa44fbcfedf7c193bc2c599", // intermediary token - "000bb8", // pool fee - "00003c", // tick spacing - // Next swap - "006d", // ple encoded swaps - "01", // token in index - "02", // token out index - "000000", // split - "dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address - "2260fac5e5542a773aa44fbcfedf7c193bc2c599", // token in - "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out - "000bb8", // pool fee - "3ede3eca2a72b3aecc820e955b36f38437d01395", // router address - "cbcdf9626bc03e24f779434178a73a0b4bad62ed", // component id - "01", // zero2one - "00000000000000000000000000000000000000000000000000000000000000" // padding - ] - .join(""); - - assert_eq!(hex_calldata[..520], expected_input); - assert_eq!(hex_calldata[1288..], expected_swaps); - println!("{}", hex_calldata); - } } diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index 9283c11..f69d8a3 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -105,10 +105,8 @@ impl EVMTychoEncoder { split_tokens_already_considered.insert(swap.token_in.clone()); } } else { - // it's the last swap of the split - if split_tokens_already_considered.contains(&swap.token_in) { - split_tokens_already_considered.remove(&swap.token_in); - } else { + // it might be the last swap of the split or a regular swap + if !split_tokens_already_considered.contains(&swap.token_in) { solution_tokens.push(swap.token_in.clone()); } } @@ -129,6 +127,14 @@ impl EVMTychoEncoder { return Err(EncodingError::FatalError( "Cyclical swaps are only allowed if they are the first and last token of a solution".to_string(), )); + } else { + // it is a valid cyclical swap + // we don't support any wrapping or unwrapping in this case + if let Some(_native_action) = solution.clone().native_action { + return Err(EncodingError::FatalError( + "Wrapping/Unwrapping is not available in cyclical swaps".to_string(), + )); + } } } } @@ -488,9 +494,10 @@ mod tests { #[test] fn test_validate_cyclical_swap() { // This validation passes because the cyclical swap is the first and last token - // 50% -> WETH - // DAI -> DAI - // 50% -> WETH + // 50% -> WETH + // DAI - -> DAI + // 50% -> WETH + // (some of the pool addresses in this test are fake) let encoder = get_mocked_tycho_encoder(); let swaps = vec![ Swap { @@ -605,4 +612,66 @@ mod tests { ) ); } + #[test] + fn test_validate_cyclical_swap_split_native_action_fail() { + // This validation fails because there is a native action with a valid cyclical swap + // -> WETH + // ETH -> WETH -> DAI + // -> WETH + // (some of the pool addresses in this test are fake) + let encoder = get_mocked_tycho_encoder(); + let swaps = vec![ + Swap { + component: ProtocolComponent { + id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: weth(), + token_out: dai(), + split: 0f64, + }, + Swap { + component: ProtocolComponent { + id: "0x0000000000000000000000000000000000000000".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: dai(), + token_out: weth(), + split: 0.5f64, + }, + Swap { + component: ProtocolComponent { + id: "0x0000000000000000000000000000000000000000".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: dai(), + token_out: weth(), + split: 0f64, + }, + ]; + + let solution = Solution { + exact_out: false, + given_token: eth(), + checked_token: weth(), + swaps, + native_action: Some(NativeAction::Wrap), + ..Default::default() + }; + + let result = encoder.validate_solution(&solution); + + assert!(result.is_err()); + assert_eq!( + result.err().unwrap(), + EncodingError::FatalError( + "Wrapping/Unwrapping is not available in cyclical swaps" + .to_string() + .to_string() + ) + ); + } }