Merge remote-tracking branch 'origin/main' into ekubo
This commit is contained in:
@@ -206,6 +206,11 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
}
|
||||
|
||||
let encoded_swaps = self.ple_encode(swaps);
|
||||
let tokens_len = if solution.given_token == solution.checked_token {
|
||||
tokens.len() - 1
|
||||
} else {
|
||||
tokens.len()
|
||||
};
|
||||
let method_calldata = if let Some(permit2) = self.permit2.clone() {
|
||||
let (permit, signature) = permit2.get_permit(
|
||||
&solution.router_address,
|
||||
@@ -220,7 +225,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
biguint_to_u256(&min_amount_out),
|
||||
wrap,
|
||||
unwrap,
|
||||
U256::from(tokens.len()),
|
||||
U256::from(tokens_len),
|
||||
bytes_to_address(&solution.receiver)?,
|
||||
permit,
|
||||
signature.as_bytes().to_vec(),
|
||||
@@ -235,7 +240,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
biguint_to_u256(&min_amount_out),
|
||||
wrap,
|
||||
unwrap,
|
||||
U256::from(tokens.len()),
|
||||
U256::from(tokens_len),
|
||||
bytes_to_address(&solution.receiver)?,
|
||||
encoded_swaps,
|
||||
)
|
||||
@@ -1212,4 +1217,448 @@ mod tests {
|
||||
let hex_calldata = encode(&calldata);
|
||||
println!("{}", hex_calldata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cyclic_sequential_swap() {
|
||||
// This test has start and end tokens that are the same
|
||||
// The flow is:
|
||||
// USDC -> WETH -> USDC using two pools
|
||||
|
||||
// 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();
|
||||
|
||||
// Create two Uniswap V3 pools for the cyclic swap
|
||||
// USDC -> WETH (Pool 1)
|
||||
let swap_usdc_weth = 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: 0f64,
|
||||
};
|
||||
|
||||
// WETH -> USDC (Pool 2)
|
||||
let swap_weth_usdc = 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: 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: 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("99889294").unwrap()), /* Expected output from
|
||||
* test */
|
||||
slippage: None,
|
||||
swaps: vec![swap_usdc_weth, swap_weth_usdc],
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
let hex_calldata = hex::encode(&calldata);
|
||||
let expected_input = [
|
||||
"d499aa88", // selector
|
||||
"0000000000000000000000000000000000000000000000000000000005f5e100", // given amount
|
||||
"000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // given token
|
||||
"000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // checked token
|
||||
"0000000000000000000000000000000000000000000000000000000005f4308e", // min amount out
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // wrap action
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // unwrap action
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // tokens length
|
||||
"000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
|
||||
]
|
||||
.join("");
|
||||
|
||||
let expected_swaps = [
|
||||
"00000000000000000000000000000000000000000000000000000000000000de", // length of ple encoded swaps without padding
|
||||
"006d", // ple encoded swaps
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
|
||||
"0001f4", // pool fee
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
|
||||
"01", // zero2one
|
||||
"006d", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
|
||||
"000bb8", // pool fee
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"8ad599c3a0ff1de082011efddc58f1908eb6e6d8", // component id
|
||||
"000000", // zero2one
|
||||
]
|
||||
.join("");
|
||||
|
||||
assert_eq!(hex_calldata[..520], expected_input);
|
||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_input_cyclic_swap() {
|
||||
// This test has start and end tokens that are the same
|
||||
// The flow is:
|
||||
// ┌─ (USV3, 60% split) ──> WETH ─┐
|
||||
// │ │
|
||||
// USDC ──────┤ ├──(USV2)──> USDC
|
||||
// │ │
|
||||
// └─ (USV3, 40% split) ──> WETH ─┘
|
||||
|
||||
// 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: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(), /* USDC-WETH USV2
|
||||
* Pool 2 */
|
||||
protocol_system: "uniswap_v2".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("99574171").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_usdc_weth_pool2, swap_weth_usdc_pool2],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
|
||||
let hex_calldata = hex::encode(&calldata);
|
||||
let expected_input = [
|
||||
"d499aa88", // selector
|
||||
"0000000000000000000000000000000000000000000000000000000005f5e100", // given amount
|
||||
"000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // given token
|
||||
"000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // checked token
|
||||
"0000000000000000000000000000000000000000000000000000000005ef619b", // min amount out
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // wrap action
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // unwrap action
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // tokens length
|
||||
"000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
|
||||
]
|
||||
.join("");
|
||||
let expected_swaps = [
|
||||
"0000000000000000000000000000000000000000000000000000000000000136", // length of ple encoded swaps without padding
|
||||
"006d", // ple encoded swaps
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"999999", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
|
||||
"0001f4", // pool fee
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
|
||||
"01", // zero2one
|
||||
"006d", // ple encoded swaps
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
|
||||
"000bb8", // pool fee
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"8ad599c3a0ff1de082011efddc58f1908eb6e6d8", // component id
|
||||
"01", // zero2one
|
||||
"0056", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
"000000", // split
|
||||
"f6c5be66fff9dc69962d73da0a617a827c382329", // executor address,
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"b4e16d0168e52d35cacd2c6185b44281ec28c9dc", // component id,
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"00", // zero2one
|
||||
"00000000000000000000" // padding
|
||||
]
|
||||
.join("");
|
||||
assert_eq!(hex_calldata[..520], expected_input);
|
||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_output_cyclic_swap() {
|
||||
// This test has start and end tokens that are the same
|
||||
// The flow is:
|
||||
// ┌─── (USV3, 60% split) ───┐
|
||||
// │ │
|
||||
// USDC ──(USV2) ── WETH──| ├─> USDC
|
||||
// │ │
|
||||
// └─── (USV3, 40% split) ───┘
|
||||
|
||||
// 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();
|
||||
|
||||
let swap_usdc_weth_v2 = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(), // USDC-WETH USV2
|
||||
protocol_system: "uniswap_v2".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.0f64,
|
||||
};
|
||||
|
||||
let swap_weth_usdc_v3_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: weth.clone(),
|
||||
token_out: usdc.clone(),
|
||||
split: 0.6f64,
|
||||
};
|
||||
|
||||
let swap_weth_usdc_v3_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("99525908").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_v2, swap_weth_usdc_v3_pool1, swap_weth_usdc_v3_pool2],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
|
||||
let hex_calldata = hex::encode(&calldata);
|
||||
let expected_input = [
|
||||
"d499aa88", // selector
|
||||
"0000000000000000000000000000000000000000000000000000000005f5e100", // given amount
|
||||
"000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // given token
|
||||
"000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // checked token
|
||||
"0000000000000000000000000000000000000000000000000000000005eea514", // min amount out
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // wrap action
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // unwrap action
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // tokens length
|
||||
"000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
|
||||
]
|
||||
.join("");
|
||||
|
||||
let expected_swaps = [
|
||||
"0000000000000000000000000000000000000000000000000000000000000136", // length of ple encoded swaps without padding
|
||||
"0056", // ple encoded swaps
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
"f6c5be66fff9dc69962d73da0a617a827c382329", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"b4e16d0168e52d35cacd2c6185b44281ec28c9dc", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"01", // zero2one
|
||||
"006d", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
"999999", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
|
||||
"0001f4", // pool fee
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
|
||||
"00", // zero2one
|
||||
"006d", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
"000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
|
||||
"000bb8", // pool fee
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"8ad599c3a0ff1de082011efddc58f1908eb6e6d8", // component id
|
||||
"00", // zero2one
|
||||
"00000000000000000000" // padding
|
||||
]
|
||||
.join("");
|
||||
|
||||
assert_eq!(hex_calldata[..520], expected_input);
|
||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,11 +143,14 @@ impl SplitSwapValidator {
|
||||
|
||||
// Build directed graph of token flows
|
||||
let mut graph: HashMap<&Bytes, HashSet<&Bytes>> = HashMap::new();
|
||||
let mut all_tokens = HashSet::new();
|
||||
for swap in swaps {
|
||||
graph
|
||||
.entry(&swap.token_in)
|
||||
.or_default()
|
||||
.insert(&swap.token_out);
|
||||
all_tokens.insert(&swap.token_in);
|
||||
all_tokens.insert(&swap.token_out);
|
||||
}
|
||||
|
||||
// BFS from validation_given
|
||||
@@ -160,8 +163,8 @@ impl SplitSwapValidator {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Early success check
|
||||
if token == checked_token && visited.len() == graph.len() + 1 {
|
||||
// Early success check - if we've reached the checked token and visited all tokens
|
||||
if token == checked_token && visited.len() == all_tokens.len() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -174,6 +177,13 @@ impl SplitSwapValidator {
|
||||
}
|
||||
}
|
||||
|
||||
// After BFS completes, check if both conditions are met:
|
||||
// 1. The checked token is in the visited set
|
||||
// 2. All unique tokens from the swaps are visited
|
||||
if visited.contains(checked_token) && visited.len() == all_tokens.len() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// If we get here, either checked_token wasn't reached or not all tokens were visited
|
||||
if !visited.contains(checked_token) {
|
||||
Err(EncodingError::InvalidInput(
|
||||
@@ -291,6 +301,41 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_path_cyclic_swap() {
|
||||
let validator = SplitSwapValidator;
|
||||
let eth = Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap();
|
||||
let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
|
||||
let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
|
||||
|
||||
let cyclic_swaps = vec![
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "pool1".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: usdc.clone(),
|
||||
token_out: weth.clone(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "pool2".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth.clone(),
|
||||
token_out: usdc.clone(),
|
||||
split: 0f64,
|
||||
},
|
||||
];
|
||||
|
||||
// Test with USDC as both given token and checked token
|
||||
let result = validator.validate_swap_path(&cyclic_swaps, &usdc, &usdc, &None, ð, &weth);
|
||||
assert_eq!(result, Ok(()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_path_unreachable_checked_token() {
|
||||
let validator = SplitSwapValidator;
|
||||
|
||||
@@ -25,7 +25,6 @@ impl SwapEncoderBuilder {
|
||||
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
|
||||
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))),
|
||||
"uniswap_v3" => Ok(Box::new(UniswapV3SwapEncoder::new(self.executor_address))),
|
||||
// TODO replace this with V4 encoder once implemented
|
||||
"uniswap_v4" => Ok(Box::new(UniswapV4SwapEncoder::new(self.executor_address))),
|
||||
_ => Err(EncodingError::FatalError(format!(
|
||||
"Unknown protocol system: {}",
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::Bytes;
|
||||
|
||||
@@ -92,6 +94,50 @@ impl EVMTychoEncoder {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut solution_tokens = vec![];
|
||||
let mut split_tokens_already_considered = HashSet::new();
|
||||
for (i, swap) in solution.swaps.iter().enumerate() {
|
||||
// so we don't count the split tokens more than once
|
||||
if swap.split != 0.0 {
|
||||
if !split_tokens_already_considered.contains(&swap.token_in) {
|
||||
solution_tokens.push(swap.token_in.clone());
|
||||
split_tokens_already_considered.insert(swap.token_in.clone());
|
||||
}
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
if i == solution.swaps.len() - 1 {
|
||||
solution_tokens.push(swap.token_out.clone());
|
||||
}
|
||||
}
|
||||
|
||||
if solution_tokens.len() !=
|
||||
solution_tokens
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<HashSet<Bytes>>()
|
||||
.len()
|
||||
{
|
||||
if let Some(last_swap) = solution.swaps.last() {
|
||||
if solution.swaps[0].token_in != last_swap.token_out {
|
||||
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(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -148,6 +194,14 @@ mod tests {
|
||||
Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap()
|
||||
}
|
||||
|
||||
fn usdc() -> Bytes {
|
||||
Bytes::from_str("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48").unwrap()
|
||||
}
|
||||
|
||||
fn wbtc() -> Bytes {
|
||||
Bytes::from_str("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599").unwrap()
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MockStrategy;
|
||||
|
||||
@@ -436,4 +490,230 @@ 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
|
||||
// (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: 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,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
},
|
||||
];
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: dai(),
|
||||
checked_token: dai(),
|
||||
swaps,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_cyclical_swap_fail() {
|
||||
// This test should fail because the cyclical swap is not the first and last token
|
||||
// DAI -> WETH -> USDC -> DAI -> WBTC
|
||||
// (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: dai(),
|
||||
token_out: weth(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: usdc(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: usdc(),
|
||||
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: wbtc(),
|
||||
split: 0f64,
|
||||
},
|
||||
];
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: dai(),
|
||||
checked_token: wbtc(),
|
||||
swaps,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Cyclical swaps are only allowed if they are the first and last token of a solution".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_validate_cyclical_swap_split_output() {
|
||||
// This validation passes because it is a valid cyclical swap
|
||||
// -> WETH
|
||||
// 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: weth(),
|
||||
checked_token: weth(),
|
||||
swaps,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_cyclical_swap_native_action_fail() {
|
||||
// This validation fails because there is a native action with a valid cyclical swap
|
||||
// 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: 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()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,10 +74,10 @@ pub fn get_min_amount_for_solution(solution: Solution) -> BigUint {
|
||||
if let (Some(expected_amount), Some(slippage)) =
|
||||
(solution.expected_amount.as_ref(), solution.slippage)
|
||||
{
|
||||
let one_hundred = BigUint::from(100u32);
|
||||
let slippage_percent = BigUint::from((slippage * 100.0) as u32);
|
||||
let multiplier = &one_hundred - slippage_percent;
|
||||
let expected_amount_with_slippage = (expected_amount * &multiplier) / &one_hundred;
|
||||
let bps = BigUint::from(10_000u32);
|
||||
let slippage_percent = BigUint::from((slippage * 10000.0) as u32);
|
||||
let multiplier = &bps - slippage_percent;
|
||||
let expected_amount_with_slippage = (expected_amount * &multiplier) / &bps;
|
||||
min_amount_out = max(min_amount_out, expected_amount_with_slippage);
|
||||
}
|
||||
min_amount_out
|
||||
@@ -133,3 +133,27 @@ pub fn get_runtime() -> Result<(Handle, Option<Arc<Runtime>>), EncodingError> {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use num_bigint::BigUint;
|
||||
|
||||
use super::*;
|
||||
use crate::encoding::models::Solution;
|
||||
|
||||
#[test]
|
||||
fn test_min_amount_out_small_slippage() {
|
||||
// Tests that the calculation's precision is high enough to support a slippage of 0.1%.
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_amount: BigUint::from(1000000000000000000u64),
|
||||
checked_amount: None,
|
||||
slippage: Some(0.001f64),
|
||||
expected_amount: Some(BigUint::from(1000000000000000000u64)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let min_amount_out = get_min_amount_for_solution(solution);
|
||||
assert_eq!(min_amount_out, BigUint::from(999000000000000000u64));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user