Merge pull request #114 from propeller-heads/encoding/dc/ENG-4331-cyclical-swap-validation
feat: Add validation for cyclical trades
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user