From eb4ce40f6e0e2e1807201bc90e68f32f87157a7e Mon Sep 17 00:00:00 2001 From: royvardhan Date: Tue, 11 Mar 2025 20:31:37 +0530 Subject: [PATCH] test: add testCyclicSwapUnwrapOutputIntegration in encoder and testCyclicSwapUnwrapOutputIntegration test --- foundry/test/TychoRouter.t.sol | 19 ++++ .../evm/strategy_encoder/strategy_encoders.rs | 90 ++++++++++++++++++- 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/foundry/test/TychoRouter.t.sol b/foundry/test/TychoRouter.t.sol index 3801a2e..718e4b5 100644 --- a/foundry/test/TychoRouter.t.sol +++ b/foundry/test/TychoRouter.t.sol @@ -1389,6 +1389,25 @@ 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 91db180..1c1fca7 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -1421,7 +1421,7 @@ mod tests { // │ │ // USDC ──(USV2) ── WETH──| ├─> USDC // │ │ - // └─── WETH (USV3 Pool 2)───┘ + // └─── (USV3, 40% split) ───┘ // Set up a mock private key for signing (Alice's pk in our router tests) let private_key = @@ -1519,4 +1519,92 @@ mod tests { println!("{}", hex::encode(&calldata)); } + + #[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), + ..Default::default() + }; + + let (calldata, _) = encoder + .encode_strategy(solution) + .unwrap(); + + println!("{}", hex::encode(&calldata)); + } }