feat: Add SequentialSwap integration test with regular approvals

Improve docstrings

--- don't change below this line ---
ENG-4306 Took 20 minutes
This commit is contained in:
Diana Carvalho
2025-04-04 16:48:20 +01:00
parent 2f81b167d4
commit 6e7bf3c019
2 changed files with 91 additions and 31 deletions

View File

@@ -5,9 +5,6 @@ import "./TychoRouterTestSetup.sol";
contract TychoRouterTestIntegration is TychoRouterTestSetup { contract TychoRouterTestIntegration is TychoRouterTestSetup {
function testSplitSwapSingleIntegration() public { function testSplitSwapSingleIntegration() public {
// Test created with calldata from our router encoder, replacing the executor
// address with the USV2 executor address.
// Tests swapping WETH -> DAI on a USV2 pool // Tests swapping WETH -> DAI on a USV2 pool
deal(WETH_ADDR, ALICE, 1 ether); deal(WETH_ADDR, ALICE, 1 ether);
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE); uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
@@ -29,9 +26,6 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup {
} }
function testSplitSwapSingleWithoutPermit2Integration() public { function testSplitSwapSingleWithoutPermit2Integration() public {
// Test created with calldata from our router encoder, replacing the executor
// address with the USV2 executor address.
// Tests swapping WETH -> DAI on a USV2 pool without permit2 // Tests swapping WETH -> DAI on a USV2 pool without permit2
deal(WETH_ADDR, ALICE, 1 ether); deal(WETH_ADDR, ALICE, 1 ether);
vm.startPrank(ALICE); vm.startPrank(ALICE);
@@ -127,9 +121,6 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup {
} }
function testSplitSwapSingleWithWrapIntegration() public { function testSplitSwapSingleWithWrapIntegration() public {
// Test created with calldata from our router encoder, replacing the executor
// address with the USV2 executor address.
// Tests swapping WETH -> DAI on a USV2 pool, but ETH is received from the user // Tests swapping WETH -> DAI on a USV2 pool, but ETH is received from the user
// and wrapped before the swap // and wrapped before the swap
deal(ALICE, 1 ether); deal(ALICE, 1 ether);
@@ -151,9 +142,6 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup {
} }
function testSplitSwapSingleWithUnwrapIntegration() public { function testSplitSwapSingleWithUnwrapIntegration() public {
// Test created with calldata from our router encoder, replacing the executor
// address with the USV2 executor address.
// Tests swapping DAI -> WETH on a USV2 pool, and WETH is unwrapped to ETH // Tests swapping DAI -> WETH on a USV2 pool, and WETH is unwrapped to ETH
// before sending back to the user // before sending back to the user
deal(DAI_ADDR, ALICE, 3000 ether); deal(DAI_ADDR, ALICE, 3000 ether);
@@ -206,16 +194,10 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup {
assertTrue(success, "Call Failed"); assertTrue(success, "Call Failed");
assertGe(balancerAfter - balancerBefore, 26173932); assertGe(balancerAfter - balancerBefore, 26173932);
// All input tokens are transferred to the router at first. Make sure we used
// all of it (and thus our splits are correct).
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
} }
function testSplitSwapIntegration() public { function testSplitSwapIntegration() public {
// Test created with calldata from our router encoder, replacing the executor
// address with the USV2 executor address.
// Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools // Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools
// //
// ┌──(USV2)──> WBTC ───(USV2)──> USDC // ┌──(USV2)──> WBTC ───(USV2)──> USDC
@@ -244,10 +226,7 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup {
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
} }
function testSequentialSwapIntegration() public { function testSequentialSwapIntegrationPermit2() public {
// Test created with calldata from our router encoder, replacing the executor
// address with the USV2 executor address.
// Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools // Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools
// //
// WETH ──(USV2)──> WBTC ───(USV2)──> USDC // WETH ──(USV2)──> WBTC ───(USV2)──> USDC
@@ -268,9 +247,30 @@ contract TychoRouterTestIntegration is TychoRouterTestSetup {
assertTrue(success, "Call Failed"); assertTrue(success, "Call Failed");
assertEq(balancerAfter - balancerBefore, 2552915143); assertEq(balancerAfter - balancerBefore, 2552915143);
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
}
// All input tokens are transferred to the router at first. Make sure we used function testSequentialSwapIntegration() public {
// all of it (and thus our splits are correct). // Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools
//
// WETH ──(USV2)──> WBTC ───(USV2)──> USDC
deal(WETH_ADDR, ALICE, 1 ether);
uint256 balancerBefore = 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"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000a600515615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d013950000515615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a4163ede3eca2a72b3aecc820e955b36f38437d01395010000000000000000000000000000000000000000000000000000"
);
vm.stopPrank();
uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE);
assertTrue(success, "Call Failed");
assertEq(balancerAfter - balancerBefore, 2552915143);
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0); assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
} }

View File

@@ -83,7 +83,7 @@ impl SingleSwapStrategyEncoder {
Ok(Self { permit2, selector, swap_encoder_registry, router_address }) Ok(Self { permit2, selector, swap_encoder_registry, router_address })
} }
/// Encodes information necessary for performing a single swap against a given executor for /// Encodes information necessary for performing a single hop against a given executor for
/// a protocol. /// a protocol.
fn encode_swap_header(&self, executor_address: Bytes, protocol_data: Vec<u8>) -> Vec<u8> { fn encode_swap_header(&self, executor_address: Bytes, protocol_data: Vec<u8>) -> Vec<u8> {
let mut encoded = Vec::new(); let mut encoded = Vec::new();
@@ -202,7 +202,7 @@ impl StrategyEncoder for SingleSwapStrategyEncoder {
} }
} }
/// Represents the encoder for a swap strategy which supports single swaps. /// Represents the encoder for a swap strategy which supports sequential swaps.
/// ///
/// # Fields /// # Fields
/// * `swap_encoder_registry`: SwapEncoderRegistry, containing all possible swap encoders /// * `swap_encoder_registry`: SwapEncoderRegistry, containing all possible swap encoders
@@ -253,7 +253,7 @@ impl SequentialSwapStrategyEncoder {
}) })
} }
/// Encodes information necessary for performing a single swap against a given executor for /// Encodes information necessary for performing a single hop against a given executor for
/// a protocol. /// a protocol.
fn encode_swap_header(&self, executor_address: Bytes, protocol_data: Vec<u8>) -> Vec<u8> { fn encode_swap_header(&self, executor_address: Bytes, protocol_data: Vec<u8>) -> Vec<u8> {
let mut encoded = Vec::new(); let mut encoded = Vec::new();
@@ -373,7 +373,7 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder {
} }
} }
/// Represents the encoder for a swap strategy which supports single, sequential and split swaps. /// Represents the encoder for a swap strategy which supports split swaps.
/// ///
/// # Fields /// # Fields
/// * `swap_encoder_registry`: SwapEncoderRegistry, containing all possible swap encoders /// * `swap_encoder_registry`: SwapEncoderRegistry, containing all possible swap encoders
@@ -439,7 +439,7 @@ impl SplitSwapStrategyEncoder {
}) })
} }
/// Encodes information necessary for performing a single swap against a given executor for /// Encodes information necessary for performing a single hop against a given executor for
/// a protocol as part of a split swap solution. /// a protocol as part of a split swap solution.
fn encode_swap_header( fn encode_swap_header(
&self, &self,
@@ -1416,8 +1416,6 @@ mod tests {
}, },
token_in: weth.clone(), token_in: weth.clone(),
token_out: wbtc.clone(), token_out: wbtc.clone(),
// This represents the remaining 50%, but to avoid any rounding errors we set this to
// 0 to signify "the remainder of the WETH value". It should still be very close to 50%
split: 0f64, split: 0f64,
}; };
let swap_wbtc_usdc = Swap { let swap_wbtc_usdc = Swap {
@@ -1459,6 +1457,68 @@ mod tests {
println!("{}", _hex_calldata); println!("{}", _hex_calldata);
} }
#[test]
fn test_sequential_swap_strategy_encoder_no_permit2() {
// 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
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 (calldata, _) = encoder
.encode_strategy(solution)
.unwrap();
let _hex_calldata = encode(&calldata);
println!("{}", _hex_calldata);
}
#[test] #[test]
fn test_split_encoding_strategy_usv4() { fn test_split_encoding_strategy_usv4() {
// Performs a sequential swap from USDC to PEPE though ETH using two consecutive USV4 pools // Performs a sequential swap from USDC to PEPE though ETH using two consecutive USV4 pools