test: add testCyclicSplitSwapIntegration

This commit is contained in:
royvardhan
2025-03-10 21:52:14 +05:30
parent 79d6c020fa
commit 0a0101cc4d
2 changed files with 126 additions and 1 deletions

View File

@@ -1303,5 +1303,113 @@ mod tests {
}
#[test]
fn test_split_input_cyclic_swap() {
// This test has start and end tokens that are the same
// The flow is:
// ┌─── WETH (USV3 Pool 1) ───┐
// │ │
// USDC (60% split) ─┤ ├─> USDC
// │ │
// └─── WETH (USV3 Pool 2) ───┘
// 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 usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
// USDC -> WETH (Pool 1) - 60% of input
let swap_usdc_weth_pool1 = Swap {
component: ProtocolComponent {
id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(), /* USDC-WETH USV3
* Pool 1 */
protocol_system: "uniswap_v3".to_string(),
static_attributes: {
let mut attrs = HashMap::new();
attrs.insert(
"fee".to_string(),
Bytes::from(BigInt::from(500).to_signed_bytes_be()),
);
attrs
},
..Default::default()
},
token_in: usdc.clone(),
token_out: weth.clone(),
split: 0.6f64, // 60% of input
};
// USDC -> WETH (Pool 2) - 40% of input (remaining)
let swap_usdc_weth_pool2 = Swap {
component: ProtocolComponent {
id: "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".to_string(), /* USDC-WETH USV3
* Pool 2 */
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: usdc.clone(),
token_out: weth.clone(),
split: 0f64, // Remaining 40%
};
// WETH -> USDC (Pool 2)
let swap_weth_usdc_pool2 = Swap {
component: ProtocolComponent {
id: "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8".to_string(), /* USDC-WETH USV3
* Pool 2 */
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: usdc.clone(),
split: 0.0f64,
};
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SplitSwapStrategyEncoder::new(
eth_chain(),
swap_encoder_registry,
Some(private_key.clone()),
)
.unwrap();
let solution = Solution {
exact_out: false,
given_token: usdc.clone(),
given_amount: BigUint::from_str("100000000").unwrap(), // 100 USDC (6 decimals)
checked_token: usdc.clone(),
expected_amount: None,
checked_amount: Some(BigUint::from_str("99345512").unwrap()), /* Expected output from
* test */
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
slippage: None,
swaps: vec![swap_usdc_weth_pool1, swap_weth_usdc_pool2, swap_usdc_weth_pool2],
..Default::default()
};
let (calldata, _) = encoder
.encode_strategy(solution)
.unwrap();
println!("{}", hex::encode(&calldata));
}
}